ctk-filedialog.c

00001 /*
00002  * Copyright (c) 2004, Swedish Institute of Computer Science.
00003  * All rights reserved. 
00004  *
00005  * Redistribution and use in source and binary forms, with or without 
00006  * modification, are permitted provided that the following conditions 
00007  * are met: 
00008  * 1. Redistributions of source code must retain the above copyright 
00009  *    notice, this list of conditions and the following disclaimer. 
00010  * 2. Redistributions in binary form must reproduce the above copyright 
00011  *    notice, this list of conditions and the following disclaimer in the 
00012  *    documentation and/or other materials provided with the distribution. 
00013  * 3. Neither the name of the Institute nor the names of its contributors 
00014  *    may be used to endorse or promote products derived from this software 
00015  *    without specific prior written permission. 
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
00027  * SUCH DAMAGE. 
00028  *
00029  * This file is part of the Contiki operating system.
00030  * 
00031  * Author: Adam Dunkels <adam@sics.se>
00032  *
00033  * $Id: ctk-filedialog.c,v 1.2 2007/08/30 14:39:18 matsutsuka Exp $
00034  */
00035 
00036 #include "contiki.h"
00037 #include "lib/ctk-filedialog.h"
00038 #include "ctk/ctk.h"
00039 #include "cfs/cfs.h"
00040 
00041 #include <string.h>
00042 
00043 #define MAX_NUMFILES 40
00044 #define FILES_WIDTH 17
00045 #if FILES_CONF_HEIGHT
00046 #define FILES_HEIGHT FILES_CONF_HEIGHT
00047 #else
00048 #define FILES_HEIGHT 14
00049 #endif
00050 
00051 static struct ctk_window dialog;
00052 static char leftptr[FILES_HEIGHT];
00053 static struct ctk_label leftptrlabel =
00054   {CTK_LABEL(0, 1, 1, FILES_HEIGHT, leftptr)};
00055 
00056 static char files[FILES_WIDTH * MAX_NUMFILES];
00057 static struct ctk_label fileslabel =
00058   {CTK_LABEL(1, 1,
00059              FILES_WIDTH, FILES_HEIGHT, files)};
00060 
00061 static char rightptr[FILES_HEIGHT];
00062 static struct ctk_label rightptrlabel =
00063   {CTK_LABEL(1 + FILES_WIDTH, 1, 1, FILES_HEIGHT, rightptr)};
00064 
00065 static char filename[FILES_WIDTH + 1];
00066 static struct ctk_textentry filenameentry =
00067   {CTK_TEXTENTRY(1, 2 + FILES_HEIGHT, FILES_WIDTH, 1, filename,
00068                  FILES_WIDTH)};
00069 
00070 static struct ctk_button button;
00071 
00072 #define STATE_CLOSED 0
00073 #define STATE_OPEN 1
00074 static char state = STATE_CLOSED;
00075 static unsigned char fileptr, dirfileptr;
00076 static struct cfs_dir dir;
00077 /*---------------------------------------------------------------------------*/
00078 static void
00079 clearptr(void)
00080 {
00081   leftptr[fileptr] = ' ';
00082   rightptr[fileptr] = ' '; 
00083 }
00084 /*---------------------------------------------------------------------------*/
00085 static void
00086 showptr(void)
00087 {
00088   leftptr[fileptr] = '>';
00089   rightptr[fileptr] = '<';
00090 
00091   strncpy(filename,
00092           &files[fileptr * FILES_WIDTH],
00093           FILES_WIDTH);
00094   
00095   CTK_WIDGET_REDRAW(&filenameentry);
00096   CTK_WIDGET_REDRAW(&leftptrlabel);
00097   CTK_WIDGET_REDRAW(&rightptrlabel);
00098 }
00099 /*---------------------------------------------------------------------------*/
00100 void
00101 ctk_filedialog_init(CC_REGISTER_ARG struct ctk_filedialog_state *s)
00102 {
00103   state = STATE_CLOSED;
00104 }
00105 /*---------------------------------------------------------------------------*/
00106 void
00107 ctk_filedialog_open(CC_REGISTER_ARG struct ctk_filedialog_state *s,
00108                     const char *buttontext, process_event_t event)
00109 {
00110   ctk_dialog_new(&dialog, 20, 5 + FILES_HEIGHT);
00111   CTK_WIDGET_ADD(&dialog, &leftptrlabel);
00112   CTK_WIDGET_ADD(&dialog, &fileslabel);
00113   CTK_WIDGET_ADD(&dialog, &rightptrlabel);
00114   CTK_WIDGET_ADD(&dialog, &filenameentry);
00115   CTK_BUTTON_NEW(&button, 1, 4 + FILES_HEIGHT, strlen(buttontext), (char *)buttontext);
00116   CTK_WIDGET_ADD(&dialog, &button);
00117   ctk_dialog_open(&dialog);
00118   state = STATE_OPEN;
00119   memset(filename, 0, sizeof(filename));
00120   memset(leftptr, ' ', sizeof(leftptr));
00121   memset(rightptr, ' ', sizeof(rightptr));
00122   memset(files, 0, sizeof(files));
00123   
00124   fileptr = 0;
00125   dirfileptr = 0;
00126   showptr();
00127   cfs_opendir(&dir, ".");
00128   process_post(PROCESS_CURRENT(), PROCESS_EVENT_CONTINUE, s);
00129 }
00130 /*---------------------------------------------------------------------------*/
00131 char
00132 ctk_filedialog_eventhandler(struct ctk_filedialog_state *s,
00133                             process_event_t ev, process_data_t data)
00134 {
00135   static struct cfs_dirent dirent;
00136   
00137   if(state == STATE_OPEN) {
00138     if(ev == ctk_signal_widget_activate &&
00139        data == (process_data_t)&button) {
00140       ctk_dialog_close();
00141       state = STATE_CLOSED;
00142       process_post(PROCESS_CURRENT(), s->ev, &filename);
00143       return 1;
00144     } else if(ev == PROCESS_EVENT_CONTINUE &&
00145               (process_data_t)s == data) {
00146       if(cfs_readdir(&dir, &dirent) == 0 &&
00147          dirfileptr < MAX_NUMFILES) {
00148         strncpy(&files[dirfileptr * FILES_WIDTH],
00149                 dirent.name, FILES_WIDTH);
00150         CTK_WIDGET_REDRAW(&fileslabel);
00151         ++dirfileptr;
00152         process_post(PROCESS_CURRENT(), PROCESS_EVENT_CONTINUE, s);
00153       } else {
00154         fileptr = 0;    
00155         cfs_closedir(&dir);
00156       }
00157       return 1; 
00158     } else if(ev == ctk_signal_keypress) {
00159       if((ctk_arch_key_t)data == CH_CURS_UP) {
00160         clearptr();
00161         if(fileptr > 0) {
00162           --fileptr;
00163         }
00164         showptr();
00165         return 1;
00166       } else if((ctk_arch_key_t)data == CH_CURS_DOWN) {
00167         clearptr();
00168         if(fileptr < FILES_HEIGHT - 1) {
00169           ++fileptr;
00170         }
00171         showptr();
00172         return 1;
00173       }
00174     }
00175   }
00176   return 0;
00177 }
00178 /*---------------------------------------------------------------------------*/

Generated on Mon Apr 11 14:23:28 2011 for Contiki 2.5 by  doxygen 1.6.1