1 /*
2  * GNT - The GLib Ncurses Toolkit
3  *
4  * GNT is the legal property of its developers, whose names are too numerous
5  * to list here.  Please refer to the COPYRIGHT file distributed with this
6  * source distribution.
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
21  */
22 
23 #include "gntinternal.h"
24 #include "gntstyle.h"
25 #include "gntwindow.h"
26 
27 #include <string.h>
28 
29 struct _GntWindowPriv
30 {
31 	GHashTable *accels;   /* key => menuitem-id */
32 	GntWindowFlags flags;
33 };
34 
35 enum
36 {
37 	SIG_WORKSPACE_HIDE,
38 	SIG_WORKSPACE_SHOW,
39 	SIGS,
40 };
41 
42 static guint signals[SIGS] = { 0 };
43 
44 static GntBoxClass *parent_class = NULL;
45 
46 static void (*org_destroy)(GntWidget *widget);
47 
48 static gboolean
show_menu(GntBindable * bind,GList * null)49 show_menu(GntBindable *bind, GList *null)
50 {
51 	GntWindow *win = GNT_WINDOW(bind);
52 	if (win->menu) {
53 		GntMenu *menu = win->menu;
54 
55 		gnt_screen_menu_show(menu);
56 		if (menu->type == GNT_MENU_TOPLEVEL) {
57 			GntMenuItem *item;
58 			item = g_list_nth_data(menu->list, menu->selected);
59 			if (item && gnt_menuitem_get_submenu(item)) {
60 				gnt_widget_activate(GNT_WIDGET(menu));
61 			}
62 		}
63 		return TRUE;
64 	}
65 	return FALSE;
66 }
67 
68 static void
gnt_window_destroy(GntWidget * widget)69 gnt_window_destroy(GntWidget *widget)
70 {
71 	GntWindow *window = GNT_WINDOW(widget);
72 	if (window->menu)
73 		gnt_widget_destroy(GNT_WIDGET(window->menu));
74 	if (window->priv) {
75 		if (window->priv->accels)
76 			g_hash_table_destroy(window->priv->accels);
77 		g_free(window->priv);
78 	}
79 	org_destroy(widget);
80 }
81 
82 static void
gnt_window_class_init(GntWindowClass * klass)83 gnt_window_class_init(GntWindowClass *klass)
84 {
85 	GntBindableClass *bindable = GNT_BINDABLE_CLASS(klass);
86 	GntWidgetClass *wid_class = GNT_WIDGET_CLASS(klass);
87 	parent_class = GNT_BOX_CLASS(klass);
88 
89 	org_destroy = wid_class->destroy;
90 	wid_class->destroy = gnt_window_destroy;
91 
92 	signals[SIG_WORKSPACE_HIDE] =
93 		g_signal_new("workspace-hidden",
94 					 G_TYPE_FROM_CLASS(klass),
95 					 G_SIGNAL_RUN_LAST,
96 					 0,
97 					 NULL, NULL,
98 					 g_cclosure_marshal_VOID__VOID,
99 					 G_TYPE_NONE, 0);
100 
101 	signals[SIG_WORKSPACE_SHOW] =
102 		g_signal_new("workspace-shown",
103 					 G_TYPE_FROM_CLASS(klass),
104 					 G_SIGNAL_RUN_LAST,
105 					 0,
106 					 NULL, NULL,
107 					 g_cclosure_marshal_VOID__VOID,
108 					 G_TYPE_NONE, 0);
109 
110 	gnt_bindable_class_register_action(bindable, "show-menu", show_menu,
111 				GNT_KEY_CTRL_O, NULL);
112 	gnt_bindable_register_binding(bindable, "show-menu", GNT_KEY_F10, NULL);
113 	gnt_style_read_actions(G_OBJECT_CLASS_TYPE(klass), bindable);
114 
115 	GNTDEBUG;
116 }
117 
118 static void
gnt_window_init(GTypeInstance * instance,gpointer class)119 gnt_window_init(GTypeInstance *instance, gpointer class)
120 {
121 	GntWidget *widget = GNT_WIDGET(instance);
122 	GntWindow *win = GNT_WINDOW(widget);
123 
124 	gnt_widget_set_has_border(widget, TRUE);
125 	gnt_widget_set_has_shadow(widget, TRUE);
126 	gnt_widget_set_take_focus(widget, TRUE);
127 
128 	win->priv = g_new0(GntWindowPriv, 1);
129 	win->priv->accels = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
130 	GNTDEBUG;
131 }
132 
133 /******************************************************************************
134  * GntWindow API
135  *****************************************************************************/
136 GType
gnt_window_get_gtype(void)137 gnt_window_get_gtype(void)
138 {
139 	static GType type = 0;
140 
141 	if(type == 0)
142 	{
143 		static const GTypeInfo info = {
144 			sizeof(GntWindowClass),
145 			NULL,					/* base_init		*/
146 			NULL,					/* base_finalize	*/
147 			(GClassInitFunc)gnt_window_class_init,
148 			NULL,					/* class_finalize	*/
149 			NULL,					/* class_data		*/
150 			sizeof(GntWindow),
151 			0,						/* n_preallocs		*/
152 			gnt_window_init,			/* instance_init	*/
153 			NULL					/* value_table		*/
154 		};
155 
156 		type = g_type_register_static(GNT_TYPE_BOX,
157 									  "GntWindow",
158 									  &info, 0);
159 	}
160 
161 	return type;
162 }
163 
gnt_window_new()164 GntWidget *gnt_window_new()
165 {
166 	GntWidget *widget = g_object_new(GNT_TYPE_WINDOW, NULL);
167 
168 	return widget;
169 }
170 
gnt_window_box_new(gboolean homo,gboolean vert)171 GntWidget *gnt_window_box_new(gboolean homo, gboolean vert)
172 {
173 	GntWidget *wid = gnt_window_new();
174 	GntBox *box = GNT_BOX(wid);
175 
176 	box->homogeneous = homo;
177 	box->vertical = vert;
178 	box->alignment = vert ? GNT_ALIGN_LEFT : GNT_ALIGN_MID;
179 
180 	return wid;
181 }
182 
183 void
gnt_window_workspace_hiding(GntWindow * window)184 gnt_window_workspace_hiding(GntWindow *window)
185 {
186 	if (window->menu)
187 		gnt_widget_hide(GNT_WIDGET(window->menu));
188 	g_signal_emit(window, signals[SIG_WORKSPACE_HIDE], 0);
189 }
190 
191 void
gnt_window_workspace_showing(GntWindow * window)192 gnt_window_workspace_showing(GntWindow *window)
193 {
194 	g_signal_emit(window, signals[SIG_WORKSPACE_SHOW], 0);
195 }
196 
gnt_window_set_menu(GntWindow * window,GntMenu * menu)197 void gnt_window_set_menu(GntWindow *window, GntMenu *menu)
198 {
199 	/* If a menu already existed, then destroy that first. */
200 	const char *name = gnt_widget_get_name(GNT_WIDGET(window));
201 	if (window->menu)
202 		gnt_widget_destroy(GNT_WIDGET(window->menu));
203 	window->menu = menu;
204 	if (name && window->priv) {
205 		if (!gnt_style_read_menu_accels(name, window->priv->accels)) {
206 			g_hash_table_destroy(window->priv->accels);
207 			window->priv->accels = NULL;
208 		}
209 	}
210 }
211 
212 GntMenu *
gnt_window_get_menu(GntWindow * window)213 gnt_window_get_menu(GntWindow *window)
214 {
215 	g_return_val_if_fail(GNT_IS_WINDOW(window), NULL);
216 
217 	return window->menu;
218 }
219 
gnt_window_get_accel_item(GntWindow * window,const char * key)220 const char * gnt_window_get_accel_item(GntWindow *window, const char *key)
221 {
222 	if (window->priv->accels)
223 		return g_hash_table_lookup(window->priv->accels, key);
224 	return NULL;
225 }
226 
gnt_window_set_maximize(GntWindow * window,GntWindowFlags maximize)227 void gnt_window_set_maximize(GntWindow *window, GntWindowFlags maximize)
228 {
229 	if (maximize & GNT_WINDOW_MAXIMIZE_X)
230 		window->priv->flags |= GNT_WINDOW_MAXIMIZE_X;
231 	else
232 		window->priv->flags &= ~GNT_WINDOW_MAXIMIZE_X;
233 
234 	if (maximize & GNT_WINDOW_MAXIMIZE_Y)
235 		window->priv->flags |= GNT_WINDOW_MAXIMIZE_Y;
236 	else
237 		window->priv->flags &= ~GNT_WINDOW_MAXIMIZE_Y;
238 }
239 
gnt_window_get_maximize(GntWindow * window)240 GntWindowFlags gnt_window_get_maximize(GntWindow *window)
241 {
242 	return (window->priv->flags & (GNT_WINDOW_MAXIMIZE_X | GNT_WINDOW_MAXIMIZE_Y));
243 }
244 
245