summaryrefslogtreecommitdiff
path: root/new/unix/window.c
blob: 039feb7e74867f4ccf4797c4a7de880b7eb7bd42 (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
// 6 april 2015
#include "uipriv_unix.h"

struct window {
	uiWindow w;
	GtkWidget *widget;
	uiParent *content;
	int (*onClosing)(uiWindow *, void *);
	void *onClosingData;
	int margined;
};

static gboolean onClosing(GtkWidget *win, GdkEvent *e, gpointer data)
{
	struct window *w = (struct window *) data;

	// return exact values just in case
	if ((*(w->onClosing))(uiWindow(w), w->onClosingData))
		return FALSE;
	return TRUE;
}

static int defaultOnClosing(uiWindow *w, void *data)
{
	return 1;
}

static void onDestroy(GtkWidget *widget, gpointer data)
{
	struct window *w = (struct window *) data;

	uiFree(w);
}

static void windowDestroy(uiWindow *ww)
{
	struct window *w = (struct window *) ww;

	gtk_widget_destroy(w->widget);
}

static uintptr_t handle(uiWindow *ww)
{
	struct window *w = (struct window *) ww;

	return (uintptr_t) (w->widget);
}

static char *getTitle(uiWindow *ww)
{
	struct window *w = (struct window *) ww;

	return g_strdup(gtk_window_get_title(GTK_WINDOW(w->widget)));
}

static void setTitle(uiWindow *ww, const char *title)
{
	struct window *w = (struct window *) ww;

	gtk_window_set_title(GTK_WINDOW(w->widget), title);
}

static void show(uiWindow *ww)
{
	struct window *w = (struct window *) ww;

	// don't use gtk_widget_show_all(); that will override user hidden settings
	gtk_widget_show(w->widget);
}

static void hide(uiWindow *ww)
{
	struct window *w = (struct window *) ww;
	gtk_widget_hide(w->widget);
}

static void setOnClosing(uiWindow *ww, int (*f)(uiWindow *, void *), void *data)
{
	struct window *w = (struct window *) ww;

	w->onClosing = f;
	w->onClosingData = data;
}

static void setChild(uiWindow *ww, uiControl *c)
{
	struct window *w = (struct window *) ww;

	uiParentSetChild(w->content, c);
	uiParentUpdate(w->content);
}

static int margined(uiWindow *ww)
{
	struct window *w = (struct window *) ww;

	return w->margined;
}

static void setMargined(uiWindow *ww, int margined)
{
	struct window *w = (struct window *) ww;

	w->margined = margined;
	if (w->margined)
		uiParentSetMargins(w->content, gtkXMargin, gtkYMargin, gtkXMargin, gtkYMargin);
	else
		uiParentSetMargins(w->content, 0, 0, 0, 0);
	uiParentUpdate(w->content);
}

uiWindow *uiNewWindow(const char *title, int width, int height)
{
	struct window *w;

	w = uiNew(struct window);
	w->widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(w->widget), title);
	gtk_window_resize(GTK_WINDOW(w->widget), width, height);
	g_signal_connect(w->widget, "delete-event", G_CALLBACK(onClosing), w);
	g_signal_connect(w->widget, "destroy", G_CALLBACK(onDestroy), w);
	w->content = uiNewParent((uintptr_t) (w->widget));
	w->onClosing = defaultOnClosing;

	uiWindow(w)->Destroy = windowDestroy;
	uiWindow(w)->Handle = handle;
	uiWindow(w)->Title = getTitle;
	uiWindow(w)->SetTitle = setTitle;
	uiWindow(w)->Show = show;
	uiWindow(w)->Hide = hide;
	uiWindow(w)->OnClosing = setOnClosing;
	uiWindow(w)->SetChild = setChild;
	uiWindow(w)->Margined = margined;
	uiWindow(w)->SetMargined = setMargined;

	return uiWindow(w);
}