00001 /** 00002 * \file 00003 * An experimental CTK text edit widget. 00004 * \author Adam Dunkels <adam@dunkels.com> 00005 * 00006 * This module contains an experimental CTK widget which is 00007 * implemented in the application process rather than in the CTK 00008 * process. The widget is instantiated in a similar fashion as other 00009 * CTK widgets, but is different from other widgets in that it 00010 * requires a signal handler function to be called by the process 00011 * signal handler function. 00012 * 00013 */ 00014 00015 /* 00016 * Copyright (c) 2003, Adam Dunkels. 00017 * All rights reserved. 00018 * 00019 * Redistribution and use in source and binary forms, with or without 00020 * modification, are permitted provided that the following conditions 00021 * are met: 00022 * 1. Redistributions of source code must retain the above copyright 00023 * notice, this list of conditions and the following disclaimer. 00024 * 2. Redistributions in binary form must reproduce the above 00025 * copyright notice, this list of conditions and the following 00026 * disclaimer in the documentation and/or other materials provided 00027 * with the distribution. 00028 * 3. The name of the author may not be used to endorse or promote 00029 * products derived from this software without specific prior 00030 * written permission. 00031 * 00032 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 00033 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00034 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00035 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 00036 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00037 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 00038 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00039 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 00040 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00041 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00042 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00043 * 00044 * This file is part of the Contiki desktop environment 00045 * 00046 * $Id: ctk-textedit.c,v 1.1 2006/06/17 22:41:17 adamdunkels Exp $ 00047 * 00048 */ 00049 00050 00051 #include "ctk-textedit.h" 00052 00053 #include <string.h> 00054 00055 /*-----------------------------------------------------------------------------------*/ 00056 void 00057 ctk_textedit_init(struct ctk_textedit *t) 00058 { 00059 t->xpos = t->ypos = 0; 00060 } 00061 /*-----------------------------------------------------------------------------------*/ 00062 /** 00063 * Add a CTK textedit widget to a window. 00064 * 00065 * \param w A pointer to the window to which the entry is to be added. 00066 * \param t A pointer to the CTK textentry structure. 00067 */ 00068 /*-----------------------------------------------------------------------------------*/ 00069 void 00070 ctk_textedit_add(struct ctk_window *w, 00071 struct ctk_textedit *t) 00072 { 00073 CTK_WIDGET_SET_FLAG(t, CTK_WIDGET_FLAG_MONOSPACE); 00074 CTK_WIDGET_ADD(w, t); 00075 } 00076 /*-----------------------------------------------------------------------------------*/ 00077 /** 00078 * The CTK textedit signal handler. 00079 * 00080 * This function must be called as part of the normal signal handler 00081 * of the process that contains the CTK textentry structure. 00082 * 00083 * \param t A pointer to the CTK textentry structure. 00084 * \param s The signal number. 00085 * \param data The signal data. 00086 */ 00087 /*-----------------------------------------------------------------------------------*/ 00088 void 00089 ctk_textedit_eventhandler(struct ctk_textedit *t, 00090 process_event_t s, 00091 process_data_t data) 00092 { 00093 char *textptr, *textptr2; 00094 unsigned char len; 00095 00096 if(s == ctk_signal_keypress) { 00097 CTK_WIDGET_FOCUS(t->label.window, &t->label); 00098 textptr = &(t->label.text[t->ypos * t->label.w + t->xpos]); 00099 *textptr &= 0x7f; 00100 switch((ctk_arch_key_t)data) { 00101 case CH_CURS_DOWN: 00102 if(t->ypos < t->label.h - 1) { 00103 ++t->ypos; 00104 } 00105 break; 00106 case CH_CURS_UP: 00107 if(t->ypos > 0) { 00108 --t->ypos; 00109 } 00110 break; 00111 case CH_CURS_RIGHT: 00112 len = strlen(&t->label.text[t->ypos * t->label.w]); 00113 if(t->xpos < len) { 00114 /* if(t->xpos < t->label.w) {*/ 00115 ++t->xpos; 00116 } else { 00117 t->xpos = len; 00118 } 00119 break; 00120 case CH_CURS_LEFT: 00121 if(t->xpos > 0) { 00122 --t->xpos; 00123 } else { 00124 if(t->ypos > 0) { 00125 --t->ypos; 00126 t->xpos = t->label.w - 1; 00127 } 00128 } 00129 break; 00130 case CH_ENTER: 00131 t->xpos = 0; 00132 if(t->ypos < t->label.h - 1) { 00133 ++t->ypos; 00134 } 00135 break; 00136 case CH_DEL: 00137 len = t->label.w - t->xpos; 00138 if(t->xpos > 0 && len > 0) { 00139 strncpy(textptr - 1, textptr, 00140 len); 00141 *(textptr + len - 1) = 0; 00142 --t->xpos; 00143 } 00144 break; 00145 default: 00146 len = t->label.w - t->xpos; 00147 if(len > 0) { 00148 textptr2 = textptr + len - 1; 00149 while(textptr2 + 1 > textptr) { 00150 *(textptr2 + 1) = *textptr2; 00151 --textptr2; 00152 } 00153 00154 *textptr = (char)data; 00155 ++t->xpos; 00156 if(t->xpos == t->label.w) { 00157 t->xpos = 0; 00158 if(t->ypos < t->label.h - 1) { 00159 ++t->ypos; 00160 } 00161 } 00162 } 00163 break; 00164 } 00165 textptr = &(t->label.text[t->ypos * t->label.w + t->xpos]); 00166 *textptr |= 0x80; 00167 CTK_WIDGET_REDRAW(&t->label); 00168 } else if(s == ctk_signal_widget_activate && 00169 data == (process_data_t)t) { 00170 textptr = &(t->label.text[t->ypos * t->label.w + t->xpos]); 00171 *textptr &= 0x7f; 00172 t->xpos = 0; 00173 if(t->ypos < t->label.h - 1) { 00174 ++t->ypos; 00175 } 00176 textptr = &(t->label.text[t->ypos * t->label.w + t->xpos]); 00177 *textptr |= 0x80; 00178 CTK_WIDGET_REDRAW(&t->label); 00179 } 00180 } 00181 /*-----------------------------------------------------------------------------------*/