Class instantiation fixes.
[ruby-esmart.git] / src / esmart_file_dialog / rb_esmart_file_dialog.c
1 /*
2  * $Id: rb_esmart_file_dialog.c 356 2006-02-10 18:27:31Z tilman $
3  *
4  * Copyright (C) 2005 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 <Esmart/Esmart_File_Dialog.h>
24 #include <evas/rb_evas.h>
25 #include <evas/rb_evas_object.h>
26
27 #include "../rb_esmart.h"
28
29 typedef struct {
30         RbEvasObject real;
31         VALUE callback;
32         VALUE edje;
33 } RbFileDialog;
34
35 static void on_action (void *data, Evas_Object *edje,
36                        Esmart_File_Dialog_Op op);
37
38 static void c_mark (RbFileDialog *e)
39 {
40         c_evas_object_mark (&e->real);
41
42         if (!NIL_P (e->callback)) {
43                 rb_gc_mark (e->callback);
44                 rb_gc_mark (e->edje);
45         }
46 }
47
48 static void c_free (RbFileDialog *e)
49 {
50         c_evas_object_free (&e->real, false);
51
52         free (e);
53 }
54
55 static VALUE c_alloc (VALUE klass)
56 {
57         RbFileDialog *fd = NULL;
58
59         return Data_Make_Struct (klass, RbFileDialog, c_mark, c_free, fd);
60 }
61
62 static VALUE c_init (VALUE self, VALUE evas, VALUE edj)
63 {
64         if (!rb_block_given_p ()) {
65                 rb_raise (rb_eStandardError, "no block given");
66                 return Qnil;
67         }
68
69         CHECK_CLASS (evas, cEvas);
70         StringValue (edj);
71         GET_OBJ (evas, RbEvas, e);
72         GET_OBJ (self, RbFileDialog, fd);
73
74         fd->real.real = esmart_file_dialog_new (e->real,
75                                                 StringValuePtr (edj));
76
77         if (!fd->real.real)
78                 rb_raise (rb_eStandardError, "failed");
79
80         rb_call_super (1, &evas);
81
82         fd->callback = rb_block_proc ();
83         fd->edje = Qnil;
84
85         esmart_file_dialog_callback_add (fd->real.real, on_action,
86                                          (void *) fd->callback);
87
88         return self;
89 }
90
91 static void on_action (void *data, Evas_Object *edje,
92                        Esmart_File_Dialog_Op op)
93 {
94         ID sym;
95
96         switch (op) {
97                 case ESMART_FILE_DIALOG_NEW:
98                         sym = rb_intern ("new");
99                         break;
100                 case ESMART_FILE_DIALOG_RENAME:
101                         sym = rb_intern ("rename");
102                         break;
103                 case ESMART_FILE_DIALOG_DELETE:
104                         sym = rb_intern ("delete");
105                         break;
106                 case ESMART_FILE_DIALOG_OK:
107                         sym = rb_intern ("ok");
108                         break;
109                 case ESMART_FILE_DIALOG_CANCEL:
110                         sym = rb_intern ("cancel");
111                         break;
112                 case ESMART_FILE_DIALOG_DIR_CHANGED:
113                         sym = rb_intern ("dir_changed");
114                         break;
115                 default:
116                         sym = Qnil;
117         }
118
119         rb_funcall ((VALUE) data, rb_intern ("call"), 1, ID2SYM (sym));
120 }
121
122 static VALUE c_edje_get (VALUE self)
123 {
124         Evas_Object *e;
125         VALUE mEdje, cEdje, s;
126         ID const_get;
127
128         GET_OBJ (self, RbFileDialog, fd);
129
130         if (!NIL_P (fd->edje))
131                 return fd->edje;
132
133         const_get = rb_intern ("const_get");
134         s = rb_str_new2 ("Edje");
135
136         mEdje = rb_funcall (rb_cModule, const_get, 1, s);
137         cEdje = rb_funcall (mEdje, const_get, 1, s);
138
139         e = esmart_file_dialog_edje_get (fd->real.real);
140         fd->edje = rb_funcall (cEdje, rb_intern ("new_from_pointer"), 2,
141                                TO_EVAS (self, evas_object_evas_get (e)),
142                                (VALUE) e);
143
144         return fd->edje;
145 }
146
147 static VALUE c_selections_get (VALUE self)
148 {
149         Evas_List *list, *l;
150         VALUE ary;
151
152         GET_OBJ (self, RbFileDialog, fd);
153
154         ary = rb_ary_new ();
155
156         list = esmart_file_dialog_selections_get (fd->real.real);
157         if (!list)
158                 return ary;
159
160         for (l = list; l; l = l->next)
161                 rb_ary_push (ary, rb_str_new2 (l->data));
162
163         return ary;
164 }
165
166 static VALUE c_current_directory_get (VALUE self)
167 {
168         const char *d;
169
170         GET_OBJ (self, RbFileDialog, fd);
171
172         d = esmart_file_dialog_current_directory_get (fd->real.real);
173
174         return d ? rb_str_new2 (d) : Qnil;
175 }
176
177 void Init_esmart_file_dialog (void)
178 {
179         VALUE c;
180
181         rb_require ("esmart");
182
183         c = rb_define_class_under (mEsmart, "FileDialog", cEvasObject);
184
185         rb_define_alloc_func (c, c_alloc);
186         rb_define_method (c, "initialize", c_init, 2);
187         rb_define_method (c, "edje", c_edje_get, 0);
188         rb_define_method (c, "selections", c_selections_get, 0);
189         rb_define_method (c, "current_directory", c_current_directory_get, 0);
190 }