[PB5.11] Mass Keyboard shortcut list producer

Share your advanced PureBasic knowledge/code with the community.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4790
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

[PB5.11] Mass Keyboard shortcut list producer

Post by Fangbeast »

I am always adding keyboard shortcuts to my programs and always keep retyping masses of the same code in every program when I don't have to. Keep in mind that this program is setup for my needs and use it accordingly.

1. When the cursor is on the #Gadget_ShortCutter_copytolist gadget, typing multiple keyboard shortcuts will add them in succession to the list above.

2. To edit the various fields, simply single click on them in the list or double left click to open an edit window.

3. You can save shortcut lists to disk and load them back again.

4. You can drag and drop a row to change the list order.

5. You can export the list to a PB include file with various structures, database insert and other things I wanted setup.

6. Key sets are saved and loaded as ".ks" (just plain text) files. The sample is below.

Code: Select all

Add New Record|131137|Ctrl + A|Add a new record to the database
Bash Old Record|131138|Ctrl + B|Soundly bash the old record
Copy Current Record|131139|Ctrl + C|Copy the formatted record to the clipboard
Delete Current Record|131140|Ctrl + D|Delete the currently selected record
Edit Current Record|131141|Ctrl + E|Edit the currently selected record
Find Records|131142|Ctrl + F|Find records using advanced SQL syntax
7. There are buglets!!! yay!

8. I didn't include the graphics as Hex but the person's code I tried to use for the job simply won't drag and drop here on this Win 8 x64 box.

9. Where there were author's notes or copyrights, they are included in the procedure or declaration headers.

The code is in the next post. Hoping I haven't exceeded the post length, have stripped nearly all my nice formatting (sad look)
Last edited by Fangbeast on Thu Sep 26, 2013 9:23 am, edited 1 time in total.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4790
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: [PB5.11] Mass Keyboard shortcut list producer

Post by Fangbeast »

Code: Select all

; Window Constants

Enumeration 1
  #Window_ShortCutter
  #Window_ShortCutEdit
EndEnumeration

#WindowIndex = #PB_Compiler_EnumerationValue

; Gadget Constants

Enumeration 1
  ; Window_ShortCutter
  #Gadget_ShortCutter_cKeyList
  #Gadget_ShortCutter_Keylist
  #Gadget_ShortCutter_cCurrent
  #Gadget_ShortCutter_lcurrent
  #Gadget_ShortCutter_current
  #Gadget_ShortCutter_cControl1
  #Gadget_ShortCutter_load
  #Gadget_ShortCutter_save
  #Gadget_ShortCutter_cControl2
  #Gadget_ShortCutter_add
  #Gadget_ShortCutter_copy
  #Gadget_ShortCutter_delete
  #Gadget_ShortCutter_export
  #Gadget_ShortCutter_cControl3
  #Gadget_ShortCutter_cControl4
  #Gadget_ShortCutter_lControl1
  #Gadget_ShortCutter_edit
  #Gadget_ShortCutter_clear
  #Gadget_ShortCutter_lControl2
  #Gadget_ShortCutter_lControl3
  #Gadget_ShortCutter_cControl5
  #Gadget_ShortCutter_exit
  #Gadget_ShortCutter_lControl5
  #Gadget_ShortCutter_copytolist
  #Gadget_ShortCutter_messages
  #Gadget_ShortCutter_shortcuts
  #Gadget_ShortCutter_lControl4

  ; Window_ShortCutEdit
  
  #Gadget_ShortCutEdit_cVariable
  #Gadget_ShortCutEdit_lkeyboardvariable
  #Gadget_ShortCutEdit_keyboardvariable
  #Gadget_ShortCutEdit_cShortcut
  #Gadget_ShortCutEdit_lkeyboardvalue
  #Gadget_ShortCutEdit_keyboardvalue
  #Gadget_ShortCutEdit_lkeyboardshortcut
  #Gadget_ShortCutEdit_keyboardshortcut
  #Gadget_ShortCutEdit_cText
  #Gadget_ShortCutEdit_lkeyboardtext
  #Gadget_ShortCutEdit_keyboardtext
  #Gadget_ShortCutEdit_cControl1
  #Gadget_ShortCutEdit_save
  #Gadget_ShortCutEdit_lControl1
  #Gadget_ShortCutEdit_cControl2
  #Gadget_ShortCutEdit_cControl3
  #Gadget_ShortCutEdit_exit
  #Gadget_ShortCutEdit_lControl3
  #Gadget_ShortCutEdit_messages
  #Gadget_ShortCutEdit_lControl2
EndEnumeration

#GadgetIndex = #PB_Compiler_EnumerationValue

; Image Constants

Enumeration 1
  #Image_ShortCutter_load
  #Image_ShortCutter_save
  #Image_ShortCutter_add
  #Image_ShortCutter_copy
  #Image_ShortCutter_delete
  #Image_ShortCutter_export
  #Image_ShortCutter_edit
  #Image_ShortCutter_clear
  #Image_ShortCutter_exit

  #Image_ShortCutEdit_save
  #Image_ShortCutEdit_exit
EndEnumeration

#ImageIndex = #PB_Compiler_EnumerationValue

; Load Images

CatchImage(#Image_ShortCutter_load,   ?_OPT_ShortCutter_load)
CatchImage(#Image_ShortCutter_save,   ?_OPT_ShortCutter_save)
CatchImage(#Image_ShortCutter_add,    ?_OPT_ShortCutter_add)
CatchImage(#Image_ShortCutter_copy,   ?_OPT_ShortCutter_copy)
CatchImage(#Image_ShortCutter_delete, ?_OPT_ShortCutter_delete)
CatchImage(#Image_ShortCutter_export, ?_OPT_ShortCutter_export)
CatchImage(#Image_ShortCutter_edit,   ?_OPT_ShortCutter_edit)
CatchImage(#Image_ShortCutter_clear,  ?_OPT_ShortCutter_clear)
CatchImage(#Image_ShortCutter_exit,   ?_OPT_ShortCutter_exit)
CatchImage(#Image_ShortCutEdit_save,  ?_OPT_ShortCutter_save)
CatchImage(#Image_ShortCutEdit_exit,  ?_OPT_ShortCutter_exit)

DataSection
  _OPT_ShortCutter_load   : IncludeBinary "Images\open32x32.ico"
  _OPT_ShortCutter_save   : IncludeBinary "Images\save32x32.ico"
  _OPT_ShortCutter_add    : IncludeBinary "Images\add32x32.ico"
  _OPT_ShortCutter_copy   : IncludeBinary "Images\copy32x32.ico"
  _OPT_ShortCutter_delete : IncludeBinary "Images\delete32x32.ico"
  _OPT_ShortCutter_export : IncludeBinary "Images\export32x32.ico"
  _OPT_ShortCutter_edit   : IncludeBinary "Images\edit32x32.ico"
  _OPT_ShortCutter_clear  : IncludeBinary "Images\clear32x32.ico"
  _OPT_ShortCutter_exit   : IncludeBinary "Images\exit32x32.ico"
EndDataSection

; Windows code

Procedure.l Window_ShortCutter()
  If OpenWindow(#Window_ShortCutter,65,70,985,656,"Keyboard shortcut gadget template creator",#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered|#PB_Window_Invisible)
      SetWindowColor(#Window_ShortCutter,$A2A2A2)
      ContainerGadget(#Gadget_ShortCutter_cKeyList,5,5,975,530,#PB_Container_Flat)
        SetGadgetColor(#Gadget_ShortCutter_cKeyList,#PB_Gadget_BackColor,$BFBFBF)
      ListIconGadget(#Gadget_ShortCutter_Keylist,5,5,965,520,"Keyboard variable name",210,#PB_ListIcon_GridLines|#PB_ListIcon_FullRowSelect|#PB_ListIcon_AlwaysShowSelection)
        SetGadgetColor(#Gadget_ShortCutter_Keylist,#PB_Gadget_BackColor,$DBDBDB)
        AddGadgetColumn(#Gadget_ShortCutter_Keylist,1,"Keyboard value",110)
        AddGadgetColumn(#Gadget_ShortCutter_Keylist,2,"Keyboard shortcut",230)
        AddGadgetColumn(#Gadget_ShortCutter_Keylist,3,"Keyboard variable text",390)
        SetGadgetFont(#Gadget_ShortCutter_Keylist,LoadFont(#Gadget_ShortCutter_Keylist,"Arial",10,0))
      CloseGadgetList()
      ContainerGadget(#Gadget_ShortCutter_cCurrent,5,540,975,40,#PB_Container_Flat)
        SetGadgetColor(#Gadget_ShortCutter_cCurrent,#PB_Gadget_BackColor,$BFBFBF)
      TextGadget(#Gadget_ShortCutter_lcurrent,15,10,120,20,"Quick add bar")
        SetGadgetColor(#Gadget_ShortCutter_lcurrent,#PB_Gadget_BackColor,$BFBFBF)
        SetGadgetFont(#Gadget_ShortCutter_lcurrent,LoadFont(#Gadget_ShortCutter_lcurrent,"Arial",11,0))
      ShortcutGadget(#Gadget_ShortCutter_current,135,5,830,25,0)
        SetGadgetColor(#Gadget_ShortCutter_current,#PB_Gadget_BackColor,$DBDBDB)
        SetGadgetFont(#Gadget_ShortCutter_current,LoadFont(#Gadget_ShortCutter_current,"Arial",11,0))
      CloseGadgetList()
      ContainerGadget(#Gadget_ShortCutter_cControl1,5,585,130,65,#PB_Container_Flat)
        SetGadgetColor(#Gadget_ShortCutter_cControl1,#PB_Gadget_BackColor,$BFBFBF)
      ButtonImageGadget(#Gadget_ShortCutter_load,5,5,40,40,ImageID(#Image_ShortCutter_load))
      ButtonImageGadget(#Gadget_ShortCutter_save,45,5,40,40,ImageID(#Image_ShortCutter_save))
      ButtonImageGadget(#Gadget_ShortCutter_export,85,5,40,40,ImageID(#Image_ShortCutter_export))
      TextGadget(#Gadget_ShortCutter_lControl1,5,45,120,15,"   Load     Save   Export")
        SetGadgetColor(#Gadget_ShortCutter_lControl1,#PB_Gadget_BackColor,$BFBFBF)
        SetGadgetFont(#Gadget_ShortCutter_lControl1,LoadFont(#Gadget_ShortCutter_lControl1,"Comic Sans MS",8,0))
      CloseGadgetList()
      ContainerGadget(#Gadget_ShortCutter_cControl2,140,585,170,65,#PB_Container_Flat)
        SetGadgetColor(#Gadget_ShortCutter_cControl2,#PB_Gadget_BackColor,$BFBFBF)
      ButtonImageGadget(#Gadget_ShortCutter_add,5,5,40,40,ImageID(#Image_ShortCutter_add))
      ButtonImageGadget(#Gadget_ShortCutter_copy,45,5,40,40,ImageID(#Image_ShortCutter_copy))
      ButtonImageGadget(#Gadget_ShortCutter_delete,85,5,40,40,ImageID(#Image_ShortCutter_delete))
      ButtonImageGadget(#Gadget_ShortCutter_edit,125,5,40,40,ImageID(#Image_ShortCutter_edit))
      TextGadget(#Gadget_ShortCutter_lControl2,5,45,160,15,"   Add      Copy   Delete     Edit")
        SetGadgetColor(#Gadget_ShortCutter_lControl2,#PB_Gadget_BackColor,$BFBFBF)
        SetGadgetFont(#Gadget_ShortCutter_lControl2,LoadFont(#Gadget_ShortCutter_lControl2,"Comic Sans MS",8,0))
      CloseGadgetList()
      ContainerGadget(#Gadget_ShortCutter_cControl3,315,585,50,65,#PB_Container_Flat)
        SetGadgetColor(#Gadget_ShortCutter_cControl3,#PB_Gadget_BackColor,$BFBFBF)
      ButtonImageGadget(#Gadget_ShortCutter_clear,5,5,40,40,ImageID(#Image_ShortCutter_clear))
      TextGadget(#Gadget_ShortCutter_lControl3,5,45,40,15,"  Clear")
        SetGadgetColor(#Gadget_ShortCutter_lControl3,#PB_Gadget_BackColor,$BFBFBF)
        SetGadgetFont(#Gadget_ShortCutter_lControl3,LoadFont(#Gadget_ShortCutter_lControl3,"Comic Sans MS",8,0))
      CloseGadgetList()
      ContainerGadget(#Gadget_ShortCutter_cControl4,370,585,555,65,#PB_Container_Flat)
        SetGadgetColor(#Gadget_ShortCutter_cControl4,#PB_Gadget_BackColor,$BFBFBF)
      CheckBoxGadget(#Gadget_ShortCutter_copytolist,10,25,15,20,"",$8000)
        SetGadgetFont(#Gadget_ShortCutter_copytolist,LoadFont(#Gadget_ShortCutter_copytolist,"Arial",10,0))
      StringGadget(#Gadget_ShortCutter_messages,45,25,420,20,"",#PB_String_ReadOnly|#PB_String_BorderLess)
        SetGadgetColor(#Gadget_ShortCutter_messages,#PB_Gadget_BackColor,$DBDBDB)
        SetGadgetFont(#Gadget_ShortCutter_messages,LoadFont(#Gadget_ShortCutter_messages,"Comic Sans MS",10,0))
      StringGadget(#Gadget_ShortCutter_shortcuts,485,25,60,20,"",#PB_String_ReadOnly|#PB_String_BorderLess)
        SetGadgetColor(#Gadget_ShortCutter_shortcuts,#PB_Gadget_BackColor,$DBDBDB)
        SetGadgetFont(#Gadget_ShortCutter_shortcuts,LoadFont(#Gadget_ShortCutter_shortcuts,"Comic Sans MS",10,0))
      TextGadget(#Gadget_ShortCutter_lControl4,5,45,540,15," Auto     Messages                                                                                                                                  Shortcuts")
        SetGadgetColor(#Gadget_ShortCutter_lControl4,#PB_Gadget_BackColor,$BFBFBF)
        SetGadgetFont(#Gadget_ShortCutter_lControl4,LoadFont(#Gadget_ShortCutter_lControl4,"Comic Sans MS",8,0))
      CloseGadgetList()
      ContainerGadget(#Gadget_ShortCutter_cControl5,930,585,50,65,#PB_Container_Flat)
        SetGadgetColor(#Gadget_ShortCutter_cControl5,#PB_Gadget_BackColor,$BFBFBF)
      ButtonImageGadget(#Gadget_ShortCutter_exit,5,5,40,40,ImageID(#Image_ShortCutter_exit))
      TextGadget(#Gadget_ShortCutter_lControl5,5,45,40,15,"   Exit")
        SetGadgetColor(#Gadget_ShortCutter_lControl5,#PB_Gadget_BackColor,$BFBFBF)
        SetGadgetFont(#Gadget_ShortCutter_lControl5,LoadFont(#Gadget_ShortCutter_lControl5,"Comic Sans MS",8,0))
      CloseGadgetList()
      HideWindow(#Window_ShortCutter,0)
    ProcedureReturn WindowID(#Window_ShortCutter)
  EndIf
EndProcedure

Procedure.l Window_ShortCutEdit()
  If OpenWindow(#Window_ShortCutEdit,80,80,449,335,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_Invisible,WindowID(#Window_ShortCutter))
      SetWindowColor(#Window_ShortCutEdit,$A2A2A2)
      ContainerGadget(#Gadget_ShortCutEdit_cVariable,5,5,440,45,#PB_Container_Flat)
        SetGadgetColor(#Gadget_ShortCutEdit_cVariable,#PB_Gadget_BackColor,$BFBFBF)
      TextGadget(#Gadget_ShortCutEdit_lkeyboardvariable,10,15,150,20,"Keyboard variable name")
        SetGadgetColor(#Gadget_ShortCutEdit_lkeyboardvariable,#PB_Gadget_BackColor,$BFBFBF)
        SetGadgetFont(#Gadget_ShortCutEdit_lkeyboardvariable,LoadFont(#Gadget_ShortCutEdit_lkeyboardvariable,"Comic Sans MS",10,0))
      StringGadget(#Gadget_ShortCutEdit_keyboardvariable,165,10,265,25,"",#PB_String_BorderLess)
        SetGadgetColor(#Gadget_ShortCutEdit_keyboardvariable,#PB_Gadget_BackColor,$DBDBDB)
        SetGadgetFont(#Gadget_ShortCutEdit_keyboardvariable,LoadFont(#Gadget_ShortCutEdit_keyboardvariable,"Comic Sans MS",10,0))
      CloseGadgetList()
      ContainerGadget(#Gadget_ShortCutEdit_cShortcut,5,55,440,75,#PB_Container_Flat)
        SetGadgetColor(#Gadget_ShortCutEdit_cShortcut,#PB_Gadget_BackColor,$BFBFBF)
      TextGadget(#Gadget_ShortCutEdit_lkeyboardvalue,10,15,150,20,"Keyboard value")
        SetGadgetColor(#Gadget_ShortCutEdit_lkeyboardvalue,#PB_Gadget_BackColor,$BFBFBF)
        SetGadgetFont(#Gadget_ShortCutEdit_lkeyboardvalue,LoadFont(#Gadget_ShortCutEdit_lkeyboardvalue,"Comic Sans MS",10,0))
      StringGadget(#Gadget_ShortCutEdit_keyboardvalue,165,10,265,25,"",#PB_String_BorderLess|#PB_String_ReadOnly)
        SetGadgetColor(#Gadget_ShortCutEdit_keyboardvalue,#PB_Gadget_BackColor,$DBDBDB)
        SetGadgetFont(#Gadget_ShortCutEdit_keyboardvalue,LoadFont(#Gadget_ShortCutEdit_keyboardvalue,"Comic Sans MS",10,0))
      TextGadget(#Gadget_ShortCutEdit_lkeyboardshortcut,10,45,150,20,"Keyboard shortcut")
        SetGadgetColor(#Gadget_ShortCutEdit_lkeyboardshortcut,#PB_Gadget_BackColor,$BFBFBF)
        SetGadgetFont(#Gadget_ShortCutEdit_lkeyboardshortcut,LoadFont(#Gadget_ShortCutEdit_lkeyboardshortcut,"Comic Sans MS",10,0))
      ShortcutGadget(#Gadget_ShortCutEdit_keyboardshortcut,165,40,265,25,0)
        SetGadgetColor(#Gadget_ShortCutEdit_keyboardshortcut,#PB_Gadget_BackColor,$DBDBDB)
        SetGadgetFont(#Gadget_ShortCutEdit_keyboardshortcut,LoadFont(#Gadget_ShortCutEdit_keyboardshortcut,"Comic Sans MS",10,0))
      CloseGadgetList()
      ContainerGadget(#Gadget_ShortCutEdit_cText,5,135,440,125,#PB_Container_Flat)
        SetGadgetColor(#Gadget_ShortCutEdit_cText,#PB_Gadget_BackColor,$BFBFBF)
      TextGadget(#Gadget_ShortCutEdit_lkeyboardtext,10,15,150,20,"Variable text")
        SetGadgetColor(#Gadget_ShortCutEdit_lkeyboardtext,#PB_Gadget_BackColor,$BFBFBF)
        SetGadgetFont(#Gadget_ShortCutEdit_lkeyboardtext,LoadFont(#Gadget_ShortCutEdit_lkeyboardtext,"Comic Sans MS",10,0))
      EditorGadget(#Gadget_ShortCutEdit_keyboardtext,165,15,265,100,#PB_Editor_WordWrap)
        SetGadgetColor(#Gadget_ShortCutEdit_keyboardtext,#PB_Gadget_BackColor,$DBDBDB)
        SetGadgetFont(#Gadget_ShortCutEdit_keyboardtext,LoadFont(#Gadget_ShortCutEdit_keyboardtext,"Comic Sans MS",10,0))
      CloseGadgetList()
      ContainerGadget(#Gadget_ShortCutEdit_cControl1,5,265,50,65,#PB_Container_Flat)
        SetGadgetColor(#Gadget_ShortCutEdit_cControl1,#PB_Gadget_BackColor,$BFBFBF)
      ButtonImageGadget(#Gadget_ShortCutEdit_save,5,5,40,40,ImageID(#Image_ShortCutEdit_save))
      TextGadget(#Gadget_ShortCutEdit_lControl1,5,45,40,15,"  Save")
        SetGadgetColor(#Gadget_ShortCutEdit_lControl1,#PB_Gadget_BackColor,$BFBFBF)
        SetGadgetFont(#Gadget_ShortCutEdit_lControl1,LoadFont(#Gadget_ShortCutEdit_lControl1,"Comic Sans MS",8,0))
      CloseGadgetList()
      ContainerGadget(#Gadget_ShortCutEdit_cControl2,60,265,330,65,#PB_Container_Flat)
        SetGadgetColor(#Gadget_ShortCutEdit_cControl2,#PB_Gadget_BackColor,$BFBFBF)
      StringGadget(#Gadget_ShortCutEdit_messages,5,20,320,25,"",#PB_String_BorderLess)
        SetGadgetColor(#Gadget_ShortCutEdit_messages,#PB_Gadget_BackColor,$DBDBDB)
        SetGadgetFont(#Gadget_ShortCutEdit_messages,LoadFont(#Gadget_ShortCutEdit_messages,"Comic Sans MS",10,0))
      TextGadget(#Gadget_ShortCutEdit_lControl2,5,45,320,15," Messages")
        SetGadgetColor(#Gadget_ShortCutEdit_lControl2,#PB_Gadget_BackColor,$BFBFBF)
        SetGadgetFont(#Gadget_ShortCutEdit_lControl2,LoadFont(#Gadget_ShortCutEdit_lControl2,"Comic Sans MS",8,0))
      CloseGadgetList()
      ContainerGadget(#Gadget_ShortCutEdit_cControl3,395,265,50,65,#PB_Container_Flat)
        SetGadgetColor(#Gadget_ShortCutEdit_cControl3,#PB_Gadget_BackColor,$BFBFBF)
      ButtonImageGadget(#Gadget_ShortCutEdit_exit,5,5,40,40,ImageID(#Image_ShortCutEdit_exit))
      TextGadget(#Gadget_ShortCutEdit_lControl3,5,45,40,15,"   Exit")
        SetGadgetColor(#Gadget_ShortCutEdit_lControl3,#PB_Gadget_BackColor,$BFBFBF)
        SetGadgetFont(#Gadget_ShortCutEdit_lControl3,LoadFont(#Gadget_ShortCutEdit_lControl3,"Comic Sans MS",8,0))
      CloseGadgetList()
      HideWindow(#Window_ShortCutEdit,0)
    ProcedureReturn WindowID(#Window_ShortCutEdit)
  EndIf
EndProcedure

Declare.s FormatField(InString.s, FieldLen.i)                                           ; Format the passed string to the correct field length for printing
Declare.s GetShortcutText(State.i)                                                      ; Get the text of the associated shortcut constant
Declare   ListIconGadgetMove(GadgetNumber.i, Position.i, Destination.i)                 ; Move lines around in a single ListIconGadget
Declare   ClearShortCuts()                                                              ; Clear all shortcuts from the list
Declare   CloseMyWindow(WindowId.i)                                                     ; Custom window closer with variable cleanup
Declare   CopyShortcutToList()                                                          ; Copy the current shortcut to the key list
Declare   ExportShortCutsToCode()                                                       ; Export keyboard shortcuts to pb include file
Declare   ShortCutToValue()                                                             ; Copy shortcut value to edit form
Declare   AddShortCut()                                                                 ; Add a new shortcut to the list
Declare   CopyShortCut()                                                                ; Copy shortcut to the clipboard
Declare   DeleteShortCut()                                                              ; Delete the currently selected shortcut
Declare   EditShortCut()                                                                ; Edit the selected shortcut
Declare   SaveShortCut()                                                                ; Save the new or changed shortcut
Declare   LoadShortCutsFromDisk()                                                       ; Load a shortcuts key list from disk
Declare   SaveShortCutsFromDisk()                                                       ; Save the shortcuts key list to disk

Structure ProgramData                                        ; Program data structure
  QuitValue.i                                                 ; Program quit value
  DragItem.i                                                  ; Dragging items between rows
  WindowMutex.i                                               ; Window lock mutex
  LastWindow.s                                                ; The last window opened
  CurrentLine.i                                               ; Currently selected line
EndStructure

Structure KeysData                                           ; Keyboard shortcuts data structure
  KeyDataName.s                                               ; The variable name
  KeyDataValue.s                                              ; The PB value
  KeyDataKeys.s                                               ; The text representation of the shortcut
  KeyDataText.s                                               ; The shortcut full text as used in menu structures
EndStructure

Global Program.ProgramData                                   ; Program data list
Global NewList Keys.KeysData()                              ; Keyboard shortcuts data list

Program\DragItem = -1                                         ; Initial item drag state

; Format the passed string to the correct field length for printing

Procedure.s FormatField(InString.s, FieldLen.i)
  InStringLen.i = Len(InString.s)
  SpaceLen.i    = FieldLen.i - InStringLen.i
  If SpaceLen.i > 1
    OutString.s = InString.s + " " + Space(SpaceLen.i)
  Else
    OutString.s = InString.s + " "
  EndIf
  ProcedureReturn OutString.s
EndProcedure

; 'Editable ListIcon'.
;
; Stephen Rodriguez.
; Created with Purebasic 4.02 for Windows.
;
; Date:  May 2007.
;
; Platforms:  Windows.
;
; Licence: DAYLike
;   (Do As You Like with it! - No Warranties!)
;   A credit to myself, whilst nice, is not absolutely necessary.
;
;
; NOTES.
; ------
; 1)  Register a listicon gadget to have editable cells by using the command SetListIconEditable(listID).
;     You MUST set up such listicons to have a column zero of zero width.
; 2)  Cells are made editable by intercepting double-clicks, setting the #LVS_EDITLABELS style,
;     repositioning the edit control which Windows uses to edit the labels in column zero and copying
;     the resulting text to the listicon cell.
; 3)  Cells can also be edited by means of the command EditCell().
;

#LVM_SUBITEMHITTEST = #LVM_FIRST + 57
#LVM_GETSUBITEMRECT = #LVM_FIRST + 56
#LVM_GETHEADER      = #LVM_FIRST + 31
#EC_RIGHTMARGIN     = 2
; 
EnableExplicit
; 
DeclareDLL.i SetListIconEditable(listID)
Declare.i   _LIEwinProc(hWnd, uMsg, wParam, lParam)
Declare.i   _LIEListProc(hWnd, uMsg, wParam, lParam)
Declare.i   _LIEeditProc(hWnd, uMsg, wParam, lParam)
; 
Structure _LIEdit
  listOldProc.i
  editHwnd.i
  item.i
  subitem.i
  x.i
  y.i
  cx.i
  cy.i
EndStructure
; 
ProcedureDLL.i SetListIconEditable(listID)
  Protected result, parenthWnd, *mem._LIEdit, hWnd
  If IsGadget(listID) And GadgetType(listID) = #PB_GadgetType_ListIcon                     ; Check that listID references a valid listicon.
    hWnd = GadgetID(listID)
    If GetProp_(hWnd, "_LIEdit") = 0                                                        ; No!;Is the listicon already registered?
      *mem = AllocateMemory(SizeOf(_LIEdit))                                              ; Allocate enough memory for a _LIEdit structure.
      If *mem
        SetWindowLongPtr_(hWnd, #GWL_STYLE, GetWindowLongPtr_(hWnd, #GWL_STYLE) &~ #LVS_EDITLABELS)
        *mem\listOldProc = SetWindowLongPtr_(hWnd, #GWL_WNDPROC, @_LIEListProc())            ; Set the fields of the _LIEedit structure.
        SetProp_(hWnd, "_LIEdit", *mem)                                                   ; Store a pointer to this structure in a window property ofthe listicon.         
        parenthWnd = GetParent_(hWnd)                                                     ; Subclass the parent window if not already through another listicon.
        If GetProp_(parenthWnd, "_LIEditOldProc") = 0                                     ; No!
          SetProp_(parenthWnd, "_LIEditOldProc", SetWindowLongPtr_(parenthWnd, #GWL_WNDPROC, @_LIEwinProc()))
        EndIf
        result = 1
      EndIf
    EndIf
  EndIf
  ProcedureReturn result
EndProcedure

; Sets the specified cell to be edited.

ProcedureDLL EditCell(listID, item, subitem)
  Protected hWnd, *liedit._LIEdit, numrows, numcols
  If IsGadget(listID) And GadgetType(listID) = #PB_GadgetType_ListIcon                        ; Check that listID references a valid listicon.
    hWnd = GadgetID(listID)                                                               ; Check that the listicon is registered as editable.
    *liedit = GetProp_(hWnd, "_LIEdit")
    If *liedit
      numrows = CountGadgetItems(listID)                                                ; Check parameters are in range.
      numcols = SendMessage_(SendMessage_(hWnd, #LVM_GETHEADER, 0, 0), #HDM_GETITEMCOUNT, 0, 0)
      If item > = 0 And item < numrows And subitem > 0 And subitem < numcols
        *liedit\item = item
        *liedit\subitem = subitem
        SetWindowLongPtr_(hWnd, #GWL_STYLE, GetWindowLongPtr_(hWnd, #GWL_STYLE) | #LVS_EDITLABELS)
        SetActiveGadget(listID)
        SendMessage_(hWnd, #LVM_EDITLABEL, item, 0)
      EndIf
    EndIf
  EndIf
EndProcedure

; Window proc of the ListIcon parent window.

Procedure.i _LIEwinProc(hWnd, uMsg, wParam, lParam)
  Protected result, oldwinproc, *nmh. NMHDR, listhWnd, edithWnd, *liedit._LIEdit, *lvd.LV_DISPINFO, rc.RECT
  Static celltext$
  oldwinproc = GetProp_(hWnd, "_LIEditOldProc")                                               ; Retrieve the address of the old proc.
  Select uMsg
    Case #WM_NOTIFY
      *nmh = lParam
      Select *nmh\code
        Case #LVN_BEGINLABELEDIT
          listhWnd = *nmh\hwndFrom
          *liedit = GetProp_(listhWnd, "_LIEdit")                                           ; Retrieve the address of the LIEdit structure.
          If *liedit                                                                        ; Good to go!
            *liedit\editHwnd = 0
            edithWnd = SendMessage_(listhWnd, #LVM_GETEDITCONTROL, 0, 0)                  ; Get the handle of the edit control used to edit the label.
            SetProp_(edithWnd, "_LIEditOldProc", SetWindowLongPtr_(edithWnd, #GWL_WNDPROC, @_LIEeditProc()))               ;Subclass the edit control.
            celltext$ = GetGadgetItemText(*nmh\idFrom, *liedit\item, *liedit\subitem)       ; Set text.
            SendMessage_(edithWnd, #WM_SETTEXT, 0, celltext$)
            SetGadgetItemText(*nmh\idFrom, *liedit\item, "", *liedit\subitem)
            rc\top = *liedit\subitem                                                      ; Get bounding rectangle.
            rc\left = #LVIR_BOUNDS
            SendMessage_(listhWnd, #LVM_GETSUBITEMRECT, *liedit\item, rc)
            *liedit\x = rc\left
            *liedit\y = rc\top
            *liedit\cx = SendMessage_(listhWnd, #LVM_GETCOLUMNWIDTH, *liedit\subitem, 0)
            *liedit\cy = rc\bottom - rc\top
          EndIf
        Case #LVN_ENDLABELEDIT
          listhWnd = *nmh\hwndFrom
          *liedit = GetProp_(listhWnd, "_LIEdit")                                           ; Retrieve the address of the LIEdit structure.
          If *liedit                                                                        ; Good to go!
            *lvd = lParam
            If *lvd\item\pszText
              SetGadgetItemText(*nmh\idFrom, *liedit\item, PeekS(*lvd\item\pszText), *liedit\subitem)
            Else             
              SetGadgetItemText(*nmh\idFrom, *liedit\item, celltext$, *liedit\subitem)
            EndIf
            SetWindowLongPtr_(listhWnd, #GWL_STYLE, GetWindowLongPtr_(listhWnd, #GWL_STYLE) &~ #LVS_EDITLABELS)
          EndIf
        Default
          result=CallWindowProc_(oldwinproc, hWnd, uMsg, wParam, lParam)
      EndSelect
    Case #WM_NCDESTROY
      result = CallWindowProc_(oldwinproc, hWnd, uMsg, wParam, lParam)
      RemoveProp_(hWnd, "_LIEditOldProc")
    Default
      result = CallWindowProc_(oldwinproc, hWnd, uMsg, wParam, lParam)
  EndSelect
  ProcedureReturn result
EndProcedure

; Window proc of the ListIcon.

Procedure.i _LIEListProc(hWnd, uMsg, wParam, lParam)
  Protected result, *liedit._LIEdit, PInfo.LVHITTESTINFO, *nmHEADER.HD_NOTIFY, rc.RECT, clientrc.RECT
  *liedit = GetProp_(hWnd, "_LIEdit")                                                       ; Retrieve the address of the LIEdit structure.
  Select uMsg
    Case #WM_NOTIFY
      *nmHEADER = lParam
      Select *nmHEADER\hdr\code
        Case #HDN_BEGINTRACK, #HDN_BEGINTRACKW                                                ; Prevent column 0 from being resized.
          If *nmHEADER\iItem = 0
            result = 1
          EndIf
        Default
          result = CallWindowProc_(*liedit\listOldProc, hWnd, uMsg, wParam, lParam)
      EndSelect
    Case #WM_LBUTTONDBLCLK
      PInfo\pt\x = lParam & $ffff                                                           ; Identify the clicked item
      PInfo\pt\y = (lParam >> 16) & $ffff
      SendMessage_(hwnd, #LVM_SUBITEMHITTEST, 0, PInfo)
      If PInfo\iItem <> -1                                                                    ; A valid cell was clicked.
        *liedit\item = PInfo\iItem
        *liedit\subitem = PInfo\iSubItem
        ; scroll the listicon if the clicked cell is not entirely visible
        ; *****IF YOU WISH TO RESTRICT WHICH CELLS CAN BE EDITED, THEN PERFORM THE NECESSARY CHECKS HERE
        ; *****ON THE VALUES OF *liedit\item and *liedit\subitem (WHICH INDICATE WHICH CELL IS ABOUT
        ; *****TO BE EDITED) AND RUN THE FOLLOWING 2 LINES FOR THOSE CELLS WHICH ARE TO BE EDITED.
        rc\top = *liedit\subitem
        rc\left = #LVIR_BOUNDS
        SendMessage_(hWnd, #LVM_GETSUBITEMRECT, *liedit\item, rc)
        GetClientRect_(hWnd, clientrc)
        If rc\left < 0 Or (rc\right - rc\left) > = clientrc\right
          SendMessage_(hWnd, #LVM_SCROLL, rc\left, 0)
        Else
          If rc\right > clientrc\right
            SendMessage_(hWnd, #LVM_SCROLL,rc\right - clientrc\right, 0)
          EndIf
        EndIf
        SetWindowLongPtr_(hWnd, #GWL_STYLE, GetWindowLongPtr_(hWnd, #GWL_STYLE) | #LVS_EDITLABELS)
        SendMessage_(hWnd, #LVM_EDITLABEL, PInfo\iItem, 0)
      EndIf
    Case #WM_NCDESTROY
      result = CallWindowProc_(*liedit\listOldProc, hWnd, uMsg, wParam, lParam)
      RemoveProp_(hWnd, "_LIEdit")
      FreeMemory(*liedit)
    Default
      result = CallWindowProc_(*liedit\listOldProc, hWnd, uMsg, wParam, lParam)
  EndSelect
  ProcedureReturn result
EndProcedure

; Window proc of the edit control.

Procedure.i _LIEeditProc(hWnd, uMsg, wParam, lParam)
  Protected result, oldwinproc, *liedit._LIEdit, *wpos.WINDOWPOS
  oldwinproc = GetProp_(hWnd, "_LIEditOldProc")                                             ; Retrieve the address of the old proc.
  *liedit = GetProp_(GetParent_(hWnd), "_LIEdit")                                           ; Retrieve the address of the LIEdit structure.
  Select uMsg
    Case #WM_ERASEBKGND
      result = CallWindowProc_(oldwinproc, hWnd, uMsg, wParam, lParam)                          ; A hack in order to clear the default selection of characters.
      If *liedit\editHwnd = 0
        *liedit\editHwnd = hWnd
        SendMessage_(hWnd, #EM_SETMARGINS, #EC_LEFTMARGIN | #EC_RIGHTMARGIN, 4)                 ; Set margins.
        SendMessage_(hWnd, #EM_SETSEL, -1, 0)
      EndIf
    Case #WM_WINDOWPOSCHANGING
      *wpos = lParam
      *wpos\cx = *liedit\cx                                                                     ; Comment this line to get an edit control which grows with the text.
      *wpos\cy = *liedit\cy + 3
      *wpos\x  = *liedit\x
      *wpos\y  = *liedit\y - 2
      result = CallWindowProc_(oldwinproc, hWnd, uMsg, wParam, lParam)
    Case #WM_NCDESTROY
      result = CallWindowProc_(oldwinproc, hWnd, uMsg, wParam, lParam)
      RemoveProp_(hWnd, "_LIEditOldProc")
      InvalidateRect_(GetParent_(hWnd), 0, 0)
    Default
      result = CallWindowProc_(oldwinproc, hWnd, uMsg, wParam, lParam)
  EndSelect
  ProcedureReturn result
EndProcedure

DisableExplicit

; Get the text of the associated shortcut constant

Procedure.s GetShortcutText(Constant.i)
  ; NetMaestro 2009. NetMaestro code modded by Sroddy code  ; Unused  mod$, ctrl$, shift$, alt$
  Protected KeyLocale, VirtualKey, KeyString.s, ResultString.s
  If Constant.i & #PB_Shortcut_Control
    ResultString.s = "Ctrl + "
  EndIf
  If Constant.i & #PB_Shortcut_Shift
    ResultString.s + "Shift + "
  EndIf
  If Constant.i & #PB_Shortcut_Alt
    ResultString.s + "Alt + "
  EndIf
  KeyLocale = GetKeyboardLayout_(GetCurrentThreadId_())
  KeyString.s = Space(100)
  ; Virtual key
  VirtualKey = MapVirtualKeyEx_(Constant.i & $FF, 0, KeyLocale) << 16
  If Constant.i >> 8 & #HOTKEYF_EXT Or (Constant.i & $FF >= #PB_Shortcut_Prior And Constant.i & $FF <= #PB_Shortcut_Down)
    VirtualKey | (1 << 24) ; Set the extended key bit
  EndIf
  GetKeyNameText_(VirtualKey, @KeyString, 100)
  ResultString.s + KeyString.s
  If ResultString.s = ""
    ResultString.s = "None"
  EndIf
  ProcedureReturn ResultString.s
EndProcedure

; Move lines around in a single ListIconGadget

Procedure ListIconGadgetMove(GadgetNumber.i, SourceLine.i, DestinationLine.i)
  If DestinationLine.i > = 0
    HeaderControl.i  = SendMessage_(GadgetID(GadgetNumber.i), #LVM_GETHEADER,    0, 0)
    Columns.i        = SendMessage_(HeaderControl.i,          #HDM_GETITEMCOUNT, 0, 0) - 1
    For ColumnLoop.i = 0 To Columns.i
      SourceString.s = GetGadgetItemText(GadgetNumber.i, SourceLine.i, ColumnLoop.i)
      If SourceLine.i < DestinationLine.i
        For FromToLoop.i = SourceLine.i To DestinationLine.i - 1
          HelpString.s = GetGadgetItemText(GadgetNumber.i, FromToLoop.i + 1, ColumnLoop.i)
          SetGadgetItemText(GadgetNumber.i, FromToLoop.i, HelpString.s, ColumnLoop.i)
        Next FromToLoop.i
      Else
        For FromToLoop.i = SourceLine.i To DestinationLine.i + 1 Step - 1
          HelpString.s = GetGadgetItemText(GadgetNumber.i, FromToLoop.i - 1, ColumnLoop.i)
          SetGadgetItemText(GadgetNumber.i, FromToLoop.i, HelpString.s, ColumnLoop.i)
        Next FromToLoop.i
      EndIf
      SetGadgetItemText(GadgetNumber.i, i, SourceString.s, ColumnLoop.i)
    Next ColumnLoop.i
  EndIf
EndProcedure

; Custom modules by me

Procedure ClearShortCuts()
  If CountGadgetItems(#Gadget_ShortCutter_keylist) <> 0
    If MessageRequester("Clear list", "Clear all shortcuts in the list?", #PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes
      ClearGadgetItems(#Gadget_ShortCutter_keylist)
    EndIf
  EndIf
EndProcedure

; Custom window closer with variable cleanup

Procedure CloseMyWindow(WindowId.i)
  Program\WindowMutex = -1
  Program\LastWindow  = ""
  Program\CurrentLine = -1
  CloseWindow(#Window_shortcutedit)
  DisableWindow(#Window_ShortCutter, 0)
  SetActiveGadget(#Gadget_ShortCutter_Keylist)
EndProcedure

; Copy the current shortcut to the key list

Procedure CopyShortCutToList()
  CurrentShortCutValue = GetGadgetState(#Gadget_ShortCutter_current)
  CurrentShortCut.s    = GetShortcutText(CurrentShortCutValue.i)
  If GetGadgetState(#Gadget_ShortCutter_copytolist) = 1
    AddGadgetItem(#Gadget_ShortCutter_keylist, -1, "" + Chr(10) + Str(CurrentShortCutValue.i) + Chr(10) + CurrentShortCut.s + Chr(10) + "")
  EndIf
EndProcedure

; Export keyboard shortcuts to pb include file

Procedure ExportShortCutsToCode()
  ; Count the number of shortcuts in the list to be processed
  NumShortCuts.i = CountGadgetItems(#Gadget_ShortCutter_keylist)
  ; Only execute the following code if we have any shortcuts to be processed
  If NumShortCuts.i
    ; Clear the existing list of shortcuts to be processed
    ClearList(Keys.s())
    ; Grab all the shortcuts into a list for repeat processing
    For ShortCutGrab.i = 0 To NumShortCuts.i - 1
      AddElement(Keys.s())
      Keys()\KeyDataName   = RemoveString(GetGadgetItemText(#Gadget_ShortCutter_keylist, ShortCutGrab.i, 0), " ", 1, 1)
      Keys()\KeyDataValue  =              GetGadgetItemText(#Gadget_ShortCutter_keylist, ShortCutGrab.i, 1)
      Keys()\KeyDataKeys   =              GetGadgetItemText(#Gadget_ShortCutter_keylist, ShortCutGrab.i, 2)
      Keys()\KeyDataText   =              GetGadgetItemText(#Gadget_ShortCutter_keylist, ShortCutGrab.i, 3)
    Next
    ; Ask the user which file we are going to be exporting all the work to
    ShortCutFile.s = SaveFileRequester("Save Keyboard shortcuts export", GetCurrentDirectory() + "_DefaultInclude", "PBI (*.pbi)|*.pbi", 0)
    ; If the user didn't add the correct extension, tag one on
    If LCase(GetExtensionPart(ShortCutFile.s)) <> ".pbi"
      ShortCutFile.s + ".pbi"
    EndIf
    ; Proceed if the user selected a file to save to
    If ShortCutFile.s
      ; Create the outfile to save to
      OutputFile.i = CreateFile(#PB_Any, ShortCutFile.s)
      ; Proceed if we got a valid file handle for the output file
      If OutputFile.i
        ; Key data structure heading
        WriteStringN(OutputFile.i, "")
        WriteStringN(OutputFile.i, LSet(";", 123, "="))
        WriteStringN(OutputFile.i, "; My default KeyBoard ShortCut values")
        WriteStringN(OutputFile.i, LSet(";", 123, "="))
        WriteStringN(OutputFile.i, "")
        ; Key data structure. No values in here, just headings
        WriteStringN(OutputFile.i, "Structure KeyData               ; Key value            ; Key pair")
        ForEach Keys.s()
          WriteString(OutputFile.i, "  " + FormatField(Keys()\KeyDataName + ".i",   30))
          WriteString(OutputFile.i, "; ")
          WriteString(OutputFile.i,        FormatField(Keys()\KeyDataValue,         20))
          WriteString(OutputFile.i, "; ")
          WriteString(OutputFile.i,        FormatField(Keys()\KeyDataKeys,          15))
          WriteString(OutputFile.i, "; ")
          WriteStringN(OutputFile.i,       FormatField(Keys()\KeyDataText,          15))
        Next 
        WriteStringN(OutputFile.i, "EndStructure")
        ; Key menu text data structure heading
        WriteStringN(OutputFile.i, "")
        WriteStringN(OutputFile.i, LSet(";", 123, "="))
        WriteStringN(OutputFile.i, "; Keyboard shortcuts text holders")
        WriteStringN(OutputFile.i, LSet(";", 123, "="))
        WriteStringN(OutputFile.i, "")
        ; Key menu text data structure. No values in here
        WriteStringN(OutputFile.i, "Structure MenuTextData          ; Key value            ; Key pair")
        ForEach Keys.s()
          WriteString(OutputFile.i, "  " + FormatField(Keys()\KeyDataName + ".s",   30))
          WriteString(OutputFile.i, "; ")
          WriteString(OutputFile.i,        FormatField(Keys()\KeyDataValue,         20))
          WriteString(OutputFile.i, "; ")
          WriteString(OutputFile.i,        FormatField(Keys()\KeyDataKeys,          15))
          WriteString(OutputFile.i, "; ")
          WriteStringN(OutputFile.i,       FormatField(Keys()\KeyDataText,          15))
        Next
        WriteStringN(OutputFile.i, "EndStructure")
        ; Menu text variables heading
        WriteStringN(OutputFile.i, "")
        WriteStringN(OutputFile.i, LSet(";", 123, "="))
        WriteStringN(OutputFile.i, "; Keyboard shortcuts menu text")
        WriteStringN(OutputFile.i, LSet(";", 123, "="))
        WriteStringN(OutputFile.i, "")
        ; Menu text variables filled in
        ForEach Keys.s()
          WriteString(OutputFile.i,        FormatField("MenuText\" + RemoveString(Keys()\KeyDataName, " ", 1, 1), 30)  + " = ")
          WriteString(OutputFile.i,        FormatField(Chr(34) + Keys()\KeyDataText + Chr(34), 50))
          WriteString(OutputFile.i, " ; ")
          WriteString(OutputFile.i,        FormatField(Keys()\KeyDataValue,  20))
          WriteString(OutputFile.i, " ; ")
          WriteStringN(OutputFile.i,       FormatField(Keys()\KeyDataKeys,   15))
        Next
        ; Global structure declarations heading
        WriteStringN(OutputFile.i, "")
        WriteStringN(OutputFile.i, LSet(";", 123, "="))
        WriteStringN(OutputFile.i, "; Global variables")
        WriteStringN(OutputFile.i, LSet(";", 123, "="))
        WriteStringN(OutputFile.i, "")
        ; Global structure declarations
        WriteStringN(OutputFile.i, "Global Keys.KeyData                                                                  ; All my keyboard shortcuts")
        WriteStringN(OutputFile.i, "Global MenuText.MenuTextData                                                         ; All repeated menu texts")
        ; Write the keyboard shortcuts heading
        WriteStringN(OutputFile.i, "")
        WriteStringN(OutputFile.i, LSet(";", 123, "="))
        WriteStringN(OutputFile.i, "; Keyboard shortcuts database procedural declaration")
        WriteStringN(OutputFile.i, LSet(";", 123, "="))
        WriteStringN(OutputFile.i, "")
        ; Create the procedural declaration for the database table
        WriteStringN(OutputFile.i,            "DECLARE OpenSystemDatabase()")
        ; Write the keyboard shortcuts heading
        WriteStringN(OutputFile.i, "")
        WriteStringN(OutputFile.i, LSet(";", 123, "="))
        WriteStringN(OutputFile.i, "; Keyboard shortcuts table procedure")
        WriteStringN(OutputFile.i, LSet(";", 123, "="))
        WriteStringN(OutputFile.i, "")
        ; Write the keyboard shortcuts table out
        WriteStringN(OutputFile.i,            "PROCEDURE OpenSystemDatabase()")
        WriteStringN(OutputFile.i,            "  Query.s = " + Chr(34) + "CREATE TABLE IF NOT EXISTS KeyboardShortcuts(" + Chr(34))
        LastElementIndex = ListSize(Keys.s()) - 1
        Counter = 0
        ForEach Keys.s()
          Index = ListIndex(Keys.s())
          If Index & 3 = 3 Or Index = LastElementIndex    ; Wilbert & Luis
            WriteStringN(OutputFile.i,      "  Query.s + " + Chr(34) + OutString.s + Keys()\KeyDataName + " TEXT, " + Chr(34))
            OutString.s = ""
          Else
            OutString.s + Keys()\KeyDataName + " TEXT, "
          EndIf
        Next
        WriteStringN(OutputFile.i,            "  Query.s + " + Chr(34) + "Record INTEGER PRIMARY KEY AUTOINCREMENT)" + Chr(34))
        WriteStringN(OutputFile.i,            "  If DatabaseUpdate(Program\DatabaseHandle, Query.s)")
        WriteStringN(OutputFile.i,            "    If CountRecords(" + Chr(34) + "SELECT COUNT(Record) AS TotalRecs FROM KeyboardShortcuts" + Chr(34) + ") = "  + Chr(34) + "0" + Chr(34))
        WriteStringN(OutputFile.i,            "      Query.s = " + Chr(34) + "INSERT INTO KeyboardShortcuts("  + Chr(34))
        ForEach Keys.s()
          Index = ListIndex(Keys.s())
          If Index & 3 = 3 Or Index = LastElementIndex    ; Wilbert & Luis
            If Index = LastElementIndex
              WriteStringN(OutputFile.i,      "      Query.s + " + Chr(34) + OutString.s + Keys()\KeyDataName + ") " + Chr(34))
            Else
              WriteStringN(OutputFile.i,      "      Query.s + " + Chr(34) + OutString.s + Keys()\KeyDataName + ", " + Chr(34))
            EndIf
            OutString.s = ""
          Else
            OutString.s + Keys()\KeyDataName + ", "
          EndIf
        Next
        WriteStringN(OutputFile.i,            "      Query.s + " + Chr(34) + " VALUES("  + Chr(34))
        ForEach Keys.s()
          Index = ListIndex(Keys.s())
          If Index & 3 = 3 Or Index = LastElementIndex    ; Wilbert & Luis
            If Index = LastElementIndex
              WriteStringN(OutputFile.i,      "      Query.s + " + Chr(34) + OutString.s + Keys()\KeyDataValue + ") " + Chr(34))
            Else
              WriteStringN(OutputFile.i,      "      Query.s + " + Chr(34) + OutString.s + Keys()\KeyDataValue + ", " + Chr(34))
            EndIf
            OutString.s = ""
          Else
            OutString.s + Keys()\KeyDataValue + ", "
          EndIf
        Next
        WriteStringN(OutputFile.i,            "      If DatabaseUpdate(Program\DatabaseHandle, Query.s)")
        WriteStringN(OutputFile.i,            "      EndIf")
        WriteStringN(OutputFile.i,            "    EndIf")
        WriteStringN(OutputFile.i,            "  EndIf")
        WriteStringN(OutputFile.i,            "EndProcedure")
        ; Close the output file as we are finished writing to it
        CloseFile(OutputFile.i)
      Else
        ; Output file could not be created  
      EndIf
    Else
      ; No filename specified, user aborted
    EndIf
  Else
    ; No shortcuts in list to save
  EndIf
EndProcedure

; Copy the current shortcut to the key list

Procedure ShortCutToValue()
  CurrentShortCutValue = GetGadgetState(#Gadget_ShortCutEdit_keyboardshortcut)
  CurrentShortCut.s    = GetShortcutText(CurrentShortCutValue.i)
  SetGadgetText(#Gadget_ShortCutEdit_keyboardvalue, CurrentShortCut.s)
EndProcedure

; Add a new shortcut to the list

Procedure AddShortCut()
  If Program\WindowMutex <> 1
    If Window_shortcutedit()
      Program\WindowMutex = 1
      Program\LastWindow = "Add new shortcut"
      DisableWindow(#Window_ShortCutter, 1)
      SetActiveGadget(#Gadget_shortcutedit_keyboardvariable)
    Else
      ; Could not open the program window, severe error.
    EndIf
  Else
    ; Another program window is still open. Should never happen
  EndIf
EndProcedure

Procedure CopyShortCut()
  CurrentLine.i = GetGadgetState(#Gadget_ShortCutter_Keylist)
  If CurrentLine.i <> -1
    KeyDataName.s     = FormatField("Variable Text: ",   23) + GetGadgetItemText(#Gadget_ShortCutter_Keylist, Program\CurrentLine, 0)  ; 
    KeyDataValue.s    = FormatField("Numerical value: ", 23) + GetGadgetItemText(#Gadget_ShortCutter_Keylist, Program\CurrentLine, 1)  ; 
    KeyDataKeys.s     = FormatField("Text format: ",     23) + GetGadgetItemText(#Gadget_ShortCutter_Keylist, Program\CurrentLine, 2)  ; 
    KeyDescribe.s     = FormatField("Menu text: ",       23) + GetGadgetItemText(#Gadget_ShortCutter_Keylist, Program\CurrentLine, 3)  ; 
    SetClipboardText(KeyVariable.s + #CRLF$ + KeyValue.s + #CRLF$ + KeyText.s + #CRLF$ + KeyMenuText.s)
  Else
    ; Could not get a current line
  EndIf
EndProcedure

; Delete the currently selected shortcut

Procedure DeleteShortCut()
  CurrentLine.i = GetGadgetState(#Gadget_ShortCutter_Keylist)
  If CurrentLine.i <> -1
    If MessageRequester("Delete shortcut", "Delete the currently selected shortcut?", #PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes
      RemoveGadgetItem(#Gadget_ShortCutter_keylist, CurrentLine.i)
    EndIf
  Else
    ; Could not get a current line
  EndIf
EndProcedure

Procedure EditShortCut()
  Program\CurrentLine = GetGadgetState(#Gadget_ShortCutter_Keylist)
  If Program\CurrentLine <> -1
    If Program\WindowMutex <> 1
      If Window_shortcutedit()
        Program\WindowMutex = 1
        Program\LastWindow = "Edit old shortcut"
        DisableWindow(#Window_ShortCutter, 1)
        SetGadgetText(#Gadget_shortcutedit_keyboardvariable,      GetGadgetItemText(#Gadget_ShortCutter_Keylist, Program\CurrentLine, 0))
        SetGadgetText(#Gadget_shortcutedit_keyboardvalue,         GetGadgetItemText(#Gadget_ShortCutter_Keylist, Program\CurrentLine, 1))
        SetGadgetState(#Gadget_shortcutedit_keyboardshortcut, Val(GetGadgetItemText(#Gadget_ShortCutter_Keylist, Program\CurrentLine, 2)))
        SetGadgetText(#Gadget_shortcutedit_keyboardtext,          GetGadgetItemText(#Gadget_ShortCutter_Keylist, Program\CurrentLine, 3))
      Else
        ; Could not open the program window, severe error.
      EndIf
    Else
      ; Another program window is still open. Should never happen
    EndIf
  Else
    ; Could not get a current line to work on
  EndIf
EndProcedure

; Save the new or changed shortcut

Procedure SaveShortCut()
  KeyDataName.s   = GetGadgetText(#Gadget_ShortCutEdit_keyboardvariable)
  KeyDataValue.s  = GetGadgetText(#Gadget_ShortCutEdit_keyboardvalue)
  KeyDataKeys.s   = GetGadgetText(#Gadget_ShortCutEdit_keyboardshortcut)
  KeyDescribe.s   = GetGadgetText(#Gadget_ShortCutEdit_keyboardtext)
  Select Program\LastWindow
    Case  "Add new shortcut"
      AddGadgetItem(#Gadget_ShortCutter_Keylist, -1, KeyDataName.s + Chr(10) + KeyDataValue.s + Chr(10) + KeyDataKeys.s + Chr(10) + KeyDescribe.s)
    Case  "Edit old shortcut"
      SetGadgetItemText(#Gadget_ShortCutter_Keylist, Program\CurrentLine,  KeyDataName.s,   0)
      SetGadgetItemText(#Gadget_ShortCutter_Keylist, Program\CurrentLine,  KeyDataValue.s,  1)
      SetGadgetItemText(#Gadget_ShortCutter_Keylist, Program\CurrentLine,  KeyDataKeys.s,   2)
      SetGadgetItemText(#Gadget_ShortCutter_Keylist, Program\CurrentLine,  KeyDescribe.s,   3)
  EndSelect
  CloseMyWindow(#Window_ShortCutEdit)
EndProcedure

; Load keyboard shortcuts

Procedure LoadShortCutsFromDisk()
  ShortCutFile.s = OpenFileRequester("Load Keyboard shortcuts file", GetCurrentDirectory() + "DefaultKeySave", "KS (*.ks)|*.ks", 0)
  If ShortCutFile.s
    ClearGadgetItems(#Gadget_ShortCutter_keylist)
    InputFile.i = ReadFile(#PB_Any, ShortCutFile.s)
    If InputFile.i
      While Eof(InputFile.i) = 0
        CurrentLine.s = ReadString(InputFile.i)
        CurrentLine.s = ReplaceString(CurrentLine.s, "|", Chr(10), 1, 1)
        AddGadgetItem(#Gadget_ShortCutter_keylist, -1, CurrentLine.s)
      Wend
      CloseFile(InputFile.i)
    Else
      ; Output file could not be created  
    EndIf
  Else
    ; No filename specified, user aborted
  EndIf
EndProcedure

; Save keyboard shortcuts

Procedure SaveShortCutsToDisk()
  NumShortCuts.i = CountGadgetItems(#Gadget_ShortCutter_keylist)
  If NumShortCuts.i
    ShortCutFile.s = SaveFileRequester("Save Keyboard shortcuts file", GetCurrentDirectory() + "DefaultKeySave", "KS (*.ks)|*.ks", 0)
    If LCase(GetExtensionPart(ShortCutFile.s)) <> ".ks"
      ShortCutFile.s + ".ks"
    EndIf
    If ShortCutFile.s
      OutputFile.i = OpenFile(#PB_Any, ShortCutFile.s)
      If OutputFile.i
        For ShortCutGrab.i = 0 To NumShortCuts.i - 1
          KeyDataName.s   = GetGadgetItemText(#Gadget_ShortCutter_keylist, ShortCutGrab.i, 0) + "|"
          KeyDataValue.s  = GetGadgetItemText(#Gadget_ShortCutter_keylist, ShortCutGrab.i, 1) + "|"
          KeyDataKeys.s   = GetGadgetItemText(#Gadget_ShortCutter_keylist, ShortCutGrab.i, 2) + "|"
          KeyDescribe.s   = GetGadgetItemText(#Gadget_ShortCutter_keylist, ShortCutGrab.i, 3)
          WriteStringN(OutputFile.i, KeyDataName.s + KeyDataValue.s + KeyDataKeys.s + KeyDescribe.s)
        Next ShortCutGrab.i
        CloseFile(OutputFile.i)
      Else
        ; Output file could not be created  
      EndIf
    Else
      ; No filename specified, user aborted
    EndIf
  Else
    ; No shortcuts in list to save
  EndIf
EndProcedure

If Window_ShortCutter()
  Program\QuitValue = 0
  SetListIconEditable(#Gadget_ShortCutter_keylist)
  EnableGadgetDrop(#Gadget_ShortCutter_keylist, #PB_Drop_Private, #PB_Drag_Move, 1)
  SetGadgetState(#Gadget_ShortCutter_copytolist, 1)
  SetActiveGadget(#Gadget_ShortCutter_current)
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Select EventWindow()
          Case  #Window_ShortCutter                   : Program\QuitValue = 1
          Case  #Window_ShortCutEdit                  : CloseMyWindow(#Window_ShortCutEdit)
        EndSelect
      Case #PB_Event_GadgetDrop 
        Select EventGadget()
          Case #Gadget_ShortCutter_keylist
            Select EventDropType()
              Case #PB_Drop_Private                   : ListIconGadgetMove(#Gadget_ShortCutter_keylist, Program\DragItem, GetGadgetState(#Gadget_ShortCutter_keylist))
            EndSelect
        EndSelect
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #Gadget_ShortCutter_keylist
            Select EventType()
              Case #PB_EventType_LeftDoubleClick      : EditShortCut()
              Case #PB_EventType_RightDoubleClick     : AddShortCut()
              Case #PB_EventType_DragStart            : Program\DragItem = GetGadgetState(#Gadget_ShortCutter_keylist) : DragPrivate(1, #PB_Drag_Move)
            EndSelect
          Case #Gadget_ShortCutter_load               : LoadShortCutsFromDisk()
          Case #Gadget_ShortCutter_save               : SaveShortCutsToDisk()
          Case #Gadget_ShortCutter_export             : ExportShortCutsToCode()            
          Case #Gadget_ShortCutter_add                : AddShortCut()
          Case #Gadget_ShortCutter_copy               : CopyShortCut()
          Case #Gadget_ShortCutter_delete             : DeleteShortCut()
          Case #Gadget_ShortCutter_edit               : EditShortCut()
          Case #Gadget_ShortCutter_clear              : ClearShortCuts()
          Case #Gadget_ShortCutter_exit               : Program\QuitValue = 1
          Case #Gadget_ShortCutter_current            : CopyShortcutToList()
          Case #Gadget_ShortCutEdit_keyboardshortcut  :
          Case #Gadget_ShortCutEdit_save              : SaveShortCut()
          Case #Gadget_ShortCutEdit_exit              : CloseMyWindow(#Window_ShortCutEdit)
        EndSelect
    EndSelect
  Until Program\QuitValue
  CloseWindow(#Window_ShortCutter)
EndIf
End
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4790
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: [PB5.11] Mass Keyboard shortcut list producer

Post by Fangbeast »

This is what the default autocreated include file looks like. I am sure I will need more than this at some stage but for now, I need to fix bugs.

Code: Select all

;==========================================================================================================================
; My default KeyBoard ShortCut values
;==========================================================================================================================

Structure KeyData               ; Key value            ; Key pair
  AddNewRecord.i                 ; 131137               ; Ctrl + A        ; Add a new record to the database 
  BashOldRecord.i                ; 131138               ; Ctrl + B        ; Soundly bash the old record 
  CopyCurrentRecord.i            ; 131139               ; Ctrl + C        ; Copy the formatted record to the clipboard 
  DeleteCurrentRecord.i          ; 131140               ; Ctrl + D        ; Delete the currently selected record 
  EditCurrentRecord.i            ; 131141               ; Ctrl + E        ; Edit the currently selected record 
  FindRecords.i                  ; 131142               ; Ctrl + F        ; Find records using advanced SQL syntax 
EndStructure

;==========================================================================================================================
; Keyboard shortcuts text holders
;==========================================================================================================================

Structure MenuTextData          ; Key value            ; Key pair
  AddNewRecord.s                 ; 131137               ; Ctrl + A        ; Add a new record to the database 
  BashOldRecord.s                ; 131138               ; Ctrl + B        ; Soundly bash the old record 
  CopyCurrentRecord.s            ; 131139               ; Ctrl + C        ; Copy the formatted record to the clipboard 
  DeleteCurrentRecord.s          ; 131140               ; Ctrl + D        ; Delete the currently selected record 
  EditCurrentRecord.s            ; 131141               ; Ctrl + E        ; Edit the currently selected record 
  FindRecords.s                  ; 131142               ; Ctrl + F        ; Find records using advanced SQL syntax 
EndStructure

;==========================================================================================================================
; Keyboard shortcuts menu text
;==========================================================================================================================

MenuText\AddNewRecord           = "Add a new record to the database"                  ; 131137                ; Ctrl + A        
MenuText\BashOldRecord          = "Soundly bash the old record"                       ; 131138                ; Ctrl + B        
MenuText\CopyCurrentRecord      = "Copy the formatted record to the clipboard"        ; 131139                ; Ctrl + C        
MenuText\DeleteCurrentRecord    = "Delete the currently selected record"              ; 131140                ; Ctrl + D        
MenuText\EditCurrentRecord      = "Edit the currently selected record"                ; 131141                ; Ctrl + E        
MenuText\FindRecords            = "Find records using advanced SQL syntax"            ; 131142                ; Ctrl + F        

;==========================================================================================================================
; Global variables
;==========================================================================================================================

Global Keys.KeyData                                                                  ; All my keyboard shortcuts
Global MenuText.MenuTextData                                                         ; All repeated menu texts

;==========================================================================================================================
; Keyboard shortcuts database procedural declaration
;==========================================================================================================================

Declare OpenSystemDatabase()

;==========================================================================================================================
; Keyboard shortcuts table procedure
;==========================================================================================================================

Procedure OpenSystemDatabase()
  Query.s = "CREATE TABLE IF NOT EXISTS KeyboardShortcuts("
  Query.s + "AddNewRecord TEXT, BashOldRecord TEXT, CopyCurrentRecord TEXT, DeleteCurrentRecord TEXT, "
  Query.s + "EditCurrentRecord TEXT, FindRecords TEXT, "
  Query.s + "Record INTEGER PRIMARY KEY AUTOINCREMENT)"
  If DatabaseUpdate(Program\DatabaseHandle, Query.s)
    If CountRecords("SELECT COUNT(Record) AS TotalRecs FROM KeyboardShortcuts") = "0"
      Query.s = "INSERT INTO KeyboardShortcuts("
      Query.s + "AddNewRecord, BashOldRecord, CopyCurrentRecord, DeleteCurrentRecord, "
      Query.s + "EditCurrentRecord, FindRecords) "
      Query.s + " VALUES("
      Query.s + "131137, 131138, 131139, 131140, "
      Query.s + "131141, 131142) "
      If DatabaseUpdate(Program\DatabaseHandle, Query.s)
      EndIf
    EndIf
  EndIf
EndProcedure
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4790
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: [PB5.11] Mass Keyboard shortcut list producer

Post by Fangbeast »

Fixed a bumload of errors. Replace the old "ExportShortCutsToCode()" procedure with this one.

The following sections are generated in the include file:

KeyData Structure
MenuTextData structure
Structure globals

Menu text structure filled in
Procedure Declares

System database procedure created for keyboard shortcuts
Keyboard shortcuts loader procedure
Image menu creation procedure
Keyboard shortcuts data section

Keyboard shortcuts enumeration
Menu variables enumeration
Menubar icon images enumeration

Menubar icon images caught from data section
Menubar icon images data section

1. 2 things have to be worked on yet (for my use): The current window name to have everything generated for is set manually at the top of the export procedure (Without the hash!! : WindowName.s = "WhateverMyWindownameIs"

2. The name of your icon files should be the name of your keyboard variables (Without the spaces) as this is not an interactive picker.

I am now autogenerating 217 lines of code that I no longer have to type manually and can redo this for 30 of my applications which use the same way of doing things.

I'll see how far I can push this:)

Code: Select all

;==========================================================================================================================
; Export keyboard shortcuts to pb include file
;==========================================================================================================================

Procedure ExportShortCutsToCode()
  
  ;------------------------------------------------------------------------------------------------
  ; Temporary window name to test with
  ;------------------------------------------------------------------------------------------------
  
  WindowName.s  = "WhateverMyWindownameIs"
  
  HeadingLine.s = LSet(";", 123, "=")
  BlankLine.s   = ""
  
  ;------------------------------------------------------------------------------------------------
  ; Count the number of shortcuts in the list to be processed
  ;------------------------------------------------------------------------------------------------
  
  NumShortCuts.i = CountGadgetItems(#Gadget_ShortCutter_keylist)
  
  ;------------------------------------------------------------------------------------------------
  ; Only execute the following code if we have any shortcuts to be processed
  ;------------------------------------------------------------------------------------------------
  
  If NumShortCuts.i
    
    ;----------------------------------------------------------------------------------------------
    ; Clear the existing list of shortcuts to be processed
    ;----------------------------------------------------------------------------------------------
    
    ClearList(Keys.s())
    
    ;----------------------------------------------------------------------------------------------
    ; Grab all the shortcuts into a linked list for repeat processing
    ;----------------------------------------------------------------------------------------------
    
    For ShortCutGrab.i = 0 To NumShortCuts.i - 1
      AddElement(Keys.s())
      Keys()\KeyDataName   = RemoveString(GetGadgetItemText(#Gadget_ShortCutter_keylist, ShortCutGrab.i, 1), " ", 1, 1)
      Keys()\KeyDataValue  =              GetGadgetItemText(#Gadget_ShortCutter_keylist, ShortCutGrab.i, 2)
      Keys()\KeyDataKeys   =              GetGadgetItemText(#Gadget_ShortCutter_keylist, ShortCutGrab.i, 3)
      Keys()\KeyDataText   =              GetGadgetItemText(#Gadget_ShortCutter_keylist, ShortCutGrab.i, 4)
    Next
        
    ;----------------------------------------------------------------------------------------------
    ; Ask the user which pb include file we are going to be exporting all the work to
    ;----------------------------------------------------------------------------------------------
    
    ShortCutFile.s = SaveFileRequester("Save Keyboard shortcuts export", GetCurrentDirectory() + "_DefaultInclude", "PBI (*.pbi)|*.pbi", 0)
    
    ;----------------------------------------------------------------------------------------------
    ; If the user didn't add the correct extension, tag one on
    ;----------------------------------------------------------------------------------------------
    
    If LCase(GetExtensionPart(ShortCutFile.s)) <> "pbi"
      ShortCutFile.s + ".pbi"
    EndIf
    
    ;----------------------------------------------------------------------------------------------
    ; Proceed if the user selected a file to save to
    ;----------------------------------------------------------------------------------------------
    
    If ShortCutFile.s
      
      ;--------------------------------------------------------------------------------------------
      ; Create the output file to save to
      ;--------------------------------------------------------------------------------------------
      
      OutputFile.i = CreateFile(#PB_Any, ShortCutFile.s)
      
      ;--------------------------------------------------------------------------------------------
      ; Proceed if we got a valid file handle for the output file
      ;--------------------------------------------------------------------------------------------
      
      If OutputFile.i
        
        ;------------------------------------------------------------------------------------------
        ; Key data structure heading
        ;------------------------------------------------------------------------------------------
        
        WriteStringN(OutputFile.i, BlankLine.s)
        WriteStringN(OutputFile.i, HeadingLine.s)
        WriteStringN(OutputFile.i, "; My default KeyBoard ShortCut values")
        WriteStringN(OutputFile.i, HeadingLine.s)
        WriteStringN(OutputFile.i, BlankLine.s)
        
        ;------------------------------------------------------------------------------------------
        ; Key data structure. No values in here, just headings
        ;------------------------------------------------------------------------------------------
        
        WriteStringN(OutputFile.i, "Structure KeyData               ; Key value            ; Key pair")
        
        ForEach Keys.s()
          WriteString(OutputFile.i, "  " + FormatField(Keys()\KeyDataName + ".i",   30))
          WriteString(OutputFile.i, "; ")
          WriteString(OutputFile.i,        FormatField(Keys()\KeyDataValue,         20))
          WriteString(OutputFile.i, "; ")
          WriteString(OutputFile.i,        FormatField(Keys()\KeyDataKeys,          15))
          WriteString(OutputFile.i, "; ")
          WriteStringN(OutputFile.i,       FormatField(Keys()\KeyDataText,          15))
        Next 
        
        WriteStringN(OutputFile.i, "EndStructure")
        
        ;------------------------------------------------------------------------------------------
        ; Key menu text data structure heading
        ;------------------------------------------------------------------------------------------
        
        WriteStringN(OutputFile.i, BlankLine.s)
        WriteStringN(OutputFile.i, HeadingLine.s)
        WriteStringN(OutputFile.i, "; Keyboard shortcuts text holders")
        WriteStringN(OutputFile.i, HeadingLine.s)
        WriteStringN(OutputFile.i, BlankLine.s)
        
        ;------------------------------------------------------------------------------------------
        ; Key menu text data structure. No values in here
        ;------------------------------------------------------------------------------------------
        
        WriteStringN(OutputFile.i, "Structure MenuTextData          ; Key value            ; Key pair")
        
        ForEach Keys.s()
          WriteString(OutputFile.i, "  " + FormatField(Keys()\KeyDataName + ".s",   30))
          WriteString(OutputFile.i, "; ")
          WriteString(OutputFile.i,        FormatField(Keys()\KeyDataValue,         20))
          WriteString(OutputFile.i, "; ")
          WriteString(OutputFile.i,        FormatField(Keys()\KeyDataKeys,          15))
          WriteString(OutputFile.i, "; ")
          WriteStringN(OutputFile.i,       FormatField(Keys()\KeyDataText,          15))
        Next
        
        WriteStringN(OutputFile.i, "EndStructure")
        
        ;------------------------------------------------------------------------------------------
        ; Global structure declarations heading
        ;------------------------------------------------------------------------------------------
        
        WriteStringN(OutputFile.i, BlankLine.s)
        WriteStringN(OutputFile.i, HeadingLine.s)
        WriteStringN(OutputFile.i, "; Global variables")
        WriteStringN(OutputFile.i, HeadingLine.s)
        WriteStringN(OutputFile.i, BlankLine.s)
        
        ;------------------------------------------------------------------------------------------
        ; Global structure declarations
        ;------------------------------------------------------------------------------------------
        
        WriteStringN(OutputFile.i, "Global Keys.KeyData                                                                  ; All my keyboard shortcuts")
        WriteStringN(OutputFile.i, "Global MenuText.MenuTextData                                                         ; All repeated menu texts")

        ;------------------------------------------------------------------------------------------
        ; Menu text variables heading
        ;------------------------------------------------------------------------------------------
        
        WriteStringN(OutputFile.i, BlankLine.s)
        WriteStringN(OutputFile.i, HeadingLine.s)
        WriteStringN(OutputFile.i, "; Keyboard shortcuts menu text")
        WriteStringN(OutputFile.i, HeadingLine.s)
        WriteStringN(OutputFile.i, BlankLine.s)
        
        ;------------------------------------------------------------------------------------------
        ; Menu text variables filled in
        ;------------------------------------------------------------------------------------------
        
        ForEach Keys.s()
          WriteString(OutputFile.i,   FormatField("MenuText\" + RemoveString(Keys()\KeyDataName, " ", 1, 1), 30)  + " = ")
          WriteString(OutputFile.i,   FormatField(Chr(34) + Keys()\KeyDataText + Chr(34), 50))
          WriteString(OutputFile.i,   " ; ")
          WriteString(OutputFile.i,   FormatField(Keys()\KeyDataValue,  20))
          WriteString(OutputFile.i,   " ; ")
          WriteStringN(OutputFile.i,  FormatField(Keys()\KeyDataKeys,   15))
        Next
        
        ;------------------------------------------------------------------------------------------
        ; Write the keyboard shortcuts heading
        ;------------------------------------------------------------------------------------------
        
        WriteStringN(OutputFile.i, BlankLine.s)
        WriteStringN(OutputFile.i, HeadingLine.s)
        WriteStringN(OutputFile.i, "; Keyboard shortcuts database procedural declaration")
        WriteStringN(OutputFile.i, HeadingLine.s)
        WriteStringN(OutputFile.i, BlankLine.s)
        
        ;------------------------------------------------------------------------------------------
        ; Create the procedural declaration for the database table
        ;------------------------------------------------------------------------------------------
        
        WriteStringN(OutputFile.i,            "DECLARE OpenSystemDatabase()")
        WriteStringN(OutputFile.i,            "DECLARE LoadkeyboardShortCuts()")
        WriteStringN(OutputFile.i,            "DECLARE CreateImageMenu()")
        
        ;------------------------------------------------------------------------------------------
        ; Write the keyboard shortcuts heading
        ;------------------------------------------------------------------------------------------
        
        WriteStringN(OutputFile.i, BlankLine.s)
        WriteStringN(OutputFile.i, HeadingLine.s)
        WriteStringN(OutputFile.i, "; Keyboard shortcuts table procedure")
        WriteStringN(OutputFile.i, HeadingLine.s)
        WriteStringN(OutputFile.i, BlankLine.s)
        
        ;------------------------------------------------------------------------------------------
        ; Write the keyboard shortcuts table out
        ;------------------------------------------------------------------------------------------
        
        WriteStringN(OutputFile.i,            "PROCEDURE OpenSystemDatabase()")
        WriteStringN(OutputFile.i,            "  Query.s = " + Chr(34) + "CREATE TABLE IF NOT EXISTS KeyboardShortcuts(" + Chr(34))
        
        LastElementIndex = ListSize(Keys.s()) - 1
        
        Counter = 0
        
        ForEach Keys.s()
          Index = ListIndex(Keys.s())
          If Index & 3 = 3 Or Index = LastElementIndex
            WriteStringN(OutputFile.i,      "  Query.s + " + Chr(34) + OutString.s + Keys()\KeyDataName + " TEXT, " + Chr(34))
            OutString.s = ""
          Else
            OutString.s + Keys()\KeyDataName + " TEXT, "
          EndIf
        Next
        
        WriteStringN(OutputFile.i,            "  Query.s + " + Chr(34) + "Record INTEGER PRIMARY KEY AUTOINCREMENT)" + Chr(34))
        WriteStringN(OutputFile.i,            "  If DatabaseUpdate(Program\DatabaseHandle, Query.s)")
        WriteStringN(OutputFile.i,            "    If CountRecords(" + Chr(34) + "SELECT COUNT(Record) AS TotalRecs FROM KeyboardShortcuts" + Chr(34) + ") = "  + Chr(34) + "0" + Chr(34))
        WriteStringN(OutputFile.i,            "      Query.s = " + Chr(34) + "INSERT INTO KeyboardShortcuts("  + Chr(34))

        ForEach Keys.s()
          Index = ListIndex(Keys.s())
          If Index & 3 = 3 Or Index = LastElementIndex
            If Index = LastElementIndex
              WriteStringN(OutputFile.i,      "      Query.s + " + Chr(34) + OutString.s + Keys()\KeyDataName + ") " + Chr(34))
            Else
              WriteStringN(OutputFile.i,      "      Query.s + " + Chr(34) + OutString.s + Keys()\KeyDataName + ", " + Chr(34))
            EndIf
            OutString.s = ""
          Else
            OutString.s + Keys()\KeyDataName + ", "
          EndIf
        Next
        
        WriteStringN(OutputFile.i,            "      Query.s + " + Chr(34) + "VALUES("  + Chr(34))
        
        ForEach Keys.s()
          Index = ListIndex(Keys.s())
          If Index & 3 = 3 Or Index = LastElementIndex
            If Index = LastElementIndex
              WriteStringN(OutputFile.i,      "      Query.s + " + Chr(34) + OutString.s + "'" + Keys()\KeyDataValue + "')" + Chr(34))
            Else
              WriteStringN(OutputFile.i,      "      Query.s + " + Chr(34) + OutString.s + "'" + Keys()\KeyDataValue + "', " + Chr(34))
            EndIf
            OutString.s = ""
          Else
            OutString.s +  "'" + Keys()\KeyDataValue + "', "
          EndIf
        Next
        
        WriteStringN(OutputFile.i,            "      If DatabaseUpdate(Program\DatabaseHandle, Query.s)")
        WriteStringN(OutputFile.i,            "      EndIf")
        WriteStringN(OutputFile.i,            "    EndIf")
        WriteStringN(OutputFile.i,            "  EndIf")
        WriteStringN(OutputFile.i,            "EndProcedure")
        
        ;------------------------------------------------------------------------------------------
        ; Write the keyboard shortcuts heading
        ;------------------------------------------------------------------------------------------
        
        WriteStringN(OutputFile.i, BlankLine.s)
        WriteStringN(OutputFile.i, HeadingLine.s)
        WriteStringN(OutputFile.i, "; Keyboard shortcuts loader procedure")
        WriteStringN(OutputFile.i, HeadingLine.s)
        WriteStringN(OutputFile.i, BlankLine.s)
        
        ;------------------------------------------------------------------------------------------
        ; Write the keyboard shortcuts loader
        ;------------------------------------------------------------------------------------------
        
        WriteStringN(OutputFile.i,            "PROCEDURE LoadkeyboardShortCuts()")
        WriteStringN(OutputFile.i,            "  ; Remove all existing shortcuts for this window")
        WriteStringN(OutputFile.i,            "  RemoveKeyboardShortcut(" + "#" + WindowName.s + ", #PB_Shortcut_All)")
        WriteStringN(OutputFile.i,            "  ; Load the keyboard shortcuts from the system database")
        WriteStringN(OutputFile.i,            "  If DatabaseQuery(Program\DatabaseHandle, " + Chr(34) + "Select * FROM KeyboardShortcuts WHERE Record='1'" + Chr(34))
        WriteStringN(OutputFile.i,            "    While NextDatabaseRow(Program\DatabaseHandle)")
          
        RowCounter.i = 0
        
        ForEach Keys.s()
          CurrentKey.s = FormatField("Keys\" + Keys()\KeyDataName, 30) + " = Val(GetDatabaseString(Program\DatabaseHandle,  " + Str(RowCounter.i) + "))"
          RowCounter.i + 1
          WriteStringN(OutputFile.i, "      " + CurrentKey.s)
        Next           
          
        WriteStringN(OutputFile.i,            "    Wend")
        WriteStringN(OutputFile.i,            "    FinishDatabaseQuery(Program\DatabaseHandle)")
        WriteStringN(OutputFile.i,            "    ; Reload the shortcuts with the new values.")
        
        ForEach Keys.s()
          CurrentKey.s = "AddKeyboardShortcut(" + "#" + WindowName.s + ", " + FormatField("Keys\" + Keys()\KeyDataName + ", ", 30) + "#ShortCut_" + Keys()\KeyDataName + ")"
          WriteStringN(OutputFile.i,          "    " + CurrentKey.s)
        Next           
        
        WriteStringN(OutputFile.i,            "  Else")
        WriteStringN(OutputFile.i,            "    ; Problem with the database query")
        WriteStringN(OutputFile.i,            "  EndIf")
        
        WriteStringN(OutputFile.i,            "EndProcedure")
        
        ;------------------------------------------------------------------------------------------
        ; Write the image menu creation area heading
        ;------------------------------------------------------------------------------------------
        
        WriteStringN(OutputFile.i, BlankLine.s)
        WriteStringN(OutputFile.i, HeadingLine.s)
        WriteStringN(OutputFile.i, "; Image menu creation procedure")
        WriteStringN(OutputFile.i, HeadingLine.s)
        WriteStringN(OutputFile.i, BlankLine.s)
        
        ;------------------------------------------------------------------------------------------
        ; Write the image menu creation area
        ;------------------------------------------------------------------------------------------
        
        WriteStringN(OutputFile.i,            "PROCEDURE CreateImageMenu()")
        WriteStringN(OutputFile.i,            "  ; Remove all existing shortcuts for this window")
        WriteStringN(OutputFile.i,            "  CreateImageMenu(#MenuBar_" + WindowName.s + ", WindowID(" + "#" + WindowName.s + "), #PB_Menu_ModernLook")
        
        ForEach Keys.s()
          WriteStringN(OutputFile.i, FormatField("  MenuItem(#MenuBar_" + WindowName.s + "_" + Keys()\KeyDataName + ", ", 70) + FormatField("MenuText\" + Keys()\KeyDataName, 30) + " + Chr(09) + " + FormatField("GetShortcutText(Keys\" + Keys()\KeyDataName + "),", 50) + FormatField("ImageID(#MenuBarIcon_" + WindowName.s + "_" + Keys()\KeyDataName + ")", 50))
        Next
        
        WriteStringN(OutputFile.i,            "EndProcedure")
        
        ;------------------------------------------------------------------------------------------
        ; Write the shortcut keys datasection heading
        ;------------------------------------------------------------------------------------------
        
        WriteStringN(OutputFile.i, BlankLine.s)
        WriteStringN(OutputFile.i, HeadingLine.s)
        WriteStringN(OutputFile.i, "; Keyboard shortcuts data section")
        WriteStringN(OutputFile.i, HeadingLine.s)
        WriteStringN(OutputFile.i, BlankLine.s)
        
        ;------------------------------------------------------------------------------------------
        ; Write the shortcut keys datasection #ShortCut_ + WindowName + Keys()\KeyDataName
        ;------------------------------------------------------------------------------------------
        
        WriteStringN(OutputFile.i,            "Enumeration #GadgetIndex")
        WriteStringN(OutputFile.i,            "  ; Main program GUI shortcuts")
        
        ForEach Keys.s()
          WriteStringN(OutputFile.i,          "  #ShortCut_" + WindowName.s + "_" + Keys()\KeyDataName)
        Next
        
        WriteStringN(OutputFile.i, BlankLine.s)
        
        WriteStringN(OutputFile.i,            "  ; Main program GUI menubar constants")
        
        ForEach Keys.s()
          WriteStringN(OutputFile.i,          "  #MenuBar_" + WindowName.s + "_" + Keys()\KeyDataName)
        Next
        
        WriteStringN(OutputFile.i,            "EndEnumeration")
        
        ;------------------------------------------------------------------------------------------
        ; Menubar icon image constants heading
        ;------------------------------------------------------------------------------------------
        
        WriteStringN(OutputFile.i, BlankLine.s)
        WriteStringN(OutputFile.i, HeadingLine.s)
        WriteStringN(OutputFile.i, "; Menubar icon image constants")
        WriteStringN(OutputFile.i, HeadingLine.s)
        WriteStringN(OutputFile.i, BlankLine.s)
        
        ;------------------------------------------------------------------------------------------
        ; Menubar icon image constants heading
        ;------------------------------------------------------------------------------------------
        
        WriteStringN(OutputFile.i,            "Enumeration #ImageIndex")
        WriteStringN(OutputFile.i,            "  ; Main program MenuBar icon constants")
        
        ForEach Keys.s()
          WriteStringN(OutputFile.i,          "  #MenuBarIcon_" + WindowName.s + "_" + Keys()\KeyDataName)
        Next
        
        WriteStringN(OutputFile.i,            "EndEnumeration")
        
        ;------------------------------------------------------------------------------------------
        ; Load the menuBar icon images heading
        ;------------------------------------------------------------------------------------------
        
        WriteStringN(OutputFile.i, BlankLine.s)
        WriteStringN(OutputFile.i, HeadingLine.s)
        WriteStringN(OutputFile.i, "; Menubar icon images")
        WriteStringN(OutputFile.i, HeadingLine.s)
        WriteStringN(OutputFile.i, BlankLine.s)
        
        ;------------------------------------------------------------------------------------------
        ; Load the menuBar icon images heading
        ;------------------------------------------------------------------------------------------
        
        ForEach Keys.s()
          WriteStringN(OutputFile.i, FormatField("CatchImage(#MenuBarIcon_" + WindowName.s + "_" + Keys()\KeyDataName + ",", 67) + "?_MenuBarIcon_" + Keys()\KeyDataName + ")")
        Next
        
        ;------------------------------------------------------------------------------------------
        ; DataSection for image includes heading
        ;------------------------------------------------------------------------------------------
        
        WriteStringN(OutputFile.i, BlankLine.s)
        WriteStringN(OutputFile.i, HeadingLine.s)
        WriteStringN(OutputFile.i, "; Menubar icon images")
        WriteStringN(OutputFile.i, HeadingLine.s)
        WriteStringN(OutputFile.i, BlankLine.s)
        
        ;------------------------------------------------------------------------------------------
        ; DataSection for image includes heading
        ;------------------------------------------------------------------------------------------
        
        WriteStringN(OutputFile.i, "DataSection")
          
        ForEach Keys.s()
          WriteStringN(OutputFile.i, FormatField("  ?_MenuBarIcon_" + Keys()\KeyDataName + ":", 50) + "IncludeBinary  " + Chr(34) + "Images\MenuBarIcon_" + Keys()\KeyDataName + ".ico" + Chr(34))
        Next
        
        WriteStringN(OutputFile.i, "EndDataSection")
        
        ;------------------------------------------------------------------------------------------
        ; Close the output file as we are finished writing to it
        ;------------------------------------------------------------------------------------------
        
        CloseFile(OutputFile.i)
        
        ;------------------------------------------------------------------------------------------
        ; 
        ;------------------------------------------------------------------------------------------
        
      Else
        ; Output file could not be created  
      EndIf
      
      ;--------------------------------------------------------------------------------------------
      ; 
      ;--------------------------------------------------------------------------------------------
      
    Else
      ; No filename specified, user aborted
    EndIf
    
    ;----------------------------------------------------------------------------------------------
    ; 
    ;----------------------------------------------------------------------------------------------
    
  Else
    ; No shortcuts in list to save
  EndIf
  
  ;------------------------------------------------------------------------------------------------
  ; 
  ;------------------------------------------------------------------------------------------------
  
EndProcedure
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: [PB5.11] Mass Keyboard shortcut list producer

Post by BorisTheOld »

Fangbeast wrote:I am now autogenerating 217 lines of code that I no longer have to type manually and can redo this for 30 of my applications which use the same way of doing things.

I'll see how far I can push this
Keep up the good work. You can push it as far as you like.

Our in-house Data Dictionary package allows us to autogenerate several million lines of code for all our applications. It's the only way to fly. :)

This all started in 1970 when I bought a book titled, A Compiler Generator, and used its concepts to generate application code. It's a bootstrap strategy for creating code generators that are able to generate themselves plus the next version. Our code generators have evolved through various versions: ASM --> PL/I --> COBOL --> Visual Basic --> PowerBasic --> PureBasic

Here's a very small sample of PowerBasic code from our Data Dictionary package, which of course generates itself. The current manifestation of our software uses PowerBasic, and we're in the process of creating the templates to generate the PureBasic version. This will be our first version to incorporate OOP, custom drawn gadgets, auto-sizing, and cross-platform support.

Code: Select all

Sub subValS1782X11R1(ByRef brlError As Long, ByRef brlRuleSatisfied As Long, ByRef brnFileA As Dword)
'
'  screen 1782  :  box 11  :  rule 1
'
  Dim cRuleFlag                        As Local String     ' Y = rule is selected
  Dim cTestFlag                        As Local String     ' Y = test is successful
  Dim sMsgText                         As Local String     ' error message

  cRuleFlag = $cFLAG_NO                                    ' assume rule not selected
  If prrR270.exnConstantA = %iVALUE_0 Then
    cRuleFlag = $cFLAG_YES                                 ' rule has been selected
  End If

  If cRuleFlag = $cFLAG_NO Then                            ' try next rule
    brlError = %lVALUE_FALSE
    brlRuleSatisfied = %lVALUE_FALSE
    Exit Sub
  End If

  cTestFlag = $cFLAG_NO                                    ' assume no tests successful

' rule test 1
  If cTestFlag = $cFLAG_NO Then
    sMsgText = $sMSG_4041                                  ' Value must not be zero
    If brnFileA <> %iVALUE_0 Then
      cTestFlag = $cFLAG_YES                               ' test is successful
    End If
  End If

  If cTestFlag = $cFLAG_NO Then                            ' box validation failed
    modGui_subMsgDisplay(prrState, sMsgText)
    brlError = %lVALUE_TRUE
    brlRuleSatisfied = %lVALUE_FALSE
    Exit Sub
  End If

  brlError = %lVALUE_FALSE                                 ' successful box validation
  brlRuleSatisfied = %lVALUE_TRUE                          ' validation complete

End Sub
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4790
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: [PB5.11] Mass Keyboard shortcut list producer

Post by Fangbeast »

New version about due but too big to post now so trying to figure out where to post the project.

1. Added the ability to add headings (for MenuTitle areas etc) for when you export the code and creates the menu procedure for you.

2. Added the ability to choose icons for the menu options. The data section is created for those, icons are renamed to the menu variables etc.

3. You can now drag and drop shortcuts on the list.

4. Added the balloon tooltip tracking procedure and associated code.

5. Added the event checking area for the created constants.

6. A project directory is created off the current one into which all files are put for the associated/exported code.

7. Global tooltips procedure (dummy procedure)

8. Some defaults for the main event checking procedure.

More later if anyone is still reading this.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: [PB5.11] Mass Keyboard shortcut list producer

Post by netmaestro »

Nice work Fangs, thanks for sharing it 8)
BERESHEIT
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4790
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: [PB5.11] Mass Keyboard shortcut list producer

Post by Fangbeast »

netmaestro wrote:Nice work Fangs, thanks for sharing it 8)
It's actually quite rubbish code and since I haven't figured out where to upload the project (and nobody commented on it yet, I did quite a lot of work on it.

The previous version shoved everything into a monolithic include file from which you had to copy and paste to your own procedures and I figured that it was a pain.

1. This version asks you for the target window name and target program name and creates the %Programname%_MyDeclarations, %Programname%_MyConstants and %Programname%.pb files, putting the code into the correct files and also writes the included procedures as separate files in the "Modules\" directory.

2. And I did a lot of formatting, including tons of spaces for more readable code due to my crappy eyesight.

3. Split the export routine into separate procedures because it was over 1,000 lines and getting very hard to read for me.

I'll upload it to my ISP's web site tonight in case anyone is still reading. Hope I squashed and squished all bugs.

Humbug!!:) This is the current version of the junk. Undoubtedly I have left in bugs. Get out the Mortein!!

http://members.westnet.com.au/bangfeast ... tCutter.7z
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4790
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: [PB5.11] Mass Keyboard shortcut list producer

Post by Fangbeast »

Minor update (I'll update tomorrow)

1. It came to my lazy mind that I should have written out the include sections that handled the events named by the variables. So they are also created now as below:

Code: Select all

;==========================================================================================================================
; Files menu includes
;==========================================================================================================================

XIncludeFile "Modules\OpenDatabase.pbi"                                                    ; 
XIncludeFile "Modules\ReloadMaster.pbi"                                                    ; 
XIncludeFile "Modules\DumpTables.pbi"                                                      ; 
XIncludeFile "Modules\UndumpTables.pbi"                                                    ; 
XIncludeFile "Modules\VacuumTables.pbi"                                                    ; 
XIncludeFile "Modules\ExitProgram.pbi"                                                     ; 
2. As the sections are written out, subdirectories are also created for the heading sections for you to create the procedures in (for neatness).

For the sample project included, the created directories look like below:

%CurrentDir%\Project\ The target project directory
\Project\Icons\ The target project icons
\Project\Modules\ The target project main modules directory
\Project\Modules\Category The target project 'Category' menu directory

Etc, etc.

P.s. Not really lazy but my wrists are starting to complain of 30 years of mouse work:(
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4790
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: [PB5.11] Mass Keyboard shortcut list producer

Post by Fangbeast »

*Updated*

1. Remembered to add the xInclude statements and declare statements for the exported project.

2. Ruthlessly eliminated all the debug statements (shot the with ping pong balls and they died gasping) and redirected them to the message bar.

3. Fixed the forgotten loaded shortcut file message (went missing in Barbados!).

Hmm, I think that was all.

Maybe I'll also create the empty procedures as well, if I think I need them but for now, I can actually use this to create the skeleton of my next project.

**EDIT** Couldn't help myself, I needed it. Empty procedures created from the variable names.

**EDIT** It annoyed me that I had to enter the window name and target program name each time I started shortcutter so it is saved at end and loaded at start. Anyone want the changes?
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
Post Reply