Don't try to convert NULL into a String.
[ruby-ecore.git] / src / ecore_x / rb_window.c
1 /*
2  * $Id: rb_window.c 97 2004-08-23 18:30:28Z tilman $
3  *
4  * Copyright (C) 2004 ruby-ecore team (see AUTHORS)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <ruby.h>
22
23 #include <Ecore.h>
24 #include <Ecore_X.h>
25
26 #define __RB_WINDOW_C
27 #include "../ecore/rb_ecore.h"
28 #include "rb_ecore_x.h"
29 #include "rb_window.h"
30
31 VALUE cWindow;
32
33 static void c_mark (RbWindow *w)
34 {
35         if (!NIL_P (w->parent))
36                 rb_gc_mark (w->parent);
37 }
38
39 static void c_free (RbWindow *w)
40 {
41         if (w->real)
42                 ecore_x_window_del (w->real);
43
44         free (w);
45 }
46
47 VALUE TO_ECORE_X_WINDOW (VALUE parent, Ecore_X_Window w)
48 {
49         VALUE self;
50         RbWindow *window = NULL;
51
52         self = Data_Make_Struct (cWindow, RbWindow, c_mark, free, window);
53
54         window->real = w;
55         window->parent = parent;
56
57         rb_obj_call_init (self, 0, NULL);
58
59         return self;
60 }
61
62 static VALUE c_new (int argc, VALUE *argv, VALUE klass)
63 {
64         VALUE self, parent, geom[4];
65         RbWindow *window = NULL, *p = NULL;
66         int i, igeom[4] = {0, 0, 0, 0};
67         Ecore_X_Window pwin = 0;
68
69         self = Data_Make_Struct (cWindow, RbWindow, c_mark, c_free, window);
70
71         rb_scan_args (argc, argv, "05",
72                       &parent, &geom[0], &geom[1], &geom[2], &geom[3]);
73
74         if (!NIL_P (parent)) {
75                 CHECK_CLASS (parent, cWindow);
76                 Data_Get_Struct (parent, RbWindow, p);
77                 pwin = p->real;
78         }
79
80         for (i = 0; i < 4; i++)
81                 if (!NIL_P (geom[i])) {
82                         Check_Type (geom[i], T_FIXNUM);
83                         igeom[i] = FIX2INT (geom[i]);
84                 }
85
86         window->real = ecore_x_window_new (pwin, igeom[0], igeom[1],
87                                            igeom[2], igeom[3]);
88         window->parent = parent;
89
90         rb_obj_call_init (self, 0, NULL);
91
92         return self;
93 }
94
95 static VALUE c_inspect (VALUE self)
96 {
97         char buf[128];
98
99         GET_OBJ (self, RbWindow, win);
100
101         snprintf (buf, sizeof (buf),
102                   "#<%s:%p id=%u>", rb_obj_classname (self),
103                   (void *) self, win->real);
104
105         return rb_str_new2 (buf);
106 }
107
108 static VALUE c_equal_value (VALUE self, VALUE other)
109 {
110         GET_OBJ (self, RbWindow, w1);
111
112         CHECK_CLASS (other, cWindow);
113         GET_OBJ (other, RbWindow, w2);
114
115         return w1->real == w2->real ? Qtrue : Qfalse;
116 }
117
118 static VALUE c_show (VALUE self)
119 {
120         GET_OBJ (self, RbWindow, win);
121
122         ecore_x_window_show (win->real);
123
124         return Qnil;
125 }
126
127 static VALUE c_hide (VALUE self)
128 {
129         GET_OBJ (self, RbWindow, win);
130
131         ecore_x_window_hide (win->real);
132
133         return Qnil;
134 }
135
136 static VALUE c_visible_get (VALUE self)
137 {
138         GET_OBJ (self, RbWindow, win);
139
140         return ecore_x_window_visible_get (win->real) ? Qtrue : Qfalse;
141 }
142
143 static VALUE c_delete (VALUE self)
144 {
145         GET_OBJ (self, RbWindow, win);
146
147         ecore_x_window_del (win->real);
148
149         return Qnil;
150 }
151
152 static VALUE c_send_delete_request (VALUE self)
153 {
154         GET_OBJ (self, RbWindow, win);
155
156         ecore_x_window_delete_request_send (win->real);
157
158         return Qnil;
159 }
160
161 static VALUE c_raise (VALUE self)
162 {
163         GET_OBJ (self, RbWindow, win);
164
165         ecore_x_window_raise (win->real);
166
167         return Qnil;
168 }
169
170 static VALUE c_lower (VALUE self)
171 {
172         GET_OBJ (self, RbWindow, win);
173
174         ecore_x_window_lower (win->real);
175
176         return Qnil;
177 }
178
179 static VALUE c_reparent (VALUE self, VALUE other, VALUE x, VALUE y)
180 {
181         GET_OBJ (self, RbWindow, win);
182
183         CHECK_CLASS (other, cWindow);
184         GET_OBJ (other, RbWindow, o);
185
186         Check_Type (x, T_FIXNUM);
187         Check_Type (y, T_FIXNUM);
188
189         ecore_x_window_reparent (win->real, o->real,
190                                  FIX2INT (x), FIX2INT (y));
191
192         return Qnil;
193 }
194
195 static VALUE c_focus (VALUE self)
196 {
197         GET_OBJ (self, RbWindow, win);
198
199         ecore_x_window_focus (win->real);
200
201         return Qnil;
202 }
203
204 static VALUE c_move (VALUE self, VALUE x, VALUE y)
205 {
206         GET_OBJ (self, RbWindow, win);
207
208         Check_Type (x, T_FIXNUM);
209         Check_Type (y, T_FIXNUM);
210
211         ecore_x_window_move (win->real, FIX2INT (x), FIX2INT (y));
212
213         return Qnil;
214 }
215
216 static VALUE c_resize (VALUE self, VALUE w, VALUE h)
217 {
218         GET_OBJ (self, RbWindow, win);
219
220         Check_Type (w, T_FIXNUM);
221         Check_Type (h, T_FIXNUM);
222
223         ecore_x_window_move (win->real, FIX2INT (w), FIX2INT (h));
224
225         return Qnil;
226 }
227
228 static VALUE c_size_get (VALUE self)
229 {
230         int w = 0, h = 0;
231
232         GET_OBJ (self, RbWindow, win);
233
234         ecore_x_window_size_get (win->real, &w, &h);
235
236         return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
237 }
238
239 static VALUE c_geometry_get (VALUE self)
240 {
241         int x = 0, y = 0, w = 0, h = 0;
242
243         GET_OBJ (self, RbWindow, win);
244
245         ecore_x_window_geometry_get (win->real, &x, &y, &w, &h);
246
247         return rb_ary_new3 (4, INT2FIX (x), INT2FIX (y),
248                             INT2FIX (w), INT2FIX (h));
249 }
250
251 static VALUE c_border_width_get (VALUE self)
252 {
253         GET_OBJ (self, RbWindow, win);
254
255         return INT2FIX (ecore_x_window_border_width_get (win->real));
256 }
257
258 static VALUE c_depth_get (VALUE self)
259 {
260         GET_OBJ (self, RbWindow, win);
261
262         return INT2FIX (ecore_x_window_depth_get (win->real));
263 }
264
265 static VALUE c_parent_get (VALUE self)
266 {
267         GET_OBJ (self, RbWindow, win);
268
269         return TO_ECORE_X_WINDOW (Qnil,
270                                   ecore_x_window_parent_get (win->real));
271 }
272
273 static VALUE c_title_get (VALUE self)
274 {
275         char *s;
276
277         GET_OBJ (self, RbWindow, win);
278
279         s = ecore_x_window_prop_title_get (win->real);
280
281         return s ? rb_str_new2 (s) : Qnil;
282 }
283
284 static VALUE c_title_set (VALUE self, VALUE val)
285 {
286         GET_OBJ (self, RbWindow, win);
287
288         Check_Type (val, T_STRING);
289
290         ecore_x_window_prop_title_set (win->real, StringValuePtr (val));
291
292         return Qnil;
293 }
294
295 static VALUE c_set_event_mask (VALUE self, VALUE val)
296 {
297         GET_OBJ (self, RbWindow, win);
298
299         Check_Type (val, T_FIXNUM);
300
301         ecore_x_event_mask_set (win->real, FIX2INT (val));
302
303         return Qnil;
304 }
305
306 static VALUE c_unset_event_mask (VALUE self, VALUE val)
307 {
308         GET_OBJ (self, RbWindow, win);
309
310         Check_Type (val, T_FIXNUM);
311
312         ecore_x_event_mask_unset (win->real, FIX2INT (val));
313
314         return Qnil;
315 }
316
317 static VALUE c_set_protocol (VALUE self, VALUE proto, VALUE on)
318 {
319         GET_OBJ (self, RbWindow, win);
320
321         Check_Type (proto, T_FIXNUM);
322         CHECK_BOOL (on);
323
324         ecore_x_window_prop_protocol_set (win->real, FIX2INT (proto),
325                                           on == Qtrue);
326
327         return Qnil;
328 }
329
330 static VALUE c_get_protocol (VALUE self, VALUE proto)
331 {
332         int s;
333
334         GET_OBJ (self, RbWindow, win);
335
336         s = ecore_x_window_prop_protocol_isset (win->real, FIX2INT (proto));
337
338         return s ? Qtrue : Qfalse;
339 }
340
341 static VALUE c_sticky_get (VALUE self)
342 {
343         int s;
344
345         GET_OBJ (self, RbWindow, win);
346
347         s = ecore_x_window_prop_state_isset (win->real,
348                                              ECORE_X_WINDOW_STATE_STICKY);
349
350         return s ? Qtrue : Qfalse;
351 }
352
353 static VALUE c_sticky_set (VALUE self, VALUE val)
354 {
355         GET_OBJ (self, RbWindow, win);
356
357         CHECK_BOOL (val);
358
359         ecore_x_window_prop_sticky_set (win->real, val == Qtrue);
360
361         return Qnil;
362 }
363
364 static VALUE c_manage (VALUE self)
365 {
366         GET_OBJ (self, RbWindow, win);
367
368         ecore_x_window_manage (win->real);
369
370         return Qnil;
371 }
372
373 static VALUE c_manage_container (VALUE self)
374 {
375         GET_OBJ (self, RbWindow, win);
376
377         ecore_x_window_container_manage (win->real);
378
379         return Qnil;
380 }
381
382 static VALUE c_manage_client (VALUE self)
383 {
384         GET_OBJ (self, RbWindow, win);
385
386         ecore_x_window_client_manage (win->real);
387
388         return Qnil;
389 }
390
391 static VALUE c_sniff (VALUE self)
392 {
393         GET_OBJ (self, RbWindow, win);
394
395         ecore_x_window_sniff (win->real);
396
397         return Qnil;
398 }
399
400 static VALUE c_sniff_client (VALUE self)
401 {
402         GET_OBJ (self, RbWindow, win);
403
404         ecore_x_window_client_sniff (win->real);
405
406         return Qnil;
407 }
408
409 void Init_Window (void)
410 {
411         cWindow = rb_define_class_under (mX, "Window", rb_cObject);
412
413         rb_define_singleton_method (cWindow, "new", c_new, -1);
414         rb_define_method (cWindow, "inspect", c_inspect, 0);
415         rb_define_method (cWindow, "==", c_equal_value, 1);
416         rb_define_method (cWindow, "show", c_show, 0);
417         rb_define_method (cWindow, "hide", c_hide, 0);
418         rb_define_method (cWindow, "visible?", c_visible_get, 0);
419         rb_define_method (cWindow, "delete", c_delete, 0);
420         rb_define_method (cWindow, "send_delete_request",
421                           c_send_delete_request, 0);
422         rb_define_method (cWindow, "raise", c_raise, 0);
423         rb_define_method (cWindow, "lower", c_lower, 0);
424         rb_define_method (cWindow, "reparent", c_reparent, 3);
425         rb_define_method (cWindow, "focus", c_focus, 0);
426         rb_define_method (cWindow, "move", c_move, 2);
427         rb_define_method (cWindow, "resize", c_resize, 2);
428         rb_define_method (cWindow, "size", c_size_get, 0);
429         rb_define_method (cWindow, "geometry", c_geometry_get, 0);
430         rb_define_method (cWindow, "border_width", c_border_width_get, 0);
431         rb_define_method (cWindow, "depth", c_depth_get, 0);
432         rb_define_method (cWindow, "parent", c_parent_get, 0);
433         rb_define_method (cWindow, "title", c_title_get, 0);
434         rb_define_method (cWindow, "title=", c_title_set, 1);
435         rb_define_method (cWindow, "set_event_mask", c_set_event_mask, 1);
436         rb_define_method (cWindow, "unset_event_mask",
437                           c_unset_event_mask, 1);
438         rb_define_method (cWindow, "set_protocol", c_set_protocol, 2);
439         rb_define_method (cWindow, "get_protocol", c_get_protocol, 1);
440         rb_define_method (cWindow, "sticky?", c_sticky_get, 0);
441         rb_define_method (cWindow, "sticky=", c_sticky_set, 1);
442         rb_define_method (cWindow, "manage", c_manage, 0);
443         rb_define_method (cWindow, "manage_container", c_manage_container, 0);
444         rb_define_method (cWindow, "manage_client", c_manage_client, 0);
445         rb_define_method (cWindow, "sniff", c_sniff, 0);
446         rb_define_method (cWindow, "sniff_client", c_sniff_client, 0);
447 }