Fri, 14 Jun 2024 18:24:31 +0200
implement ui_list_getselection (GTK)
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
4 | * Copyright 2017 Olaf Wintermann. All rights reserved. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15 | * |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
29 | #include <stdio.h> |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
30 | #include <stdlib.h> |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
31 | #include <string.h> |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
32 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
33 | #include "toolbar.h" |
32 | 34 | #include "menu.h" |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35 | #include "button.h" |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
36 | #include "image.h" |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
37 | #include "tree.h" |
29
3fc287f06305
add minimal working download
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
0
diff
changeset
|
38 | #include <cx/mempool.h> |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
39 | #include <cx/hash_map.h> |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
40 | #include <cx/linked_list.h> |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
41 | #include <cx/array_list.h> |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
42 | #include "../common/context.h" |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
43 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
44 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
45 | GtkWidget* ui_create_toolbar(UiObject *obj) { |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
46 | GtkWidget *toolbar = gtk_toolbar_new(); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
47 | #ifdef UI_GTK3 |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
48 | gtk_style_context_add_class( |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
49 | gtk_widget_get_style_context(toolbar), |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
50 | GTK_STYLE_CLASS_PRIMARY_TOOLBAR); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
51 | #endif |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
52 | |
32 | 53 | CxMap *items = uic_get_toolbar_items(); |
54 | CxList *left_defaults = uic_get_toolbar_defaults(UI_TOOLBAR_LEFT); | |
55 | CxList *center_defaults = uic_get_toolbar_defaults(UI_TOOLBAR_CENTER); | |
56 | CxList *right_defaults = uic_get_toolbar_defaults(UI_TOOLBAR_RIGHT); | |
57 | ||
58 | ui_toolbar_add_items(obj, toolbar, items, left_defaults); | |
59 | ui_toolbar_add_items(obj, toolbar, items, center_defaults); | |
60 | ui_toolbar_add_items(obj, toolbar, items, right_defaults); | |
61 | ||
62 | /* | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
63 | GtkToolbar *tb = GTK_TOOLBAR(toolbar); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
64 | CxIterator i = cxListIterator(defaults); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
65 | cx_foreach(char *, def, i) { |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
66 | UiToolItemI *item = cxMapGet(toolbar_items, def); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
67 | if(item) { |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
68 | item->add_to(tb, item, obj); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
69 | } else if(!strcmp(def, "@separator")) { |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
70 | gtk_toolbar_insert(tb, gtk_separator_tool_item_new(), -1); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
71 | } else { |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
72 | fprintf(stderr, "UI Error: Unknown toolbar item: %s\n", def); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
73 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
74 | } |
32 | 75 | */ |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
76 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
77 | return toolbar; |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
78 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
79 | |
32 | 80 | static void create_item(UiObject *obj, GtkWidget *toolbar, UiToolbarItemI *i) { |
81 | GtkToolbar *tb = GTK_TOOLBAR(toolbar); | |
82 | switch(i->type) { | |
83 | case UI_TOOLBAR_ITEM: { | |
84 | add_toolitem_widget(tb, (UiToolbarItem*)i, obj); | |
85 | break; | |
86 | } | |
87 | case UI_TOOLBAR_TOGGLEITEM: { | |
88 | add_toolitem_toggle_widget(tb, (UiToolbarToggleItem*)i, obj); | |
89 | break; | |
90 | } | |
91 | case UI_TOOLBAR_MENU: { | |
92 | add_toolitem_menu_widget(tb, (UiToolbarMenuItem*)i, obj); | |
93 | break; | |
94 | } | |
95 | default: fprintf(stderr, "toolbar item type unimplemented: %d\n", (int)i->type); | |
96 | } | |
97 | } | |
98 | ||
99 | void ui_toolbar_add_items(UiObject *obj, GtkWidget *toolbar, CxMap *items, CxList *defaults) { | |
100 | // add pre-configured items | |
101 | CxIterator i = cxListIterator(defaults); | |
102 | cx_foreach(char*, def, i) { | |
103 | UiToolbarItemI* item = uic_toolbar_get_item(def); | |
104 | if (!item) { | |
105 | fprintf(stderr, "unknown toolbar item: %s\n", def); | |
106 | continue; | |
107 | } | |
108 | create_item(obj, toolbar, item); | |
109 | } | |
110 | } | |
111 | ||
112 | static void set_toolbutton_icon(GtkToolItem *item, const char *icon_name) { | |
113 | #if GTK_MAJOR_VERSION >= 3 | |
114 | gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(item), icon_name); | |
115 | #else | |
116 | UiIcon *icon = ui_icon(icon_name, 24); | |
117 | if(icon) { | |
118 | GdkPixbuf *pixbuf = ui_icon_pixbuf(icon); | |
119 | if(pixbuf) { | |
120 | GtkWidget *image = gtk_image_new_from_pixbuf(pixbuf); | |
121 | gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(item), image); | |
122 | } | |
123 | } | |
124 | #endif | |
125 | } | |
126 | ||
127 | void add_toolitem_widget(GtkToolbar *tb, UiToolbarItem *item, UiObject *obj) { | |
128 | GtkToolItem *button; | |
129 | if(item->args.stockid) { | |
130 | #ifdef UI_GTK2 | |
131 | button = gtk_tool_button_new_from_stock(item->args.stockid); | |
132 | #else | |
133 | // TODO: gtk3 stock | |
134 | button = gtk_tool_button_new(NULL, item->args.label); | |
135 | #endif | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
136 | } else { |
32 | 137 | button = gtk_tool_button_new(NULL, item->args.label); |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
138 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
139 | |
32 | 140 | gtk_tool_item_set_homogeneous(button, FALSE); |
141 | if(item->args.icon) { | |
142 | set_toolbutton_icon(button, item->args.icon); | |
143 | } | |
144 | gtk_tool_item_set_is_important(button, TRUE); | |
145 | ||
146 | if(item->args.onclick) { | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
147 | UiEventData *event = cxMalloc( |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
148 | obj->ctx->allocator, |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
149 | sizeof(UiEventData)); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
150 | event->obj = obj; |
32 | 151 | event->callback = item->args.onclick; |
152 | event->userdata = item->args.onclickdata; | |
153 | event->customdata = NULL; | |
154 | event->value = 0; | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
155 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
156 | g_signal_connect( |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
157 | button, |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
158 | "clicked", |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
159 | G_CALLBACK(ui_button_clicked), |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
160 | event); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
161 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
162 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
163 | gtk_toolbar_insert(tb, button, -1); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
164 | |
32 | 165 | /* |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
166 | if(item->groups) { |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
167 | uic_add_group_widget(obj->ctx, button, (ui_enablefunc)ui_set_enabled, item->groups); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
168 | } |
32 | 169 | */ |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
170 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
171 | |
32 | 172 | void add_toolitem_toggle_widget(GtkToolbar *tb, UiToolbarToggleItem *item, UiObject *obj) { |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
173 | GtkToolItem *button; |
32 | 174 | if(item->args.stockid) { |
175 | #ifdef UI_GTK2 | |
176 | button = gtk_toggle_tool_button_new_from_stock(item->args.stockid); | |
177 | #else | |
178 | button = gtk_toggle_tool_button_new_from_stock(item->args.stockid); // TODO: gtk3 stock | |
179 | #endif | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
180 | } else { |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
181 | button = gtk_toggle_tool_button_new(); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
182 | gtk_tool_item_set_homogeneous(button, FALSE); |
32 | 183 | if(item->args.label) { |
184 | gtk_tool_button_set_label(GTK_TOOL_BUTTON(button), item->args.label); | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
185 | } |
32 | 186 | if(item->args.icon) { |
187 | set_toolbutton_icon(button, item->args.icon); | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
188 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
189 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
190 | |
32 | 191 | UiVar* var = uic_widget_var(obj->ctx, obj->ctx, NULL, item->args.varname, UI_VAR_INTEGER); |
192 | if(var) { | |
193 | UiInteger *i = (UiInteger*)var->value; | |
194 | if(i) { | |
195 | i->get = ui_tool_toggle_button_get; | |
196 | i->set = ui_tool_toggle_button_set; | |
197 | i->obj = button; | |
198 | ||
199 | if(i->value != 0) { | |
200 | gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(button), TRUE); | |
201 | } | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
202 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
203 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
204 | |
32 | 205 | UiVarEventData *event = cxMalloc( |
206 | obj->ctx->allocator, | |
207 | sizeof(UiVarEventData)); | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
208 | event->obj = obj; |
32 | 209 | event->callback = item->args.onchange; |
210 | event->userdata = item->args.onchangedata; | |
211 | event->var = var; | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
212 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
213 | g_signal_connect( |
32 | 214 | button, |
215 | "toggled", | |
216 | G_CALLBACK(ui_tool_button_toggled), | |
217 | event); | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
218 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
219 | // add item to toolbar |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
220 | gtk_toolbar_insert(tb, button, -1); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
221 | |
32 | 222 | /* |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
223 | if(item->groups) { |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
224 | uic_add_group_widget(obj->ctx, button, (ui_enablefunc)ui_set_enabled, item->groups); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
225 | } |
32 | 226 | */ |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
227 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
228 | |
32 | 229 | void ui_tool_button_toggled(GtkToggleToolButton *widget, UiVarEventData *event) { |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
230 | UiEvent e; |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
231 | e.obj = event->obj; |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
232 | e.window = event->obj->window; |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
233 | e.document = event->obj->ctx->document; |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
234 | e.eventdata = NULL; |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
235 | e.intval = gtk_toggle_tool_button_get_active(widget); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
236 | |
32 | 237 | if(event->callback) { |
238 | event->callback(&e, event->userdata); | |
239 | } | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
240 | |
32 | 241 | UiVar *var = event->var; |
242 | UiInteger *i = var ? var->value : NULL; | |
243 | ||
244 | if(i) { | |
245 | ui_notify_evt(i->observers, &e); | |
246 | } | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
247 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
248 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
249 | int64_t ui_tool_toggle_button_get(UiInteger *integer) { |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
250 | integer->value = gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(integer->obj)); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
251 | return integer->value; |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
252 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
253 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
254 | void ui_tool_toggle_button_set(UiInteger *integer, int64_t value) { |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
255 | gboolean s = value != 0 ? TRUE : FALSE; |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
256 | gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(integer->obj), s); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
257 | integer->value = s; |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
258 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
259 | |
32 | 260 | |
261 | ||
262 | typedef struct UiToolbarMenuWidget { | |
263 | GtkWidget *button; | |
264 | GtkMenu *menu; | |
265 | } UiToolbarMenuWidget; | |
266 | ||
267 | static void ui_toolbar_menubutton_clicked(GtkWidget *widget, UiToolbarMenuWidget *menu) { | |
268 | int x; | |
269 | gtk_menu_popup_at_widget(menu->menu, menu->button, GDK_GRAVITY_SOUTH_WEST, GDK_GRAVITY_NORTH_WEST, NULL); | |
270 | } | |
271 | ||
272 | static void ui_toolbar_menubutton_destroy(GtkWidget *widget, UiToolbarMenuWidget *menu) { | |
273 | free(menu); | |
274 | } | |
275 | ||
276 | void add_toolitem_menu_widget(GtkToolbar *tb, UiToolbarMenuItem *item, UiObject *obj) { | |
277 | GtkToolItem *button; | |
278 | if(item->args.stockid) { | |
279 | #ifdef UI_GTK2 | |
280 | button = gtk_tool_button_new_from_stock(item->args.stockid); | |
281 | #else | |
282 | // TODO: gtk3 stock | |
283 | button = gtk_tool_button_new(NULL, item->args.label); | |
284 | #endif | |
285 | } else { | |
286 | button = gtk_tool_button_new(NULL, item->args.label); | |
287 | } | |
288 | ||
289 | gtk_tool_item_set_homogeneous(button, FALSE); | |
290 | if(item->args.icon) { | |
291 | set_toolbutton_icon(button, item->args.icon); | |
292 | } | |
293 | gtk_tool_item_set_is_important(button, TRUE); | |
294 | ||
295 | gtk_toolbar_insert(tb, button, -1); | |
296 | ||
297 | // menu | |
298 | GtkWidget *menu_widget = gtk_menu_new(); | |
299 | ui_add_menu_items(menu_widget, 0, &item->menu, obj); | |
300 | gtk_widget_show_all(menu_widget); | |
301 | ||
302 | UiToolbarMenuWidget *tbmenu = malloc(sizeof(UiToolbarMenuWidget)); | |
303 | tbmenu->button = GTK_WIDGET(button); | |
304 | tbmenu->menu = GTK_MENU(menu_widget); | |
305 | ||
306 | g_signal_connect( | |
307 | button, | |
308 | "clicked", | |
309 | G_CALLBACK(ui_toolbar_menubutton_clicked), | |
310 | tbmenu); | |
311 | ||
312 | g_signal_connect( | |
313 | button, | |
314 | "destroy", | |
315 | G_CALLBACK(ui_toolbar_menubutton_destroy), | |
316 | tbmenu); | |
317 | } | |
318 | ||
319 | ||
320 | ||
321 | ||
322 | // deprecated / unsupported | |
323 | /* | |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
324 | void add_toolbar_combobox(GtkToolbar *tb, UiToolbarComboBox *cb, UiObject *obj) { |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
325 | UiModel *modelinfo = ui_model(obj->ctx, UI_STRING, "", -1); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
326 | modelinfo->getvalue = cb->getvalue; |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
327 | UiListModel *model = ui_list_model_new(obj, cb->var, modelinfo); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
328 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
329 | GtkWidget *combobox = ui_create_combobox(obj, model, cb->callback, cb->userdata); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
330 | GtkToolItem *item = gtk_tool_item_new(); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
331 | gtk_container_add(GTK_CONTAINER(item), combobox); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
332 | gtk_toolbar_insert(tb, item, -1); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
333 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
334 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
335 | void add_toolbar_combobox_nv(GtkToolbar *tb, UiToolbarComboBoxNV *cb, UiObject *obj) { |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
336 | UiVar *var = uic_create_var(obj->ctx, cb->listname, UI_VAR_LIST); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
337 | if(var) { |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
338 | UiModel *modelinfo = ui_model(obj->ctx, UI_STRING, "", -1); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
339 | modelinfo->getvalue = cb->getvalue; |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
340 | UiListModel *model = ui_list_model_new(obj, var, modelinfo); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
341 | |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
342 | GtkWidget *combobox = ui_create_combobox(obj, model, cb->callback, cb->userdata); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
343 | GtkToolItem *item = gtk_tool_item_new(); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
344 | gtk_container_add(GTK_CONTAINER(item), combobox); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
345 | gtk_toolbar_insert(tb, item, -1); |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
346 | } |
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
347 | } |
32 | 348 | */ |
0
2483f517c562
add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
349 | |
32 | 350 | |
351 | ||
352 | #ifdef UI_GTK3 | |
353 | ||
354 | GtkWidget* ui_create_headerbar(UiObject *obj) { | |
355 | GtkWidget *headerbar = gtk_header_bar_new(); | |
356 | ||
357 | CxMap *items = uic_get_toolbar_items(); | |
358 | CxList *left_defaults = uic_get_toolbar_defaults(UI_TOOLBAR_LEFT); | |
359 | CxList *center_defaults = uic_get_toolbar_defaults(UI_TOOLBAR_CENTER); | |
360 | CxList *right_defaults = uic_get_toolbar_defaults(UI_TOOLBAR_RIGHT); | |
361 | ||
362 | ui_headerbar_add_items(obj, headerbar, items, left_defaults); | |
363 | ui_headerbar_add_items(obj, headerbar, items, center_defaults); | |
364 | ui_headerbar_add_items(obj, headerbar, items, right_defaults); | |
365 | ||
366 | return headerbar; | |
367 | } | |
368 | ||
369 | static void hb_create_item(UiObject *obj, GtkWidget *toolbar, UiToolbarItemI *i) { | |
370 | GtkHeaderBar *tb = GTK_HEADER_BAR(toolbar); | |
371 | switch(i->type) { | |
372 | case UI_TOOLBAR_ITEM: { | |
373 | add_headerbar_item_widget(tb, (UiToolbarItem*)i, obj); | |
374 | break; | |
375 | } | |
376 | case UI_TOOLBAR_TOGGLEITEM: { | |
377 | add_headerbar_item_toggle_widget(tb, (UiToolbarToggleItem*)i, obj); | |
378 | break; | |
379 | } | |
380 | case UI_TOOLBAR_MENU: { | |
381 | add_headerbar_item_menu_widget(tb, (UiToolbarMenuItem*)i, obj); | |
382 | break; | |
383 | } | |
384 | default: fprintf(stderr, "toolbar item type unimplemented: %d\n", (int)i->type); | |
385 | } | |
386 | } | |
387 | ||
388 | ||
389 | void ui_headerbar_add_items(UiObject *obj, GtkWidget *headerbar, CxMap *items, CxList *defaults) { | |
390 | // add pre-configured items | |
391 | CxIterator i = cxListIterator(defaults); | |
392 | cx_foreach(char*, def, i) { | |
393 | UiToolbarItemI* item = uic_toolbar_get_item(def); | |
394 | if (!item) { | |
395 | fprintf(stderr, "unknown toolbar item: %s\n", def); | |
396 | continue; | |
397 | } | |
398 | hb_create_item(obj, headerbar, item); | |
399 | } | |
400 | } | |
401 | ||
402 | void add_headerbar_item_widget(GtkHeaderBar *hb, UiToolbarItem *item, UiObject *obj) { | |
403 | GtkWidget *button = gtk_button_new_with_label(item->args.label); | |
404 | if(item->args.icon) { | |
405 | ui_button_set_icon_name(button, item->args.icon); | |
406 | } | |
407 | ||
408 | gtk_header_bar_pack_start(hb, button); | |
409 | ||
410 | } | |
411 | ||
412 | void add_headerbar_item_toggle_widget(GtkHeaderBar *hb, UiToolbarToggleItem *item, UiObject *obj) { | |
413 | ||
414 | } | |
415 | ||
416 | void add_headerbar_item_menu_widget(GtkHeaderBar *hb, UiToolbarMenuItem *item, UiObject *obj) { | |
417 | ||
418 | } | |
419 | ||
420 | #endif |