comparison src/gtkrequest.c @ 5480:f19620d8694f

[gaim-migrate @ 5876] Added the base GTK+ request API code. committer: Tailor Script <tailor@pidgin.im>
author Christian Hammond <chipx86@chipx86.com>
date Thu, 22 May 2003 10:21:43 +0000
parents
children c103ba09c62a
comparison
equal deleted inserted replaced
5479:175de59cd1c5 5480:f19620d8694f
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 GtkWidget *entry;
38
39 size_t cb_count;
40 GCallback *cbs;
41
42 } GaimRequestData;
43
44 static void
45 __input_response_cb(GtkDialog *dialog, gint id, GaimRequestData *data)
46 {
47 if (id < data->cb_count && data->cbs[id] != NULL) {
48 ((GaimRequestInputCb)data->cbs[id])(
49 gtk_entry_get_text(GTK_ENTRY(data->entry)), data->user_data);
50 }
51
52 gaim_request_close(GAIM_REQUEST_INPUT, data);
53 }
54
55 #define STOCK_ITEMIZE(r, l) \
56 if (!strcmp((r), text)) \
57 return (l);
58
59 static const char *
60 __text_to_stock(const char *text)
61 {
62 STOCK_ITEMIZE(_("Yes"), GTK_STOCK_YES);
63 STOCK_ITEMIZE(_("No"), GTK_STOCK_NO);
64 STOCK_ITEMIZE(_("OK"), GTK_STOCK_OK);
65 STOCK_ITEMIZE(_("Cancel"), GTK_STOCK_CANCEL);
66 STOCK_ITEMIZE(_("Apply"), GTK_STOCK_APPLY);
67 STOCK_ITEMIZE(_("Close"), GTK_STOCK_CLOSE);
68 STOCK_ITEMIZE(_("Delete"), GTK_STOCK_DELETE);
69 STOCK_ITEMIZE(_("Add"), GTK_STOCK_ADD);
70 STOCK_ITEMIZE(_("Remove"), GTK_STOCK_REMOVE);
71
72 return NULL;
73 }
74
75 void *
76 gaim_gtk_request_input(const char *title, const char *primary,
77 const char *secondary, const char *default_value,
78 gboolean multiline,
79 const char *ok_text, GCallback ok_cb,
80 const char *cancel_text, GCallback cancel_cb,
81 void *user_data)
82 {
83 GaimRequestData *data;
84 GtkWidget *dialog;
85 GtkWidget *vbox;
86 GtkWidget *hbox;
87 GtkWidget *label;
88 GtkWidget *entry;
89 GtkWidget *img;
90 char *label_text;
91
92 data = g_new0(GaimRequestData, 1);
93 data->type = GAIM_REQUEST_INPUT;
94 data->user_data = user_data;
95
96 data->cb_count = 2;
97 data->cbs = g_new0(GCallback, 2);
98
99 data->cbs[0] = ok_cb;
100 data->cbs[1] = cancel_cb;
101
102 #if 0
103 data->dialog = dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
104 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
105 gtk_window_set_role(GTK_WINDOW(dialog), "input");
106 gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
107 #endif
108
109 /* Create the dialog. */
110 dialog = gtk_dialog_new_with_buttons("", NULL, 0,
111 __text_to_stock(ok_text), 0,
112 __text_to_stock(cancel_text), 1,
113 NULL);
114 data->dialog = dialog;
115
116 g_signal_connect(G_OBJECT(dialog), "response",
117 G_CALLBACK(__input_response_cb), data);
118
119 /* Setup the dialog */
120 gtk_container_set_border_width(GTK_CONTAINER(dialog), 6);
121 gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
122 gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
123 gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dialog)->vbox), 12);
124 gtk_container_set_border_width(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), 6);
125
126 /* Setup the main horizontal box */
127 hbox = gtk_hbox_new(FALSE, 12);
128 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), hbox);
129
130 /* Dialog icon. */
131 img = gtk_image_new_from_stock(GAIM_STOCK_DIALOG_QUESTION,
132 GTK_ICON_SIZE_DIALOG);
133 gtk_misc_set_alignment(GTK_MISC(img), 0, 0);
134 gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 0);
135
136 /* Vertical box */
137 vbox = gtk_vbox_new(FALSE, 12);
138
139 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
140
141 /* Descriptive label */
142 label_text = g_strdup_printf("<span weight=\"bold\" size=\"larger\">"
143 "%s</span>\n\n%s",
144 primary, (secondary ? secondary : ""));
145
146 label = gtk_label_new(NULL);
147
148 gtk_label_set_markup(GTK_LABEL(label), label_text);
149 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
150 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
151 gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 0);
152
153 g_free(label_text);
154
155 /* Entry field. */
156 data->entry = entry = gtk_entry_new();
157 gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
158
159 if (default_value != NULL)
160 gtk_entry_set_text(GTK_ENTRY(entry), default_value);
161
162 /* Show everything. */
163 gtk_widget_show_all(dialog);
164
165 return data;
166 }
167
168 void *
169 gaim_gtk_request_choice(const char *title, const char *primary,
170 const char *secondary, unsigned int default_value,
171 const char *ok_text, GCallback ok_cb,
172 const char *cancel_text, GCallback cancel_cb,
173 void *user_data, va_list args)
174 {
175 return NULL;
176 }
177
178 void *
179 gaim_gtk_request_action(const char *title, const char *primary,
180 const char *secondary, unsigned int default_action,
181 void *user_data, va_list actions)
182 {
183 return NULL;
184 }
185
186 void
187 gaim_gtk_close_request(GaimRequestType type, void *ui_handle)
188 {
189 GaimRequestData *data = (GaimRequestData *)ui_handle;
190
191 if (data->cbs != NULL)
192 g_free(data->cbs);
193
194 gtk_widget_destroy(data->dialog);
195
196 g_free(data);
197 }
198
199 static GaimRequestUiOps ops =
200 {
201 gaim_gtk_request_input,
202 gaim_gtk_request_choice,
203 gaim_gtk_request_action,
204 gaim_gtk_close_request
205 };
206
207 GaimRequestUiOps *
208 gaim_get_gtk_request_ui_ops(void)
209 {
210 return &ops;
211 }