a45d3a49e245cb87e1b3bc280aa8f5b650aa51be
[ruby-esmart.git] / src / esmart_file_dialog / rb_esmart_file_dialog.c
1 /*
2  * $Id: rb_esmart_file_dialog.c 337 2005-05-02 17:48:20Z 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_new (VALUE klass, VALUE evas, VALUE edj)
56 {
57         VALUE self, argv[1];
58         RbFileDialog *fd = NULL;
59
60         if (!rb_block_given_p ()) {
61                 rb_raise (rb_eStandardError, "no block given");
62                 return Qnil;
63         }
64
65         CHECK_CLASS (evas, cEvas);
66         StringValue (edj);
67         GET_OBJ (evas, RbEvas, e);
68
69         self = Data_Make_Struct (klass, RbFileDialog, c_mark, c_free, fd);
70         fd->real.real = esmart_file_dialog_new (e->real,
71                                                 StringValuePtr (edj));
72
73         if (!fd->real.real)
74                 rb_raise (rb_eStandardError, "failed");
75
76         argv[0] = evas;
77         rb_obj_call_init (self, 1, argv);
78
79         fd->callback = rb_block_proc ();
80         fd->edje = Qnil;
81
82         esmart_file_dialog_callback_add (fd->real.real, on_action,
83                                          (void *) fd->callback);
84
85         return self;
86 }
87
88 static void on_action (void *data, Evas_Object *edje,
89                        Esmart_File_Dialog_Op op)
90 {
91         ID sym;
92
93         switch (op) {
94                 case ESMART_FILE_DIALOG_NEW:
95                         sym = rb_intern ("new");
96                         break;
97                 case ESMART_FILE_DIALOG_RENAME:
98                         sym = rb_intern ("rename");
99                         break;
100                 case ESMART_FILE_DIALOG_DELETE:
101                         sym = rb_intern ("delete");
102                         break;
103                 case ESMART_FILE_DIALOG_OK:
104                         sym = rb_intern ("ok");
105                         break;
106                 case ESMART_FILE_DIALOG_CANCEL:
107                         sym = rb_intern ("cancel");
108                         break;
109                 case ESMART_FILE_DIALOG_DIR_CHANGED:
110                         sym = rb_intern ("dir_changed");
111                         break;
112                 default:
113                         sym = Qnil;
114         }
115
116         rb_funcall ((VALUE) data, rb_intern ("call"), 1, ID2SYM (sym));
117 }
118
119 static VALUE c_edje_get (VALUE self)
120 {
121         Evas_Object *e;
122         VALUE mEdje, cEdje, s;
123         ID const_get;
124
125         GET_OBJ (self, RbFileDialog, fd);
126
127         if (!NIL_P (fd->edje))
128                 return fd->edje;
129
130         const_get = rb_intern ("const_get");
131         s = rb_str_new2 ("Edje");
132
133         mEdje = rb_funcall (rb_cModule, const_get, 1, s);
134         cEdje = rb_funcall (mEdje, const_get, 1, s);
135
136         e = esmart_file_dialog_edje_get (fd->real.real);
137         fd->edje = rb_funcall (cEdje, rb_intern ("new_from_pointer"), 2,
138                                TO_EVAS (self, evas_object_evas_get (e)),
139                                (VALUE) e);
140
141         return fd->edje;
142 }
143
144 static VALUE c_selections_get (VALUE self)
145 {
146         Evas_List *list, *l;
147         VALUE ary;
148
149         GET_OBJ (self, RbFileDialog, fd);
150
151         ary = rb_ary_new ();
152
153         list = esmart_file_dialog_selections_get (fd->real.real);
154         if (!list)
155                 return ary;
156
157         for (l = list; l; l = l->next)
158                 rb_ary_push (ary, rb_str_new2 (l->data));
159
160         return ary;
161 }
162
163 static VALUE c_current_directory_get (VALUE self)
164 {
165         const char *d;
166
167         GET_OBJ (self, RbFileDialog, fd);
168
169         d = esmart_file_dialog_current_directory_get (fd->real.real);
170
171         return d ? rb_str_new2 (d) : Qnil;
172 }
173
174 void Init_esmart_file_dialog (void)
175 {
176         VALUE c;
177
178         rb_require ("esmart");
179
180         c = rb_define_class_under (mEsmart, "FileDialog", cEvasObject);
181
182         rb_define_singleton_method (c, "new", c_new, 2);
183         rb_define_method (c, "edje", c_edje_get, 0);
184         rb_define_method (c, "selections", c_selections_get, 0);
185         rb_define_method (c, "current_directory", c_current_directory_get, 0);
186 }