svn update: 51469 (latest:51480)
[framework/uifw/elementary.git] / src / lib / elc_notepad.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3 /**
4  * @defgroup Notepad Notepad
5  *
6  * The notepad is an object for quickly loading a text file, displaying it,
7  * allowing editing of it and saving of changes back to the file loaded.
8  *
9  * Signals that you can add callbacks for are:
10  *
11  * NONE
12  *
13  * A notepad object contains a scroller and an entry. It is a convenience
14  * widget that loads a text file indicated, puts it in the scrollable entry
15  * and allows the user to edit it. Changes are written back to the original
16  * file after a short delay. The file to load and save to is specified by
17  * elm_notepad_file_set().
18  */
19 typedef struct _Widget_Data Widget_Data;
20
21 struct _Widget_Data
22 {
23    Evas_Object *scr, *entry;
24    const char *file;
25    Elm_Text_Format format;
26    Ecore_Timer *delay_write;
27    Eina_Bool can_write : 1;
28    Eina_Bool autosave : 1;
29 };
30
31 static const char *widtype = NULL;
32 static void _del_hook(Evas_Object *obj);
33 static void _sizing_eval(Evas_Object *obj);
34 static void _on_focus_hook(void *data, Evas_Object *obj);
35 static void _load(Evas_Object *obj);
36 static void _save(Evas_Object *obj);
37
38 static void
39 _del_hook(Evas_Object *obj)
40 {
41    Widget_Data *wd = elm_widget_data_get(obj);
42    if (!wd) return;
43    if (wd->file) eina_stringshare_del(wd->file);
44    if (wd->delay_write)
45      {
46         ecore_timer_del(wd->delay_write);
47         if (wd->autosave) _save(obj);
48      }
49    free(wd);
50 }
51
52 static void
53 _sizing_eval(Evas_Object *obj)
54 {
55    Widget_Data *wd = elm_widget_data_get(obj);
56    if (!wd) return;
57    evas_object_size_hint_min_set(obj, -1, -1);
58    evas_object_size_hint_max_set(obj, -1, -1);
59 }
60
61 static void
62 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
63 {
64    Widget_Data *wd = elm_widget_data_get(obj);
65    if (!wd) return;
66    if (elm_widget_focus_get(obj)) elm_widget_focus_steal(wd->entry);
67 }
68
69 static char *
70 _buf_append(char *buf, const char *str, int *len, int *alloc)
71 {
72    int len2 = strlen(str);
73    if ((*len + len2) >= *alloc)
74      {
75         char *buf2 = realloc(buf, *alloc + len2 + 512);
76         if (!buf2) return NULL;
77         buf = buf2;
78         *alloc += (512 + len2);
79      }
80    strcpy(buf + *len, str);
81    *len += len2;
82    return buf;
83 }
84
85 static char *
86 _load_file(const char *file)
87 {
88    FILE *f;
89    size_t size;
90    int alloc = 0, len = 0;
91    char *text = NULL, buf[PATH_MAX];
92
93    f = fopen(file, "rb");
94    if (!f) return NULL;
95    while ((size = fread(buf, 1, sizeof(buf), f)))
96      {
97         buf[size] = 0;
98         text = _buf_append(text, buf, &len, &alloc);
99      }
100    fclose(f);
101    return text;
102 }
103
104 static char *
105 _load_plain(const char *file)
106 {
107    char *text;
108
109    text = _load_file(file);
110    if (text)
111      {
112         char *text2;
113
114         text2 = elm_entry_utf8_to_markup(text);
115         free(text);
116         return text2;
117      }
118    return NULL;
119 }
120
121 static void
122 _load(Evas_Object *obj)
123 {
124    Widget_Data *wd = elm_widget_data_get(obj);
125    char *text;
126    if (!wd) return;
127    if (!wd->file)
128      {
129         elm_entry_entry_set(wd->entry, "");
130         return;
131      }
132    switch (wd->format)
133      {
134      case ELM_TEXT_FORMAT_PLAIN_UTF8:
135         text = _load_plain(wd->file);
136         break;
137      case ELM_TEXT_FORMAT_MARKUP_UTF8:
138         text = _load_file(wd->file);
139         break;
140      default:
141         text = NULL;
142         break;
143      }
144    if (text)
145      {
146         elm_entry_entry_set(wd->entry, text);
147         free(text);
148      }
149    else
150      elm_entry_entry_set(wd->entry, "");
151 }
152
153 static void
154 _save_markup_utf8(const char *file, const char *text)
155 {
156    FILE *f;
157    
158    if ((!text) || (text[0] == 0))
159      {
160         ecore_file_unlink(file);
161         return;
162      }
163    f = fopen(file, "wb");
164    if (!f)
165      {
166         // FIXME: report a write error
167         return;
168      }
169    fputs(text, f); // FIXME: catch error
170    fclose(f);
171 }
172
173 static void
174 _save_plain_utf8(const char *file, const char *text)
175 {
176    char *text2;
177    
178    text2 = elm_entry_markup_to_utf8(text);
179    if (text2)
180      {
181         _save_markup_utf8(file, text2);
182         free(text2);
183      }
184 }
185
186 static void
187 _save(Evas_Object *obj)
188 {
189    Widget_Data *wd = elm_widget_data_get(obj);
190    if (!wd) return;
191    if (!wd->file) return;
192    switch (wd->format)
193      {
194       case ELM_TEXT_FORMAT_PLAIN_UTF8:
195         _save_plain_utf8(wd->file, elm_entry_entry_get(wd->entry));
196         break;
197       case ELM_TEXT_FORMAT_MARKUP_UTF8:
198         _save_markup_utf8(wd->file, elm_entry_entry_get(wd->entry));
199         break;
200       default:
201         break;
202      }
203 }
204
205 static Eina_Bool
206 _delay_write(void *data)
207 {
208    Widget_Data *wd = elm_widget_data_get(data);
209    if (!wd) return ECORE_CALLBACK_CANCEL;
210    _save(data);
211    wd->delay_write = NULL;
212    return ECORE_CALLBACK_CANCEL;
213 }
214
215 static void
216 _entry_changed(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
217 {
218    Widget_Data *wd = elm_widget_data_get(data);
219    if (!wd) return;
220    if (wd->delay_write)
221      {
222         ecore_timer_del(wd->delay_write);
223         wd->delay_write = NULL;
224      }
225    if (!wd->autosave) return;
226    wd->delay_write = ecore_timer_add(2.0, _delay_write, data);
227 }
228
229 static void
230 _hold_on(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
231 {
232    Widget_Data *wd = elm_widget_data_get(obj);
233    if (!wd) return;
234    elm_widget_scroll_hold_push(wd->scr);
235 }
236
237 static void
238 _hold_off(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
239 {
240    Widget_Data *wd = elm_widget_data_get(obj);
241    if (!wd) return;
242    elm_widget_scroll_hold_pop(wd->scr);
243 }
244
245 static void
246 _freeze_on(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
247 {
248    Widget_Data *wd = elm_widget_data_get(obj);
249    if (!wd) return;
250    elm_widget_scroll_hold_push(wd->scr);
251 }
252
253 static void
254 _freeze_off(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
255 {
256    Widget_Data *wd = elm_widget_data_get(obj);
257    if (!wd) return;
258    elm_widget_scroll_hold_pop(wd->scr);
259 }
260
261 /**
262  * Add a new notepad to the parent
263  *
264  * @param parent The parent object
265  * @return The new object or NULL if it cannot be created
266  *
267  * @ingroup Notepad
268  */
269 EAPI Evas_Object *
270 elm_notepad_add(Evas_Object *parent)
271 {
272    Evas_Object *obj;
273    Evas *e;
274    Widget_Data *wd;
275
276    wd = ELM_NEW(Widget_Data);
277    e = evas_object_evas_get(parent);
278    obj = elm_widget_add(e);
279    ELM_SET_WIDTYPE(widtype, "notepad");
280    elm_widget_type_set(obj, "notepad");
281    elm_widget_sub_object_add(parent, obj);
282    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
283    elm_widget_data_set(obj, wd);
284    elm_widget_del_hook_set(obj, _del_hook);
285    elm_widget_can_focus_set(obj, 1);
286
287    wd->scr = elm_scroller_add(parent);
288    elm_widget_resize_object_set(obj, wd->scr);
289
290    wd->entry = elm_entry_add(parent);
291    evas_object_size_hint_weight_set(wd->entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
292    evas_object_size_hint_align_set(wd->entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
293    elm_scroller_content_set(wd->scr, wd->entry);
294    evas_object_show(wd->entry);
295
296    elm_entry_entry_set(wd->entry, "");
297    evas_object_smart_callback_add(wd->entry, "changed", _entry_changed, obj);
298    
299    evas_object_smart_callback_add(obj, "scroll-hold-on", _hold_on, obj);
300    evas_object_smart_callback_add(obj, "scroll-hold-off", _hold_off, obj);
301    evas_object_smart_callback_add(obj, "scroll-freeze-on", _freeze_on, obj);
302    evas_object_smart_callback_add(obj, "scroll-freeze-off", _freeze_off, obj);
303    
304    wd->autosave = EINA_TRUE;
305
306    _sizing_eval(obj);
307    return obj;
308 }
309
310 /**
311  * This sets the file (and implicitly loads it) for the text to display and
312  * then edit. All changes are written back to the file after a short delay if
313  * the notepad object is set to autosave.
314  *
315  * @param obj The notepad object
316  * @param file The path to the file to load and save
317  * @param format The file format
318  *
319  * @ingroup Notepad
320  */
321 EAPI void
322 elm_notepad_file_set(Evas_Object *obj, const char *file, Elm_Text_Format format)
323 {
324    ELM_CHECK_WIDTYPE(obj, widtype);
325    Widget_Data *wd = elm_widget_data_get(obj);
326    if (!wd) return;
327    if (wd->delay_write)
328      {
329         ecore_timer_del(wd->delay_write);
330         wd->delay_write = NULL;
331      }
332    if (wd->autosave) _save(obj);
333    eina_stringshare_replace(&wd->file, file);
334    wd->format = format;
335    _load(obj);
336 }
337
338 /**
339  * This function writes any changes made to the file.
340  * All changes are written back to the file after a short delay.
341  *
342  * @param obj The notepad object
343  * @param file The path to the file to save
344  * @param format The file format
345  *
346  * @ingroup Notepad
347  */
348 EAPI void 
349 elm_notepad_file_save(Evas_Object *obj, const char *file, Elm_Text_Format format) 
350 {
351    ELM_CHECK_WIDTYPE(obj, widtype);
352    Widget_Data *wd = elm_widget_data_get(obj);
353    if ((!wd) || (!file)) return;
354    if (wd->delay_write)
355      {
356         ecore_timer_del(wd->delay_write);
357         wd->delay_write = NULL;
358      }
359    wd->format = format;
360    eina_stringshare_replace(&wd->file, file);
361    wd->delay_write = ecore_timer_add(2.0, _delay_write, obj);
362 }
363
364 /**
365  * This will enable or disable the scroller bounce mode for the notepad. See
366  * elm_scroller_bounce_set() for details
367  *
368  * @param obj The notepad object
369  * @param h_bounce Allow bounce horizontally
370  * @param v_bounce Allow bounce vertically
371  *
372  * @ingroup Notepad
373  */
374 EAPI void
375 elm_notepad_bounce_set(Evas_Object *obj, Eina_Bool h_bounce, Eina_Bool v_bounce)
376 {
377    ELM_CHECK_WIDTYPE(obj, widtype);
378    Widget_Data *wd = elm_widget_data_get(obj);
379    if (!wd) return;
380    elm_scroller_bounce_set(wd->scr, h_bounce, v_bounce);
381 }
382
383 /**
384  * This sets the notepad object to 'autosave' the loaded text file or not.
385  *
386  * @param obj The notepad object
387  * @param autosave Autosave the loaded file or not
388  *
389  * @ingroup Notepad
390  */
391 EAPI void 
392 elm_notepad_autosave_set(Evas_Object *obj, Eina_Bool autosave) 
393 {
394    ELM_CHECK_WIDTYPE(obj, widtype);
395    Widget_Data *wd = elm_widget_data_get(obj);
396    if (!wd) return;
397    wd->autosave = autosave;
398 }