2 * Copyright (c) 2004, Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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 $
37 #include "lib/ctk-filedialog.h"
43 #define MAX_NUMFILES 40
44 #define FILES_WIDTH 17
46 #define FILES_HEIGHT FILES_CONF_HEIGHT
48 #define FILES_HEIGHT 14
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
=
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
,
70 static struct ctk_button button
;
72 #define STATE_CLOSED 0
74 static char state
= STATE_CLOSED
;
75 static unsigned char fileptr
, dirfileptr
;
76 static struct cfs_dir dir
;
77 /*---------------------------------------------------------------------------*/
81 leftptr
[fileptr
] = ' ';
82 rightptr
[fileptr
] = ' ';
84 /*---------------------------------------------------------------------------*/
88 leftptr
[fileptr
] = '>';
89 rightptr
[fileptr
] = '<';
92 &files
[fileptr
* FILES_WIDTH
],
95 CTK_WIDGET_REDRAW(&filenameentry
);
96 CTK_WIDGET_REDRAW(&leftptrlabel
);
97 CTK_WIDGET_REDRAW(&rightptrlabel
);
99 /*---------------------------------------------------------------------------*/
101 ctk_filedialog_init(CC_REGISTER_ARG
struct ctk_filedialog_state
*s
)
103 state
= STATE_CLOSED
;
105 /*---------------------------------------------------------------------------*/
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
);
119 memset(filename
, 0, sizeof(filename
));
120 memset(leftptr
, ' ', sizeof(leftptr
));
121 memset(rightptr
, ' ', sizeof(rightptr
));
122 memset(files
, 0, sizeof(files
));
127 cfs_opendir(&dir
, ".");
128 process_post(PROCESS_CURRENT(), PROCESS_EVENT_CONTINUE
, s
);
130 /*---------------------------------------------------------------------------*/
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
) {
141 state
= STATE_CLOSED
;
142 process_post(PROCESS_CURRENT(), s
->ev
, &filename
);
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
);
152 process_post(PROCESS_CURRENT(), PROCESS_EVENT_CONTINUE
, s
);
158 } else if(ev
== ctk_signal_keypress
) {
159 if((ctk_arch_key_t
)data
== CH_CURS_UP
) {
166 } else if((ctk_arch_key_t
)data
== CH_CURS_DOWN
) {
168 if(fileptr
< FILES_HEIGHT
- 1) {
178 /*---------------------------------------------------------------------------*/