GTK+2 panel 

GTK+2 panel Git Source Tree

Root/libgpwidgets/gpw-quit.c

1/* Gtk2Panel - A bleeding edge Gtk+2 panel
2 * Copyright (C) 2009 Nicola Fontana <ntd at entidi.it>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20
21/**
22 * SECTION:gpw-quit
23 * @title: GpwQuit
24 * @short_description: A #GtkAction providing a way to quit the panel
25 *
26 * GpwQuit is a GtkAction with the embedded feature of spawning
27 * the shell command specified by its GpwQuit:confirm-dialog property
28 * whenever the GpwQuit::activate signal is emitted.
29 **/
30
31/**
32 * GpwQuit:
33 *
34 * All fields are private and should not be used directly.
35 * Use its public methods instead.
36 **/
37
38
39#include "gpw-quit.h"
40#include "gpw-quit-private.h"
41#include "gpw-intl.h"
42
43
44enum {
45 PROP_0,
46 PROP_CONFIRM_DIALOG
47};
48
49
50static void get_property (GObject *object,
51 guint prop_id,
52 GValue *value,
53 GParamSpec *pspec);
54static void set_property (GObject *object,
55 guint prop_id,
56 const GValue *value,
57 GParamSpec *pspec);
58static void dispose (GObject *object);
59static void set_confirm_dialog (GpwQuit *quit,
60 GtkDialog *confirm_dialog);
61static void activate (GtkAction *action);
62
63
64G_DEFINE_TYPE(GpwQuit, gpw_quit, GTK_TYPE_ACTION);
65
66
67static void
68gpw_quit_class_init(GpwQuitClass *klass)
69{
70 GObjectClass *gobject_class;
71 GtkActionClass *action_class;
72 GParamSpec *param;
73
74 gobject_class = (GObjectClass *) klass;
75 action_class = (GtkActionClass *) klass;
76
77 g_type_class_add_private(klass, sizeof(GpwQuitPrivate));
78
79 gobject_class->get_property = get_property;
80 gobject_class->set_property = set_property;
81 gobject_class->dispose = dispose;
82
83 action_class->activate = activate;
84
85 param = g_param_spec_object("confirm-dialog",
86 P_("Confirm Dialog"),
87 P_("The dialog to validate before leaving the panel"),
88 GTK_TYPE_DIALOG,
89 G_PARAM_READWRITE);
90 g_object_class_install_property(gobject_class, PROP_CONFIRM_DIALOG, param);
91}
92
93static void
94gpw_quit_init(GpwQuit *quit)
95{
96 GpwQuitPrivate *data;
97
98 data = G_TYPE_INSTANCE_GET_PRIVATE(quit, GPW_TYPE_QUIT, GpwQuitPrivate);
99
100 data->confirm_dialog = NULL;
101
102 quit->data = data;
103}
104
105static void
106get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
107{
108 GpwQuitPrivate *data = GPW_QUIT_GET_DATA(object);
109
110 switch (prop_id) {
111 case PROP_CONFIRM_DIALOG:
112 g_value_set_object(value, data->confirm_dialog);
113 break;
114 default:
115 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
116 break;
117 }
118}
119
120static void
121set_property(GObject *object,
122 guint prop_id, const GValue *value, GParamSpec *pspec)
123{
124 GpwQuit *quit = (GpwQuit *) object;
125
126 switch (prop_id) {
127 case PROP_CONFIRM_DIALOG:
128 set_confirm_dialog(quit, (GtkDialog *) g_value_get_object(value));
129 break;
130 default:
131 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
132 break;
133 }
134}
135
136static void
137dispose(GObject *object)
138{
139 GpwQuit *quit = (GpwQuit *) object;
140
141 set_confirm_dialog(quit, NULL);
142
143 G_OBJECT_CLASS(gpw_quit_parent_class)->dispose(object);
144}
145
146static void
147set_confirm_dialog(GpwQuit *quit, GtkDialog *confirm_dialog)
148{
149 GpwQuitPrivate *data = GPW_QUIT_GET_DATA(quit);
150
151 /* Check if something changed */
152 if (data->confirm_dialog == confirm_dialog)
153 return;
154
155 /* Remove the old dialog, if any */
156 if (data->confirm_dialog != NULL) {
157 g_object_unref(data->confirm_dialog);
158 data->confirm_dialog = NULL;
159 }
160
161 /* Add the new dialog, if any */
162 if (confirm_dialog != NULL) {
163 data->confirm_dialog = confirm_dialog;
164 g_object_ref_sink(confirm_dialog);
165 }
166}
167
168static void
169activate(GtkAction *action)
170{
171 GpwQuitPrivate *data;
172 GtkResponseType response;
173
174 data = GPW_QUIT_GET_DATA(action);
175
176 if (data->confirm_dialog != NULL) {
177 gtk_widget_show_all((GtkWidget *) data->confirm_dialog);
178 response = gtk_dialog_run(data->confirm_dialog);
179 gtk_widget_hide((GtkWidget *) data->confirm_dialog);
180 } else {
181 response = GTK_RESPONSE_YES;
182 }
183
184 if (response == GTK_RESPONSE_YES || response == GTK_RESPONSE_OK)
185gtk_main_quit();
186}

Archive Download this file

Branches

Tags