comparison console/gntrequest.c @ 13983:b7a99d54a5a9

[gaim-migrate @ 16552] Add a debug window. The scrolling doesn't seem to work properly. I will try to figure out what's wrong with it. Start the request-ui. The ui for request input, choice and action are mostly done. I am not handling multiline input requests yet. It's not too high in my todo-list either. committer: Tailor Script <tailor@pidgin.im>
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Sun, 23 Jul 2006 20:38:07 +0000
parents
children 19a1812e6c46
comparison
equal deleted inserted replaced
13982:052aee037835 13983:b7a99d54a5a9
1 #include <gnt.h>
2 #include <gntbox.h>
3 #include <gntbutton.h>
4 #include <gntcombobox.h>
5 #include <gntentry.h>
6 #include <gntlabel.h>
7
8 #include "gntrequest.h"
9
10 static GntWidget *
11 setup_request_window(const char *title, const char *primary,
12 const char *secondary)
13 {
14 GntWidget *window;
15
16 window = gnt_vbox_new(FALSE);
17 gnt_box_set_toplevel(GNT_BOX(window), TRUE);
18 gnt_box_set_title(GNT_BOX(window), title);
19 gnt_box_set_alignment(GNT_BOX(window), GNT_ALIGN_MID);
20
21 if (primary)
22 gnt_box_add_widget(GNT_BOX(window),
23 gnt_label_new_with_format(primary, GNT_TEXT_FLAG_BOLD));
24 if (secondary)
25 gnt_box_add_widget(GNT_BOX(window), gnt_label_new(secondary));
26
27 return window;
28 }
29
30 static GntWidget *
31 setup_button_box(gpointer userdata, gpointer cb, gpointer data, ...)
32 {
33 GntWidget *box, *button;
34 va_list list;
35 const char *text;
36 gpointer callback;
37
38 box = gnt_hbox_new(TRUE);
39
40 va_start(list, data);
41
42 while ((text = va_arg(list, const char *)))
43 {
44 callback = va_arg(list, gpointer);
45 button = gnt_button_new(text);
46 gnt_box_add_widget(GNT_BOX(box), button);
47 g_object_set_data(G_OBJECT(button), "activate-callback", callback);
48 g_object_set_data(G_OBJECT(button), "activate-userdata", userdata);
49 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(cb), data);
50 }
51
52 va_end(list);
53 return box;
54 }
55
56 static void
57 notify_input_cb(GntWidget *button, GntWidget *entry)
58 {
59 GaimRequestInputCb callback = g_object_get_data(G_OBJECT(button), "activate-callback");
60 gpointer data = g_object_get_data(G_OBJECT(button), "activate-userdata");
61 const char *text = gnt_entry_get_text(GNT_ENTRY(entry));
62
63 if (callback)
64 callback(data, text);
65
66 while (button->parent)
67 button = button->parent;
68
69 gaim_request_close(GAIM_REQUEST_INPUT, button);
70 }
71
72 static void *
73 gg_request_input(const char *title, const char *primary,
74 const char *secondary, const char *default_value,
75 gboolean multiline, gboolean masked, gchar *hint,
76 const char *ok_text, GCallback ok_cb,
77 const char *cancel_text, GCallback cancel_cb,
78 void *user_data)
79 {
80 GntWidget *window, *box, *entry;
81
82 window = setup_request_window(title, primary, secondary);
83
84 entry = gnt_entry_new(default_value);
85 if (masked)
86 gnt_entry_set_masked(GNT_ENTRY(entry), TRUE);
87 gnt_box_add_widget(GNT_BOX(window), entry);
88
89 box = setup_button_box(user_data, notify_input_cb, entry,
90 ok_text, ok_cb, cancel_text, cancel_cb, NULL);
91 gnt_box_add_widget(GNT_BOX(window), box);
92
93 gnt_widget_show(window);
94
95 return window;
96 }
97
98 static void
99 gg_close_request(GaimRequestType type, gpointer ui_handle)
100 {
101 GntWidget *widget = GNT_WIDGET(ui_handle);
102 while (widget->parent)
103 widget = widget->parent;
104 gnt_widget_destroy(widget);
105 }
106
107 static void
108 request_choice_cb(GntWidget *button, GntComboBox *combo)
109 {
110 GaimRequestChoiceCb callback = g_object_get_data(G_OBJECT(button), "activate-callback");
111 gpointer data = g_object_get_data(G_OBJECT(button), "activate-userdata");
112 int choice = GPOINTER_TO_INT(gnt_combo_box_get_selected_data(GNT_COMBO_BOX(combo))) - 1;
113
114 if (callback)
115 callback(data, choice);
116
117 while (button->parent)
118 button = button->parent;
119
120 gaim_request_close(GAIM_REQUEST_INPUT, button);
121 }
122
123 static void *
124 gg_request_choice(const char *title, const char *primary,
125 const char *secondary, unsigned int default_value,
126 const char *ok_text, GCallback ok_cb,
127 const char *cancel_text, GCallback cancel_cb,
128 void *user_data, va_list choices)
129 {
130 GntWidget *window, *combo, *box;
131 const char *text;
132 int val;
133
134 window = setup_request_window(title, primary, secondary);
135
136 combo = gnt_combo_box_new();
137 gnt_box_add_widget(GNT_BOX(window), combo);
138 while ((text = va_arg(choices, const char *)))
139 {
140 val = va_arg(choices, int);
141 gnt_combo_box_add_data(GNT_COMBO_BOX(combo), GINT_TO_POINTER(val + 1), text);
142 }
143 gnt_combo_box_set_selected(GNT_COMBO_BOX(combo), GINT_TO_POINTER(default_value + 1));
144
145 box = setup_button_box(user_data, request_choice_cb, combo,
146 ok_text, ok_cb, cancel_text, cancel_cb, NULL);
147 gnt_box_add_widget(GNT_BOX(window), box);
148
149 gnt_widget_show(window);
150
151 return window;
152 }
153
154 static void
155 request_action_cb(GntWidget *button, GntWidget *window)
156 {
157 GaimRequestActionCb callback = g_object_get_data(G_OBJECT(button), "activate-callback");
158 gpointer data = g_object_get_data(G_OBJECT(button), "activate-userdata");
159 int id = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "activate-id"));
160
161 callback(data, id);
162
163 gaim_request_close(GAIM_REQUEST_ACTION, window);
164 }
165
166 static void*
167 gg_request_action(const char *title, const char *primary,
168 const char *secondary, unsigned int default_value,
169 void *user_data, size_t actioncount,
170 va_list actions)
171 {
172 GntWidget *window, *box, *button;
173 int i;
174
175 window = setup_request_window(title, primary, secondary);
176
177 box = gnt_hbox_new(TRUE);
178 gnt_box_add_widget(GNT_BOX(window), box);
179 for (i = 0; i < actioncount; i++)
180 {
181 const char *text = va_arg(actions, const char *);
182 GaimRequestActionCb callback = va_arg(actions, GaimRequestActionCb);
183
184 button = gnt_button_new(text);
185 gnt_box_add_widget(GNT_BOX(box), button);
186
187 g_object_set_data(G_OBJECT(button), "activate-callback", callback);
188 g_object_set_data(G_OBJECT(button), "activate-userdata", user_data);
189 g_object_set_data(G_OBJECT(button), "activate-id", GINT_TO_POINTER(i));
190 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(request_action_cb), window);
191 }
192
193 gnt_widget_show(window);
194
195 return window;
196 }
197
198 static GaimRequestUiOps uiops =
199 {
200 .request_input = gg_request_input,
201 .close_request = gg_close_request,
202 .request_choice = gg_request_choice,
203 .request_action = gg_request_action,
204 };
205
206 GaimRequestUiOps *gg_request_get_ui_ops()
207 {
208 return &uiops;
209 }
210
211 void gg_request_init()
212 {
213 }
214
215 void gg_request_uninit()
216 {
217 }
218