ui/gtk/toolkit.c

changeset 44
473954dc6b74
parent 42
9af327d0e0e4
child 45
ab71409644b0
equal deleted inserted replaced
43:ef01d2c90128 44:473954dc6b74
44 #include <cx/string.h> 44 #include <cx/string.h>
45 #include <cx/printf.h> 45 #include <cx/printf.h>
46 46
47 #include <pthread.h> 47 #include <pthread.h>
48 48
49 #ifndef UI_GTK2 49 #ifdef UI_APPLICATION
50 static GtkApplication *app; 50 UI_APPLICATION app;
51 #endif 51 #endif
52 52
53 static const char *application_name; 53 static const char *application_name;
54 54
55 static ui_callback startup_func; 55 static ui_callback startup_func;
65 static UiObject *active_window; 65 static UiObject *active_window;
66 66
67 static int scale_factor = 1; 67 static int scale_factor = 1;
68 68
69 UIEXPORT void ui_init(const char *appname, int argc, char **argv) { 69 UIEXPORT void ui_init(const char *appname, int argc, char **argv) {
70 application_name = appname;
70 uic_init_global_context(); 71 uic_init_global_context();
71 72
73 #if GTK_MAJOR_VERSION >= 4
74 gtk_init();
75 #else
72 gtk_init(&argc, &argv); 76 gtk_init(&argc, &argv);
73 application_name = appname; 77 #endif
74 78
75 ui_css_init(); 79 ui_css_init();
76
77 uic_docmgr_init(); 80 uic_docmgr_init();
78
79 uic_toolbar_init(); 81 uic_toolbar_init();
80
81 ui_image_init(); 82 ui_image_init();
82
83 uic_load_app_properties(); 83 uic_load_app_properties();
84 84
85 #ifdef UI_SUPPORTS_SCALE 85 #if GTK_MAJOR_VERSION >= 4
86 scale_factor = 1; // TODO
87 #elif defined(UI_SUPPORTS_SCALE)
86 scale_factor = gdk_monitor_get_scale_factor( 88 scale_factor = gdk_monitor_get_scale_factor(
87 gdk_display_get_primary_monitor(gdk_display_get_default())); 89 gdk_display_get_primary_monitor(gdk_display_get_default()));
88 #endif 90 #endif
89 } 91 }
90 92
119 printf("activate\n"); 121 printf("activate\n");
120 } 122 }
121 #endif 123 #endif
122 124
123 void ui_main() { 125 void ui_main() {
124 #ifndef UI_GTK2 126 #ifdef UI_APPLICATION
125 cxmutstr appid = cx_asprintf( 127 cxmutstr appid = cx_asprintf(
126 "ui.%s", 128 "ui.%s",
127 application_name ? application_name : "application1"); 129 application_name ? application_name : "application1");
128 130 app = UI_APPLICATION_NEW(appid.ptr);
129 app = gtk_application_new(
130 appid.ptr,
131 G_APPLICATION_FLAGS_NONE);
132 g_signal_connect (app, "startup", G_CALLBACK (app_startup), NULL); 131 g_signal_connect (app, "startup", G_CALLBACK (app_startup), NULL);
133 g_signal_connect (app, "activate", G_CALLBACK (app_activate), NULL); 132 g_signal_connect (app, "activate", G_CALLBACK (app_activate), NULL);
134 g_application_run(G_APPLICATION (app), 0, NULL); 133 g_application_run(G_APPLICATION (app), 0, NULL);
135 g_object_unref (app); 134 g_object_unref (app);
136 135
151 void ui_app_quit() { 150 void ui_app_quit() {
152 g_application_quit(G_APPLICATION(app)); 151 g_application_quit(G_APPLICATION(app));
153 } 152 }
154 153
155 GtkApplication* ui_get_application() { 154 GtkApplication* ui_get_application() {
156 return app; 155 return GTK_APPLICATION(app);
157 } 156 }
158 #endif 157 #endif
159 158
160 void ui_show(UiObject *obj) { 159 void ui_show(UiObject *obj) {
161 uic_check_group_widgets(obj->ctx); 160 uic_check_group_widgets(obj->ctx);
161 #if GTK_MAJOR_VERSION >= 4
162 gtk_window_present(GTK_WINDOW(obj->widget));
163 #elif GTK_MAJOR_VERSION <= 3
162 gtk_widget_show_all(obj->widget); 164 gtk_widget_show_all(obj->widget);
165 #endif
163 } 166 }
164 167
165 void ui_close(UiObject *obj) { 168 void ui_close(UiObject *obj) {
166 gtk_widget_destroy(obj->widget); 169 // TODO
170 //gtk_widget_destroy(obj->widget);
167 } 171 }
168 172
169 173
170 static gboolean ui_job_finished(void *data) { 174 static gboolean ui_job_finished(void *data) {
171 UiJob *job = data; 175 UiJob *job = data;
224 void ui_set_enabled(UIWIDGET widget, int enabled) { 228 void ui_set_enabled(UIWIDGET widget, int enabled) {
225 gtk_widget_set_sensitive(widget, enabled); 229 gtk_widget_set_sensitive(widget, enabled);
226 } 230 }
227 231
228 void ui_set_show_all(UIWIDGET widget, int value) { 232 void ui_set_show_all(UIWIDGET widget, int value) {
233 // TODO: gtk4
234 #if GTK_MAJOR_VERSION <= 3
229 gtk_widget_set_no_show_all(widget, !value); 235 gtk_widget_set_no_show_all(widget, !value);
236 #endif
230 } 237 }
231 238
232 void ui_set_visible(UIWIDGET widget, int visible) { 239 void ui_set_visible(UIWIDGET widget, int visible) {
240 // TODO: gtk4
241 #if GTK_MAJOR_VERSION <= 3
233 if(visible) { 242 if(visible) {
234 gtk_widget_set_no_show_all(widget, FALSE); 243 gtk_widget_set_no_show_all(widget, FALSE);
235 gtk_widget_show_all(widget); 244 gtk_widget_show_all(widget);
236 } else { 245 } else {
237 gtk_widget_hide(widget); 246 gtk_widget_hide(widget);
238 } 247 }
248 #endif
239 } 249 }
240 250
241 void ui_clipboard_set(char *str) { 251 void ui_clipboard_set(char *str) {
252 #if GTK_MAJOR_VERSION >= 4
253 // TODO: gtk4: needs widget
254 #else
242 GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); 255 GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
243 gtk_clipboard_set_text(cb, str, strlen(str)); 256 gtk_clipboard_set_text(cb, str, strlen(str));
257 #endif
244 } 258 }
245 259
246 char* ui_clipboard_get() { 260 char* ui_clipboard_get() {
261 #if GTK_MAJOR_VERSION >= 4
262 // TODO: gtk4: needs widget
263 return NULL;
264 #else
247 GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); 265 GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
248 char *str = gtk_clipboard_wait_for_text(cb); 266 char *str = gtk_clipboard_wait_for_text(cb);
249 if(str) { 267 if(str) {
250 char *copy = strdup(str); 268 char *copy = strdup(str);
251 g_free(str); 269 g_free(str);
252 return copy; 270 return copy;
253 } else { 271 } else {
254 return NULL; 272 return NULL;
255 } 273 }
274 #endif
256 } 275 }
257 276
258 int ui_get_scalefactor() { 277 int ui_get_scalefactor() {
259 return scale_factor; 278 return scale_factor;
260 } 279 }
293 312
294 #if GTK_MAJOR_VERSION >= 3 313 #if GTK_MAJOR_VERSION >= 3
295 314
296 static GtkCssProvider* ui_gtk_css_provider; 315 static GtkCssProvider* ui_gtk_css_provider;
297 316
317 #if GTK_MAJOR_VERSION == 4
318 static const char *ui_gtk_css =
319 "#path-textfield-box {\n"
320 " background-color: alpha(currentColor, 0.1);"
321 " border-radius: 6px;"
322 " padding: 0px;"
323 "}\n"
324 ".pathbar-extra-button {\n"
325 " border-top-right-radius: 6px;"
326 " border-bottom-right-radius: 6px;"
327 " border-top-left-radius: 0px;"
328 " border-bottom-left-radius: 0px;"
329 "}\n"
330 "#pathbar button {\n"
331 " margin: 3px;"
332 " border-radius: 4px;"
333 " padding-top: 0px;"
334 " padding-bottom: 0px;"
335 " padding-left: 8px;"
336 " padding-right: 8px;"
337 "}\n"
338 "#path-textfield-box entry {\n"
339 " background-color: #00000000;"
340 " border-top-left-radius: 6px;"
341 " border-bottom-left-radius: 6px;"
342 " border-top-right-radius: 0px;"
343 " border-bottom-right-radius: 0px;"
344 "}"
345 ;
346
347 #elif GTK_MAJOR_VERSION == 3
298 static const char *ui_gtk_css = 348 static const char *ui_gtk_css =
299 "#path-textfield-box {" 349 "#path-textfield-box {"
300 " background-color: @theme_base_color;" 350 " background-color: @theme_base_color;"
301 " border-radius: 5px;" 351 " border-radius: 5px;"
302 " padding: 0px;" 352 " padding: 0px;"
303 "}"; 353 "}";
354 #endif
304 355
305 void ui_css_init(void) { 356 void ui_css_init(void) {
306 ui_gtk_css_provider = gtk_css_provider_new(); 357 ui_gtk_css_provider = gtk_css_provider_new();
307 358
308 #ifdef UI_GTK3 359 #ifdef UI_GTK3
323 #else 374 #else
324 gtk_css_provider_load_from_string(ui_gtk_css_provider, ui_gtk_css); 375 gtk_css_provider_load_from_string(ui_gtk_css_provider, ui_gtk_css);
325 #endif /* GTK_MINOR_VERSION < 12 */ 376 #endif /* GTK_MINOR_VERSION < 12 */
326 377
327 GdkDisplay *display = gdk_display_get_default(); 378 GdkDisplay *display = gdk_display_get_default();
328 gtk_style_context_add_provider_for_display(display, GTK_STYLE_PROVIDER(ui_gtk_css_provider), GTK_STYLE_PROVIDER_PRIORITY_USER); 379 gtk_style_context_add_provider_for_display(display, GTK_STYLE_PROVIDER(ui_gtk_css_provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
329 380
330 #endif /* UI_GTK4 */ 381 #endif /* UI_GTK4 */
331 } 382 }
332 383
333 384

mercurial