summaryrefslogtreecommitdiff
path: root/new/ui.h
blob: 2dd2a08e347e2e29243376719984d7e837df2093 (plain)
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
// 6 april 2015

#ifndef __UI_UI_H__
#define __UI_UI_H__

#include <stdint.h>

typedef struct uiInitOptions uiInitOptions;

// TODO note that should be initialized to zero
struct uiInitOptions {
	// TODO cbSize

	// If nonzero, allocations will be logged to stderr.
	// See leaks.awk.
	int debugLogAllocations;
};

const char *uiInit(uiInitOptions *);
void uiFreeInitError(const char *);

void uiMain(void);
void uiQuit(void);

void uiFreeText(char *);

typedef struct uiSizing uiSizing;
typedef struct uiSizingSys uiSizingSys;
struct uiSizing {
	intmax_t xPadding;
	intmax_t yPadding;
	uiSizingSys *sys;
};

typedef struct uiParent uiParent;

typedef struct uiControl uiControl;
struct uiControl {
	void *data;			// for use by implementations only
	void *internal;			// for use by ui only
	void (*destroy)(uiControl *);
	uintptr_t (*handle)(uiControl *);
	void (*setParent)(uiControl *, uiParent *);
	void (*preferredSize)(uiControl *, uiSizing *, intmax_t *, intmax_t *);
	void (*resize)(uiControl *, intmax_t, intmax_t, intmax_t, intmax_t, uiSizing *);
	int (*visible)(uiControl *);
	void (*show)(uiControl *);
	void (*hide)(uiControl *);
	void (*containerShow)(uiControl *);
	void (*containerHide)(uiControl *);
	void (*enable)(uiControl *);
	void (*disable)(uiControl *);
	void (*containerEnable)(uiControl *);
	void (*containerDisable)(uiControl *);
};
void uiControlDestroy(uiControl *);
uintptr_t uiControlHandle(uiControl *);
void uiControlSetParent(uiControl *, uiParent *);
void uiControlPreferredSize(uiControl *, uiSizing *, intmax_t *width, intmax_t *height);
void uiControlResize(uiControl *, intmax_t, intmax_t, intmax_t, intmax_t, uiSizing *);
int uiControlVisible(uiControl *);
void uiControlShow(uiControl *);
void uiControlHide(uiControl *);
void uiControlContainerShow(uiControl *);
void uiControlContainerHide(uiControl *);
void uiControlEnable(uiControl *);
void uiControlDisable(uiControl *);
void uiControlContainerEnable(uiControl *);
void uiControlContainerDisable(uiControl *);

// uiParent represents an OS control that hosts other OS controls.
// It is used internally by package ui and by implementations.
// uiWindow, uiTab, and uiGroup all use uiParents to store their controls.
struct uiParent {
	// Internal points to internal data.
	// Do not access or alter this field.
	void *Internal;

	// TODO destroy
	// TODO object destruction debug handler

	// Handle returns the window handle of the uiParent.
	// On Windows, this is a HWND.
	// On GTK+, this is a GtkContainer.
	// On Mac OS X, this is a NSView.
	uintptr_t (*Handle)(uiParent *p);
#define uiParentHandle(p) ((*((p)->Handle))((p)))

	// SetChild sets the uiControl that this uiParent relegates.
	// It calls uiControl.SetParent() which should, in turn, call uiParent.Update().
	// The uiParent should already not have a child and the uiControl should already not have a parent.
	// 
	// child can be NULL, in which case the uiParent has no children.
	// This version should also call uiControl.SetParent(), passing NULL.
	// 
	// If this uiParent has a child already, then the current child is replaced with the new one.
	void (*SetChild)(uiParent *p, uiControl *child);
#define uiParentSetChild(p, child) ((*((p)->SetChild))((p), (child)))

	// SetMargins sets the margins of the uiParent to the given margins.
	// It does not call uiParent.Update(); its caller must.
	// The units of the margins are backend-defined.
	// The initial margins are all 0.
	void (*SetMargins)(uiParent *p, intmax_t left, intmax_t top, intmax_t right, intmax_t bottom);
#define uiParentSetMargins(p, left, top, right, bottom) ((*((p)->SetMargins))((p), (left), (top), (right), (bottom)))

	// TODO Resize?

	// Update tells the uiParent to re-layout its children immediately.
	// It is called when a widget is shown or hidden or when a control is added or removed from a container such as uiStack.
	void (*Update)(uiParent *p);
#define uiParentUpdate(p) ((*((p)->Update))((p)))
};
uiParent *uiNewParent(uintptr_t);

typedef struct uiWindow uiWindow;
uiWindow *uiNewWindow(char *, int, int);
void uiWindowDestroy(uiWindow *);
uintptr_t uiWindowHandle(uiWindow *);
char *uiWindowTitle(uiWindow *);
void uiWindowSetTitle(uiWindow *, const char *);
void uiWindowShow(uiWindow *);
void uiWindowHide(uiWindow *);
void uiWindowOnClosing(uiWindow *, int (*)(uiWindow *, void *), void *);
void uiWindowSetChild(uiWindow *, uiControl *);
int uiWindowMargined(uiWindow *);
void uiWindowSetMargined(uiWindow *, int);

uiControl *uiNewButton(const char *);
char *uiButtonText(uiControl *);
void uiButtonSetText(uiControl *, const char *);
void uiButtonOnClicked(uiControl *, void (*)(uiControl *, void *), void *);

uiControl *uiNewHorizontalStack(void);
uiControl *uiNewVerticalStack(void);
void uiStackAdd(uiControl *, uiControl *, int);
void uiStackRemove(uiControl *, uintmax_t);
int uiStackPadded(uiControl *);
void uiStackSetPadded(uiControl *, int);

uiControl *uiNewEntry(void);
char *uiEntryText(uiControl *);
void uiEntrySetText(uiControl *, const char *);

uiControl *uiNewCheckbox(const char *);
char *uiCheckboxText(uiControl *);
void uiCheckboxSetText(uiControl *, const char *);
void uiCheckboxOnToggled(uiControl *, void (*)(uiControl *, void *), void *);
int uiCheckboxChecked(uiControl *);
void uiCheckboxSetChecked(uiControl *, int);

uiControl *uiNewLabel(const char *);
char *uiLabelText(uiControl *);
void uiLabelSetText(uiControl *, const char *);

uiControl *uiNewTab(void);
void uiTabAddPage(uiControl *, const char *, uiControl *);

#endif