ui/gtk/window.c

changeset 44
473954dc6b74
parent 32
e5f4d8af567e
child 45
ab71409644b0
equal deleted inserted replaced
43:ef01d2c90128 44:473954dc6b74
39 #include <cx/mempool.h> 39 #include <cx/mempool.h>
40 40
41 #include "menu.h" 41 #include "menu.h"
42 #include "toolbar.h" 42 #include "toolbar.h"
43 #include "container.h" 43 #include "container.h"
44 #include "headerbar.h"
44 45
45 static int nwindows = 0; 46 static int nwindows = 0;
46 47
47 static int window_default_width = 650; 48 static int window_default_width = 650;
48 static int window_default_height = 550; 49 static int window_default_height = 550;
70 } 71 }
71 72
72 static UiObject* create_window(const char *title, void *window_data, UiBool simple) { 73 static UiObject* create_window(const char *title, void *window_data, UiBool simple) {
73 CxMempool *mp = cxBasicMempoolCreate(256); 74 CxMempool *mp = cxBasicMempoolCreate(256);
74 UiObject *obj = cxCalloc(mp->allocator, 1, sizeof(UiObject)); 75 UiObject *obj = cxCalloc(mp->allocator, 1, sizeof(UiObject));
75 76
76 #ifndef UI_GTK2 77 #ifdef UI_LIBADWAITA
78 obj->widget = adw_application_window_new(ui_get_application());
79 #elif !defined(UI_GTK2)
77 obj->widget = gtk_application_window_new(ui_get_application()); 80 obj->widget = gtk_application_window_new(ui_get_application());
78 #else 81 #else
79 obj->widget = gtk_window_new(GTK_WINDOW_TOPLEVEL); 82 obj->widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
80 #endif 83 #endif
81 84
82 85
83 obj->ctx = uic_context(obj, mp); 86 obj->ctx = uic_context(obj, mp);
84 obj->window = window_data; 87 obj->window = window_data;
88
89 #if GTK_CHECK_VERSION(4, 0, 0)
90 obj->ctx->action_map = G_ACTION_MAP(obj->widget);
91 #endif
85 92
86 if(title != NULL) { 93 if(title != NULL) {
87 gtk_window_set_title(GTK_WINDOW(obj->widget), title); 94 gtk_window_set_title(GTK_WINDOW(obj->widget), title);
88 } 95 }
89 96
106 "destroy", 113 "destroy",
107 G_CALLBACK(ui_exit_event), 114 G_CALLBACK(ui_exit_event),
108 obj); 115 obj);
109 116
110 GtkWidget *vbox = ui_gtk_vbox_new(0); 117 GtkWidget *vbox = ui_gtk_vbox_new(0);
118 #ifdef UI_LIBADWAITA
119 GtkWidget *toolbar_view = adw_toolbar_view_new();
120 adw_application_window_set_content(ADW_APPLICATION_WINDOW(obj->widget), toolbar_view);
121 adw_toolbar_view_set_content(ADW_TOOLBAR_VIEW(toolbar_view), vbox);
122
123 GtkWidget *headerbar = adw_header_bar_new();
124 adw_toolbar_view_add_top_bar(ADW_TOOLBAR_VIEW(toolbar_view), headerbar);
125
126 ui_fill_headerbar(obj, headerbar);
127 #elif GTK_MAJOR_VERSION >= 4
128 WINDOW_SET_CONTENT(obj->widget, vbox);
129 #else
111 gtk_container_add(GTK_CONTAINER(obj->widget), vbox); 130 gtk_container_add(GTK_CONTAINER(obj->widget), vbox);
112 131
113 if(!simple) { 132 if(!simple) {
114 // menu 133 // menu
115 if(uic_get_menu_list()) { 134 if(uic_get_menu_list()) {
128 } 147 }
129 148
130 //GtkWidget *hb = ui_create_headerbar(obj); 149 //GtkWidget *hb = ui_create_headerbar(obj);
131 //gtk_window_set_titlebar(GTK_WINDOW(obj->widget), hb); 150 //gtk_window_set_titlebar(GTK_WINDOW(obj->widget), hb);
132 } 151 }
152 #endif
133 153
134 // window content 154 // window content
135 // the content has a (TODO: not yet) configurable frame 155 // the content has a (TODO: not yet) configurable frame
156 // TODO: really? why
157 /*
136 GtkWidget *frame = gtk_frame_new(NULL); 158 GtkWidget *frame = gtk_frame_new(NULL);
137 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE); 159 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE);
138 gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 0); 160 gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 0);
139 161
140 // content vbox 162 // content vbox
141 GtkWidget *content_box = ui_gtk_vbox_new(0); 163 GtkWidget *content_box = ui_gtk_vbox_new(0);
142 gtk_container_add(GTK_CONTAINER(frame), content_box); 164 gtk_container_add(GTK_CONTAINER(frame), content_box);
143 obj->container = ui_box_container(obj, content_box); 165 obj->container = ui_box_container(obj, content_box);
166 */
167 GtkWidget *content_box = ui_gtk_vbox_new(0);
168 BOX_ADD(GTK_BOX(vbox), content_box);
169 obj->container = ui_box_container(obj, content_box, UI_CONTAINER_VBOX);
144 170
145 nwindows++; 171 nwindows++;
146 return obj; 172 return obj;
147 } 173 }
148 174
171 evt.eventdata = NULL; 197 evt.eventdata = NULL;
172 evt.intval = 0; 198 evt.intval = 0;
173 199
174 if(data->customdata) { 200 if(data->customdata) {
175 GtkWidget *entry = data->customdata; 201 GtkWidget *entry = data->customdata;
176 evt.eventdata = (void*)gtk_entry_get_text(GTK_ENTRY(entry)); 202 evt.eventdata = (void*)ENTRY_GET_TEXT(GTK_ENTRY(entry));
177 203
178 } 204 }
179 205
180 if(response_id == 1 || response_id == 2) { 206 if(response_id == 1 || response_id == 2) {
181 evt.intval = response_id; 207 evt.intval = response_id;
184 210
185 if(data->callback) { 211 if(data->callback) {
186 data->callback(&evt, data->userdata); 212 data->callback(&evt, data->userdata);
187 } 213 }
188 214
189 gtk_widget_destroy(GTK_WIDGET(self)); 215 WINDOW_DESTROY(GTK_WIDGET(self));
190 } 216 }
191 217
192 void ui_dialog_create(UiObject *parent, UiDialogArgs args) { 218 void ui_dialog_create(UiObject *parent, UiDialogArgs args) {
193 GtkDialog *dialog = GTK_DIALOG(gtk_dialog_new()); 219 GtkDialog *dialog = GTK_DIALOG(gtk_dialog_new());
194 GtkWidget *dialog_w = GTK_WIDGET(dialog); 220 GtkWidget *dialog_w = GTK_WIDGET(dialog);
206 } 232 }
207 233
208 GtkWidget *content_area = gtk_dialog_get_content_area(dialog); 234 GtkWidget *content_area = gtk_dialog_get_content_area(dialog);
209 if(args.content) { 235 if(args.content) {
210 GtkWidget *label = gtk_label_new(args.content); 236 GtkWidget *label = gtk_label_new(args.content);
211 gtk_container_add(GTK_CONTAINER(content_area), label); 237 BOX_ADD(content_area, label);
212 } 238 }
213 239
214 GtkWidget *textfield = NULL; 240 GtkWidget *textfield = NULL;
215 if(args.input) { 241 if(args.input) {
216 textfield = gtk_entry_new(); 242 textfield = gtk_entry_new();
217 gtk_container_add(GTK_CONTAINER(content_area), textfield); 243 BOX_ADD(content_area, textfield);
218 } 244 }
219 245
220 UiEventData *event = malloc(sizeof(UiEventData)); 246 UiEventData *event = malloc(sizeof(UiEventData));
221 event->obj = parent; 247 event->obj = parent;
222 event->callback = args.result; 248 event->callback = args.result;
227 g_signal_connect(dialog_w, 253 g_signal_connect(dialog_w,
228 "response", 254 "response",
229 G_CALLBACK(ui_dialog_response), 255 G_CALLBACK(ui_dialog_response),
230 event); 256 event);
231 257
232 gtk_widget_show_all(GTK_WIDGET(dialog_w)); 258 WINDOW_SHOW(GTK_WIDGET(dialog_w));
233 } 259 }
234 260
261
262 #if GTK_MAJOR_VERSION >= 4
263 static void ui_gtkfilechooser(UiObject *obj, GtkFileChooserAction action, unsigned int mode, ui_callback file_selected_callback, void *cbdata) {
264 // TODO
265 }
266 #else
235 static void ui_gtkfilechooser(UiObject *obj, GtkFileChooserAction action, unsigned int mode, ui_callback file_selected_callback, void *cbdata) { 267 static void ui_gtkfilechooser(UiObject *obj, GtkFileChooserAction action, unsigned int mode, ui_callback file_selected_callback, void *cbdata) {
236 char *button; 268 char *button;
237 char *title; 269 char *title;
238 270
239 GtkWidget *dialog; 271 GtkWidget *dialog;
270 } 302 }
271 303
272 if((mode & UI_FILEDIALOG_SELECT_MULTI) == UI_FILEDIALOG_SELECT_MULTI) { 304 if((mode & UI_FILEDIALOG_SELECT_MULTI) == UI_FILEDIALOG_SELECT_MULTI) {
273 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE); 305 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
274 } 306 }
275 307
276 UiEvent evt; 308 UiEvent evt;
277 evt.obj = obj; 309 evt.obj = obj;
278 evt.document = evt.obj->ctx->document; 310 evt.document = evt.obj->ctx->document;
279 evt.window = evt.obj->window; 311 evt.window = evt.obj->window;
280 evt.intval = 0; 312 evt.intval = 0;
308 free(flist.files); 340 free(flist.files);
309 g_slist_free(selection); 341 g_slist_free(selection);
310 342
311 gtk_widget_destroy(dialog); 343 gtk_widget_destroy(dialog);
312 } 344 }
345 #endif
313 346
314 void ui_openfiledialog(UiObject *obj, unsigned int mode, ui_callback file_selected_callback, void *cbdata) { 347 void ui_openfiledialog(UiObject *obj, unsigned int mode, ui_callback file_selected_callback, void *cbdata) {
315 ui_gtkfilechooser(obj, GTK_FILE_CHOOSER_ACTION_OPEN, mode, file_selected_callback, cbdata); 348 ui_gtkfilechooser(obj, GTK_FILE_CHOOSER_ACTION_OPEN, mode, file_selected_callback, cbdata);
316 } 349 }
317 350

mercurial