1 #include <Elementary.h>
4 * @defgroup Notepad Notepad
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.
9 * Signals that you can add callbacks for are:
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().
19 typedef struct _Widget_Data Widget_Data;
23 Evas_Object *scr, *entry;
25 Elm_Text_Format format;
26 Ecore_Timer *delay_write;
27 Eina_Bool can_write : 1;
28 Eina_Bool autosave : 1;
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);
39 _del_hook(Evas_Object *obj)
41 Widget_Data *wd = elm_widget_data_get(obj);
43 if (wd->file) eina_stringshare_del(wd->file);
46 ecore_timer_del(wd->delay_write);
47 if (wd->autosave) _save(obj);
53 _sizing_eval(Evas_Object *obj)
55 Widget_Data *wd = elm_widget_data_get(obj);
57 evas_object_size_hint_min_set(obj, -1, -1);
58 evas_object_size_hint_max_set(obj, -1, -1);
62 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
64 Widget_Data *wd = elm_widget_data_get(obj);
66 if (elm_widget_focus_get(obj)) elm_widget_focus_steal(wd->entry);
70 _buf_append(char *buf, const char *str, int *len, int *alloc)
72 int len2 = strlen(str);
73 if ((*len + len2) >= *alloc)
75 char *buf2 = realloc(buf, *alloc + len2 + 512);
76 if (!buf2) return NULL;
78 *alloc += (512 + len2);
80 strcpy(buf + *len, str);
86 _load_file(const char *file)
90 int alloc = 0, len = 0;
91 char *text = NULL, buf[PATH_MAX];
93 f = fopen(file, "rb");
95 while ((size = fread(buf, 1, sizeof(buf), f)))
98 text = _buf_append(text, buf, &len, &alloc);
105 _load_plain(const char *file)
109 text = _load_file(file);
114 text2 = elm_entry_utf8_to_markup(text);
122 _load(Evas_Object *obj)
124 Widget_Data *wd = elm_widget_data_get(obj);
129 elm_entry_entry_set(wd->entry, "");
134 case ELM_TEXT_FORMAT_PLAIN_UTF8:
135 text = _load_plain(wd->file);
137 case ELM_TEXT_FORMAT_MARKUP_UTF8:
138 text = _load_file(wd->file);
146 elm_entry_entry_set(wd->entry, text);
150 elm_entry_entry_set(wd->entry, "");
154 _save_markup_utf8(const char *file, const char *text)
158 if ((!text) || (text[0] == 0))
160 ecore_file_unlink(file);
163 f = fopen(file, "wb");
166 // FIXME: report a write error
169 fputs(text, f); // FIXME: catch error
174 _save_plain_utf8(const char *file, const char *text)
178 text2 = elm_entry_markup_to_utf8(text);
181 _save_markup_utf8(file, text2);
187 _save(Evas_Object *obj)
189 Widget_Data *wd = elm_widget_data_get(obj);
191 if (!wd->file) return;
194 case ELM_TEXT_FORMAT_PLAIN_UTF8:
195 _save_plain_utf8(wd->file, elm_entry_entry_get(wd->entry));
197 case ELM_TEXT_FORMAT_MARKUP_UTF8:
198 _save_markup_utf8(wd->file, elm_entry_entry_get(wd->entry));
206 _delay_write(void *data)
208 Widget_Data *wd = elm_widget_data_get(data);
209 if (!wd) return ECORE_CALLBACK_CANCEL;
211 wd->delay_write = NULL;
212 return ECORE_CALLBACK_CANCEL;
216 _entry_changed(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
218 Widget_Data *wd = elm_widget_data_get(data);
222 ecore_timer_del(wd->delay_write);
223 wd->delay_write = NULL;
225 if (!wd->autosave) return;
226 wd->delay_write = ecore_timer_add(2.0, _delay_write, data);
230 _hold_on(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
232 Widget_Data *wd = elm_widget_data_get(obj);
234 elm_widget_scroll_hold_push(wd->scr);
238 _hold_off(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
240 Widget_Data *wd = elm_widget_data_get(obj);
242 elm_widget_scroll_hold_pop(wd->scr);
246 _freeze_on(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
248 Widget_Data *wd = elm_widget_data_get(obj);
250 elm_widget_scroll_hold_push(wd->scr);
254 _freeze_off(void *data __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
256 Widget_Data *wd = elm_widget_data_get(obj);
258 elm_widget_scroll_hold_pop(wd->scr);
262 * Add a new notepad to the parent
264 * @param parent The parent object
265 * @return The new object or NULL if it cannot be created
270 elm_notepad_add(Evas_Object *parent)
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);
287 wd->scr = elm_scroller_add(parent);
288 elm_widget_resize_object_set(obj, wd->scr);
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);
296 elm_entry_entry_set(wd->entry, "");
297 evas_object_smart_callback_add(wd->entry, "changed", _entry_changed, obj);
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);
304 wd->autosave = EINA_TRUE;
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.
315 * @param obj The notepad object
316 * @param file The path to the file to load and save
317 * @param format The file format
322 elm_notepad_file_set(Evas_Object *obj, const char *file, Elm_Text_Format format)
324 ELM_CHECK_WIDTYPE(obj, widtype);
325 Widget_Data *wd = elm_widget_data_get(obj);
329 ecore_timer_del(wd->delay_write);
330 wd->delay_write = NULL;
332 if (wd->autosave) _save(obj);
333 eina_stringshare_replace(&wd->file, file);
339 * This function writes any changes made to the file.
340 * All changes are written back to the file after a short delay.
342 * @param obj The notepad object
343 * @param file The path to the file to save
344 * @param format The file format
349 elm_notepad_file_save(Evas_Object *obj, const char *file, Elm_Text_Format format)
351 ELM_CHECK_WIDTYPE(obj, widtype);
352 Widget_Data *wd = elm_widget_data_get(obj);
353 if ((!wd) || (!file)) return;
356 ecore_timer_del(wd->delay_write);
357 wd->delay_write = NULL;
360 eina_stringshare_replace(&wd->file, file);
361 wd->delay_write = ecore_timer_add(2.0, _delay_write, obj);
365 * This will enable or disable the scroller bounce mode for the notepad. See
366 * elm_scroller_bounce_set() for details
368 * @param obj The notepad object
369 * @param h_bounce Allow bounce horizontally
370 * @param v_bounce Allow bounce vertically
375 elm_notepad_bounce_set(Evas_Object *obj, Eina_Bool h_bounce, Eina_Bool v_bounce)
377 ELM_CHECK_WIDTYPE(obj, widtype);
378 Widget_Data *wd = elm_widget_data_get(obj);
380 elm_scroller_bounce_set(wd->scr, h_bounce, v_bounce);
384 * This sets the notepad object to 'autosave' the loaded text file or not.
386 * @param obj The notepad object
387 * @param autosave Autosave the loaded file or not
392 elm_notepad_autosave_set(Evas_Object *obj, Eina_Bool autosave)
394 ELM_CHECK_WIDTYPE(obj, widtype);
395 Widget_Data *wd = elm_widget_data_get(obj);
397 wd->autosave = autosave;