A bit of adapting.
[Rockbox.git] / apps / plugins / text_editor.c
blob108a62aa0ceb604806564a7699246a5ba07e3ecb
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 Jonathan Gordon
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "plugin.h"
20 #include "action.h"
21 #include "playback_control.h"
23 #if PLUGIN_BUFFER_SIZE > 0x45000
24 #define MAX_CHARS 0x40000 /* 128 kiB */
25 #else
26 #define MAX_CHARS 0x6000 /* 24 kiB */
27 #endif
28 #define MAX_LINE_LEN 2048
29 PLUGIN_HEADER
30 static struct plugin_api* rb;
32 static char buffer[MAX_CHARS];
33 static char eol[3];
34 static int char_count = 0;
35 static int line_count = 0;
36 static int last_action_line = 0;
37 static int last_char_index = 0;
39 #define ACTION_INSERT 0
40 #define ACTION_GET 1
41 #define ACTION_REMOVE 2
42 #define ACTION_UPDATE 3
43 #define ACTION_CONCAT 4
45 int _do_action(int action, char* str, int line);
46 #ifndef HAVE_ADJUSTABLE_CPU_FREQ
47 #define do_action _do_action
48 #else
49 int do_action(int action, char* str, int line)
51 int r;
52 rb->cpu_boost(1);
53 r = _do_action(action,str,line);
54 rb->cpu_boost(0);
55 return r;
57 #endif
59 int _do_action(int action, char* str, int line)
61 int len;
62 int i=0,c=0;
63 if (line>=last_action_line)
65 i = last_action_line;
66 c = last_char_index;
68 while (i<line && i<line_count)
70 c += rb->strlen(&buffer[c])+1;
71 i++;
73 switch (action)
75 case ACTION_INSERT:
76 len = rb->strlen(str)+1;
77 if ( char_count+ len > MAX_CHARS )
78 return 0;
79 rb->memmove(&buffer[c+len],&buffer[c],char_count);
80 rb->strcpy(&buffer[c],str);
81 char_count += len;
82 line_count++;
83 break;
84 case ACTION_GET:
85 if (line > line_count)
86 return 0;
87 last_action_line = i;
88 last_char_index = c;
89 return c;
90 break;
91 case ACTION_REMOVE:
92 if (line > line_count)
93 return 0;
94 len = rb->strlen(&buffer[c])+1;
95 char_count -= len;
96 rb->memmove(&buffer[c],&buffer[c+len],char_count);
97 line_count--;
98 break;
99 case ACTION_UPDATE:
100 if (line > line_count)
101 return 0;
102 len = rb->strlen(&buffer[c])+1;
103 rb->memmove(&buffer[c+rb->strlen(str)+1],&buffer[c+len],char_count);
104 rb->strcpy(&buffer[c],str);
105 char_count += rb->strlen(str)+1-len;
106 break;
107 case ACTION_CONCAT:
108 if (line > line_count)
109 return 0;
110 rb->memmove(&buffer[c-1],&buffer[c],char_count);
111 break;
112 default:
113 return 0;
115 last_action_line = i;
116 last_char_index = c;
117 return 1;
119 char *list_get_name_cb(int selected_item,void* data,char* buf)
121 char *b = &buffer[do_action(ACTION_GET,0,selected_item)];
122 (void)data;
123 if (rb->strlen(b) >= MAX_PATH)
125 char t = b[MAX_PATH-10];
126 b[MAX_PATH-10] = '\0';
127 rb->snprintf(buf,MAX_PATH,"%s ...",b);
128 b[MAX_PATH-10] = t;
130 else rb->strcpy(buf,b);
131 return buf;
133 char filename[MAX_PATH];
134 int get_eol_string(char* fn)
136 int fd=-1;
137 char t;
138 if (!fn)
139 return 0;
140 else if (!fn[0])
141 return 0;
142 fd = rb->PREFIX(open(fn,O_RDONLY));
143 if (fd<0)
144 return 0;
145 eol[0] = '\0';
146 while (!eol[0])
148 if (!rb->read(fd,&t,1))
150 rb->strcpy(eol,"\n");
151 return 0;
153 if (t == '\r')
155 if (rb->read(fd,&t,1) && t=='\n')
156 rb->strcpy(eol,"\r\n");
157 else rb->strcpy(eol,"\r");
159 else if (t == '\n')
161 rb->strcpy(eol,"\n");
164 rb->close(fd);
165 return 1;
168 void save_changes(int overwrite)
170 int fd;
171 int i;
173 if (!filename[0] || !overwrite)
175 rb->strcpy(filename,"/");
176 rb->kbd_input(filename,MAX_PATH);
179 fd = rb->open(filename,O_WRONLY|O_CREAT|O_TRUNC);
180 if (fd < 0)
182 rb->splash(HZ*2, "Changes NOT saved");
183 return;
186 if (!overwrite)
187 /* current directory may have changed */
188 rb->reload_directory();
190 rb->lcd_clear_display();
191 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
192 rb->cpu_boost(1);
193 #endif
194 for (i=0;i<line_count;i++)
196 rb->fdprintf(fd,"%s%s",&buffer[do_action(ACTION_GET,0,i)],eol);
198 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
199 rb->cpu_boost(0);
200 #endif
201 rb->close(fd);
204 void setup_lists(struct gui_synclist *lists, int sel)
206 rb->gui_synclist_init(lists,list_get_name_cb,0, false, 1);
207 rb->gui_synclist_set_icon_callback(lists,NULL);
208 rb->gui_synclist_set_nb_items(lists,line_count);
209 rb->gui_synclist_limit_scroll(lists,true);
210 rb->gui_synclist_select_item(lists, sel);
211 rb->gui_synclist_draw(lists);
213 enum {
214 MENU_RET_SAVE = -1,
215 MENU_RET_NO_UPDATE,
216 MENU_RET_UPDATE,
218 int do_item_menu(int cur_sel, char* copy_buffer)
220 int ret = 0;
221 MENUITEM_STRINGLIST(menu, "Line Options", NULL,
222 "Cut/Delete", "Copy",
223 "Insert Above", "Insert Below",
224 "Concat To Above", "Save");
226 switch (rb->do_menu(&menu, NULL))
228 case 0: /* cut */
229 rb->strcpy(copy_buffer,&buffer[do_action(ACTION_GET,0,cur_sel)]);
230 do_action(ACTION_REMOVE,0,cur_sel);
231 ret = MENU_RET_UPDATE;
232 break;
233 case 1: /* copy */
234 rb->strcpy(copy_buffer,&buffer[do_action(ACTION_GET,0,cur_sel)]);
235 ret = MENU_RET_NO_UPDATE;
236 break;
237 case 2: /* insert above */
238 if (!rb->kbd_input(copy_buffer,MAX_LINE_LEN))
240 do_action(ACTION_INSERT,copy_buffer,cur_sel);
241 copy_buffer[0]='\0';
242 ret = MENU_RET_UPDATE;
244 break;
245 case 3: /* insert below */
246 if (!rb->kbd_input(copy_buffer,MAX_LINE_LEN))
248 do_action(ACTION_INSERT,copy_buffer,cur_sel+1);
249 copy_buffer[0]='\0';
250 ret = MENU_RET_UPDATE;
252 break;
253 case 4: /* cat to above */
254 if (cur_sel>0)
256 do_action(ACTION_CONCAT,0,cur_sel);
257 ret = MENU_RET_UPDATE;
259 break;
260 case 5: /* save */
261 ret = MENU_RET_SAVE;
262 break;
263 default:
264 ret = MENU_RET_NO_UPDATE;
265 break;
267 return ret;
270 #ifdef HAVE_LCD_COLOR
271 /* in misc.h but no need to polute the api */
272 #define toupper(c) (((c >= 'a') && (c <= 'z'))?c+'A':c)
273 #define isxdigit(c) ((c>='a' && c<= 'f') || (c>='A' && c<= 'F') \
274 || (c>='0' && c<= '9'))
275 #define hex2dec(c) (((c) >= '0' && ((c) <= '9')) ? (toupper(c)) - '0' : \
276 (toupper(c)) - 'A' + 10)
277 int hex_to_rgb(const char* hex)
278 { int ok = 1;
279 int i;
280 int red, green, blue;
282 if (rb->strlen(hex) == 6) {
283 for (i=0; i < 6; i++ ) {
284 if (!isxdigit(hex[i])) {
285 ok=0;
286 break;
290 if (ok) {
291 red = (hex2dec(hex[0]) << 4) | hex2dec(hex[1]);
292 green = (hex2dec(hex[2]) << 4) | hex2dec(hex[3]);
293 blue = (hex2dec(hex[4]) << 4) | hex2dec(hex[5]);
294 return LCD_RGBPACK(red,green,blue);
298 return 0;
300 #endif /* HAVE_LCD_COLOR */
302 /* this is the plugin entry point */
303 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
305 int fd;
306 static char temp_line[MAX_LINE_LEN];
308 struct gui_synclist lists;
309 bool exit = false;
310 int button;
311 bool changed = false;
312 int cur_sel=0;
313 static char copy_buffer[MAX_LINE_LEN];
314 bool prev_show_statusbar;
315 #ifdef HAVE_LCD_COLOR
316 bool edit_colors_file = false;
317 #endif
319 rb = api;
321 copy_buffer[0]='\0';
322 prev_show_statusbar = rb->global_settings->statusbar;
323 rb->global_settings->statusbar = false;
325 #if LCD_DEPTH > 1
326 rb->lcd_set_backdrop(NULL);
327 #endif
329 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
330 rb->cpu_boost(1);
331 #endif
332 if (parameter)
334 #ifdef HAVE_LCD_COLOR
335 char *c = NULL;
336 #endif
337 rb->strcpy(filename,(char*)parameter);
338 if (!get_eol_string(filename))
340 rb->strcpy(eol,"\n");
342 fd = rb->open(filename,O_RDONLY);
343 if (fd<0)
345 rb->splash(HZ*2,"Couldnt open file: %s",(char*)parameter);
346 return PLUGIN_ERROR;
348 #ifdef HAVE_LCD_COLOR
349 c = rb->strrchr(filename, '.');
350 if (c && !rb->strcmp(c, ".colours"))
351 edit_colors_file = true;
352 #endif
353 /* read in the file */
354 while (rb->read_line(fd,temp_line,MAX_LINE_LEN))
356 if (!do_action(ACTION_INSERT,temp_line,line_count))
358 rb->splash(HZ*2,"Error reading file: %s",(char*)parameter);
359 rb->close(fd);
360 return PLUGIN_ERROR;
363 rb->close(fd);
365 else
367 filename[0] = '\0';
368 rb->strcpy(eol,"\n");
370 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
371 rb->cpu_boost(0);
372 #endif
373 /* now dump it in the list */
374 setup_lists(&lists,0);
375 rb->lcd_update();
376 while (!exit)
378 rb->gui_synclist_draw(&lists);
379 cur_sel = rb->gui_synclist_get_sel_pos(&lists);
380 button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
381 if (rb->gui_synclist_do_button(&lists,&button,LIST_WRAP_UNLESS_HELD))
382 continue;
383 switch (button)
385 case ACTION_STD_OK:
387 bool edit_text = true;
388 #ifdef HAVE_LCD_COLOR
389 int color;
390 #endif
391 if (line_count)
392 rb->strcpy(temp_line,&buffer[do_action(ACTION_GET,0,cur_sel)]);
393 #ifdef HAVE_LCD_COLOR
394 if (edit_colors_file)
396 char *name = temp_line, *value = NULL;
397 char extension[MAX_LINE_LEN];
398 rb->settings_parseline(temp_line, &name, &value);
399 if (line_count)
401 MENUITEM_STRINGLIST(menu, "Edit What?", NULL,
402 "Extension", "Color",);
403 switch (rb->do_menu(&menu, NULL))
405 case 0:
406 edit_text = true;
407 break;
408 case 1:
409 edit_text = false;
410 if (value)
411 color = hex_to_rgb(value);
412 else color = 0;
413 rb->strcpy(extension, name);
414 rb->set_color(rb->screens[SCREEN_MAIN], name, &color, -1);
415 rb->snprintf(temp_line, MAX_LINE_LEN, "%s: %02X%02X%02X",
416 extension, RGB_UNPACK_RED(color),
417 RGB_UNPACK_GREEN(color),
418 RGB_UNPACK_BLUE(color));
419 if (line_count)
421 do_action(ACTION_UPDATE,temp_line,cur_sel);
423 else do_action(ACTION_INSERT,temp_line,cur_sel);
424 changed = true;
425 break;
429 #endif
430 if (edit_text &&!rb->kbd_input(temp_line,MAX_LINE_LEN))
432 if (line_count)
434 do_action(ACTION_UPDATE,temp_line,cur_sel);
436 else do_action(ACTION_INSERT,temp_line,cur_sel);
437 changed = true;
440 break;
441 case ACTION_STD_CONTEXT:
442 if (!line_count) break;
443 rb->strcpy(copy_buffer,&buffer[do_action(ACTION_GET,0,cur_sel)]);
444 do_action(ACTION_REMOVE,0,cur_sel);
445 changed = true;
446 break;
447 case ACTION_STD_MENU:
448 { /* do the item menu */
449 switch (do_item_menu(cur_sel, copy_buffer))
451 case MENU_RET_SAVE:
452 save_changes(1);
453 changed = false;
454 break;
455 case MENU_RET_UPDATE:
456 changed = true;
457 break;
458 case MENU_RET_NO_UPDATE:
459 break;
462 break;
463 case ACTION_STD_CANCEL:
464 if (changed)
466 MENUITEM_STRINGLIST(menu, "Do What?", NULL,
467 "Return",
468 "Show Playback Menu", "Save Changes",
469 "Save As...", "Save and Exit",
470 "Ignore Changes and Exit");
471 switch (rb->do_menu(&menu, NULL))
473 case 0:
474 break;
475 case 1:
476 playback_control(rb);
477 break;
478 case 2: //save to disk
479 save_changes(1);
480 changed = 0;
481 break;
482 case 3:
483 save_changes(0);
484 changed = 0;
485 break;
487 case 4:
488 save_changes(1);
489 exit=1;
490 break;
491 case 5:
492 exit=1;
493 break;
496 else exit=1;
497 break;
499 rb->gui_synclist_set_nb_items(&lists,line_count);
501 rb->global_settings->statusbar = prev_show_statusbar;
502 return PLUGIN_OK;