source: trunk/gtk/util.c @ 5

Last change on this file since 5 was 5, checked in by root, 16 years ago

Update 2005-11-18

File size: 3.2 KB
Line 
1/*
2  Copyright (c) 2005 Joshua Elsasser. All rights reserved.
3   
4  Redistribution and use in source and binary forms, with or without
5  modification, are permitted provided that the following conditions
6  are met:
7   
8   1. Redistributions of source code must retain the above copyright
9      notice, this list of conditions and the following disclaimer.
10   2. Redistributions in binary form must reproduce the above copyright
11      notice, this list of conditions and the following disclaimer in the
12      documentation and/or other materials provided with the distribution.
13   
14  THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS "AS IS" AND
15  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  POSSIBILITY OF SUCH DAMAGE.
25*/
26
27#include <stdarg.h>
28#include <gtk/gtk.h>
29
30#include "util.h"
31
32static void
33errcb(GtkWidget *widget, int resp, gpointer data);
34
35gboolean
36strbool(const char *str) {
37  switch(str[0]) {
38    case 'y':
39    case 'Y':
40    case '1':
41    case 'j':
42    case 'e':
43      return TRUE;
44    default:
45      if(0 == g_ascii_strcasecmp("on", str))
46        return TRUE;
47      break;
48  }
49
50  return FALSE;
51}
52
53GtkWidget *
54errmsg(GtkWidget *wind, const char *format, ...) {
55  GtkWidget *dialog;
56  va_list ap;
57
58  va_start(ap, format);
59  dialog = verrmsg(wind, NULL, NULL, format, ap);
60  va_end(ap);
61
62  return dialog;
63}
64
65GtkWidget *
66errmsg_full(GtkWidget *wind, errfunc_t func, void *data,
67            const char *format, ...) {
68  GtkWidget *dialog;
69  va_list ap;
70
71  va_start(ap, format);
72  dialog = verrmsg(wind, func, data, format, ap);
73  va_end(ap);
74
75  return dialog;
76}
77
78GtkWidget *
79verrmsg(GtkWidget *wind, errfunc_t func, void *data,
80        const char *format, va_list ap) {
81  GtkWidget *dialog;
82  char *msg;
83  GList *funcdata;
84
85  msg = g_strdup_vprintf(format, ap);
86
87  if(NULL == wind)
88    dialog = gtk_message_dialog_new(
89      NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", msg);
90  else
91    dialog = gtk_message_dialog_new(
92      GTK_WINDOW(wind), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
93      GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", msg);
94
95  if(NULL == func)
96    funcdata = NULL;
97  else
98    funcdata = g_list_append(g_list_append(NULL, func), data);
99  g_signal_connect(dialog, "response", G_CALLBACK(errcb), funcdata);
100  if(NULL != wind)
101    gtk_widget_show_all(dialog);
102  g_free(msg);
103
104  return dialog;
105}
106
107static void
108errcb(GtkWidget *widget, int resp SHUTUP, gpointer data) {
109  GList *funcdata;
110  errfunc_t func;
111
112  if(NULL != data) {
113    funcdata = g_list_first(data);
114    func = funcdata->data;
115    data = funcdata->next->data;
116    func(data);
117    g_list_free(funcdata);
118  }
119
120  gtk_widget_destroy(widget);
121}
Note: See TracBrowser for help on using the repository browser.