This repository has been archived by the owner on Aug 20, 2024. It is now read-only.
forked from adamdruppe/arsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minigui.d
7708 lines (6480 loc) · 195 KB
/
minigui.d
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
// http://msdn.microsoft.com/en-us/library/windows/desktop/bb775498%28v=vs.85%29.aspx
// FIXME: slider widget.
// FIXME: number widget
// osx style menu search.
// would be cool for a scroll bar to have marking capabilities
// kinda like vim's marks just on clicks etc and visual representation
// generically. may be cool to add an up arrow to the bottom too
//
// leave a shadow of where you last were for going back easily
// So a window needs to have a selection, and that can be represented by a type. This is manipulated by various
// functions like cut, copy, paste. Widgets can have a selection and that would assert teh selection ownership for
// the window.
// so what about context menus?
// https://docs.microsoft.com/en-us/windows/desktop/Controls/about-custom-draw
// FIXME: make the scroll thing go to bottom when the content changes.
// add a knob slider view... you click and go up and down so basically same as a vertical slider, just presented as a round image
// FIXME: the scroll area MUST be fixed to use the proper apis under the hood.
// FIXME: add a command search thingy built in and implement tip.
// FIXME: omg omg what if menu functions have arguments and it can pop up a gui or command line script them?!
// On Windows:
// FIXME: various labels look broken in high contrast mode
// FIXME: changing themes while the program is upen doesn't trigger a redraw
// add note about manifest to documentation. also icons.
// a pager control is just a horizontal scroll area just with arrows on the sides instead of a scroll bar
// FIXME: clear the corner of scrollbars if they pop up
// minigui needs to have a stdout redirection for gui mode on windows writeln
// I kinda wanna do state reacting. sort of. idk tho
// need a viewer widget that works like a web page - arrows scroll down consistently
// I want a nanovega widget, and a svg widget with some kind of event handlers attached to the inside.
// FIXME: the menus should be a bit more discoverable, at least a single click to open the others instead of two.
// and help info about menu items.
// and search in menus?
// FIXME: a scroll area event signaling when a thing comes into view might be good
// FIXME: arrow key navigation and accelerators in dialog boxes will be a must
// FIXME: unify Windows style line endings
/*
TODO:
pie menu
class Form with submit behavior -- see AutomaticDialog
disabled widgets and menu items
TrackBar controls
event cleanup
tooltips.
api improvements
margins are kinda broken, they don't collapse like they should. at least.
a table form btw would be a horizontal layout of vertical layouts holding each column
that would give the same width things
*/
/*
1(15:19:48) NotSpooky: Menus, text entry, label, notebook, box, frame, file dialogs and layout (this one is very useful because I can draw lines between its child widgets
*/
/++
minigui is a smallish GUI widget library, aiming to be on par with at least
HTML4 forms and a few other expected gui components. It uses native controls
on Windows and does its own thing on Linux (Mac is not currently supported but
may be later, and should use native controls) to keep size down. The Linux
appearance is similar to Windows 95 and avoids using images to maintain network
efficiency on remote X connections, though you can customize that.
minigui's only required dependencies are [arsd.simpledisplay] and [arsd.color].
Its #1 goal is to be useful without being large and complicated like GTK and Qt.
It isn't hugely concerned with appearance - on Windows, it just uses the native
controls and native theme, and on Linux, it keeps it simple and I may change that
at any time.
I love Qt, if you want something full featured, use it! But if you want something
you can just drop into a small project and expect the basics to work without outside
dependencies, hopefully minigui will work for you.
The event model is similar to what you use in the browser with Javascript and the
layout engine tries to automatically fit things in, similar to a css flexbox.
FOR BEST RESULTS: be sure to link with the appropriate subsystem command
`-L/SUBSYSTEM:WINDOWS:5.0`, for example, because otherwise you'll get a
console and other visual bugs.
HTML_To_Classes:
`<input type="text">` = [LineEdit]
`<textarea>` = [TextEdit]
`<select>` = [DropDownSelection]
`<input type="checkbox">` = [Checkbox]
`<input type="radio">` = [Radiobox]
`<button>` = [Button]
Stretchiness:
The default is 4. You can use larger numbers for things that should
consume a lot of space, and lower numbers for ones that are better at
smaller sizes.
Overlapped_input:
COMING SOON:
minigui will include a little bit of I/O functionality that just works
with the event loop. If you want to get fancy, I suggest spinning up
another thread and posting events back and forth.
$(H2 Add ons)
$(H2 XML definitions)
If you use [arsd.minigui_xml], you can create widget trees from XML at runtime.
$(H3 Scriptability)
minigui is compatible with [arsd.script]. If you see `@scriptable` on a method
in this documentation, it means you can call it from the script language.
Tip: to allow easy creation of widget trees from script, import [arsd.minigui_xml]
and make [arsd.minigui_xml.makeWidgetFromString] available to your script:
---
import arsd.minigui_xml;
import arsd.script;
var globals = var.emptyObject;
globals.makeWidgetFromString = &makeWidgetFromString;
// this now works
interpret(`var window = makeWidgetFromString("<MainWindow />");`, globals);
---
More to come.
+/
module arsd.minigui;
public import arsd.simpledisplay;
private alias Rectangle = arsd.color.Rectangle; // I specifically want this in here, not the win32 GDI Rectangle()
version(Windows) {
import core.sys.windows.winnls;
import core.sys.windows.windef;
import core.sys.windows.basetyps;
import core.sys.windows.winbase;
import core.sys.windows.winuser;
import core.sys.windows.wingdi;
static import gdi = core.sys.windows.wingdi;
}
// this is a hack to call the original window procedure on native win32 widgets if our event listener thing prevents default.
private bool lastDefaultPrevented;
/// Methods marked with this are available from scripts if added to the [arsd.script] engine.
alias scriptable = arsd_jsvar_compatible;
version(Windows) {
// use native widgets when available unless specifically asked otherwise
version(custom_widgets) {
enum bool UsingCustomWidgets = true;
enum bool UsingWin32Widgets = false;
} else {
version = win32_widgets;
enum bool UsingCustomWidgets = false;
enum bool UsingWin32Widgets = true;
}
// and native theming when needed
//version = win32_theming;
} else {
enum bool UsingCustomWidgets = true;
enum bool UsingWin32Widgets = false;
version=custom_widgets;
}
/*
The main goals of minigui.d are to:
1) Provide basic widgets that just work in a lightweight lib.
I basically want things comparable to a plain HTML form,
plus the easy and obvious things you expect from Windows
apps like a menu.
2) Use native things when possible for best functionality with
least library weight.
3) Give building blocks to provide easy extension for your
custom widgets, or hooking into additional native widgets
I didn't wrap.
4) Provide interfaces for easy interaction between third
party minigui extensions. (event model, perhaps
signals/slots, drop-in ease of use bits.)
5) Zero non-system dependencies, including Phobos as much as
I reasonably can. It must only import arsd.color and
my simpledisplay.d. If you need more, it will have to be
an extension module.
6) An easy layout system that generally works.
A stretch goal is to make it easy to make gui forms with code,
some kind of resource file (xml?) and even a wysiwyg designer.
Another stretch goal is to make it easy to hook data into the gui,
including from reflection. So like auto-generate a form from a
function signature or struct definition, or show a list from an
array that automatically updates as the array is changed. Then,
your program focuses on the data more than the gui interaction.
STILL NEEDED:
* combo box. (this is diff than select because you can free-form edit too. more like a lineedit with autoselect)
* slider
* listbox
* spinner
* label?
* rich text
*/
alias HWND=void*;
///
abstract class ComboboxBase : Widget {
// if the user can enter arbitrary data, we want to use 2 == CBS_DROPDOWN
// or to always show the list, we want CBS_SIMPLE == 1
version(win32_widgets)
this(uint style, Widget parent = null) {
super(parent);
createWin32Window(this, "ComboBox"w, null, style);
}
else version(custom_widgets)
this(Widget parent = null) {
super(parent);
addEventListener("keydown", (Event event) {
if(event.key == Key.Up) {
if(selection > -1) { // -1 means select blank
selection--;
fireChangeEvent();
}
event.preventDefault();
}
if(event.key == Key.Down) {
if(selection + 1 < options.length) {
selection++;
fireChangeEvent();
}
event.preventDefault();
}
});
}
else static assert(false);
private string[] options;
private int selection = -1;
string[] getOptions() {
return options;
}
int getSelection() {
return selection;
}
string currentText() {
if (selection >= 0 && selection + 1 <= options.length)
return options[selection];
return "";
}
void currentText(string value) {
foreach (uint index, string option; this.options)
if (option == value) {
this.setSelection(index);
return;
}
// not found
this.addOption(value);
this.setSelection(this.options.length - 1);
}
void addOption(string s) {
options ~= s;
version(win32_widgets)
SendMessageW(hwnd, 323 /*CB_ADDSTRING*/, 0, cast(LPARAM) toWstringzInternal(s));
}
void setSelection(int idx) {
selection = idx;
version(win32_widgets)
SendMessageW(hwnd, 334 /*CB_SETCURSEL*/, idx, 0);
auto t = new Event(EventType.change, this);
t.intValue = selection;
t.stringValue = selection == -1 ? null : options[selection];
t.dispatch();
}
version(win32_widgets)
override void handleWmCommand(ushort cmd, ushort id) {
selection = cast(int) SendMessageW(hwnd, 327 /* CB_GETCURSEL */, 0, 0);
fireChangeEvent();
}
private void fireChangeEvent() {
if(selection >= options.length)
selection = -1;
auto event = new Event(EventType.change, this);
event.intValue = selection;
event.stringValue = selection == -1 ? null : options[selection];
event.dispatch();
}
version(win32_widgets) {
override int minHeight() { return Window.lineHeight + 6; }
override int maxHeight() { return Window.lineHeight + 6; }
} else {
override int minHeight() { return Window.lineHeight + 4; }
override int maxHeight() { return Window.lineHeight + 4; }
}
version(custom_widgets) {
SimpleWindow dropDown;
void popup() {
auto w = width;
// FIXME: suggestedDropdownHeight see below
auto h = cast(int) this.options.length * Window.lineHeight + 8;
auto coord = this.globalCoordinates();
auto dropDown = new SimpleWindow(
w, h,
null, OpenGlOptions.no, Resizability.fixedSize, WindowTypes.dropdownMenu, WindowFlags.dontAutoShow/*, window*/);
dropDown.move(coord.x, coord.y + this.height);
{
auto painter = dropDown.draw();
draw3dFrame(0, 0, w, h, painter, FrameStyle.risen);
auto p = Point(4, 4);
painter.outlineColor = Color.black;
foreach(option; options) {
painter.drawText(p, option);
p.y += Window.lineHeight;
}
}
dropDown.setEventHandlers(
(MouseEvent event) {
if(event.type == MouseEventType.buttonReleased) {
auto element = (event.y - 4) / Window.lineHeight;
if(element >= 0 && element <= options.length) {
selection = element;
fireChangeEvent();
}
dropDown.close();
}
}
);
dropDown.show();
dropDown.grabInput();
}
}
}
/++
A drop-down list where the user must select one of the
given options. Like `<select>` in HTML.
+/
class DropDownSelection : ComboboxBase {
this(Widget parent = null) {
version(win32_widgets)
super(3 /* CBS_DROPDOWNLIST */ | WS_VSCROLL, parent);
else version(custom_widgets) {
super(parent);
addEventListener("focus", () { this.redraw; });
addEventListener("blur", () { this.redraw; });
addEventListener(EventType.change, () { this.redraw; });
addEventListener("mousedown", () { this.focus(); this.popup(); });
addEventListener("keydown", (Event event) {
if(event.key == Key.Space)
popup();
});
} else static assert(false);
}
version(custom_widgets)
override void paint(WidgetPainter painter) {
draw3dFrame(this, painter, FrameStyle.risen);
painter.outlineColor = Color.black;
painter.drawText(Point(4, 4), selection == -1 ? "" : options[selection]);
painter.outlineColor = Color.black;
painter.fillColor = Color.black;
Point[4] triangle;
enum padding = 6;
enum paddingV = 7;
enum triangleWidth = 10;
triangle[0] = Point(width - padding - triangleWidth, paddingV);
triangle[1] = Point(width - padding - triangleWidth / 2, height - paddingV);
triangle[2] = Point(width - padding - 0, paddingV);
triangle[3] = triangle[0];
painter.drawPolygon(triangle[]);
if(isFocused()) {
painter.fillColor = Color.transparent;
painter.pen = Pen(Color.black, 1, Pen.Style.Dotted);
painter.drawRectangle(Point(2, 2), width - 4, height - 4);
painter.pen = Pen(Color.black, 1, Pen.Style.Solid);
}
}
version(win32_widgets)
override void registerMovement() {
version(win32_widgets) {
if(hwnd) {
auto pos = getChildPositionRelativeToParentHwnd(this);
// the height given to this from Windows' perspective is supposed
// to include the drop down's height. so I add to it to give some
// room for that.
// FIXME: maybe make the subclass provide a suggestedDropdownHeight thing
MoveWindow(hwnd, pos[0], pos[1], width, height + 200, true);
}
}
sendResizeEvent();
}
}
/++
A text box with a drop down arrow listing selections.
The user can choose from the list, or type their own.
+/
class FreeEntrySelection : ComboboxBase {
this(Widget parent = null) {
version(win32_widgets)
super(2 /* CBS_DROPDOWN */, parent);
else version(custom_widgets) {
super(parent);
auto hl = new HorizontalLayout(this);
lineEdit = new LineEdit(hl);
tabStop = false;
lineEdit.addEventListener("focus", &lineEdit.selectAll);
auto btn = new class ArrowButton {
this() {
super(ArrowDirection.down, hl);
}
override int maxHeight() {
return int.max;
}
};
//btn.addDirectEventListener("focus", &lineEdit.focus);
btn.addEventListener("triggered", &this.popup);
addEventListener(EventType.change, (Event event) {
lineEdit.content = event.stringValue;
lineEdit.focus();
redraw();
});
}
else static assert(false);
}
version(custom_widgets) {
LineEdit lineEdit;
}
}
/++
A combination of free entry with a list below it.
+/
class ComboBox : ComboboxBase {
this(Widget parent = null) {
version(win32_widgets)
super(1 /* CBS_SIMPLE */ | CBS_NOINTEGRALHEIGHT, parent);
else version(custom_widgets) {
super(parent);
lineEdit = new LineEdit(this);
listWidget = new ListWidget(this);
listWidget.multiSelect = false;
listWidget.addEventListener(EventType.change, delegate(Widget, Event) {
string c = null;
foreach(option; listWidget.options)
if(option.selected) {
c = option.label;
break;
}
lineEdit.content = c;
});
listWidget.tabStop = false;
this.tabStop = false;
listWidget.addEventListener("focus", &lineEdit.focus);
this.addEventListener("focus", &lineEdit.focus);
addDirectEventListener(EventType.change, {
listWidget.setSelection(selection);
if(selection != -1)
lineEdit.content = options[selection];
lineEdit.focus();
redraw();
});
lineEdit.addEventListener("focus", &lineEdit.selectAll);
listWidget.addDirectEventListener(EventType.change, {
int set = -1;
foreach(idx, opt; listWidget.options)
if(opt.selected) {
set = cast(int) idx;
break;
}
if(set != selection)
this.setSelection(set);
});
} else static assert(false);
}
override int minHeight() { return Window.lineHeight * 3; }
override int maxHeight() { return int.max; }
override int heightStretchiness() { return 5; }
version(custom_widgets) {
LineEdit lineEdit;
ListWidget listWidget;
override void addOption(string s) {
listWidget.options ~= ListWidget.Option(s);
ComboboxBase.addOption(s);
}
}
}
/+
class Spinner : Widget {
version(win32_widgets)
this(Widget parent = null) {
super(parent);
parentWindow = parent.parentWindow;
auto hlayout = new HorizontalLayout(this);
lineEdit = new LineEdit(hlayout);
upDownControl = new UpDownControl(hlayout);
}
LineEdit lineEdit;
UpDownControl upDownControl;
}
class UpDownControl : Widget {
version(win32_widgets)
this(Widget parent = null) {
super(parent);
parentWindow = parent.parentWindow;
createWin32Window(this, "msctls_updown32"w, null, 4/*UDS_ALIGNRIGHT*/| 2 /* UDS_SETBUDDYINT */ | 16 /* UDS_AUTOBUDDY */ | 32 /* UDS_ARROWKEYS */);
}
override int minHeight() { return Window.lineHeight; }
override int maxHeight() { return Window.lineHeight * 3/2; }
override int minWidth() { return Window.lineHeight * 3/2; }
override int maxWidth() { return Window.lineHeight * 3/2; }
}
+/
/+
class DataView : Widget {
// this is the omnibus data viewer
// the internal data layout is something like:
// string[string][] but also each node can have parents
}
+/
// http://msdn.microsoft.com/en-us/library/windows/desktop/bb775491(v=vs.85).aspx#PROGRESS_CLASS
// http://svn.dsource.org/projects/bindings/trunk/win32/commctrl.d
// FIXME: menus should prolly capture the mouse. ugh i kno.
/*
TextEdit needs:
* caret manipulation
* selection control
* convenience functions for appendText, insertText, insertTextAtCaret, etc.
For example:
connect(paste, &textEdit.insertTextAtCaret);
would be nice.
I kinda want an omnibus dataview that combines list, tree,
and table - it can be switched dynamically between them.
Flattening policy: only show top level, show recursive, show grouped
List styles: plain list (e.g. <ul>), tiles (some details next to it), icons (like Windows explorer)
Single select, multi select, organization, drag+drop
*/
//static if(UsingSimpledisplayX11)
version(win32_widgets) {}
else version(custom_widgets) {
enum windowBackgroundColor = Color(212, 212, 212); // used to be 192
enum activeTabColor = lightAccentColor;
enum hoveringColor = Color(228, 228, 228);
enum buttonColor = windowBackgroundColor;
enum depressedButtonColor = darkAccentColor;
enum activeListXorColor = Color(255, 255, 127);
enum progressBarColor = Color(0, 0, 128);
enum activeMenuItemColor = Color(0, 0, 128);
enum scrollClickRepeatInterval = 50;
}
else static assert(false);
// these are used by horizontal rule so not just custom_widgets. for now at least.
enum darkAccentColor = Color(172, 172, 172);
enum lightAccentColor = Color(223, 223, 223); // used to be 223
private const(wchar)* toWstringzInternal(in char[] s) {
wchar[] str;
str.reserve(s.length + 1);
foreach(dchar ch; s)
str ~= ch;
str ~= '\0';
return str.ptr;
}
static if(SimpledisplayTimerAvailable)
void setClickRepeat(Widget w, int interval, int delay = 250) {
Timer timer;
int delayRemaining = delay / interval;
if(delayRemaining <= 1)
delayRemaining = 2;
immutable originalDelayRemaining = delayRemaining;
w.addDirectEventListener("mousedown", (Event ev) {
if(ev.srcElement !is w)
return;
if(timer !is null) {
timer.destroy();
timer = null;
}
delayRemaining = originalDelayRemaining;
timer = new Timer(interval, () {
if(delayRemaining > 0)
delayRemaining--;
else {
auto ev = new Event("click", w);
ev.sendDirectly();
}
});
});
w.addDirectEventListener("mouseup", (Event ev) {
if(ev.srcElement !is w)
return;
if(timer !is null) {
timer.destroy();
timer = null;
}
});
w.addDirectEventListener("mouseleave", (Event ev) {
if(ev.srcElement !is w)
return;
if(timer !is null) {
timer.destroy();
timer = null;
}
});
}
else
void setClickRepeat(Widget w, int interval, int delay = 250) {}
enum FrameStyle {
risen,
sunk
}
version(custom_widgets)
void draw3dFrame(Widget widget, ScreenPainter painter, FrameStyle style, Color background = windowBackgroundColor) {
draw3dFrame(0, 0, widget.width, widget.height, painter, style, background);
}
version(custom_widgets)
void draw3dFrame(int x, int y, int width, int height, ScreenPainter painter, FrameStyle style, Color background = windowBackgroundColor) {
// outer layer
painter.outlineColor = style == FrameStyle.sunk ? Color.white : Color.black;
painter.fillColor = background;
painter.drawRectangle(Point(x + 0, y + 0), width, height);
painter.outlineColor = (style == FrameStyle.sunk) ? darkAccentColor : lightAccentColor;
painter.drawLine(Point(x + 0, y + 0), Point(x + width, y + 0));
painter.drawLine(Point(x + 0, y + 0), Point(x + 0, y + height - 1));
// inner layer
//right, bottom
painter.outlineColor = (style == FrameStyle.sunk) ? lightAccentColor : darkAccentColor;
painter.drawLine(Point(x + width - 2, y + 2), Point(x + width - 2, y + height - 2));
painter.drawLine(Point(x + 2, y + height - 2), Point(x + width - 2, y + height - 2));
// left, top
painter.outlineColor = (style == FrameStyle.sunk) ? Color.black : Color.white;
painter.drawLine(Point(x + 1, y + 1), Point(x + width, y + 1));
painter.drawLine(Point(x + 1, y + 1), Point(x + 1, y + height - 2));
}
///
class Action {
version(win32_widgets) {
private int id;
private static int lastId = 9000;
private static Action[int] mapping;
}
KeyEvent accelerator;
///
this(string label, ushort icon = 0, void delegate() triggered = null) {
this.label = label;
this.iconId = icon;
if(triggered !is null)
this.triggered ~= triggered;
version(win32_widgets) {
id = ++lastId;
mapping[id] = this;
}
}
private string label;
private ushort iconId;
// icon
// when it is triggered, the triggered event is fired on the window
void delegate()[] triggered;
}
/*
plan:
keyboard accelerators
* menus (and popups and tooltips)
* status bar
* toolbars and buttons
sortable table view
maybe notification area icons
basic clipboard
* radio box
splitter
toggle buttons (optionally mutually exclusive, like in Paint)
label, rich text display, multi line plain text (selectable)
* fieldset
* nestable grid layout
single line text input
* multi line text input
slider
spinner
list box
drop down
combo box
auto complete box
* progress bar
terminal window/widget (on unix it might even be a pty but really idk)
ok button
cancel button
keyboard hotkeys
scroll widget
event redirections and network transparency
script integration
*/
/*
MENUS
auto bar = new MenuBar(window);
window.menuBar = bar;
auto fileMenu = bar.addItem(new Menu("&File"));
fileMenu.addItem(new MenuItem("&Exit"));
EVENTS
For controls, you should usually use "triggered" rather than "click", etc., because
triggered handles both keyboard (focus and press as well as hotkeys) and mouse activation.
This is the case on menus and pushbuttons.
"click", on the other hand, currently only fires when it is literally clicked by the mouse.
*/
/*
enum LinePreference {
AlwaysOnOwnLine, // always on its own line
PreferOwnLine, // it will always start a new line, and if max width <= line width, it will expand all the way
PreferToShareLine, // does not force new line, and if the next child likes to share too, they will div it up evenly. otherwise, it will expand as much as it can
}
*/
mixin template Padding(string code) {
override int paddingLeft() { return mixin(code);}
override int paddingRight() { return mixin(code);}
override int paddingTop() { return mixin(code);}
override int paddingBottom() { return mixin(code);}
}
mixin template Margin(string code) {
override int marginLeft() { return mixin(code);}
override int marginRight() { return mixin(code);}
override int marginTop() { return mixin(code);}
override int marginBottom() { return mixin(code);}
}
mixin template LayoutInfo() {
private int
_maxWidth = int.max, _maxHeight = int.max,
_widthStretchiness = 4, _heightStretchiness = 4,
_marginLeft = 0, _marginRight = 0,
_marginTop = 0, _marginBottom = 0,
_paddingLeft = 0, _paddingRight = 0,
_paddingTop = 0, _paddingBottom = 0;
int minWidth() { return 0; }
int minHeight() {
// default widgets have a vertical layout, therefore the minimum height is the sum of the contents
int sum = 0;
foreach(child; children) {
sum += child.minHeight();
sum += child.marginTop();
sum += child.marginBottom();
}
return sum;
}
int maxWidth() { return _maxWidth; }
int maxHeight() { return _maxHeight; }
int widthStretchiness() { return _widthStretchiness; }
int heightStretchiness() { return _heightStretchiness; }
void setMaxWidth(int value) { _maxWidth = value; }
void setMaxHeight(int value) { _maxHeight = value; }
void setStretchiness(int ws, int hs) {
_widthStretchiness = ws;
_heightStretchiness = hs;
}
int marginLeft() { return _marginLeft; }
int marginRight() { return _marginRight; }
int marginTop() { return _marginTop; }
int marginBottom() { return _marginBottom; }
int paddingLeft() { return _paddingLeft; }
int paddingRight() { return _paddingRight; }
int paddingTop() { return _paddingTop; }
int paddingBottom() { return _paddingBottom; }
void setMargins(int top, int left, int bottom, int right) {
_marginTop = top;
_marginLeft = left;
_marginBottom = bottom;
_marginRight = right;
}
void setPadding(int top, int left, int bottom, int right) {
_paddingTop = top;
_paddingLeft = left;
_paddingBottom = bottom;
_paddingRight = right;
}
//LinePreference linePreference() { return LinePreference.PreferOwnLine; }
void recomputeChildLayout() {
.recomputeChildLayout!"height"(this);
}
}
private
void recomputeChildLayout(string relevantMeasure)(Widget parent) {
enum calcingV = relevantMeasure == "height";
parent.registerMovement();
if(parent.children.length == 0)
return;
enum firstThingy = relevantMeasure == "height" ? "Top" : "Left";
enum secondThingy = relevantMeasure == "height" ? "Bottom" : "Right";
enum otherFirstThingy = relevantMeasure == "height" ? "Left" : "Top";
enum otherSecondThingy = relevantMeasure == "height" ? "Right" : "Bottom";
// my own width and height should already be set by the caller of this function...
int spaceRemaining = mixin("parent." ~ relevantMeasure) -
mixin("parent.padding"~firstThingy~"()") -
mixin("parent.padding"~secondThingy~"()");
int stretchinessSum;
int stretchyChildSum;
int lastMargin = 0;
// set initial size
foreach(child; parent.children) {
if(cast(StaticPosition) child)
continue;
if(child.hidden)
continue;
static if(calcingV) {
child.width = parent.width -
mixin("child.margin"~otherFirstThingy~"()") -
mixin("child.margin"~otherSecondThingy~"()") -
mixin("parent.padding"~otherFirstThingy~"()") -
mixin("parent.padding"~otherSecondThingy~"()");
if(child.width < 0)
child.width = 0;
if(child.width > child.maxWidth())
child.width = child.maxWidth();
child.height = child.minHeight();
} else {
child.height = parent.height -
mixin("child.margin"~firstThingy~"()") -
mixin("child.margin"~secondThingy~"()") -
mixin("parent.padding"~firstThingy~"()") -
mixin("parent.padding"~secondThingy~"()");
if(child.height < 0)
child.height = 0;
if(child.height > child.maxHeight())
child.height = child.maxHeight();
child.width = child.minWidth();
}
spaceRemaining -= mixin("child." ~ relevantMeasure);
int thisMargin = mymax(lastMargin, mixin("child.margin"~firstThingy~"()"));
auto margin = mixin("child.margin" ~ secondThingy ~ "()");
lastMargin = margin;
spaceRemaining -= thisMargin + margin;
auto s = mixin("child." ~ relevantMeasure ~ "Stretchiness()");
stretchinessSum += s;
if(s > 0)
stretchyChildSum++;
}
// stretch to fill space
while(spaceRemaining > 0 && stretchinessSum && stretchyChildSum) {
//import std.stdio; writeln("str ", stretchinessSum);
auto spacePerChild = spaceRemaining / stretchinessSum;
bool spreadEvenly;
bool giveToBiggest;
if(spacePerChild <= 0) {
spacePerChild = spaceRemaining / stretchyChildSum;
spreadEvenly = true;
}
if(spacePerChild <= 0) {
giveToBiggest = true;
}
int previousSpaceRemaining = spaceRemaining;
stretchinessSum = 0;
Widget mostStretchy;