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