00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "contiki.h"
00037 #include "lib/ctk-filedialog.h"
00038 #include "ctk/ctk.h"
00039 #include "cfs/cfs.h"
00040
00041 #include <string.h>
00042
00043 #define MAX_NUMFILES 40
00044 #define FILES_WIDTH 17
00045 #if FILES_CONF_HEIGHT
00046 #define FILES_HEIGHT FILES_CONF_HEIGHT
00047 #else
00048 #define FILES_HEIGHT 14
00049 #endif
00050
00051 static struct ctk_window dialog;
00052 static char leftptr[FILES_HEIGHT];
00053 static struct ctk_label leftptrlabel =
00054 {CTK_LABEL(0, 1, 1, FILES_HEIGHT, leftptr)};
00055
00056 static char files[FILES_WIDTH * MAX_NUMFILES];
00057 static struct ctk_label fileslabel =
00058 {CTK_LABEL(1, 1,
00059 FILES_WIDTH, FILES_HEIGHT, files)};
00060
00061 static char rightptr[FILES_HEIGHT];
00062 static struct ctk_label rightptrlabel =
00063 {CTK_LABEL(1 + FILES_WIDTH, 1, 1, FILES_HEIGHT, rightptr)};
00064
00065 static char filename[FILES_WIDTH + 1];
00066 static struct ctk_textentry filenameentry =
00067 {CTK_TEXTENTRY(1, 2 + FILES_HEIGHT, FILES_WIDTH, 1, filename,
00068 FILES_WIDTH)};
00069
00070 static struct ctk_button button;
00071
00072 #define STATE_CLOSED 0
00073 #define STATE_OPEN 1
00074 static char state = STATE_CLOSED;
00075 static unsigned char fileptr, dirfileptr;
00076 static struct cfs_dir dir;
00077
00078 static void
00079 clearptr(void)
00080 {
00081 leftptr[fileptr] = ' ';
00082 rightptr[fileptr] = ' ';
00083 }
00084
00085 static void
00086 showptr(void)
00087 {
00088 leftptr[fileptr] = '>';
00089 rightptr[fileptr] = '<';
00090
00091 strncpy(filename,
00092 &files[fileptr * FILES_WIDTH],
00093 FILES_WIDTH);
00094
00095 CTK_WIDGET_REDRAW(&filenameentry);
00096 CTK_WIDGET_REDRAW(&leftptrlabel);
00097 CTK_WIDGET_REDRAW(&rightptrlabel);
00098 }
00099
00100 void
00101 ctk_filedialog_init(CC_REGISTER_ARG struct ctk_filedialog_state *s)
00102 {
00103 state = STATE_CLOSED;
00104 }
00105
00106 void
00107 ctk_filedialog_open(CC_REGISTER_ARG struct ctk_filedialog_state *s,
00108 const char *buttontext, process_event_t event)
00109 {
00110 ctk_dialog_new(&dialog, 20, 5 + FILES_HEIGHT);
00111 CTK_WIDGET_ADD(&dialog, &leftptrlabel);
00112 CTK_WIDGET_ADD(&dialog, &fileslabel);
00113 CTK_WIDGET_ADD(&dialog, &rightptrlabel);
00114 CTK_WIDGET_ADD(&dialog, &filenameentry);
00115 CTK_BUTTON_NEW(&button, 1, 4 + FILES_HEIGHT, strlen(buttontext), (char *)buttontext);
00116 CTK_WIDGET_ADD(&dialog, &button);
00117 ctk_dialog_open(&dialog);
00118 state = STATE_OPEN;
00119 memset(filename, 0, sizeof(filename));
00120 memset(leftptr, ' ', sizeof(leftptr));
00121 memset(rightptr, ' ', sizeof(rightptr));
00122 memset(files, 0, sizeof(files));
00123
00124 fileptr = 0;
00125 dirfileptr = 0;
00126 showptr();
00127 cfs_opendir(&dir, ".");
00128 process_post(PROCESS_CURRENT(), PROCESS_EVENT_CONTINUE, s);
00129 }
00130
00131 char
00132 ctk_filedialog_eventhandler(struct ctk_filedialog_state *s,
00133 process_event_t ev, process_data_t data)
00134 {
00135 static struct cfs_dirent dirent;
00136
00137 if(state == STATE_OPEN) {
00138 if(ev == ctk_signal_widget_activate &&
00139 data == (process_data_t)&button) {
00140 ctk_dialog_close();
00141 state = STATE_CLOSED;
00142 process_post(PROCESS_CURRENT(), s->ev, &filename);
00143 return 1;
00144 } else if(ev == PROCESS_EVENT_CONTINUE &&
00145 (process_data_t)s == data) {
00146 if(cfs_readdir(&dir, &dirent) == 0 &&
00147 dirfileptr < MAX_NUMFILES) {
00148 strncpy(&files[dirfileptr * FILES_WIDTH],
00149 dirent.name, FILES_WIDTH);
00150 CTK_WIDGET_REDRAW(&fileslabel);
00151 ++dirfileptr;
00152 process_post(PROCESS_CURRENT(), PROCESS_EVENT_CONTINUE, s);
00153 } else {
00154 fileptr = 0;
00155 cfs_closedir(&dir);
00156 }
00157 return 1;
00158 } else if(ev == ctk_signal_keypress) {
00159 if((ctk_arch_key_t)data == CH_CURS_UP) {
00160 clearptr();
00161 if(fileptr > 0) {
00162 --fileptr;
00163 }
00164 showptr();
00165 return 1;
00166 } else if((ctk_arch_key_t)data == CH_CURS_DOWN) {
00167 clearptr();
00168 if(fileptr < FILES_HEIGHT - 1) {
00169 ++fileptr;
00170 }
00171 showptr();
00172 return 1;
00173 }
00174 }
00175 }
00176 return 0;
00177 }
00178