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