-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript_gui.cpp
9413 lines (8796 loc) · 486 KB
/
script_gui.cpp
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
/*
AutoHotkey
Copyright 2003-2008 Chris Mallett ([email protected])
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include "stdafx.h" // pre-compiled headers
#include "script.h"
#include "globaldata.h" // for a lot of things
#include "application.h" // for MsgSleep()
#include "window.h" // for SetForegroundWindowEx()
#include "qmath.h" // for qmathLog()
ResultType Script::PerformGui(char *aCommand, char *aParam2, char *aParam3, char *aParam4)
{
/*
int window_index = g.GuiDefaultWindowIndex; // Which window to operate upon. Initialized to thread's default.
char *options; // This will contain something that is meaningful only when gui_command == GUI_CMD_OPTIONS.
GuiCommands gui_command = Line::ConvertGuiCommand(aCommand, &window_index, &options);
if (gui_command == GUI_CMD_INVALID)
return ScriptError(ERR_PARAM1_INVALID ERR_ABORT, aCommand);
if (window_index < 0 || window_index >= MAX_GUI_WINDOWS)
return ScriptError("Max window number is " MAX_GUI_WINDOWS_STR "." ERR_ABORT, aCommand);
// First completely handle any sub-command that doesn't require the window to exist.
// In other words, don't auto-create the window before doing this command like we do
// for the others:
switch(gui_command)
{
case GUI_CMD_DESTROY:
return GuiType::Destroy(window_index);
case GUI_CMD_DEFAULT:
// Change the "default" member, not g.GuiWindowIndex because that contains the original
// window number reponsible for launching this thread, which should not be changed because it is
// used to produce the contents of A_Gui. Also, it's okay if the specify window index doesn't
// currently exist.
g.GuiDefaultWindowIndex = window_index;
return OK;
}
// If the window doesn't currently exist, don't auto-create it for those commands for
// which it wouldn't make sense. Note that things like FONT and COLOR are allowed to
// auto-create the window, since those commands can be legitimately used prior to the
// first "Gui Add" command. Also, it seems best to allow SHOW even though all it will
// do is create and display an empty window.
if (!g_gui[window_index])
{
switch(gui_command)
{
case GUI_CMD_SUBMIT:
case GUI_CMD_CANCEL:
case GUI_CMD_FLASH:
case GUI_CMD_MINIMIZE:
case GUI_CMD_MAXIMIZE:
case GUI_CMD_RESTORE:
return OK; // Nothing needs to be done since the window object doesn't exist.
// v1.0.43.09:
// Don't overload "+LastFound" because it would break existing scripts that rely on the window
// being created by +LastFound.
case GUI_CMD_OPTIONS:
if (!stricmp(options, "+LastFoundExist"))
{
g.hWndLastUsed = NULL;
return OK;
}
break;
}
// Otherwise: Create the object and (later) its window, since all the other sub-commands below need it:
if ( !(g_gui[window_index] = new GuiType(window_index)) )
return FAIL; // No error displayed since extremely rare.
if ( !(g_gui[window_index]->mControl = (GuiControlType *)malloc(GUI_CONTROL_BLOCK_SIZE * sizeof(GuiControlType))) )
{
delete g_gui[window_index];
g_gui[window_index] = NULL;
return FAIL; // No error displayed since extremely rare.
}
g_gui[window_index]->mControlCapacity = GUI_CONTROL_BLOCK_SIZE;
// Probably better to increment here rather than in constructor in case GuiType objects
// are ever created outside of the g_gui array (such as for temp local variables):
++GuiType::sGuiCount; // This count is maintained to help performance in the main event loop and other places.
}
GuiType &gui = *g_gui[window_index]; // For performance and convenience.
// Now handle any commands that should be handled prior to creation of the window in the case
// where the window doesn't already exist:
bool set_last_found_window = false;
ToggleValueType own_dialogs = TOGGLE_INVALID;
if (gui_command == GUI_CMD_OPTIONS)
if (!gui.ParseOptions(options, set_last_found_window, own_dialogs))
return FAIL; // It already displayed the error.
// Create the window if needed. Since it should not be possible for our window to get destroyed
// without our knowning about it (via the explicit handling in its window proc), it shouldn't
// be necessary to check the result of IsWindow(gui.mHwnd):
if (!gui.mHwnd && !gui.Create())
{
GuiType::Destroy(window_index); // Get rid of the object so that it stays in sync with the window's existence.
return ScriptError("Could not create window." ERR_ABORT);
}
// After creating the window, return from any commands that were fully handled above:
if (gui_command == GUI_CMD_OPTIONS)
{
if (set_last_found_window)
g.hWndLastUsed = gui.mHwnd;
// Fix for v1.0.35.05: Must do the following only if gui_command==GUI_CMD_OPTIONS, otherwise
// the own_dialogs setting will get reset during other commands such as "Gui Show", "Gui Add"
if (own_dialogs != TOGGLE_INVALID) // v1.0.35.06: Plus or minus "OwnDialogs" was present rather than being entirely absent.
g.DialogOwnerIndex = (own_dialogs == TOGGLED_ON) ? window_index : MAX_GUI_WINDOWS; // Reset to out-of-bounds when "-OwnDialogs" is present.
return OK;
}
GuiControls gui_control_type = GUI_CONTROL_INVALID;
int index;
switch (gui_command)
{
case GUI_CMD_ADD:
if ( !(gui_control_type = Line::ConvertGuiControl(aParam2)) )
return ScriptError(ERR_PARAM2_INVALID ERR_ABORT, aParam2);
return gui.AddControl(gui_control_type, aParam3, aParam4); // It already displayed any error.
case GUI_CMD_MARGIN:
if (*aParam2)
gui.mMarginX = ATOI(aParam2); // Seems okay to allow negative margins.
if (*aParam3)
gui.mMarginY = ATOI(aParam3); // Seems okay to allow negative margins.
return OK;
case GUI_CMD_MENU:
UserMenu *menu;
if (*aParam2)
{
// By design, the below will give a slightly misleading error if the specified menu is the
// TRAY menu, since it should be obvious that it cannot be used as a menu bar (since it
// must always be of the popup type):
if ( !(menu = FindMenu(aParam2)) || menu == g_script.mTrayMenu ) // Relies on short-circuit boolean.
return ScriptError(ERR_MENU ERR_ABORT, aParam2);
menu->Create(MENU_TYPE_BAR); // Ensure the menu physically exists and is the "non-popup" type (for a menu bar).
}
else
menu = NULL;
SetMenu(gui.mHwnd, menu ? menu->mMenu : NULL); // Add or remove the menu.
return OK;
case GUI_CMD_SHOW:
return gui.Show(aParam2, aParam3);
case GUI_CMD_SUBMIT:
return gui.Submit(stricmp(aParam2, "NoHide"));
case GUI_CMD_CANCEL:
return gui.Cancel();
case GUI_CMD_MINIMIZE:
// If the window is hidden, it is unhidden as a side-effect (this happens even for SW_SHOWMINNOACTIVE).
ShowWindow(gui.mHwnd, SW_MINIMIZE);
return OK;
case GUI_CMD_MAXIMIZE:
ShowWindow(gui.mHwnd, SW_MAXIMIZE); // If the window is hidden, it is unhidden as a side-effect.
return OK;
case GUI_CMD_RESTORE:
ShowWindow(gui.mHwnd, SW_RESTORE); // If the window is hidden, it is unhidden as a side-effect.
return OK;
case GUI_CMD_FONT:
return gui.SetCurrentFont(aParam2, aParam3);
case GUI_CMD_LISTVIEW:
case GUI_CMD_TREEVIEW:
if (*aParam2)
{
GuiIndexType control_index = gui.FindControl(aParam2); // Search on either the control's variable name or its ClassNN.
if (control_index != -1) // Must compare directly to -1 due to unsigned.
{
GuiControlType &control = gui.mControl[control_index]; // For maintainability, and might slightly reduce code size.
if (gui_command == GUI_CMD_LISTVIEW)
{
if (control.type == GUI_CONTROL_LISTVIEW) // v1.0.46.09: Must validate that it's the right type of control; otherwise some LV_* functions can crash due to the control not having malloc'd the special ListView struct that tracks column attributes.
gui.mCurrentListView = &control;
//else mismatched control type, so just leave it unchanged.
}
else // GUI_CMD_TREEVIEW
{
if (control.type == GUI_CONTROL_TREEVIEW)
gui.mCurrentTreeView = &control;
//else mismatched control type, so just leave it unchanged.
}
}
//else it seems best never to change ite to be "no control" since it doesn't seem to have much use.
}
return OK;
case GUI_CMD_TAB:
{
TabIndexType prev_tab_index = gui.mCurrentTabIndex;
TabControlIndexType prev_tab_control_index = gui.mCurrentTabControlIndex;
if (!*aParam2 && !*aParam3) // Both the tab control number and the tab number were omitted.
gui.mCurrentTabControlIndex = MAX_TAB_CONTROLS; // i.e. "no tab"
else
{
if (*aParam3) // Which tab control. Must be processed prior to Param2 since it might change mCurrentTabControlIndex.
{
index = ATOI(aParam3) - 1;
if (index < 0 || index > MAX_TAB_CONTROLS - 1)
return ScriptError(ERR_PARAM3_INVALID ERR_ABORT, aParam3);
if (index != gui.mCurrentTabControlIndex) // This is checked early in case of early return in the next section due to error.
{
gui.mCurrentTabControlIndex = index;
// Fix for v1.0.38.02: Changing to a different tab control (or none at all when there
// was one before, or vice versa) should start a new radio group:
gui.mInRadioGroup = false;
}
}
if (*aParam2) // Index or name of a particular tab inside a control.
{
if (!*aParam3 && gui.mCurrentTabControlIndex == MAX_TAB_CONTROLS)
// Provide a default: the most recently added tab control. If there are no
// tab controls, assume the index is the first tab control (i.e. a tab control
// to be created in the future). Fix for v1.0.46.16: This section must be done
// prior to gui.FindTabControl() below because otherwise, a script that does
// "Gui Tab" will find that a later use of "Gui Tab, TabName" won't work unless
// the third parameter (which tab control) is explicitly specified.
gui.mCurrentTabControlIndex = gui.mTabControlCount ? gui.mTabControlCount - 1 : 0;
bool exact_match = !stricmp(aParam4, "Exact"); // v1.0.37.03.
// Unlike "GuiControl, Choose", in this case, don't allow negatives since that would just
// generate an error msg further below:
if (!exact_match && IsPureNumeric(aParam2, false, false))
{
index = ATOI(aParam2) - 1;
if (index < 0 || index > MAX_TABS_PER_CONTROL - 1)
return ScriptError(ERR_PARAM2_INVALID ERR_ABORT, aParam2);
}
else
{
index = -1; // Set default to be "failure".
GuiControlType *tab_control = gui.FindTabControl(gui.mCurrentTabControlIndex);
if (tab_control)
index = gui.FindTabIndexByName(*tab_control, aParam2, exact_match); // Returns -1 on failure.
if (index == -1)
return ScriptError("Tab name doesn't exist yet." ERR_ABORT, aParam2);
}
gui.mCurrentTabIndex = index;
}
if (gui.mCurrentTabIndex != prev_tab_index || gui.mCurrentTabControlIndex != prev_tab_control_index)
gui.mInRadioGroup = false; // A fix for v1.0.38.02, see comments at similar line above.
}
return OK;
}
case GUI_CMD_COLOR:
// AssignColor() takes care of deleting old brush, etc.
// In this case, a blank for either param means "leaving existing color alone", in which
// case AssignColor() is not called since it would assume CLR_NONE then.
if (*aParam2)
AssignColor(aParam2, gui.mBackgroundColorWin, gui.mBackgroundBrushWin);
if (*aParam3)
{
AssignColor(aParam3, gui.mBackgroundColorCtl, gui.mBackgroundBrushCtl);
// As documented, the following is not done. Primary reasons:
// 1) Allows any custom color that was explicitly specified via "Gui, Add, ListView, BackgroundGreen"
// to stay in effect rather than being overridden by this change. You could argue that this
// could be detected by asking the control its background color and if it matches the previous
// mBackgroundColorCtl (which might be CLR_DEFAULT?), it's 99% likely it was not an
// individual/explicit custom color and thus should be changed here. But that would be even
// more complexity so it seems better to keep it simple.
// 2) Reduce code size.
//for (GuiIndexType u = 0; u < gui.mControlCount; ++u)
// if (gui.mControl[u].type == GUI_CONTROL_LISTVIEW && ListView_GetTextBkColor(..) != prev_bk_color_ctl)
// {
// ListView_SetTextBkColor(gui.mControl[u].hwnd, gui.mBackgroundColorCtl);
// ListView_SetBkColor(gui.mControl[u].hwnd, gui.mBackgroundColorCtl);
// }
// ... and probably similar for TREEVIEW.
}
if (IsWindowVisible(gui.mHwnd))
// Force the window to repaint so that colors take effect immediately.
// UpdateWindow() isn't enough sometimes/always, so do something more aggressive:
InvalidateRect(gui.mHwnd, NULL, TRUE);
return OK;
case GUI_CMD_FLASH:
// Note that FlashWindowEx() would have to be loaded dynamically since it is not available
// on Win9x/NT. But for now, just this simple method is provided. In the future, more
// sophisticated parameters can be made available to flash the window a given number of times
// and at a certain frequency, and other options such as only-taskbar-button or only-caption.
// Set FlashWindowEx() for more ideas:
FlashWindow(gui.mHwnd, stricmp(aParam2, "Off") ? TRUE : FALSE);
return OK;
} // switch()
return FAIL; // Should never be reached, but avoids compiler warning and improves bug detection.
*/
}
ResultType Line::GuiControl(char *aCommand, char *aControlID, char *aParam3)
{
/*
char *options; // This will contain something that is meaningful only when gui_command == GUICONTROL_CMD_OPTIONS.
int window_index = g.GuiDefaultWindowIndex; // Which window to operate upon. Initialized to thread's default.
GuiControlCmds guicontrol_cmd = Line::ConvertGuiControlCmd(aCommand, &window_index, &options);
if (guicontrol_cmd == GUICONTROL_CMD_INVALID)
// This is caught at load-time 99% of the time and can only occur here if the sub-command name
// is contained in a variable reference. Since it's so rare, the handling of it is debatable,
// but to keep it simple just set ErrorLevel:
return g_ErrorLevel->Assign(ERRORLEVEL_ERROR);
if (window_index < 0 || window_index >= MAX_GUI_WINDOWS || !g_gui[window_index]) // Relies on short-circuit boolean order.
// This departs from the tradition used by PerformGui() but since this type of error is rare,
// and since use ErrorLevel adds a little bit of flexibility (since the script's curretn thread
// is not unconditionally aborted), this seems best:
return g_ErrorLevel->Assign(ERRORLEVEL_ERROR);
GuiType &gui = *g_gui[window_index]; // For performance and convenience.
GuiIndexType control_index = gui.FindControl(aControlID);
if (control_index >= gui.mControlCount) // Not found.
return g_ErrorLevel->Assign(ERRORLEVEL_ERROR);
GuiControlType &control = gui.mControl[control_index]; // For performance and convenience.
// Beyond this point, errors are rare so set the default to "no error":
g_ErrorLevel->Assign(ERRORLEVEL_NONE);
char *malloc_buf;
RECT rect;
WPARAM checked;
GuiControlType *tab_control;
int new_pos;
SYSTEMTIME st[2];
bool do_redraw_if_in_tab = false;
bool do_redraw_unconditionally = false;
switch (guicontrol_cmd)
{
case GUICONTROL_CMD_OPTIONS:
{
GuiControlOptionsType go; // Its contents not currently used here, but it might be in the future.
gui.ControlInitOptions(go, control);
return gui.ControlParseOptions(options, go, control, control_index);
}
case GUICONTROL_CMD_CONTENTS:
case GUICONTROL_CMD_TEXT:
switch (control.type)
{
case GUI_CONTROL_TEXT:
case GUI_CONTROL_GROUPBOX:
do_redraw_unconditionally = (control.attrib & GUI_CONTROL_ATTRIB_BACKGROUND_TRANS); // v1.0.40.01.
// Note that it isn't sufficient in this case to do InvalidateRect(control.hwnd, ...).
break;
case GUI_CONTROL_PIC:
{
// Update: The below doesn't work, so it will be documented that a picture control
// should be always be referred to by its original filename even if the picture changes.
// Set the text unconditionally even if the picture can't be loaded. This text must
// be set to allow GuiControl(Get) to be able to operate upon the picture without
// needing to indentify it via something like "Static14".
//SetWindowText(control.hwnd, aParam3);
//SendMessage(control.hwnd, WM_SETTEXT, 0, (LPARAM)aParam3);
// Set default options, to be possibly overridden by any options actually present:
// Fixed for v1.0.23: Below should use GetClientRect() vs. GetWindowRect(), otherwise
// a size too large will be returned if the control has a border:
GetClientRect(control.hwnd, &rect);
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
int icon_number = 0; // Zero means "load icon or bitmap (doesn't matter)".
// The below must be done only after the above, because setting the control's picture handle
// to NULL sometimes or always shrinks the control down to zero dimensions:
// Although all HBITMAPs are freed upon program termination, if the program changes
// the picture frequently, memory/resources would continue to rise in the meantime
// unless this is done.
// 1.0.40.12: For maintainability, destroy the handle returned by STM_SETIMAGE, even though it
// should be identical to control.union_hbitmap (due to a call to STM_GETIMAGE in another section).
if (control.union_hbitmap)
if (control.attrib & GUI_CONTROL_ATTRIB_ALTBEHAVIOR) // union_hbitmap is an icon or cursor.
// The control's image is set to NULL for the following reasons:
// 1) It turns off the control's animation timer in case the new image is not animated.
// 2) It feels a little bit safter to destroy the image only after it has been removed
// from the control.
// NOTE: IMAGE_ICON or IMAGE_CURSOR must be passed, not IMAGE_BITMAP. Otherwise the
// animated property of the control (via a timer that the control created) will remain
// in effect for the next image, even if it isn't animated, which results in a
// flashing/redrawing effect:
DestroyIcon((HICON)SendMessage(control.hwnd, STM_SETIMAGE, IMAGE_CURSOR, NULL));
// DestroyIcon() works on cursors too. See notes in LoadPicture().
else // union_hbitmap is a bitmap
DeleteObject((HGDIOBJ)SendMessage(control.hwnd, STM_SETIMAGE, IMAGE_BITMAP, NULL));
// Parse any options that are present in front of the filename:
char *next_option = omit_leading_whitespace(aParam3);
if (*next_option == '*') // Options are present. Must check this here and in the for-loop to avoid omitting legitimate whitespace in a filename that starts with spaces.
{
char *option_end, orig_char;
for (; *next_option == '*'; next_option = omit_leading_whitespace(option_end))
{
// Find the end of this option item:
if ( !(option_end = StrChrAny(next_option, " \t")) ) // Space or tab.
option_end = next_option + strlen(next_option); // Set to position of zero terminator instead.
// Permanently terminate in between options to help eliminate ambiguity for words contained
// inside other words, and increase confidence in decimal and hexadecimal conversion.
orig_char = *option_end;
*option_end = '\0';
++next_option; // Skip over the asterisk. It might point to a zero terminator now.
if (!strnicmp(next_option, "Icon", 4))
icon_number = ATOI(next_option + 4); // LoadPicture() correctly handles any negative value.
else
{
switch (toupper(*next_option))
{
case 'W':
width = ATOI(next_option + 1);
break;
case 'H':
height = ATOI(next_option + 1);
break;
// If not one of the above, such as zero terminator or a number, just ignore it.
}
}
*option_end = orig_char; // Undo the temporary termination so that loop's omit_leading() will work.
} // for() each item in option list
// The below assigns option_end + 1 vs. next_option in case the filename is contained in a
// variable ref and/ that filename contains leading spaces. Example:
// GuiControl,, MyPic, *w100 *h-1 %FilenameWithLeadingSpaces%
// Update: Windows XP and perhaps other OSes will load filenames-containing-leading-spaces
// even if those spaces are omitted. However, I'm not sure whether all API calls that
// use filenames do this, so it seems best to include those spaces wheneve possible.
aParam3 = *option_end ? option_end + 1 : option_end; // Set aParam3 to the start of the image's filespec.
}
//else options are not present, so do not set aParam3 to be next_option because that would
// omit legitimate spaces and tabs that might exist at the beginning of a real filename (file
// names can start with spaces).
// See comments in AddControl():
int image_type;
if ( !(control.union_hbitmap = LoadPicture(aParam3, width, height, image_type, icon_number
, control.attrib & GUI_CONTROL_ATTRIB_ALTSUBMIT)) )
return g_ErrorLevel->Assign(ERRORLEVEL_ERROR);
DWORD style = GetWindowLong(control.hwnd, GWL_STYLE);
DWORD style_image_type = style & 0x0F;
style &= ~0x0F; // Purge the low-order four bits in case style-image-type needs to be altered below.
if (image_type == IMAGE_BITMAP)
{
if (style_image_type != SS_BITMAP)
SetWindowLong(control.hwnd, GWL_STYLE, style | SS_BITMAP);
}
else // Icon or Cursor.
if (style_image_type != SS_ICON) // Must apply SS_ICON or such handles cannot be displayed.
SetWindowLong(control.hwnd, GWL_STYLE, style | SS_ICON);
// LoadPicture() uses CopyImage() to scale the image, which seems to provide better scaling
// quality than using MoveWindow() (followed by redrawing the parent window) on the static
// control that contains the image.
SendMessage(control.hwnd, STM_SETIMAGE, image_type, (LPARAM)control.union_hbitmap); // Always returns NULL due to previous call to STM_SETIMAGE above.
// Fix for 1.0.40.12: The below was added because STM_SETIMAGE above may have caused the control to
// create a new hbitmap (possibly only for alpha channel bitmaps on XP, but might also apply to icons),
// in which case we now have two handles: the one inside the control and the one from which
// it was copied. Task Manager confirms that the control does not delete the original
// handle when it creates a new handle. Rather than waiting until later to delete the handle,
// it seems best to do it here so that:
// 1) The script uses less memory during the time that the picture control exists.
// 2) Don't have to delete two handles (control.union_hbitmap and the one returned by STM_SETIMAGE)
// when the time comes to change the image inside the control.
//
// MSDN: "With Microsoft Windows XP, if the bitmap passed in the STM_SETIMAGE message contains pixels
// with non-zero alpha, the static control takes a copy of the bitmap. This copied bitmap is returned
// by the next STM_SETIMAGE message... if it does not check and release the bitmaps returned from
// STM_SETIMAGE messages, the bitmaps are leaked."
HBITMAP hbitmap_actual;
if ( (hbitmap_actual = (HBITMAP)SendMessage(control.hwnd, STM_GETIMAGE, image_type, 0)) // Assign
&& hbitmap_actual != control.union_hbitmap ) // The control decided to make a new handle.
{
if (image_type == IMAGE_BITMAP)
DeleteObject(control.union_hbitmap);
else // Icon or cursor.
DestroyIcon((HICON)control.union_hbitmap); // Works on cursors too.
// In additional to improving maintainability, the following might also be necessary to allow
// Gui::Destroy() to avoid a memory leak when the picture control is destroyed as a result
// of its parent being destroyed (though I've read that the control is supposed to destroy its
// hbitmap when it was directly responsible for creating it originally [but not otherwise]):
control.union_hbitmap = hbitmap_actual;
}
if (image_type == IMAGE_BITMAP)
control.attrib &= ~GUI_CONTROL_ATTRIB_ALTBEHAVIOR; // Flag it as a bitmap so that DeleteObject vs. DestroyIcon will be called for it.
else // Cursor or Icon, which are functionally identical.
control.attrib |= GUI_CONTROL_ATTRIB_ALTBEHAVIOR;
// Fix for v1.0.33.02: If this control belongs to a tab control and is visible (i.e. its page
// in the tab control is the current page), must redraw the tab control to get the picture/icon
// to update correctly. v1.0.40.01: Pictures such as .Gif sometimes disappear (even if they're
// not in a tab control):
//do_redraw_if_in_tab = true;
do_redraw_unconditionally = true;
break; // Rather than return, continue on to do the redraw.
}
case GUI_CONTROL_BUTTON:
break;
case GUI_CONTROL_CHECKBOX:
case GUI_CONTROL_RADIO:
if (guicontrol_cmd == GUICONTROL_CMD_CONTENTS && IsPureNumeric(aParam3, true, false))
{
checked = ATOI(aParam3);
if (!checked || checked == 1 || (control.type == GUI_CONTROL_CHECKBOX && checked == -1))
{
if (checked == -1)
checked = BST_INDETERMINATE;
//else the "checked" var is already set correctly.
if (control.type == GUI_CONTROL_RADIO)
{
gui.ControlCheckRadioButton(control, control_index, checked);
return OK;
}
// Otherwise, we're operating upon a checkbox.
SendMessage(control.hwnd, BM_SETCHECK, checked, 0);
return OK;
}
//else the default SetWindowText() action will be taken below.
}
// else assume it's the text/caption for the item, so the default SetWindowText() action will be taken below.
break; // Fix for v1.0.35.01: Don't return, continue onward.
case GUI_CONTROL_LISTVIEW:
case GUI_CONTROL_TREEVIEW:
// Due to the fact that an LV's first col. can't be directly deleted and other complexities,
// this is not currently supported (also helps reduce code size). The built-in function
// for modifying columns should be used instead. Similar for TreeView.
return OK;
case GUI_CONTROL_EDIT:
// Note that TranslateLFtoCRLF() will return the original buffer we gave it if no translation
// is needed. Otherwise, it will return a new buffer which we are responsible for freeing
// when done (or NULL if it failed to allocate the memory).
malloc_buf = (*aParam3 && (GetWindowLong(control.hwnd, GWL_STYLE) & ES_MULTILINE))
? TranslateLFtoCRLF(aParam3) : aParam3; // Automatic translation, as documented.
SetWindowText(control.hwnd, malloc_buf ? malloc_buf : aParam3); // malloc_buf is checked again in case the mem alloc failed.
if (malloc_buf && malloc_buf != aParam3)
free(malloc_buf);
return OK;
case GUI_CONTROL_DATETIME:
if (guicontrol_cmd == GUICONTROL_CMD_CONTENTS)
{
if (*aParam3)
{
if (YYYYMMDDToSystemTime(aParam3, st[0], true))
DateTime_SetSystemtime(control.hwnd, GDT_VALID, st);
//else invalid, so leave current sel. unchanged.
}
else // User wants there to be no date selection.
{
// Ensure the DTS_SHOWNONE style is present, otherwise it won't work. However,
// it appears that this style cannot be applied after the control is created, so
// this line is commented out:
//SetWindowLong(control.hwnd, GWL_STYLE, GetWindowLong(control.hwnd, GWL_STYLE) | DTS_SHOWNONE);
DateTime_SetSystemtime(control.hwnd, GDT_NONE, st); // Contents of st are ignored in this mode.
}
}
else // GUICONTROL_CMD_TEXT
{
bool use_custom_format = false; // Set default.
// Reset style to "pure" so that new style (or custom format) can take effect.
DWORD style = GetWindowLong(control.hwnd, GWL_STYLE) // DTS_SHORTDATEFORMAT==0 so can be omitted below.
& ~(DTS_LONGDATEFORMAT | DTS_SHORTDATECENTURYFORMAT | DTS_TIMEFORMAT);
if (*aParam3)
{
// DTS_SHORTDATEFORMAT and DTS_SHORTDATECENTURYFORMAT
// seem to produce identical results (both display 4-digit year), at least on XP. Perhaps
// DTS_SHORTDATECENTURYFORMAT is obsolete. In any case, it's uncommon so for simplicity, is
// not a named style. It can always be applied numerically if desired. Update:
// DTS_SHORTDATECENTURYFORMAT is now applied by default upon creation, which can be overridden
// explicitly via -0x0C in the control's options.
if (!stricmp(aParam3, "LongDate")) // LongDate seems more readable than "Long". It also matches the keyword used by FormatTime.
style |= DTS_LONGDATEFORMAT; // Competing styles were already purged above.
else if (!stricmp(aParam3, "Time"))
style |= DTS_TIMEFORMAT; // Competing styles were already purged above.
else // Custom format.
use_custom_format = true;
}
//else aText is blank and use_custom_format==false, which will put DTS_SHORTDATEFORMAT into effect.
if (!use_custom_format)
SetWindowLong(control.hwnd, GWL_STYLE, style);
//else leave style unchanged so that if format is later removed, the underlying named style will
// not have been altered.
// This both adds and removes the custom format depending on aParma3:
DateTime_SetFormat(control.hwnd, (use_custom_format ? aParam3 : (char*)(NULL))); // NULL removes any custom format so that the underlying style format is revealed.
}
return OK;
case GUI_CONTROL_MONTHCAL:
if (*aParam3)
{
DWORD gdtr = YYYYMMDDToSystemTime2(aParam3, st);
if (!gdtr) // Neither min nor max is present (or both are invalid).
break; // Leave current sel. unchanged.
if (GetWindowLong(control.hwnd, GWL_STYLE) & MCS_MULTISELECT) // Must use range-selection even if selection is only one date.
{
if (gdtr == GDTR_MIN) // No maximum is present, so set maximum to minimum.
st[1] = st[0];
//else just max, or both are present. Assume both for code simplicity.
MonthCal_SetSelRange(control.hwnd, st);
}
else
MonthCal_SetCurSel(control.hwnd, st);
//else invalid, so leave current sel. unchanged.
do_redraw_if_in_tab = true; // Confirmed necessary.
break;
}
//else blank, so do nothing (control does not support having "no selection").
return OK; // Don't break since don't the other actions below to be taken.
case GUI_CONTROL_HOTKEY:
SendMessage(control.hwnd, HKM_SETHOTKEY, gui.TextToHotkey(aParam3), 0); // This will set it to "None" if aParam3 is blank.
return OK; // Don't break since don't the other actions below to be taken.
case GUI_CONTROL_UPDOWN:
if (*aParam3 == '+') // Apply as delta from its current position.
{
new_pos = ATOI(aParam3 + 1);
// Any out of range or non-numeric value in the buddy is ignored since error reporting is
// left up to the script, which can compare contents of buddy to those of UpDown to check
// validity if it wants.
if (control.attrib & GUI_CONTROL_ATTRIB_ALTBEHAVIOR) // It has a 32-bit vs. 16-bit range.
new_pos += (int)SendMessage(control.hwnd, UDM_GETPOS32, 0, 0);
else // 16-bit. Must cast to short to omit the error portion (see comment above).
new_pos += (short)SendMessage(control.hwnd, UDM_GETPOS, 0, 0);
// Above uses +1 to omit the plus sign, which allows a negative delta via +-5.
// -5 is not treated as a delta because that would be ambiguous with an absolute position.
// In any case, it seems like too much code to be justified.
}
else
new_pos = ATOI(aParam3);
// MSDN: "If the parameter is outside the control's specified range, nPos will be set to the nearest
// valid value."
SendMessage(control.hwnd, (control.attrib & GUI_CONTROL_ATTRIB_ALTBEHAVIOR) ? UDM_SETPOS32 : UDM_SETPOS
, 0, new_pos); // Unnecessary to cast to short in the case of UDM_SETPOS, since it ignores the high-order word.
return OK; // Don't break since don't the other actions below to be taken.
case GUI_CONTROL_SLIDER:
// Confirmed this fact from MSDN: That the control automatically deals with out-of-range values
// by setting slider to min or max:
if (*aParam3 == '+') // Apply as delta from its current position.
{
new_pos = ATOI(aParam3 + 1);
if (control.attrib & GUI_CONTROL_ATTRIB_ALTBEHAVIOR)
new_pos = -new_pos; // Delta moves to opposite direction if control is inverted.
SendMessage(control.hwnd, TBM_SETPOS, TRUE
, SendMessage(control.hwnd, TBM_GETPOS, 0, 0) + new_pos);
// Above uses +1 to omit the plus sign, which allows a negative delta via +-5.
// -5 is not treated as a delta because that would be ambiguous with an absolute position.
// In any case, it seems like too much code to be justified.
}
else
SendMessage(control.hwnd, TBM_SETPOS, TRUE, gui.ControlInvertSliderIfNeeded(control, ATOI(aParam3)));
// Above msg has no return value.
return OK; // Don't break since don't the other actions below to be taken.
case GUI_CONTROL_PROGRESS:
// Confirmed through testing (PBM_DELTAPOS was also tested): The control automatically deals
// with out-of-range values by setting bar to min or max.
if (*aParam3 == '+')
// This allows a negative delta, e.g. via +-5. Nothing fancier is done since the need
// to go backwards in a progress bar is rare.
SendMessage(control.hwnd, PBM_DELTAPOS, ATOI(aParam3 + 1), 0);
else
SendMessage(control.hwnd, PBM_SETPOS, ATOI(aParam3), 0);
return OK; // Don't break since don't the other actions below to be taken.
case GUI_CONTROL_STATUSBAR:
SetWindowText(control.hwnd, aParam3);
return OK;
default: // Namely the following:
//case GUI_CONTROL_DROPDOWNLIST:
//case GUI_CONTROL_COMBOBOX:
//case GUI_CONTROL_LISTBOX:
//case GUI_CONTROL_TAB:
if (control.type == GUI_CONTROL_COMBOBOX && guicontrol_cmd == GUICONTROL_CMD_TEXT)
{
// Fix for v1.0.40.08: Must clear the current selection to avoid Submit/GuiControlGet
// retrieving it instead of the text that's about to be put into the Edit field. Note that
// whatever changes are done here should tested to work with ComboBox's AltSubmit option also.
// After the next text is added to the Edit field, upon GuiControlGet or "Gui Submit", that
// text will be checked against the drop-list to see if it matches any of the selections
// It's done at that stage rather than here because doing it there also solves the issue
// of the user manually entering a selection into the Edit field and then failing to get
// the position of the matching item when the ComboBox is set to AltSubmit mode.
SendMessage(control.hwnd, CB_SETCURSEL, -1, 0);
break; // v1.0.38: Fall through to the SetWindowText() method, which works to set combo's edit field.
}
// Seems best not to do the below due to the extreme rarity of anyone wanting to change a
// ListBox or ComboBox's hidden caption. That can be done via ControlSetText if it is
// ever needed. The advantage of not doing this is that the "TEXT" command can be used
// as a gentle, slight-variant of GUICONTROL_CMDCONTENTS, i.e. without needing to worry
// what the target control's type is:
//if (guicontrol_cmd == GUICONTROL_CMD_TEXT)
// break;
bool list_replaced;
if (*aParam3 == gui.mDelimiter) // The signal to overwrite rather than append to the list.
{
list_replaced = true;
++aParam3; // Exclude the initial pipe from further consideration.
int msg;
switch (control.type)
{
case GUI_CONTROL_TAB: msg = TCM_DELETEALLITEMS; break; // Same as TabCtrl_DeleteAllItems().
case GUI_CONTROL_LISTBOX: msg = LB_RESETCONTENT; break;
default: // DropDownList or ComboBox
msg = CB_RESETCONTENT;
}
SendMessage(control.hwnd, msg, 0, 0); // Delete all items currently in the list.
}
else
list_replaced = false;
gui.ControlAddContents(control, aParam3, 0);
if (control.type == GUI_CONTROL_TAB && list_replaced)
{
// In case replacement tabs deleted the currently active tab, update the tab.
// The "false" param will cause focus to jump to first item in z-order if
// a the control that previously had focus was inside a tab that was just
// deleted (seems okay since this kind of operation is fairly rare):
gui.ControlUpdateCurrentTab(control, false);
// Must invalidate part of parent window to get controls to redraw correctly, at least
// in the following case: Tab that is currently active still exists and is still active
// after the tab-rebuild done above.
// For simplicitly, invalidate the whole thing since changing the quantity/names of tabs
// while the window is visible is rare. NOTE: It might be necessary to invalidate
// the entire window *anyway* in case some of this tab's controls exist outside its
// boundaries (e.g. TCS_BUTTONS). Another reason is the fact that there have been
// problems retrieving an accurate client area for tab controls when they have certain
// styles such as TCS_VERTICAL:
InvalidateRect(gui.mHwnd, NULL, TRUE); // TRUE = Seems safer to erase, not knowing all possible overlaps.
}
return OK; // Don't break since don't the other actions below to be taken.
} // inner switch() for control's type for contents/txt sub-commands.
if (do_redraw_if_in_tab) // Excludes the SetWindowText() below, but might need changing for future control types.
break;
// Otherwise:
// The only other reason it wouldn't have already returned is to fall back to SetWindowText() here.
// Since above didn't return or break, it's either:
// 1) A control that uses the standard SetWindowText() method such as GUI_CONTROL_TEXT,
// GUI_CONTROL_GROUPBOX, or GUI_CONTROL_BUTTON.
// 2) A radio or checkbox whose caption is being changed instead of its checked state.
SetWindowText(control.hwnd, aParam3); // Seems more reliable to set text before doing the redraw, plus it saves code size.
if (do_redraw_unconditionally)
break;
return OK;
case GUICONTROL_CMD_MOVE:
case GUICONTROL_CMD_MOVEDRAW:
{
int xpos = COORD_UNSPECIFIED;
int ypos = COORD_UNSPECIFIED;
int width = COORD_UNSPECIFIED;
int height = COORD_UNSPECIFIED;
for (char *cp = aParam3; *cp; ++cp)
{
switch(toupper(*cp))
{
// For options such as W, H, X and Y:
// Use atoi() vs. ATOI() to avoid interpreting something like 0x01B as hex when in fact
// the B was meant to be an option letter (though in this case, none of the hex digits are
// currently used as option letters):
case 'W':
width = atoi(cp + 1);
break;
case 'H':
height = atoi(cp + 1);
break;
case 'X':
xpos = atoi(cp + 1);
break;
case 'Y':
ypos = atoi(cp + 1);
break;
}
}
GetWindowRect(control.hwnd, &rect); // Failure seems too rare to check for.
POINT dest_pt = {rect.left, rect.top};
ScreenToClient(gui.mHwnd, &dest_pt); // Set default x/y target position, to be possibly overridden below.
if (xpos != COORD_UNSPECIFIED)
dest_pt.x = xpos;
if (ypos != COORD_UNSPECIFIED)
dest_pt.y = ypos;
if (!MoveWindow(control.hwnd, dest_pt.x, dest_pt.y
, width == COORD_UNSPECIFIED ? rect.right - rect.left : width
, height == COORD_UNSPECIFIED ? rect.bottom - rect.top : height
, TRUE)) // Do repaint.
return g_ErrorLevel->Assign(ERRORLEVEL_ERROR);
// Note that GUI_CONTROL_UPDOWN has no special handling here. This is because unlike slider buddies,
// whose only purpose is to label the control, an up-down's is also content-linked to it, so the
// inability to move the up-down to separate it from its buddy would be a loss of flexibility. For
// this reason and also to reduce code size, the control is not re-buddied to snap them together.
if (control.type == GUI_CONTROL_SLIDER) // It seems buddies don't move automatically, so trigger the move.
{
HWND buddy1 = (HWND)SendMessage(control.hwnd, TBM_GETBUDDY, TRUE, 0);
HWND buddy2 = (HWND)SendMessage(control.hwnd, TBM_GETBUDDY, FALSE, 0);
if (buddy1)
{
SendMessage(control.hwnd, TBM_SETBUDDY, TRUE, (LPARAM)buddy1);
// It doesn't always redraw the buddies correctly, at least on XP, so do it manually:
InvalidateRect(buddy1, NULL, TRUE);
}
if (buddy2)
{
SendMessage(control.hwnd, TBM_SETBUDDY, FALSE, (LPARAM)buddy2);
InvalidateRect(buddy2, NULL, TRUE);
}
}
// v1.0.41.02: To prevent severe flickering when resizing ListViews and other controls,
// the MOVE mode now avoids doing the invalidate-rect, but the MOVEDRAW mode does do it.
if (guicontrol_cmd == GUICONTROL_CMD_MOVEDRAW)
{
// This must be done, at least in cases such as GroupBox under certain themes/conditions.
// More than just control.hwnd must be invalided, otherwise the interior of the GroupBox retains
// a ghost image of whatever was in it before the move:
GetWindowRect(control.hwnd, &rect); // Limit it to only that part of the client area that is receiving the rect.
MapWindowPoints(NULL, gui.mHwnd, (LPPOINT)&rect, 2); // Convert rect to client coordinates (not the same as GetClientRect()).
InvalidateRect(gui.mHwnd, &rect, TRUE); // Seems safer to use TRUE, not knowing all possible overlaps, etc.
}
return OK;
}
case GUICONTROL_CMD_FOCUS:
return SetFocus(control.hwnd) ? OK : g_ErrorLevel->Assign(ERRORLEVEL_ERROR);
case GUICONTROL_CMD_ENABLE:
case GUICONTROL_CMD_DISABLE:
// GUI_CONTROL_ATTRIB_EXPLICITLY_DISABLED is maintained for use with tab controls. It allows controls
// on inactive tabs to be marked for later enabling. It also allows explicitly disabled controls to
// stay disabled even when their tab/page becomes active. It is updated unconditionally for simplicity
// and maintainability.
if (guicontrol_cmd == GUICONTROL_CMD_ENABLE)
control.attrib &= ~GUI_CONTROL_ATTRIB_EXPLICITLY_DISABLED;
else
control.attrib |= GUI_CONTROL_ATTRIB_EXPLICITLY_DISABLED;
if ((tab_control = gui.FindTabControl(control.tab_control_index)) // It belongs to a tab control that already exists.
&& ((GetWindowLong(tab_control->hwnd, GWL_STYLE) & WS_DISABLED) // But its tab control is disabled...
|| TabCtrl_GetCurSel(tab_control->hwnd) != control.tab_index))
// ... or either there is no current tab/page (or there are no tabs at all) or the one selected
// is not this control's: Do not disable or re-enable the control in this case.
return OK;
// Since above didn't return, act upon the enabled/disable:
EnableWindow(control.hwnd, guicontrol_cmd == GUICONTROL_CMD_ENABLE ? TRUE : FALSE);
if (control.type == GUI_CONTROL_TAB) // This control is a tab control.
// Update the control so that its current tab's controls will all be enabled or disabled (now
// that the tab control itself has just been enabled or disabled):
gui.ControlUpdateCurrentTab(control, false);
return OK;
case GUICONTROL_CMD_SHOW:
case GUICONTROL_CMD_HIDE:
// GUI_CONTROL_ATTRIB_EXPLICITLY_HIDDEN is maintained for use with tab controls. It allows controls
// on inactive tabs to be marked for later showing. It also allows explicitly hidden controls to
// stay hidden even when their tab/page becomes active. It is updated unconditionally for simplicity
// and maintainability.
if (guicontrol_cmd == GUICONTROL_CMD_SHOW)
control.attrib &= ~GUI_CONTROL_ATTRIB_EXPLICITLY_HIDDEN;
else
control.attrib |= GUI_CONTROL_ATTRIB_EXPLICITLY_HIDDEN;
if ((tab_control = gui.FindTabControl(control.tab_control_index)) // It belongs to a tab control that already exists.
&& (!(GetWindowLong(tab_control->hwnd, GWL_STYLE) & WS_VISIBLE) // But its tab control is hidden...
|| TabCtrl_GetCurSel(tab_control->hwnd) != control.tab_index))
// ... or either there is no current tab/page (or there are no tabs at all) or the one selected
// is not this control's: Do not show or hide the control in this case.
return OK;
// Since above didn't return, act upon the show/hide:
ShowWindow(control.hwnd, guicontrol_cmd == GUICONTROL_CMD_SHOW ? SW_SHOWNOACTIVATE : SW_HIDE);
if (control.type == GUI_CONTROL_TAB) // This control is a tab control.
// Update the control so that its current tab's controls will all be shown or hidden (now
// that the tab control itself has just been shown or hidden):
gui.ControlUpdateCurrentTab(control, false);
return OK;
case GUICONTROL_CMD_CHOOSE:
case GUICONTROL_CMD_CHOOSESTRING:
{
int selection_index;
int extra_actions = 0; // Set default.
if (*aParam3 == gui.mDelimiter) // First extra action.
{
++aParam3; // Omit this pipe char from further consideration below.
++extra_actions;
}
if (control.type == GUI_CONTROL_TAB)
{
// Generating the TCN_SELCHANGING and TCN_SELCHANGE messages manually is fairly complex since they
// need a struct and who knows whether it's even valid for sources other than the tab controls
// themselves to generate them. I would use TabCtrl_SetCurFocus(), but that is shot down by
// the fact that it only generates TCN_SELCHANGING and TCN_SELCHANGE if the tab control lacks
// the TCS_BUTTONS style, which would make it an incomplete/inconsistent solution. But I guess
// it's better than nothing as long as it's documented.
// MSDN: "If the tab control does not have the TCS_BUTTONS style, changing the focus also changes
// selected tab. In this case, the tab control sends the TCN_SELCHANGING and TCN_SELCHANGE
// notification messages to its parent window.
// Automatically switch to CHOOSESTRING if parameter isn't numeric:
if (guicontrol_cmd == GUICONTROL_CMD_CHOOSE && !IsPureNumeric(aParam3, true, false))
guicontrol_cmd = GUICONTROL_CMD_CHOOSESTRING;
if (guicontrol_cmd == GUICONTROL_CMD_CHOOSESTRING)
selection_index = gui.FindTabIndexByName(control, aParam3); // Returns -1 on failure.
else
selection_index = ATOI(aParam3) - 1;
if (selection_index < 0 || selection_index > MAX_TABS_PER_CONTROL - 1)
return g_ErrorLevel->Assign(ERRORLEVEL_ERROR);
int previous_selection_index = TabCtrl_GetCurSel(control.hwnd);
if (!extra_actions || (GetWindowLong(control.hwnd, GWL_STYLE) & TCS_BUTTONS))
{
if (TabCtrl_SetCurSel(control.hwnd, selection_index) == -1)
return g_ErrorLevel->Assign(ERRORLEVEL_ERROR);
// In this case but not the "else" below, must update the tab to show the proper controls:
if (previous_selection_index != selection_index)
gui.ControlUpdateCurrentTab(control, extra_actions > 0); // And set focus if the more forceful extra_actions was done.
}
else // There is an extra_action and it's not TCS_BUTTONS, so extra_action is possible via TabCtrl_SetCurFocus.
{
TabCtrl_SetCurFocus(control.hwnd, selection_index); // No return value, so check for success below.
if (TabCtrl_GetCurSel(control.hwnd) != selection_index)
return g_ErrorLevel->Assign(ERRORLEVEL_ERROR);
}
return OK;
}
// Otherwise, it's not a tab control, but a ListBox/DropDownList/Combo or other control:
if (*aParam3 == gui.mDelimiter && control.type != GUI_CONTROL_TAB) // Second extra action.
{
++aParam3; // Omit this pipe char from further consideration below.
++extra_actions;
}
if (guicontrol_cmd == GUICONTROL_CMD_CHOOSE && !IsPureNumeric(aParam3, true, false)) // Must be done only after the above.
guicontrol_cmd = GUICONTROL_CMD_CHOOSESTRING;
UINT msg, x_msg, y_msg;
switch(control.type)
{
case GUI_CONTROL_DROPDOWNLIST:
case GUI_CONTROL_COMBOBOX:
msg = (guicontrol_cmd == GUICONTROL_CMD_CHOOSE) ? CB_SETCURSEL : CB_SELECTSTRING;
x_msg = CBN_SELCHANGE;
y_msg = CBN_SELENDOK;
break;
case GUI_CONTROL_LISTBOX:
if (GetWindowLong(control.hwnd, GWL_STYLE) & (LBS_EXTENDEDSEL|LBS_MULTIPLESEL))
{
if (guicontrol_cmd == GUICONTROL_CMD_CHOOSE)
msg = LB_SETSEL;
else
// MSDN: Do not use [LB_SELECTSTRING] with a list box that has the LBS_MULTIPLESEL or the
// LBS_EXTENDEDSEL styles:
msg = LB_FINDSTRING;
}
else // single-select listbox
if (guicontrol_cmd == GUICONTROL_CMD_CHOOSE)
msg = LB_SETCURSEL;
else
msg = LB_SELECTSTRING;
x_msg = LBN_SELCHANGE;
y_msg = LBN_DBLCLK;
break;
default: // Not a supported control type.
return g_ErrorLevel->Assign(ERRORLEVEL_ERROR);
} // switch(control.type)
if (guicontrol_cmd == GUICONTROL_CMD_CHOOSESTRING)
{
if (msg == LB_FINDSTRING)
{
// This msg is needed for multi-select listbox because LB_SELECTSTRING is not supported
// in this case.
LRESULT found_item = SendMessage(control.hwnd, msg, -1, (LPARAM)aParam3);
if (found_item == CB_ERR) // CB_ERR == LB_ERR
return g_ErrorLevel->Assign(ERRORLEVEL_ERROR);
if (SendMessage(control.hwnd, LB_SETSEL, TRUE, found_item) == CB_ERR) // CB_ERR == LB_ERR