blob: 991be53540d6e894c2844cd4b4633fd46d903a52 [file] [log] [blame]
adamdunkels539f13c2004-08-09 21:48:03 +00001
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
11static struct ctk_window dialog;
12static char leftptr[FILES_HEIGHT];
13static struct ctk_label leftptrlabel =
14 {CTK_LABEL(0, 1, 1, FILES_HEIGHT, leftptr)};
15
16static char files[FILES_WIDTH * MAX_NUMFILES];
17static struct ctk_label fileslabel =
18 {CTK_LABEL(1, 1,
19 FILES_WIDTH, FILES_HEIGHT, files)};
20
21static char rightptr[FILES_HEIGHT];
22static struct ctk_label rightptrlabel =
23 {CTK_LABEL(1 + FILES_WIDTH, 1, 1, FILES_HEIGHT, rightptr)};
24
25static char filename[FILES_WIDTH + 1];
26static struct ctk_textentry filenameentry =
27 {CTK_TEXTENTRY(1, 2 + FILES_HEIGHT, FILES_WIDTH, 1, filename,
28 FILES_WIDTH)};
29
30static struct ctk_button button;
31
32#define STATE_CLOSED 0
33#define STATE_OPEN 1
34static char state = STATE_CLOSED;
35static unsigned char fileptr, dirfileptr;
36static struct cfs_dir dir;
37/*---------------------------------------------------------------------------*/
38static void
39clearptr(void)
40{
41 leftptr[fileptr] = ' ';
42 rightptr[fileptr] = ' ';
43}
44/*---------------------------------------------------------------------------*/
45static void
46showptr(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/*---------------------------------------------------------------------------*/
60void
61ctk_filedialog_init(register struct ctk_filedialog_state *s)
62{
63 state = STATE_CLOSED;
64}
65/*---------------------------------------------------------------------------*/
66void
67ctk_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/*---------------------------------------------------------------------------*/
91char
92ctk_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/*---------------------------------------------------------------------------*/