Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can a toolbar display/support a checkbox?
#1
I wish to use a checkbox on a toolbar to toggle an option and probably save that option to an ini file, (if most appropriate), for future reference.
If the checkbox is checked, I want a particular macro to be enabled and allowed to be triggered. If not checked, particular macro should be disabled and unavailable.
The toolbar will be attached to a window.
Just say HEMP!
#2
example
Toolbar Toolbar73
Code:
Copy      Help
;/hook QmToolbar_HookCheck
Check1 :out QmToolbar_IsChecked ;;[check c1] * cut.ico
Simple button :out "simple"
;comment
Check2 :out QmToolbar_IsChecked ;;[check c2] * copy.ico
Check, don't save :out QmToolbar_Check(2) * paste.ico
Enable macro :if(QmToolbar_IsChecked) dis- "Macro1111"; else dis+ "Macro1111" ;;[check enableMacro]

Need these functions. Can be used with all toolbars, don't need to edit.
Function QmToolbar_HookCheck
Code:
Copy      Help
;/
function# hwnd message wParam lParam

;QM toolbar hook function that manages button checked states.
;In toolbar text finds text [check id] in button code comments, sets checkbox style for these buttons.
;Here id is some alphanumeric text, unique in the toolbar, used to save/restore button states with this name. Example: Label :out 1 ;;[check c1] * icon.ico
;When closing toolbar, saves button checked states in macro resources of the toolbar. When toolbar created or modified, restores button checked states.


;OutWinMsg message wParam lParam
sel message
,case WM_INITDIALOG
,;gInit
,sub.CheckboxesInitOrSave 1 hwnd
,
,case WM_DESTROY
,sub.CheckboxesInitOrSave 0 hwnd
,
,case WM_NOTIFY
,NMHDR* nh=+lParam
,sel nh.code
,,case TBN_DELETINGBUTTON ;;toolbar modified, need to update checkboxes
,,SetTimer hwnd 8216 50 0
,
,case WM_TIMER
,sel wParam
,,case 8216
,,KillTimer hwnd wParam
,,goto gInit


#sub CheckboxesInitOrSave
function !init hwnd

int htb=id(9999 hwnd)
int* p=+GetWindowLong(hwnd 0)
int iid=p[2]
str s1.getmacro(iid)
ARRAY(str) a=s1; int i
for i 1 a.len
,str& s=a[i]
,str cName
,if(findrx(s "[^ ;].+? :.+?;;.*?\[check +(\w+).*?\]" 0 0 cName 1)<0) continue
,;out s; out cName
,TBBUTTONINFOW b.cbSize=sizeof(b); b.dwMask=TBIF_STYLE|TBIF_STATE
,if(SendMessage(htb TB_GETBUTTONINFOW i &b)<0) continue
,int check=_qmfile.SettingGetI(+iid cName); err
,if init
,,b.fsStyle|BTNS_CHECK
,,if(check) b.fsState|TBSTATE_CHECKED
,,SendMessage(htb TB_SETBUTTONINFOW i &b)
,else
,,int icCheckedNow=b.fsState&TBSTATE_CHECKED!=0
,,if(icCheckedNow!=check) _qmfile.SettingAddI(+iid cName icCheckedNow)
Function QmToolbar_IsChecked
Code:
Copy      Help
;/
function#

;Gets the checked state of the clicked button of current QM toolbar.
;Returns 1 if checked, 0 if not.

;REMARKS
;Call this from a QM toolbar item code.
;Gets button from mouse position.


int h=id(9999 TriggerWindow)
POINT p; xm p h 1
int i=SendMessage(h TB_HITTEST 0 &p)
if(i<0) ret

ret SendMessage(h TB_ISBUTTONCHECKED i 0)
Function QmToolbar_Check
Code:
Copy      Help
;/
function# check ;;check: 0 uncheck, 1 check, 2 toggle

;Checks, unchecks or toggles the check state of the clicked button of current QM toolbar.
;Returns the final state: 1 checked, 0 unchecked.

;REMARKS
;Call this from a QM toolbar item code.
;Gets button from mouse position.


int h=id(9999 TriggerWindow)
POINT p; xm p h 1
int i=SendMessage(h TB_HITTEST 0 &p)
if(i<0) ret

int isChecked=SendMessage(h TB_ISBUTTONCHECKED i 0)
if(check<=1 and isChecked=check) ret check
check=!isChecked
if(!SendMessage(h TB_CHECKBUTTON i check)) ret isChecked
ret check
Function QmToolbar_GetSavedCheckState
Code:
Copy      Help
;/
function# $toolbarName $checkId

;Gets the saved checked state of a checkbox-button of a QM toolbar.
;Returns 1 if checked, 0 if not (or not saved).


ret _qmfile.SettingGetI(toolbarName checkId); err
Function QmToolbar_SetSavedCheckState
Code:
Copy      Help
;/
function $toolbarName $checkId isChecked

;Sets the saved checked state of a checkbox-button of a QM toolbar.


opt noerrorshere 1
_qmfile.SettingAddI(toolbarName checkId isChecked!=0)
#3
Wow you are so amazing. Thank you!

When I run the sample, the process stops with an error.
Error in QmToolbar_HookCheck: unknown identifier.
The Wow in:
Wow.CheckboxesInitOrSave 1 hwnd
is selected.

I seemed to have fixed this issue by replaceing "Wow" with "sub". The sample code works perfectly now.

Thanks again.
Just say HEMP!
#4
Thank you very much!
I only needed the check button without saving the state and some functions weren't necessary, but it works perfect!


Forum Jump:


Users browsing this thread: 1 Guest(s)