Mercurial > pidgin
annotate src/gtkrequest.c @ 5490:421020171808
[gaim-migrate @ 5886]
Added multiline input requests.
committer: Tailor Script <tailor@pidgin.im>
| author | Christian Hammond <chipx86@chipx86.com> |
|---|---|
| date | Thu, 22 May 2003 19:44:59 +0000 |
| parents | c103ba09c62a |
| children | b7c0be69c749 |
| rev | line source |
|---|---|
| 5480 | 1 /** |
| 2 * @file gtkrequest.c GTK+ Request API | |
| 3 * @ingroup gtkui | |
| 4 * | |
| 5 * gaim | |
| 6 * | |
| 7 * Copyright (C) 2003 Christian Hammond <chipx86@gnupdate.org> | |
| 8 * | |
| 9 * This program is free software; you can redistribute it and/or modify | |
| 10 * it under the terms of the GNU General Public License as published by | |
| 11 * the Free Software Foundation; either version 2 of the License, or | |
| 12 * (at your option) any later version. | |
| 13 * | |
| 14 * This program is distributed in the hope that it will be useful, | |
| 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 17 * GNU General Public License for more details. | |
| 18 * | |
| 19 * You should have received a copy of the GNU General Public License | |
| 20 * along with this program; if not, write to the Free Software | |
| 21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 22 */ | |
| 23 #include "gtkrequest.h" | |
| 24 #include "stock.h" | |
| 25 #include <gtk/gtk.h> | |
| 26 #include <string.h> | |
| 27 | |
| 28 /* XXX For _(..) */ | |
| 29 #include "gaim.h" | |
| 30 | |
| 31 typedef struct | |
| 32 { | |
| 33 GaimRequestType type; | |
| 34 | |
| 35 void *user_data; | |
| 36 GtkWidget *dialog; | |
| 37 | |
| 38 size_t cb_count; | |
| 39 GCallback *cbs; | |
| 40 | |
|
5490
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
41 union |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
42 { |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
43 struct |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
44 { |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
45 GtkWidget *entry; |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
46 |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
47 gboolean multiline; |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
48 |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
49 } input; |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
50 |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
51 } u; |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
52 |
| 5480 | 53 } GaimRequestData; |
| 54 | |
| 55 static void | |
| 56 __input_response_cb(GtkDialog *dialog, gint id, GaimRequestData *data) | |
| 57 { | |
|
5490
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
58 const char *value; |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
59 |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
60 if (data->u.input.multiline) { |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
61 GtkTextIter start_iter, end_iter; |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
62 GtkTextBuffer *buffer = |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
63 gtk_text_view_get_buffer(GTK_TEXT_VIEW(data->u.input.entry)); |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
64 |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
65 gtk_text_buffer_get_start_iter(buffer, &start_iter); |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
66 gtk_text_buffer_get_end_iter(buffer, &end_iter); |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
67 |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
68 value = gtk_text_buffer_get_text(buffer, &start_iter, &end_iter, |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
69 FALSE); |
| 5480 | 70 } |
|
5490
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
71 else |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
72 value = gtk_entry_get_text(GTK_ENTRY(data->u.input.entry)); |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
73 |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
74 if (id < data->cb_count && data->cbs[id] != NULL) |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
75 ((GaimRequestInputCb)data->cbs[id])(value, data->user_data); |
| 5480 | 76 |
| 77 gaim_request_close(GAIM_REQUEST_INPUT, data); | |
| 78 } | |
| 79 | |
| 80 #define STOCK_ITEMIZE(r, l) \ | |
| 81 if (!strcmp((r), text)) \ | |
| 82 return (l); | |
| 83 | |
| 84 static const char * | |
| 85 __text_to_stock(const char *text) | |
| 86 { | |
| 87 STOCK_ITEMIZE(_("Yes"), GTK_STOCK_YES); | |
| 88 STOCK_ITEMIZE(_("No"), GTK_STOCK_NO); | |
| 89 STOCK_ITEMIZE(_("OK"), GTK_STOCK_OK); | |
| 90 STOCK_ITEMIZE(_("Cancel"), GTK_STOCK_CANCEL); | |
| 91 STOCK_ITEMIZE(_("Apply"), GTK_STOCK_APPLY); | |
| 92 STOCK_ITEMIZE(_("Close"), GTK_STOCK_CLOSE); | |
| 93 STOCK_ITEMIZE(_("Delete"), GTK_STOCK_DELETE); | |
| 94 STOCK_ITEMIZE(_("Add"), GTK_STOCK_ADD); | |
| 95 STOCK_ITEMIZE(_("Remove"), GTK_STOCK_REMOVE); | |
| 96 | |
|
5490
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
97 return text; |
| 5480 | 98 } |
| 99 | |
| 100 void * | |
| 101 gaim_gtk_request_input(const char *title, const char *primary, | |
| 102 const char *secondary, const char *default_value, | |
| 103 gboolean multiline, | |
| 104 const char *ok_text, GCallback ok_cb, | |
| 105 const char *cancel_text, GCallback cancel_cb, | |
| 106 void *user_data) | |
| 107 { | |
| 108 GaimRequestData *data; | |
| 109 GtkWidget *dialog; | |
| 110 GtkWidget *vbox; | |
| 111 GtkWidget *hbox; | |
| 112 GtkWidget *label; | |
| 113 GtkWidget *entry; | |
| 114 GtkWidget *img; | |
| 115 char *label_text; | |
| 116 | |
| 117 data = g_new0(GaimRequestData, 1); | |
| 118 data->type = GAIM_REQUEST_INPUT; | |
| 119 data->user_data = user_data; | |
| 120 | |
| 121 data->cb_count = 2; | |
| 122 data->cbs = g_new0(GCallback, 2); | |
| 123 | |
| 124 data->cbs[0] = ok_cb; | |
| 125 data->cbs[1] = cancel_cb; | |
| 126 | |
| 127 #if 0 | |
| 128 data->dialog = dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
| 129 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG); | |
| 130 gtk_window_set_role(GTK_WINDOW(dialog), "input"); | |
| 131 gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE); | |
| 132 #endif | |
| 133 | |
| 134 /* Create the dialog. */ | |
| 135 dialog = gtk_dialog_new_with_buttons("", NULL, 0, | |
| 5485 | 136 __text_to_stock(cancel_text), 1, |
| 137 __text_to_stock(ok_text), 0, | |
| 138 NULL); | |
| 5480 | 139 data->dialog = dialog; |
| 140 | |
| 141 g_signal_connect(G_OBJECT(dialog), "response", | |
| 142 G_CALLBACK(__input_response_cb), data); | |
| 143 | |
| 144 /* Setup the dialog */ | |
| 145 gtk_container_set_border_width(GTK_CONTAINER(dialog), 6); | |
| 146 gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE); | |
| 147 gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE); | |
| 148 gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dialog)->vbox), 12); | |
| 149 gtk_container_set_border_width(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), 6); | |
| 150 | |
| 151 /* Setup the main horizontal box */ | |
| 152 hbox = gtk_hbox_new(FALSE, 12); | |
| 153 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), hbox); | |
| 154 | |
| 155 /* Dialog icon. */ | |
| 156 img = gtk_image_new_from_stock(GAIM_STOCK_DIALOG_QUESTION, | |
| 157 GTK_ICON_SIZE_DIALOG); | |
| 158 gtk_misc_set_alignment(GTK_MISC(img), 0, 0); | |
| 159 gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 0); | |
| 160 | |
| 161 /* Vertical box */ | |
| 162 vbox = gtk_vbox_new(FALSE, 12); | |
| 163 | |
| 164 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0); | |
| 165 | |
| 166 /* Descriptive label */ | |
| 167 label_text = g_strdup_printf("<span weight=\"bold\" size=\"larger\">" | |
| 168 "%s</span>\n\n%s", | |
| 169 primary, (secondary ? secondary : "")); | |
| 170 | |
| 171 label = gtk_label_new(NULL); | |
| 172 | |
| 173 gtk_label_set_markup(GTK_LABEL(label), label_text); | |
| 174 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE); | |
| 175 gtk_misc_set_alignment(GTK_MISC(label), 0, 0); | |
| 176 gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 0); | |
| 177 | |
| 178 g_free(label_text); | |
| 179 | |
| 180 /* Entry field. */ | |
|
5490
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
181 data->u.input.multiline = multiline; |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
182 |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
183 if (multiline) { |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
184 GtkWidget *sw; |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
185 |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
186 sw = gtk_scrolled_window_new(NULL, NULL); |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
187 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
188 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS); |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
189 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
190 GTK_SHADOW_IN); |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
191 |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
192 gtk_widget_set_size_request(sw, 300, 75); |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
193 |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
194 gtk_box_pack_start(GTK_BOX(vbox), sw, TRUE, TRUE, 0); |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
195 |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
196 entry = gtk_text_view_new(); |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
197 gtk_text_view_set_editable(GTK_TEXT_VIEW(entry), TRUE); |
| 5480 | 198 |
|
5490
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
199 gtk_container_add(GTK_CONTAINER(sw), entry); |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
200 |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
201 if (default_value != NULL) { |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
202 GtkTextBuffer *buffer; |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
203 |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
204 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(entry)); |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
205 gtk_text_buffer_set_text(buffer, default_value, -1); |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
206 } |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
207 } |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
208 else { |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
209 entry = gtk_entry_new(); |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
210 |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
211 gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0); |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
212 |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
213 if (default_value != NULL) |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
214 gtk_entry_set_text(GTK_ENTRY(entry), default_value); |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
215 } |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
216 |
|
421020171808
[gaim-migrate @ 5886]
Christian Hammond <chipx86@chipx86.com>
parents:
5485
diff
changeset
|
217 data->u.input.entry = entry; |
| 5480 | 218 |
| 219 /* Show everything. */ | |
| 220 gtk_widget_show_all(dialog); | |
| 221 | |
| 222 return data; | |
| 223 } | |
| 224 | |
| 225 void * | |
| 226 gaim_gtk_request_choice(const char *title, const char *primary, | |
| 227 const char *secondary, unsigned int default_value, | |
| 228 const char *ok_text, GCallback ok_cb, | |
| 229 const char *cancel_text, GCallback cancel_cb, | |
| 230 void *user_data, va_list args) | |
| 231 { | |
| 232 return NULL; | |
| 233 } | |
| 234 | |
| 235 void * | |
| 236 gaim_gtk_request_action(const char *title, const char *primary, | |
| 237 const char *secondary, unsigned int default_action, | |
| 238 void *user_data, va_list actions) | |
| 239 { | |
| 240 return NULL; | |
| 241 } | |
| 242 | |
| 243 void | |
| 244 gaim_gtk_close_request(GaimRequestType type, void *ui_handle) | |
| 245 { | |
| 246 GaimRequestData *data = (GaimRequestData *)ui_handle; | |
| 247 | |
| 248 if (data->cbs != NULL) | |
| 249 g_free(data->cbs); | |
| 250 | |
| 251 gtk_widget_destroy(data->dialog); | |
| 252 | |
| 253 g_free(data); | |
| 254 } | |
| 255 | |
| 256 static GaimRequestUiOps ops = | |
| 257 { | |
| 258 gaim_gtk_request_input, | |
| 259 gaim_gtk_request_choice, | |
| 260 gaim_gtk_request_action, | |
| 261 gaim_gtk_close_request | |
| 262 }; | |
| 263 | |
| 264 GaimRequestUiOps * | |
| 265 gaim_get_gtk_request_ui_ops(void) | |
| 266 { | |
| 267 return &ops; | |
| 268 } |
