-
Notifications
You must be signed in to change notification settings - Fork 63
/
luigi2.h
7188 lines (6007 loc) · 249 KB
/
luigi2.h
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
// TODO UITextbox features - mouse input, undo, number dragging.
// TODO New elements - list view, menu bar.
// TODO Keyboard navigation in menus.
// TODO Easier to use fonts.
/////////////////////////////////////////
// Header includes.
/////////////////////////////////////////
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdarg.h>
#ifdef UI_LINUX
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/cursorfont.h>
#endif
#ifdef UI_SSE2
#include <xmmintrin.h>
#endif
#ifdef UI_WINDOWS
#undef _UNICODE
#undef UNICODE
#include <windows.h>
#define UI_ASSERT(x) do { if (!(x)) { ui.assertionFailure = true; \
MessageBox(0, "Assertion failure on line " _UI_TO_STRING_2(__LINE__), 0, 0); \
ExitProcess(1); } } while (0)
#define UI_CALLOC(x) HeapAlloc(ui.heap, HEAP_ZERO_MEMORY, (x))
#define UI_FREE(x) HeapFree(ui.heap, 0, (x))
#define UI_MALLOC(x) HeapAlloc(ui.heap, 0, (x))
#define UI_REALLOC _UIHeapReAlloc
#define UI_CLOCK GetTickCount
#define UI_CLOCKS_PER_SECOND (1000)
#define UI_CLOCK_T DWORD
#define UI_MEMMOVE _UIMemmove
#endif
#ifdef UI_COCOA
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import <Carbon/Carbon.h>
#endif
#if defined(UI_LINUX) || defined(UI_COCOA)
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <math.h>
#define UI_ASSERT assert
#define UI_CALLOC(x) calloc(1, (x))
#define UI_FREE free
#define UI_MALLOC malloc
#define UI_REALLOC realloc
#define UI_CLOCK _UIClock
#define UI_CLOCKS_PER_SECOND 1000
#define UI_CLOCK_T clock_t
#define UI_MEMMOVE(d, s, n) do { size_t _n = n; if (_n) { memmove(d, s, _n); } } while (0)
#endif
#if defined(UI_ESSENCE)
#include <essence.h>
#define UI_ASSERT EsAssert
#define UI_CALLOC(x) EsHeapAllocate((x), true)
#define UI_FREE EsHeapFree
#define UI_MALLOC(x) EsHeapAllocate((x), false)
#define UI_REALLOC(x, y) EsHeapReallocate((x), (y), false)
#define UI_CLOCK EsTimeStampMs
#define UI_CLOCKS_PER_SECOND 1000
#define UI_CLOCK_T uint64_t
#define UI_MEMMOVE EsCRTmemmove
// Callback to allow the application to process messages.
void _UIMessageProcess(EsMessage *message);
#endif
#ifdef UI_DEBUG
#include <stdio.h>
#endif
#ifdef UI_FREETYPE
#include <ft2build.h>
#include FT_FREETYPE_H
#include <freetype/ftbitmap.h>
#endif
/////////////////////////////////////////
// Definitions.
/////////////////////////////////////////
#define _UI_TO_STRING_1(x) #x
#define _UI_TO_STRING_2(x) _UI_TO_STRING_1(x)
#define UI_SIZE_BUTTON_MINIMUM_WIDTH (100)
#define UI_SIZE_BUTTON_PADDING (16)
#define UI_SIZE_BUTTON_HEIGHT (27)
#define UI_SIZE_BUTTON_CHECKED_AREA (4)
#define UI_SIZE_CHECKBOX_BOX (14)
#define UI_SIZE_CHECKBOX_GAP (8)
#define UI_SIZE_MENU_ITEM_HEIGHT (24)
#define UI_SIZE_MENU_ITEM_MINIMUM_WIDTH (160)
#define UI_SIZE_MENU_ITEM_MARGIN (9)
#define UI_SIZE_GAUGE_WIDTH (200)
#define UI_SIZE_GAUGE_HEIGHT (22)
#define UI_SIZE_SLIDER_WIDTH (200)
#define UI_SIZE_SLIDER_HEIGHT (25)
#define UI_SIZE_SLIDER_THUMB (15)
#define UI_SIZE_SLIDER_TRACK (5)
#define UI_SIZE_TEXTBOX_MARGIN (3)
#define UI_SIZE_TEXTBOX_WIDTH (200)
#define UI_SIZE_TEXTBOX_HEIGHT (27)
#define UI_SIZE_TAB_PANE_SPACE_TOP (2)
#define UI_SIZE_TAB_PANE_SPACE_LEFT (4)
#define UI_SIZE_SPLITTER (8)
#define UI_SIZE_SCROLL_BAR (16)
#define UI_SIZE_SCROLL_MINIMUM_THUMB (20)
#define UI_SIZE_CODE_MARGIN (ui.activeFont->glyphWidth * 5)
#define UI_SIZE_CODE_MARGIN_GAP (ui.activeFont->glyphWidth * 1)
#define UI_SIZE_TABLE_HEADER (26)
#define UI_SIZE_TABLE_COLUMN_GAP (20)
#define UI_SIZE_TABLE_ROW (20)
#define UI_SIZE_PANE_LARGE_BORDER (20)
#define UI_SIZE_PANE_LARGE_GAP (10)
#define UI_SIZE_PANE_MEDIUM_BORDER (5)
#define UI_SIZE_PANE_MEDIUM_GAP (5)
#define UI_SIZE_PANE_SMALL_BORDER (3)
#define UI_SIZE_PANE_SMALL_GAP (3)
#define UI_SIZE_MDI_CHILD_BORDER (6)
#define UI_SIZE_MDI_CHILD_TITLE (30)
#define UI_SIZE_MDI_CHILD_CORNER (12)
#define UI_SIZE_MDI_CHILD_MINIMUM_WIDTH (100)
#define UI_SIZE_MDI_CHILD_MINIMUM_HEIGHT (50)
#define UI_SIZE_MDI_CASCADE (30)
#define UI_MDI_CHILD_CALCULATE_LAYOUT(bounds, scale) \
int titleSize = UI_SIZE_MDI_CHILD_TITLE * scale; \
int borderSize = UI_SIZE_MDI_CHILD_BORDER * scale; \
UIRectangle title = UIRectangleAdd(bounds, UI_RECT_4(borderSize, -borderSize, 0, 0)); \
title.b = title.t + titleSize; \
UIRectangle content = UIRectangleAdd(bounds, UI_RECT_4(borderSize, -borderSize, titleSize, -borderSize));
#define UI_UPDATE_HOVERED (1)
#define UI_UPDATE_PRESSED (2)
#define UI_UPDATE_FOCUSED (3)
#define UI_UPDATE_DISABLED (4)
typedef enum UIMessage {
// General messages.
UI_MSG_PAINT, // dp = pointer to UIPainter
UI_MSG_PAINT_FOREGROUND, // after children have painted
UI_MSG_LAYOUT,
UI_MSG_DESTROY,
UI_MSG_DEALLOCATE,
UI_MSG_UPDATE, // di = UI_UPDATE_... constant
UI_MSG_ANIMATE,
UI_MSG_SCROLLED,
UI_MSG_GET_WIDTH, // di = height (if known); return width
UI_MSG_GET_HEIGHT, // di = width (if known); return height
UI_MSG_GET_CHILD_STABILITY, // dp = child element; return stable axes, 1 (width) | 2 (height)
// Input events.
UI_MSG_INPUT_EVENTS_START, // not sent to disabled elements
UI_MSG_LEFT_DOWN,
UI_MSG_LEFT_UP,
UI_MSG_MIDDLE_DOWN,
UI_MSG_MIDDLE_UP,
UI_MSG_RIGHT_DOWN,
UI_MSG_RIGHT_UP,
UI_MSG_KEY_TYPED, // dp = pointer to UIKeyTyped; return 1 if handled
UI_MSG_KEY_RELEASED, // dp = pointer to UIKeyTyped; return 1 if handled
UI_MSG_MOUSE_MOVE,
UI_MSG_MOUSE_DRAG,
UI_MSG_MOUSE_WHEEL, // di = delta; return 1 if handled
UI_MSG_CLICKED,
UI_MSG_GET_CURSOR, // return cursor code
UI_MSG_PRESSED_DESCENDENT, // dp = pointer to child that is/contains pressed element
UI_MSG_INPUT_EVENTS_END,
// Specific elements.
UI_MSG_VALUE_CHANGED, // sent to notify that the element's value has changed
UI_MSG_TABLE_GET_ITEM, // dp = pointer to UITableGetItem; return string length
UI_MSG_CODE_GET_MARGIN_COLOR, // di = line index (starts at 1); return color
UI_MSG_CODE_DECORATE_LINE, // dp = pointer to UICodeDecorateLine
UI_MSG_TAB_SELECTED, // sent to the tab that was selected (not the tab pane itself)
// Windows.
UI_MSG_WINDOW_DROP_FILES, // di = count, dp = char ** of paths
UI_MSG_WINDOW_ACTIVATE,
UI_MSG_WINDOW_CLOSE, // return 1 to prevent default (process exit for UIWindow; close for UIMDIChild)
UI_MSG_WINDOW_UPDATE_START,
UI_MSG_WINDOW_UPDATE_BEFORE_DESTROY,
UI_MSG_WINDOW_UPDATE_BEFORE_LAYOUT,
UI_MSG_WINDOW_UPDATE_BEFORE_PAINT,
UI_MSG_WINDOW_UPDATE_END,
// User-defined messages.
UI_MSG_USER,
} UIMessage;
#ifdef UI_ESSENCE
#define UIRectangle EsRectangle
#else
typedef struct UIRectangle {
int l, r, t, b;
} UIRectangle;
#endif
typedef struct UITheme {
uint32_t panel1, panel2, selected, border;
uint32_t text, textDisabled, textSelected;
uint32_t buttonNormal, buttonHovered, buttonPressed, buttonDisabled;
uint32_t textboxNormal, textboxFocused;
uint32_t codeFocused, codeBackground, codeDefault, codeComment, codeString, codeNumber, codeOperator, codePreprocessor;
uint32_t accent1, accent2;
} UITheme;
typedef struct UIPainter {
UIRectangle clip;
uint32_t *bits;
int width, height;
#ifdef UI_DEBUG
int fillCount;
#endif
} UIPainter;
typedef struct UIFont {
int glyphWidth, glyphHeight;
#ifdef UI_FREETYPE
bool isFreeType;
FT_Face font;
#ifdef UI_UNICODE
FT_Bitmap *glyphs;
bool *glyphsRendered;
int *glyphOffsetsX, *glyphOffsetsY;
#else
FT_Bitmap glyphs[128];
bool glyphsRendered[128];
int glyphOffsetsX[128], glyphOffsetsY[128];
#endif
#endif
} UIFont;
typedef struct UIShortcut {
intptr_t code;
bool ctrl, shift, alt;
void (*invoke)(void *cp);
void *cp;
} UIShortcut;
typedef struct UIStringSelection {
int carets[2];
uint32_t colorText, colorBackground;
} UIStringSelection;
typedef struct UIKeyTyped {
char *text;
int textBytes;
intptr_t code;
} UIKeyTyped;
typedef struct UITableGetItem {
char *buffer;
size_t bufferBytes;
int index, column;
bool isSelected;
} UITableGetItem;
typedef struct UICodeDecorateLine {
UIRectangle bounds;
int index; // Starting at 1!
int x, y; // Position where additional text can be drawn.
UIPainter *painter;
} UICodeDecorateLine;
#define UI_RECT_1(x) ((UIRectangle) { (x), (x), (x), (x) })
#define UI_RECT_1I(x) ((UIRectangle) { (x), -(x), (x), -(x) })
#define UI_RECT_2(x, y) ((UIRectangle) { (x), (x), (y), (y) })
#define UI_RECT_2I(x, y) ((UIRectangle) { (x), -(x), (y), -(y) })
#define UI_RECT_2S(x, y) ((UIRectangle) { 0, (x), 0, (y) })
#define UI_RECT_4(x, y, z, w) ((UIRectangle) { (x), (y), (z), (w) })
#define UI_RECT_4PD(x, y, w, h) ((UIRectangle) { (x), ((x) + (w)), (y), ((y) + (h)) })
#define UI_RECT_WIDTH(_r) ((_r).r - (_r).l)
#define UI_RECT_HEIGHT(_r) ((_r).b - (_r).t)
#define UI_RECT_TOTAL_H(_r) ((_r).r + (_r).l)
#define UI_RECT_TOTAL_V(_r) ((_r).b + (_r).t)
#define UI_RECT_SIZE(_r) UI_RECT_WIDTH(_r), UI_RECT_HEIGHT(_r)
#define UI_RECT_TOP_LEFT(_r) (_r).l, (_r).t
#define UI_RECT_BOTTOM_LEFT(_r) (_r).l, (_r).b
#define UI_RECT_BOTTOM_RIGHT(_r) (_r).r, (_r).b
#define UI_RECT_ALL(_r) (_r).l, (_r).r, (_r).t, (_r).b
#define UI_RECT_VALID(_r) ((_r).l < (_r).r && (_r).t < (_r).b)
#define UI_COLOR_ALPHA_F(x) ((((x) >> 24) & 0xFF) / 255.0f)
#define UI_COLOR_RED_F(x) ((((x) >> 16) & 0xFF) / 255.0f)
#define UI_COLOR_GREEN_F(x) ((((x) >> 8) & 0xFF) / 255.0f)
#define UI_COLOR_BLUE_F(x) ((((x) >> 0) & 0xFF) / 255.0f)
#define UI_COLOR_ALPHA(x) ((((x) >> 24) & 0xFF))
#define UI_COLOR_RED(x) ((((x) >> 16) & 0xFF))
#define UI_COLOR_GREEN(x) ((((x) >> 8) & 0xFF))
#define UI_COLOR_BLUE(x) ((((x) >> 0) & 0xFF))
#define UI_COLOR_FROM_FLOAT(r, g, b) (((uint32_t) ((r) * 255.0f) << 16) | ((uint32_t) ((g) * 255.0f) << 8) | ((uint32_t) ((b) * 255.0f) << 0))
#define UI_COLOR_FROM_RGBA_F(r, g, b, a) (((uint32_t) ((r) * 255.0f) << 16) | ((uint32_t) ((g) * 255.0f) << 8) \
| ((uint32_t) ((b) * 255.0f) << 0) | ((uint32_t) ((a) * 255.0f) << 24))
#define UI_SWAP(s, a, b) do { s t = (a); (a) = (b); (b) = t; } while (0)
#ifndef UI_DRAW_CONTROL_CUSTOM
#define UIDrawControl UIDrawControlDefault
#endif
#define UI_DRAW_CONTROL_PUSH_BUTTON (1)
#define UI_DRAW_CONTROL_DROP_DOWN (2)
#define UI_DRAW_CONTROL_MENU_ITEM (3)
#define UI_DRAW_CONTROL_CHECKBOX (4)
#define UI_DRAW_CONTROL_LABEL (5)
#define UI_DRAW_CONTROL_SPLITTER (6)
#define UI_DRAW_CONTROL_SCROLL_TRACK (7)
#define UI_DRAW_CONTROL_SCROLL_UP (8)
#define UI_DRAW_CONTROL_SCROLL_DOWN (9)
#define UI_DRAW_CONTROL_SCROLL_THUMB (10)
#define UI_DRAW_CONTROL_GAUGE (11)
#define UI_DRAW_CONTROL_SLIDER (12)
#define UI_DRAW_CONTROL_TEXTBOX (13)
#define UI_DRAW_CONTROL_MODAL_POPUP (14)
#define UI_DRAW_CONTROL_MENU (15)
#define UI_DRAW_CONTROL_TABLE_ROW (16)
#define UI_DRAW_CONTROL_TABLE_CELL (17)
#define UI_DRAW_CONTROL_TABLE_BACKGROUND (18)
#define UI_DRAW_CONTROL_TABLE_HEADER (19)
#define UI_DRAW_CONTROL_MDI_CHILD (20)
#define UI_DRAW_CONTROL_TAB (21)
#define UI_DRAW_CONTROL_TAB_BAND (22)
#define UI_DRAW_CONTROL_TYPE_MASK (0xFF)
#define UI_DRAW_CONTROL_STATE_SELECTED (1 << 24)
#define UI_DRAW_CONTROL_STATE_VERTICAL (1 << 25)
#define UI_DRAW_CONTROL_STATE_INDETERMINATE (1 << 26)
#define UI_DRAW_CONTROL_STATE_CHECKED (1 << 27)
#define UI_DRAW_CONTROL_STATE_HOVERED (1 << 28)
#define UI_DRAW_CONTROL_STATE_FOCUSED (1 << 29)
#define UI_DRAW_CONTROL_STATE_PRESSED (1 << 30)
#define UI_DRAW_CONTROL_STATE_DISABLED (1 << 31)
#define UI_DRAW_CONTROL_STATE_FROM_ELEMENT(x) ((((x)->flags & UI_ELEMENT_DISABLED) ? UI_DRAW_CONTROL_STATE_DISABLED : 0) \
| (((x)->window->hovered == (x)) ? UI_DRAW_CONTROL_STATE_HOVERED : 0) \
| (((x)->window->focused == (x)) ? UI_DRAW_CONTROL_STATE_FOCUSED : 0) \
| (((x)->window->pressed == (x)) ? UI_DRAW_CONTROL_STATE_PRESSED : 0))
#define UI_CURSOR_ARROW (0)
#define UI_CURSOR_TEXT (1)
#define UI_CURSOR_SPLIT_V (2)
#define UI_CURSOR_SPLIT_H (3)
#define UI_CURSOR_FLIPPED_ARROW (4)
#define UI_CURSOR_CROSS_HAIR (5)
#define UI_CURSOR_HAND (6)
#define UI_CURSOR_RESIZE_UP (7)
#define UI_CURSOR_RESIZE_LEFT (8)
#define UI_CURSOR_RESIZE_UP_RIGHT (9)
#define UI_CURSOR_RESIZE_UP_LEFT (10)
#define UI_CURSOR_RESIZE_DOWN (11)
#define UI_CURSOR_RESIZE_RIGHT (12)
#define UI_CURSOR_RESIZE_DOWN_RIGHT (13)
#define UI_CURSOR_RESIZE_DOWN_LEFT (14)
#define UI_CURSOR_COUNT (15)
#define UI_ALIGN_LEFT (1)
#define UI_ALIGN_RIGHT (2)
#define UI_ALIGN_CENTER (3)
extern const int UI_KEYCODE_A;
extern const int UI_KEYCODE_BACKSPACE;
extern const int UI_KEYCODE_DELETE;
extern const int UI_KEYCODE_DOWN;
extern const int UI_KEYCODE_END;
extern const int UI_KEYCODE_ENTER;
extern const int UI_KEYCODE_ESCAPE;
extern const int UI_KEYCODE_F1;
extern const int UI_KEYCODE_HOME;
extern const int UI_KEYCODE_LEFT;
extern const int UI_KEYCODE_RIGHT;
extern const int UI_KEYCODE_SPACE;
extern const int UI_KEYCODE_TAB;
extern const int UI_KEYCODE_UP;
extern const int UI_KEYCODE_INSERT;
extern const int UI_KEYCODE_0;
extern const int UI_KEYCODE_BACKTICK;
extern const int UI_KEYCODE_PAGE_UP;
extern const int UI_KEYCODE_PAGE_DOWN;
#define UI_KEYCODE_LETTER(x) (UI_KEYCODE_A + (x) - 'A')
#define UI_KEYCODE_DIGIT(x) (UI_KEYCODE_0 + (x) - '0')
#define UI_KEYCODE_FKEY(x) (UI_KEYCODE_F1 + (x) - 1)
#define UI_ELEMENT_FILL (UI_ELEMENT_V_FILL | UI_ELEMENT_H_FILL)
typedef struct UIElement {
#define UI_ELEMENT_V_FILL (1 << 16)
#define UI_ELEMENT_H_FILL (1 << 17)
#define UI_ELEMENT_WINDOW (1 << 18)
#define UI_ELEMENT_PARENT_PUSH (1 << 19)
#define UI_ELEMENT_TAB_STOP (1 << 20)
#define UI_ELEMENT_NON_CLIENT (1 << 21) // Don't destroy in UIElementDestroyDescendents, like scroll bars.
#define UI_ELEMENT_DISABLED (1 << 22) // Don't receive input events.
#define UI_ELEMENT_BORDER (1 << 23)
#define UI_ELEMENT_HIDE (1 << 27)
#define UI_ELEMENT_RELAYOUT (1 << 28)
#define UI_ELEMENT_RELAYOUT_DESCENDENT (1 << 29)
#define UI_ELEMENT_DESTROY (1 << 30)
#define UI_ELEMENT_DESTROY_DESCENDENT (1 << 31)
uint32_t flags; // First 16 bits are element specific.
uint32_t id;
uint32_t childCount;
uint32_t _unused0;
struct UIElement *parent;
struct UIElement **children;
struct UIWindow *window;
UIRectangle bounds, clip;
void *cp; // Context pointer (for user).
int (*messageClass)(struct UIElement *element, UIMessage message, int di /* data integer */, void *dp /* data pointer */);
int (*messageUser)(struct UIElement *element, UIMessage message, int di, void *dp);
const char *cClassName;
} UIElement;
#define UI_SHORTCUT(code, ctrl, shift, alt, invoke, cp) ((UIShortcut) { (code), (ctrl), (shift), (alt), (invoke), (cp) })
typedef struct UIWindow {
#define UI_WINDOW_MENU (1 << 0)
#define UI_WINDOW_INSPECTOR (1 << 1)
#define UI_WINDOW_CENTER_IN_OWNER (1 << 2)
#define UI_WINDOW_MAXIMIZE (1 << 3)
UIElement e;
UIElement *dialog;
UIShortcut *shortcuts;
size_t shortcutCount, shortcutAllocated;
float scale;
uint32_t *bits;
int width, height;
struct UIWindow *next;
UIElement *hovered, *pressed, *focused, *dialogOldFocus;
int pressedButton;
int cursorX, cursorY;
int cursorStyle;
// Set when a textbox is modified.
// Useful for tracking whether changes to the loaded document have been saved.
bool textboxModifiedFlag;
bool ctrl, shift, alt;
UIRectangle updateRegion;
#ifdef UI_DEBUG
float lastFullFillCount;
#endif
#ifdef UI_LINUX
Window window;
XImage *image;
XIC xic;
unsigned ctrlCode, shiftCode, altCode;
Window dragSource;
#endif
#ifdef UI_WINDOWS
HWND hwnd;
bool trackingLeave;
#endif
#ifdef UI_ESSENCE
EsWindow *window;
EsElement *canvas;
int cursor;
#endif
#ifdef UI_COCOA
NSWindow *window;
void *view;
#endif
} UIWindow;
typedef struct UIPanel {
#define UI_PANEL_HORIZONTAL (1 << 0)
#define UI_PANEL_COLOR_1 (1 << 2)
#define UI_PANEL_COLOR_2 (1 << 3)
#define UI_PANEL_SMALL_SPACING (1 << 5)
#define UI_PANEL_MEDIUM_SPACING (1 << 6)
#define UI_PANEL_LARGE_SPACING (1 << 7)
#define UI_PANEL_SCROLL (1 << 8)
#define UI_PANEL_EXPAND (1 << 9)
UIElement e;
struct UIScrollBar *scrollBar;
UIRectangle border;
int gap;
} UIPanel;
typedef struct UIButton {
#define UI_BUTTON_SMALL (1 << 0)
#define UI_BUTTON_MENU_ITEM (1 << 1)
#define UI_BUTTON_CAN_FOCUS (1 << 2)
#define UI_BUTTON_DROP_DOWN (1 << 3)
#define UI_BUTTON_CHECKED (1 << 15)
UIElement e;
char *label;
ptrdiff_t labelBytes;
void (*invoke)(void *cp);
} UIButton;
typedef struct UICheckbox {
#define UI_CHECKBOX_ALLOW_INDETERMINATE (1 << 0)
UIElement e;
#define UI_CHECK_UNCHECKED (0)
#define UI_CHECK_CHECKED (1)
#define UI_CHECK_INDETERMINATE (2)
uint8_t check;
char *label;
ptrdiff_t labelBytes;
void (*invoke)(void *cp);
} UICheckbox;
typedef struct UILabel {
UIElement e;
char *label;
ptrdiff_t labelBytes;
} UILabel;
typedef struct UISpacer {
UIElement e;
int width, height;
} UISpacer;
typedef struct UISplitPane {
#define UI_SPLIT_PANE_VERTICAL (1 << 0)
UIElement e;
float weight;
} UISplitPane;
typedef struct UITabPane {
UIElement e;
char *tabs;
uint32_t active;
} UITabPane;
typedef struct UIScrollBar {
#define UI_SCROLL_BAR_HORIZONTAL (1 << 0)
UIElement e;
int64_t maximum, page;
int64_t dragOffset;
double position;
UI_CLOCK_T lastAnimateTime;
bool inDrag, horizontal;
} UIScrollBar;
#define _UI_LAYOUT_SCROLL_BAR_PAIR(element) \
element->vScroll->page = vSpace - (element->hScroll->page < element->hScroll->maximum ? scrollBarSize : 0); \
element->hScroll->page = hSpace - (element->vScroll->page < element->vScroll->maximum ? scrollBarSize : 0); \
element->vScroll->page = vSpace - (element->hScroll->page < element->hScroll->maximum ? scrollBarSize : 0); \
UIRectangle vScrollBarBounds = element->e.bounds, hScrollBarBounds = element->e.bounds; \
hScrollBarBounds.r = vScrollBarBounds.l = vScrollBarBounds.r - (element->vScroll->page < element->vScroll->maximum ? scrollBarSize : 0); \
vScrollBarBounds.b = hScrollBarBounds.t = hScrollBarBounds.b - (element->hScroll->page < element->hScroll->maximum ? scrollBarSize : 0); \
UIElementMove(&element->vScroll->e, vScrollBarBounds, true); \
UIElementMove(&element->hScroll->e, hScrollBarBounds, true);
#define _UI_KEY_INPUT_VSCROLL(element, rowHeight, pageHeight) \
if (m->code == UI_KEYCODE_UP) element->vScroll->position -= (rowHeight); \
else if (m->code == UI_KEYCODE_DOWN) element->vScroll->position += (rowHeight); \
else if (m->code == UI_KEYCODE_PAGE_UP) element->vScroll->position += (pageHeight); \
else if (m->code == UI_KEYCODE_PAGE_DOWN) element->vScroll->position -= (pageHeight); \
else if (m->code == UI_KEYCODE_HOME) element->vScroll->position = 0; \
else if (m->code == UI_KEYCODE_END) element->vScroll->position = element->vScroll->maximum; \
UIElementRefresh(&element->e);
typedef struct UICodeLine {
int offset, bytes;
} UICodeLine;
typedef struct UICode {
#define UI_CODE_NO_MARGIN (1 << 0)
#define UI_CODE_SELECTABLE (1 << 1)
UIElement e;
UIScrollBar *vScroll, *hScroll;
UICodeLine *lines;
UIFont *font;
int lineCount, focused;
bool moveScrollToFocusNextLayout;
bool leftDownInMargin;
char *content;
size_t contentBytes;
int tabSize;
int columns;
UI_CLOCK_T lastAnimateTime;
struct { int line, offset; } selection[4 /* start, end, anchor, caret */];
int verticalMotionColumn;
bool useVerticalMotionColumn;
bool moveScrollToCaretNextLayout;
} UICode;
typedef struct UIGauge {
UIElement e;
double position;
} UIGauge;
typedef struct UITable {
UIElement e;
UIScrollBar *vScroll, *hScroll;
int itemCount;
char *columns;
int *columnWidths, columnCount, columnHighlight;
} UITable;
typedef struct UITextbox {
UIElement e;
char *string;
ptrdiff_t bytes;
int carets[2];
int scroll;
bool rejectNextKey;
} UITextbox;
#define UI_MENU_PLACE_ABOVE (1 << 0)
#define UI_MENU_NO_SCROLL (1 << 1)
#if defined(UI_COCOA)
typedef NSMenu UIMenu;
#elif defined(UI_ESSENCE)
typedef EsMenu UIMenu;
#else
typedef struct UIMenu {
UIElement e;
int pointX, pointY;
UIScrollBar *vScroll;
UIWindow *parentWindow;
} UIMenu;
#endif
typedef struct UISlider {
UIElement e;
double position;
int steps;
} UISlider;
typedef struct UIMDIClient {
#define UI_MDI_CLIENT_TRANSPARENT (1 << 0)
UIElement e;
struct UIMDIChild *active;
int cascade;
} UIMDIClient;
typedef struct UIMDIChild {
#define UI_MDI_CHILD_CLOSE_BUTTON (1 << 0)
UIElement e;
UIRectangle bounds;
char *title;
ptrdiff_t titleBytes;
int dragHitTest;
UIRectangle dragOffset;
} UIMDIChild;
typedef struct UIExpandPane {
UIElement e;
UIButton *button;
UIPanel *panel;
bool expanded;
} UIExpandPane;
typedef struct UIImageDisplay {
#define UI_IMAGE_DISPLAY_INTERACTIVE (1 << 0)
#define _UI_IMAGE_DISPLAY_ZOOM_FIT (1 << 1)
UIElement e;
uint32_t *bits;
int width, height;
float panX, panY, zoom;
// Internals:
int previousWidth, previousHeight;
int previousPanPointX, previousPanPointY;
} UIImageDisplay;
typedef struct UIWrapPanel {
UIElement e;
} UIWrapPanel;
typedef struct UISwitcher {
UIElement e;
UIElement *active;
} UISwitcher;
void UIInitialise();
int UIMessageLoop();
UIElement *UIElementCreate(size_t bytes, UIElement *parent, uint32_t flags,
int (*messageClass)(UIElement *, UIMessage, int, void *), const char *cClassName);
UICheckbox *UICheckboxCreate(UIElement *parent, uint32_t flags, const char *label, ptrdiff_t labelBytes);
UIExpandPane *UIExpandPaneCreate(UIElement *parent, uint32_t flags, const char *label, ptrdiff_t labelBytes, uint32_t panelFlags);
UIMDIClient *UIMDIClientCreate(UIElement *parent, uint32_t flags);
UIMDIChild *UIMDIChildCreate(UIElement *parent, uint32_t flags, UIRectangle initialBounds, const char *title, ptrdiff_t titleBytes);
UIPanel *UIPanelCreate(UIElement *parent, uint32_t flags);
UIScrollBar *UIScrollBarCreate(UIElement *parent, uint32_t flags);
UISlider *UISliderCreate(UIElement *parent, uint32_t flags);
UISpacer *UISpacerCreate(UIElement *parent, uint32_t flags, int width, int height);
UISplitPane *UISplitPaneCreate(UIElement *parent, uint32_t flags, float weight);
UITabPane *UITabPaneCreate(UIElement *parent, uint32_t flags, const char *tabs /* separate with \t, terminate with \0 */);
UIWrapPanel *UIWrapPanelCreate(UIElement *parent, uint32_t flags);
UIGauge *UIGaugeCreate(UIElement *parent, uint32_t flags);
void UIGaugeSetPosition(UIGauge *gauge, float value);
UIButton *UIButtonCreate(UIElement *parent, uint32_t flags, const char *label, ptrdiff_t labelBytes);
void UIButtonSetLabel(UIButton *button, const char *string, ptrdiff_t stringBytes);
UILabel *UILabelCreate(UIElement *parent, uint32_t flags, const char *label, ptrdiff_t labelBytes);
void UILabelSetContent(UILabel *code, const char *content, ptrdiff_t byteCount);
UIImageDisplay *UIImageDisplayCreate(UIElement *parent, uint32_t flags, uint32_t *bits, size_t width, size_t height, size_t stride);
void UIImageDisplaySetContent(UIImageDisplay *display, uint32_t *bits, size_t width, size_t height, size_t stride);
UISwitcher *UISwitcherCreate(UIElement *parent, uint32_t flags);
void UISwitcherSwitchTo(UISwitcher *switcher, UIElement *child);
UIWindow *UIWindowCreate(UIWindow *owner, uint32_t flags, const char *cTitle, int width, int height);
void UIWindowRegisterShortcut(UIWindow *window, UIShortcut shortcut);
void UIWindowPostMessage(UIWindow *window, UIMessage message, void *dp); // Thread-safe.
void UIWindowPack(UIWindow *window, int width); // Change the size of the window to best match its contents.
typedef void (*UIDialogUserCallback)(UIElement *);
const char *UIDialogShow(UIWindow *window, uint32_t flags, const char *format, ...);
UIMenu *UIMenuCreate(UIElement *parent, uint32_t flags);
void UIMenuAddItem(UIMenu *menu, uint32_t flags, const char *label, ptrdiff_t labelBytes, void (*invoke)(void *cp), void *cp);
void UIMenuShow(UIMenu *menu);
bool UIMenusOpen();
UITextbox *UITextboxCreate(UIElement *parent, uint32_t flags);
void UITextboxReplace(UITextbox *textbox, const char *text, ptrdiff_t bytes, bool sendChangedMessage);
void UITextboxClear(UITextbox *textbox, bool sendChangedMessage);
void UITextboxMoveCaret(UITextbox *textbox, bool backward, bool word);
char *UITextboxToCString(UITextbox *textbox); // Free with UI_FREE.
UITable *UITableCreate(UIElement *parent, uint32_t flags, const char *columns /* separate with \t, terminate with \0 */);
int UITableHitTest(UITable *table, int x, int y); // Returns item index. Returns -1 if not on an item.
int UITableHeaderHitTest(UITable *table, int x, int y); // Returns column index or -1.
bool UITableEnsureVisible(UITable *table, int index); // Returns false if the item was already visible.
void UITableResizeColumns(UITable *table);
UICode *UICodeCreate(UIElement *parent, uint32_t flags);
void UICodeFocusLine(UICode *code, int index); // Line numbers are 1-indexed!!
int UICodeHitTest(UICode *code, int x, int y); // Returns line number; negates if in margin. Returns 0 if not on a line.
void UICodePositionToByte(UICode *code, int x, int y, int *line, int *byte);
void UICodeInsertContent(UICode *code, const char *content, ptrdiff_t byteCount, bool replace);
void UICodeMoveCaret(UICode *code, bool backward, bool word);
void UIDrawBlock(UIPainter *painter, UIRectangle rectangle, uint32_t color);
void UIDrawCircle(UIPainter *painter, int centerX, int centerY, int radius, uint32_t fillColor, uint32_t outlineColor, bool hollow);
void UIDrawControl(UIPainter *painter, UIRectangle bounds, uint32_t mode /* UI_DRAW_CONTROL_* */, const char *label, ptrdiff_t labelBytes, double position, float scale);
void UIDrawControlDefault(UIPainter *painter, UIRectangle bounds, uint32_t mode, const char *label, ptrdiff_t labelBytes, double position, float scale);
void UIDrawInvert(UIPainter *painter, UIRectangle rectangle);
bool UIDrawLine(UIPainter *painter, int x0, int y0, int x1, int y1, uint32_t color); // Returns false if the line was not visible.
void UIDrawTriangle(UIPainter *painter, int x0, int y0, int x1, int y1, int x2, int y2, uint32_t color);
void UIDrawTriangleOutline(UIPainter *painter, int x0, int y0, int x1, int y1, int x2, int y2, uint32_t color);
void UIDrawGlyph(UIPainter *painter, int x, int y, int c, uint32_t color);
void UIDrawRectangle(UIPainter *painter, UIRectangle r, uint32_t mainColor, uint32_t borderColor, UIRectangle borderSize);
void UIDrawBorder(UIPainter *painter, UIRectangle r, uint32_t borderColor, UIRectangle borderSize);
void UIDrawString(UIPainter *painter, UIRectangle r, const char *string, ptrdiff_t bytes, uint32_t color, int align, UIStringSelection *selection);
int UIDrawStringHighlighted(UIPainter *painter, UIRectangle r, const char *string, ptrdiff_t bytes, int tabSize, UIStringSelection *selection); // Returns final x position.
int UIMeasureStringWidth(const char *string, ptrdiff_t bytes);
int UIMeasureStringHeight();
uint64_t UIAnimateClock(); // In ms.
bool UIElementAnimate(UIElement *element, bool stop);
void UIElementDestroy(UIElement *element);
void UIElementDestroyDescendents(UIElement *element);
UIElement *UIElementFindByPoint(UIElement *element, int x, int y);
void UIElementFocus(UIElement *element);
UIRectangle UIElementScreenBounds(UIElement *element); // Returns bounds of element in same coordinate system as used by UIWindowCreate.
void UIElementRefresh(UIElement *element);
void UIElementRelayout(UIElement *element);
void UIElementRepaint(UIElement *element, UIRectangle *region);
void UIElementMeasurementsChanged(UIElement *element, int which);
void UIElementMove(UIElement *element, UIRectangle bounds, bool alwaysLayout);
int UIElementMessage(UIElement *element, UIMessage message, int di, void *dp);
UIElement *UIElementChangeParent(UIElement *element, UIElement *newParent, UIElement *insertBefore); // Set insertBefore to null to insert at the end. Returns the element it was before in its previous parent, or NULL.
UIElement *UIParentPush(UIElement *element);
UIElement *UIParentPop();
UIRectangle UIRectangleIntersection(UIRectangle a, UIRectangle b);
UIRectangle UIRectangleBounding(UIRectangle a, UIRectangle b);
UIRectangle UIRectangleAdd(UIRectangle a, UIRectangle b);
UIRectangle UIRectangleTranslate(UIRectangle a, UIRectangle b);
UIRectangle UIRectangleCenter(UIRectangle parent, UIRectangle child);
UIRectangle UIRectangleFit(UIRectangle parent, UIRectangle child, bool allowScalingUp);
bool UIRectangleEquals(UIRectangle a, UIRectangle b);
bool UIRectangleContains(UIRectangle a, int x, int y);
bool UIColorToHSV(uint32_t rgb, float *hue, float *saturation, float *value);
void UIColorToRGB(float hue, float saturation, float value, uint32_t *rgb);
char *UIStringCopy(const char *in, ptrdiff_t inBytes);
UIFont *UIFontCreate(const char *cPath, uint32_t size);
UIFont *UIFontActivate(UIFont *font); // Returns the previously active font.
#ifdef UI_DEBUG
void UIInspectorLog(const char *cFormat, ...);
#endif
ptrdiff_t _UIStringLength(const char *cString) {
if (!cString) return 0;
ptrdiff_t length;
for (length = 0; cString[length]; length++);
return length;
}
#ifdef UI_UNICODE
#ifndef UI_FREETYPE
#error "Unicode support requires Freetype"
#endif
#define _UNICODE_MAX_CODEPOINT 0x10FFFF
int Utf8GetCodePoint(const char *cString, ptrdiff_t bytesLength, ptrdiff_t *bytesConsumed) {
UI_ASSERT(bytesLength > 0 && "Attempted to get UTF-8 code point from an empty string");
if (bytesConsumed == NULL) {
ptrdiff_t bytesConsumed;
return Utf8GetCodePoint(cString, bytesLength, &bytesConsumed);
}
ptrdiff_t numExtraBytes;
uint8_t first = cString[0];
*bytesConsumed = 1;
if ((first & 0xF0) == 0xF0) {
numExtraBytes = 3;
} else if ((first & 0xE0) == 0xE0) {
numExtraBytes = 2;
} else if ((first & 0xC0) == 0xC0) {
numExtraBytes = 1;
} else if (first & 0x7F) {
return first & 0x80 ? -1 : first;
} else {
return -1;
}
if (bytesLength < numExtraBytes + 1) {
return -1;
}
int codePoint = ((int)first & (0x3F >> numExtraBytes)) << (6 * numExtraBytes);
for (ptrdiff_t idx = 1; idx < numExtraBytes + 1; idx++) {
char byte = cString[idx];
if ((byte & 0xC0) != 0x80) {
return -1;
}
codePoint |= (byte & 0x3F) << (6 * (numExtraBytes - idx));
(*bytesConsumed)++;
}
return codePoint > _UNICODE_MAX_CODEPOINT ? -1 : codePoint;
}
char * Utf8GetPreviousChar(char *string, char *offset) {
if (string == offset) {
return string;
}
char *prev = offset - 1;
while (prev > string) {
if ((*prev & 0xC0) == 0x80) prev--;
else break;
}
return prev;
}
ptrdiff_t Utf8GetCharBytes(const char *cString, ptrdiff_t bytes) {
if (!cString) {
return 0;
}
if (bytes == -1) {
bytes = _UIStringLength(cString);
}
ptrdiff_t bytesConsumed;
Utf8GetCodePoint(cString, bytes, &bytesConsumed);
return bytesConsumed;
}
ptrdiff_t Utf8StringLength(const char *cString, ptrdiff_t bytes) {
if (!cString) {
return 0;
}
if (bytes == -1) {
bytes = _UIStringLength(cString);
}
ptrdiff_t length = 0;
ptrdiff_t byteIndex = 0;
while (byteIndex < bytes) {
ptrdiff_t bytesConsumed;
Utf8GetCodePoint(cString+ byteIndex, bytes - byteIndex, &bytesConsumed);
byteIndex += bytesConsumed;
length++;
UI_ASSERT(byteIndex <= bytes && "Overran the end of the string while counting the number of UTF-8 code points");
}
return length;
}
#define _UI_ADVANCE_CHAR(index, text, count) \
index += Utf8GetCharBytes(text, count - index)
#define _UI_SKIP_TAB(ti, text, bytesLeft, tabSize) do { \
int c = Utf8GetCodePoint(text, bytesLeft, NULL); \
if (c == '\t') while (ti % tabSize) ti++; \
} while (0)
#define _UI_MOVE_CARET_BACKWARD(caret, text, offset, offset2) do { \
char *prev = Utf8GetPreviousChar(text, text + offset); \
caret = prev - text - offset2; \
} while (0)
#define _UI_MOVE_CARET_FORWARD(caret, text, bytes, offset) do { \
caret += Utf8GetCharBytes(text + caret, bytes - offset); \
} while (0)
#define _UI_MOVE_CARET_BY_WORD(text, bytes, offset) { \
char *prev = Utf8GetPreviousChar(text, text + offset); \
int c1 = Utf8GetCodePoint(prev, bytes - (prev - text), NULL); \
int c2 = Utf8GetCodePoint(text + offset, bytes - offset, NULL); \
if (_UICharIsAlphaOrDigitOrUnderscore(c1) != _UICharIsAlphaOrDigitOrUnderscore(c2)) break; \
}
#else
#define _UI_ADVANCE_CHAR(index, code, count) index++
#define _UI_SKIP_TAB(ti, text, bytesLeft, tabSize) \
if (*(text) == '\t') while (ti % tabSize) ti++
#define _UI_MOVE_CARET_BACKWARD(caret, text, offset, offset2) caret--
#define _UI_MOVE_CARET_FORWARD(caret, text, bytes, offset) caret++
#define _UI_MOVE_CARET_BY_WORD(text, bytes, offset) { \
char c1 = (text)[offset - 1]; \
char c2 = (text)[offset]; \
if (_UICharIsAlphaOrDigitOrUnderscore(c1) != _UICharIsAlphaOrDigitOrUnderscore(c2)) break; \
}
#endif // UI_UNICODE
#ifdef UI_IMPLEMENTATION
/////////////////////////////////////////
// Global variables.
/////////////////////////////////////////
struct {
UIWindow *windows;
UITheme theme;
UIElement **animating;
uint32_t animatingCount;
UIElement *parentStack[16];