563be555b6430020945c92e24248e5d9ad7b6959
[ruby-edje.git] / src / rb_part.c
1 /*
2  * $Id: rb_part.c 51 2004-08-01 10:19:02Z tilman $
3  *
4  * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
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 <Edje.h>
24
25 #include "rb_edje_main.h"
26 #include "rb_edje.h"
27
28 static VALUE cPart;
29
30 static inline char *GET_NAME (VALUE o)
31 {
32         static ID id;
33         VALUE name;
34
35         if (!id)
36                 id = rb_intern ("@name");
37
38         name = rb_ivar_get (o, id);
39
40         return StringValuePtr (name);
41 }
42
43 static inline VALUE GET_EDJE (VALUE o)
44 {
45         static ID id;
46
47         if (!id)
48                 id = rb_intern ("@edje");
49
50         return rb_ivar_get (o, id);
51 }
52
53 VALUE TO_PART (VALUE edje, VALUE name)
54 {
55         VALUE self;
56
57         CHECK_CLASS (edje, cEdje);
58         Check_Type (name, T_STRING);
59
60         self = rb_obj_alloc (cPart);
61
62         rb_iv_set (self, "@edje", edje);
63         rb_iv_set (self, "@name", rb_str_dup (name));
64
65         rb_obj_call_init (self, 0, NULL);
66
67         return self;
68 }
69
70 static VALUE c_get_geometry (VALUE self)
71 {
72         int x = 0, y = 0, w = 0, h = 0;
73
74         GET_OBJ (GET_EDJE (self), RbEdje, e);
75
76         edje_object_part_geometry_get (e->real.real, GET_NAME (self),
77                                        (Evas_Coord *) &x,
78                                        (Evas_Coord *) &y,
79                                        (Evas_Coord *) &w,
80                                        (Evas_Coord *) &h);
81
82         return rb_ary_new3 (4, INT2FIX (x), INT2FIX (y),
83                             INT2FIX (y), INT2FIX (h));
84 }
85
86 static VALUE c_swallow (VALUE self, VALUE target)
87 {
88         GET_OBJ (GET_EDJE (self), RbEdje, e);
89
90         CHECK_CLASS (target, cEvasObject);
91         GET_OBJ (target, RbEvasObject, t);
92
93         edje_object_part_swallow (e->real.real, GET_NAME (self), t->real);
94
95         return Qnil;
96 }
97
98 static VALUE c_unswallow (VALUE self)
99 {
100         Evas_Object *o;
101
102         GET_OBJ (GET_EDJE (self), RbEdje, e);
103
104         o = edje_object_part_swallow_get (e->real.real, GET_NAME (self));
105         if (!o) {
106                 rb_raise (rb_eException, "Part didn't swallow an EvasObject");
107                 return Qnil;
108         }
109
110         edje_object_part_unswallow (e->real.real, o);
111
112         return Qnil;
113 }
114
115 static VALUE c_swallowed_object_get (VALUE self)
116 {
117         Evas_Object *o;
118         void *obj;
119
120         GET_OBJ (GET_EDJE (self), RbEdje, e);
121
122         o = edje_object_part_swallow_get (e->real.real, GET_NAME (self));
123         if (!o)
124                 return Qnil;
125
126         if (!(obj = evas_object_data_get (o, RUBY_EVAS_OBJECT_KEY))) {
127                 rb_raise (rb_eException, "EvasObject Ruby object key missing");
128                 return Qnil;
129         }
130
131         return (VALUE) obj;
132 }
133
134 static VALUE c_text_get (VALUE self)
135 {
136         const char *s;
137
138         GET_OBJ (GET_EDJE (self), RbEdje, e);
139
140         s = edje_object_part_text_get (e->real.real, GET_NAME (self));
141
142         return s ? rb_str_new2 (s) : Qnil;
143 }
144
145 static VALUE c_text_set (VALUE self, VALUE text)
146 {
147         GET_OBJ (GET_EDJE (self), RbEdje, e);
148
149         Check_Type (text, T_STRING);
150
151         edje_object_part_text_set (e->real.real, GET_NAME (self),
152                                    StringValuePtr (text));
153
154         return Qnil;
155 }
156
157 static VALUE c_get_drag_value (VALUE self)
158 {
159         double dx = 0, dy = 0;
160
161         GET_OBJ (GET_EDJE (self), RbEdje, e);
162
163         edje_object_part_drag_value_get (e->real.real, GET_NAME (self), &dx, &dy);
164
165         return rb_ary_new3 (2, rb_float_new (dx), rb_float_new (dy));
166 }
167
168 static VALUE c_set_drag_value (VALUE self, VALUE dx, VALUE dy)
169 {
170         GET_OBJ (GET_EDJE (self), RbEdje, e);
171
172         if (!FIXNUM_P (dx))
173                 Check_Type (dx, T_FLOAT);
174
175         if (!FIXNUM_P (dy))
176                 Check_Type (dy, T_FLOAT);
177
178         edje_object_part_drag_value_set (e->real.real, GET_NAME (self),
179                                          NUM2DBL (dx), NUM2DBL (dy));
180
181         return Qnil;
182 }
183
184 void Init_Part (void)
185 {
186         cPart = rb_define_class_under (mEdje, "Part", rb_cObject);
187
188         rb_define_attr (cPart, "edje", 1, 0);
189         rb_define_attr (cPart, "name", 1, 0);
190
191         /* not publically instantiable yet */
192         rb_define_private_method (rb_singleton_class (cPart),
193                                   "new", NULL, 0);
194         rb_define_method (cPart, "get_geometry", c_get_geometry, 0);
195         rb_define_method (cPart, "swallow", c_swallow, 1);
196         rb_define_method (cPart, "unswallow", c_unswallow, 1);
197         rb_define_method (cPart, "swallowed_object",
198                           c_swallowed_object_get, 0);
199         rb_define_method (cPart, "text", c_text_get, 0);
200         rb_define_method (cPart, "text=", c_text_set, 1);
201         rb_define_method (cPart, "get_drag_value", c_get_drag_value, 2);
202         rb_define_method (cPart, "set_drag_value", c_set_drag_value, 2);
203 }