File: dialogs.c

package info (click to toggle)
libxfcegui4 4.0.6-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,372 kB
  • ctags: 1,672
  • sloc: ansic: 16,344; sh: 8,496; makefile: 207
file content (385 lines) | stat: -rw-r--r-- 9,311 bytes parent folder | download
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
/*  xfce4
 *  
 *  Copyright (C) 2003 Jasper Huijsmans (huysmans@users.sourceforge.net)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <ctype.h>
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#ifdef HAVE_MEMORY_H
#include <memory.h>
#endif
#include <stdio.h>
#ifdef HAVE_STDDEF_H
#include <stddef.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include <glib.h>
#include <gtk/gtk.h>

#include "dialogs.h"

#ifndef PATH_MAX
#define DEFAULT_LENGTH 1024
#else
#if (PATH_MAX < 1024)
#define DEFAULT_LENGTH 1024
#else
#define DEFAULT_LENGTH PATH_MAX
#endif
#endif

/* max. length of a message */
#define MAXMESSAGELEN   2048

extern char **environ;

/* create a header with optional icon (may be NULL) in larger bold font;
 * background and foreground colors are taken from gtk */

static void
private_cb_eventbox_style_set (GtkWidget * widget, GtkStyle * old_style)
{
    static gboolean recursive = 0;
    GtkStyle *style;

    if (recursive > 0)
	return;

    ++recursive;
    style = gtk_widget_get_style (widget);
    gtk_widget_modify_bg (widget, GTK_STATE_NORMAL,
			  &style->bg[GTK_STATE_SELECTED]);
    --recursive;
}

static void
private_cb_label_style_set (GtkWidget * widget, GtkStyle * old_style)
{
    static gboolean recursive = 0;
    GtkStyle *style;

    if (recursive > 0)
	return;

    ++recursive;
    style = gtk_widget_get_style (widget);
    gtk_widget_modify_fg (widget, GTK_STATE_NORMAL,
			  &style->fg[GTK_STATE_SELECTED]);
    --recursive;
}

GtkWidget *
create_header (GdkPixbuf * icon, const char *text)
{
    GtkWidget *eventbox, *label, *hbox, *image;
    GtkStyle *style;
    char *markup;

    eventbox = gtk_event_box_new ();
    gtk_widget_show (eventbox);

    hbox = gtk_hbox_new (FALSE, 12);
    gtk_container_set_border_width (GTK_CONTAINER (hbox), 4);
    gtk_widget_show (hbox);
    gtk_container_add (GTK_CONTAINER (eventbox), hbox);

    if (icon)
    {
	image = gtk_image_new_from_pixbuf (icon);
	gtk_widget_show (image);
	gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
    }

    style = gtk_widget_get_style (eventbox);
    gtk_widget_modify_bg (eventbox, GTK_STATE_NORMAL,
			  &style->bg[GTK_STATE_SELECTED]);

    markup =
	g_strconcat ("<span size=\"larger\" weight=\"bold\">", text,
		     "</span>", NULL);
    label = gtk_label_new (markup);
    g_free (markup);
    gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
/*    gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);*/
    gtk_widget_show (label);
    gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
    style = gtk_widget_get_style (label);
    gtk_widget_modify_fg (label, GTK_STATE_NORMAL,
			  &style->fg[GTK_STATE_SELECTED]);

    g_signal_connect_after (G_OBJECT (eventbox), "style_set",
			    G_CALLBACK (private_cb_eventbox_style_set), NULL);
    g_signal_connect_after (G_OBJECT (label), "style_set",
			    G_CALLBACK (private_cb_label_style_set), NULL);

    return eventbox;
}

/* creates a button with stock icon and text */
GtkWidget *
mixed_button_new (const char *stock, const char *text)
{
    GtkWidget *button, *align, *image, *hbox, *label;

    button = gtk_button_new ();
    label = gtk_label_new_with_mnemonic (text);
    gtk_label_set_mnemonic_widget (GTK_LABEL (label), button);

    image = gtk_image_new_from_stock (stock, GTK_ICON_SIZE_BUTTON);
    hbox = gtk_hbox_new (FALSE, 2);

    align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);

    gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
    gtk_box_pack_end (GTK_BOX (hbox), label, FALSE, FALSE, 0);

    gtk_container_add (GTK_CONTAINER (button), align);
    gtk_container_add (GTK_CONTAINER (align), hbox);
    gtk_widget_show_all (align);

    return button;
}

/* Create a small italic label */
GtkWidget *
small_label (const char *text)
{
    GtkWidget *label;
    gchar *tmp;

    g_return_val_if_fail (text != NULL, NULL);

    tmp = g_strdup_printf ("<small><i>%s</i></small>", text);
    label = gtk_label_new (tmp);
    gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
    g_free (tmp);

    return label;
}

static void
message_dialog (GtkMessageType type, const gchar * message)
{
    GtkWidget *dlg;

    dlg = gtk_message_dialog_new (NULL,
				  GTK_DIALOG_MODAL,
				  type, GTK_BUTTONS_CLOSE, message);

    gtk_window_set_position (GTK_WINDOW (dlg), GTK_WIN_POS_CENTER);
    gtk_dialog_run (GTK_DIALOG (dlg));
    gtk_widget_destroy (dlg);
}

/* new wrappers around gtk_message_dialog */
void
xfce_err (const gchar * format, ...)
{
    va_list ap;

    va_start (ap, format);
    xfce_verr (format, ap);
    va_end (ap);
}

void
xfce_verr (const gchar * format, va_list ap)
{
    gchar message_buffer[MAXMESSAGELEN];

    g_return_if_fail (format != NULL);

    g_vsnprintf (message_buffer, sizeof (message_buffer), format, ap);
    message_dialog (GTK_MESSAGE_ERROR, message_buffer);
}

void
xfce_warn (const gchar * format, ...)
{
    va_list ap;

    va_start (ap, format);
    xfce_vwarn (format, ap);
    va_end (ap);
}

void
xfce_vwarn (const gchar * format, va_list ap)
{
    gchar message_buffer[MAXMESSAGELEN];

    g_return_if_fail (format != NULL);

    g_vsnprintf (message_buffer, sizeof (message_buffer), format, ap);
    message_dialog (GTK_MESSAGE_WARNING, message_buffer);
}

void
xfce_info (const gchar * format, ...)
{
    va_list ap;

    va_start (ap, format);
    xfce_vinfo (format, ap);
    va_end (ap);
}

void
xfce_vinfo (const gchar * format, va_list ap)
{
    gchar message_buffer[MAXMESSAGELEN];

    g_return_if_fail (format != NULL);

    g_vsnprintf (message_buffer, sizeof (message_buffer), format, ap);
    message_dialog (GTK_MESSAGE_INFO, message_buffer);
}

/* wrappers around gtk_message_dialog (OBSOLETE) */
void
show_info (const char *text)
{
    message_dialog (GTK_MESSAGE_INFO, text);
}

void
show_warning (const char *text)
{
    message_dialog (GTK_MESSAGE_WARNING, text);
}

void
show_error (const char *text)
{
    message_dialog (GTK_MESSAGE_ERROR, text);
}

/* confirmation dialog; has 'cancel' and 'confirm' buttons.
   if 'action' is given this text is used on the confirm button
   together with the stock icon given by 'stock'.
   if 'action' is NULL, the stock text is used as well.
*/
gboolean
confirm (const char *text, const char *stock, const char *action)
{
    GtkWidget *dialog, *button;
    int response = GTK_RESPONSE_NONE;

    dialog = gtk_message_dialog_new (NULL,
				     GTK_DIALOG_MODAL,
				     GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
				     text);

    button = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
    gtk_widget_show (button);
    gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
				  GTK_RESPONSE_NO);

    if (action)
	button = mixed_button_new (stock, action);
    else
	button = gtk_button_new_from_stock (stock);
    gtk_widget_show (button);
    gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
				  GTK_RESPONSE_YES);

    gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
    gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);

    response = gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_hide (dialog);
    gtk_widget_destroy (dialog);

    if (response == GTK_RESPONSE_YES)
	return TRUE;
    else
	return FALSE;
}

/* command execution with error dialog */

gboolean
exec_command (gchar * command)
{
    gboolean success = TRUE;
    GError *error = NULL;	/* this must be NULL to prevent crash :( */

    g_return_val_if_fail (command != NULL, FALSE);

    if (!g_spawn_command_line_async (command, &error))
    {
	char *msg = g_strcompress (error->message);
	char *text =
	    g_strconcat ("Could not run command: ", command, ":\n", msg,
			 NULL);
	show_error (text);
	g_free (msg);
	g_free (text);
	g_error_free (error);
	success = FALSE;
    }
    return success;
}

gboolean
exec_command_full_with_envp (gchar ** argv, gchar ** envp)
{
    gboolean success = TRUE;
    gboolean retval;
    GError *error = NULL;	/* this must be NULL to prevent crash :( */

    g_return_val_if_fail (argv != NULL, FALSE);

    if (envp == NULL)
    {
	envp = environ;
    }

    retval =
	g_spawn_async_with_pipes (NULL, argv, envp, G_SPAWN_SEARCH_PATH, 
				  NULL, NULL, NULL, 
				  NULL, NULL, NULL,
				  &error);
    
    if (!retval)
    {
	gchar *msg = g_strcompress (error->message);
	char *text =
	    g_strconcat ("Could not run command: ", argv[0], ":\n", msg,
			 NULL);
	g_error_free (error);
	g_free (msg);
	show_error (text);
	g_free (text);
	success = FALSE;
    }

    return success;
}