Contiki 2.5
ctk-filedialog.c
1 /*
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  *
33  * $Id: ctk-filedialog.c,v 1.2 2007/08/30 14:39:18 matsutsuka Exp $
34  */
35 
36 #include "contiki.h"
37 #include "lib/ctk-filedialog.h"
38 #include "ctk/ctk.h"
39 #include "cfs/cfs.h"
40 
41 #include <string.h>
42 
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
50 
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)};
55 
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)};
60 
61 static char rightptr[FILES_HEIGHT];
62 static struct ctk_label rightptrlabel =
63  {CTK_LABEL(1 + FILES_WIDTH, 1, 1, FILES_HEIGHT, rightptr)};
64 
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)};
69 
70 static struct ctk_button button;
71 
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)
80 {
81  leftptr[fileptr] = ' ';
82  rightptr[fileptr] = ' ';
83 }
84 /*---------------------------------------------------------------------------*/
85 static void
86 showptr(void)
87 {
88  leftptr[fileptr] = '>';
89  rightptr[fileptr] = '<';
90 
91  strncpy(filename,
92  &files[fileptr * FILES_WIDTH],
93  FILES_WIDTH);
94 
95  CTK_WIDGET_REDRAW(&filenameentry);
96  CTK_WIDGET_REDRAW(&leftptrlabel);
97  CTK_WIDGET_REDRAW(&rightptrlabel);
98 }
99 /*---------------------------------------------------------------------------*/
100 void
101 ctk_filedialog_init(CC_REGISTER_ARG struct ctk_filedialog_state *s)
102 {
103  state = STATE_CLOSED;
104 }
105 /*---------------------------------------------------------------------------*/
106 void
107 ctk_filedialog_open(CC_REGISTER_ARG struct ctk_filedialog_state *s,
108  const char *buttontext, process_event_t event)
109 {
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));
123 
124  fileptr = 0;
125  dirfileptr = 0;
126  showptr();
127  cfs_opendir(&dir, ".");
128  process_post(PROCESS_CURRENT(), PROCESS_EVENT_CONTINUE, s);
129 }
130 /*---------------------------------------------------------------------------*/
131 char
132 ctk_filedialog_eventhandler(struct ctk_filedialog_state *s,
133  process_event_t ev, process_data_t data)
134 {
135  static struct cfs_dirent dirent;
136 
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);
156  }
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;
163  }
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;
170  }
171  showptr();
172  return 1;
173  }
174  }
175  }
176  return 0;
177 }
178 /*---------------------------------------------------------------------------*/