NXWidgets: ckeypad.cxx Source File

NXWidgets 1.19

ckeypad.cxx
Go to the documentation of this file.
1 /****************************************************************************
2 * NxWidgets/libnxwidgets/src/ckeypad.cxx
3 *
4 * Copyright (C) 2012 Gregory Nutt. All rights reserved.
5 * Author: Gregory Nutt <gnutt@nuttx.org>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name NuttX, NxWidgets, nor the names of its contributors
18 * me be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *
34 ****************************************************************************/
35
36 /****************************************************************************
37 * Included Files
38 ****************************************************************************/
39
40 #include <nuttx/config.h>
41
42 #include <stdint.h>
43 #include <stdbool.h>
44
45 #include <nuttx/nx/nx.h>
46 #include <nuttx/nx/nxglib.h>
47
48 #include "cwidgetcontrol.hxx"
49 #include "cwidgetstyle.hxx"
50 #include "ckeypad.hxx"
51
52 /****************************************************************************
53 * Pre-Processor Definitions
54 ****************************************************************************/
55
56 // The geometry of the button array
57
58 #define BUTTONARRAY_NCOLUMNS 4
59 #define BUTTONARRAY_NROWS 7
60
61 /****************************************************************************
62 * Private Data
63 ****************************************************************************/
64
65 using namespace NXWidgets;
66
67 static FAR const char *g_alphaLabels[BUTTONARRAY_NCOLUMNS*BUTTONARRAY_NROWS] = {
68 "=>", "A", "B", "<DEL",
69 "C", "D", "E", "F",
70 "G", "H", "I", "J",
71 "K", "L", "M", "N",
72 "O", "P", "Q", "R",
73 "S", "T", "U", "V",
74 "W", "X", "Y", "Z"
75 };
76
77 static FAR const char *g_numLabels[BUTTONARRAY_NCOLUMNS*BUTTONARRAY_NROWS] = {
78 "=>", "0", "1", "<DEL",
79 "2", "3", "4", "5",
80 "6", "7", "8", "9",
81 "-", "#", "*", "&",
82 "?", "!", "@", "%",
83 ":", ",", ".", "/",
84 "\"", "'", "(", ")"
85 };
86
87 /****************************************************************************
88 * Method Implementations
89 ****************************************************************************/
90
91 /**
92 * Constructor for buttons that display a string.
93 *
94 * @param pWidgetControl The widget control for the display.
95 * @param hNxServer The NX server that will receive the keyboard input
96 * @param x The x coordinate of the keypad, relative to its parent.
97 * @param y The y coordinate of the keypad, relative to its parent.
98 * @param width The width of the keypad
99 * @param height The height of the keypad
100 * @param style The style that the button should use. If this is not
101 * specified, the button will use the global default widget
102 * style.
103 */
104
105 CKeypad::CKeypad(CWidgetControl *pWidgetControl, NXHANDLE hNxServer,
106 nxgl_coord_t x, nxgl_coord_t y,
107 nxgl_coord_t width, nxgl_coord_t height,
108 CWidgetStyle *style)
109 : CButtonArray(pWidgetControl, x, y, 4, 7, width / BUTTONARRAY_NCOLUMNS,
110 height / BUTTONARRAY_NROWS, style)
111 {
112 m_hNxServer = hNxServer;
113
114 // Force the height and width to be an exact multiple of the button height and width
115
116 resize(m_buttonWidth * m_buttonColumns, m_buttonHeight * m_buttonRows);
117
118 // Start in alphabetic mode
119
120 setKeypadMode(false);
121 }
122
123 /**
124 * Catch button clicks.
125 *
126 * @param x The x coordinate of the click.
127 * @param y The y coordinate of the click.
128 */
129
130 void CKeypad::onClick(nxgl_coord_t x, nxgl_coord_t y)
131 {
132 // Perform the redraw
133
134 CButtonArray::onClick(x, y);
135
136 // The button should now be clicked
137
138 int column;
139 int row;
140
141 if (isButtonClicked(column, row))
142 {
143 // The left arrow at (0,0) is a special case
144
145 char ch;
146 if (column == 0 && row == 0)
147 {
148 m_widgetControl->newCursorControlEvent(CURSOR_RIGHT);
149 return;
150 }
151
152 // The backspace at (3,0) is a special case
153
154 else if (column == 3 && row == 0)
155 {
156 ch = KEY_CODE_BACKSPACE;
157 }
158
159 // Otherwise, just grab the character for the arrays
160
161 else
162 {
163 int index = row * BUTTONARRAY_NCOLUMNS + column;
164 FAR const char *ptr;
165 if (m_numeric)
166 {
167 ptr = g_numLabels[index];
168 }
169 else
170 {
171 ptr = g_alphaLabels[index];
172 }
173
174 ch = ptr[0];
175 }
176
177 // Then inject the keyboard input into the NX server. The NX
178 // server will determine which window receives the input.
179 // The ultimate recipient will be the widget in the top window
180 // that has focus.
181
182 (void)nx_kbdchin(m_hNxServer, (uint8_t)ch);
183 }
184 }
185
186 /**
187 * Configure the keypad for the currenly selected display mode.
188 */
189
190 void CKeypad::configureKeypadMode(void)
191 {
192 // Add the labels to each button. Each call to setText would
193 // cause a text change event and, hence, redrawing of the
194
195 FAR const char **ptr = m_numeric ? g_numLabels : g_alphaLabels;
196 for (int j = 0; j < BUTTONARRAY_NROWS; j++)
197 {
198 for (int i = 0; i < BUTTONARRAY_NCOLUMNS; i++)
199 {
200 // setText does this. But each call to setText would
201 // cause a text change even and, hence, redrawing of
202 // the whole keypad. So we access the protected data
203 // directly and redraw only once.
204
205 CNxString string = *ptr++;
206 m_buttonText[j * m_buttonColumns + i] = string;
207 }
208 }
209
210 // Then redraw the display
211
212 onTextChange();
213 }
214
NXWidgets::CButtonArray::m_buttonHeight
nxgl_coord_t m_buttonHeight
Definition: cbuttonarray.hxx:86
NXWidgets::CWidgetStyle
Definition: cwidgetstyle.hxx:125
NXWidgets::CButtonArray::isButtonClicked
virtual bool isButtonClicked(int &column, int &row) const
Definition: cbuttonarray.cxx:167
NXWidgets::CWidgetControl
Definition: cwidgetcontrol.hxx:95
NXWidgets::CButtonArray::m_buttonWidth
nxgl_coord_t m_buttonWidth
Definition: cbuttonarray.hxx:85
NXWidgets::CKeypad::m_numeric
bool m_numeric
Definition: ckeypad.hxx:79
NXWidgets::CKeypad::setKeypadMode
void setKeypadMode(bool numeric)
Definition: ckeypad.hxx:137
NXWidgets::CButtonArray::m_buttonRows
uint8_t m_buttonRows
Definition: cbuttonarray.hxx:81
NXWidgets::CButtonArray::onTextChange
virtual void onTextChange(void)
Definition: cbuttonarray.cxx:629
cwidgetstyle.hxx
cwidgetcontrol.hxx
NXWidgets::CNxWidget::resize
bool resize(nxgl_coord_t width, nxgl_coord_t height)
Definition: cnxwidget.cxx:1079
NXWidgets::CURSOR_RIGHT
Definition: cwidgeteventargs.hxx:118
ckeypad.hxx
NXWidgets::CButtonArray::m_buttonText
CNxString * m_buttonText
Definition: cbuttonarray.hxx:91
NXWidgets::CKeypad::m_hNxServer
NXHANDLE m_hNxServer
Definition: ckeypad.hxx:78
NXWidgets::CNxWidget::m_widgetControl
CWidgetControl * m_widgetControl
Definition: cnxwidget.hxx:171
NXWidgets
Definition: cbgwindow.hxx:65
g_alphaLabels
static FAR const char * g_alphaLabels[BUTTONARRAY_NCOLUMNS *BUTTONARRAY_NROWS]
Definition: ckeypad.cxx:67
NXWidgets::CWidgetControl::newCursorControlEvent
void newCursorControlEvent(ECursorControl cursorControl)
Definition: cwidgetcontrol.cxx:624
NXWidgets::CButtonArray
Definition: cbuttonarray.hxx:77
NXWidgets::CNxString
Definition: cnxstring.hxx:121
NXWidgets::CKeypad::configureKeypadMode
void configureKeypadMode(void)
Definition: ckeypad.cxx:190
g_numLabels
static FAR const char * g_numLabels[BUTTONARRAY_NCOLUMNS *BUTTONARRAY_NROWS]
Definition: ckeypad.cxx:77
NXWidgets::CKeypad::onClick
virtual void onClick(nxgl_coord_t x, nxgl_coord_t y)
Definition: ckeypad.cxx:130
NXWidgets::CKeypad::CKeypad
CKeypad(const CKeypad &keypad)
Definition: ckeypad.hxx:91
NXWidgets::CButtonArray::m_buttonColumns
uint8_t m_buttonColumns
Definition: cbuttonarray.hxx:80
NXWidgets::CButtonArray::onClick
virtual void onClick(nxgl_coord_t x, nxgl_coord_t y)
Definition: cbuttonarray.cxx:550