|
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 |
|
33 #include "../ui/window.h" |
|
34 #include "../ui/properties.h" |
|
35 #include "../common/context.h" |
|
36 |
|
37 #include <cx/basic_mempool.h> |
|
38 |
|
39 #include "menu.h" |
|
40 #include "toolbar.h" |
|
41 #include "container.h" |
|
42 |
|
43 static int nwindows = 0; |
|
44 |
|
45 static int window_default_width = 650; |
|
46 static int window_default_height = 550; |
|
47 |
|
48 void ui_exit_event(GtkWidget *widget, gpointer data) { |
|
49 UiObject *obj = data; |
|
50 UiEvent ev; |
|
51 ev.window = obj->window; |
|
52 ev.document = obj->ctx->document; |
|
53 ev.obj = obj; |
|
54 ev.eventdata = NULL; |
|
55 ev.intval = 0; |
|
56 |
|
57 if(obj->ctx->close_callback) { |
|
58 obj->ctx->close_callback(&ev, obj->ctx->close_data); |
|
59 } |
|
60 // TODO: free UiObject |
|
61 |
|
62 nwindows--; |
|
63 #ifdef UI_GTK2 |
|
64 if(nwindows == 0) { |
|
65 gtk_main_quit(); |
|
66 } |
|
67 #endif |
|
68 } |
|
69 |
|
70 static UiObject* create_window(char *title, void *window_data, UiBool simple) { |
|
71 CxMempool *mp = cxBasicMempoolCreate(256); |
|
72 UiObject *obj = cxCalloc(mp->allocator, 1, sizeof(UiObject)); |
|
73 |
|
74 #ifndef UI_GTK2 |
|
75 obj->widget = gtk_application_window_new(ui_get_application()); |
|
76 #else |
|
77 obj->widget = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
|
78 #endif |
|
79 |
|
80 |
|
81 obj->ctx = uic_context(obj, mp->allocator); |
|
82 obj->window = window_data; |
|
83 |
|
84 if(title != NULL) { |
|
85 gtk_window_set_title(GTK_WINDOW(obj->widget), title); |
|
86 } |
|
87 |
|
88 char *width = ui_get_property("ui.window.width"); |
|
89 char *height = ui_get_property("ui.window.height"); |
|
90 if(width && height) { |
|
91 gtk_window_set_default_size( |
|
92 GTK_WINDOW(obj->widget), |
|
93 atoi(width), |
|
94 atoi(height)); |
|
95 } else { |
|
96 gtk_window_set_default_size( |
|
97 GTK_WINDOW(obj->widget), |
|
98 window_default_width, |
|
99 window_default_height); |
|
100 } |
|
101 |
|
102 g_signal_connect( |
|
103 obj->widget, |
|
104 "destroy", |
|
105 G_CALLBACK(ui_exit_event), |
|
106 obj); |
|
107 |
|
108 GtkWidget *vbox = ui_gtk_vbox_new(0); |
|
109 gtk_container_add(GTK_CONTAINER(obj->widget), vbox); |
|
110 |
|
111 if(!simple) { |
|
112 // menu |
|
113 GtkWidget *mb = ui_create_menubar(obj); |
|
114 if(mb) { |
|
115 gtk_box_pack_start(GTK_BOX(vbox), mb, FALSE, FALSE, 0); |
|
116 } |
|
117 |
|
118 // toolbar |
|
119 GtkWidget *tb = ui_create_toolbar(obj); |
|
120 if(tb) { |
|
121 gtk_box_pack_start(GTK_BOX(vbox), tb, FALSE, FALSE, 0); |
|
122 } |
|
123 } |
|
124 |
|
125 // window content |
|
126 // the content has a (TODO: not yet) configurable frame |
|
127 GtkWidget *frame = gtk_frame_new(NULL); |
|
128 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE); |
|
129 gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 0); |
|
130 |
|
131 // content vbox |
|
132 GtkWidget *content_box = ui_gtk_vbox_new(0); |
|
133 gtk_container_add(GTK_CONTAINER(frame), content_box); |
|
134 obj->container = ui_box_container(obj, content_box); |
|
135 |
|
136 nwindows++; |
|
137 return obj; |
|
138 } |
|
139 |
|
140 |
|
141 UiObject* ui_window(char *title, void *window_data) { |
|
142 return create_window(title, window_data, FALSE); |
|
143 } |
|
144 |
|
145 UiObject* ui_simplewindow(char *title, void *window_data) { |
|
146 return create_window(title, window_data, TRUE); |
|
147 } |
|
148 |
|
149 static char* ui_gtkfilechooser(UiObject *obj, GtkFileChooserAction action) { |
|
150 char *button; |
|
151 char *title; |
|
152 |
|
153 if(action == GTK_FILE_CHOOSER_ACTION_OPEN) { |
|
154 button = GTK_STOCK_OPEN; |
|
155 title = "Datei öffnen..."; |
|
156 } else { |
|
157 button = GTK_STOCK_SAVE; |
|
158 title = "Datei speichern..."; |
|
159 } |
|
160 |
|
161 GtkWidget *dialog = gtk_file_chooser_dialog_new( |
|
162 title, |
|
163 GTK_WINDOW(obj->widget), |
|
164 action, |
|
165 GTK_STOCK_CANCEL, |
|
166 GTK_RESPONSE_CANCEL, |
|
167 button, |
|
168 GTK_RESPONSE_ACCEPT, |
|
169 NULL); |
|
170 if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { |
|
171 char *file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); |
|
172 gtk_widget_destroy(dialog); |
|
173 char *copy = strdup(file); |
|
174 g_free(file); |
|
175 return copy; |
|
176 } else { |
|
177 gtk_widget_destroy(dialog); |
|
178 return NULL; |
|
179 } |
|
180 } |
|
181 |
|
182 char* ui_openfiledialog(UiObject *obj) { |
|
183 return ui_gtkfilechooser(obj, GTK_FILE_CHOOSER_ACTION_OPEN); |
|
184 } |
|
185 |
|
186 char* ui_savefiledialog(UiObject *obj) { |
|
187 return ui_gtkfilechooser(obj, GTK_FILE_CHOOSER_ACTION_SAVE); |
|
188 } |
|
189 |