blob: 3a345ab552e7b9b01cddc3aae2e3d66cea664daf [file] [log] [blame]
adamdunkels1e45c6d2003-09-02 21:47:27 +00001/**
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
adamdunkelsb8f64972003-08-11 22:22:44 +000015/*
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.
adamdunkels1e45c6d2003-09-02 21:47:27 +000028 * 3. The name of the author may not be used to endorse or promote
adamdunkelsb8f64972003-08-11 22:22:44 +000029 * 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 *
adamdunkelsd58321e2004-09-01 18:26:55 +000046 * $Id: ctk-textedit.c,v 1.4 2004/09/01 18:26:55 adamdunkels Exp $
adamdunkelsb8f64972003-08-11 22:22:44 +000047 *
48 */
49
50
51#include "ctk-textedit.h"
52
adamdunkels077ad942004-07-04 15:12:56 +000053#include <string.h>
54
adamdunkelsb8f64972003-08-11 22:22:44 +000055/*-----------------------------------------------------------------------------------*/
adamdunkelsd58321e2004-09-01 18:26:55 +000056void
57ctk_textedit_init(struct ctk_textedit *t)
58{
59 t->xpos = t->ypos = 0;
60}
61/*-----------------------------------------------------------------------------------*/
adamdunkels1e45c6d2003-09-02 21:47:27 +000062/**
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/*-----------------------------------------------------------------------------------*/
adamdunkelsb8f64972003-08-11 22:22:44 +000069void
70ctk_textedit_add(struct ctk_window *w,
71 struct ctk_textedit *t)
72{
adamdunkelsd58321e2004-09-01 18:26:55 +000073 CTK_WIDGET_SET_FLAG(t, CTK_WIDGET_FLAG_MONOSPACE);
adamdunkelsb8f64972003-08-11 22:22:44 +000074 CTK_WIDGET_ADD(w, t);
75}
76/*-----------------------------------------------------------------------------------*/
adamdunkels1e45c6d2003-09-02 21:47:27 +000077/**
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/*-----------------------------------------------------------------------------------*/
adamdunkelsb8f64972003-08-11 22:22:44 +000088void
adamdunkels077ad942004-07-04 15:12:56 +000089ctk_textedit_eventhandler(struct ctk_textedit *t,
90 ek_event_t s,
91 ek_data_t data)
adamdunkelsb8f64972003-08-11 22:22:44 +000092{
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:
adamdunkelsd58321e2004-09-01 18:26:55 +0000102 if(t->ypos < t->label.h - 1) {
adamdunkelsb8f64972003-08-11 22:22:44 +0000103 ++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:
adamdunkelsd58321e2004-09-01 18:26:55 +0000112 len = strlen(&t->label.text[t->ypos * t->label.w]);
113 if(t->xpos < len) {
114 /* if(t->xpos < t->label.w) {*/
adamdunkelsb8f64972003-08-11 22:22:44 +0000115 ++t->xpos;
adamdunkelsd58321e2004-09-01 18:26:55 +0000116 } else {
117 t->xpos = len;
adamdunkelsb8f64972003-08-11 22:22:44 +0000118 }
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;
adamdunkelsd58321e2004-09-01 18:26:55 +0000132 if(t->ypos < t->label.h - 1) {
adamdunkelsb8f64972003-08-11 22:22:44 +0000133 ++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/*-----------------------------------------------------------------------------------*/