blob: 41e887f8cc81065c0a76a96c5c9f5094174a0804 [file] [log] [blame]
adamdunkelsa2f3c422004-09-12 20:24:53 +00001/*
2 * Copyright (c) 2004, Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
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.
16 *
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.
28 *
29 * This file is part of the Contiki operating system.
30 *
31 * Author: Adam Dunkels <adam@sics.se>
32 *
oliverschmidt58d4eb22005-05-08 10:39:08 +000033 * $Id: ctk-filedialog.c,v 1.5 2005/05/08 10:40:10 oliverschmidt Exp $
adamdunkelsa2f3c422004-09-12 20:24:53 +000034 */
adamdunkels539f13c2004-08-09 21:48:03 +000035
36#include "contiki.h"
37#include "ctk-filedialog.h"
adamdunkelscd78ed92004-09-01 18:13:03 +000038#include "ctk.h"
39#include "cfs.h"
adamdunkels539f13c2004-08-09 21:48:03 +000040
41#include <string.h>
42
43#define MAX_NUMFILES 40
44#define FILES_WIDTH 17
45#define FILES_HEIGHT 14
46
47static struct ctk_window dialog;
48static char leftptr[FILES_HEIGHT];
49static struct ctk_label leftptrlabel =
50 {CTK_LABEL(0, 1, 1, FILES_HEIGHT, leftptr)};
51
52static char files[FILES_WIDTH * MAX_NUMFILES];
53static struct ctk_label fileslabel =
54 {CTK_LABEL(1, 1,
55 FILES_WIDTH, FILES_HEIGHT, files)};
56
57static char rightptr[FILES_HEIGHT];
58static struct ctk_label rightptrlabel =
59 {CTK_LABEL(1 + FILES_WIDTH, 1, 1, FILES_HEIGHT, rightptr)};
60
61static char filename[FILES_WIDTH + 1];
62static struct ctk_textentry filenameentry =
63 {CTK_TEXTENTRY(1, 2 + FILES_HEIGHT, FILES_WIDTH, 1, filename,
64 FILES_WIDTH)};
65
66static struct ctk_button button;
67
68#define STATE_CLOSED 0
69#define STATE_OPEN 1
70static char state = STATE_CLOSED;
71static unsigned char fileptr, dirfileptr;
72static struct cfs_dir dir;
73/*---------------------------------------------------------------------------*/
74static void
75clearptr(void)
76{
77 leftptr[fileptr] = ' ';
78 rightptr[fileptr] = ' ';
79}
80/*---------------------------------------------------------------------------*/
81static void
82showptr(void)
83{
84 leftptr[fileptr] = '>';
85 rightptr[fileptr] = '<';
86
87 strncpy(filename,
88 &files[fileptr * FILES_WIDTH],
89 FILES_WIDTH);
90
91 CTK_WIDGET_REDRAW(&filenameentry);
92 CTK_WIDGET_REDRAW(&leftptrlabel);
93 CTK_WIDGET_REDRAW(&rightptrlabel);
94}
95/*---------------------------------------------------------------------------*/
96void
97ctk_filedialog_init(register struct ctk_filedialog_state *s)
98{
99 state = STATE_CLOSED;
100}
101/*---------------------------------------------------------------------------*/
102void
103ctk_filedialog_open(register struct ctk_filedialog_state *s,
104 const char *buttontext, ek_event_t event)
105{
106 ctk_dialog_new(&dialog, 20, 5 + FILES_HEIGHT);
107 CTK_WIDGET_ADD(&dialog, &leftptrlabel);
108 CTK_WIDGET_ADD(&dialog, &fileslabel);
109 CTK_WIDGET_ADD(&dialog, &rightptrlabel);
110 CTK_WIDGET_ADD(&dialog, &filenameentry);
111 CTK_BUTTON_NEW(&button, 1, 4 + FILES_HEIGHT, strlen(buttontext), (char *)buttontext);
112 CTK_WIDGET_ADD(&dialog, &button);
113 ctk_dialog_open(&dialog);
114 state = STATE_OPEN;
115 memset(filename, 0, sizeof(filename));
116 memset(leftptr, ' ', sizeof(leftptr));
117 memset(rightptr, ' ', sizeof(rightptr));
oliverschmidt72da8302005-04-28 21:16:37 +0000118 memset(files, 0, sizeof(files));
adamdunkels539f13c2004-08-09 21:48:03 +0000119
120 fileptr = 0;
121 dirfileptr = 0;
122 showptr();
oliverschmidt58d4eb22005-05-08 10:39:08 +0000123 cfs_opendir(&dir, ".");
adamdunkels539f13c2004-08-09 21:48:03 +0000124 ek_post(EK_PROC_ID(EK_CURRENT()), EK_EVENT_CONTINUE, s);
125}
126/*---------------------------------------------------------------------------*/
127char
128ctk_filedialog_eventhandler(struct ctk_filedialog_state *s,
129 ek_event_t ev, ek_data_t data)
130{
131 static struct cfs_dirent dirent;
132
133 if(state == STATE_OPEN) {
134 if(ev == ctk_signal_widget_activate &&
135 data == (ek_data_t)&button) {
136 ctk_dialog_close();
137 state = STATE_CLOSED;
138 ek_post(EK_PROC_ID(EK_CURRENT()), s->ev, &filename);
139 return 1;
140 } else if(ev == EK_EVENT_CONTINUE &&
141 (ek_data_t)s == data) {
142 if(cfs_readdir(&dir, &dirent) == 0 &&
143 dirfileptr < MAX_NUMFILES) {
144 strncpy(&files[dirfileptr * FILES_WIDTH],
145 dirent.name, FILES_WIDTH);
146 CTK_WIDGET_REDRAW(&fileslabel);
147 ++dirfileptr;
148 ek_post(EK_PROC_ID(EK_CURRENT()), EK_EVENT_CONTINUE, s);
149 } else {
150 fileptr = 0;
151 cfs_closedir(&dir);
152 }
153 return 1;
154 } else if(ev == ctk_signal_keypress) {
155 if((ctk_arch_key_t)data == CH_CURS_UP) {
156 clearptr();
157 if(fileptr > 0) {
158 --fileptr;
159 }
160 showptr();
161 return 1;
162 } else if((ctk_arch_key_t)data == CH_CURS_DOWN) {
163 clearptr();
164 if(fileptr < FILES_HEIGHT - 1) {
165 ++fileptr;
166 }
167 showptr();
168 return 1;
169 }
170 }
171 }
172 return 0;
173}
174/*---------------------------------------------------------------------------*/