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