Added README.
[irreco.git] / irreco / src / core / irreco_dlg.c
blobcdb7da24ed54af2bae432045521f3d22cc1ca33a
1 /*
2 * irreco - Ir Remote Control
3 * Copyright (C) 2007 Arto Karppinen (arto.karppinen@iki.fi)
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "irreco_dlg.h"
22 /**
23 * @addtogroup IrrecoDlg
24 * @ingroup Irreco
26 * Base class for Irreco dialogs.
28 * @{
31 /**
32 * @file
33 * Source file of @ref IrrecoDlg.
38 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
39 /* Prototypes */
40 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
42 #define IRRECO_DLG_CHILD_DATA_KEY "IrrecoChildDialog"
44 static void irreco_dlg_data_key_cleanup(IrrecoDlg *self, GtkWindow *parent);
48 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
49 /* Construction & Destruction */
50 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
52 /**
53 * @name Construction & Destruction
54 * @{
57 G_DEFINE_TYPE(IrrecoDlg, irreco_dlg, GTK_TYPE_DIALOG)
59 static void irreco_dlg_finalize(GObject *object)
61 G_OBJECT_CLASS(irreco_dlg_parent_class)->finalize(object);
64 static void irreco_dlg_class_init(IrrecoDlgClass *klass)
66 GObjectClass *object_class = G_OBJECT_CLASS(klass);
67 object_class->finalize = irreco_dlg_finalize;
70 static void irreco_dlg_init(IrrecoDlg *self)
72 IRRECO_ENTER
73 IRRECO_RETURN
76 GtkWidget *irreco_dlg_new(void)
78 return g_object_new(IRRECO_TYPE_DLG, NULL);
81 /** @} */
85 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
86 /* Private Functions */
87 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
93 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
94 /* Public Functions */
95 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
97 /**
98 * @name Public Functions
99 * @{
103 * Find the current topmost window, and set it as the parent window of this
104 * dialog.
106 * Normal Gtk windowing system stores only the parent window of dialogs. It
107 * does not store which children a parent window has. Because of that, it
108 * is not possible to iterate towards the topmost window from the parent windows
109 * until you reach the topmost window. This is a problem because Irreco uses
110 * a lot of dialogs, and dialogs may be created on somewhat random order.
111 * Especially windows created by backends can cause problems if order of window
112 * parentage is not properly handled.
114 * irreco_dlg_set_parent() works around this problem by using
115 * g_object_set_data() function to store a pointer to the current child window
116 * inside the parent window. This in effect creates a linked list of GtkWindows
117 * which can then be iterated until the current topmost window is found.
119 * @param parent
120 * Any GtkWindow which has IrrecoChildDialog data key set or
121 * Irreco main window.
123 void irreco_dlg_set_parent(IrrecoDlg *self, GtkWindow *parent)
125 gint depth = 0;
126 gpointer data;
127 IRRECO_ENTER
129 if (gtk_window_get_transient_for(GTK_WINDOW(self))) {
130 IRRECO_PRINTF("Dialog is already tracient for something.\n");
131 IRRECO_RETURN
134 /* Iterate trough child windows, until we find the one that is
135 topmost on screen. */
136 do {
137 data = g_object_get_data(G_OBJECT(parent),
138 IRRECO_DLG_CHILD_DATA_KEY);
139 if (data != NULL && GTK_IS_WINDOW(data)) {
140 parent = GTK_WINDOW(data);
141 } else {
142 break;
144 depth++;
145 } while (TRUE);
147 IRRECO_PRINTF("Number of parent dialogs on screen: %i\n", depth);
149 /* Set self as a child window. */
150 gtk_window_set_transient_for(GTK_WINDOW(self), parent);
151 g_object_set_data(G_OBJECT(parent), IRRECO_DLG_CHILD_DATA_KEY, self);
153 /* Set up cleanup event. */
154 g_signal_connect(G_OBJECT(self), "destroy",
155 G_CALLBACK(irreco_dlg_data_key_cleanup), parent);
156 IRRECO_RETURN
159 /** @} */
163 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
164 /* Events and Callbacks */
165 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
168 * @name Events and Callbacks
169 * @{
172 static void irreco_dlg_data_key_cleanup(IrrecoDlg *self, GtkWindow *parent)
174 gpointer data;
175 IRRECO_ENTER
177 if (G_IS_OBJECT(parent) == FALSE) IRRECO_RETURN;
179 /* Does parent have a pointer which points to this instace? */
180 data = g_object_get_data(G_OBJECT(parent), IRRECO_DLG_CHILD_DATA_KEY);
181 if (data == NULL || data != self) IRRECO_RETURN;
183 /* Remote the pointer. */
184 g_object_steal_data(G_OBJECT(parent), IRRECO_DLG_CHILD_DATA_KEY);
185 IRRECO_RETURN
188 /** @} */
190 /** @} */