2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2017 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.
33 #include "../ui/window.h"
34 #include "../ui/properties.h"
35 #include "../common/context.h"
39 #include "container.h"
41 static int nwindows = 0;
43 static int window_default_width = 650;
44 static int window_default_height = 550;
46 void ui_exit_event(GtkWidget *widget, gpointer data) {
49 ev.window = obj->window;
50 ev.document = obj->ctx->document;
55 if(obj->ctx->close_callback) {
56 obj->ctx->close_callback(&ev, obj->ctx->close_data);
58 // TODO: free UiObject
68 static UiObject* create_window(char *title, void *window_data, UiBool simple) {
69 UcxMempool *mp = ucx_mempool_new(256);
70 UiObject *obj = ucx_mempool_calloc(mp, 1, sizeof(UiObject));
73 obj->widget = gtk_application_window_new(ui_get_application());
75 obj->widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
79 obj->ctx = uic_context(obj, mp);
80 obj->window = window_data;
83 gtk_window_set_title(GTK_WINDOW(obj->widget), title);
86 char *width = ui_get_property("ui.window.width");
87 char *height = ui_get_property("ui.window.height");
89 gtk_window_set_default_size(
90 GTK_WINDOW(obj->widget),
94 gtk_window_set_default_size(
95 GTK_WINDOW(obj->widget),
97 window_default_height);
103 G_CALLBACK(ui_exit_event),
106 GtkWidget *vbox = ui_gtk_vbox_new(0);
107 gtk_container_add(GTK_CONTAINER(obj->widget), vbox);
111 GtkWidget *mb = ui_create_menubar(obj);
113 gtk_box_pack_start(GTK_BOX(vbox), mb, FALSE, FALSE, 0);
117 GtkWidget *tb = ui_create_toolbar(obj);
119 gtk_box_pack_start(GTK_BOX(vbox), tb, FALSE, FALSE, 0);
124 // the content has a (TODO: not yet) configurable frame
125 GtkWidget *frame = gtk_frame_new(NULL);
126 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE);
127 gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 0);
130 GtkWidget *content_box = ui_gtk_vbox_new(0);
131 gtk_container_add(GTK_CONTAINER(frame), content_box);
132 obj->container = ui_box_container(obj, content_box);
139 UiObject* ui_window(char *title, void *window_data) {
140 return create_window(title, window_data, FALSE);
143 UiObject* ui_simplewindow(char *title, void *window_data) {
144 return create_window(title, window_data, TRUE);
147 static char* ui_gtkfilechooser(UiObject *obj, GtkFileChooserAction action) {
151 if(action == GTK_FILE_CHOOSER_ACTION_OPEN) {
152 button = GTK_STOCK_OPEN;
153 title = "Datei öffnen...";
155 button = GTK_STOCK_SAVE;
156 title = "Datei speichern...";
159 GtkWidget *dialog = gtk_file_chooser_dialog_new(
161 GTK_WINDOW(obj->widget),
168 if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
169 char *file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
170 gtk_widget_destroy(dialog);
171 char *copy = strdup(file);
175 gtk_widget_destroy(dialog);
180 char* ui_openfiledialog(UiObject *obj) {
181 return ui_gtkfilechooser(obj, GTK_FILE_CHOOSER_ACTION_OPEN);
184 char* ui_savefiledialog(UiObject *obj) {
185 return ui_gtkfilechooser(obj, GTK_FILE_CHOOSER_ACTION_SAVE);