Debian Codegraph @

ui/UIEdit.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/ui/UIEdit.cpp $
21 * $Revision: 33 $
22 * $Date: 10/21/99 3:30p $
23 * $Author: Kevin $
24 *
25 * Edit control
26 *
27 * $Log: /DescentIII/Main/ui/UIEdit.cpp $
28 *
29 * 33 10/21/99 3:30p Kevin
30 * mac merge
31 *
32 * 32 5/17/99 5:19p Samir
33 * added ability for edit box to autoselect when hitting the max number of
34 * characters typable.
35 *
36 * 31 5/06/99 1:24a Samir
37 * upped editbox maxlength to 512
38 *
39 * 30 5/06/99 1:20a Samir
40 * allow editboxes with max buffer length of 280
41 *
42 * 29 4/28/99 1:54a Samir
43 * visual tweaks to text
44 *
45 * 28 4/21/99 12:42p Samir
46 * change some communication betwen UIEdit and child classes.
47 *
48 * 27 4/20/99 11:46a Samir
49 * numerous ui 'feel' fixes.
50 *
51 * 26 4/14/99 1:52a Jeff
52 * fixed case mismatched #includes
53 *
54 * 25 2/28/99 3:25a Samir
55 * added flag to input only numbers.
56 *
57 * 24 2/21/99 6:36p Samir
58 * focusing changes and key input changes to ui.,
59 *
60 * 23 10/19/98 12:18p Samir
61 * made tabbing between edit controls possible.
62 *
63 * 22 10/17/98 11:26a Samir
64 * fixed some edit box scrolling issues.
65 *
66 * 21 10/13/98 8:23p Samir
67 * communication between gadget and window and edit system to work
68 * together.
69 *
70 * 20 10/06/98 7:30p Samir
71 * activate listbox when created.
72 *
73 * 19 9/30/98 4:33p Samir
74 * disabled gadgets display dim text.
75 *
76 * 18 9/24/98 1:13p Samir
77 * edit box a bit better.
78 *
79 * 17 9/14/98 11:22a Samir
80 * enhanced editboxes
81 *
82 * 16 8/26/98 12:25p Samir
83 * fixed the whole editbox, keyboard access mess.
84 *
85 * 15 8/25/98 5:36p Samir
86 * added call to lock key focus in Activate too.
87 *
88 * 14 8/25/98 5:27p Samir
89 * new focusing rules for keyboard.
90 *
91 * 13 6/22/98 7:31p Samir
92 * added UIEdit::Activate, which activates an edit box manually.
93 *
94 * 12 6/22/98 3:46p Samir
95 * fixed bug in UIEdit password system.
96 *
97 * 11 6/22/98 2:52p Samir
98 * call Onselect for UIEdit boxes if edit is pressed.
99 *
100 * 10 6/22/98 2:15p Samir
101 * implemented border flag.
102 *
103 * 9 6/15/98 5:27p Kevin
104 * Fixed bug in UIEdit::GetText where m_TextBuf was NULL
105 *
106 * 8 5/25/98 8:17p Samir
107 * fixed some UI bugs with edit boxes.
108 *
109 * 7 4/13/98 7:02p Samir
110 * made some things protected.
111 *
112 * 6 3/10/98 11:03a Jeff
113 * changed strcpy to strncpy in GetText
114 *
115 * 5 1/30/98 7:05p Samir
116 * Each gadget now has their own coordinate system.
117 *
118 * 4 1/18/98 4:23p Samir
119 * Moved object destruction code to a virtual OnDestroy function for
120 * gadgets. Must destroy window too.
121 *
122 * 3 1/13/98 4:27p Samir
123 * Keyboard timing delay stored UIGadget. Moved from UIEdit.
124 *
125 * 2 1/05/98 4:36p Samir
126 * Moved centering and fittext control flags to UIGadget class.
127 *
128 * 1 1/02/98 12:49p Samir
129 * Initial revision.
130 *
131 *
132 * $NoKeywords: $
133 */
134
135 #include <cstdio>
136 #include <cstring>
137 #include <cstdlib>
138
139 #include "UIlib.h"
140 #include "grtext.h"
141 #include "Macros.h"
142 #include "mem.h"
143
144
145 static UIEdit *UI_current_editbox = NULL;
146
147 // ----------------------------------------------------------------------------
148 // UIEdit
149 // A single line edit control.
150
151 UIEdit::UIEdit() {
152 m_TextBuf = NULL;
153 m_BufSize = 0;
154 m_FontHandle = -1;
155 }
156
157 UIEdit::~UIEdit() {}
158
159 void UIEdit::Create(UIWindow *parent, int id, int x, int y, int w, int h, int flags) {
160 m_BufSize = 32;
161 m_TextBuf = (char *)mem_malloc(m_BufSize + 1);
162 m_TextBuf[0] = 0;
163 m_CursorPosX = 0;
164 m_Color = GR_WHITE;
165 m_Active = false;
166 m_StartPos = -1;
167 m_CurPos = 0;
168 UIGadget::Create(parent, id, x, y, w, h, flags);
169 SetTextBounds(2, m_W - 2, 0);
170 m_ScrollThresh = 0;
171 m_UpdateCount = false;
172 // UIEdit::Activate();
173 }
174
175 void UIEdit::SetTextBounds(int x1, int x2, int yoff) {
176 m_X1b = x1;
177 m_X2b = x2;
178 m_Yb = yoff;
179 }
180
181 void UIEdit::SetColor(ddgr_color col) { m_Color = col; }
182
183 #define EDIT_BUFLEN_MAX 1025 // DAJ utb 512
184
185 // setings
186 // sets the length of the input buffer.
187 void UIEdit::SetBufferLen(int length) {
188 char *newbuf;
189 ASSERT(length < EDIT_BUFLEN_MAX); // there are several places in the code that depend on something like this.
190
191 newbuf = (char *)mem_malloc(length + 1);
192 strncpy(newbuf, m_TextBuf, length);
193 newbuf[length] = 0;
194 if (m_TextBuf) {
195 mem_free(m_TextBuf);
196 }
197 m_TextBuf = newbuf;
198 m_BufSize = length;
199 m_UpdateCount = false;
200 }
201
202 // sets the text buffer.
203 void UIEdit::SetText(const char *text) {
204 ASSERT(strlen(text) <= (unsigned)m_BufSize);
205
206 if (CHECK_FLAG(m_Flags, UIED_NUMBERS)) {
207 int num = atoi(text);
208 sprintf(m_TextBuf, "%d", num);
209 } else {
210 strcpy(m_TextBuf, text);
211 }
212
213 m_CurPos = strlen(m_TextBuf);
214 m_StartPos = -1;
215 m_UpdateCount = false;
216 }
217
218 // retrieve text in edit control.
219 void UIEdit::GetText(char *buffer, int len) {
220 // ASSERT(strlen(m_TextBuf) < (unsigned)len);
221 if (!m_TextBuf) {
222 buffer[0] = '\0';
223 return;
224 }
225 strncpy(buffer, m_TextBuf, len - 1);
226 buffer[len - 1] = 0;
227 }
228
229 // sets font of control (default is window's font.)
230 void UIEdit::SetFont(int font) { m_FontHandle = font; }
231
232 void UIEdit::Activate() {
233 if (UI_current_editbox) {
234 UI_current_editbox->Deactivate();
235 }
236
237 m_Wnd->HoldHotkeys();
238 m_Active = true;
239 UI_current_editbox = this;
240 m_UpdateCount = false;
241
242 // mprintf(0, "activating edit box\n");
243 }
244
245 // deactivates edit box (use if you know what you're doing.)
246 void UIEdit::Deactivate() {
247 // mprintf(0, "deactivating edit box\n");
248
249 m_Active = false;
250 UI_current_editbox = NULL;
251 m_Wnd->ResumeHotkeys();
252 }
253
254 // Overrides
255
256 void UIEdit::OnDestroy() {
257 UIGadget::OnDestroy();
258
259 if (m_TextBuf) {
260 mem_free(m_TextBuf);
261 }
262
263 // if editbox is being destroyed and is the current editbox, reset to NULL.
264 if (UI_current_editbox == this)
265 UI_current_editbox = NULL;
266 }
267
268 // override: behavior when gadget loses input focus.
269 void UIEdit::OnLostFocus() {
270 // mprintf(0, "Edit control lost focus.\n");
271 UIEdit::Deactivate();
272 }
273
274 // override: behavior when gadget gains input focus.
275 void UIEdit::OnGainFocus() {
276 // mprintf(0, "Edit control gained focus.\n");
277 }
278
279 // behavior when gadget is being drawn.
280 void UIEdit::OnDraw() {
281 if (m_Flags & UIF_BORDER) {
282 ui_DrawBox(GR_RGB(128, 128, 128), m_X1b - 1, 0, m_X2b + 1, m_H);
283 }
284
285 DrawText();
286 }
287
288 void UIEdit::DrawText() {
289 char buffer[EDIT_BUFLEN_MAX];
290 int str_w, i, cursor_w;
291
292 if (m_FontHandle > -1)
293 ui_DrawSetFont(m_FontHandle);
294
295 // I need an extra character.
296 ASSERT(strlen(m_TextBuf) < sizeof(buffer));
297
298 if (m_Flags & UIED_PASSWORD) {
299 memset(buffer, '*', strlen(m_TextBuf));
300 buffer[strlen(m_TextBuf)] = 0;
301 } else {
302 strcpy(buffer, m_TextBuf);
303 }
304
305 if (m_Active || HasFocus())
306 ui_SetCharAlpha(255);
307 else
308 ui_SetCharAlpha(164);
309
310 if (IsDisabled())
311 ui_SetCharAlpha(96);
312
313 // we must set StartPos if StartPos is -1
314 if (m_StartPos < 0) {
315 m_StartPos = 0;
316 cursor_w = ui_GetTextWidth("_");
317 str_w = ui_GetTextWidth(&buffer[m_StartPos]);
318 while ((m_X1b + str_w) > (m_X2b - cursor_w)) {
319 m_StartPos++;
320 str_w = ui_GetTextWidth(&buffer[m_StartPos]);
321 }
322 }
323
324 // determine how much of string to draw at one time.
325 i = 0;
326
327 cursor_w = ui_GetTextWidth("_") * 2;
328 str_w = ui_GetTextWidth(&buffer[m_StartPos]);
329 while ((m_X1b + str_w) > (m_X2b - cursor_w) && ((int)strlen(buffer) > m_StartPos)) {
330 buffer[strlen(buffer) - 1] = 0;
331 str_w = ui_GetTextWidth(&buffer[m_StartPos]);
332 i++;
333 }
334
335 // if i is > 0, then the string displayed overlaps the boundaries of the listbox
336 m_ScrollThresh = i ? (strlen(buffer)) : 0;
337
338 if (m_ScrollThresh && m_UpdateCount) {
339 m_StartPos++;
340 m_UpdateCount = false;
341 }
342
343 // if cursor position is outside viewing region, we must adjust the viewing region.
344 // i = strlen(buffer);
345 // while (m_CurPos > i)
346 // m_CurPos--;
347
348 ui_DrawSetTextType(0);
349 ui_DrawString(m_Color, m_X1b, m_Yb + (m_H / 2 - ui_GetFontHeight() / 2), &buffer[m_StartPos]);
350
351 if (m_Active) {
352 buffer[m_CurPos] = 0;
353 str_w = ui_GetTextWidth(buffer + m_StartPos);
354 if (((int)UI_TIME()) % 2)
355 ui_DrawString(m_Color, m_X1b + str_w + 1, m_Yb + (m_H / 2 - ui_GetFontHeight() / 2), "_");
356 }
357 }
358
359 // behavior when key is pressed.
360 void UIEdit::OnKeyDown(int key) {
361 int ascii = grfont_KeyToAscii(m_FontHandle, key);
362 int len = strlen(m_TextBuf);
363 int i;
364
365 if (!m_Active) {
366 UIEdit::Activate();
367 }
368
369 if (key == KEY_ESC || key == KEY_ENTER) {
370 UIEdit::Deactivate();
371 if (key == KEY_ENTER) {
372 UIGadget::OnSelect(); // do this to enforce pressing enter ONLY causes OnSelect to work.
373 }
374 } else if (key == KEY_DELETE) {
375 for (i = m_CurPos; i < len; i++)
376 m_TextBuf[i] = m_TextBuf[i + 1];
377 } else {
378 if (m_CurPos < m_BufSize) {
379 if (CHECK_FLAG(m_Flags, UIED_NUMBERS)) {
380 if (ascii < '0' || ascii > '9') {
381 goto skip_add_to_buffer;
382 }
383 }
384
385 if (ascii >= 32 && ascii < 127 && len < m_BufSize) {
386 // insert character
387 for (i = len; i >= m_CurPos; i--)
388 m_TextBuf[i + 1] = m_TextBuf[i];
389 // adjust scroll region if cursor will go out of visible region.
390 mprintf(0, "scroll=%d, curpos=%d\n", m_ScrollThresh, m_CurPos);
391 // if (m_ScrollThresh<=m_CurPos && m_ScrollThresh)
392 // m_StartPos++;
393 m_TextBuf[m_CurPos++] = (char)ascii;
394 m_UpdateCount = true;
395 if (m_CurPos == m_BufSize && CHECK_FLAG(m_Flags, UIED_AUTOSELECT)) {
396 UIEdit::Deactivate();
397 UIGadget::OnSelect();
398 return;
399 }
400 }
401 }
402
403 skip_add_to_buffer:
404 if (m_CurPos < len) {
405 if (key == KEY_RIGHT) {
406 if (m_ScrollThresh <= m_CurPos && m_ScrollThresh) {
407 m_StartPos++;
408 }
409 // Update_count = true;
410 m_CurPos++;
411 }
412 }
413
414 if (m_CurPos > 0) {
415 if (key == KEY_LEFT) {
416 m_CurPos--;
417 } else if (key == KEY_BACKSP) {
418 // delete character
419 for (i = m_CurPos - 1; i < len; i++)
420 m_TextBuf[i] = m_TextBuf[i + 1];
421
422 m_CurPos--;
423 }
424 if (m_CurPos < m_StartPos)
425 m_StartPos = m_CurPos;
426 }
427 }
428 }
429
430 // behavior when key is released.
431 void UIEdit::OnKeyUp(int key) {}
432
433 // behavior when mouse button is pressed.
434 void UIEdit::OnMouseBtnDown(int btn) {
435 if (btn == UILMSEBTN) {
436 if (!m_Active) {
437 UIEdit::Activate();
438 } else if (!PT_IN_GADGET(m_Wnd, this, UI_input.mx, UI_input.my) && m_Wnd->GetFocus()) {
439 // click outside control and in another control, then deactivate, this means we've accepted the input.
440 UIEdit::Deactivate();
441 }
442 }
443 }
444
445 // behavior when mouse button is released.
446 void UIEdit::OnMouseBtnUp(int btn) {}
447
448 void UIEdit::OnSelect() {
449 // except do this to release key focus on this edit box.
450 UIEdit::Deactivate();
451 UIGadget::OnSelect();
452 }