adamdunkels | 539f13c | 2004-08-09 21:48:03 +0000 | [diff] [blame^] | 1 | |
| 2 | #include "contiki.h" |
| 3 | #include "ctk-filedialog.h" |
| 4 | |
| 5 | #include <string.h> |
| 6 | |
| 7 | #define MAX_NUMFILES 40 |
| 8 | #define FILES_WIDTH 17 |
| 9 | #define FILES_HEIGHT 14 |
| 10 | |
| 11 | static struct ctk_window dialog; |
| 12 | static char leftptr[FILES_HEIGHT]; |
| 13 | static struct ctk_label leftptrlabel = |
| 14 | {CTK_LABEL(0, 1, 1, FILES_HEIGHT, leftptr)}; |
| 15 | |
| 16 | static char files[FILES_WIDTH * MAX_NUMFILES]; |
| 17 | static struct ctk_label fileslabel = |
| 18 | {CTK_LABEL(1, 1, |
| 19 | FILES_WIDTH, FILES_HEIGHT, files)}; |
| 20 | |
| 21 | static char rightptr[FILES_HEIGHT]; |
| 22 | static struct ctk_label rightptrlabel = |
| 23 | {CTK_LABEL(1 + FILES_WIDTH, 1, 1, FILES_HEIGHT, rightptr)}; |
| 24 | |
| 25 | static char filename[FILES_WIDTH + 1]; |
| 26 | static struct ctk_textentry filenameentry = |
| 27 | {CTK_TEXTENTRY(1, 2 + FILES_HEIGHT, FILES_WIDTH, 1, filename, |
| 28 | FILES_WIDTH)}; |
| 29 | |
| 30 | static struct ctk_button button; |
| 31 | |
| 32 | #define STATE_CLOSED 0 |
| 33 | #define STATE_OPEN 1 |
| 34 | static char state = STATE_CLOSED; |
| 35 | static unsigned char fileptr, dirfileptr; |
| 36 | static struct cfs_dir dir; |
| 37 | /*---------------------------------------------------------------------------*/ |
| 38 | static void |
| 39 | clearptr(void) |
| 40 | { |
| 41 | leftptr[fileptr] = ' '; |
| 42 | rightptr[fileptr] = ' '; |
| 43 | } |
| 44 | /*---------------------------------------------------------------------------*/ |
| 45 | static void |
| 46 | showptr(void) |
| 47 | { |
| 48 | leftptr[fileptr] = '>'; |
| 49 | rightptr[fileptr] = '<'; |
| 50 | |
| 51 | strncpy(filename, |
| 52 | &files[fileptr * FILES_WIDTH], |
| 53 | FILES_WIDTH); |
| 54 | |
| 55 | CTK_WIDGET_REDRAW(&filenameentry); |
| 56 | CTK_WIDGET_REDRAW(&leftptrlabel); |
| 57 | CTK_WIDGET_REDRAW(&rightptrlabel); |
| 58 | } |
| 59 | /*---------------------------------------------------------------------------*/ |
| 60 | void |
| 61 | ctk_filedialog_init(register struct ctk_filedialog_state *s) |
| 62 | { |
| 63 | state = STATE_CLOSED; |
| 64 | } |
| 65 | /*---------------------------------------------------------------------------*/ |
| 66 | void |
| 67 | ctk_filedialog_open(register struct ctk_filedialog_state *s, |
| 68 | const char *buttontext, ek_event_t event) |
| 69 | { |
| 70 | ctk_dialog_new(&dialog, 20, 5 + FILES_HEIGHT); |
| 71 | CTK_WIDGET_ADD(&dialog, &leftptrlabel); |
| 72 | CTK_WIDGET_ADD(&dialog, &fileslabel); |
| 73 | CTK_WIDGET_ADD(&dialog, &rightptrlabel); |
| 74 | CTK_WIDGET_ADD(&dialog, &filenameentry); |
| 75 | CTK_BUTTON_NEW(&button, 1, 4 + FILES_HEIGHT, strlen(buttontext), (char *)buttontext); |
| 76 | CTK_WIDGET_ADD(&dialog, &button); |
| 77 | ctk_dialog_open(&dialog); |
| 78 | state = STATE_OPEN; |
| 79 | memset(filename, 0, sizeof(filename)); |
| 80 | memset(leftptr, ' ', sizeof(leftptr)); |
| 81 | memset(rightptr, ' ', sizeof(rightptr)); |
| 82 | memset(files, ' ', sizeof(files)); |
| 83 | |
| 84 | fileptr = 0; |
| 85 | dirfileptr = 0; |
| 86 | showptr(); |
| 87 | cfs_opendir(&dir, "/"); |
| 88 | ek_post(EK_PROC_ID(EK_CURRENT()), EK_EVENT_CONTINUE, s); |
| 89 | } |
| 90 | /*---------------------------------------------------------------------------*/ |
| 91 | char |
| 92 | ctk_filedialog_eventhandler(struct ctk_filedialog_state *s, |
| 93 | ek_event_t ev, ek_data_t data) |
| 94 | { |
| 95 | static struct cfs_dirent dirent; |
| 96 | |
| 97 | if(state == STATE_OPEN) { |
| 98 | if(ev == ctk_signal_widget_activate && |
| 99 | data == (ek_data_t)&button) { |
| 100 | ctk_dialog_close(); |
| 101 | state = STATE_CLOSED; |
| 102 | ek_post(EK_PROC_ID(EK_CURRENT()), s->ev, &filename); |
| 103 | return 1; |
| 104 | } else if(ev == EK_EVENT_CONTINUE && |
| 105 | (ek_data_t)s == data) { |
| 106 | if(cfs_readdir(&dir, &dirent) == 0 && |
| 107 | dirfileptr < MAX_NUMFILES) { |
| 108 | strncpy(&files[dirfileptr * FILES_WIDTH], |
| 109 | dirent.name, FILES_WIDTH); |
| 110 | CTK_WIDGET_REDRAW(&fileslabel); |
| 111 | ++dirfileptr; |
| 112 | ek_post(EK_PROC_ID(EK_CURRENT()), EK_EVENT_CONTINUE, s); |
| 113 | } else { |
| 114 | fileptr = 0; |
| 115 | cfs_closedir(&dir); |
| 116 | } |
| 117 | return 1; |
| 118 | } else if(ev == ctk_signal_keypress) { |
| 119 | if((ctk_arch_key_t)data == CH_CURS_UP) { |
| 120 | clearptr(); |
| 121 | if(fileptr > 0) { |
| 122 | --fileptr; |
| 123 | } |
| 124 | showptr(); |
| 125 | return 1; |
| 126 | } else if((ctk_arch_key_t)data == CH_CURS_DOWN) { |
| 127 | clearptr(); |
| 128 | if(fileptr < FILES_HEIGHT - 1) { |
| 129 | ++fileptr; |
| 130 | } |
| 131 | showptr(); |
| 132 | return 1; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | return 0; |
| 137 | } |
| 138 | /*---------------------------------------------------------------------------*/ |