File: smallbox.c

package info (click to toggle)
libgpewidget 0.117-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,620 kB
  • ctags: 738
  • sloc: sh: 9,164; ansic: 6,417; makefile: 141
file content (291 lines) | stat: -rw-r--r-- 7,147 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (C) 2002, 2003 Philip Blundell <philb@gnu.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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.
 */

#include <gtk/gtk.h>

#include "smallbox.h"
#include "picturebutton.h"
#include "spacing.h"

#ifdef ENABLE_NLS
#include <libintl.h>
#define _(x) dgettext(PACKAGE, x)
#else
#define _(x) (x)
#endif

void
smallbox_click_ok (GtkWidget *widget, gpointer p)
{
  gtk_main_quit ();
}

void
smallbox_click_cancel (GtkWidget *widget, gpointer p)
{
  gtk_widget_destroy (GTK_WIDGET (p));
}

void
smallbox_note_destruction (GtkWidget *widget, gpointer p)
{
  gboolean *b = (gboolean *)p;

  if (*b == FALSE)
    {
      *b = TRUE;
      
      gtk_main_quit ();
    }
}

/**
 * smallbox_x:
 * @title: Box title.
 * @d: Array of box content descriptions (Query text and default value).
 *
 * Small and simple text input box to query for text input.
 * 
 * Returns: TRUE if a value was entered and confirmed, FALSE otherwise.
 */
gboolean
smallbox_x (gchar *title, struct box_desc *d)
{
  GtkWidget *window = gtk_dialog_new ();
  GtkWidget *table;
  GtkWidget **entry;
  gboolean destroyed = FALSE;
  guint i = 0;
  struct box_desc *di = d;
  GtkWidget *buttonok, *buttoncancel;
  guint gpe_boxspacing = gpe_get_boxspacing ();

  gtk_widget_realize (window);
  buttonok  = gpe_button_new_from_stock (GTK_STOCK_OK, GPE_BUTTON_TYPE_BOTH);
  buttoncancel  = gpe_button_new_from_stock (GTK_STOCK_CANCEL, GPE_BUTTON_TYPE_BOTH);

  while (di->label)
    {
      i++;
      di++;
    }
  
  table = gtk_table_new (i, 2, FALSE);
  entry = g_malloc (i * sizeof (GtkEntry *));

  i = 0;
  di = d;
  
  while (di->label)
    {
      GtkWidget *label = gtk_label_new (di->label);
      entry[i] = gtk_entry_new ();

      if (di->value)
        gtk_entry_set_text (GTK_ENTRY (entry[i]), di->value);

      gtk_table_attach (GTK_TABLE (table), label, 0, 1, i, i + 1, 0, 0, gpe_boxspacing, 1);
      gtk_table_attach_defaults (GTK_TABLE (table), entry[i], 1, 2, i, i + 1);

      i++;
      di++;
    }
  
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->vbox), table, 
		      TRUE, FALSE, 0);

  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->action_area), 
		      buttoncancel, TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->action_area), 
		      buttonok, TRUE, TRUE, 0);

  gtk_signal_connect (GTK_OBJECT (buttonok), "clicked", 
		      GTK_SIGNAL_FUNC (smallbox_click_ok), NULL);
  gtk_signal_connect (GTK_OBJECT (buttoncancel), "clicked", 
		      GTK_SIGNAL_FUNC (smallbox_click_cancel), window);

  gtk_window_set_title (GTK_WINDOW (window), title);
  gtk_window_set_modal (GTK_WINDOW (window), TRUE);

  g_signal_connect (G_OBJECT (window), "destroy",
		    G_CALLBACK (smallbox_note_destruction), &destroyed);

  gtk_window_set_default_size (GTK_WINDOW (window), 200, 100);

  gtk_widget_show_all (window);
  gtk_widget_grab_focus (entry[0]);

  gtk_main ();

  if (destroyed)
    return FALSE;

  destroyed = TRUE;

  i = 0;
  di = d;
  
  while (di->label)
    {
      if (di->value)
        g_free (di->value);
      di->value = gtk_editable_get_chars (GTK_EDITABLE (entry[i]), 0, -1);
      di++;
      i++;
    }

  gtk_widget_destroy (window);

  return TRUE;
}

static GtkWidget *
make_combo (GList *options)
{
  GtkWidget *w = gtk_combo_new ();
  gtk_combo_set_popdown_strings (GTK_COMBO (w), options);
  return w;
}

/**
 * smallbox_x2:
 * @title: Box title.
 * @d: Aray of box content descriptions containing query text, default value and
 *     a list of predefined values.
 *
 * Small and simple text input box to query for text input offering a defined
 * set of values to select from or to enter own text.
 * 
 * Returns: TRUE if a value was entered and confirmed, FALSE otherwise.
 */
gboolean
smallbox_x2 (gchar *title, struct box_desc2 *d)
{
  GtkWidget *window = gtk_dialog_new ();
  GtkWidget *buttonok  = gpe_button_new_from_stock (GTK_STOCK_OK, GPE_BUTTON_TYPE_BOTH);
  GtkWidget *buttoncancel  = gpe_button_new_from_stock (GTK_STOCK_CANCEL, GPE_BUTTON_TYPE_BOTH);
  GtkWidget *table;
  GtkWidget **entry;
  gboolean destroyed = FALSE;
  guint i = 0;
  struct box_desc2 *di = d;
  guint gpe_boxspacing = gpe_get_boxspacing ();

  while (di->label)
    {
      i++;
      di++;
    }
  
  table = gtk_table_new (i, 2, FALSE);
  entry = g_malloc (i * sizeof (GtkEntry *));

  i = 0;
  di = d;
  
  while (di->label)
    {
      GtkWidget *label = gtk_label_new (di->label);
      if (di->suggestions)
	{
	  GtkWidget *combo = make_combo (di->suggestions);
	  entry[i] = GTK_COMBO (combo)->entry;
	  gtk_table_attach_defaults (GTK_TABLE (table), combo, 1, 2, i, i + 1);
	}
      else
	{
	  entry[i] = gtk_entry_new ();
	  gtk_table_attach_defaults (GTK_TABLE (table), entry[i], 1, 2, i, i + 1);
	}

      if (di->value)
	gtk_entry_set_text (GTK_ENTRY (entry[i]), di->value);

      gtk_table_attach (GTK_TABLE (table), label, 0, 1, i, i + 1, 0, 0, gpe_boxspacing, 1);

      i++;
      di++;
    }
  
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->vbox), table, 
		      TRUE, FALSE, 0);

  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->action_area), 
		      buttoncancel, TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->action_area), 
		      buttonok, TRUE, TRUE, 0);

  gtk_signal_connect (GTK_OBJECT (buttonok), "clicked", 
		      GTK_SIGNAL_FUNC (smallbox_click_ok), NULL);
  gtk_signal_connect (GTK_OBJECT (buttoncancel), "clicked", 
		      GTK_SIGNAL_FUNC (smallbox_click_cancel), window);

  gtk_window_set_title (GTK_WINDOW (window), title);
  gtk_window_set_modal (GTK_WINDOW (window), TRUE);

  g_signal_connect (G_OBJECT (window), "destroy",
		    G_CALLBACK (smallbox_note_destruction), &destroyed);

  gtk_window_set_default_size (GTK_WINDOW (window), 200, 100);

  gtk_widget_show_all (window);
  gtk_widget_grab_focus (entry[0]);

  gtk_main ();

  if (destroyed)
    return FALSE;

  destroyed = TRUE;

  i = 0;
  di = d;
  
  while (di->label)
    {
      if (di->value)
	g_free (di->value);
      di->value = gtk_editable_get_chars (GTK_EDITABLE (entry[i]), 0, -1);
      di++;
      i++;
    }

  gtk_widget_destroy (window);

  return TRUE;
}

gchar *
smallbox (gchar *title, gchar *labeltext, gchar *dval)
{
  struct box_desc d[2];

  d[0].label = labeltext;
  d[0].value = g_strdup (dval);
  d[1].label = NULL;
  d[1].value = NULL;

  if (smallbox_x (title, d) == FALSE)
    {
      g_free (d[0].value);
      return NULL;
    }

  return d[0].value;
}