API tweaks.
[ruby-ecore.git] / src / ecore_x / rb_window.c
1 /*
2  * $Id: rb_window.c 78 2004-08-19 21:02:11Z 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         if (!w)
53                 return Qnil;
54
55         self = Data_Make_Struct (cWindow, RbWindow, c_mark, free, window);
56
57         window->real = w;
58         window->parent = parent;
59
60         rb_obj_call_init (self, 0, NULL);
61
62         return self;
63 }
64
65 static VALUE c_new (int argc, VALUE *argv, VALUE klass)
66 {
67         VALUE self, parent, geom[4];
68         RbWindow *window = NULL, *p = NULL;
69         int i, igeom[4] = {0, 0, 0, 0};
70         Ecore_X_Window pwin = 0;
71
72         self = Data_Make_Struct (cWindow, RbWindow, c_mark, c_free, window);
73
74         rb_scan_args (argc, argv, "05",
75                       &parent, &geom[0], &geom[1], &geom[2], &geom[3]);
76
77         if (!NIL_P (parent)) {
78                 CHECK_CLASS (parent, cWindow);
79                 Data_Get_Struct (parent, RbWindow, p);
80                 pwin = p->real;
81         }
82
83         for (i = 0; i < 4; i++)
84                 if (!NIL_P (geom[i])) {
85                         Check_Type (geom[i], T_FIXNUM);
86                         igeom[i] = FIX2INT (geom[i]);
87                 }
88
89         window->real = ecore_x_window_new (pwin, igeom[0], igeom[1],
90                                            igeom[2], igeom[3]);
91         window->parent = parent;
92
93         rb_obj_call_init (self, 0, NULL);
94
95         return self;
96 }
97
98 static VALUE c_equal_value (VALUE self, VALUE other)
99 {
100         GET_OBJ (self, RbWindow, w1);
101
102         CHECK_CLASS (other, cWindow);
103         GET_OBJ (other, RbWindow, w2);
104
105         return w1->real == w2->real ? Qtrue : Qfalse;
106 }
107
108 static VALUE c_show (VALUE self)
109 {
110         GET_OBJ (self, RbWindow, win);
111
112         ecore_x_window_show (win->real);
113
114         return Qnil;
115 }
116
117 static VALUE c_hide (VALUE self)
118 {
119         GET_OBJ (self, RbWindow, win);
120
121         ecore_x_window_hide (win->real);
122
123         return Qnil;
124 }
125
126 static VALUE c_delete (VALUE self)
127 {
128         GET_OBJ (self, RbWindow, win);
129
130         if (win->real) {
131                 ecore_x_window_del (win->real);
132                 win->real = 0;
133         }
134
135         return Qnil;
136 }
137
138 static VALUE c_raise (VALUE self)
139 {
140         GET_OBJ (self, RbWindow, win);
141
142         ecore_x_window_raise (win->real);
143
144         return Qnil;
145 }
146
147 static VALUE c_lower (VALUE self)
148 {
149         GET_OBJ (self, RbWindow, win);
150
151         ecore_x_window_lower (win->real);
152
153         return Qnil;
154 }
155
156 static VALUE c_move (VALUE self, VALUE x, VALUE y)
157 {
158         GET_OBJ (self, RbWindow, win);
159
160         Check_Type (x, T_FIXNUM);
161         Check_Type (y, T_FIXNUM);
162
163         ecore_x_window_move (win->real, FIX2INT (x), FIX2INT (y));
164
165         return Qnil;
166 }
167
168 static VALUE c_resize (VALUE self, VALUE w, VALUE h)
169 {
170         GET_OBJ (self, RbWindow, win);
171
172         Check_Type (w, T_FIXNUM);
173         Check_Type (h, T_FIXNUM);
174
175         ecore_x_window_move (win->real, FIX2INT (w), FIX2INT (h));
176
177         return Qnil;
178 }
179
180 static VALUE c_size_get (VALUE self)
181 {
182         int w = 0, h = 0;
183
184         GET_OBJ (self, RbWindow, win);
185
186         ecore_x_window_size_get (win->real, &w, &h);
187
188         return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
189 }
190
191 static VALUE c_geometry_get (VALUE self)
192 {
193         int x = 0, y = 0, w = 0, h = 0;
194
195         GET_OBJ (self, RbWindow, win);
196
197         ecore_x_window_geometry_get (win->real, &x, &y, &w, &h);
198
199         return rb_ary_new3 (4, INT2FIX (x), INT2FIX (y),
200                             INT2FIX (w), INT2FIX (h));
201 }
202
203 static VALUE c_set_event_mask (VALUE self, VALUE val)
204 {
205         GET_OBJ (self, RbWindow, win);
206
207         Check_Type (val, T_FIXNUM);
208
209         ecore_x_event_mask_set (win->real, FIX2INT (val));
210
211         return Qnil;
212 }
213
214 static VALUE c_unset_event_mask (VALUE self, VALUE val)
215 {
216         GET_OBJ (self, RbWindow, win);
217
218         Check_Type (val, T_FIXNUM);
219
220         ecore_x_event_mask_unset (win->real, FIX2INT (val));
221
222         return Qnil;
223 }
224
225 static VALUE c_set_protocol (VALUE self, VALUE proto, VALUE on)
226 {
227         GET_OBJ (self, RbWindow, win);
228
229         Check_Type (proto, T_FIXNUM);
230         CHECK_BOOL (on);
231
232         ecore_x_window_prop_protocol_set (win->real, FIX2INT (proto),
233                                           on == Qtrue);
234
235         return Qnil;
236 }
237
238 void Init_Window (void)
239 {
240         cWindow = rb_define_class_under (mX, "Window", rb_cObject);
241
242         rb_define_singleton_method (cWindow, "new", c_new, -1);
243         rb_define_method (cWindow, "==", c_equal_value, 1);
244         rb_define_method (cWindow, "show", c_show, 0);
245         rb_define_method (cWindow, "hide", c_hide, 0);
246         rb_define_method (cWindow, "delete", c_delete, 0);
247         rb_define_method (cWindow, "raise", c_raise, 0);
248         rb_define_method (cWindow, "lower", c_lower, 0);
249         rb_define_method (cWindow, "move", c_move, 2);
250         rb_define_method (cWindow, "resize", c_resize, 2);
251         rb_define_method (cWindow, "size", c_size_get, 0);
252         rb_define_method (cWindow, "geometry", c_geometry_get, 0);
253         rb_define_method (cWindow, "set_event_mask", c_set_event_mask, 1);
254         rb_define_method (cWindow, "unset_event_mask",
255                           c_unset_event_mask, 1);
256         rb_define_method (cWindow, "set_protocol", c_set_protocol, 2);
257 }