-
Notifications
You must be signed in to change notification settings - Fork 906
/
script_gui.cpp
11010 lines (10078 loc) · 507 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-2009 Chris Mallett (support@autohotkey.com)
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 <Shlwapi.h>
#include <uxtheme.h>
#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()
#include "script_func_impl.h"
// Gui methods use this macro either if they require the Gui to have a window,
// or if they aren't safe to use after Dispose() is called.
#define GUI_MUST_HAVE_HWND if (!mHwnd) return GuiNoWindowError()
// Using a noinline function reduces code by a few hundred bytes across all methods.
// Returning something like FR_E_WIN32(ERROR_INVALID_WINDOW_HANDLE) instead is about
// the same, so it seems better to use the clearer error message.
__declspec(noinline) FResult GuiNoWindowError() { return FError(ERR_GUI_NO_WINDOW); }
#define CTRL_THROW_IF_DESTROYED if (!hwnd) return ControlDestroyedError()
__declspec(noinline) FResult ControlDestroyedError() { return FError(_T("The control is destroyed.")); }
// Window class atom used by Guis.
static ATOM sGuiWinClass;
LPTSTR GuiControlType::sTypeNames[GUI_CONTROL_TYPE_COUNT] = { GUI_CONTROL_TYPE_NAMES };
GuiControls GuiControlType::ConvertTypeName(LPCTSTR aTypeName)
{
for (int i = 1; i < _countof(sTypeNames); ++i) // Start at 1 because 0 is GUI_CONTROL_INVALID.
if (!_tcsicmp(aTypeName, sTypeNames[i]))
return (GuiControls)i;
if (!_tcsicmp(aTypeName, _T("DropDownList"))) return GUI_CONTROL_DROPDOWNLIST;
if (!_tcsicmp(aTypeName, _T("Picture"))) return GUI_CONTROL_PIC;
return GUI_CONTROL_INVALID;
}
LPTSTR GuiControlType::GetTypeName()
{
if (type == GUI_CONTROL_TAB)
{
// Tab2 and Tab3 are changed into Tab at an early stage.
if (attrib & GUI_CONTROL_ATTRIB_ALTBEHAVIOR)
return sTypeNames[GUI_CONTROL_TAB2];
if (GetProp(hwnd, _T("ahk_dlg")))
return sTypeNames[GUI_CONTROL_TAB3];
}
return sTypeNames[type];
}
GuiControlType::TypeAttribs GuiControlType::TypeHasAttrib(GuiControls aType, TypeAttribs aAttrib)
{
static TypeAttribs sAttrib[] = { 0,
/*Text*/ TYPE_STATICBACK | TYPE_SUPPORTS_BGTRANS | TYPE_NO_SUBMIT,
/*Pic*/ TYPE_STATICBACK | TYPE_SUPPORTS_BGTRANS | TYPE_NO_SUBMIT | TYPE_HAS_NO_TEXT | TYPE_RESERVE_UNION,
/*GroupBox*/ TYPE_STATICBACK | TYPE_SUPPORTS_BGTRANS | TYPE_NO_SUBMIT, // Background affects only the label.
/*Button*/ TYPE_MSGBKCOLOR | TYPE_SUPPORTS_BGTRANS | TYPE_NO_SUBMIT, // Background affects only the button's border, on Windows 10.
/*CheckBox*/ TYPE_STATICBACK,
/*Radio*/ TYPE_STATICBACK | TYPE_NO_SUBMIT, // No-submit: Radio controls are handled separately, by group.
/*DDL*/ TYPE_MSGBKCOLOR | TYPE_HAS_ITEMS, // Background affects only the main control, and requires -Theme.
/*ComboBox*/ TYPE_MSGBKCOLOR | TYPE_HAS_ITEMS, // Background affects only the main control.
/*ListBox*/ TYPE_MSGBKCOLOR | TYPE_HAS_ITEMS,
/*ListView*/ TYPE_SETBKCOLOR | TYPE_NO_SUBMIT | TYPE_RESERVE_UNION | TYPE_HAS_ITEMS,
/*TreeView*/ TYPE_SETBKCOLOR | TYPE_NO_SUBMIT,
/*Edit*/ TYPE_MSGBKCOLOR,
/*DateTime*/ 0,
/*MonthCal*/ 0,
/*Hotkey*/ 0,
/*UpDown*/ TYPE_HAS_NO_TEXT,
/*Slider*/ TYPE_STATICBACK | TYPE_HAS_NO_TEXT,
/*Progress*/ TYPE_SETBKCOLOR | TYPE_HAS_NO_TEXT | TYPE_NO_SUBMIT,
/*Tab*/ TYPE_STATICBACK | TYPE_HAS_ITEMS,
/*Tab2*/ TYPE_STATICBACK | TYPE_HAS_ITEMS, // Only used prior to control construction; at least TYPE_HAS_ITEMS is needed here.
/*Tab3*/ TYPE_STATICBACK | TYPE_HAS_ITEMS, // As above.
/*ActiveX*/ TYPE_HAS_NO_TEXT | TYPE_NO_SUBMIT | TYPE_RESERVE_UNION,
/*Link*/ TYPE_STATICBACK | TYPE_NO_SUBMIT,
/*Custom*/ TYPE_MSGBKCOLOR | TYPE_NO_SUBMIT, // Custom controls *may* use WM_CTLCOLOR.
/*StatusBar*/ TYPE_SETBKCOLOR | TYPE_NO_SUBMIT,
};
return sAttrib[aType] & aAttrib;
}
UCHAR **ConstructEventSupportArray()
{
// This is mostly static data, but I haven't figured out how to construct it with just
// an array initialiser (other than by using a fixed size for the sub-array, which
// wastes space due to ListView and TreeView having many more events than other types).
static UCHAR *raises[GUI_CONTROL_TYPE_COUNT];
// Not necessary since the array is static and therefore initialised to 0 (NULL):
//#define RAISES_NONE(ctrl) \
// raises[ctrl] = NULL;
#define RAISES(ctrl, ...) { \
static UCHAR events[] = { __VA_ARGS__, 0 }; \
raises[ctrl] = events; \
}
RAISES(GUI_CONTROL_TEXT, GUI_EVENT_CLICK, GUI_EVENT_DBLCLK)
RAISES(GUI_CONTROL_PIC, GUI_EVENT_CLICK, GUI_EVENT_DBLCLK)
//RAISES_NONE(GUI_CONTROL_GROUPBOX)
RAISES(GUI_CONTROL_BUTTON, GUI_EVENT_CLICK, GUI_EVENT_DBLCLK, GUI_EVENT_FOCUS, GUI_EVENT_LOSEFOCUS)
RAISES(GUI_CONTROL_CHECKBOX, GUI_EVENT_CLICK, GUI_EVENT_DBLCLK, GUI_EVENT_FOCUS, GUI_EVENT_LOSEFOCUS)
RAISES(GUI_CONTROL_RADIO, GUI_EVENT_CLICK, GUI_EVENT_DBLCLK, GUI_EVENT_FOCUS, GUI_EVENT_LOSEFOCUS)
RAISES(GUI_CONTROL_DROPDOWNLIST, GUI_EVENT_CHANGE, GUI_EVENT_FOCUS, GUI_EVENT_LOSEFOCUS)
RAISES(GUI_CONTROL_COMBOBOX, GUI_EVENT_CHANGE, GUI_EVENT_DBLCLK, GUI_EVENT_FOCUS, GUI_EVENT_LOSEFOCUS)
RAISES(GUI_CONTROL_LISTBOX, GUI_EVENT_CHANGE, GUI_EVENT_DBLCLK, GUI_EVENT_FOCUS, GUI_EVENT_LOSEFOCUS)
RAISES(GUI_CONTROL_LISTVIEW, GUI_EVENT_DBLCLK, GUI_EVENT_COLCLK, GUI_EVENT_CLICK, GUI_EVENT_ITEMFOCUS, GUI_EVENT_ITEMSELECT, GUI_EVENT_ITEMCHECK, GUI_EVENT_ITEMEDIT, GUI_EVENT_FOCUS, GUI_EVENT_LOSEFOCUS)
RAISES(GUI_CONTROL_TREEVIEW, GUI_EVENT_ITEMSELECT, GUI_EVENT_DBLCLK, GUI_EVENT_CLICK, GUI_EVENT_ITEMEXPAND, GUI_EVENT_ITEMCHECK, GUI_EVENT_ITEMEDIT, GUI_EVENT_FOCUS, GUI_EVENT_LOSEFOCUS)
RAISES(GUI_CONTROL_EDIT, GUI_EVENT_CHANGE, GUI_EVENT_FOCUS, GUI_EVENT_LOSEFOCUS)
RAISES(GUI_CONTROL_DATETIME, GUI_EVENT_CHANGE, GUI_EVENT_FOCUS, GUI_EVENT_LOSEFOCUS)
RAISES(GUI_CONTROL_MONTHCAL, GUI_EVENT_CHANGE)
RAISES(GUI_CONTROL_HOTKEY, GUI_EVENT_CHANGE)
RAISES(GUI_CONTROL_UPDOWN, GUI_EVENT_CHANGE)
RAISES(GUI_CONTROL_SLIDER, GUI_EVENT_CHANGE)
//RAISES_NONE(GUI_CONTROL_PROGRESS)
RAISES(GUI_CONTROL_TAB, GUI_EVENT_CHANGE)
//RAISES(GUI_CONTROL_TAB2, ) // Not used since it is changed to TAB at an early stage.
//RAISES(GUI_CONTROL_TAB3, )
//RAISES_NONE(GUI_CONTROL_ACTIVEX)
RAISES(GUI_CONTROL_LINK, GUI_EVENT_CLICK)
//RAISES_NONE(GUI_CONTROL_CUSTOM)
RAISES(GUI_CONTROL_STATUSBAR, GUI_EVENT_CLICK, GUI_EVENT_DBLCLK)
//#undef RAISES_NONE
#undef RAISES
return raises;
}
UCHAR **GuiControlType::sRaisesEvents = ConstructEventSupportArray();
bool GuiControlType::SupportsEvent(GuiEventType aEvent)
{
if (UCHAR *this_raises = sRaisesEvents[type])
{
for (;; ++this_raises)
{
if (*this_raises == aEvent)
return true;
if (!*this_raises) // Checked after aEvent, as it will often be the first item in the array.
break;
}
}
if (aEvent == GUI_EVENT_CONTEXTMENU)
return true; // All controls implicitly support this.
return false;
}
// Helper function used to convert a token to an Array object.
static Array* TokenToArray(ExprTokenType &token)
{
return dynamic_cast<Array *>(TokenToObject(token));
}
// Enumerator for GuiType objects.
ResultType GuiType::GetEnumItem(UINT &aIndex, Var *aOutputVar1, Var *aOutputVar2, int aVarCount)
{
if (aIndex >= mControlCount) // Use >= vs. == in case the Gui was destroyed.
return CONDITION_FALSE;
GuiControlType* ctrl = mControl[aIndex];
if (aVarCount == 1) // `for x in myGui` or `[myGui*]`
{
aOutputVar2 = aOutputVar1; // Return the more useful value in single-var mode: the control object.
aOutputVar1 = nullptr;
}
if (aOutputVar1)
aOutputVar1->AssignHWND(ctrl->hwnd);
if (aOutputVar2)
aOutputVar2->Assign(ctrl);
return CONDITION_TRUE;
}
FResult GuiType::__Enum(optl<int> aVarCount, IObject *&aRetVal)
{
GUI_MUST_HAVE_HWND;
aRetVal = new IndexEnumerator(this, aVarCount.value_or(0)
, static_cast<IndexEnumerator::Callback>(&GuiType::GetEnumItem));
return OK;
}
ObjectMemberMd GuiType::sMembers[] =
{
md_member(GuiType, __Enum, CALL, (In_Opt, Int32, VarCount), (Ret, Object, RetVal)),
md_member(GuiType, __New, CALL, (In_Opt, String, Options), (In_Opt, String, Title), (In_Opt, Object, EventObj)),
md_member(GuiType, Add, CALL, (In, String, ControlType), (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddActiveX, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddButton, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddCheckBox, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddComboBox, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddCustom, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddDateTime, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddDDL, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddDropDownList, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddEdit, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddGroupBox, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddHotkey, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddLink, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddListBox, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddListView, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddMonthCal, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddPic, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddPicture, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddProgress, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddRadio, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddSlider, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddStatusBar, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddTab, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddTab2, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddTab3, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddText, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddTreeView, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, AddUpDown, CALL, (In_Opt, String, Options), (In_Opt, Variant, Content), (Ret, Object, RetVal)),
md_member(GuiType, Destroy, CALL, md_arg_none),
md_member(GuiType, Flash, CALL, (In_Opt, Bool32, Blink)),
md_member(GuiType, GetClientPos, CALL, (Out_Opt, Int32, X), (Out_Opt, Int32, Y), (Out_Opt, Int32, Width), (Out_Opt, Int32, Height)),
md_member(GuiType, GetPos, CALL, (Out_Opt, Int32, X), (Out_Opt, Int32, Y), (Out_Opt, Int32, Width), (Out_Opt, Int32, Height)),
md_member(GuiType, Hide, CALL, md_arg_none),
md_member(GuiType, Maximize, CALL, md_arg_none),
md_member(GuiType, Minimize, CALL, md_arg_none),
md_member(GuiType, Move, CALL, (In_Opt, Int32, X), (In_Opt, Int32, Y), (In_Opt, Int32, Width), (In_Opt, Int32, Height)),
md_member(GuiType, OnEvent, CALL, (In, String, EventName), (In, Variant, Callback), (In_Opt, Int32, AddRemove)),
md_member(GuiType, Opt, CALL, (In, String, Options)),
md_member(GuiType, Restore, CALL, md_arg_none),
md_member(GuiType, SetFont, CALL, (In_Opt, String, Options), (In_Opt, String, FontName)),
md_member(GuiType, Show, CALL, (In_Opt, String, Options)),
md_member(GuiType, Submit, CALL, (In_Opt, Bool32, Hide), (Ret, Object, RetVal)),
md_member (GuiType, __Item, GET, (In, Variant, Index), (Ret, Object, RetVal)),
md_property_get (GuiType, Hwnd, UInt32),
md_property (GuiType, Title, String),
md_property (GuiType, Name, String),
md_property_get (GuiType, FocusedCtrl, Object),
md_property (GuiType, BackColor, Variant),
md_property (GuiType, MarginX, Int32),
md_property (GuiType, MarginY, Int32),
md_property (GuiType, MenuBar, Variant)
};
int GuiType::sMemberCount = _countof(sMembers);
FResult GuiType::Add(StrArg aCtrlType, optl<StrArg> aOptions, ExprTokenType *aContent, IObject *&aRetVal)
{
auto ctrl_type = GuiControlType::ConvertTypeName(aCtrlType);
if (ctrl_type == GUI_CONTROL_INVALID)
return FValueError(_T("Invalid control type."), aCtrlType);
auto fr = AddControl(aOptions, aContent, aRetVal, ctrl_type);
return fr;
}
FResult GuiType::AddControl(optl<StrArg> aOptions, ExprTokenType *aContent, IObject *&aRetVal, GuiControls ctrl_type)
{
auto options = aOptions.value_or_empty();
TCHAR text_buf[MAX_NUMBER_SIZE], *text = _T("");
Array *text_obj = nullptr;
if (aContent)
{
auto temp_obj = TokenToObject(*aContent);
if (GuiControlType::TypeHasAttrib(ctrl_type, GuiControlType::TYPE_HAS_ITEMS)) // Need an Array.
{
if ( !(text_obj = dynamic_cast<Array*>(temp_obj)) )
return FTypeError(_T("Array"), *aContent);
}
else // Need a String.
{
if (temp_obj)
return FTypeError(_T("String"), *aContent);
text = TokenToString(*aContent, text_buf);
}
}
GUI_MUST_HAVE_HWND;
GuiControlType* pcontrol;
ResultType result = AddControl(ctrl_type, options, text, pcontrol, text_obj);
if (pcontrol)
{
pcontrol->AddRef();
aRetVal = pcontrol;
}
return result ? OK : FR_FAIL;
}
FResult GuiType::__New(optl<StrArg> aOptions, optl<StrArg> aTitle, optl<IObject*> aEventObj)
{
if (mHwnd || mDisposed)
return FError(ERR_INVALID_USAGE);
bool set_last_found_window = false;
ToggleValueType own_dialogs = TOGGLE_INVALID;
if (!ParseOptions(aOptions.value_or_empty(), set_last_found_window, own_dialogs))
return FR_FAIL;
mControl = (GuiControlType **)malloc(GUI_CONTROL_BLOCK_SIZE * sizeof(GuiControlType*));
if (!mControl)
return FR_E_OUTOFMEM;
mControlCapacity = GUI_CONTROL_BLOCK_SIZE;
if (aEventObj.has_value())
{
// The caller specified an object to use as event sink.
mEventSink = aEventObj.value();
if (mEventSink != this) // Primarily for custom GUI classes: prevent a circular reference.
mEventSink->AddRef();
}
LPCTSTR title = aTitle.has_value() ? aTitle.value() : g_script.DefaultDialogTitle();
// Create the Gui, now that we're past all other failure points.
auto fr = Create(title);
if (fr != OK)
return fr;
if (set_last_found_window)
g->hWndLastUsed = mHwnd;
SetOwnDialogs(own_dialogs);
// Successful creation - add the Gui to the global list of Guis and return it
AddGuiToList(this);
return OK;
}
FResult GuiType::get_Hwnd(UINT &aRetVal)
{
// Throwing if Destroy() has been called provides a clearer error message than
// returning 0 when the Gui is being passed to a WinTitle parameter, and ensures
// that it won't cause silent failures, such as with DllCall.
GUI_MUST_HAVE_HWND;
aRetVal = (UINT)(UINT_PTR)mHwnd;
return OK;
}
FResult GuiType::get_Title(StrRet &aRetVal)
{
GUI_MUST_HAVE_HWND;
int length = GetWindowTextLength(mHwnd);
auto buf = aRetVal.Alloc(length);
if (!buf)
return FR_E_OUTOFMEM;
GetWindowText(mHwnd, buf, length+1);
return OK;
}
FResult GuiType::set_Title(StrArg aValue)
{
GUI_MUST_HAVE_HWND;
SetWindowText(mHwnd, aValue);
return OK;
}
FResult GuiType::get___Item(ExprTokenType &aIndex, IObject *&aRetVal)
{
GUI_MUST_HAVE_HWND; // Seems clearer than relying on the error below.
GuiControlType* ctrl = NULL;
if (TokenIsPureNumeric(aIndex) == SYM_INTEGER)
{
HWND hWnd = (HWND)(UINT_PTR)TokenToInt64(aIndex);
ctrl = FindControl(hWnd);
}
else
{
TCHAR buf[MAX_NUMBER_SIZE];
GuiIndexType u = FindControl(TokenToString(aIndex, buf));
if (u < mControlCount)
ctrl = mControl[u];
}
if (!ctrl)
return FError(_T("The specified control does not exist."), nullptr, ErrorPrototype::UnsetItem);
ctrl->AddRef();
aRetVal = ctrl;
return OK;
}
FResult GuiType::get_FocusedCtrl(IObject *&aRetVal)
{
GUI_MUST_HAVE_HWND;
HWND hwnd = GetFocus();
aRetVal = hwnd ? FindControl(hwnd) : nullptr;
if (aRetVal)
aRetVal->AddRef();
return OK;
}
FResult GuiType::get_Margin(int &aRetVal, int &aMargin)
{
if (aMargin == COORD_UNSPECIFIED)
{
// Avoid returning an arbitrary value such as COORD_UNSPECIFIED or -1 (Unscale(COORD_UNSPECIFIED)).
// Instead, set the default margins if MarginX or MarginY is queried without having first been set,
// prior to adding any controls.
SetDefaultMargins();
}
aRetVal = Unscale(aMargin);
return OK;
}
FResult GuiType::get_BackColor(ResultToken &aResultToken)
{
GUI_MUST_HAVE_HWND;
if (mBackgroundColorWin == CLR_DEFAULT)
return OK; // "" is the default return value.
_sntprintf(_f_retval_buf, _f_retval_buf_size, _T("%06X"), bgr_to_rgb(mBackgroundColorWin));
aResultToken.SetValue(_f_retval_buf);
return OK;
}
FResult GuiType::set_BackColor(ExprTokenType &aValue)
{
GUI_MUST_HAVE_HWND;
COLORREF new_color;
if (!ColorToBGR(aValue, new_color))
return FValueError(ERR_INVALID_VALUE);
// AssignColor() takes care of deleting old brush, etc.
AssignColor(new_color, mBackgroundColorWin, mBackgroundBrushWin);
if (IsWindowVisible(mHwnd))
// Force the window to repaint so that colors take effect immediately.
// UpdateWindow() isn't enough sometimes/always, so do something more aggressive:
InvalidateRect(mHwnd, NULL, TRUE);
return OK;
}
FResult GuiType::get_Name(StrRet &aRetVal)
{
aRetVal.SetTemp(mName); // Can safely be nullptr.
return OK;
}
FResult GuiType::set_Name(StrArg aName)
{
LPTSTR new_name = nullptr;
if (*aName)
{
new_name = _tcsdup(aName);
if (!new_name)
return FR_E_OUTOFMEM;
}
free(mName);
mName = new_name;
return OK;
}
void GuiType::MethodGetPos(int *aX, int *aY, int *aWidth, int *aHeight, RECT &aPos, HWND aOrigin)
{
MapWindowPoints(aOrigin, mOwner, (LPPOINT)&aPos, 2);
if (aX) *aX = Unscale(aPos.left);
if (aY) *aY = Unscale(aPos.top);
if (aWidth) *aWidth = Unscale(aPos.right - aPos.left);
if (aHeight) *aHeight = Unscale(aPos.bottom - aPos.top);
}
FResult GuiType::GetPos(int *aX, int *aY, int *aWidth, int *aHeight)
{
GUI_MUST_HAVE_HWND;
RECT rect;
if (GetWindowRect(mHwnd, &rect))
MethodGetPos(aX, aY, aWidth, aHeight, rect, NULL);
return OK;
}
FResult GuiType::GetClientPos(int *aX, int *aY, int *aWidth, int *aHeight)
{
GUI_MUST_HAVE_HWND;
RECT rect;
if (GetClientRect(mHwnd, &rect))
MethodGetPos(aX, aY, aWidth, aHeight, rect, mHwnd);
return OK;
}
FResult GuiType::Move(optl<int> aX, optl<int> aY, optl<int> aWidth, optl<int> aHeight)
{
GUI_MUST_HAVE_HWND;
// Like WinMove, this doesn't check if the window is minimized or do any autosizing.
// Unlike WinMove, this does DPI scaling.
RECT rect;
GetWindowRect(mHwnd, &rect);
rect.right -= rect.left; // Convert to width.
rect.bottom -= rect.top; // Convert to height.
if (HWND parent = GetParent(mHwnd)) // Allow for +Parent.
ScreenToClient(parent, (LPPOINT)&rect); // Must do this before the loop, as coord[] already contains client coords.
if (aX.has_value()) rect.left = Scale(aX.value());
if (aY.has_value()) rect.top = Scale(aY.value());
if (aWidth.has_value()) rect.right = Scale(aWidth.value());
if (aHeight.has_value()) rect.bottom = Scale(aHeight.value());
MoveWindow(mHwnd, rect.left, rect.top, rect.right, rect.bottom, TRUE);
return OK;
}
bif_impl void GuiFromHwnd(UINT aHwnd, optl<BOOL> aRecurse, IObject *&aGui)
{
if (aGui = aRecurse.value_or(FALSE) ? GuiType::FindGuiParent((HWND)(UINT_PTR)aHwnd) : GuiType::FindGui((HWND)(UINT_PTR)aHwnd))
aGui->AddRef();
}
bif_impl void GuiCtrlFromHwnd(UINT aHwnd, IObject *&aGuiCtrl)
{
if (GuiType* gui = GuiType::FindGuiParent((HWND)(UINT_PTR)aHwnd))
if (aGuiCtrl = gui->FindControl((HWND)(UINT_PTR)aHwnd))
aGuiCtrl->AddRef();
}
ObjectMemberMd GuiControlType::sMembers[] =
{
md_member(GuiControlType, Focus, CALL, md_arg_none),
md_member(GuiControlType, GetPos, CALL, (Out_Opt, Int32, X), (Out_Opt, Int32, Y), (Out_Opt, Int32, Width), (Out_Opt, Int32, Height)),
md_member(GuiControlType, Move, CALL, (In_Opt, Int32, X), (In_Opt, Int32, Y), (In_Opt, Int32, Width), (In_Opt, Int32, Height)),
md_member(GuiControlType, OnCommand, CALL, (In, Int32, NotifyCode), (In, Variant, Callback), (In_Opt, Int32, AddRemove)),
md_member(GuiControlType, OnEvent, CALL, (In, String, EventName), (In, Variant, Callback), (In_Opt, Int32, AddRemove)),
md_member(GuiControlType, OnNotify, CALL, (In, Int32, NotifyCode), (In, Variant, Callback), (In_Opt, Int32, AddRemove)),
md_member(GuiControlType, Opt, CALL, (In, String, Options)),
md_member(GuiControlType, Redraw, CALL, md_arg_none),
md_member(GuiControlType, SetFont, CALL, (In_Opt, String, Options), (In_Opt, String, FontName)),
md_property_get (GuiControlType, ClassNN, String),
md_property (GuiControlType, Enabled, Bool32),
md_property_get (GuiControlType, Focused, Bool32),
md_property_get (GuiControlType, Gui, Object),
md_property_get (GuiControlType, Hwnd, UInt32),
md_property (GuiControlType, Name, String),
md_property (GuiControlType, Text, Variant),
md_property_get (GuiControlType, Type, String),
md_property (GuiControlType, Value, Variant),
md_property (GuiControlType, Visible, Bool32)
};
ObjectMemberMd GuiControlType::sMembersList[] =
{
md_member_x(GuiControlType, Add, List_Add, CALL, (In, Variant, Value)),
md_member_x(GuiControlType, Choose, List_Choose, CALL, (In, Variant, Value)),
md_member_x(GuiControlType, Delete, List_Delete, CALL, (In_Opt, Int32, Index))
};
ObjectMemberMd GuiControlType::sMembersTab[] =
{
md_member_x(GuiControlType, UseTab, Tab_UseTab, CALL, (In_Opt, Variant, Tab), (In_Opt, Bool32, ExactMatch))
};
ObjectMemberMd GuiControlType::sMembersDate[] =
{
md_member_x(GuiControlType, SetFormat, DT_SetFormat, CALL, (In_Opt, String, Format))
};
#define FUN1(name, minp, maxp, bif) Object_Member(name, bif, 0, IT_CALL, minp, maxp)
#define FUNn(name, minp, maxp, bif, cat) Object_Member(name, bif, FID_##cat##_##name, IT_CALL, minp, maxp)
ObjectMemberMd GuiControlType::sMembersLV[] =
{
md_member_x(GuiControlType, GetNext, LV_GetNext, CALL, (In_Opt, Int32, StartIndex), (In_Opt, String, RowType), (Ret, Int32, RetVal)),
md_member_x(GuiControlType, GetCount, LV_GetCount, CALL, (In_Opt, String, Mode), (Ret, Int32, RetVal)),
md_member_x(GuiControlType, GetText, LV_GetText, CALL, (In, Int32, Row), (In_Opt, Int32, Column), (Ret, String, RetVal)),
md_member_x(GuiControlType, Add, LV_Add, CALL, (In_Opt, String, Options), (In, Params, Columns), (Ret, Int32, RetVal)),
md_member_x(GuiControlType, Insert, LV_Insert, CALL, (In, Int32, Row), (In_Opt, String, Options), (In, Params, Columns), (Ret, Int32, RetVal)),
md_member_x(GuiControlType, Modify, LV_Modify, CALL, (In, Int32, Row), (In_Opt, String, Options), (In, Params, Columns)),
md_member_x(GuiControlType, Delete, LV_Delete, CALL, (In_Opt, Int32, Row)),
md_member_x(GuiControlType, InsertCol, LV_InsertCol, CALL, (In_Opt, Int32, Column), (In_Opt, String, Options), (In_Opt, String, Title), (Ret, Int32, RetVal)),
md_member_x(GuiControlType, ModifyCol, LV_ModifyCol, CALL, (In_Opt, Int32, Column), (In_Opt, String, Options), (In_Opt, String, Title)),
md_member_x(GuiControlType, DeleteCol, LV_DeleteCol, CALL, (In, Int32, Column)),
md_member_x(GuiControlType, SetImageList, LV_SetImageList, CALL, (In, UIntPtr, ImageListID), (In_Opt, Int32, IconType), (Ret, UIntPtr, RetVal))
};
ObjectMemberMd GuiControlType::sMembersTV[] =
{
md_member_x(GuiControlType, Add, TV_Add, CALL, (In, String, Name), (In_Opt, UIntPtr, ParentItemID), (In_Opt, String, Options), (Ret, UIntPtr, RetVal)),
md_member_x(GuiControlType, Delete, TV_Delete, CALL, (In_Opt, UIntPtr, ItemID)),
md_member_x(GuiControlType, Get, TV_Get, CALL, (In, UIntPtr, ItemID), (In, String, Attribute), (Ret, UIntPtr, RetVal)),
md_member_x(GuiControlType, GetChild, TV_GetChild, CALL, (In, UIntPtr, ItemID), (Ret, UIntPtr, RetVal)),
md_member_x(GuiControlType, GetCount, TV_GetCount, CALL, (Ret, UInt32, RetVal)),
md_member_x(GuiControlType, GetNext, TV_GetNext, CALL, (In_Opt, UIntPtr, ItemID), (In_Opt, String, ItemType), (Ret, UIntPtr, RetVal)),
md_member_x(GuiControlType, GetParent, TV_GetParent, CALL, (In, UIntPtr, ItemID), (Ret, UIntPtr, RetVal)),
md_member_x(GuiControlType, GetPrev, TV_GetPrev, CALL, (In, UIntPtr, ItemID), (Ret, UIntPtr, RetVal)),
md_member_x(GuiControlType, GetSelection, TV_GetSelection, CALL, (Ret, UIntPtr, RetVal)),
md_member_x(GuiControlType, GetText, TV_GetText, CALL, (In, UIntPtr, ItemID), (Ret, String, RetVal)),
md_member_x(GuiControlType, Modify, TV_Modify, CALL, (In, UIntPtr, ItemID), (In_Opt, String, Options), (In_Opt, String, NewName), (Ret, UIntPtr, RetVal)),
md_member_x(GuiControlType, SetImageList, TV_SetImageList, CALL, (In, UIntPtr, ImageListID), (In_Opt, Int32, IconType), (Ret, UIntPtr, RetVal))
};
ObjectMemberMd GuiControlType::sMembersSB[] =
{
md_member_x(GuiControlType, SetIcon, SB_SetIcon, CALL, (In, String, Filename), (In_Opt, Int32, IconNumber), (In_Opt, UInt32, PartNumber), (Ret, UIntPtr, RetVal)),
md_member_x(GuiControlType, SetParts, SB_SetParts, CALL, (In, Params, Filename), (Ret, UInt32, RetVal)),
md_member_x(GuiControlType, SetText, SB_SetText, CALL, (In, String, NewText), (In_Opt, UInt32, PartNumber), (In_Opt, UInt32, Style))
};
#undef FUN1
#undef FUNn
Object *GuiControlType::sPrototype;
Object *GuiControlType::sPrototypeList;
Object *GuiControlType::sPrototypes[GUI_CONTROL_TYPE_COUNT];
void GuiControlType::DefineControlClasses()
{
auto gui_class = (Object *)g_script.FindGlobalVar(_T("Gui"), 3)->Object();
sPrototype = CreatePrototype(_T("Gui.Control"), Object::sPrototype, sMembers, _countof(sMembers));
sPrototypeList = CreatePrototype(_T("Gui.List"), sPrototype, sMembersList, _countof(sMembersList));
auto ctrl_class = CreateClass(sPrototype);
auto list_class = CreateClass(sPrototypeList);
ctrl_class->SetBase(Object::sClass);
list_class->SetBase(ctrl_class);
gui_class->DefineClass(_T("Control"), ctrl_class);
gui_class->DefineClass(_T("List"), list_class);
for (int i = GUI_CONTROL_INVALID + 1; i < GUI_CONTROL_TYPE_COUNT; ++i)
{
if (i == GUI_CONTROL_TAB2 || i == GUI_CONTROL_TAB3)
continue;
// Determine base prototype and control-type-specific members.
Object *base_proto = sPrototype, *base_class = ctrl_class;
ObjectMemberListType more_items;
int how_many = 0;
switch (i)
{
case GUI_CONTROL_TAB: more_items = sMembersTab; how_many = _countof(sMembersTab); // Fall through:
case GUI_CONTROL_DROPDOWNLIST:
case GUI_CONTROL_COMBOBOX:
case GUI_CONTROL_LISTBOX: base_proto = sPrototypeList; base_class = list_class; break;
case GUI_CONTROL_DATETIME: more_items = sMembersDate; how_many = _countof(sMembersDate); break;
case GUI_CONTROL_LISTVIEW: more_items = sMembersLV; how_many = _countof(sMembersLV); break;
case GUI_CONTROL_TREEVIEW: more_items = sMembersTV; how_many = _countof(sMembersTV); break;
case GUI_CONTROL_STATUSBAR: more_items = sMembersSB; how_many = _countof(sMembersSB); break;
}
TCHAR buf[32];
_sntprintf(buf, 32, _T("Gui.%s"), sTypeNames[i]);
sPrototypes[i] = CreatePrototype(buf, base_proto, more_items, how_many);
auto cls = CreateClass(sPrototypes[i]);
cls->SetBase(base_class);
gui_class->DefineClass(sTypeNames[i], cls);
}
}
Object *GuiControlType::GetPrototype(GuiControls aType)
{
ASSERT(aType <= _countof(sPrototypes));
if (aType == GUI_CONTROL_TAB2 || aType == GUI_CONTROL_TAB3)
aType = GUI_CONTROL_TAB; // Just make them all Gui.Tab.
return sPrototypes[aType];
}
FResult GuiControlType::Focus()
{
CTRL_THROW_IF_DESTROYED;
// MSDN: "WM_NEXTDLGCTL updates the default pushbutton border, sets the default control identifier,
// and automatically selects the text of an edit control (if the target window is an edit control)."
// (It also sets the focus.)
SendMessage(gui->mHwnd, WM_NEXTDLGCTL, (WPARAM)hwnd, TRUE);
return OK;
}
FResult GuiControlType::GetPos(int *aX, int *aY, int *aWidth, int *aHeight)
{
CTRL_THROW_IF_DESTROYED;
RECT rect;
GetWindowRect(hwnd, &rect);
MapWindowPoints(NULL, gui->mHwnd, (LPPOINT)&rect, 2);
if (aX) *aX = gui->Unscale(rect.left);
if (aY) *aY = gui->Unscale(rect.top);
if (aWidth) *aWidth = gui->Unscale(rect.right - rect.left);
if (aHeight) *aHeight = gui->Unscale(rect.bottom - rect.top);
return OK;
}
FResult GuiControlType::Move(optl<int> aX, optl<int> aY, optl<int> aWidth, optl<int> aHeight)
{
CTRL_THROW_IF_DESTROYED;
return gui->ControlMove(*this, aX.value_or(COORD_UNSPECIFIED), aY.value_or(COORD_UNSPECIFIED), aWidth.value_or(COORD_UNSPECIFIED), aHeight.value_or(COORD_UNSPECIFIED));
}
FResult GuiControlType::OnCommand(int aNotifyCode, ExprTokenType &aCallback, optl<int> aAddRemove)
{
CTRL_THROW_IF_DESTROYED;
return gui->OnEvent(this, aNotifyCode, GUI_EVENTKIND_COMMAND, aCallback, aAddRemove);
}
FResult GuiControlType::OnEvent(StrArg aEventName, ExprTokenType &aCallback, optl<int> aAddRemove)
{
CTRL_THROW_IF_DESTROYED;
UINT event_code = GuiType::ConvertEvent(aEventName);
if (!event_code || !SupportsEvent(event_code))
return FR_E_ARG(0);
return gui->OnEvent(this, event_code, GUI_EVENTKIND_EVENT, aCallback, aAddRemove);
}
FResult GuiControlType::OnNotify(int aNotifyCode, ExprTokenType &aCallback, optl<int> aAddRemove)
{
CTRL_THROW_IF_DESTROYED;
return gui->OnEvent(this, aNotifyCode, GUI_EVENTKIND_NOTIFY, aCallback, aAddRemove);
}
FResult GuiControlType::Opt(StrArg aOptions)
{
CTRL_THROW_IF_DESTROYED;
GuiControlOptionsType go; // Its contents are not currently used here, but they might be in the future.
gui->ControlInitOptions(go, *this);
if (!gui->ControlParseOptions(aOptions, go, *this, GUI_HWND_TO_INDEX(hwnd)))
return FR_FAIL;
return OK;
}
FResult GuiControlType::Redraw()
{
CTRL_THROW_IF_DESTROYED;
RECT rect;
GetWindowRect(hwnd, &rect); // Limit it to only that part of the client area that the control occupies.
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;
}
FResult GuiControlType::SetFont(optl<StrArg> aOptions, optl<StrArg> aFontName)
{
CTRL_THROW_IF_DESTROYED;
return gui->ControlSetFont(*this, aOptions.value_or_empty(), aFontName.value_or_empty()) ? OK : FR_FAIL;
}
FResult GuiControlType::get_ClassNN(StrRet &aRetVal)
{
CTRL_THROW_IF_DESTROYED;
TCHAR class_nn[WINDOW_CLASS_NN_SIZE];
auto fr = ControlGetClassNN(gui->mHwnd, hwnd, class_nn, _countof(class_nn));
return fr != OK ? fr : aRetVal.Copy(class_nn) ? OK : FR_E_OUTOFMEM;
}
FResult GuiControlType::get_Enabled(BOOL &aRetVal)
{
CTRL_THROW_IF_DESTROYED;
aRetVal = IsWindowEnabled(hwnd) ? 1 : 0;
return OK;
}
FResult GuiControlType::set_Enabled(BOOL aValue)
{
CTRL_THROW_IF_DESTROYED;
gui->ControlSetEnabled(*this, aValue);
return OK;
}
FResult GuiControlType::get_Focused(BOOL &aRetVal)
{
CTRL_THROW_IF_DESTROYED;
HWND focus = GetFocus();
// For controls like ComboBox and ActiveX that may have a focused child window,
// also co nsider the control focused if any child of the control is focused.
aRetVal = (focus == hwnd || IsChild(hwnd, focus));
return OK;
}
FResult GuiControlType::get_Gui(IObject *&aRetVal)
{
CTRL_THROW_IF_DESTROYED;
gui->AddRef();
aRetVal = gui;
return OK;
}
FResult GuiControlType::get_Hwnd(UINT &aRetVal)
{
CTRL_THROW_IF_DESTROYED;
aRetVal = (UINT)(UINT_PTR)hwnd;
return OK;
}
FResult GuiControlType::get_Name(StrRet &aRetVal)
{
aRetVal.SetTemp(name); // Can safely be nullptr.
return OK;
}
FResult GuiControlType::set_Name(StrArg aValue)
{
CTRL_THROW_IF_DESTROYED;
return gui->ControlSetName(*this, aValue);
}
FResult GuiControlType::get_Text(ResultToken &aResultToken)
{
CTRL_THROW_IF_DESTROYED;
gui->ControlGetContents(aResultToken, *this, GuiType::Text_Mode);
return aResultToken.Exited() ? FR_FAIL : OK;
}
FResult GuiControlType::set_Text(ExprTokenType &aValue)
{
CTRL_THROW_IF_DESTROYED;
return gui->ControlSetContents(*this, aValue, true);
}
FResult GuiControlType::get_Type(StrRet &aRetVal)
{
aRetVal.SetStatic(GetTypeName());
return OK;
}
FResult GuiControlType::get_Value(ResultToken &aResultToken)
{
CTRL_THROW_IF_DESTROYED;
gui->ControlGetContents(aResultToken, *this, GuiType::Value_Mode);
return aResultToken.Exited() ? FR_FAIL : OK;
}
FResult GuiControlType::set_Value(ExprTokenType &aValue)
{
CTRL_THROW_IF_DESTROYED;
return gui->ControlSetContents(*this, aValue, false);
}
FResult GuiControlType::get_Visible(BOOL &aRetVal)
{
CTRL_THROW_IF_DESTROYED;
// Return the requested visibility state, not the actual state, which would
// be affected by the parent window's visiblity and currently selected tab.
aRetVal = (attrib & GUI_CONTROL_ATTRIB_EXPLICITLY_HIDDEN) ? 0 : 1;
return OK;
}
FResult GuiControlType::set_Visible(BOOL aValue)
{
CTRL_THROW_IF_DESTROYED;
gui->ControlSetVisible(*this, aValue);
return OK;
}
FResult GuiControlType::Tab_UseTab(ExprTokenType *aTab, optl<BOOL> aExact)
{
CTRL_THROW_IF_DESTROYED;
BOOL whole_match = aExact.value_or(FALSE);
int index; // int vs TabControlIndexType for detection of negative/out-of-range values.
if (!aTab)
index = -1;
else if (TokenIsPureNumeric(*aTab) == SYM_INTEGER)
index = (int)TokenToInt64(*aTab) - 1; // Convert to zero-based and let 0 indicate "no tab" (-1).
else
{
TCHAR float_buf[MAX_NUMBER_SIZE];
auto tab_name = TokenToString(*aTab, float_buf);
index = gui->FindTabIndexByName(*this, tab_name, whole_match);
if (index == -1 && *tab_name) // Invalid tab name (but "" is a valid way to specify "no tab").
index = -2; // Flag it as invalid.
}
// Validate parameters before making any changes to the Gui's state:
if (index < -1 || index >= MAX_TABS_PER_CONTROL)
return FR_E_ARG(0);
TabIndexType prev_tab_index = gui->mCurrentTabIndex;
TabControlIndexType prev_tab_control_index = gui->mCurrentTabControlIndex;
if (index == -1)
gui->mCurrentTabControlIndex = MAX_TAB_CONTROLS; // i.e. "no tab"
else
{
gui->mCurrentTabControlIndex = tab_index;
if (gui->mCurrentTabIndex != index)
{
gui->mCurrentTabIndex = index;
// Changing to a different tab should start a new radio group:
gui->mInRadioGroup = false;
}
}
if (gui->mCurrentTabControlIndex != prev_tab_control_index)
{
if (GuiControlType *tab_control = gui->FindTabControl(prev_tab_control_index))
{
// Autosize the previous tab control. Has no effect if it is not a Tab3 control or has
// already been autosized. Doing it at this point allows the script to set different
// margins for inside and outside the tab control, and is simpler than the alternative:
// waiting until the next external control is added. The main drawback is that the
// script is unable to alternate between tab controls and still utilize autosizing.
// On the flip side, scripts can use this to their advantage -- to add controls which
// should not affect the tab control's size.
gui->AutoSizeTabControl(*tab_control);
}
// 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;
}
return OK;
}
FResult GuiControlType::List_Add(ExprTokenType &aItems)
{
CTRL_THROW_IF_DESTROYED;
auto obj = TokenToArray(aItems);
if (!obj)
return FParamError(0, &aItems, _T("Array"));
gui->ControlAddItems(*this, obj);
if (type == GUI_CONTROL_TAB)
{
// Adding tabs may change the space usable by the tab dialog.
gui->UpdateTabDialog(hwnd);
// Appears to be necessary to resolve a redraw issue, at least for Tab3 on Windows 10.
InvalidateRect(gui->mHwnd, NULL, TRUE);
}
return OK;
}
FResult GuiControlType::List_Delete(optl<int> aIndex)
{
CTRL_THROW_IF_DESTROYED;
UINT msg_one;
UINT msg_all;
switch (type)
{
case GUI_CONTROL_TAB: msg_one = TCM_DELETEITEM; msg_all = TCM_DELETEALLITEMS; break;
case GUI_CONTROL_LISTBOX: msg_one = LB_DELETESTRING; msg_all = LB_RESETCONTENT; break;
default: msg_one = CB_DELETESTRING; msg_all = CB_RESETCONTENT; break; // ComboBox/DDL.
}
if (aIndex.has_value()) // Delete item with given index.
{
int index = aIndex.value() - 1;
if (index < 0)
return FR_E_ARG(0);
SendMessage(hwnd, msg_one, (WPARAM)index, 0);
}
else // Delete all items.
{
SendMessage(hwnd, msg_all, 0, 0);
}
if (type == GUI_CONTROL_TAB)
{
// Removing tabs may change the space usable by the tab dialog.
gui->UpdateTabDialog(hwnd);
}
return OK;
}