Jump to content

Allowing user to customize a gui?


Chimaera
 Share

Recommended Posts

Yeah thx i have looked at that but kylomas has done something that's not obvious to me

It did cross my mind whether it was removed so it doesn't affect all the editboxes and treeview?

Ill keep looking..

Edited by Chimaera
Link to comment
Share on other sites

Chimaera,

I like your button toolbar better than the one I made...

One thing I forgot to mention, the treeview does NOT handle case sensitivity for category names.  If you have category names of AA and Aa, for example, and you add another snippet to Aa the snippet will be shown under whichever of these categories appears first.  However, the database is intact and when the app is restarted all of the snippets will be under the correct category.  This is because I am using the function _GUICtrlTreeView_FindItem to find the category name and the function does not handle case sensitivity.  I considered forcing categories to upper case but that seemed like a hoky solution.   

kylomas

edit: I have no idea about resizing...as you can tell GUI's are not my strong suit...

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Chimaera,

Updated code.  The wrong snippet was being returned for duplicately named snippets in different categories.  I am using the code you modified as base code.

;--------------------------------------------------------------------------------------------------------------------------------------
;
; Change Log
;
; 09/15/13
; Chimaera
; Added copy button and reformatted button toolbar.
;
; 09/16/13
; kylomas
; Fixed error selecting duplicately named members from different categories.
;
;--------------------------------------------------------------------------------------------------------------------------------------


#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Run_Obfuscator=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <SQLite.au3>
#include <array.au3>
#include <GuiTreeView.au3>
#include <GuiToolbar.au3>
FileInstall(".\compile\sqlite3.def", @ScriptDir & "\", 1)
FileInstall(".\compile\sqlite3.dll", @ScriptDir & "\", 1)
;---------------------------------------------------------------------------------------
; initialize SQLite and open DB - create table if it does not exist
;---------------------------------------------------------------------------------------
Local $sn_DB = @ScriptDir & '\snippets_datastore.DB3'
_SQLite_Startup()
If @error Then
    ConsoleWrite('error loading sqlite.dll' & @LF)
    Exit
EndIf
Local $hDB = _SQLite_Open($sn_DB)
If @error Then
    ConsoleWrite('Unable to open DB' & @LF)
    _exit()
EndIf
OnAutoItExitRegister("_DbClose")
;  uncomment the following to re-initialize t5he DB

;~ If _SQLite_Exec($hDB, "drop table if exists Categories; drop table if exists Snippets;") <> $sqlite_ok Then
;~  MsgBox($mb_ok, '***** SQL Error *****', 'Drop table error' & @LF & _SQLite_ErrCode() & @LF & _SQLite_ErrMsg())
;~  Exit
;~ EndIf

If _SQLite_Exec($hDB, "create table if not exists Categories (id integer primary key, catname);") <> $sqlite_ok Then
    MsgBox($mb_ok, '***** SQL Error *****', 'Category table create error' & @LF & _SQLite_ErrCode() & @LF & _SQLite_ErrMsg())
    Exit
EndIf

If _SQLite_Exec($hDB, "create table if not exists Snippets (category_id, title, text);") <> $sqlite_ok Then
    MsgBox($mb_ok, '***** SQL Error *****', 'Snippets table create error' & @LF & _SQLite_ErrCode() & @LF & _SQLite_ErrMsg())
    Exit
EndIf

;---------------------------------------------------------------------------------------
; setup gui
;---------------------------------------------------------------------------------------

Local $gui010   =   GUICreate('Snippet Manager', 900, 700, default, default, default, $ws_ex_acceptfiles)
local $hToolBar =   _guictrltoolbar_create($gui010)
local $aSize    =   _GUICtrlToolbar_GetMaxSize($hToolbar)

Local $iStyle   =   BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)
Local $hTV      =   _GUICtrlTreeView_Create($gui010, 20, $aSize[1]+80, 200, 580, $iStyle, $WS_EX_CLIENTEDGE)
                    _GUICtrlTreeView_SetTextColor($hTV, 0x0000ff)
                    _GUICtrlTreeView_SetLineColor($hTV, 0xaa0000)
local $hImage   =   _GUIImageList_Create(16, 16, 5, 3)
                    _GUIImageList_AddIcon($hImage, "shell32.dll", 3)
                    _GUIImageList_AddIcon($hImage, "shell32.dll", 19)
                    _GUIImageList_AddIcon($hImage, "shell32.dll", 70)
                    _GUIImageList_AddIcon($hImage, "shell32.dll", 55)
                    _GUIImageList_AddIcon($hImage, "shell32.dll", 165)
                    _GUICtrlTreeView_SetNormalImageList($hTV, $hImage)

                    guictrlcreatelabel('Category',20,58,80,15)
                    guictrlsetfont(-1,8,800,default,'Tahoma')
local $catin    =   guictrlcreateinput('',20,75,200,20)
                    guictrlsetfont(-1,10,600)
                    guictrlcreatelabel('Title',250,58,40,15)
                    guictrlsetfont(-1,8,800,default,'Tahoma')
local $titin    =   guictrlcreateinput('',250,75,400,20)
                    guictrlsetfont(-1,10,600)
local $editok   =   guictrlcreatecheckbox('Allow Edit',750,$aSize[1]+50,100,20)
                    guictrlsetfont(-1,10,800,default,'Tahoma')
Local $edt010   =   GUICtrlCreateEdit('', 250, $aSize[1]+80, 630, 580, bitor($es_readonly,$ss_sunken,$ws_vscroll,$ws_hscroll))
                    guictrlsetstate($edt010,$GUI_DROPACCEPTED)
                    GUICtrlSetFont($edt010, 8.5, 800, -1, 'Tahoma')
                    guictrlcreatelabel('Status',500,15,50,20)
                    guictrlsetfont(-1,10,800,default,'Tahoma')
local $status   =   guictrlcreatelabel('',550,15,330,20,$ss_sunken)
                    guictrlsetfont(-1,10,800,default,'Tahoma')
                    guictrlsetcolor($status,0xaa0000)

Local $aStrings[5]
local Enum $cDEL = 1000, $sADD, $sDEL, $sSAV, $sCOP
_GUICtrlToolbar_AddBitmap($hToolbar, 1, -1, $IDB_STD_LARGE_COLOR)

$aStrings[0] = _GUICtrlToolbar_AddString($hToolbar, "Delete Category")
$aStrings[1] = _GUICtrlToolbar_AddString($hToolbar, "Add Snippet")
$aStrings[2] = _GUICtrlToolbar_AddString($hToolbar, "Delete Snippet")
$aStrings[3] = _GUICtrlToolbar_AddString($hToolbar, "Update Snippet")
$aStrings[4] = _GUICtrlToolbar_AddString($hToolbar, "Copy Snippet")
_GUICtrlToolbar_AddButton($hToolbar, $sCOP, $STD_COPY, $aStrings[4]) ; Copy
_GUICtrlToolbar_AddButtonSep($hToolbar)
_GUICtrlToolbar_AddButton($hToolbar, $sADD, $STD_FILENEW, $aStrings[1]) ; Add
_GUICtrlToolbar_AddButtonSep($hToolbar)
_GUICtrlToolbar_AddButton($hToolbar, $sSAV, $STD_FILESAVE, $aStrings[3]) ; Save / Update
_GUICtrlToolbar_AddButtonSep($hToolbar)
_GUICtrlToolbar_AddButton($hToolbar, $sDEL, $STD_DELETE, $aStrings[2]) ; Delete Snippet
_GUICtrlToolbar_AddButtonSep($hToolbar)
_GUICtrlToolbar_AddButton($hToolbar, $cDEL, $STD_DELETE, $aStrings[0]) ; Delete Category
_GUICtrlToolbar_AddButtonSep($hToolbar)
GUISetState()

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

_populate_treeview()

Func _populate_treeview()

    Local $crows, $cnbrows, $cnbcols, $cret, $srows, $snbrows, $snbcols, $sret, $hItem, $hT

    $cret = _SQLite_GetTable2d(-1, "select * from Categories;", $crows, $cnbrows, $cnbcols)
    If @error Then
        MsgBox($mb_ok, '***** ERROR *****', 'Error getting categories' & @LF & _SQLite_ErrCode() & @LF & _SQLite_ErrMsg())
        Exit
    EndIf

    _GUICtrlTreeView_BeginUpdate($hTV)

    For $1 = 1 To $cnbrows

        ;$hItem = _GUICtrlTreeView_Add($hTV, 0, $crows[$1][1], $1)
        $hItem = _GUICtrlTreeView_Add($hTV, 0, $crows[$1][1], 0,1)
        _GUICtrlTreeView_SetBold($hTV, $hItem)

        $sret = _SQLite_GetTable2d(-1, "select * from snippets where category_id = " & $crows[$1][0] & ';', $srows, $snbrows, $snbcols)

        ;_arraydisplay($srows)

        For $2 = 1 To $snbrows
            $hT = _GUICtrlTreeView_AddChild($hTV, $hItem, $srows[$2][1],2,3)
        Next

    Next

    _guictrltreeview_sort($hTV)

    _GUICtrlTreeView_EndUpdate($hTV)

EndFunc   ;==>_populate_treeview

local $dropped_files

While 1
    Switch GUIGetMsg()
        Case $gui_event_close
            Exit
        Case $editok
            if guictrlread($editok) <> $gui_checked then
                guictrlsetstyle($edt010,$es_readonly)
            else
                guictrlsetstyle($edt010,bitor($ES_MULTILINE, $WS_TABSTOP, $es_wantreturn, not $es_readonly))
            endif
        Case $GUI_EVENT_DROPPED
            guictrlsetdata($catin,'')
            guictrlsetdata($titin,'')
            Switch @GUI_DropId
                Case $edt010
                    guictrlsetdata($edt010,'')
                    guictrlsetdata($edt010,fileread(stringreplace(@gui_dragfile,@crlf,'')),1)
            endswitch
   EndSwitch
WEnd

Func _add_snippet()

    local $trows, $tret

    if guictrlread($edt010) = '' then
        guictrlsetdata($status,'  No snippet to add...')
        Return 1
    EndIf

    if guictrlread($catin) = '' or guictrlread($titin) = '' then
        guictrlsetdata($status,'  Enter Category and Title...')
        return 1
    endif

    local $cat = _SQLite_FastEscape(guictrlread($catin))
    local $tit = _SQLite_FastEscape(guictrlread($titin))

    ;  add cat if it does not exist

    $tret = _SQLite_QuerySingleRow( $hDB, 'select id from categories where catname = ' & $cat & ';',$trows)
    if $tret <> $sqlite_ok then
        _sqlite_exec($hDB,'insert into categories (catname) values(' & $cat & ');')
        $tret = _SQLite_QuerySingleRow( $hDB, 'select id from categories where catname = ' & $cat & ';',$trows)
        if $tret <> $sqlite_ok then
            MsgBox($mb_ok, '***** ERROR *****', 'Error inserting category name' & @LF & _SQLite_ErrCode() & @LF & _SQLite_ErrMsg())
            Exit
        EndIf
    EndIf

    local $tcat = $trows[0]

    ;  determine if cat/title exists

    $tret = _SQLite_QuerySingleRow( $hDB, _
                                'select title,category_id from snippets where title = ' & $tit & ' and category_id = ' & $tcat & ';',$trows)

    if $tret = $sqlite_ok then

        guictrlsetdata($status,'  Snippet already exists...specify another name...')
        guictrlsetstate($titin,$gui_focus)
        return 1

    EndIf

    ; insert snippet record

    if _sqlite_exec($hDB,'insert into snippets (category_id,title,text) values(' & $tcat & ',' & $tit & ',' & _
        _SQLite_FastEscape(guictrlread($edt010)) & ');') <> $sqlite_ok then
            MsgBox($mb_ok, '***** ERROR *****', 'Insert error for snippet add' & @LF & _SQLite_ErrCode() & @LF & _SQLite_ErrMsg())
            Exit
    EndIf

    guictrlsetdata($status,guictrlread($catin) & '\' & guictrlread($titin) & ' added...')

    ; update treeview control

    local $tHND = _GUICtrlTreeView_FindItem($hTV,guictrlread($catin))

;~  ConsoleWrite(stringformat('%-30s = %-30s\n%-30s = %-30s', _
;~                              'value from gettext', _
;~                              _guictrltreeview_gettext($hTV,$tHND), _
;~                              'value from guictrlread', _
;~                              guictrlread($catin)) & @LF)

;~  if _guictrltreeview_gettext($hTV,$tHND) == guictrlread($catin) then
;~  Else
;~      $tHND = 0
;~  endif

    if $tHND <> 0 then
        _GUICtrlTreeView_AddChild($hTV, $tHND, guictrlread($titin),2,3)
    Else
        local $tH = _GUICtrlTreeView_Add($hTV,0,guictrlread($catin),0,1)
        _GUICtrlTreeView_AddChild($hTV,$tH,guictrlread($titin),2,3)
        _guictrltreeview_expand($hTV,$tH)
    endif

    _guictrltreeview_sort($hTV)

EndFunc

Func _del_snippet()

    local $trows, $tret

    if guictrlread($catin) = '' or guictrlread($titin) = '' then
        guictrlsetdata($status,'  Enter Category and Title...')
        return 1
    endif

    local $cat = _SQLite_FastEscape(guictrlread($catin))
    local $tit = _SQLite_FastEscape(guictrlread($titin))

    ;  get category

    $tret = _SQLite_QuerySingleRow( $hDB, 'select id from categories where catname = ' & $cat & ';',$trows)
    if $tret <> $sqlite_ok then
        guictrlsetdata($status,'Category does NOT exist...respecify...')
        guictrlsetstate($catin,$gui_focus)
        return 1
    EndIf

    local $tcat = $trows[0]

    ;  determine if cat/title exists

    $tret = _SQLite_QuerySingleRow( $hDB, 'select title,category_id from snippets where title = ' & $tit & ' and category_id = ' & $tcat & ';',$trows)

    if $tret <> $sqlite_ok then
        guictrlsetdata($status,'  Snippet does NOT exist...respecify...')
        guictrlsetstate($titin,$gui_focus)
        return
    EndIf

    ; delete snippet record

    if _sqlite_exec($hDB,'delete from snippets where category_id = ' & $tcat & ' and title = ' & $tit & ';') <> $sqlite_ok then
            MsgBox($mb_ok, '***** ERROR *****', 'Delete Error' & @LF & _SQLite_ErrCode() & @LF & _SQLite_ErrMsg())
            Exit
    EndIf

    guictrlsetdata($status,guictrlread($catin) & '\' & guictrlread($titin) & ' deleted...')

    ; update treeview control

    local $tHND = _GUICtrlTreeView_FindItemEx($hTV, guictrlread($catin) & '|' & guictrlread($titin))

    if $tHND <> 0 then
        $tret = _GUICtrlTreeView_Delete($hTV, $tHND)
    endif

EndFunc

Func _del_cat()

    local $trows, $tret

    if _GUICtrlTreeView_GetSelection($hTV) = 0 or _GUICtrlTreeView_Level($hTV, _GUICtrlTreeView_GetSelection($hTV)) <> 0 then
        guictrlsetdata($status,'No category selected...')
        return
    Else
        local $cat = _sqlite_escape(_GUICtrlTreeView_GetText($hTV, _GUICtrlTreeView_GetSelection($hTV)))
    endif

    $tret = _SQLite_QuerySingleRow( $hDB, 'select id from categories where catname = ' & $cat & ';',$trows)
    if $tret <> $sqlite_ok then
        guictrlsetdata($status,'Category does NOT exist...respecify...')
        return 1
    EndIf

    local $tcat = $trows[0]

    ; delete snippet and category records

    if _sqlite_exec($hDB,'delete from snippets where category_id = ' & $tcat & ';') <> $sqlite_ok then
            MsgBox($mb_ok, '***** ERROR *****', 'Delete Error for snippets on category delete' & @LF & _SQLite_ErrCode() & @LF & _SQLite_ErrMsg())
            Exit
    EndIf

    if _sqlite_exec($hDB,'delete from categories where id = ' & $tcat & ';') <> $sqlite_ok then
            MsgBox($mb_ok, '***** ERROR *****', 'Delete Error for category on category delete' & @LF & _SQLite_ErrCode() & @LF & _SQLite_ErrMsg())
            Exit
    EndIf

    guictrlsetdata($status,'  Category = ' & $cat & ' deleted...')

    ; update treeview control

    local $tHND = _GUICtrlTreeView_FindItem($hTV, stringreplace($cat,"'",''))

    if $tHND <> 0 then
        $tret = _GUICtrlTreeView_Delete($hTV, $tHND)
    endif

    ;_guictrltreeview_sort($hTV)

EndFunc

Func _sav_snippet()

    local $trows, $tret

    if guictrlread($edt010) = '' then
        guictrlsetdata($status,'  No snippet to save...')
        Return 1
    EndIf

    if guictrlread($catin) = '' or guictrlread($titin) = '' then
        guictrlsetdata($status,'  Enter Category and Title...')
        return 1
    endif

    local $cat = _SQLite_FastEscape(guictrlread($catin))
    local $tit = _SQLite_FastEscape(guictrlread($titin))

    ;  if category does not exist then call _add_snippet() routine

    $tret = _SQLite_QuerySingleRow( $hDB, 'select id from categories where catname = ' & $cat & ';',$trows)
    if $tret <> $sqlite_ok then
        _add_snippet()
        return
    else
        local $tcat = $trows[0]
    EndIf

    ;  if cat\title does not exist then call _add_snippet() routine

    $tret = _SQLite_QuerySingleRow( $hDB, _
                                'select title,category_id from snippets where title = ' & $tit & ' and category_id = ' & $tcat & ';',$trows)

    if $tret <> $sqlite_ok then
        _add_snippet()
        return
    EndIf

    ; update snippet record

    if _sqlite_exec($hDB,'update snippets set text = ' & _
        _SQLite_FastEscape(guictrlread($edt010)) & ' where category_id = ' & $tcat & ' and title = ' & $tit & ';') <> $sqlite_ok then
            MsgBox($mb_ok, '***** ERROR *****', 'Insert error for category add' & @LF & _SQLite_ErrCode() & @LF & _SQLite_ErrMsg())
            Exit
    Else
        guictrlsetdata($status,guictrlread($catin) & '\' & guictrlread($titin) & ' updated...')
    EndIf

EndFunc

Func _copy_snippet()

        If guictrlread($edt010) = '' then
            guictrlsetdata($status,'  No snippet to copy...')
            Return 1
        Else
            ClipPut(guictrlread($edt010))
        EndIf

EndFunc ; <==  _copy_snippet()


Func _DbClose()
    _SQLite_Close($hDB)
EndFunc   ;==>_DbClose

Func _exit()
    _SQLite_Close()
    _SQLite_Shutdown()
    Exit
EndFunc   ;==>_exit

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview, $arow
    $hWndTreeview = $hTV
    If Not IsHWnd($hTV) Then $hWndTreeview = GUICtrlGetHandle($hTV)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndTreeview
            Switch $iCode
                Case $NM_DBLCLK, $TVN_SELCHANGEDW

                    guictrlsetdata($edt010,'')

                    Local $tname = _GUICtrlTreeView_GetText($hTV, _GUICtrlTreeView_GetSelection($hTV))

                    if _GUICtrlTreeView_Level($hTV, _GUICtrlTreeView_GetSelection($hTV)) = 0 then
                        guictrlsetdata($catin,$tname)
                        guictrlsetdata($titin,'')
                    else
                        $cat = _GUICtrlTreeView_GetText($htv,_GUICtrlTreeView_GetParentHandle($hTV))
                        _SQLite_QuerySingleRow( $hDB, 'select id from categories where catname = ' & _sqlite_fastescape($cat) & ';',$arow)
                        _SQLite_QuerySingleRow(-1, "SELECT text from snippets where title = " & _SQLite_FastEscape($tname) & _
                                " and category_id = " & $arow[0] & ";", $arow)
                        GUICtrlSetData($edt010, $arow[0])
                        guictrlsetdata($titin,$tname)
                        guictrlsetdata($catin, _GUICtrlTreeView_GetText($htv,_GUICtrlTreeView_GetParentHandle($hTV)))
                    endif

                    Return
            EndSwitch

        Case $hToolbar
            Switch $icode
                Case $NM_LDOWN
                    local $tNMMOUSE
                    $tNMMOUSE = dllstructcreate($tagNMMOUSE,$ilparam)
                    local $cID = dllstructgetdata($tNMMOUSE,'ItemSpec')
                    Switch $cID
                        Case $cDEL
                            _del_cat()
                        Case $sADD
                            _add_snippet()
                        Case $sSAV
                            _sav_snippet()
                        Case $sDEL
                            _del_snippet()
            Case $sCOP
                 _copy_snippet()
                    EndSwitch
            EndSwitch

    EndSwitch

EndFunc   ;==>WM_NOTIFY

kylomas

edit: removed console write from code

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Thx for the update m8

I will go through and copy across the changes as ive changed it loads since the intial start

Im curious if it would be possible to get a copy selection, im not sure how copy works on a small part of the whole?

Link to comment
Share on other sites

Chimaera,

Added logic to copy only the selection if any text is selected, otherwise copy all text in the edit control.  See comments in the code.

;--------------------------------------------------------------------------------------------------------------------------------------
;
; Change Log
;
; 09/15/13
; Chimaera
; Added copy button and reformatted button toolbar.
;
; 09/16/13
; kylomas
; Fixed error selecting duplicately named members from different categories.
;
; 09/17/13
; kylomas
; Added code to copy selection only if any text is selected, otherwise copy all text in the edit control
;

;--------------------------------------------------------------------------------------------------------------------------------------


#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Run_Obfuscator=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <SQLite.au3>
#include <array.au3>
#include <GuiTreeView.au3>
#include <GuiToolbar.au3>
#include <GuiEdit.au3>

FileInstall(".\compile\sqlite3.def", @ScriptDir & "\", 1)
FileInstall(".\compile\sqlite3.dll", @ScriptDir & "\", 1)
;---------------------------------------------------------------------------------------
; initialize SQLite and open DB - create table if it does not exist
;---------------------------------------------------------------------------------------
Local $sn_DB = @ScriptDir & '\snippets_datastore.DB3'
_SQLite_Startup()
If @error Then
    ConsoleWrite('error loading sqlite.dll' & @LF)
    Exit
EndIf
Local $hDB = _SQLite_Open($sn_DB)
If @error Then
    ConsoleWrite('Unable to open DB' & @LF)
    _exit()
EndIf
OnAutoItExitRegister("_DbClose")
;  uncomment the following to re-initialize t5he DB

;~ If _SQLite_Exec($hDB, "drop table if exists Categories; drop table if exists Snippets;") <> $sqlite_ok Then
;~  MsgBox($mb_ok, '***** SQL Error *****', 'Drop table error' & @LF & _SQLite_ErrCode() & @LF & _SQLite_ErrMsg())
;~  Exit
;~ EndIf

If _SQLite_Exec($hDB, "create table if not exists Categories (id integer primary key, catname);") <> $sqlite_ok Then
    MsgBox($mb_ok, '***** SQL Error *****', 'Category table create error' & @LF & _SQLite_ErrCode() & @LF & _SQLite_ErrMsg())
    Exit
EndIf

If _SQLite_Exec($hDB, "create table if not exists Snippets (category_id, title, text);") <> $sqlite_ok Then
    MsgBox($mb_ok, '***** SQL Error *****', 'Snippets table create error' & @LF & _SQLite_ErrCode() & @LF & _SQLite_ErrMsg())
    Exit
EndIf

;---------------------------------------------------------------------------------------
; setup gui
;---------------------------------------------------------------------------------------

Local $gui010   =   GUICreate('Snippet Manager', 900, 700, default, default, default, $ws_ex_acceptfiles)
local $hToolBar =   _guictrltoolbar_create($gui010)
local $aSize    =   _GUICtrlToolbar_GetMaxSize($hToolbar)

Local $iStyle   =   BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)
Local $hTV      =   _GUICtrlTreeView_Create($gui010, 20, $aSize[1]+80, 200, 580, $iStyle, $WS_EX_CLIENTEDGE)
                    _GUICtrlTreeView_SetTextColor($hTV, 0x0000ff)
                    _GUICtrlTreeView_SetLineColor($hTV, 0xaa0000)
local $hImage   =   _GUIImageList_Create(16, 16, 5, 3)
                    _GUIImageList_AddIcon($hImage, "shell32.dll", 3)
                    _GUIImageList_AddIcon($hImage, "shell32.dll", 19)
                    _GUIImageList_AddIcon($hImage, "shell32.dll", 70)
                    _GUIImageList_AddIcon($hImage, "shell32.dll", 55)
                    _GUIImageList_AddIcon($hImage, "shell32.dll", 165)
                    _GUICtrlTreeView_SetNormalImageList($hTV, $hImage)

                    guictrlcreatelabel('Category',20,58,80,15)
                    guictrlsetfont(-1,8,800,default,'Tahoma')
local $catin    =   guictrlcreateinput('',20,75,200,20)
                    guictrlsetfont(-1,10,600)
                    guictrlcreatelabel('Title',250,58,40,15)
                    guictrlsetfont(-1,8,800,default,'Tahoma')
local $titin    =   guictrlcreateinput('',250,75,400,20)
                    guictrlsetfont(-1,10,600)
local $editok   =   guictrlcreatecheckbox('Allow Edit',750,$aSize[1]+50,100,20)
                    guictrlsetfont(-1,10,800,default,'Tahoma')
Local $edt010   =   GUICtrlCreateEdit('', 250, $aSize[1]+80, 630, 580, bitor($es_readonly,$ss_sunken,$ws_vscroll,$ws_hscroll))
                    guictrlsetstate($edt010,$GUI_DROPACCEPTED)
                    GUICtrlSetFont($edt010, 8.5, 800, -1, 'Tahoma')
                    guictrlcreatelabel('Status',500,15,50,20)
                    guictrlsetfont(-1,10,800,default,'Tahoma')
local $status   =   guictrlcreatelabel('',550,15,330,20,$ss_sunken)
                    guictrlsetfont(-1,10,800,default,'Tahoma')
                    guictrlsetcolor($status,0xaa0000)

Local $aStrings[5]
local Enum $cDEL = 1000, $sADD, $sDEL, $sSAV, $sCOP
_GUICtrlToolbar_AddBitmap($hToolbar, 1, -1, $IDB_STD_LARGE_COLOR)

$aStrings[0] = _GUICtrlToolbar_AddString($hToolbar, "Delete Category")
$aStrings[1] = _GUICtrlToolbar_AddString($hToolbar, "Add Snippet")
$aStrings[2] = _GUICtrlToolbar_AddString($hToolbar, "Delete Snippet")
$aStrings[3] = _GUICtrlToolbar_AddString($hToolbar, "Update Snippet")
$aStrings[4] = _GUICtrlToolbar_AddString($hToolbar, "Copy Snippet")
_GUICtrlToolbar_AddButton($hToolbar, $sCOP, $STD_COPY, $aStrings[4]) ; Copy
_GUICtrlToolbar_AddButtonSep($hToolbar)
_GUICtrlToolbar_AddButton($hToolbar, $sADD, $STD_FILENEW, $aStrings[1]) ; Add
_GUICtrlToolbar_AddButtonSep($hToolbar)
_GUICtrlToolbar_AddButton($hToolbar, $sSAV, $STD_FILESAVE, $aStrings[3]) ; Save / Update
_GUICtrlToolbar_AddButtonSep($hToolbar)
_GUICtrlToolbar_AddButton($hToolbar, $sDEL, $STD_DELETE, $aStrings[2]) ; Delete Snippet
_GUICtrlToolbar_AddButtonSep($hToolbar)
_GUICtrlToolbar_AddButton($hToolbar, $cDEL, $STD_DELETE, $aStrings[0]) ; Delete Category
_GUICtrlToolbar_AddButtonSep($hToolbar)
GUISetState()

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

_populate_treeview()

Func _populate_treeview()

    Local $crows, $cnbrows, $cnbcols, $cret, $srows, $snbrows, $snbcols, $sret, $hItem, $hT

    $cret = _SQLite_GetTable2d(-1, "select * from Categories;", $crows, $cnbrows, $cnbcols)
    If @error Then
        MsgBox($mb_ok, '***** ERROR *****', 'Error getting categories' & @LF & _SQLite_ErrCode() & @LF & _SQLite_ErrMsg())
        Exit
    EndIf

    _GUICtrlTreeView_BeginUpdate($hTV)

    For $1 = 1 To $cnbrows

        ;$hItem = _GUICtrlTreeView_Add($hTV, 0, $crows[$1][1], $1)
        $hItem = _GUICtrlTreeView_Add($hTV, 0, $crows[$1][1], 0,1)
        _GUICtrlTreeView_SetBold($hTV, $hItem)

        $sret = _SQLite_GetTable2d(-1, "select * from snippets where category_id = " & $crows[$1][0] & ';', $srows, $snbrows, $snbcols)

        ;_arraydisplay($srows)

        For $2 = 1 To $snbrows
            $hT = _GUICtrlTreeView_AddChild($hTV, $hItem, $srows[$2][1],2,3)
        Next

    Next

    _guictrltreeview_sort($hTV)

    _GUICtrlTreeView_EndUpdate($hTV)

EndFunc   ;==>_populate_treeview

local $dropped_files

While 1
    Switch GUIGetMsg()
        Case $gui_event_close
            Exit
        Case $editok
            if guictrlread($editok) <> $gui_checked then
                guictrlsetstyle($edt010,$es_readonly)
            else
                guictrlsetstyle($edt010,bitor($ES_MULTILINE, $WS_TABSTOP, $es_wantreturn, not $es_readonly))
            endif
        Case $GUI_EVENT_DROPPED
            guictrlsetdata($catin,'')
            guictrlsetdata($titin,'')
            Switch @GUI_DropId
                Case $edt010
                    guictrlsetdata($edt010,'')
                    guictrlsetdata($edt010,fileread(stringreplace(@gui_dragfile,@crlf,'')),1)
            endswitch
   EndSwitch
WEnd

Func _add_snippet()

    local $trows, $tret

    if guictrlread($edt010) = '' then
        guictrlsetdata($status,'  No snippet to add...')
        Return 1
    EndIf

    if guictrlread($catin) = '' or guictrlread($titin) = '' then
        guictrlsetdata($status,'  Enter Category and Title...')
        return 1
    endif

    local $cat = _SQLite_FastEscape(guictrlread($catin))
    local $tit = _SQLite_FastEscape(guictrlread($titin))

    ;  add cat if it does not exist

    $tret = _SQLite_QuerySingleRow( $hDB, 'select id from categories where catname = ' & $cat & ';',$trows)
    if $tret <> $sqlite_ok then
        _sqlite_exec($hDB,'insert into categories (catname) values(' & $cat & ');')
        $tret = _SQLite_QuerySingleRow( $hDB, 'select id from categories where catname = ' & $cat & ';',$trows)
        if $tret <> $sqlite_ok then
            MsgBox($mb_ok, '***** ERROR *****', 'Error inserting category name' & @LF & _SQLite_ErrCode() & @LF & _SQLite_ErrMsg())
            Exit
        EndIf
    EndIf

    local $tcat = $trows[0]

    ;  determine if cat/title exists

    $tret = _SQLite_QuerySingleRow( $hDB, _
                                'select title,category_id from snippets where title = ' & $tit & ' and category_id = ' & $tcat & ';',$trows)

    if $tret = $sqlite_ok then

        guictrlsetdata($status,'  Snippet already exists...specify another name...')
        guictrlsetstate($titin,$gui_focus)
        return 1

    EndIf

    ; insert snippet record

    if _sqlite_exec($hDB,'insert into snippets (category_id,title,text) values(' & $tcat & ',' & $tit & ',' & _
        _SQLite_FastEscape(guictrlread($edt010)) & ');') <> $sqlite_ok then
            MsgBox($mb_ok, '***** ERROR *****', 'Insert error for snippet add' & @LF & _SQLite_ErrCode() & @LF & _SQLite_ErrMsg())
            Exit
    EndIf

    guictrlsetdata($status,guictrlread($catin) & '\' & guictrlread($titin) & ' added...')

    ; update treeview control

    local $tHND = _GUICtrlTreeView_FindItem($hTV,guictrlread($catin))

;~  ConsoleWrite(stringformat('%-30s = %-30s\n%-30s = %-30s', _
;~                              'value from gettext', _
;~                              _guictrltreeview_gettext($hTV,$tHND), _
;~                              'value from guictrlread', _
;~                              guictrlread($catin)) & @LF)

;~  if _guictrltreeview_gettext($hTV,$tHND) == guictrlread($catin) then
;~  Else
;~      $tHND = 0
;~  endif

    if $tHND <> 0 then
        _GUICtrlTreeView_AddChild($hTV, $tHND, guictrlread($titin),2,3)
    Else
        local $tH = _GUICtrlTreeView_Add($hTV,0,guictrlread($catin),0,1)
        _GUICtrlTreeView_AddChild($hTV,$tH,guictrlread($titin),2,3)
        _guictrltreeview_expand($hTV,$tH)
    endif

    _guictrltreeview_sort($hTV)

EndFunc

Func _del_snippet()

    local $trows, $tret

    if guictrlread($catin) = '' or guictrlread($titin) = '' then
        guictrlsetdata($status,'  Enter Category and Title...')
        return 1
    endif

    local $cat = _SQLite_FastEscape(guictrlread($catin))
    local $tit = _SQLite_FastEscape(guictrlread($titin))

    ;  get category

    $tret = _SQLite_QuerySingleRow( $hDB, 'select id from categories where catname = ' & $cat & ';',$trows)
    if $tret <> $sqlite_ok then
        guictrlsetdata($status,'Category does NOT exist...respecify...')
        guictrlsetstate($catin,$gui_focus)
        return 1
    EndIf

    local $tcat = $trows[0]

    ;  determine if cat/title exists

    $tret = _SQLite_QuerySingleRow( $hDB, 'select title,category_id from snippets where title = ' & $tit & ' and category_id = ' & $tcat & ';',$trows)

    if $tret <> $sqlite_ok then
        guictrlsetdata($status,'  Snippet does NOT exist...respecify...')
        guictrlsetstate($titin,$gui_focus)
        return
    EndIf

    ; delete snippet record

    if _sqlite_exec($hDB,'delete from snippets where category_id = ' & $tcat & ' and title = ' & $tit & ';') <> $sqlite_ok then
            MsgBox($mb_ok, '***** ERROR *****', 'Delete Error' & @LF & _SQLite_ErrCode() & @LF & _SQLite_ErrMsg())
            Exit
    EndIf

    guictrlsetdata($status,guictrlread($catin) & '\' & guictrlread($titin) & ' deleted...')

    ; update treeview control

    local $tHND = _GUICtrlTreeView_FindItemEx($hTV, guictrlread($catin) & '|' & guictrlread($titin))

    if $tHND <> 0 then
        $tret = _GUICtrlTreeView_Delete($hTV, $tHND)
    endif

EndFunc

Func _del_cat()

    local $trows, $tret

    if _GUICtrlTreeView_GetSelection($hTV) = 0 or _GUICtrlTreeView_Level($hTV, _GUICtrlTreeView_GetSelection($hTV)) <> 0 then
        guictrlsetdata($status,'No category selected...')
        return
    Else
        local $cat = _sqlite_escape(_GUICtrlTreeView_GetText($hTV, _GUICtrlTreeView_GetSelection($hTV)))
    endif

    $tret = _SQLite_QuerySingleRow( $hDB, 'select id from categories where catname = ' & $cat & ';',$trows)
    if $tret <> $sqlite_ok then
        guictrlsetdata($status,'Category does NOT exist...respecify...')
        return 1
    EndIf

    local $tcat = $trows[0]

    ; delete snippet and category records

    if _sqlite_exec($hDB,'delete from snippets where category_id = ' & $tcat & ';') <> $sqlite_ok then
            MsgBox($mb_ok, '***** ERROR *****', 'Delete Error for snippets on category delete' & @LF & _SQLite_ErrCode() & @LF & _SQLite_ErrMsg())
            Exit
    EndIf

    if _sqlite_exec($hDB,'delete from categories where id = ' & $tcat & ';') <> $sqlite_ok then
            MsgBox($mb_ok, '***** ERROR *****', 'Delete Error for category on category delete' & @LF & _SQLite_ErrCode() & @LF & _SQLite_ErrMsg())
            Exit
    EndIf

    guictrlsetdata($status,'  Category = ' & $cat & ' deleted...')

    ; update treeview control

    local $tHND = _GUICtrlTreeView_FindItem($hTV, stringreplace($cat,"'",''))

    if $tHND <> 0 then
        $tret = _GUICtrlTreeView_Delete($hTV, $tHND)
    endif

    ;_guictrltreeview_sort($hTV)

EndFunc

Func _sav_snippet()

    local $trows, $tret

    if guictrlread($edt010) = '' then
        guictrlsetdata($status,'  No snippet to save...')
        Return 1
    EndIf

    if guictrlread($catin) = '' or guictrlread($titin) = '' then
        guictrlsetdata($status,'  Enter Category and Title...')
        return 1
    endif

    local $cat = _SQLite_FastEscape(guictrlread($catin))
    local $tit = _SQLite_FastEscape(guictrlread($titin))

    ;  if category does not exist then call _add_snippet() routine

    $tret = _SQLite_QuerySingleRow( $hDB, 'select id from categories where catname = ' & $cat & ';',$trows)
    if $tret <> $sqlite_ok then
        _add_snippet()
        return
    else
        local $tcat = $trows[0]
    EndIf

    ;  if cat\title does not exist then call _add_snippet() routine

    $tret = _SQLite_QuerySingleRow( $hDB, _
                                'select title,category_id from snippets where title = ' & $tit & ' and category_id = ' & $tcat & ';',$trows)

    if $tret <> $sqlite_ok then
        _add_snippet()
        return
    EndIf

    ; update snippet record

    if _sqlite_exec($hDB,'update snippets set text = ' & _
        _SQLite_FastEscape(guictrlread($edt010)) & ' where category_id = ' & $tcat & ' and title = ' & $tit & ';') <> $sqlite_ok then
            MsgBox($mb_ok, '***** ERROR *****', 'Insert error for category add' & @LF & _SQLite_ErrCode() & @LF & _SQLite_ErrMsg())
            Exit
    Else
        guictrlsetdata($status,guictrlread($catin) & '\' & guictrlread($titin) & ' updated...')
    EndIf

EndFunc

Func _copy_snippet()

        If guictrlread($edt010) = '' then
            guictrlsetdata($status,'  No snippet to copy...')
            Return 1
        Else

            ; start of modified code for get selection

            local $sText = guictrlread($edt010)
            local $aPos = _GUICtrlEdit_GetSel($edt010)

            if $aPos[0] < $aPos[1] then
                clipput(stringmid($sText,$aPos[0]+1,($aPos[1]-$aPos[0])+1))
            else
                ClipPut(guictrlread($edt010))
            endif

            ; end of modified code for get selection

        EndIf

EndFunc ; <==  _copy_snippet()


Func _DbClose()
    _SQLite_Close($hDB)
EndFunc   ;==>_DbClose

Func _exit()
    _SQLite_Close()
    _SQLite_Shutdown()
    Exit
EndFunc   ;==>_exit

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview, $arow
    $hWndTreeview = $hTV
    If Not IsHWnd($hTV) Then $hWndTreeview = GUICtrlGetHandle($hTV)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndTreeview
            Switch $iCode
                Case $NM_DBLCLK, $TVN_SELCHANGEDW

                    guictrlsetdata($edt010,'')

                    Local $tname = _GUICtrlTreeView_GetText($hTV, _GUICtrlTreeView_GetSelection($hTV))

                    if _GUICtrlTreeView_Level($hTV, _GUICtrlTreeView_GetSelection($hTV)) = 0 then
                        guictrlsetdata($catin,$tname)
                        guictrlsetdata($titin,'')
                    else
                        $cat = _GUICtrlTreeView_GetText($htv,_GUICtrlTreeView_GetParentHandle($hTV))
                        _SQLite_QuerySingleRow( $hDB, 'select id from categories where catname = ' & _sqlite_fastescape($cat) & ';',$arow)
                        _SQLite_QuerySingleRow(-1, "SELECT text from snippets where title = " & _SQLite_FastEscape($tname) & _
                                " and category_id = " & $arow[0] & ";", $arow)
                        GUICtrlSetData($edt010, $arow[0])
                        guictrlsetdata($titin,$tname)
                        guictrlsetdata($catin, _GUICtrlTreeView_GetText($htv,_GUICtrlTreeView_GetParentHandle($hTV)))
                    endif

                    Return
            EndSwitch

        Case $hToolbar
            Switch $icode
                Case $NM_LDOWN
                    local $tNMMOUSE
                    $tNMMOUSE = dllstructcreate($tagNMMOUSE,$ilparam)
                    local $cID = dllstructgetdata($tNMMOUSE,'ItemSpec')
                    Switch $cID
                        Case $cDEL
                            _del_cat()
                        Case $sADD
                            _add_snippet()
                        Case $sSAV
                            _sav_snippet()
                        Case $sDEL
                            _del_snippet()
            Case $sCOP
                 _copy_snippet()
                    EndSwitch
            EndSwitch

    EndSwitch

EndFunc   ;==>WM_NOTIFY

I think it would be better if we were both modifying a single piece of code, or at least, a current copy of the code.

If you are interested we can take this offline and establish some sort of change procedure...

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...