summaryrefslogtreecommitdiff
path: root/pidgin/minidialog.c
blob: 6599123ee3d8106fdba633f0d5b8439c4e3a960c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/**
 * @file minidialog.c Implementation of the #PidginMiniDialog Gtk widget.
 * @ingroup pidgin
 */

/* pidgin
 *
 * Pidgin is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 */

#include "internal.h"

#include <gtk/gtk.h>

#include "libpurple/prefs.h"

#include "pidgin/minidialog.h"
#include "pidgin/pidgin.h"
#include "pidgin/pidginstock.h"

static void     pidgin_mini_dialog_init       (PidginMiniDialog      *self);
static void     pidgin_mini_dialog_class_init (PidginMiniDialogClass *klass);

static gpointer pidgin_mini_dialog_parent_class = NULL;

static void
pidgin_mini_dialog_class_intern_init (gpointer klass)
{
	pidgin_mini_dialog_parent_class = g_type_class_peek_parent (klass);
	pidgin_mini_dialog_class_init ((PidginMiniDialogClass*) klass);
}

GType
pidgin_mini_dialog_get_type (void)
{
	static GType g_define_type_id = 0;
	if (g_define_type_id == 0)
	{
		static const GTypeInfo g_define_type_info = {
			sizeof (PidginMiniDialogClass),
			(GBaseInitFunc) NULL,
			(GBaseFinalizeFunc) NULL,
			(GClassInitFunc) pidgin_mini_dialog_class_intern_init,
			(GClassFinalizeFunc) NULL,
			NULL,   /* class_data */
			sizeof (PidginMiniDialog),
			0,      /* n_preallocs */
			(GInstanceInitFunc) pidgin_mini_dialog_init,
			NULL,
		};
		g_define_type_id = g_type_register_static (GTK_TYPE_VBOX,
			"PidginMiniDialog", &g_define_type_info, 0);
	}
	return g_define_type_id;
}

enum
{
	PROP_TITLE = 1,
	PROP_DESCRIPTION,
	PROP_ICON_NAME,

	LAST_PROPERTY
} HazeConnectionProperties;

typedef struct _PidginMiniDialogPrivate
{
	GtkImage *icon;
	GtkBox *title_box;
	GtkLabel *title;
	GtkLabel *desc;
	GtkBox *buttons;

	guint idle_destroy_cb_id;
} PidginMiniDialogPrivate;

#define PIDGIN_MINI_DIALOG_GET_PRIVATE(dialog) \
	((PidginMiniDialogPrivate *) ((dialog)->priv))

PidginMiniDialog *
pidgin_mini_dialog_new(const gchar *title,
                       const gchar *description,
                       const gchar *icon_name)
{
	PidginMiniDialog *mini_dialog = g_object_new(PIDGIN_TYPE_MINI_DIALOG,
		"title", title,
		"description", description,
		"icon-name", icon_name,
		NULL);

	return mini_dialog;
}

void
pidgin_mini_dialog_set_title(PidginMiniDialog *mini_dialog,
                             const char *title)
{
	g_object_set(G_OBJECT(mini_dialog), "title", title, NULL);
}

void
pidgin_mini_dialog_set_description(PidginMiniDialog *mini_dialog,
                                   const char *description)
{
	g_object_set(G_OBJECT(mini_dialog), "description", description, NULL);
}

void
pidgin_mini_dialog_set_icon_name(PidginMiniDialog *mini_dialog,
                                 const char *icon_name)
{
	g_object_set(G_OBJECT(mini_dialog), "icon_name", icon_name, NULL);
}

struct _mini_dialog_button_clicked_cb_data
{
	PidginMiniDialog *mini_dialog;
	PidginMiniDialogCallback callback;
	gpointer user_data;
};

guint
pidgin_mini_dialog_get_num_children(PidginMiniDialog *mini_dialog)
{
	return g_list_length(mini_dialog->contents->children);
}

static gboolean
idle_destroy_cb(GtkWidget *mini_dialog)
{
	gtk_widget_destroy(mini_dialog);
	return FALSE;
}

static void
mini_dialog_button_clicked_cb(GtkButton *button,
                              gpointer user_data)
{
	struct _mini_dialog_button_clicked_cb_data *data = user_data;
	PidginMiniDialogPrivate *priv =
		PIDGIN_MINI_DIALOG_GET_PRIVATE(data->mini_dialog);

	/* Set up the destruction callback before calling the clicked callback,
	 * so that if the mini-dialog gets destroyed during the clicked callback
	 * the idle_destroy_cb is correctly removed by _finalize.
	 */
	priv->idle_destroy_cb_id =
		g_idle_add((GSourceFunc) idle_destroy_cb, data->mini_dialog);

	if (data->callback != NULL)
		data->callback(data->mini_dialog, button, data->user_data);

}

static void
mini_dialog_button_destroy_cb(GtkButton *button,
                              gpointer user_data)
{
	struct _mini_dialog_button_clicked_cb_data *data = user_data;
	g_free(data);
}

void
pidgin_mini_dialog_add_button(PidginMiniDialog *self,
                              const char *text,
                              PidginMiniDialogCallback clicked_cb,
                              gpointer user_data)
{
	PidginMiniDialogPrivate *priv = PIDGIN_MINI_DIALOG_GET_PRIVATE(self);
	struct _mini_dialog_button_clicked_cb_data *callback_data
		= g_new0(struct _mini_dialog_button_clicked_cb_data, 1);
	GtkWidget *button = gtk_button_new();
	GtkWidget *label = gtk_label_new(NULL);
	char *button_text =
		g_strdup_printf("<span size=\"smaller\">%s</span>", text);

	gtk_label_set_markup_with_mnemonic(GTK_LABEL(label), button_text);
	g_free(button_text);

	callback_data->mini_dialog = self;
	callback_data->callback = clicked_cb;
	callback_data->user_data = user_data;
	g_signal_connect(G_OBJECT(button), "clicked",
		(GCallback) mini_dialog_button_clicked_cb, callback_data);
	g_signal_connect(G_OBJECT(button), "destroy",
		(GCallback) mini_dialog_button_destroy_cb, callback_data);

	gtk_misc_set_alignment(GTK_MISC(label), 0.5, 0.5);
	gtk_container_add(GTK_CONTAINER(button), label);

	gtk_box_pack_end(GTK_BOX(priv->buttons), button, FALSE, FALSE,
		0);
	gtk_widget_show_all(GTK_WIDGET(button));
}

static void
pidgin_mini_dialog_get_property(GObject *object,
                                guint property_id,
                                GValue *value,
                                GParamSpec *pspec)
{
	PidginMiniDialog *self = PIDGIN_MINI_DIALOG(object);
	PidginMiniDialogPrivate *priv = PIDGIN_MINI_DIALOG_GET_PRIVATE(self);

	switch (property_id) {
		case PROP_TITLE:
			g_value_set_string(value, gtk_label_get_text(priv->title));
			break;
		case PROP_DESCRIPTION:
			g_value_set_string(value, gtk_label_get_text(priv->desc));
			break;
		case PROP_ICON_NAME:
		{
			gchar *icon_name = NULL;
			GtkIconSize size;
			gtk_image_get_stock(priv->icon, &icon_name, &size);
			g_value_set_string(value, icon_name);
			break;
		}
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
	}
}

static void
mini_dialog_set_title(PidginMiniDialog *self,
                      const char *title)
{
	PidginMiniDialogPrivate *priv = PIDGIN_MINI_DIALOG_GET_PRIVATE(self);

	char *title_esc = g_markup_escape_text(title, -1);
	char *title_markup = g_strdup_printf(
		"<span weight=\"bold\" size=\"smaller\">%s</span>",
		title_esc ? title_esc : "");

	gtk_label_set_markup(priv->title, title_markup);

	g_free(title_esc);
	g_free(title_markup);
}

static void
mini_dialog_set_description(PidginMiniDialog *self,
                            const char *description)
{
	PidginMiniDialogPrivate *priv = PIDGIN_MINI_DIALOG_GET_PRIVATE(self);
	if(description)
	{
		char *desc_esc = g_markup_escape_text(description, -1);
		char *desc_markup = g_strdup_printf(
			"<span size=\"smaller\">%s</span>", desc_esc);

		gtk_label_set_markup(priv->desc, desc_markup);

		g_free(desc_esc);
		g_free(desc_markup);

		gtk_widget_show(GTK_WIDGET(priv->desc));
		g_object_set(G_OBJECT(priv->desc), "no-show-all", FALSE, NULL);
	}
	else
	{
		gtk_label_set_text(priv->desc, NULL);
		gtk_widget_hide(GTK_WIDGET(priv->desc));
		/* make calling show_all() on the minidialog not affect desc
		 * even though it's packed inside it.
	 	 */
		g_object_set(G_OBJECT(priv->desc), "no-show-all", TRUE, NULL);
	}
}

static void
pidgin_mini_dialog_set_property(GObject *object,
                                guint property_id,
                                const GValue *value,
                                GParamSpec *pspec)
{
	PidginMiniDialog *self = PIDGIN_MINI_DIALOG(object);
	PidginMiniDialogPrivate *priv = PIDGIN_MINI_DIALOG_GET_PRIVATE(self);

	switch (property_id) {
		case PROP_TITLE:
			mini_dialog_set_title(self, g_value_get_string(value));
			break;
		case PROP_DESCRIPTION:
			mini_dialog_set_description(self, g_value_get_string(value));
			break;
		case PROP_ICON_NAME:
			gtk_image_set_from_stock(priv->icon, g_value_get_string(value),
				gtk_icon_size_from_name(PIDGIN_ICON_SIZE_TANGO_EXTRA_SMALL));
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
	}
}

static void
pidgin_mini_dialog_finalize(GObject *object)
{
	PidginMiniDialog *self = PIDGIN_MINI_DIALOG(object);
	PidginMiniDialogPrivate *priv = PIDGIN_MINI_DIALOG_GET_PRIVATE(self);

	if (priv->idle_destroy_cb_id)
		g_source_remove(priv->idle_destroy_cb_id);

	g_free(priv);
	self->priv = NULL;

	purple_prefs_disconnect_by_handle(self);

	G_OBJECT_CLASS (pidgin_mini_dialog_parent_class)->finalize (object);
}

static void
pidgin_mini_dialog_class_init(PidginMiniDialogClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS(klass);
	GParamSpec *param_spec;

	object_class->get_property = pidgin_mini_dialog_get_property;
	object_class->set_property = pidgin_mini_dialog_set_property;
	object_class->finalize = pidgin_mini_dialog_finalize;

	param_spec = g_param_spec_string("title", "title",
		"String specifying the mini-dialog's title", NULL,
		G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB |
		G_PARAM_READWRITE);
	g_object_class_install_property (object_class, PROP_TITLE, param_spec);

	param_spec = g_param_spec_string("description", "description",
		"Description text for the mini-dialog, if desired", NULL,
		G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB |
		G_PARAM_READWRITE);
	g_object_class_install_property (object_class, PROP_DESCRIPTION, param_spec);

	param_spec = g_param_spec_string("icon-name", "icon-name",
		"String specifying the Gtk stock name of the dialog's icon",
		NULL,
		G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB |
		G_PARAM_READWRITE);
	g_object_class_install_property (object_class, PROP_ICON_NAME, param_spec);
}

/* 16 is the width of the icon, due to PIDGIN_ICON_SIZE_TANGO_EXTRA_SMALL */
#define BLIST_WIDTH_OTHER_THAN_LABEL \
	((PIDGIN_HIG_BOX_SPACE * 3) + 16)

#define BLIST_WIDTH_PREF \
	(PIDGIN_PREFS_ROOT "/blist/width")

static void
blist_width_changed_cb(const char *name,
                       PurplePrefType type,
                       gconstpointer val,
                       gpointer data)
{
	PidginMiniDialog *self = PIDGIN_MINI_DIALOG(data);
	PidginMiniDialogPrivate *priv = PIDGIN_MINI_DIALOG_GET_PRIVATE(self);
	guint blist_width = GPOINTER_TO_INT(val);
	guint label_width = blist_width - BLIST_WIDTH_OTHER_THAN_LABEL;

	gtk_widget_set_size_request(GTK_WIDGET(priv->title), label_width, -1);
	gtk_widget_set_size_request(GTK_WIDGET(priv->desc), label_width, -1);
}

static void
pidgin_mini_dialog_init(PidginMiniDialog *self)
{
	GtkBox *self_box = GTK_BOX(self);
	guint blist_width = purple_prefs_get_int(BLIST_WIDTH_PREF);
	guint label_width = blist_width - BLIST_WIDTH_OTHER_THAN_LABEL;

	PidginMiniDialogPrivate *priv = g_new0(PidginMiniDialogPrivate, 1);
	self->priv = priv;

	gtk_container_set_border_width(GTK_CONTAINER(self), PIDGIN_HIG_BOX_SPACE);

	priv->title_box = GTK_BOX(gtk_hbox_new(FALSE, PIDGIN_HIG_BOX_SPACE));

	priv->icon = GTK_IMAGE(gtk_image_new());
	gtk_misc_set_alignment(GTK_MISC(priv->icon), 0, 0);

	priv->title = GTK_LABEL(gtk_label_new(NULL));
	gtk_widget_set_size_request(GTK_WIDGET(priv->title), label_width, -1);
	gtk_label_set_line_wrap(priv->title, TRUE);
	gtk_label_set_selectable(priv->title, TRUE);
	gtk_misc_set_alignment(GTK_MISC(priv->title), 0, 0);

	gtk_box_pack_start(priv->title_box, GTK_WIDGET(priv->icon), FALSE, FALSE, 0);
	gtk_box_pack_start(priv->title_box, GTK_WIDGET(priv->title), TRUE, TRUE, 0);

	priv->desc = GTK_LABEL(gtk_label_new(NULL));
	gtk_widget_set_size_request(GTK_WIDGET(priv->desc), label_width, -1);
	gtk_label_set_line_wrap(priv->desc, TRUE);
	gtk_misc_set_alignment(GTK_MISC(priv->desc), 0, 0);
	gtk_label_set_selectable(priv->desc, TRUE);
	/* make calling show_all() on the minidialog not affect desc even though
	 * it's packed inside it.
	 */
	g_object_set(G_OBJECT(priv->desc), "no-show-all", TRUE, NULL);

	purple_prefs_connect_callback(self, BLIST_WIDTH_PREF,
		blist_width_changed_cb, self);

	self->contents = GTK_BOX(gtk_vbox_new(FALSE, 0));

	priv->buttons = GTK_BOX(gtk_hbox_new(FALSE, 0));

	gtk_box_pack_start(self_box, GTK_WIDGET(priv->title_box), FALSE, FALSE, 0);
	gtk_box_pack_start(self_box, GTK_WIDGET(priv->desc), FALSE, FALSE, 0);
	gtk_box_pack_start(self_box, GTK_WIDGET(self->contents), TRUE, TRUE, 0);
	gtk_box_pack_start(self_box, GTK_WIDGET(priv->buttons), FALSE, FALSE, 0);

	gtk_widget_show_all(GTK_WIDGET(self));
}