-
Notifications
You must be signed in to change notification settings - Fork 71
/
Class_Toolbar.ahk
1266 lines (1265 loc) · 62 KB
/
Class_Toolbar.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;=======================================================================================
;
; Class Toolbar
;
; Author: Pulover [Rodolfo U. Batista]
; AHK version: 1.1.23.01
;
; Class for AutoHotkey Toolbar custom controls
;=======================================================================================
;
; This class provides intuitive methods to work with Toolbar controls created via
; Gui, Add, Custom, ClassToolbarWindow32.
;
; Note: It's recommended to call any method only after Gui, Show. Adding or modifying
; buttons of a toolbar in a Gui that is not yet visible might fail eventually.
;
;=======================================================================================
;
; Toolbar Methods:
; Add([Options, Label1[=Text]:Icon[(Options)], Label2[=Text]:Icon[(Options)]...])
; AutoSize()
; Customize()
; Delete(Button)
; Export()
; Get([HotItem, TextRows, Rows, BtnWidth, BtnHeight, Style, ExStyle])
; GetButton(Button [, ID, Text, State, Style, Icon, Label, Index])
; GetButtonPos(Button [, OutX, OutY, OutW, OutH])
; GetButtonState(Button, StateQuerry)
; GetCount()
; GetHiddenButtons()
; Insert(Position [, Options, Label1[=Text]:Icon[(Options)], Label2[=Text]:Icon[(Options)]...])
; LabelToIndex(Label)
; ModifyButton(Button, State [, Set])
; ModifyButtonInfo(Button, Property, Value)
; MoveButton(Button, Target)
; OnMessage(CommandID)
; OnNotify(Param [, MenuXPos, MenuYPos, Label, ID, AllowCustom])
; Reset()
; SetButtonSize(W, H)
; SetDefault([Options, Label1[=Text]:Icon[(Options)], Label2[=Text]:Icon[(Options)]...])
; SetExStyle(Style)
; SetHotItem(Button)
; SetImageList(IL_Default [, IL_Hot, IL_Pressed, IL_Disabled])
; SetIndent(Value)
; SetListGap(Value)
; SetMaxTextRows([MaxRows])
; SetPadding(X, Y)
; SetRows([Rows, AddMore])
; ToggleStyle(Style)
;
; Presets Methods:
; Presets.Delete(Slot)
; Presets.Export(Slot, [ArrayOut])
; Presets.Import(Slot, [Options, Label1[=Text]:Icon, Label2[=Text]:Icon, Label3[=Text]:Icon...])
; Presets.Load(Slot)
; Presets.Save(Slot, Buttons)
;
;=======================================================================================
;
; Useful Toolbar Styles: Styles can be applied to Gui command options, e.g.:
; Gui, Add, Custom, ClassToolbarWindow32 0x0800 0x0100
;
; TBSTYLE_FLAT := 0x0800 - Shows separators as bars.
; TBSTYLE_LIST := 0x1000 - Shows buttons text on their side.
; TBSTYLE_TOOLTIPS := 0x0100 - Shows buttons text as tooltips.
; CCS_ADJUSTABLE := 0x0020 - Allows customization by double-click and shift-drag.
; CCS_NODIVIDER := 0x0040 - Removes the separator line above the toolbar.
; CCS_NOPARENTALIGN := 0x0008 - Allows positioning and moving toolbars.
; CCS_NORESIZE := 0x0004 - Allows resizing toolbars.
; CCS_VERT := 0x0080 - Creates a vertical toolbar (add WRAP to button options).
;
;=======================================================================================
Class Toolbar extends Toolbar.Private
{
;=======================================================================================
; Method: Add
; Description: Add button(s) to the end the toolbar. The Buttons parameters
; sets target Label, text caption and icon index for each
; button. If not a valid label name, a function name can be
; used instead.
; To add a separator call this method without parameters.
; Prepend any non letter or digit symbol, such as "-" or "*"
; to the label to add a hidden button. Hidden buttons won't
; be visible when Gui is shown but will still be available
; in the customize window. E.g.: "-Label=New:1", "*Label:2".
; Parameters:
; Options: Enter zero or more words, separated by space or tab, from the
; following list to set buttons' initial states and styles:
; Checked, Ellipses, Enabled, Hidden, Indeterminate, Marked,
; Pressed, Wrap, Button, Sep, Check, Group, CheckGroup,
; Dropdown, AutoSize, NoPrefix, ShowText, WholeDropdown.
; You can also set the minimum and maximum button width,
; for example W20-100 would set min to 20 and max to 100.
; This option affects all buttons in the toolbar when added or
; inserted but does not prevent modifying button sizes.
; If this parameter is blank it defaults to "Enabled", otherwise
; you must set this parameter to enable buttons.
; You may pass integer values that correspond to (a combination of)
; button styles. You cannot set states this way (it will always
; be set to "Enabled").
; Buttons: Buttons can be added in the following format: Label=Text:1,
; where "Label" is the target label to execute when the
; button is pressed, "Text" is caption to be displayed
; with the button or as a Tooltip if the toolbar has the
; TBSTYLE_TOOLTIPS style (this parameter can be omitted) and
; "1" can be any numeric value that represents the icon index
; in the ImageList (0 means no icon).
; You can include specific states and styles for a button appending
; them inside parenthesis after the icon. E.g.:
; "Label=Text:3(Enabled Dropdown)". This option can also be
; an Integer value, in this case the general options are
; ignored for that button.
; To add a separator between buttons specify "" or equivalent.
; Return: TRUE if successful, FALSE if there was a problem.
;=======================================================================================
Add(Options := "Enabled", Buttons*)
{
If (!Buttons.Length())
{
Struct := this.BtnSep(TBBUTTON, Options), this.DefaultBtnInfo.Push(Struct)
SendMessage, this.TB_ADDBUTTONS, 1, &TBBUTTON,, % "ahk_id " this.tbHwnd
If (ErrorLevel = "FAIL")
return False
}
Else If (Options = "")
Options := "Enabled"
For each, Button in Buttons
{
If !(this.SendMessage(Button, Options, this.TB_ADDBUTTONS, 1))
return False
}
this.AutoSize()
return true
}
;=======================================================================================
; Method: AutoSize
; Description: Auto-sizes toolbar.
; Return: TRUE if successful, FALSE if there was a problem.
;=======================================================================================
AutoSize()
{
PostMessage, this.TB_AUTOSIZE, 0, 0,, % "ahk_id " this.tbHwnd
return ErrorLevel ? False : true
}
;=======================================================================================
; Method: Customize
; Description: Displays the Customize Toolbar dialog box.
; Return: TRUE if successful, FALSE if there was a problem.
;=======================================================================================
Customize()
{
SendMessage, this.TB_CUSTOMIZE, 0, 0,, % "ahk_id " this.tbHwnd
return (ErrorLevel = "FAIL") ? False : true
}
;=======================================================================================
; Method: Delete
; Description: Delete one or all buttons.
; Parameters:
; Button: 1-based index of the button. If omitted deletes all buttons.
; Return: TRUE if successful, FALSE if there was a problem.
;=======================================================================================
Delete(Button := "")
{
If (!Button)
{
Loop, % this.GetCount()
this.Delete(1)
}
Else
SendMessage, this.TB_DELETEBUTTON, Button-1, 0,, % "ahk_id " this.tbHwnd
return (ErrorLevel = "FAIL") ? False : true
}
;=======================================================================================
; Method: Export()
; Description: Returns a text string with current buttons and order in Add and
; Insert methods compatible format (this includes button's
; styles but not states). Duplicate labels are ignored.
; Parameters:
; ArrayOut: Set to TRUE to return an object array. The returned object
; format is compatible with Presets.Save and Presets.Load
; methods, which can be used to save and load layout presets.
; HidMark: Changes the default symbol to prepend to hidden buttons.
; Return: A text string with current buttons information to be exported.
;=======================================================================================
Export(ArrayOut := False, HidMark := "-")
{
BtnArray := [], IncLabels := ":"
Loop, % this.GetCount()
{
this.GetButton(A_Index, ID, Text, State, Style, Icon)
, Label := this.Labels[ID], IncLabels .= Label ":"
, cString := (Label ? (Label (Text ? "=" Text : "")
. ":" Icon (Style ? "(" Style ")" : "")) : "") ", "
, BtnString .= cString
If (ArrayOut)
BtnArray.Push({Icon: Icon-1, ID: ID, State: State
, Style: Style, Text: Text, Label: Label})
}
For i, Button in this.DefaultBtnInfo
{
If (!InStr(IncLabels, ":" (Label := this.Labels[Button.ID]) ":"))
{
If (!Label)
continue
oString := Label (Button.Text ? "=" Button.Text : "")
. ":" Button.Icon+1 (Button.Style ? "(" Button.Style ")" : "")
BtnString .= HidMark oString ", "
}
}
return ArrayOut ? BtnArray : RTrim(BtnString, ", ")
}
;=======================================================================================
; Method: Get
; Description: Retrieves information from the toolbar.
; Parameters:
; HotItem: OutputVar to store the 1-based index of current HotItem.
; TextRows: OutputVar to store the number of text rows
; Rows: OutputVar to store the number of rows for vertical toolbars.
; BtnWidth: OutputVar to store the buttons' width in pixels.
; BtnHeight: OutputVar to store the buttons' heigth in pixels.
; Style: OutputVar to store the current styles numeric value.
; ExStyle: OutputVar to store the current extended styles numeric value.
; Return: TRUE if successful, FALSE if there was a problem.
;=======================================================================================
Get(ByRef HotItem := "", ByRef TextRows := "", ByRef Rows := ""
, ByRef BtnWidth := "", ByRef BtnHeight := "", ByRef Style := "", ByRef ExStyle := "")
{
SendMessage, this.TB_GETHOTITEM, 0, 0,, % "ahk_id " this.tbHwnd
HotItem := (ErrorLevel = 4294967295) ? 0 : ErrorLevel+1
SendMessage, this.TB_GETTEXTROWS, 0, 0,, % "ahk_id " this.tbHwnd
TextRows := ErrorLevel
SendMessage, this.TB_GETROWS, 0, 0,, % "ahk_id " this.tbHwnd
Rows := ErrorLevel
SendMessage, this.TB_GETBUTTONSIZE, 0, 0,, % "ahk_id " this.tbHwnd
this.MakeShort(ErrorLevel, BtnWidth, BtnHeight)
SendMessage, this.TB_GETSTYLE, 0, 0,, % "ahk_id " this.tbHwnd
Style := ErrorLevel
SendMessage, this.TB_GETEXTENDEDSTYLE, 0, 0,, % "ahk_id " this.tbHwnd
ExStyle := ErrorLevel
return (ErrorLevel = "FAIL") ? False : true
}
;=======================================================================================
; Method: GetButton
; Description: Retrieves information from the toolbar buttons.
; Parameters:
; Button: 1-based index of the button.
; ID: OutputVar to store the button's command ID.
; Text: OutputVar to store the button's text caption.
; State: OutputVar to store the button's state numeric value.
; Style: OutputVar to store the button's style numeric value.
; Icon: OutputVar to store the button's icon index.
; Label: OutputVar to store the button's associated script label or function.
; Index: OutputVar to store the button's text string index.
; Return: TRUE if successful, FALSE if there was a problem.
;=======================================================================================
GetButton(Button, ByRef ID := "", ByRef Text := "", ByRef State := "", ByRef Style := ""
, ByRef Icon := "", ByRef Label := "", ByRef Index := "")
{
VarSetCapacity(BtnVar, 8 + (A_PtrSize * 3), 0)
SendMessage, this.TB_GETBUTTON, Button-1, &BtnVar,, % "ahk_id " this.tbHwnd
ID := NumGet(&BtnVar, 4, "Int"), Icon := NumGet(&BtnVar, 0, "Int")+1
, State := NumGet(&BtnVar, 8, "Char"), Style := NumGet(&BtnVar, 9, "Char")
, Index := NumGet(&BtnVar, 8 + (A_PtrSize * 2), "Int"), Label := this.Labels[ID]
SendMessage, this.TB_GETBUTTONTEXT, ID, 0,, % "ahk_id " this.tbHwnd
VarSetCapacity(Buffer, ErrorLevel * (A_IsUnicode ? 2 : 1), 0)
SendMessage, this.TB_GETBUTTONTEXT, ID, &Buffer,, % "ahk_id " this.tbHwnd
Text := StrGet(&Buffer)
return (ErrorLevel = "FAIL") ? False : true
; Alternative way to retrieve the button state.
; SendMessage, this.TB_GETSTATE, ID, 0,, % "ahk_id " this.tbHwnd
; State := ErrorLevel
}
;=======================================================================================
; Method: GetButtonPos
; Description: Retrieves position and size of a specific button, relative to
; the toolbar control.
; Parameters:
; Button: 1-based index of the button.
; OutX: OutputVar to store the button's horizontal position.
; OutY: OutputVar to store the button's vertical position.
; OutW: OutputVar to store the button's width.
; OutH: OutputVar to store the button's height.
; Return: TRUE if successful, FALSE if there was a problem.
;=======================================================================================
GetButtonPos(Button, ByRef OutX := "", ByRef OutY := "", ByRef OutW := "", ByRef OutH := "")
{
this.GetButton(Button, BtnID), VarSetCapacity(RECT, 16, 0)
SendMessage, this.TB_GETRECT, BtnID, &RECT,, % "ahk_id " this.tbHwnd
OutX := NumGet(&RECT, 0, "Int"), OutY := NumGet(&RECT, 4, "Int")
, OutW := NumGet(&RECT, 8, "Int") - OutX, OutH := NumGet(&RECT, 12, "Int") - OutY
return (ErrorLevel = "FAIL") ? False : true
}
;=======================================================================================
; Method: GetButtonState
; Description: Retrieves the state of a button based on a querry.
; Parameters:
; StateQuerry: Enter one of the following words to get the state of the button:
; Checked, Enabled, Hidden, Highlighted, Indeterminate, Pressed.
; Return: The TRUE if the StateQuerry is true, FALSE if it's not.
;=======================================================================================
GetButtonState(Button, StateQuerry)
{
this.GetButton(Button, BtnID)
If (this[ "TB_ISBUTTON" StateQuerry] )
Msg := this[ "TB_ISBUTTON" StateQuerry ]
SendMessage, Msg, BtnID, 0,, % "ahk_id " this.tbHwnd
return ErrorLevel ? true : False
}
;=======================================================================================
; Method: GetCount
; Description: Retrieves the total number of buttons.
; Return: The total number of buttons in the toolbar.
;=======================================================================================
GetCount()
{
SendMessage, this.TB_BUTTONCOUNT, 0, 0,, % "ahk_id " this.tbHwnd
return ErrorLevel
}
;=======================================================================================
; Method: GetHiddenButtons
; Description: Retrieves which buttons are hidden when the toolbar size is
; smaller then the total size of the buttons it has.
; This method is most useful when the toolbar is a child window of
; a Rebar control, in order to show a menu when the chevron is
; pushed. It does not retrieve buttons hidden by gui size.
; Return: An array with all buttons hidden by the Rebar band. Each key
; in the array has 4 properties: ID, Text, Label and Icon.
;=======================================================================================
GetHiddenButtons()
{
ControlGetPos,,, tbWidth,,, % "ahk_id " this.tbHwnd
VarSetCapacity(RECT, 16, 0), HiddenButtons := []
Loop, % this.GetCount()
{
SendMessage, this.TB_GETITEMRECT, A_Index-1, &RECT,, % "ahk_id " this.tbHwnd
If (NumGet(&RECT, 8, "Int") > tbWidth)
this.GetButton(A_Index, ID, Text, "", "", Icon)
, HiddenButtons.Push({ID: ID, Text: Text, Label: this.Labels[ID], Icon: Icon})
}
return HiddenButtons
}
;=======================================================================================
; Method: Insert
; Description: Insert button(s) in specified postion.
; To insert a separator call this method without parameters.
; Parameters:
; Position: 1-based index of button position to insert the new buttons.
; Options: Same as Add().
; Buttons: Same as Add().
; Return: TRUE if successful, FALSE if there was a problem.
;=======================================================================================
Insert(Position, Options := "Enabled", Buttons*)
{
If (!Buttons.Length())
{
this.BtnSep(TBBUTTON, Options)
SendMessage, this.TB_INSERTBUTTON, Position-1, &TBBUTTON,, % "ahk_id " this.tbHwnd
If (ErrorLevel = "FAIL")
return False
}
Else If (Options = "")
Options := "Enabled"
For i, Button in Buttons
{
If !(this.SendMessage(Button, Options, this.TB_INSERTBUTTON, (Position-1)+(i-1)))
return False
}
return true
}
;=======================================================================================
; Method: LabelToIndex
; Description: Converts a button label to its index in a toolbar.
; Parameters:
; Label: Button's associated label or function.
; Return: The 1-based index for the button or FALSE if Label is invalid.
;=======================================================================================
LabelToIndex(Label)
{
For ID, L in this.Labels
{
If (L = Label)
{
SendMessage, this.TB_COMMANDTOINDEX, ID, 0,, % "ahk_id " this.tbHwnd
return ErrorLevel+1
}
}
return False
}
;=======================================================================================
; Method: ModifyButton
; Description: Sets button states.
; Parameters:
; Button: 1-based index of the button.
; State: Enter one word from the follwing list to change a button's
; state: Check, Enable, Hide, Mark, Press.
; Set: Enter TRUE or FALSE to set the state on/off.
; Return: TRUE if successful, FALSE if there was a problem.
;=======================================================================================
ModifyButton(Button, State, Set := true)
{
If State not in CHECK,ENABLE,HIDE,MARK,PRESS
return False
Message := this[ "TB_" State "BUTTON"]
, this.GetButton(Button, BtnID)
SendMessage, Message, BtnID, Set,, % "ahk_id " this.tbHwnd
return (ErrorLevel = "FAIL") ? False : true
}
;=======================================================================================
; Method: ModifyButtonInfo
; Description: Sets button parameters such as Icon and CommandID.
; Parameters:
; Button: 1-based index of the button.
; Property: Enter one word from the following list to select the Property
; to be set: Command, Image, Size, State, Style, Text, Label.
; Value: The value to be set in the selected Property.
; If Property is State or Style you can enter named values as
; in the Add options.
; Return: TRUE if successful, FALSE if there was a problem.
;=======================================================================================
ModifyButtonInfo(Button, Property, Value)
{
If (Property = "Label")
{
this.GetButton(Button, ID), this.Labels[ID] := Value
return true
}
If (Property = "State") || (Property = "Style")
{
If Value is Integer
Value := Value
Else
{
Loop, Parse, Value, %A_Space%%A_Tab%
{
If (this[ "TBSTATE_" A_LoopField ])
tbState += this[ "TBSTATE_" A_LoopField ]
Else If (this[ "BTNS_" A_LoopField ] )
tbStyle += this[ "BTNS_" A_LoopField ]
}
Value := tb%Property%
}
}
If (Property = "Command")
this.GetButton(Button, "", "", "", "", "", Label), this.Labels[Value] := Label
this.DefineBtnInfoStruct(TBBUTTONINFO, Property, Value)
, this.GetButton(Button, BtnID)
SendMessage, this.TB_SETBUTTONINFO, BtnID, &TBBUTTONINFO,, % "ahk_id " this.tbHwnd
return (ErrorLevel = "FAIL") ? False : true
}
;=======================================================================================
; Method: MoveButton
; Description: Moves a toolbar button (change order).
; Parameters:
; Button: 1-based index of the button to be moved.
; Target: 1-based index of the new position.
; Return: TRUE if successful, FALSE if there was a problem.
;=======================================================================================
MoveButton(Button, Target)
{
SendMessage, this.TB_MOVEBUTTON, Button-1, Target-1,, % "ahk_id " this.tbHwnd
return (ErrorLevel = "FAIL") ? False : true
}
;=======================================================================================
; Method: OnMessage
; Description: Run label associated with button's Command identifier.
; This method should be called from a function monitoring the
; WM_COMMAND message. Pass the wParam as the CommandID.
; Parameters:
; CommandID: Command ID associated with the button. This is send via
; WM_COMMAND message, you must pass the wParam from
; inside a function that monitors this message.
; FuncParams: In case the button is associated with a valid function,
; you may pass optional parameters for the function call.
; You can pass any number of parameters.
; Return: TRUE if target label or function exists, or FALSE otherwise.
;=======================================================================================
OnMessage(CommandID, FuncParams*)
{
If (IsLabel(this.Labels[CommandID]))
{
GoSub, % this.Labels[CommandID]
return true
}
Else If (IsFunc(this.Labels[CommandID]))
{
BtnFunc := this.Labels[CommandID]
, %BtnFunc%(FuncParams*)
return true
}
Else
return False
}
;=======================================================================================
; Method: OnNotify
; Description: Handles toolbar notifications.
; This method should be called from a function monitoring the
; WM_NOTIFY message. Pass the lParam as the Param.
; The returned value should be used as return value for
; the monitoring function as well.
; Parameters:
; Param: The lParam from WM_NOTIFY message.
; MenuXPos: OutputVar to store the horizontal position for a menu.
; MenuYPos: OutputVar to store the vertical position for a menu.
; BtnLabel: OutputVar to store the label or function name associated with the button.
; ID: OutputVar to store the button's Command ID.
; AllowCustom: Set to FALSE to prevent customization of toolbars.
; AllowReset: Set to FALSE to prevent Reset button from restoring original buttons.
; HideHelp: Set to FALSE to show the Help button in the customize dialog.
; Return: The required return value for the function monitoring
; the the WM_NOTIFY message.
;=======================================================================================
OnNotify(ByRef Param, ByRef MenuXPos := "", ByRef MenuYPos := "", ByRef BtnLabel := "", ByRef ID := ""
, AllowCustom := true, AllowReset := true, HideHelp := true)
{
nCode := NumGet(Param + (A_PtrSize * 2), 0, "Int"), tbHwnd := NumGet(Param + 0, 0, "UPtr")
If (tbHwnd != this.tbHwnd)
return ""
If (nCode = this.TBN_DROPDOWN)
{
ID := NumGet(Param + (A_PtrSize * 3), 0, "Int"), BtnLabel := this.Labels[ID]
, VarSetCapacity(RECT, 16, 0)
SendMessage, this.TB_GETRECT, ID, &RECT,, % "ahk_id " this.tbHwnd
ControlGetPos, TBX, TBY,,,, % "ahk_id " this.tbHwnd
MenuXPos := TBX + NumGet(&RECT, 0, "Int"), MenuYPos := TBY + NumGet(&RECT, 12, "Int")
return False
}
Else
BtnLabel := "", ID := ""
If (nCode = this.TBN_QUERYINSERT)
return AllowCustom
If (nCode = this.TBN_QUERYDELETE)
return AllowCustom
If (nCode = this.TBN_GETBUTTONINFO)
{
iItem := NumGet(Param + (A_PtrSize * 3), 0, "Int")
If (iItem = this.DefaultBtnInfo.Length())
return False
For each, Member in this.DefaultBtnInfo[iItem+1]
%each% := Member
If (Text != "")
{
VarSetCapacity(BTNSTR, (StrPut(Text) * (A_IsUnicode ? 2 : 1), 0))
, StrPut(Text, &BTNSTR, StrPut(Text) * 2)
SendMessage, this.TB_ADDSTRING, 0, &BTNSTR,, % "ahk_id " this.tbHwnd
Index := ErrorLevel, this.DefaultBtnInfo[iItem+1]["Index"] := Index
, this.DefaultBtnInfo[iItem+1]["Text"] := Text
}
NumPut(Icon, Param + (A_PtrSize * 4), 0, "Int") ; iBitmap
, NumPut(ID, Param + (A_PtrSize * 4), 4, "Int") ; idCommand
, NumPut(State, Param + (A_PtrSize * 4), 8, "Char") ; tbState
, NumPut(Style, Param + (A_PtrSize * 4), 9, "Char") ; tbStyle
, NumPut(Index, Param + (A_PtrSize * 4), 8 + (A_PtrSize * 2), "Int") ; iString
return true
}
If (nCode = this.TBN_TOOLBARCHANGE)
{
CurrentButtons := this.Export(true)
, this.Presets.Load(CurrentButtons)
, CurrentButtons := ""
}
If (nCode = this.TBN_RESET)
{
If (AllowReset)
{
this.Reset()
return 2
}
}
If (nCode = this.TBN_INITCUSTOMIZE)
return HideHelp
return ""
}
;=======================================================================================
; Method: Reset
; Description: Restores all toolbar's buttons to default layout.
; Default layout is set by the buttons added. This can be changed
; calling the SetDefault method.
; Return: TRUE if successful, FALSE if there was a problem.
;=======================================================================================
Reset()
{
BtnsArray := IsObject(CustomArray) ? CustomArray : this.DefaultBtnInfo
, this.Get("", "", Rows)
Loop, % this.GetCount()
this.Delete(1)
For each, Button in BtnsArray
{
For each, Member in Button
%each% := Member
If ((Rows > 1) && (Style = this.BTNS_SEP))
State := 0x24
If (Text != "")
{
VarSetCapacity(BTNSTR, (StrPut(Text) * (A_IsUnicode ? 2 : 1), 0))
, StrPut(Text, &BTNSTR, StrPut(Text) * 2)
SendMessage, this.TB_ADDSTRING, 0, &BTNSTR,, % "ahk_id " this.tbHwnd
Index := ErrorLevel
}
VarSetCapacity(TBBUTTON, 8 + (A_PtrSize * 3), 0)
, NumPut(Icon, TBBUTTON, 0, "Int")
, NumPut(ID, TBBUTTON, 4, "Int")
, NumPut(State, TBBUTTON, 8, "Char")
, NumPut(Style, TBBUTTON, 9, "Char")
, NumPut(Index, TBBUTTON, 8 + (A_PtrSize * 2), "Int")
SendMessage, this.TB_ADDBUTTONS, 1, &TBBUTTON,, % "ahk_id " this.tbHwnd
}
return (ErrorLevel = "FAIL") ? False : true
}
;=======================================================================================
; Method: SetButtonSize
; Description: Sets the size of buttons on a toolbar. Affects current buttons.
; Parameters:
; W: Width of buttons, in pixels
; H: Height of buttons, in pixels
; Return: TRUE if successful, FALSE if there was a problem.
;=======================================================================================
SetButtonSize(W, H)
{
Long := this.MakeLong(W, H)
SendMessage, this.TB_SETBUTTONSIZE, 0, Long,, % "ahk_id " this.tbHwnd
return (ErrorLevel = "FAIL") ? False : true
}
;=======================================================================================
; Method: SetDefault
; Description: Sets the internal default layout to be used when customizing or
; when the Reset method is called.
; Parameters:
; Options: Same as Add().
; Buttons: Same as Add().
; Return: Always TRUE.
;=======================================================================================
SetDefault(Options := "Enabled", Buttons*)
{
this.DefaultBtnInfo := []
If (!Buttons.Length())
this.DefaultBtnInfo.Push({Icon: -1, ID: "", State: ""
, Style: this.BTNS_SEP, Text: "", Label: ""})
If (Options = "")
Options := "Enabled"
For each, Button in Buttons
this.SendMessage(Button, Options)
return true
}
;=======================================================================================
; Method: SetExStyle
; Description: Sets toolbar's extended style.
; Parameters:
; Style: Enter one or more words, separated by space or tab, from the
; following list to set toolbar's extended styles:
; Doublebuffer, DrawDDArrows, HideClippedButtons,
; MixedButtons.
; You may also enter an integer value to define the style.
; Return: TRUE if successful, FALSE if there was a problem.
;=======================================================================================
SetExStyle(Style)
{
If Style is Integer
tbStyle_Ex_ := Style
Else
{
Loop, Parse, Style, %A_Space%%A_Tab%
{
If (this[ "TBSTYLE_EX_" A_LoopField ] )
tbStyle_Ex_ += this[ "TBSTYLE_EX_" A_LoopField ]
}
}
SendMessage, this.TB_SETEXTENDEDSTYLE, 0, tbStyle_Ex_,, % "ahk_id " this.tbHwnd
}
;=======================================================================================
; Method: SetHotItem
; Description: Sets the hot item on a toolbar.
; Parameters:
; Button: 1-based index of the button.
; Return: TRUE if successful, FALSE if there was a problem.
;=======================================================================================
SetHotItem(Button)
{
SendMessage, this.TB_SETHOTITEM, Button-1, 0,, % "ahk_id " this.tbHwnd
return (ErrorLevel = "FAIL") ? False : true
}
;=======================================================================================
; Method: SetImageList
; Description: Sets an ImageList to the toolbar control.
; Parameters:
; IL_Default: ImageList ID of default ImageList.
; IL_Hot: ImageList ID of Hot ImageList.
; IL_Pressed: ImageList ID of Pressed ImageList.
; IL_Disabled: ImageList ID of Disabled ImageList.
; Return: TRUE if successful, FALSE if there was a problem.
;=======================================================================================
SetImageList(IL_Default, IL_Hot := "", IL_Pressed := "", IL_Disabled := "")
{
SendMessage, this.TB_SETIMAGELIST, 0, IL_Default,, % "ahk_id " this.tbHwnd
If (IL_Hot)
SendMessage, this.TB_SETHOTIMAGELIST, 0, IL_Hot,, % "ahk_id " this.tbHwnd
If (IL_Pressed)
SendMessage, this.TB_SETPRESSEDIMAGELIST, 0, IL_Pressed,, % "ahk_id " this.tbHwnd
If (IL_Disabled)
SendMessage, this.TB_SETDISABLEDIMAGELIST, 0, IL_Disabled,, % "ahk_id " this.tbHwnd
return (ErrorLevel = "FAIL") ? False : true
}
;=======================================================================================
; Method: SetIndent
; Description: Sets the indentation for the first button on a toolbar.
; Parameters:
; Value: Value specifying the indentation, in pixels.
; Return: TRUE if successful, FALSE if there was a problem.
;=======================================================================================
SetIndent(Value)
{
SendMessage, this.TB_SETINDENT, Value, 0,, % "ahk_id " this.tbHwnd
return (ErrorLevel = "FAIL") ? False : true
}
;=======================================================================================
; Method: SetListGap
; Description: Sets the distance between icons and text on a toolbar.
; Parameters:
; Value: The gap, in pixels, between buttons on the toolbar.
; Return: TRUE if successful, FALSE if there was a problem.
;=======================================================================================
SetListGap(Value)
{
SendMessage, this.TB_SETLISTGAP, Value, 0,, % "ahk_id " this.tbHwnd
return (ErrorLevel = "FAIL") ? False : true
}
;=======================================================================================
; Method: SetMaxTextRows
; Description: Sets maximum number of text rows for button captions.
; Parameters:
; MaxRows: Maximum number of text rows. If omitted defaults to 0.
; Return: TRUE if successful, FALSE if there was a problem.
;=======================================================================================
SetMaxTextRows(MaxRows := 0)
{
SendMessage, this.TB_SETMAXTEXTROWS, MaxRows, 0,, % "ahk_id " this.tbHwnd
return (ErrorLevel = "FAIL") ? False : true
}
;=======================================================================================
; Method: SetPadding
; Description: Sets the padding for icons a toolbar.
; Parameters:
; X: Horizontal padding, in pixels
; Y: Vertical padding, in pixels
; Return: TRUE if successful, FALSE if there was a problem.
;=======================================================================================
SetPadding(X, Y)
{
Long := this.MakeLong(X, Y)
SendMessage, this.TB_SETPADDING, 0, Long,, % "ahk_id " this.tbHwnd
return (ErrorLevel = "FAIL") ? False : true
}
;=======================================================================================
; Method: SetRows
; Description: Sets the number of rows for a toolbar.
; Parameters:
; Rows: Number of rows to set. If omitted defaults to 0.
; AddMore: Indicates whether to create more rows than requested when the
; system cannot create the number of rows specified.
; If TRUE, the system creates more rows. If FALSE, the system
; creates fewer rows.
; Return: TRUE if successful, FALSE if there was a problem.
;=======================================================================================
SetRows(Rows := 0, AddMore := False)
{
Long := this.MakeLong(Rows, AddMore)
SendMessage, this.TB_SETROWS, Long,,, % "ahk_id " this.tbHwnd
return (ErrorLevel = "FAIL") ? False : true
}
;=======================================================================================
; Method: ToggleStyle
; Description: Toggles toolbar's style.
; Parameters:
; Style: Enter zero or more words, separated by space or tab, from the
; following list to toggle toolbar's styles:
; AltDrag, CustomErase, Flat, List, RegisterDrop, Tooltips,
; Transparent, Wrapable, Adjustable, Border, ThickFrame,
; TabStop.
; You may also enter an integer value to define the style.
; Return: TRUE if a valid style is passed, or FALSE otherwise.
;=======================================================================================
ToggleStyle(Style)
{
If Style is Integer
tbStyle := Style
Else
{
Loop, Parse, Style, %A_Space%%A_Tab%
{
If (this[ "TBSTYLE_" A_LoopField ] )
tbStyle += this[ "TBSTYLE_" A_LoopField ]
}
}
; TB_SETSTYLE moves the toolbar away from its position for some reason.
; SendMessage, this.TB_SETSTYLE, 0, tbStyle,, % "ahk_id " this.tbHwnd
If (tbStyle != "")
{
WinSet, Style, ^%tbStyle%, % "ahk_id " this.tbHwnd
return true
}
Else
return False
}
;=======================================================================================
; Presets Methods These methods are used exclusively by the Presets Object.
; Presets can be used to quickly change buttons of a toolbar
; to predetermined or saved layouts.
;=======================================================================================
Class tbPresets extends Toolbar.Private
{
;=======================================================================================
; Method: Presets.Delete
; Description: Deletes the layout of the specified slot.
; Parameters:
; Slot: Number of the slot containing the layout to be deleted.
; Return: TRUE if the slot contains a layout, or FALSE otherwise.
;=======================================================================================
Delete(Slot)
{
If (IsObject(this[Slot]))
{
this.Delete(Slot)
return true
}
Else
return False
}
;=======================================================================================
; Method: Presets.Export
; Description: Returns a text string with buttons and order in Add and
; Insert methods compatible format from the specified slot.
; Parameters:
; Slot: Number of the slot in which to save the layout.
; ArrayOut: Set to TRUE to return an object array. The returned object
; format is compatible with Presets.Save and Presets.Load
; methods, which can be used to save and load layout presets.
; Return: A text string with buttons information to be exported.
;=======================================================================================
Export(Slot, ArrayOut := False)
{
BtnArray := []
For i, Button in this[Slot]
{
For each, Member in Button
%each% := Member
BtnString .= (Label ? (Label (Text ? "=" Text : "")
. ":" Icon+1 (Style ? "(" Style ")" : "")) : "") ", "
If (ArrayOut)
BtnArray.Push({Icon: Icon, ID: ID, State: State
, Style: Style, Text: Text, Label: Label})
}
return ArrayOut ? BtnArray : RTrim(BtnString, ", ")
}
;=======================================================================================
; Method: Presets.Import
; Description: Imports a buttons layout from a string in Add/Insert methods
; format and saves it into the specified slot.
; Parameters:
; Slot: Number of the slot in which to save the layout.
; Options: Same as Add().
; Buttons: Same as Add().
; Return: Always TRUE.
;=======================================================================================
Import(Slot, Options := "Enabled", Buttons*)
{
BtnArray := []
If (Options = "")
Options := "Enabled"
For each, Button in Buttons
{
If (RegExMatch(Button, "^(\W?)(\w+)[=\s]?(.*)?:(\d+)\(?(.*?)?\)?$", Key))
{
If (Key1)
continue
idCommand := this.StringToNumber(Key2)
, iString := Key3, iBitmap := Key4
, Struct := this.DefineBtnStruct(TBBUTTON, iBitmap, idCommand, iString, Key5 ? Key5 : Options)
, Struct.Label := Key2, BtnArray.Push(Struct)
}
Else
Struct := this.BtnSep(TBBUTTON, Options), BtnArray.Push(Struct)
}
this[Slot] := BtnArray
return true
}
;=======================================================================================
; Method: Presets.Load
; Description: Loads a layout from the specified slot.
; Parameters:
; Slot: Number of the slot containing the layout to be loaded.
; For convenience and internal operations this parameter can be an
; object in the same format of Presets.Save Buttons parameter.
; Return: TRUE if the slot contains a layout, or FALSE otherwise.
;=======================================================================================
Load(Slot)
{
If (IsObject(Slot))
Buttons := Slot
Else
Buttons := this[Slot]
If (!IsObject(Buttons))
return False
SendMessage, this.TB_GETROWS, 0, 0,, % "ahk_id " this.tbHwnd
Rows := ErrorLevel
SendMessage, this.TB_BUTTONCOUNT, 0, 0,, % "ahk_id " this.tbHwnd
Loop, % ErrorLevel
SendMessage, this.TB_DELETEBUTTON, 0, 0,, % "ahk_id " this.tbHwnd
For each, Button in Buttons
{
For each, Member in Button
%each% := Member
If ((Rows > 1) && (Style = this.BTNS_SEP))
State := 0x24
If (Text != "")
{
VarSetCapacity(BTNSTR, (StrPut(Text) * (A_IsUnicode ? 2 : 1), 0))
, StrPut(Text, &BTNSTR, StrPut(Text) * 2)
SendMessage, this.TB_ADDSTRING, 0, &BTNSTR,, % "ahk_id " this.tbHwnd
Index := ErrorLevel
}
VarSetCapacity(TBBUTTON, 8 + (A_PtrSize * 3), 0)
, NumPut(Icon, TBBUTTON, 0, "Int")
, NumPut(ID, TBBUTTON, 4, "Int")
, NumPut(State, TBBUTTON, 8, "Char")
, NumPut(Style, TBBUTTON, 9, "Char")
, NumPut(Index, TBBUTTON, 8 + (A_PtrSize * 2), "Int")
SendMessage, this.TB_ADDBUTTONS, 1, &TBBUTTON,, % "ahk_id " this.tbHwnd
}
return (ErrorLevel = "FAIL") ? False : true
}
;=======================================================================================
; Method: Presets.Save
; Description: Saves a buttons layout as a preset into the specified slot.
; Parameters:
; Slot: Number of the slot in which to save the layout. There is no
; predefined limit of slots.
; Buttons: Buttons array must be in a valid format. You can use the Export
; Toolbar Method (not the Preset Method of the same name)
; passing TRUE to the ArrayOut parameter to return a valid
; array to be used with this method.
; Return: TRUE if Buttons is an object, or FALSE otherwise.
;=======================================================================================
Save(Slot, Buttons)
{
If (IsObject(Buttons))
{
this[Slot] := Buttons
return true
}
Else
return False
}
}
;=======================================================================================
; Private Class This class is used internally.
;=======================================================================================
Class Private
{
;=======================================================================================
; Private Properties
;=======================================================================================
; Messages
Static TB_ADDBUTTONS := 0x0414
Static TB_ADDSTRING := A_IsUnicode ? 0x044D : 0x041C
Static TB_AUTOSIZE := 0x0421
Static TB_BUTTONCOUNT := 0x0418
Static TB_CHECKBUTTON := 0x0402
Static TB_COMMANDTOINDEX := 0x0419
Static TB_CUSTOMIZE := 0x041B
Static TB_DELETEBUTTON := 0x0416
Static TB_ENABLEBUTTON := 0x0401
Static TB_GETBUTTON := 0x0417
Static TB_GETBUTTONSIZE := 0x043A
Static TB_GETBUTTONTEXT := A_IsUnicode ? 0x044B : 0x042D
Static TB_GETEXTENDEDSTYLE := 0x0455
Static TB_GETHOTITEM := 0x0447
Static TB_GETIMAGELIST := 0x0431
Static TB_GETIMAGELISTCOUNT := 0x0462
Static TB_GETITEMDROPDOWNRECT := 0x0467
Static TB_GETITEMRECT := 0x041D
Static TB_GETMAXSIZE := 0x0453
Static TB_GETPADDING := 0x0456
Static TB_GETRECT := 0x0433
Static TB_GETROWS := 0x0428
Static TB_GETSTATE := 0x0412
Static TB_GETSTYLE := 0x0439
Static TB_GETSTRING := A_IsUnicode ? :0x045B 0x045C
Static TB_GETTEXTROWS := 0x043D
Static TB_HIDEBUTTON := 0x0404
Static TB_INDETERMINATE := 0x0405
Static TB_INSERTBUTTON := A_IsUnicode ? 0x0443 : 0x0415
Static TB_ISBUTTONCHECKED := 0x040A
Static TB_ISBUTTONENABLED := 0x0409
Static TB_ISBUTTONHIDDEN := 0x040C
Static TB_ISBUTTONHIGHLIGHTED := 0x040E
Static TB_ISBUTTONINDETERMINATE := 0x040D
Static TB_ISBUTTONPRESSED := 0x040B
Static TB_MARKBUTTON := 0x0406
Static TB_MOVEBUTTON := 0x0452
Static TB_PRESSBUTTON := 0x0403
Static TB_SETBUTTONINFO := A_IsUnicode ? 0x0440 : 0x0442