Added wrappers for some functions.
[ruby-edje.git] / src / rb_edje.c
1 /*
2  * $Id: rb_edje.c 17 2004-06-22 19:37:54Z 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 #include <rb_evas.h>
25 #include <rb_evas_object.h>
26
27 #include "rb_edje_main.h"
28
29 #define GET_OBJ(obj, type, o, desc) \
30         type **(o) = NULL; \
31 \
32         Data_Get_Struct ((obj), type *, (o)); \
33 \
34         if (!*(o)) { \
35                 rb_raise (rb_eException, desc " destroyed already"); \
36                 return Qnil; \
37         }
38
39 VALUE cEdje;
40
41 static VALUE c_new (VALUE klass, VALUE evas)
42 {
43         VALUE self, argv[1];
44         Evas_Object **edje;
45
46         GET_OBJ (evas, Evas, e, "Evas");
47
48         self = Data_Make_Struct (klass, Evas_Object *, c_evas_object_mark,
49                                  c_evas_object_free, edje);
50         *edje = edje_object_add (*e);
51
52         argv[0] = evas;
53         rb_obj_call_init (self, 1, argv);
54
55         return self;
56 }
57
58 static VALUE c_load (VALUE self, VALUE eet, VALUE group)
59 {
60         GET_OBJ (self, Evas_Object, e, "Edje");
61
62         Check_Type (eet, T_STRING);
63         Check_Type (group, T_STRING);
64
65         if (!edje_object_file_set (*e, StringValuePtr (eet),
66                                    StringValuePtr (group)))
67                 rb_raise (rb_eException, "Cannot load eet");
68
69         return Qnil;
70 }
71
72 static VALUE c_get_size_min (VALUE self)
73 {
74         int w = 0, h = 0;
75
76         GET_OBJ (self, Evas_Object, e, "Edje");
77
78         edje_object_size_min_get (*e, &w, &h);
79
80         return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
81 }
82
83 static VALUE c_get_size_max (VALUE self)
84 {
85         int w = 0, h = 0;
86
87         GET_OBJ (self, Evas_Object, e, "Edje");
88
89         edje_object_size_max_get (*e, &w, &h);
90
91         return rb_ary_new3 (2, INT2FIX (w), INT2FIX (h));
92 }
93
94 static VALUE c_part_exists_get (VALUE self, VALUE part)
95 {
96         int r;
97
98         GET_OBJ (self, Evas_Object, e, "Edje");
99
100         Check_Type (part, T_STRING);
101
102         r = edje_object_part_exists (*e, StringValuePtr (part));
103
104         return r ? Qtrue : Qfalse;
105 }
106
107 static VALUE c_part_swallow (VALUE self, VALUE part, VALUE target)
108 {
109         GET_OBJ (self, Evas_Object, e, "Edje");
110
111         Check_Type (part, T_STRING);
112
113         if (!rb_obj_is_kind_of (target, cEvasObject)) {
114                 rb_raise (rb_eTypeError,
115                           "wrong argument type %s (expected EvasObject)",
116                           rb_obj_classname (target));
117                 return Qnil;
118         }
119
120         GET_OBJ (target, Evas_Object, target2, "EvasObject");
121
122         edje_object_part_swallow (*e, StringValuePtr (part), *target2);
123
124         return Qnil;
125 }
126
127 static VALUE c_part_unswallow (VALUE self, VALUE target)
128 {
129         GET_OBJ (self, Evas_Object, e, "Edje");
130
131         if (!rb_obj_is_kind_of (target, cEvasObject)) {
132                 rb_raise (rb_eTypeError,
133                           "wrong argument type %s (expected EvasObject)",
134                           rb_obj_classname (target));
135                 return Qnil;
136         }
137
138         GET_OBJ (target, Evas_Object, target2, "EvasObject");
139
140         edje_object_part_unswallow (*e, *target2);
141
142         return Qnil;
143 }
144
145 static VALUE c_get_part_swallow (VALUE self, VALUE part)
146 {
147         Evas_Object *o;
148         void *obj;
149
150         GET_OBJ (self, Evas_Object, e, "Edje");
151
152         Check_Type (part, T_STRING);
153
154         if (!(o = edje_object_part_swallow_get (*e, StringValuePtr (part))))
155                 return Qnil;
156
157         if (!(obj = evas_object_data_get (o, RUBY_EVAS_OBJECT_KEY))) {
158                 rb_raise (rb_eException, "EvasObject Ruby object key missing");
159                 return Qnil;
160         }
161
162         return (VALUE) obj;
163 }
164
165 void Init_Edje (void)
166 {
167         cEdje = rb_define_class_under (mEdje, "Edje", cEvasObject);
168
169         rb_define_singleton_method (cEdje, "new", c_new, 1);
170         rb_define_method (cEdje, "load", c_load, 2);
171         rb_define_method (cEdje, "get_size_min", c_get_size_min, 0);
172         rb_define_method (cEdje, "get_size_max", c_get_size_max, 0);
173         rb_define_method (cEdje, "part_exists?", c_part_exists_get, 1);
174         rb_define_method (cEdje, "part_swallow", c_part_swallow, 2);
175         rb_define_method (cEdje, "part_unswallow", c_part_unswallow, 1);
176         rb_define_method (cEdje, "get_part_swallow", c_get_part_swallow, 1);
177 }
178