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
| /* yaogc-window.c */
#include <glib.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include "yaogc-window.h"
#define __YAOGC_WINDOW_XML___ "yaogc-window.xml"
struct _YaogcWindowPrivate {
GtkUIManager *manager;
GtkActionGroup *action_group;
GtkWidget *menu_bar;
GtkWidget *dialog_quit;
};
/* Define new type YaogcWindow, create yaogc_window_get_type
* and yaogc_window_parent
*/
G_DEFINE_TYPE (YaogcWindow, yaogc_window, GTK_TYPE_WINDOW)
static gboolean delete_event_handler (GtkWidget *widget, GdkEvent *event, gpointer data);
static void response_event_handler (GtkDialog *dialog, gint response_id, gpointer data);
static void file_quit_cb (GtkAction *action, YaogcWindow *window);
static void edit_preferences_cb (GtkAction *action, YaogcWindow *window);
static void help_about_cb (GtkAction *action, YaogcWindow *window);
static void yaogc_window_realize (GtkWidget *widget);
static void yaogc_window_size_request (GtkWidget *widget, GtkRequisition *requisition);
static void yaogc_window_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
static void yaogc_window_dispose (GObject *object);
static void yaogc_window_finalize (GObject *object);
#ifdef __YAOGC_ENABLE_DEBUG___
static void
yaogc_window_size_request_cb (GtkWidget *widget, GtkRequisition *requisition)
{
g_print ("\n[window %p] size-request result %d : %d\n",
(gpointer)widget, requisition->width, requisition->height);
}
static void
yaogc_window_size_allocate_cb (GtkWidget *widget, GtkAllocation *allocation)
{
g_print ("\n[window %p] size-alloc result %d : %d at (%d, %d)\n",
(gpointer)widget,
allocation->width, allocation->height,
allocation->x, allocation->y);
}
#endif
static void
yaogc_window_init (YaogcWindow *window)
{
GtkActionEntry menu_entries[] = {
/* Toplevel */
{"File", NULL, "_File", NULL, NULL, NULL},
{"Edit", NULL, "_Edit", NULL, NULL, NULL},
{"Help", NULL, "_Help", NULL, NULL, NULL},
/* File menu */
{"FileQuit", GTK_STOCK_QUIT, "_Quit", "<shift><control>Q", NULL, G_CALLBACK(file_quit_cb)},
/* Edit menu */
{"EditPreferences", GTK_STOCK_PREFERENCES, "_Preferences", "<shift><control>P", NULL, G_CALLBACK(edit_preferences_cb)},
/* Help menu */
{"HelpAbout", GTK_STOCK_ABOUT, "_About Yaogc", "<shift><control>A", NULL, G_CALLBACK(help_about_cb)}
};
YaogcWindowPrivate *priv;
GtkWidget *main_vbox;
GtkUIManager *manager;
GtkActionGroup *action_group;
GError *error;
/* Getting private members */
priv = window->priv = G_TYPE_INSTANCE_GET_PRIVATE (window, YAOGC_TYPE_WINDOW, YaogcWindowPrivate);
#ifdef __YAOGC_ENABLE_DEBUG___
g_signal_connect_after (window, "size-request", G_CALLBACK(yaogc_window_size_request_cb), NULL);
g_signal_connect_after (window, "size-allocate", G_CALLBACK(yaogc_window_size_allocate_cb), NULL);
#endif
/* Setting window geometry */
gtk_window_set_default_size (GTK_WINDOW(window), 640, 480);
/* Creating the main YaogcWindow's box */
main_vbox = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER(window), main_vbox);
gtk_widget_show (main_vbox);
/* Creating the work area */
window->work_area = gtk_vbox_new (FALSE, 0);
gtk_box_pack_end (GTK_BOX(main_vbox), window->work_area, TRUE, TRUE, 0);
/* Creating UI manager */
manager = priv->manager = gtk_ui_manager_new ();
/* Create the actions */
action_group = priv->action_group = gtk_action_group_new ("Main");
gtk_action_group_add_actions (action_group, menu_entries, G_N_ELEMENTS(menu_entries), window);
/* Insert action group to UI manager */
gtk_ui_manager_insert_action_group (manager, action_group, 0);
/* Load the UI */
error = NULL;
gtk_ui_manager_add_ui_from_file (manager, __YAOGC_WINDOW_XML___, &error);
if (error) {
g_printerr ("Failed to load UI: %s\n", error->message);
g_error_free (error);
}
/* Adding menubar */
priv->menu_bar = gtk_ui_manager_get_widget (manager, "/menubar");
gtk_box_pack_start (GTK_BOX(main_vbox), priv->menu_bar, FALSE, FALSE, 0);
gtk_widget_show (priv->menu_bar);
/* Create quit dialog */
priv->dialog_quit = gtk_message_dialog_new (GTK_WINDOW(window),
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_QUESTION,
GTK_BUTTONS_YES_NO,
"Voulez vous vraiment quitter\nl'application ?");
g_signal_connect (G_OBJECT(window), "delete-event", G_CALLBACK(delete_event_handler), NULL);
g_signal_connect (G_OBJECT(priv->dialog_quit), "response", G_CALLBACK(response_event_handler), NULL);
}
static void
yaogc_window_class_init (YaogcWindowClass *window_klass)
{
GObjectClass *object_class;
object_class = G_OBJECT_CLASS(window_klass);
object_class->dispose = yaogc_window_dispose;
object_class->finalize = yaogc_window_finalize;
g_type_class_add_private (object_class, sizeof(YaogcWindowPrivate));
}
static gboolean
delete_event_handler (GtkWidget *widget, GdkEvent *event, gpointer data)
{
YaogcWindow *window;
window = YAOGC_WINDOW (widget);
gtk_widget_show_all (window->priv->dialog_quit);
return TRUE;
}
static void
response_event_handler (GtkDialog *dialog, gint response_id, gpointer data)
{
gtk_widget_hide_all (GTK_WIDGET(dialog));
if (response_id == GTK_RESPONSE_YES
|| response_id == GTK_RESPONSE_NONE)
gtk_main_quit ();
}
static void
file_quit_cb (GtkAction *action, YaogcWindow *window)
{
g_assert (YAOGC_IS_WINDOW(window));
gtk_widget_show_all (window->priv->dialog_quit);
}
static void
edit_preferences_cb (GtkAction *action, YaogcWindow *window)
{
}
static void
help_about_cb (GtkAction *action, YaogcWindow *window)
{
}
static
void yaogc_window_dispose (GObject *object)
{
G_OBJECT_CLASS (yaogc_window_parent_class)->dispose (object);
}
static
void yaogc_window_finalize (GObject *object)
{
YaogcWindow *window = YAOGC_WINDOW (object);
if (window->priv->manager) {
g_object_unref (window->priv->manager);
window->priv->manager = NULL;
}
G_OBJECT_CLASS (yaogc_window_parent_class)->finalize (object);
}
GtkWidget*
yaogc_window_new (void)
{
YaogcWindow *window;
window = YAOGC_WINDOW (g_object_new(YAOGC_TYPE_WINDOW, NULL));
return GTK_WIDGET(window);
} |
Partager