Added support for the FTP standalone client to the c64 target.
[contiki-2.x.git] / core / lib / ctk-filedialog.c
blob56b79acbb422f2fe99078abafb8c017c2831baa5
1 /*
2 * Copyright (c) 2004, Swedish Institute of Computer Science.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * This file is part of the Contiki operating system.
31 * Author: Adam Dunkels <adam@sics.se>
33 * $Id: ctk-filedialog.c,v 1.2 2007/08/30 14:39:18 matsutsuka Exp $
36 #include "contiki.h"
37 #include "lib/ctk-filedialog.h"
38 #include "ctk/ctk.h"
39 #include "cfs/cfs.h"
41 #include <string.h>
43 #define MAX_NUMFILES 40
44 #define FILES_WIDTH 17
45 #if FILES_CONF_HEIGHT
46 #define FILES_HEIGHT FILES_CONF_HEIGHT
47 #else
48 #define FILES_HEIGHT 14
49 #endif
51 static struct ctk_window dialog;
52 static char leftptr[FILES_HEIGHT];
53 static struct ctk_label leftptrlabel =
54 {CTK_LABEL(0, 1, 1, FILES_HEIGHT, leftptr)};
56 static char files[FILES_WIDTH * MAX_NUMFILES];
57 static struct ctk_label fileslabel =
58 {CTK_LABEL(1, 1,
59 FILES_WIDTH, FILES_HEIGHT, files)};
61 static char rightptr[FILES_HEIGHT];
62 static struct ctk_label rightptrlabel =
63 {CTK_LABEL(1 + FILES_WIDTH, 1, 1, FILES_HEIGHT, rightptr)};
65 static char filename[FILES_WIDTH + 1];
66 static struct ctk_textentry filenameentry =
67 {CTK_TEXTENTRY(1, 2 + FILES_HEIGHT, FILES_WIDTH, 1, filename,
68 FILES_WIDTH)};
70 static struct ctk_button button;
72 #define STATE_CLOSED 0
73 #define STATE_OPEN 1
74 static char state = STATE_CLOSED;
75 static unsigned char fileptr, dirfileptr;
76 static struct cfs_dir dir;
77 /*---------------------------------------------------------------------------*/
78 static void
79 clearptr(void)
81 leftptr[fileptr] = ' ';
82 rightptr[fileptr] = ' ';
84 /*---------------------------------------------------------------------------*/
85 static void
86 showptr(void)
88 leftptr[fileptr] = '>';
89 rightptr[fileptr] = '<';
91 strncpy(filename,
92 &files[fileptr * FILES_WIDTH],
93 FILES_WIDTH);
95 CTK_WIDGET_REDRAW(&filenameentry);
96 CTK_WIDGET_REDRAW(&leftptrlabel);
97 CTK_WIDGET_REDRAW(&rightptrlabel);
99 /*---------------------------------------------------------------------------*/
100 void
101 ctk_filedialog_init(CC_REGISTER_ARG struct ctk_filedialog_state *s)
103 state = STATE_CLOSED;
105 /*---------------------------------------------------------------------------*/
106 void
107 ctk_filedialog_open(CC_REGISTER_ARG struct ctk_filedialog_state *s,
108 const char *buttontext, process_event_t event)
110 ctk_dialog_new(&dialog, 20, 5 + FILES_HEIGHT);
111 CTK_WIDGET_ADD(&dialog, &leftptrlabel);
112 CTK_WIDGET_ADD(&dialog, &fileslabel);
113 CTK_WIDGET_ADD(&dialog, &rightptrlabel);
114 CTK_WIDGET_ADD(&dialog, &filenameentry);
115 CTK_BUTTON_NEW(&button, 1, 4 + FILES_HEIGHT, strlen(buttontext), (char *)buttontext);
116 CTK_WIDGET_ADD(&dialog, &button);
117 ctk_dialog_open(&dialog);
118 state = STATE_OPEN;
119 memset(filename, 0, sizeof(filename));
120 memset(leftptr, ' ', sizeof(leftptr));
121 memset(rightptr, ' ', sizeof(rightptr));
122 memset(files, 0, sizeof(files));
124 fileptr = 0;
125 dirfileptr = 0;
126 showptr();
127 cfs_opendir(&dir, ".");
128 process_post(PROCESS_CURRENT(), PROCESS_EVENT_CONTINUE, s);
130 /*---------------------------------------------------------------------------*/
131 char
132 ctk_filedialog_eventhandler(struct ctk_filedialog_state *s,
133 process_event_t ev, process_data_t data)
135 static struct cfs_dirent dirent;
137 if(state == STATE_OPEN) {
138 if(ev == ctk_signal_widget_activate &&
139 data == (process_data_t)&button) {
140 ctk_dialog_close();
141 state = STATE_CLOSED;
142 process_post(PROCESS_CURRENT(), s->ev, &filename);
143 return 1;
144 } else if(ev == PROCESS_EVENT_CONTINUE &&
145 (process_data_t)s == data) {
146 if(cfs_readdir(&dir, &dirent) == 0 &&
147 dirfileptr < MAX_NUMFILES) {
148 strncpy(&files[dirfileptr * FILES_WIDTH],
149 dirent.name, FILES_WIDTH);
150 CTK_WIDGET_REDRAW(&fileslabel);
151 ++dirfileptr;
152 process_post(PROCESS_CURRENT(), PROCESS_EVENT_CONTINUE, s);
153 } else {
154 fileptr = 0;
155 cfs_closedir(&dir);
157 return 1;
158 } else if(ev == ctk_signal_keypress) {
159 if((ctk_arch_key_t)data == CH_CURS_UP) {
160 clearptr();
161 if(fileptr > 0) {
162 --fileptr;
164 showptr();
165 return 1;
166 } else if((ctk_arch_key_t)data == CH_CURS_DOWN) {
167 clearptr();
168 if(fileptr < FILES_HEIGHT - 1) {
169 ++fileptr;
171 showptr();
172 return 1;
176 return 0;
178 /*---------------------------------------------------------------------------*/