Debian Codegraph @

src/universal-cp/gui.c

raw
1 /*
2 * Copyright (C) 2007 Zeeshan Ali (Khattak) <[email protected]>
3 *
4 * Authors: Zeeshan Ali (Khattak) <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21 #include <config.h>
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <gmodule.h>
27
28 #include "gui.h"
29 #include "device-treeview.h"
30 #include "event-treeview.h"
31 #include "action-dialog.h"
32 #include "icons.h"
33 #include "main.h"
34
35 #define UI_FILE "/org/gupnp/Tools/Universal-CP/gupnp-universal-cp.ui"
36 #define ICON_FILE "/org/gupnp/Tools/Universal-CP/pixmaps/universal-cp.png"
37
38 GtkBuilder *builder;
39 static GtkWidget *main_window;
40
41 gboolean
42 on_delete_event (GtkWidget *widget,
43 GdkEvent *event,
44 gpointer user_data);
45
46 void
47 setup_treeview (GtkWidget *treeview,
48 GtkTreeModel *model,
49 char *headers[],
50 int render_index)
51 {
52 GtkTreeSelection *selection;
53 int i;
54
55 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview));
56 g_assert (selection != NULL);
57
58 for (i = 0; headers[i] != NULL; i++) {
59 GtkCellRenderer *renderer;
60 GtkTreeViewColumn *column;
61
62 column = gtk_tree_view_column_new ();
63 renderer = gtk_cell_renderer_text_new ();
64 gtk_tree_view_column_pack_end (column, renderer, FALSE);
65 gtk_tree_view_column_set_title (column, headers[i]);
66 gtk_tree_view_column_add_attribute (column,
67 renderer,
68 "text", i + render_index);
69 gtk_tree_view_column_set_sizing(column,
70 GTK_TREE_VIEW_COLUMN_AUTOSIZE);
71
72 gtk_tree_view_insert_column (GTK_TREE_VIEW (treeview),
73 column,
74 -1);
75 }
76
77 gtk_tree_view_set_model (GTK_TREE_VIEW (treeview),
78 model);
79 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
80 }
81
82 static void
83 setup_treeviews (void)
84 {
85 setup_details_treeview (builder);
86 setup_event_treeview (builder);
87 setup_device_treeview (builder);
88 }
89
90 G_MODULE_EXPORT
91 gboolean
92 on_delete_event (GtkWidget *widget,
93 GdkEvent *event,
94 gpointer user_data)
95 {
96 application_exit ();
97
98 return TRUE;
99 }
100
101 gboolean
102 init_ui (gint *argc,
103 gchar **argv[])
104 {
105 GtkWidget *about_dialog;
106 GdkPixbuf *icon_pixbuf;
107 GtkWidget *hpaned;
108 GtkWidget *vpaned;
109 gint window_width, window_height;
110 gint position;
111 GError *error = NULL;
112 double w,h;
113
114 gtk_init (argc, argv);
115
116 builder = gtk_builder_new ();
117 g_assert (builder != NULL);
118 gtk_builder_set_translation_domain (builder, GETTEXT_PACKAGE);
119
120 if (!gtk_builder_add_from_resource (builder, UI_FILE, &error)) {
121 g_critical ("Unable to load the GUI file %s: %s",
122 UI_FILE,
123 error->message);
124
125 g_error_free (error);
126 return FALSE;
127 }
128
129 main_window = GTK_WIDGET (gtk_builder_get_object (builder,
130 "main-window"));
131 about_dialog = GTK_WIDGET (gtk_builder_get_object (builder,
132 "about-dialog"));
133 hpaned = GTK_WIDGET (gtk_builder_get_object (builder,
134 "main-window-hpaned"));
135 vpaned = GTK_WIDGET (gtk_builder_get_object (builder,
136 "main-window-vpaned"));
137
138 g_assert (main_window != NULL);
139 g_assert (about_dialog != NULL);
140 g_assert (hpaned != NULL);
141 g_assert (vpaned != NULL);
142
143 /* 80% of the screen but don't get bigger than 1000x800 */
144 /* FIXME: Replace with proper functions */
145 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
146 w = gdk_screen_width () * 0.8;
147 h = gdk_screen_height () * 0.8;
148 G_GNUC_END_IGNORE_DEPRECATIONS
149
150 /* Keep 5/4 aspect */
151 if (w/h > 1.25) {
152 h = w / 1.25;
153 } else {
154 w = h * 1.25;
155 }
156
157 window_width = CLAMP ((int) w, 10, 1000);
158 window_height = CLAMP ((int) h, 10, 800);
159
160 gtk_window_set_default_size (GTK_WINDOW (main_window),
161 window_width,
162 window_height);
163
164 gtk_window_resize (GTK_WINDOW (main_window),
165 window_width,
166 window_height);
167
168
169 init_icons ();
170
171 icon_pixbuf = gdk_pixbuf_new_from_resource (ICON_FILE, NULL);
172 if (icon_pixbuf == NULL) {
173 return FALSE;
174 }
175
176 gtk_window_set_icon (GTK_WINDOW (main_window), icon_pixbuf);
177 gtk_window_set_icon (GTK_WINDOW (about_dialog), icon_pixbuf);
178 gtk_about_dialog_set_logo (GTK_ABOUT_DIALOG (about_dialog),
179 icon_pixbuf);
180 gtk_about_dialog_set_version (GTK_ABOUT_DIALOG (about_dialog),
181 VERSION);
182
183 g_object_unref (icon_pixbuf);
184
185 gtk_builder_connect_signals (builder, NULL);
186
187 setup_treeviews ();
188 init_action_dialog (builder);
189
190 gtk_widget_show_all (main_window);
191
192 /* Give device treeview 40% of the main window */
193 g_object_get (hpaned, "max-position", &position, NULL);
194 position = position * 40 / 100;
195 g_object_set (hpaned, "position", position, NULL);
196
197 /* Give details treeview 60% of 60% of the main window */
198 g_object_get (vpaned, "max-position", &position, NULL);
199 position = position * 60 / 100;
200 g_object_set (vpaned, "position", position, NULL);
201
202 return TRUE;
203 }
204
205 void
206 deinit_ui (void)
207 {
208 g_object_unref (builder);
209 gtk_widget_destroy (main_window);
210 deinit_action_dialog ();
211 deinit_icons ();
212 }
213