Removed RCS-style IDs.
[ruby-esmart.git] / src / esmart_file_dialog / rb_esmart_file_dialog.c
1 /*
2  * Copyright (C) 2005 Tilman Sauerbeck (tilman at code-monkey de)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include <ruby.h>
20
21 #include <Esmart/Esmart_File_Dialog.h>
22 #include <evas/rb_evas.h>
23 #include <evas/rb_evas_object.h>
24
25 #include "../rb_esmart.h"
26
27 typedef struct {
28         RbEvasObject real;
29         VALUE callback;
30         VALUE edje;
31 } RbFileDialog;
32
33 static void on_action (void *data, Evas_Object *edje,
34                        Esmart_File_Dialog_Op op);
35
36 static VALUE cEdje;
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
126         GET_OBJ (self, RbFileDialog, fd);
127
128         if (!NIL_P (fd->edje))
129                 return fd->edje;
130
131         e = esmart_file_dialog_edje_get (fd->real.real);
132         fd->edje = rb_funcall (cEdje, rb_intern ("new_from_pointer"), 2,
133                                TO_EVAS (self, evas_object_evas_get (e)),
134                                (VALUE) e);
135
136         return fd->edje;
137 }
138
139 static VALUE c_selections_get (VALUE self)
140 {
141         Evas_List *list, *l;
142         VALUE ary;
143
144         GET_OBJ (self, RbFileDialog, fd);
145
146         ary = rb_ary_new ();
147
148         list = esmart_file_dialog_selections_get (fd->real.real);
149         if (!list)
150                 return ary;
151
152         for (l = list; l; l = l->next)
153                 rb_ary_push (ary, rb_str_new2 (l->data));
154
155         return ary;
156 }
157
158 static VALUE c_current_directory_get (VALUE self)
159 {
160         const char *d;
161
162         GET_OBJ (self, RbFileDialog, fd);
163
164         d = esmart_file_dialog_current_directory_get (fd->real.real);
165
166         return d ? rb_str_new2 (d) : Qnil;
167 }
168
169 void Init_esmart_file_dialog (void)
170 {
171         VALUE m, c;
172
173         rb_require ("esmart");
174         rb_require ("edje");
175
176         m = rb_const_get (rb_cModule, rb_intern ("Edje"));
177         cEdje = rb_const_get (m, rb_intern ("Edje"));
178
179         c = rb_define_class_under (mEsmart, "FileDialog", cEvasObject);
180
181         rb_define_alloc_func (c, c_alloc);
182         rb_define_method (c, "initialize", c_init, 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 }