ui/gtk/toolkit.c

changeset 0
804d8803eade
child 2
ea89bbb0c4c8
equal deleted inserted replaced
-1:000000000000 0:804d8803eade
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2017 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdbool.h>
33
34 #include "toolkit.h"
35 #include "toolbar.h"
36 #include "model.h"
37 #include "image.h"
38 #include "../common/document.h"
39 #include "../common/properties.h"
40
41 #include <ucx/utils.h>
42
43 #include <pthread.h>
44
45 #ifndef UI_GTK2
46 static GtkApplication *app;
47 #endif
48
49 static char *application_name;
50
51 static ui_callback startup_func;
52 static void *startup_data;
53 static ui_callback open_func;
54 void *open_data;
55 static ui_callback exit_func;
56 void *exit_data;
57
58 static ui_callback appclose_fnc;
59 static void *appclose_udata;
60
61 static UiObject *active_window;
62
63 static int scale_factor = 1;
64
65 void ui_init(char *appname, int argc, char **argv) {
66 gtk_init(&argc, &argv);
67 application_name = appname;
68
69 uic_docmgr_init();
70 ui_toolbar_init();
71
72 // init custom types
73 ui_list_init();
74
75 ui_image_init();
76
77 uic_load_app_properties();
78
79 #ifdef UI_SUPPORTS_SCALE
80 scale_factor = gdk_monitor_get_scale_factor(
81 gdk_display_get_primary_monitor(gdk_display_get_default()));
82 #endif
83 }
84
85 char* ui_appname() {
86 return application_name;
87 }
88
89 void ui_onstartup(ui_callback f, void *userdata) {
90 startup_func = f;
91 startup_data = userdata;
92 }
93
94 void ui_onopen(ui_callback f, void *userdata) {
95 open_func = f;
96 open_data = userdata;
97 }
98
99 void ui_onexit(ui_callback f, void *userdata) {
100 exit_func = f;
101 exit_data = userdata;
102 }
103
104
105 #ifndef UI_GTK2
106 static void app_startup(GtkApplication* app, gpointer userdata) {
107 if(startup_func) {
108 startup_func(NULL, startup_data);
109 }
110 }
111
112 static void app_activate(GtkApplication* app, gpointer userdata) {
113 printf("activate\n");
114 }
115 #endif
116
117 void ui_main() {
118 #ifndef UI_GTK2
119 sstr_t appid = ucx_sprintf(
120 "ui.%s",
121 application_name ? application_name : "application1");
122
123 app = gtk_application_new(
124 appid.ptr,
125 G_APPLICATION_FLAGS_NONE);
126 g_signal_connect (app, "startup", G_CALLBACK (app_startup), NULL);
127 g_signal_connect (app, "activate", G_CALLBACK (app_activate), NULL);
128 g_application_run(G_APPLICATION (app), 0, NULL);
129 g_object_unref (app);
130
131 free(appid.ptr);
132 #else
133 if(startup_func) {
134 startup_func(NULL, startup_data);
135 }
136 gtk_main();
137 #endif
138 if(exit_func) {
139 exit_func(NULL, exit_data);
140 }
141 uic_store_app_properties();
142 }
143
144 #ifndef UI_GTK2
145 void ui_app_quit() {
146 g_application_quit(G_APPLICATION(app));
147 }
148
149 GtkApplication* ui_get_application() {
150 return app;
151 }
152 #endif
153
154 void ui_show(UiObject *obj) {
155 uic_check_group_widgets(obj->ctx);
156 gtk_widget_show_all(obj->widget);
157 }
158
159 void ui_close(UiObject *obj) {
160 gtk_widget_destroy(obj->widget);
161 }
162
163
164 static gboolean ui_job_finished(void *data) {
165 UiJob *job = data;
166
167 UiEvent event;
168 event.obj = job->obj;
169 event.window = job->obj->window;
170 event.document = job->obj->ctx->document;
171 event.intval = 0;
172 event.eventdata = NULL;
173
174 job->finish_callback(&event, job->finish_data);
175 free(job);
176 return FALSE;
177 }
178
179 static void* ui_jobthread(void *data) {
180 UiJob *job = data;
181 int result = job->job_func(job->job_data);
182 if(!result) {
183 g_idle_add(ui_job_finished, job);
184 }
185 return NULL;
186 }
187
188 void ui_job(UiObject *obj, ui_threadfunc tf, void *td, ui_callback f, void *fd) {
189 UiJob *job = malloc(sizeof(UiJob));
190 job->obj = obj;
191 job->job_func = tf;
192 job->job_data = td;
193 job->finish_callback = f;
194 job->finish_data = fd;
195 pthread_t pid;
196 pthread_create(&pid, NULL, ui_jobthread, job);
197 }
198
199 void ui_set_enabled(UIWIDGET widget, int enabled) {
200 gtk_widget_set_sensitive(widget, enabled);
201 }
202
203 void ui_set_show_all(UIWIDGET widget, int value) {
204 gtk_widget_set_no_show_all(widget, !value);
205 }
206
207 void ui_set_visible(UIWIDGET widget, int visible) {
208 if(visible) {
209 gtk_widget_set_no_show_all(widget, FALSE);
210 gtk_widget_show_all(widget);
211 } else {
212 gtk_widget_hide(widget);
213 }
214 }
215
216 void ui_clipboard_set(char *str) {
217 GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
218 gtk_clipboard_set_text(cb, str, strlen(str));
219 }
220
221 char* ui_clipboard_get() {
222 GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
223 char *str = gtk_clipboard_wait_for_text(cb);
224 if(str) {
225 char *copy = strdup(str);
226 g_free(str);
227 return copy;
228 } else {
229 return NULL;
230 }
231 }
232
233 int ui_get_scalefactor() {
234 return scale_factor;
235 }
236
237 void ui_destroy_userdata(GtkWidget *object, void *userdata) {
238 free(userdata);
239 }
240
241 void ui_destroy_vardata(GtkWidget *object, UiVarEventData *data) {
242 ui_destroy_boundvar(data->obj->ctx, data->var);
243 free(data);
244 }
245
246 void ui_destroy_boundvar(UiContext *ctx, UiVar *var) {
247 if(var->type == UI_VAR_SPECIAL) {
248 free(var);
249 } else {
250 uic_remove_bound_var(ctx, var);
251 }
252 }
253
254 void ui_set_active_window(UiObject *obj) {
255 active_window = obj;
256 }
257
258 UiObject *ui_get_active_window() {
259 return active_window;
260 }
261
262

mercurial