-
Notifications
You must be signed in to change notification settings - Fork 612
/
main.c
201 lines (163 loc) · 4.46 KB
/
main.c
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
// 10 june 2017
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "../../ui.h"
// TODO the grid simply flat out does not work on OS X
uiWindow *mainwin;
uiFontButton *fontButton;
uiEntry *textEntry;
uiCheckbox *nullFeatures;
uiArea *area;
uiAttributedString *attrstr = NULL;
static void remakeAttrStr(void)
{
char *text;
uiOpenTypeFeatures *otf;
uiAttributeSpec spec;
if (attrstr != NULL)
uiFreeAttributedString(attrstr);
text = uiEntryText(textEntry);
attrstr = uiNewAttributedString(text);
uiFreeText(text);
if (!uiCheckboxChecked(nullFeatures)) {
otf = uiNewOpenTypeFeatures();
// TODO
spec.Type = uiAttributeFeatures;
spec.Features = otf;
uiAttributedStringSetAttribute(attrstr, &spec,
0, uiAttributedStringLen(attrstr));
// and uiAttributedString copied otf
uiFreeOpenTypeFeatures(otf);
}
uiAreaQueueRedrawAll(area);
}
// TODO make a variable of main()? in all programs?
static uiAreaHandler handler;
static void handlerDraw(uiAreaHandler *a, uiArea *area, uiAreaDrawParams *p)
{
uiDrawTextLayout *layout;
uiDrawTextLayoutParams lp;
uiDrawFontDescriptor desc;
memset(&lp, 0, sizeof (uiDrawTextLayoutParams));
lp.String = attrstr;
uiFontButtonFont(fontButton, &desc);
lp.DefaultFont = &desc;
lp.Width = p->AreaWidth;
lp.Align = uiDrawTextAlignLeft;
layout = uiDrawNewTextLayout(&lp);
uiDrawText(p->Context, layout, 0, 0);
uiDrawFreeTextLayout(layout);
}
static void handlerMouseEvent(uiAreaHandler *a, uiArea *area, uiAreaMouseEvent *e)
{
// do nothing
}
static void handlerMouseCrossed(uiAreaHandler *ah, uiArea *a, int left)
{
// do nothing
}
static void handlerDragBroken(uiAreaHandler *ah, uiArea *a)
{
// do nothing
}
static int handlerKeyEvent(uiAreaHandler *ah, uiArea *a, uiAreaKeyEvent *e)
{
// reject all keys
return 0;
}
static void onFontChanged(uiFontButton *b, void *data)
{
remakeAttrStr();
}
static void onTextChanged(uiEntry *e, void *data)
{
remakeAttrStr();
}
static void onNULLToggled(uiCheckbox *c, void *data)
{
remakeAttrStr();
}
static int onClosing(uiWindow *w, void *data)
{
// TODO change the others to be like this? (the others destroy here rather than later)
// TODO move this below uiQuit()?
uiControlHide(uiControl(w));
uiQuit();
return 0;
}
static int shouldQuit(void *data)
{
uiControlDestroy(uiControl(mainwin));
return 1;
}
int main(void)
{
uiInitOptions o;
const char *err;
uiGrid *grid;
uiBox *vbox;
handler.Draw = handlerDraw;
handler.MouseEvent = handlerMouseEvent;
handler.MouseCrossed = handlerMouseCrossed;
handler.DragBroken = handlerDragBroken;
handler.KeyEvent = handlerKeyEvent;
memset(&o, 0, sizeof (uiInitOptions));
err = uiInit(&o);
if (err != NULL) {
fprintf(stderr, "error initializing ui: %s\n", err);
uiFreeInitError(err);
return 1;
}
uiOnShouldQuit(shouldQuit, NULL);
// TODO 800x600? the size of the GTK+ example?
mainwin = uiNewWindow("libui OpenType Features Example", 640, 480, 1);
uiWindowSetMargined(mainwin, 1);
uiWindowOnClosing(mainwin, onClosing, NULL);
grid = uiNewGrid();
uiGridSetPadded(grid, 1);
uiWindowSetChild(mainwin, uiControl(grid));
fontButton = uiNewFontButton();
uiFontButtonOnChanged(fontButton, onFontChanged, NULL);
uiGridAppend(grid, uiControl(fontButton),
0, 0, 1, 1,
// TODO are these Y values correct?
0, uiAlignFill, 0, uiAlignCenter);
textEntry = uiNewEntry();
uiEntrySetText(textEntry, "afford afire aflight");
uiEntryOnChanged(textEntry, onTextChanged, NULL);
uiGridAppend(grid, uiControl(textEntry),
1, 0, 1, 1,
// TODO are these Y values correct too?
// TODO add a baseline align? or a form align?
1, uiAlignFill, 0, uiAlignCenter);
vbox = uiNewVerticalBox();
uiBoxSetPadded(vbox, 1);
uiGridAppend(grid, uiControl(vbox),
0, 1, 1, 1,
0, uiAlignFill, 1, uiAlignFill);
nullFeatures = uiNewCheckbox("NULL uiOpenTypeFeatures");
uiCheckboxOnToggled(nullFeatures, onNULLToggled, NULL);
uiBoxAppend(vbox, uiControl(nullFeatures), 0);
// TODO separator (if other stuff isn't a tab)
// TODO needed for this to be testable on os x without rewriting everything again
{
int x;
for (x = 0; x < 10; x++)
uiBoxAppend(vbox, uiControl(uiNewEntry()), 0);
}
// TODO other stuff
area = uiNewArea(&handler);
uiGridAppend(grid, uiControl(area),
1, 1, 1, 1,
1, uiAlignFill, 1, uiAlignFill);
// and set up the initial draw
remakeAttrStr();
uiControlShow(uiControl(mainwin));
uiMain();
uiControlDestroy(uiControl(mainwin));
uiFreeAttributedString(attrstr);
uiUninit();
return 0;
}