aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/msgbox.c
blob: f9a8cb7fe89a11fbb9750d3ad88b06f746276b78 (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
/* msgbox.c - 2000/10/13 */
/*
 *  EasyTAG - Tag editor for MP3 and Ogg Vorbis files
 *  Copyright (C) 2000-2003  Jerome Couderc <easytag@gmail.com>
 *
 *  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
/* Note: Some pieces of codes come from gnome-dialog.c
 */

#include <config.h>

#include <gtk/gtk.h>
#include <glib/gi18n-lib.h>
#include <stdarg.h>

#include "msgbox.h"
#include "easytag.h"
#include "misc.h"
#include "setting.h"


static void msg_box_class_init (MsgBoxClass *class);
static void msg_box_init (MsgBox *mb);

static void msg_box_show (GtkWidget *widget);
static gint msg_box_delete_event (GtkWidget *widget, GdkEventAny *event);
static void msg_box_button_clicked (GtkButton *button, gpointer data);
void        check_button_toggled (GtkCheckButton *checkbutton, gpointer data);


static GtkDialogClass *parent_klass = NULL;


GType msg_box_get_type(void)
{
    static GType mb_type = 0;

    if (!mb_type)
    {
        GTypeInfo mb_info =
        {
            sizeof(MsgBoxClass),
            NULL,
            NULL,
            (GClassInitFunc) msg_box_class_init,
            NULL,
            NULL,
            sizeof(MsgBox),
            0,
            (GInstanceInitFunc) msg_box_init
        };
        mb_type = g_type_register_static(GTK_TYPE_DIALOG, "MsgBox", &mb_info, 0);
    }
    return mb_type;
}


static void msg_box_button_clicked (GtkButton *button, gpointer data)
{
    MsgBox *mb;

    g_return_if_fail( (mb = MSG_BOX(data)) );

    mb->button_clicked = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button),"button_value"));

    if (gtk_main_level()>1)
        gtk_main_quit();
}


static void msg_box_class_init (MsgBoxClass *class)
{
    GtkObjectClass *object_class;
    GtkWidgetClass *widget_class;
    GtkDialogClass *dialog_class;

    object_class = (GtkObjectClass*) class;
    widget_class = (GtkWidgetClass*) class;
    dialog_class = (GtkDialogClass*) class;

    parent_klass = gtk_type_class(gtk_dialog_get_type());

    object_class->destroy      = msg_box_destroy;
    widget_class->delete_event = msg_box_delete_event;
    widget_class->show         = msg_box_show;
}


static void msg_box_init (MsgBox *mb)
{
    g_return_if_fail(mb != NULL);
    g_return_if_fail(IS_MSG_BOX(mb));

    mb->icon                = -1;
    mb->check_button        = NULL;
    mb->check_button_state  = 0;

}


/*
 * Create a new message box
 */
GtkWidget *msg_box_new (gchar *title, gchar *message, const gchar *stock_id,
            /* Put all the buttons to display, and terminate by 0 */
            ...)
{
    MsgBox *mb;
    GtkWidget *Table;
    GtkWidget *Pixmap;
    GtkWidget *Label;
    GtkWidget *ButtonBox;
    GtkWidget *Button = NULL;
    va_list cursor;
    gint cursor_value;


    g_return_val_if_fail(message!=NULL, NULL);

    mb = MSG_BOX(g_object_new(TYPE_MSG_BOX, NULL));
    //mb->icon = icon;
    mb->check_button = gtk_check_button_new_with_label(_("Repeat action for the rest of the files")); /* Can save or cancel all files */

    /* Window configuration */
    gtk_window_set_title(GTK_WINDOW(mb),title);
    gtk_window_set_transient_for(GTK_WINDOW(mb),GTK_WINDOW(MainWindow));
    gtk_window_set_modal(GTK_WINDOW(mb),TRUE);

    if (MESSAGE_BOX_POSITION_NONE)
        gtk_window_set_position(GTK_WINDOW(mb),GTK_WIN_POS_NONE);
    else if (MESSAGE_BOX_POSITION_CENTER)
        gtk_window_set_position(GTK_WINDOW(mb),GTK_WIN_POS_CENTER);
    else if (MESSAGE_BOX_POSITION_MOUSE)
        gtk_window_set_position(GTK_WINDOW(mb),GTK_WIN_POS_MOUSE);
    else if (MESSAGE_BOX_POSITION_CENTER_ON_PARENT)
        gtk_window_set_position(GTK_WINDOW(mb),GTK_WIN_POS_CENTER_ON_PARENT);


    /* Table to put: the pixmap, the message and the check button */
    Table = gtk_table_new(2,2,FALSE);
    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(mb)->vbox),Table,TRUE,TRUE,0);
    gtk_container_set_border_width(GTK_CONTAINER(Table),4);
    gtk_widget_show(Table);

    /* The Pixmap */
    Pixmap = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_DIALOG);
    gtk_table_attach_defaults(GTK_TABLE(Table),Pixmap,0,1,0,1);
    //gtk_table_attach(GTK_TABLE(Table),Pixmap,0,1,0,1,GTK_FILL,GTK_FILL,0,0);
    gtk_misc_set_padding(GTK_MISC(Pixmap),6,6);
    gtk_widget_show(Pixmap);

    /* The Message */
    Label = gtk_label_new (message);
    gtk_table_attach_defaults(GTK_TABLE(Table),Label,1,2,0,1);
    gtk_misc_set_padding(GTK_MISC(Label),6,6);
    gtk_label_set_justify(GTK_LABEL(Label),GTK_JUSTIFY_CENTER);
    //gtk_label_set_line_wrap(GTK_LABEL(Label),TRUE);
    gtk_widget_show(Label);

    /* The Check Button */
    gtk_table_attach_defaults(GTK_TABLE(Table),mb->check_button,0,2,1,2);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mb->check_button),mb->check_button_state);
    g_signal_connect(G_OBJECT(mb->check_button),"toggled",G_CALLBACK(check_button_toggled),mb);
    gtk_widget_show(mb->check_button);


    /* Buttons */
    ButtonBox = gtk_hbutton_box_new();
    gtk_box_set_spacing(GTK_BOX(ButtonBox), 15);
    gtk_button_box_set_layout(GTK_BUTTON_BOX(ButtonBox), GTK_BUTTONBOX_END);
    gtk_container_add(GTK_CONTAINER(GTK_DIALOG(mb)->action_area),ButtonBox);

    /* Read buttons from variable arguments */
    va_start(cursor,stock_id);
    while ( (cursor_value = va_arg(cursor,gint)) != 0 )
    {
        Button = Create_Button_With_Pixmap(cursor_value);
        gtk_container_add(GTK_CONTAINER(ButtonBox),Button);
        GTK_WIDGET_SET_FLAGS(Button,GTK_CAN_DEFAULT);
        g_signal_connect(G_OBJECT(Button),"destroy", G_CALLBACK(gtk_widget_destroyed),&Button);
        g_signal_connect(G_OBJECT(Button),"clicked", G_CALLBACK(msg_box_button_clicked),mb);
        g_object_set_data(G_OBJECT(Button),"button_value", GINT_TO_POINTER(cursor_value));
    }
    va_end(cursor);
    gtk_widget_grab_default(Button);
    gtk_widget_show_all(ButtonBox);

    return GTK_WIDGET(mb);
}


void msg_box_destroy (GtkObject *object)
{
    MsgBox *mb;

    g_return_if_fail( (mb = MSG_BOX(object)) );

    if (mb->check_button)
        gtk_widget_destroy(mb->check_button);

/* FIXME: causes segfault in some cases (when callin gtk_widget_destroy(msgbox) ) */
/*    if (GTK_OBJECT_CLASS(parent_klass)->destroy)
 *        (* GTK_OBJECT_CLASS(parent_klass)->destroy) (object);
 */
}


static gint msg_box_delete_event (GtkWidget *widget, GdkEventAny *event)
{
    MsgBox *mb;

    g_return_val_if_fail((mb = MSG_BOX(widget)), FALSE);

    /* If the window is closed whitout pressing a button, we return -1 */
    mb->button_clicked = -1;

    if (gtk_main_level()>1)
        gtk_main_quit();

    return TRUE;
}


/*
 * "Run" and show the msgbox, and send which button was pressed (or not a button)
 */
gint msg_box_run (MsgBox *msgbox)
{
    MsgBox *mb;

    g_return_val_if_fail((mb = MSG_BOX(msgbox)),0);

    gtk_widget_show(GTK_WIDGET(mb));
    gtk_main();
    gtk_widget_hide(GTK_WIDGET(mb));
    gtk_window_set_modal(GTK_WINDOW(mb),FALSE);

    return (MSG_BOX(mb)->button_clicked);
}


static void msg_box_show (GtkWidget *widget)
{
    if (GTK_WIDGET_CLASS(parent_klass)->show)
        (* (GTK_WIDGET_CLASS(parent_klass)->show))(widget);
}


/*
 * Callback when the check_button is pressed
 */
void check_button_toggled (GtkCheckButton *checkbutton, gpointer data)
{
    MsgBox *mb;

    g_return_if_fail((mb = MSG_BOX(data)));

    mb->check_button_state = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbutton));
}


/*
 * Set the check_button to the 'is_active' state
 */
void msg_box_check_button_set_active (MsgBox *msgbox, gboolean is_active)
{
    MsgBox *mb;

    g_return_if_fail((mb = MSG_BOX(msgbox)));

    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mb->check_button),is_active);
    mb->check_button_state = is_active;
}


/*
 * Get state of the check_button
 */
gboolean msg_box_check_button_get_active (MsgBox *msgbox)
{
    MsgBox *mb;

    g_return_val_if_fail((mb = MSG_BOX(msgbox)),FALSE);

    return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(mb->check_button));
}


/*
 * Hide the check_button.
 */
void msg_box_hide_check_button (MsgBox *msgbox)
{
    MsgBox *mb;

    g_return_if_fail((mb = MSG_BOX(msgbox)));

    gtk_widget_hide(mb->check_button);
}