File: window-renderer.c

package info (click to toggle)
entity 1.0.1-8
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 5,604 kB
  • ctags: 5,394
  • sloc: ansic: 64,242; sh: 7,377; makefile: 776; perl: 319
file content (408 lines) | stat: -rw-r--r-- 10,764 bytes parent folder | download | duplicates (2)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include <gtk/gtk.h>
#include "entity.h"
#include "gtk-common.h"
#include "gtk-widget-attr.h"


static gint
rendgtk_window_realize_position (gpointer user_data)
{
    ENode *node = user_data;

    gint xipos;
    gint yipos;
    EBuf *val;

    GtkWidget *window;

    window = enode_get_kv (node, "top-widget");

    val = enode_attrib (node, "y-position", NULL);
    if (ebuf_not_empty (val))
	yipos = erend_get_integer (val);
    else
	yipos = -1;

    val = enode_attrib (node, "x-position", NULL);
    if (ebuf_not_empty (val))
	xipos = erend_get_integer (val);
    else
	xipos = -1;

#ifndef WIN32
    gtk_window_reposition (GTK_WINDOW (window), xipos, yipos);
#endif				/* WIN32 */
    return (FALSE);
}

static gint
rendgtk_window_realize_icon (gpointer user_data)
{
    ENode *node = user_data;

    GtkWidget *window;
    GdkPixmap *icon;

    gchar *filename;
    gchar *fpath;

    window = enode_get_kv (node, "top-widget");
    if (!window)
	return (FALSE);

    if (!window->window)
	gtk_widget_realize (window);

    filename = enode_attrib_str (node, "icon", NULL);
    fpath = eutils_file_search (node, filename);

    icon = gdk_pixmap_create_from_xpm (window->window, NULL, NULL, fpath);
    gdk_window_set_icon (window->window, NULL, icon, NULL);
    enode_set_kv (node, "icon-pixmap", icon);
    return (FALSE);
}

static gint
rendgtk_window_ondelete_callback (GtkWidget * widget,
				  GdkEvent * event, gpointer user_data)
{
    ENode *node = user_data;
    gchar *function = NULL;

    function = enode_attrib_str (node, "ondelete", NULL);

    enode_call_ignore_return (node, function, "");

    return (TRUE);
}

static void
rendgtk_window_resize_callback (GtkWidget * widget,
				GdkEventConfigure * event, gpointer user_data)
{
    ENode *node = user_data;
    gchar value[100];
    EBuf *val;

    /* to remember window width and height.  May also want to check an *
     * attribute in case they don't want this to be saved */

    /* We only save the new value, if the old value was set. */
    val = enode_attrib (node, "width", NULL);
    if (ebuf_not_empty (val)) {
	g_snprintf (value, 100, "%d", event->width);
	enode_attrib_quiet (node, "width", ebuf_new_with_str (value));
    }

    val = enode_attrib (node, "height", NULL);
    if (ebuf_not_empty (val)) {
	g_snprintf (value, 100, "%d", event->height);
	enode_attrib_quiet (node, "height", ebuf_new_with_str (value));
    }
}

static gint
rendgtk_window_visible_attr (ENode * node, EBuf * attr, EBuf * value)
{
    rendgtk_widget_idle_visible (node);
    return (TRUE);
}


static gint
rendgtk_window_key_repeat_attr (ENode * node, EBuf * attr, EBuf * value)
{
    GtkWidget *window;

    window = enode_get_kv (node, "top-widget");
    if (!window)
	return TRUE;

    if (enode_attrib_is_true (node, "key-auto-repeat"))
	gdk_key_repeat_restore ();
    else
	gdk_key_repeat_disable ();

    return (TRUE);
}


static gint
rendgtk_window_set_title_attr (ENode * node, EBuf * attr, EBuf * value)
{
    GtkWidget *window;

    window = enode_get_kv (node, "top-widget");
    if (!window)
	return TRUE;

    gtk_window_set_title (GTK_WINDOW (window), value->str);

    /* 2nd arg is wmclass_name, 3rd arg is wmclass_class */
    /* May want to move this into a seperate attr */
    if (!GTK_WIDGET_REALIZED (window))
	gtk_window_set_wmclass (GTK_WINDOW (window), value->str, value->str);

    return (TRUE);
}

static gint
rendgtk_window_set_xyposition_attr (ENode * node, EBuf * attr, EBuf * value)
{
    GtkWidget *window;

    window = enode_get_kv (node, "top-widget");
    if (!window)
	return (TRUE);

    gtk_idle_add (rendgtk_window_realize_position, node);

    return (TRUE);
}

static gint
rendgtk_window_set_busy_attr (ENode *node, EBuf *attr, EBuf *value)
{
    GtkWidget *window;
    GdkCursor *cursor;

    window = enode_get_kv (node, "top-widget");
    if (!window)
	return TRUE;

    EDEBUG (("window-renderer", "setting busy flag on window to %s", value->str));

	
    if (window->window) {
	if (erend_value_is_true (value)) {
	    /* Set to watch */
	    cursor = gdk_cursor_new (150);
	    gdk_window_set_cursor (window->window, cursor);
	    gdk_cursor_destroy (cursor);
	} else {
	    /* Set to old value */
	    cursor = gdk_cursor_new (68);
	    gdk_window_set_cursor (window->window, cursor);
	    gdk_cursor_destroy (cursor);
	}
    
	while (gtk_events_pending ())
	    gtk_main_iteration ();
    
    }

    return (TRUE);
}


static gint
rendgtk_window_set_position_attr (ENode * node, EBuf * attr, EBuf * value)
{
    GtkWidget *window;
    gint intval;

    window = enode_get_kv (node, "top-widget");
    if (!window)
	return TRUE;

    intval = GTK_WIN_POS_NONE;

    if (ebuf_equal_str (value, "center"))
	intval = GTK_WIN_POS_CENTER;

    if (ebuf_equal_str (value, "mouse"))
	intval = GTK_WIN_POS_MOUSE;

    gtk_window_set_position (GTK_WINDOW (window), intval);

    return (TRUE);
}

static gint
rendgtk_window_set_icon (ENode * node, EBuf * attr, EBuf * value)
{
    GtkWidget *window;

    window = enode_get_kv (node, "top-widget");
    if (!window)
	return (TRUE);

    gtk_idle_add (rendgtk_window_realize_icon, node);

    return (TRUE);
}

static void
rendgtk_window_pack (ENode * parent_node, ENode * child_node)
{
    GtkWidget *window;

    /* Only show window once something gets packed into it, should make it
     * pop up better */
    window = enode_get_kv (parent_node, "top-widget");

    rendgtk_box_pack (parent_node, child_node);
}

static void
rendgtk_window_destroy (ENode * node)
{
    GdkPixmap *icon;

    icon = enode_get_kv (node, "icon-pixmap");
    if (icon)
	gdk_pixmap_unref (icon);

    gdk_key_repeat_restore ();

    rendgtk_element_destroy (node);
}

static void
rendgtk_window_render (ENode * node)
{
    GtkWidget *window;
    GtkWidget *vbox;

    GtkWindowType win_type;
    EBuf *win_type_buf;

    win_type = GTK_WINDOW_TOPLEVEL;
    win_type_buf = enode_attrib (node, "frame-style", NULL);
    if (win_type_buf) {
      if (ebuf_equal_strcase (win_type_buf, "normal")) 
	  win_type = GTK_WINDOW_TOPLEVEL;

      if (ebuf_equal_strcase (win_type_buf, "dialog"))
	win_type = GTK_WINDOW_DIALOG;

      if (ebuf_equal_strcase (win_type_buf, "popup"))
	win_type = GTK_WINDOW_POPUP;
    }

    window = gtk_window_new (win_type);


    vbox = gtk_vbox_new (FALSE, 0);
    gtk_container_add (GTK_CONTAINER (window), vbox);

    enode_set_kv (node, "top-widget", window);
    enode_set_kv (node, "bottom-widget", vbox);

    gtk_signal_connect (GTK_OBJECT (window), "delete_event",
			GTK_SIGNAL_FUNC (rendgtk_window_ondelete_callback),
			(gpointer) node);

    gtk_signal_connect (GTK_OBJECT (window), "configure_event",
			GTK_SIGNAL_FUNC (rendgtk_window_resize_callback),
			(gpointer) node);

    gtk_window_set_policy (GTK_WINDOW (window), TRUE, TRUE, FALSE);

    enode_attribs_sync (node);

    gtk_widget_show (vbox);

    /* Delay the display of the window until it's children have had a chance
     * to render. */
    rendgtk_widget_idle_visible (node);
}


void
window_renderer_register (void)
{
    Element *element;
    ElementAttr *e_attr;

    element = g_new0 (Element, 1);
    element->render_func = rendgtk_window_render;
    element->destroy_func = rendgtk_window_destroy;
    element->parent_func = rendgtk_window_pack;
    element->tag = "window";
    element->description = "Create a new top-level window.";
    element_register (element);

    e_attr = g_new0 (ElementAttr, 1);
    e_attr->attribute = "ondelete";
    e_attr->description = "Specify function to call when window is 'closed'.";
    e_attr->value_desc = "function";
    e_attr->possible_values = "(window_node)";
    e_attr->set_attr_func = NULL;
    element_register_attrib (element, e_attr);

    e_attr = g_new0 (ElementAttr, 1);
    e_attr->attribute = "title";
    e_attr->description = "Set title and wmclass of window.";
    e_attr->value_desc = "string";
    e_attr->set_attr_func = rendgtk_window_set_title_attr;
    element_register_attrib (element, e_attr);

    e_attr = g_new0 (ElementAttr, 1);
    e_attr->attribute = "position";
    e_attr->description = "Set position of window.";
    e_attr->value_desc = "choice";
    e_attr->possible_values = "any,center,mouse";
    e_attr->set_attr_func = rendgtk_window_set_position_attr;
    element_register_attrib (element, e_attr);

    e_attr = g_new0 (ElementAttr, 1);
    e_attr->attribute = "x-position";
    e_attr->description = "Set absolute x position of window.";
    e_attr->value_desc = "integer";
    e_attr->possible_values = "-1,*";
    e_attr->set_attr_func = rendgtk_window_set_xyposition_attr;
    element_register_attrib (element, e_attr);

    e_attr = g_new0 (ElementAttr, 1);
    e_attr->attribute = "busy";
    e_attr->description = "Set to true and cursor will change to watch in this window";
    e_attr->value_desc = "boolean";
    e_attr->possible_values = "false,true";
    e_attr->set_attr_func = rendgtk_window_set_busy_attr;
    element_register_attrib (element, e_attr);
    
    e_attr = g_new0 (ElementAttr, 1);
    e_attr->attribute = "y-position";
    e_attr->description = "Set absolute y position of window.";
    e_attr->value_desc = "integer";
    e_attr->possible_values = "-1,*";
    e_attr->set_attr_func = rendgtk_window_set_xyposition_attr;
    element_register_attrib (element, e_attr);

    e_attr = g_new0 (ElementAttr, 1);
    e_attr->attribute = "icon";
    e_attr->description = "The xpm file to use as the icon";
    e_attr->value_desc = "string";
    e_attr->set_attr_func = rendgtk_window_set_icon;
    element_register_attrib (element, e_attr);

    e_attr = g_new0 (ElementAttr, 1);
    e_attr->attribute = "frame-style";
    e_attr->description =
	"Tell the window manager how to decorate this window";
    e_attr->value_desc = "normal,dialog,popup";
    element_register_attrib (element, e_attr);

    e_attr = g_new0 (ElementAttr, 1);
    e_attr->attribute = "key-auto-repeat";
    e_attr->description = "Enable or disable auto repeat on the keyboard.";
    e_attr->value_desc = "boolean";
    e_attr->set_attr_func = rendgtk_window_key_repeat_attr;
    element_register_attrib (element, e_attr);


    rendgtk_widget_attr_register (element, GTK_TYPE_WINDOW);

    /* Override the 'visible' attribute because windows in gtk are screwed and
     * will not show if you do an immediate hide/show pair */
    e_attr = g_new0 (ElementAttr, 1);
    e_attr->attribute = "visible";
    e_attr->description = "Toggle if the widget is to be visible on the screen.";
    e_attr->value_desc = "boolean";
    e_attr->possible_values = "true,false";
    e_attr->set_attr_func = rendgtk_window_visible_attr;
    element_register_attrib (element, e_attr);
    
    rendgtk_containerbox_attr_register (element);
}