ui/gtk/button.c

Sat, 05 Dec 2020 10:34:10 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 05 Dec 2020 10:34:10 +0100
changeset 161
b1eac0878ce7
parent 157
0b33b9396851
child 167
161511838ea6
permissions
-rw-r--r--

fix crash on window close (GTK)

3
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
140
c03c338a7dcf refactors value binding system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 108
diff changeset
4 * Copyright 2017 Olaf Wintermann. All rights reserved.
3
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
c1a75454b444 added toolbar
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
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include "button.h"
4
39b9b86ec452 added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
33 #include "container.h"
157
0b33b9396851 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 145
diff changeset
34 #include <ucx/mempool.h>
4
39b9b86ec452 added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
35 #include "../common/context.h"
33
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
36 #include "../common/object.h"
3
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37
4
39b9b86ec452 added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
38 UIWIDGET ui_button(UiObject *obj, char *label, ui_callback f, void *data) {
39b9b86ec452 added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
39 GtkWidget *button = gtk_button_new_with_label(label);
39b9b86ec452 added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
40
39b9b86ec452 added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
41 if(f) {
65
4697592e24ba added label, grid container, simple tabview and textfield (Gtk)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 59
diff changeset
42 UiEventData *event = malloc(sizeof(UiEventData));
4
39b9b86ec452 added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
43 event->obj = obj;
42
29b2821d1262 added table view events (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
44 event->userdata = data;
4
39b9b86ec452 added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
45 event->callback = f;
16
a499c8a72c15 added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
46 event->value = 0;
4
39b9b86ec452 added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
47
39b9b86ec452 added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
48 g_signal_connect(
39b9b86ec452 added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
49 button,
39b9b86ec452 added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
50 "clicked",
39b9b86ec452 added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
51 G_CALLBACK(ui_button_clicked),
39b9b86ec452 added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
52 event);
21
012418e7dc90 added groups for menu items (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
53 g_signal_connect(
012418e7dc90 added groups for menu items (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
54 button,
012418e7dc90 added groups for menu items (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
55 "destroy",
012418e7dc90 added groups for menu items (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
56 G_CALLBACK(ui_destroy_userdata),
012418e7dc90 added groups for menu items (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
57 event);
4
39b9b86ec452 added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
58 }
39b9b86ec452 added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
59
5
19d37cb9c96c added textarea
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
60 UiContainer *ct = uic_get_current_container(obj);
59
eb6611be50c7 added box container (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 52
diff changeset
61 ct->add(ct, button, FALSE);
4
39b9b86ec452 added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
62
39b9b86ec452 added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
63 return button;
39b9b86ec452 added simple container
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
64 }
3
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 void ui_button_clicked(GtkWidget *widget, UiEventData *event) {
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 UiEvent e;
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 e.obj = event->obj;
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 e.window = event->obj->window;
52
25e5390cce41 added document tabview (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 42
diff changeset
71 e.document = event->obj->ctx->document;
16
a499c8a72c15 added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
72 e.eventdata = NULL;
a499c8a72c15 added menu item lists (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
73 e.intval = event->value;
42
29b2821d1262 added table view events (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
74 event->callback(&e, event->userdata);
3
c1a75454b444 added toolbar
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 }
33
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
76
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
77 void ui_button_toggled(GtkToggleToolButton *widget, UiEventData *event) {
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
78 UiEvent e;
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
79 e.obj = event->obj;
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
80 e.window = event->obj->window;
52
25e5390cce41 added document tabview (GTK, Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 42
diff changeset
81 e.document = event->obj->ctx->document;
33
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
82 e.eventdata = NULL;
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
83 e.intval = gtk_toggle_tool_button_get_active(widget);
42
29b2821d1262 added table view events (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
84 event->callback(&e, event->userdata);
33
458831c574f4 added listview, sidebar and toolbar image button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 21
diff changeset
85 }
94
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
86
144
29d98cff4f56 UiInteger uses int64 now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 143
diff changeset
87 int64_t ui_toggle_button_get(UiInteger *integer) {
94
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
88 GtkToggleButton *button = integer->obj;
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
89 integer->value = (int)gtk_toggle_button_get_active(button);
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
90 return integer->value;
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
91 }
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
92
144
29d98cff4f56 UiInteger uses int64 now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 143
diff changeset
93 void ui_toggle_button_set(UiInteger *integer, int64_t value) {
94
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
94 GtkToggleButton *button = integer->obj;
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
95 integer->value = value;
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
96 gtk_toggle_button_set_active(button, value != 0 ? TRUE : FALSE);
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
97 }
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
98
142
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
99 void ui_toggled_obs(GtkToggleToolButton *widget, UiVarEventData *event) {
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
100 UiEvent e;
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
101 e.obj = event->obj;
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
102 e.window = event->obj->window;
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
103 e.document = event->obj->ctx->document;
143
d499b29d7cb6 adds observer support for textarea and textfield (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 142
diff changeset
104 e.eventdata = event->var->value;
142
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
105 e.intval = gtk_toggle_tool_button_get_active(widget);
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
106
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
107 UiInteger *i = event->var->value;
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
108 ui_notify_evt(i->observers, &e);
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
109 }
94
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
110
142
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
111 UIWIDGET ui_checkbox_var(UiObject *obj, char *label, UiVar *var) {
94
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
112 GtkWidget *button = gtk_check_button_new_with_label(label);
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
113
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
114 // bind value
142
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
115 if(var) {
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
116 UiInteger *value = var->value;
94
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
117 value->obj = GTK_TOGGLE_BUTTON(button);
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
118 value->get = ui_toggle_button_get;
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
119 value->set = ui_toggle_button_set;
142
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
120 gtk_toggle_button_set_active(value->obj, value->value);
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
121
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
122 UiVarEventData *event = malloc(sizeof(UiVarEventData));
94
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
123 event->obj = obj;
142
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
124 event->var = var;
145
853685152c1d adds spinner widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 144
diff changeset
125 event->observers = NULL;
94
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
126
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
127 g_signal_connect(
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
128 button,
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
129 "clicked",
142
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
130 G_CALLBACK(ui_toggled_obs),
94
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
131 event);
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
132 g_signal_connect(
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
133 button,
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
134 "destroy",
142
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
135 G_CALLBACK(ui_destroy_vardata),
94
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
136 event);
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
137 }
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
138
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
139 UiContainer *ct = uic_get_current_container(obj);
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
140 ct->add(ct, button, FALSE);
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
141
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
142 return button;
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
143 }
d51e334c1439 added checkbox and labels with alignment (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 65
diff changeset
144
142
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
145 UIWIDGET ui_checkbox(UiObject *obj, char *label, UiInteger *value) {
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
146 UiVar *var = NULL;
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
147 if(value) {
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
148 var = malloc(sizeof(UiVar));
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
149 var->value = value;
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
150 var->type = UI_VAR_SPECIAL;
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
151 }
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
152 return ui_checkbox_var(obj, label, var);
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
153 }
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
154
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
155 UIWIDGET ui_checkbox_nv(UiObject *obj, char *label, char *varname) {
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
156 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_INTEGER);
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
157 return ui_checkbox_var(obj, label, var);
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
158 }
107
9aff1dc3990d improved box container and added radio button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
159
9aff1dc3990d improved box container and added radio button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
160
142
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
161 UIWIDGET ui_radiobutton_var(UiObject *obj, char *label, UiVar *var) {
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
162 GSList *rg = NULL;
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
163 UiInteger *rgroup;
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
164
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
165 if(var) {
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
166 rgroup = var->value;
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
167 rg = rgroup->obj;
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
168 }
108
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
169
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
170 GtkWidget *rbutton = gtk_radio_button_new_with_label(rg, label);
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
171 rg = gtk_radio_button_get_group(GTK_RADIO_BUTTON(rbutton));
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
172
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
173 if(rgroup) {
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
174 rgroup->obj = rg;
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
175 rgroup->get = ui_radiobutton_get;
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
176 rgroup->set = ui_radiobutton_set;
142
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
177
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
178 ui_radiobutton_set(rgroup, rgroup->value);
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
179
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
180 UiVarEventData *event = malloc(sizeof(UiVarEventData));
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
181 event->obj = obj;
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
182 event->var = var;
145
853685152c1d adds spinner widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 144
diff changeset
183 event->observers = NULL;
142
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
184
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
185 g_signal_connect(
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
186 rbutton,
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
187 "clicked",
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
188 G_CALLBACK(ui_radio_obs),
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
189 event);
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
190 g_signal_connect(
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
191 rbutton,
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
192 "destroy",
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
193 G_CALLBACK(ui_destroy_vardata),
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
194 event);
107
9aff1dc3990d improved box container and added radio button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
195 }
9aff1dc3990d improved box container and added radio button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
196
9aff1dc3990d improved box container and added radio button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
197 UiContainer *ct = uic_get_current_container(obj);
9aff1dc3990d improved box container and added radio button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
198 ct->add(ct, rbutton, FALSE);
9aff1dc3990d improved box container and added radio button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
199
9aff1dc3990d improved box container and added radio button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
200 return rbutton;
9aff1dc3990d improved box container and added radio button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
201 }
9aff1dc3990d improved box container and added radio button (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 94
diff changeset
202
142
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
203 UIWIDGET ui_radiobutton(UiObject *obj, char *label, UiInteger *rgroup) {
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
204 UiVar *var = NULL;
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
205 if(rgroup) {
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
206 var = malloc(sizeof(UiVar));
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
207 var->value = rgroup;
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
208 var->type = UI_VAR_SPECIAL;
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
209 }
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
210 return ui_radiobutton_var(obj, label, var);
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
211 }
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
212
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
213 UIWIDGET ui_radiobutton_nv(UiObject *obj, char *label, char *varname) {
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
214 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_INTEGER);
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
215 return ui_radiobutton_var(obj, label, var);
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
216 }
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
217
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
218 void ui_radio_obs(GtkToggleToolButton *widget, UiVarEventData *event) {
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
219 UiInteger *i = event->var->value;
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
220
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
221 UiEvent e;
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
222 e.obj = event->obj;
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
223 e.window = event->obj->window;
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
224 e.document = event->obj->ctx->document;
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
225 e.eventdata = NULL;
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
226 e.intval = i->get(i);
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
227
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
228 ui_notify_evt(i->observers, &e);
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
229 }
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
230
144
29d98cff4f56 UiInteger uses int64 now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 143
diff changeset
231 int64_t ui_radiobutton_get(UiInteger *value) {
108
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
232 int selection = 0;
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
233 GSList *ls = value->obj;
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
234 int i = 0;
142
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
235 guint len = g_slist_length(ls);
108
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
236 while(ls) {
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
237 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ls->data))) {
142
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
238 selection = len - i - 1;
108
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
239 break;
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
240 }
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
241 ls = ls->next;
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
242 i++;
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
243 }
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
244
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
245 value->value = selection;
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
246 return selection;
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
247 }
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
248
144
29d98cff4f56 UiInteger uses int64 now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 143
diff changeset
249 void ui_radiobutton_set(UiInteger *value, int64_t i) {
108
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
250 GSList *ls = value->obj;
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
251 int s = g_slist_length(ls) - 1 - i;
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
252 int j = 0;
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
253 while(ls) {
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
254 if(j == s) {
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
255 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ls->data), TRUE);
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
256 break;
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
257 }
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
258 ls = ls->next;
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
259 j++;
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
260 }
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
261
142
46448d38885c new checkbox and radionbutton features and more refactoring
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 140
diff changeset
262 value->value = i;
108
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
263 }
fcf6d5fac8f5 radio buttons bind to UiInteger now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 107
diff changeset
264

mercurial