adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file |
| 3 | * An experimental CTK text edit widget. |
| 4 | * \author Adam Dunkels <adam@dunkels.com> |
| 5 | * |
| 6 | * This module contains an experimental CTK widget which is |
| 7 | * implemented in the application process rather than in the CTK |
| 8 | * process. The widget is instantiated in a similar fashion as other |
| 9 | * CTK widgets, but is different from other widgets in that it |
| 10 | * requires a signal handler function to be called by the process |
| 11 | * signal handler function. |
| 12 | * |
| 13 | */ |
| 14 | |
adamdunkels | b8f6497 | 2003-08-11 22:22:44 +0000 | [diff] [blame] | 15 | /* |
| 16 | * Copyright (c) 2003, Adam Dunkels. |
| 17 | * All rights reserved. |
| 18 | * |
| 19 | * Redistribution and use in source and binary forms, with or without |
| 20 | * modification, are permitted provided that the following conditions |
| 21 | * are met: |
| 22 | * 1. Redistributions of source code must retain the above copyright |
| 23 | * notice, this list of conditions and the following disclaimer. |
| 24 | * 2. Redistributions in binary form must reproduce the above |
| 25 | * copyright notice, this list of conditions and the following |
| 26 | * disclaimer in the documentation and/or other materials provided |
| 27 | * with the distribution. |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 28 | * 3. The name of the author may not be used to endorse or promote |
adamdunkels | b8f6497 | 2003-08-11 22:22:44 +0000 | [diff] [blame] | 29 | * products derived from this software without specific prior |
| 30 | * written permission. |
| 31 | * |
| 32 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| 33 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 34 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 35 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 36 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 37 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 38 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 39 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 40 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 41 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 42 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 43 | * |
| 44 | * This file is part of the Contiki desktop environment |
| 45 | * |
adamdunkels | d58321e | 2004-09-01 18:26:55 +0000 | [diff] [blame] | 46 | * $Id: ctk-textedit.c,v 1.4 2004/09/01 18:26:55 adamdunkels Exp $ |
adamdunkels | b8f6497 | 2003-08-11 22:22:44 +0000 | [diff] [blame] | 47 | * |
| 48 | */ |
| 49 | |
| 50 | |
| 51 | #include "ctk-textedit.h" |
| 52 | |
adamdunkels | 077ad94 | 2004-07-04 15:12:56 +0000 | [diff] [blame] | 53 | #include <string.h> |
| 54 | |
adamdunkels | b8f6497 | 2003-08-11 22:22:44 +0000 | [diff] [blame] | 55 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | d58321e | 2004-09-01 18:26:55 +0000 | [diff] [blame] | 56 | void |
| 57 | ctk_textedit_init(struct ctk_textedit *t) |
| 58 | { |
| 59 | t->xpos = t->ypos = 0; |
| 60 | } |
| 61 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 62 | /** |
| 63 | * Add a CTK textedit widget to a window. |
| 64 | * |
| 65 | * \param w A pointer to the window to which the entry is to be added. |
| 66 | * \param t A pointer to the CTK textentry structure. |
| 67 | */ |
| 68 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | b8f6497 | 2003-08-11 22:22:44 +0000 | [diff] [blame] | 69 | void |
| 70 | ctk_textedit_add(struct ctk_window *w, |
| 71 | struct ctk_textedit *t) |
| 72 | { |
adamdunkels | d58321e | 2004-09-01 18:26:55 +0000 | [diff] [blame] | 73 | CTK_WIDGET_SET_FLAG(t, CTK_WIDGET_FLAG_MONOSPACE); |
adamdunkels | b8f6497 | 2003-08-11 22:22:44 +0000 | [diff] [blame] | 74 | CTK_WIDGET_ADD(w, t); |
| 75 | } |
| 76 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 1e45c6d | 2003-09-02 21:47:27 +0000 | [diff] [blame] | 77 | /** |
| 78 | * The CTK textedit signal handler. |
| 79 | * |
| 80 | * This function must be called as part of the normal signal handler |
| 81 | * of the process that contains the CTK textentry structure. |
| 82 | * |
| 83 | * \param t A pointer to the CTK textentry structure. |
| 84 | * \param s The signal number. |
| 85 | * \param data The signal data. |
| 86 | */ |
| 87 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | b8f6497 | 2003-08-11 22:22:44 +0000 | [diff] [blame] | 88 | void |
adamdunkels | 077ad94 | 2004-07-04 15:12:56 +0000 | [diff] [blame] | 89 | ctk_textedit_eventhandler(struct ctk_textedit *t, |
| 90 | ek_event_t s, |
| 91 | ek_data_t data) |
adamdunkels | b8f6497 | 2003-08-11 22:22:44 +0000 | [diff] [blame] | 92 | { |
| 93 | char *textptr, *textptr2; |
| 94 | unsigned char len; |
| 95 | |
| 96 | if(s == ctk_signal_keypress) { |
| 97 | CTK_WIDGET_FOCUS(t->label.window, &t->label); |
| 98 | textptr = &(t->label.text[t->ypos * t->label.w + t->xpos]); |
| 99 | *textptr &= 0x7f; |
| 100 | switch((ctk_arch_key_t)data) { |
| 101 | case CH_CURS_DOWN: |
adamdunkels | d58321e | 2004-09-01 18:26:55 +0000 | [diff] [blame] | 102 | if(t->ypos < t->label.h - 1) { |
adamdunkels | b8f6497 | 2003-08-11 22:22:44 +0000 | [diff] [blame] | 103 | ++t->ypos; |
| 104 | } |
| 105 | break; |
| 106 | case CH_CURS_UP: |
| 107 | if(t->ypos > 0) { |
| 108 | --t->ypos; |
| 109 | } |
| 110 | break; |
| 111 | case CH_CURS_RIGHT: |
adamdunkels | d58321e | 2004-09-01 18:26:55 +0000 | [diff] [blame] | 112 | len = strlen(&t->label.text[t->ypos * t->label.w]); |
| 113 | if(t->xpos < len) { |
| 114 | /* if(t->xpos < t->label.w) {*/ |
adamdunkels | b8f6497 | 2003-08-11 22:22:44 +0000 | [diff] [blame] | 115 | ++t->xpos; |
adamdunkels | d58321e | 2004-09-01 18:26:55 +0000 | [diff] [blame] | 116 | } else { |
| 117 | t->xpos = len; |
adamdunkels | b8f6497 | 2003-08-11 22:22:44 +0000 | [diff] [blame] | 118 | } |
| 119 | break; |
| 120 | case CH_CURS_LEFT: |
| 121 | if(t->xpos > 0) { |
| 122 | --t->xpos; |
| 123 | } else { |
| 124 | if(t->ypos > 0) { |
| 125 | --t->ypos; |
| 126 | t->xpos = t->label.w - 1; |
| 127 | } |
| 128 | } |
| 129 | break; |
| 130 | case CH_ENTER: |
| 131 | t->xpos = 0; |
adamdunkels | d58321e | 2004-09-01 18:26:55 +0000 | [diff] [blame] | 132 | if(t->ypos < t->label.h - 1) { |
adamdunkels | b8f6497 | 2003-08-11 22:22:44 +0000 | [diff] [blame] | 133 | ++t->ypos; |
| 134 | } |
| 135 | break; |
| 136 | case CH_DEL: |
| 137 | len = t->label.w - t->xpos; |
| 138 | if(t->xpos > 0 && len > 0) { |
| 139 | strncpy(textptr - 1, textptr, |
| 140 | len); |
| 141 | *(textptr + len - 1) = 0; |
| 142 | --t->xpos; |
| 143 | } |
| 144 | break; |
| 145 | default: |
| 146 | len = t->label.w - t->xpos; |
| 147 | if(len > 0) { |
| 148 | textptr2 = textptr + len - 1; |
| 149 | while(textptr2 + 1 > textptr) { |
| 150 | *(textptr2 + 1) = *textptr2; |
| 151 | --textptr2; |
| 152 | } |
| 153 | |
| 154 | *textptr = (char)data; |
| 155 | ++t->xpos; |
| 156 | if(t->xpos == t->label.w) { |
| 157 | t->xpos = 0; |
| 158 | if(t->ypos < t->label.h - 1) { |
| 159 | ++t->ypos; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | break; |
| 164 | } |
| 165 | textptr = &(t->label.text[t->ypos * t->label.w + t->xpos]); |
| 166 | *textptr |= 0x80; |
| 167 | CTK_WIDGET_REDRAW(&t->label); |
| 168 | } else if(s == ctk_signal_widget_activate && |
| 169 | data == (ek_data_t)t) { |
| 170 | textptr = &(t->label.text[t->ypos * t->label.w + t->xpos]); |
| 171 | *textptr &= 0x7f; |
| 172 | t->xpos = 0; |
| 173 | if(t->ypos < t->label.h - 1) { |
| 174 | ++t->ypos; |
| 175 | } |
| 176 | textptr = &(t->label.text[t->ypos * t->label.w + t->xpos]); |
| 177 | *textptr |= 0x80; |
| 178 | CTK_WIDGET_REDRAW(&t->label); |
| 179 | } |
| 180 | } |
| 181 | /*-----------------------------------------------------------------------------------*/ |