~ubuntu-branches/debian/squeeze/freeciv/squeeze

« back to all changes in this revision

Viewing changes to client/gui-gtk-2.0/choice_dialog.c

  • Committer: Bazaar Package Importer
  • Author(s): Jordi Mallach
  • Date: 2008-07-08 18:34:23 UTC
  • mfrom: (5.1.9 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080708183423-c80u1h25xmj6h9s0
Tags: 2.1.5-2
Export datarootdir=/usr/share in debian/rules, so make calls can
have a correct value for LOCALEDIR (closes: #489455). Thanks Loïc Minier
for the help to solve this.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/********************************************************************** 
 
2
 Freeciv - Copyright (C) 1996-2005 - Freeciv Development Team
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; either version 2, or (at your option)
 
6
   any later version.
 
7
 
 
8
   This program is distributed in the hope that it will be useful,
 
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
   GNU General Public License for more details.
 
12
***********************************************************************/
 
13
#ifdef HAVE_CONFIG_H
 
14
#include <config.h>
 
15
#endif
 
16
 
 
17
#include <stdarg.h>
 
18
 
 
19
#include <gtk/gtk.h>
 
20
 
 
21
#include "support.h"
 
22
 
 
23
#include "gui_main.h"
 
24
#include "gui_stuff.h"
 
25
 
 
26
#include "choice_dialog.h"
 
27
 
 
28
/****************************************************************
 
29
  Choice dialog: A dialog with a label and a list of buttons
 
30
  placed vertically
 
31
****************************************************************/
 
32
 
 
33
/****************************************************************
 
34
...
 
35
*****************************************************************/
 
36
static GtkWidget* choice_dialog_get_nth_button(GtkWidget *cd,
 
37
                                               int button)
 
38
{
 
39
  char button_name[512];
 
40
  GtkWidget *b;
 
41
 
 
42
  my_snprintf(button_name, sizeof(button_name), "button%d", button);
 
43
 
 
44
  b = g_object_get_data(G_OBJECT(cd), button_name);
 
45
 
 
46
  return b;
 
47
}
 
48
 
 
49
/****************************************************************
 
50
...
 
51
*****************************************************************/
 
52
void choice_dialog_button_set_sensitive(GtkWidget *cd, int button,
 
53
                                         gboolean state)
 
54
{
 
55
  gtk_widget_set_sensitive(choice_dialog_get_nth_button(cd, button), state);
 
56
}
 
57
 
 
58
/****************************************************************
 
59
...
 
60
*****************************************************************/
 
61
void choice_dialog_button_set_label(GtkWidget *cd, int number,
 
62
                                    const char* label)
 
63
{
 
64
  GtkWidget* button = choice_dialog_get_nth_button(cd, number);
 
65
  gtk_button_set_label(GTK_BUTTON(button), label);
 
66
}
 
67
 
 
68
/****************************************************************
 
69
...
 
70
*****************************************************************/
 
71
GtkWidget *choice_dialog_start(GtkWindow *parent, const gchar *name,
 
72
                                const gchar *text)
 
73
{
 
74
  GtkWidget *dshell, *dlabel, *vbox, *bbox;
 
75
 
 
76
  dshell = gtk_window_new(GTK_WINDOW_TOPLEVEL);
 
77
  setup_dialog(dshell, toplevel);
 
78
  gtk_window_set_position (GTK_WINDOW(dshell), GTK_WIN_POS_MOUSE);
 
79
 
 
80
  gtk_window_set_title(GTK_WINDOW(dshell), name);
 
81
 
 
82
  gtk_window_set_transient_for(GTK_WINDOW(dshell), parent);
 
83
  gtk_window_set_destroy_with_parent(GTK_WINDOW(dshell), TRUE);
 
84
 
 
85
  vbox = gtk_vbox_new(FALSE, 5);
 
86
  gtk_container_add(GTK_CONTAINER(dshell),vbox);
 
87
 
 
88
  gtk_container_set_border_width(GTK_CONTAINER(vbox), 5);
 
89
 
 
90
  dlabel = gtk_label_new(text);
 
91
  gtk_container_add(GTK_CONTAINER(vbox), dlabel);
 
92
 
 
93
  bbox = gtk_vbutton_box_new();
 
94
  gtk_box_set_spacing(GTK_BOX(bbox), 2);
 
95
  gtk_container_add(GTK_CONTAINER(vbox), bbox);
 
96
  
 
97
  g_object_set_data(G_OBJECT(dshell), "bbox", bbox);
 
98
  g_object_set_data(G_OBJECT(dshell), "nbuttons", GINT_TO_POINTER(0));
 
99
  g_object_set_data(G_OBJECT(dshell), "hide", GINT_TO_POINTER(FALSE));
 
100
  
 
101
  gtk_widget_show(vbox);
 
102
  gtk_widget_show(dlabel);
 
103
  
 
104
  return dshell;
 
105
}
 
106
 
 
107
/****************************************************************
 
108
...
 
109
*****************************************************************/
 
110
static void choice_dialog_clicked(GtkWidget *w, gpointer data)
 
111
{
 
112
  if (g_object_get_data(G_OBJECT(data), "hide")) {
 
113
    gtk_widget_hide(GTK_WIDGET(data));
 
114
  } else {
 
115
    gtk_widget_destroy(GTK_WIDGET(data));
 
116
  }
 
117
}
 
118
 
 
119
/****************************************************************
 
120
...
 
121
*****************************************************************/
 
122
void choice_dialog_add(GtkWidget *dshell, const gchar *label,
 
123
                        GCallback handler, gpointer data)
 
124
{
 
125
  GtkWidget *button, *bbox;
 
126
  char name[512];
 
127
  int nbuttons;
 
128
 
 
129
  bbox = g_object_get_data(G_OBJECT(dshell), "bbox");
 
130
  nbuttons = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(dshell), "nbuttons"));
 
131
  g_object_set_data(G_OBJECT(dshell), "nbuttons", GINT_TO_POINTER(nbuttons+1));
 
132
 
 
133
  my_snprintf(name, sizeof(name), "button%d", nbuttons);
 
134
 
 
135
  button = gtk_button_new_from_stock(label);
 
136
  gtk_container_add(GTK_CONTAINER(bbox), button);
 
137
  g_object_set_data(G_OBJECT(dshell), name, button);
 
138
 
 
139
  if (handler) {
 
140
    g_signal_connect(button, "clicked", handler, data);
 
141
  }
 
142
 
 
143
  g_signal_connect_after(button, "clicked",
 
144
                         G_CALLBACK(choice_dialog_clicked), dshell);
 
145
}
 
146
 
 
147
/****************************************************************
 
148
...
 
149
*****************************************************************/
 
150
void choice_dialog_end(GtkWidget *dshell)
 
151
{
 
152
  GtkWidget *bbox;
 
153
  
 
154
  bbox = g_object_get_data(G_OBJECT(dshell), "bbox");
 
155
  
 
156
  gtk_widget_show_all(bbox);
 
157
  gtk_widget_show(dshell);  
 
158
}
 
159
 
 
160
/****************************************************************
 
161
...
 
162
*****************************************************************/
 
163
void choice_dialog_set_hide(GtkWidget *dshell, gboolean setting)
 
164
{
 
165
  g_object_set_data(G_OBJECT(dshell), "hide", GINT_TO_POINTER(setting));
 
166
}
 
167
 
 
168
/****************************************************************
 
169
...
 
170
*****************************************************************/
 
171
GtkWidget *popup_choice_dialog(GtkWindow *parent, const gchar *dialogname,
 
172
                                const gchar *text, ...)
 
173
{
 
174
  GtkWidget *dshell;
 
175
  va_list args;
 
176
  gchar *name;
 
177
  int i;
 
178
 
 
179
  dshell = choice_dialog_start(parent, dialogname, text);
 
180
  
 
181
  i = 0;
 
182
  va_start(args, text);
 
183
 
 
184
  while ((name = va_arg(args, gchar *))) {
 
185
    GCallback handler;
 
186
    gpointer data;
 
187
 
 
188
    handler = va_arg(args, GCallback);
 
189
    data = va_arg(args, gpointer);
 
190
 
 
191
    choice_dialog_add(dshell, name, handler, data);
 
192
  }
 
193
 
 
194
  va_end(args);
 
195
 
 
196
  choice_dialog_end(dshell);
 
197
 
 
198
  return dshell;
 
199
}