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