2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2014 Olaf Wintermann. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
37 #include "../common/document.h"
38 #include "../common/properties.h"
39 #include <ucx/buffer.h>
41 static XtAppContext app;
42 static Display *display;
43 static Widget active_window;
44 static char *application_name;
46 static ui_callback startup_func;
47 static void *startup_data;
48 static ui_callback open_func;
50 static ui_callback exit_func;
53 static ui_callback appclose_fnc;
54 static void *appclose_udata;
56 static int is_toplevel_realized = 0;
61 static String fallback[] = {
62 //"*fontList: -dt-interface system-medium-r-normal-s*utf*:",
63 "*text_area*renderTable: f1",
64 "*f1*fontType: FONT_IS_XFT",
65 "*f1*fontName: Monospace",
68 "*rt*fontType: FONT_IS_XFT",
74 void input_proc(XtPointer data, int *source, XtInputId *iid) {
76 read(event_pipe[0], &ptr, sizeof(void*));
79 void ui_init(char *appname, int argc, char **argv) {
80 application_name = appname;
82 XtToolkitInitialize();
83 XtSetLanguageProc(NULL, NULL, NULL);
84 app = XtCreateApplicationContext();
85 XtAppSetFallbackResources(app, fallback);
87 display = XtOpenDisplay(app, NULL, appname, appname, NULL, 0, &argc, argv);
88 char **missing = NULL;
91 XCreateFontSet(display, "-dt-interface system-medium-r-normal-s*utf*", &missing, &nm, &def);
97 uic_load_app_properties();
99 if(pipe(event_pipe)) {
100 fprintf(stderr, "UiError: Cannot create event pipe\n");
106 (XtPointer)XtInputReadMask,
112 return application_name;
115 Display* ui_get_display() {
119 void ui_onstartup(ui_callback f, void *userdata) {
121 startup_data = userdata;
124 void ui_onopen(ui_callback f, void *userdata) {
126 open_data = userdata;
129 void ui_onexit(ui_callback f, void *userdata) {
131 exit_data = userdata;
136 startup_func(NULL, startup_data);
140 exit_func(NULL, exit_data);
142 uic_store_app_properties();
145 void ui_exit_mainloop() {
146 XtAppSetExitFlag(app);
149 void ui_secondary_event_loop(int *loop) {
150 while(*loop && !XtAppGetExitFlag(app)) {
152 XtAppNextEvent(app, &event);
153 XtDispatchEvent(&event);
157 void ui_show(UiObject *obj) {
158 uic_check_group_widgets(obj->ctx);
159 XtRealizeWidget(obj->widget);
160 ui_window_dark_theme(XtDisplay(obj->widget), XtWindow(obj->widget)); // TODO: if
163 // implemented in window.c
164 //void ui_close(UiObject *obj)
166 void ui_set_enabled(UIWIDGET widget, int enabled) {
167 XtSetSensitive(widget, enabled);
170 void ui_set_show_all(UIWIDGET widget, int value) {
172 XtUnmanageChild(widget);
176 void ui_set_visible(UIWIDGET widget, int visible) {
178 XtManageChild(widget);
180 XtUnmanageChild(widget);
184 static Boolean ui_job_finished(void *data) {
185 printf("WorkProc\n");
189 event.obj = job->obj;
190 event.window = job->obj->window;
191 event.document = job->obj->ctx->document;
193 event.eventdata = NULL;
195 job->finish_callback(&event, job->finish_data);
200 static void* ui_jobthread(void *data) {
202 int result = job->job_func(job->job_data);
204 printf("XtAppAddWorkProc\n");
205 write(event_pipe[1], &job, sizeof(void*)); // hack
206 XtAppAddWorkProc(app, ui_job_finished, job);
211 void ui_job(UiObject *obj, ui_threadfunc tf, void *td, ui_callback f, void *fd) {
212 UiJob *job = malloc(sizeof(UiJob));
216 job->finish_callback = f;
217 job->finish_data = fd;
219 pthread_create(&pid, NULL, ui_jobthread, job);
222 void ui_clipboard_set(char *str) {
223 printf("copy: {%s}\n", str);
224 int length = strlen(str) + 1;
226 Display *dp = XtDisplayOfObject(active_window);
227 Window window = XtWindowOfObject(active_window);
229 XmString label = XmStringCreateLocalized("toolkit_clipboard");
232 while(XmClipboardStartCopy(
239 &id) == ClipboardLocked);
242 while(XmClipboardCopy(
250 NULL) == ClipboardLocked);
252 while(XmClipboardEndCopy(dp, window, id) == ClipboardLocked);
255 char* ui_clipboard_get() {
256 Display *dp = XtDisplayOfObject(active_window);
257 Window window = XtWindowOfObject(active_window);
261 char *buf = malloc(size);
265 r = XmClipboardRetrieve(dp, window, "STRING", buf, size, NULL, &id);
266 if(r == ClipboardSuccess) {
268 } else if(r == ClipboardTruncate) {
270 buf = realloc(buf, size);
271 } else if(r == ClipboardNoData) {
281 void ui_set_active_window(Widget w) {
285 Widget ui_get_active_window() {
286 return active_window;
289 void ui_window_dark_theme(Display *dp, Window window) {
290 Atom atom = XInternAtom(dp, "_GTK_THEME_VARIANT", False);
291 Atom type = XInternAtom(dp, "UTF8_STRING", False);
299 (const unsigned char*)"dark",