|
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 <cx/utils.h> |
|
42 #include <cx/string.h> |
|
43 #include <cx/printf.h> |
|
44 |
|
45 #include <pthread.h> |
|
46 |
|
47 #ifndef UI_GTK2 |
|
48 static GtkApplication *app; |
|
49 #endif |
|
50 |
|
51 static char *application_name; |
|
52 |
|
53 static ui_callback startup_func; |
|
54 static void *startup_data; |
|
55 static ui_callback open_func; |
|
56 void *open_data; |
|
57 static ui_callback exit_func; |
|
58 void *exit_data; |
|
59 |
|
60 static ui_callback appclose_fnc; |
|
61 static void *appclose_udata; |
|
62 |
|
63 static UiObject *active_window; |
|
64 |
|
65 static int scale_factor = 1; |
|
66 |
|
67 void ui_init(char *appname, int argc, char **argv) { |
|
68 uic_init_global_context(); |
|
69 |
|
70 gtk_init(&argc, &argv); |
|
71 application_name = appname; |
|
72 |
|
73 uic_docmgr_init(); |
|
74 ui_toolbar_init(); |
|
75 |
|
76 // init custom types |
|
77 ui_list_init(); |
|
78 |
|
79 ui_image_init(); |
|
80 |
|
81 uic_load_app_properties(); |
|
82 |
|
83 #ifdef UI_SUPPORTS_SCALE |
|
84 scale_factor = gdk_monitor_get_scale_factor( |
|
85 gdk_display_get_primary_monitor(gdk_display_get_default())); |
|
86 #endif |
|
87 } |
|
88 |
|
89 char* ui_appname() { |
|
90 return application_name; |
|
91 } |
|
92 |
|
93 void ui_onstartup(ui_callback f, void *userdata) { |
|
94 startup_func = f; |
|
95 startup_data = userdata; |
|
96 } |
|
97 |
|
98 void ui_onopen(ui_callback f, void *userdata) { |
|
99 open_func = f; |
|
100 open_data = userdata; |
|
101 } |
|
102 |
|
103 void ui_onexit(ui_callback f, void *userdata) { |
|
104 exit_func = f; |
|
105 exit_data = userdata; |
|
106 } |
|
107 |
|
108 |
|
109 #ifndef UI_GTK2 |
|
110 static void app_startup(GtkApplication* app, gpointer userdata) { |
|
111 if(startup_func) { |
|
112 startup_func(NULL, startup_data); |
|
113 } |
|
114 } |
|
115 |
|
116 static void app_activate(GtkApplication* app, gpointer userdata) { |
|
117 printf("activate\n"); |
|
118 } |
|
119 #endif |
|
120 |
|
121 void ui_main() { |
|
122 #ifndef UI_GTK2 |
|
123 cxmutstr appid = cx_asprintf( |
|
124 "ui.%s", |
|
125 application_name ? application_name : "application1"); |
|
126 |
|
127 app = gtk_application_new( |
|
128 appid.ptr, |
|
129 G_APPLICATION_FLAGS_NONE); |
|
130 g_signal_connect (app, "startup", G_CALLBACK (app_startup), NULL); |
|
131 g_signal_connect (app, "activate", G_CALLBACK (app_activate), NULL); |
|
132 g_application_run(G_APPLICATION (app), 0, NULL); |
|
133 g_object_unref (app); |
|
134 |
|
135 free(appid.ptr); |
|
136 #else |
|
137 if(startup_func) { |
|
138 startup_func(NULL, startup_data); |
|
139 } |
|
140 gtk_main(); |
|
141 #endif |
|
142 if(exit_func) { |
|
143 exit_func(NULL, exit_data); |
|
144 } |
|
145 uic_store_app_properties(); |
|
146 } |
|
147 |
|
148 #ifndef UI_GTK2 |
|
149 void ui_app_quit() { |
|
150 g_application_quit(G_APPLICATION(app)); |
|
151 } |
|
152 |
|
153 GtkApplication* ui_get_application() { |
|
154 return app; |
|
155 } |
|
156 #endif |
|
157 |
|
158 void ui_show(UiObject *obj) { |
|
159 uic_check_group_widgets(obj->ctx); |
|
160 gtk_widget_show_all(obj->widget); |
|
161 } |
|
162 |
|
163 void ui_close(UiObject *obj) { |
|
164 gtk_widget_destroy(obj->widget); |
|
165 } |
|
166 |
|
167 |
|
168 static gboolean ui_job_finished(void *data) { |
|
169 UiJob *job = data; |
|
170 |
|
171 UiEvent event; |
|
172 event.obj = job->obj; |
|
173 event.window = job->obj->window; |
|
174 event.document = job->obj->ctx->document; |
|
175 event.intval = 0; |
|
176 event.eventdata = NULL; |
|
177 |
|
178 job->finish_callback(&event, job->finish_data); |
|
179 free(job); |
|
180 return FALSE; |
|
181 } |
|
182 |
|
183 static void* ui_jobthread(void *data) { |
|
184 UiJob *job = data; |
|
185 int result = job->job_func(job->job_data); |
|
186 if(!result) { |
|
187 g_idle_add(ui_job_finished, job); |
|
188 } |
|
189 return NULL; |
|
190 } |
|
191 |
|
192 void ui_job(UiObject *obj, ui_threadfunc tf, void *td, ui_callback f, void *fd) { |
|
193 UiJob *job = malloc(sizeof(UiJob)); |
|
194 job->obj = obj; |
|
195 job->job_func = tf; |
|
196 job->job_data = td; |
|
197 job->finish_callback = f; |
|
198 job->finish_data = fd; |
|
199 pthread_t pid; |
|
200 pthread_create(&pid, NULL, ui_jobthread, job); |
|
201 } |
|
202 |
|
203 void ui_set_enabled(UIWIDGET widget, int enabled) { |
|
204 gtk_widget_set_sensitive(widget, enabled); |
|
205 } |
|
206 |
|
207 void ui_set_show_all(UIWIDGET widget, int value) { |
|
208 gtk_widget_set_no_show_all(widget, !value); |
|
209 } |
|
210 |
|
211 void ui_set_visible(UIWIDGET widget, int visible) { |
|
212 if(visible) { |
|
213 gtk_widget_set_no_show_all(widget, FALSE); |
|
214 gtk_widget_show_all(widget); |
|
215 } else { |
|
216 gtk_widget_hide(widget); |
|
217 } |
|
218 } |
|
219 |
|
220 void ui_clipboard_set(char *str) { |
|
221 GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); |
|
222 gtk_clipboard_set_text(cb, str, strlen(str)); |
|
223 } |
|
224 |
|
225 char* ui_clipboard_get() { |
|
226 GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); |
|
227 char *str = gtk_clipboard_wait_for_text(cb); |
|
228 if(str) { |
|
229 char *copy = strdup(str); |
|
230 g_free(str); |
|
231 return copy; |
|
232 } else { |
|
233 return NULL; |
|
234 } |
|
235 } |
|
236 |
|
237 int ui_get_scalefactor() { |
|
238 return scale_factor; |
|
239 } |
|
240 |
|
241 void ui_destroy_userdata(GtkWidget *object, void *userdata) { |
|
242 free(userdata); |
|
243 } |
|
244 |
|
245 void ui_destroy_vardata(GtkWidget *object, UiVarEventData *data) { |
|
246 ui_destroy_boundvar(data->obj->ctx, data->var); |
|
247 free(data); |
|
248 } |
|
249 |
|
250 void ui_destroy_boundvar(UiContext *ctx, UiVar *var) { |
|
251 uic_unbind_var(var); |
|
252 |
|
253 if(var->type == UI_VAR_SPECIAL) { |
|
254 free(var); |
|
255 } else { |
|
256 ui_free(var->from_ctx, var); |
|
257 // TODO: free or unbound |
|
258 //uic_remove_bound_var(ctx, var); |
|
259 } |
|
260 } |
|
261 |
|
262 void ui_set_active_window(UiObject *obj) { |
|
263 active_window = obj; |
|
264 } |
|
265 |
|
266 UiObject *ui_get_active_window() { |
|
267 return active_window; |
|
268 } |
|
269 |
|
270 |