Contiki 3.x
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  */
34 
35 #include "contiki.h"
36 #include "ctk/ctk-filedialog.h"
37 #include "ctk/ctk.h"
38 #include "cfs/cfs.h"
39 
40 #include <string.h>
41 
42 #define MAX_NUMFILES 40
43 #define FILES_WIDTH 17
44 #if FILES_CONF_HEIGHT
45 #define FILES_HEIGHT FILES_CONF_HEIGHT
46 #else
47 #define FILES_HEIGHT 14
48 #endif
49 
50 static struct ctk_window dialog;
51 static char leftptr[FILES_HEIGHT];
52 static struct ctk_label leftptrlabel =
53  {CTK_LABEL(0, 1, 1, FILES_HEIGHT, leftptr)};
54 
55 static char files[FILES_WIDTH * MAX_NUMFILES];
56 static struct ctk_label fileslabel =
57  {CTK_LABEL(1, 1,
58  FILES_WIDTH, FILES_HEIGHT, files)};
59 
60 static char rightptr[FILES_HEIGHT];
61 static struct ctk_label rightptrlabel =
62  {CTK_LABEL(1 + FILES_WIDTH, 1, 1, FILES_HEIGHT, rightptr)};
63 
64 static char filename[FILES_WIDTH + 1];
65 static struct ctk_textentry filenameentry =
66  {CTK_TEXTENTRY(1, 2 + FILES_HEIGHT, FILES_WIDTH, 1, filename,
67  FILES_WIDTH)};
68 
69 static struct ctk_button button;
70 
71 #define STATE_CLOSED 0
72 #define STATE_OPEN 1
73 static char state = STATE_CLOSED;
74 static unsigned char fileptr, dirfileptr;
75 static struct cfs_dir dir;
76 /*---------------------------------------------------------------------------*/
77 static void
78 clearptr(void)
79 {
80  leftptr[fileptr] = ' ';
81  rightptr[fileptr] = ' ';
82 }
83 /*---------------------------------------------------------------------------*/
84 static void
85 showptr(void)
86 {
87  leftptr[fileptr] = '>';
88  rightptr[fileptr] = '<';
89 
90  strncpy(filename,
91  &files[fileptr * FILES_WIDTH],
92  FILES_WIDTH);
93 
94  CTK_WIDGET_REDRAW(&filenameentry);
95  CTK_WIDGET_REDRAW(&leftptrlabel);
96  CTK_WIDGET_REDRAW(&rightptrlabel);
97 }
98 /*---------------------------------------------------------------------------*/
99 void
100 ctk_filedialog_init(CC_REGISTER_ARG struct ctk_filedialog_state *s)
101 {
102  state = STATE_CLOSED;
103 }
104 /*---------------------------------------------------------------------------*/
105 void
106 ctk_filedialog_open(CC_REGISTER_ARG struct ctk_filedialog_state *s,
107  const char *buttontext, process_event_t event)
108 {
109  ctk_dialog_new(&dialog, 20, 5 + FILES_HEIGHT);
110  CTK_WIDGET_ADD(&dialog, &leftptrlabel);
111  CTK_WIDGET_ADD(&dialog, &fileslabel);
112  CTK_WIDGET_ADD(&dialog, &rightptrlabel);
113  CTK_WIDGET_ADD(&dialog, &filenameentry);
114  CTK_BUTTON_NEW(&button, 1, 4 + FILES_HEIGHT, strlen(buttontext), (char *)buttontext);
115  CTK_WIDGET_ADD(&dialog, &button);
116  ctk_dialog_open(&dialog);
117  state = STATE_OPEN;
118  memset(filename, 0, sizeof(filename));
119  memset(leftptr, ' ', sizeof(leftptr));
120  memset(rightptr, ' ', sizeof(rightptr));
121  memset(files, 0, sizeof(files));
122 
123  fileptr = 0;
124  dirfileptr = 0;
125  showptr();
126  cfs_opendir(&dir, ".");
127  process_post(PROCESS_CURRENT(), PROCESS_EVENT_CONTINUE, s);
128 }
129 /*---------------------------------------------------------------------------*/
130 char
131 ctk_filedialog_eventhandler(struct ctk_filedialog_state *s,
132  process_event_t ev, process_data_t data)
133 {
134  static struct cfs_dirent dirent;
135 
136  if(state == STATE_OPEN) {
137  if(ev == ctk_signal_widget_activate &&
138  data == (process_data_t)&button) {
139  ctk_dialog_close();
140  state = STATE_CLOSED;
141  process_post(PROCESS_CURRENT(), s->ev, &filename);
142  return 1;
143  } else if(ev == PROCESS_EVENT_CONTINUE &&
144  (process_data_t)s == data) {
145  if(cfs_readdir(&dir, &dirent) == 0 &&
146  dirfileptr < MAX_NUMFILES) {
147  strncpy(&files[dirfileptr * FILES_WIDTH],
148  dirent.name, FILES_WIDTH);
149  CTK_WIDGET_REDRAW(&fileslabel);
150  ++dirfileptr;
151  process_post(PROCESS_CURRENT(), PROCESS_EVENT_CONTINUE, s);
152  } else {
153  fileptr = 0;
154  cfs_closedir(&dir);
155  }
156  return 1;
157  } else if(ev == ctk_signal_keypress) {
158  if((ctk_arch_key_t)data == CH_CURS_UP) {
159  clearptr();
160  if(fileptr > 0) {
161  --fileptr;
162  }
163  showptr();
164  return 1;
165  } else if((ctk_arch_key_t)data == CH_CURS_DOWN) {
166  clearptr();
167  if(fileptr < FILES_HEIGHT - 1) {
168  ++fileptr;
169  }
170  showptr();
171  return 1;
172  }
173  }
174  }
175  return 0;
176 }
177 /*---------------------------------------------------------------------------*/
#define PROCESS_CURRENT()
Get a pointer to the currently running process.
Definition: process.h:402
#define CTK_WIDGET_ADD(win, widg)
Add a widget to a window.
Definition: ctk.h:752
char ctk_arch_key_t
The keyboard character type of the system.
Definition: ctk-conio.h:40
#define CTK_TEXTENTRY(x, y, w, h, text, len)
Instantiating macro for the ctk_textentry widget.
Definition: ctk.h:275
int cfs_readdir(struct cfs_dir *dir, struct cfs_dirent *record)
Read a directory entry.
Definition: cfs-coffee.c:1264
int process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.
Definition: process.c:322
#define CTK_LABEL(x, y, w, h, text)
Instantiating macro for the ctk_label widget.
Definition: ctk.h:171
process_event_t ctk_signal_keypress
Emitted for every key being pressed.
Definition: ctk.c:126
void cfs_closedir(struct cfs_dir *dir)
Close a directory opened with cfs_opendir().
Definition: cfs-coffee.c:1290
process_event_t ctk_signal_widget_activate
Emitted when a widget is activated (pressed).
Definition: ctk.c:126
#define CC_REGISTER_ARG
Configure if the C compiler supports the &quot;register&quot; keyword for function arguments.
Definition: cc.h:57
int cfs_opendir(struct cfs_dir *dir, const char *name)
Open a directory for reading directory entries.
Definition: cfs-coffee.c:1253
Representation of a CTK window.
Definition: ctk.h:506
#define CTK_WIDGET_REDRAW(widg)
Add a widget to the redraw queue.
Definition: ctk.h:771
CTK header file.
CFS header file.