Debian Codegraph @

common/ui/inputbox.cpp

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "u_mem.h"
25 #include "maths.h"
26 #include "pstypes.h"
27 #include "event.h"
28 #include "gr.h"
29 #include "ui.h"
30 #include "key.h"
31 #include "mouse.h"
32
33 namespace dcx {
34
35 namespace {
36
37 void ui_draw_inputbox(UI_DIALOG &dlg, UI_GADGET_INPUTBOX &inputbox)
38 {
39 #if 0 //ndef OGL
40 if ((inputbox->status==1) || (inputbox->position != inputbox->oldposition))
41 #endif
42 {
43 gr_set_current_canvas(inputbox.canvas);
44 auto &canvas = *grd_curcanv;
45
46 gr_rect(canvas, 0, 0, inputbox.width - 1, inputbox.height - 1, CBLACK);
47
48 const auto &&[w, h] = gr_get_string_size(*canvas.cv_font, inputbox.text.get());
49 if (dlg.keyboard_focus_gadget == &inputbox)
50 {
51 if (inputbox.first_time)
52 {
53 gr_set_fontcolor(canvas, CBLACK, -1);
54 gr_rect(canvas, 2, 2, 2 + w, 2 + h, CRED);
55 }
56 else
57 gr_set_fontcolor(canvas, CRED, -1);
58 }
59 else
60 gr_set_fontcolor(canvas, CWHITE, -1);
61
62 gr_string(canvas, *canvas.cv_font, 2, 2, inputbox.text.get(), w, h);
63
64 if (dlg.keyboard_focus_gadget == &inputbox && !inputbox.first_time)
65 {
66 const uint8_t cred = CRED;
67 Vline(canvas, 2, inputbox.height - 3, 3 + w, cred);
68 Vline(canvas, 2, inputbox.height - 3, 4 + w, cred);
69 }
70 }
71 }
72
73 }
74
75 std::unique_ptr<UI_GADGET_INPUTBOX> ui_add_gadget_inputbox(UI_DIALOG &dlg, const short x, const short y, const uint_fast32_t length_of_initial_text, const uint_fast32_t maximum_allowed_text_length, const char *const initial_text)
76 {
77 const auto aw = grd_curcanv->cv_font->ft_w;
78 const auto h = gr_get_string_size(*grd_curcanv->cv_font, nullptr).height;
79 auto inputbox = ui_gadget_add<UI_GADGET_INPUTBOX>(dlg, x, y, x + aw * maximum_allowed_text_length - 1, y + h - 1 + 4);
80 inputbox->text = std::make_unique<char[]>(length_of_initial_text + 1);
81 const auto allocated_text = inputbox->text.get();
82 allocated_text[length_of_initial_text] = 0;
83 memcpy(allocated_text, initial_text, length_of_initial_text);
84 inputbox->position = length_of_initial_text;
85 inputbox->oldposition = inputbox->position;
86 inputbox->width = aw * maximum_allowed_text_length;
87 inputbox->height = h+4;
88 inputbox->length = length_of_initial_text;
89 inputbox->pressed = 0;
90 inputbox->first_time = 1;
91 return inputbox;
92 }
93
94 window_event_result UI_GADGET_INPUTBOX::event_handler(UI_DIALOG &dlg, const d_event &event)
95 {
96 const auto keypress = (event.type == event_type::key_command)
97 ? event_key_get(event)
98 : 0u;
99
100 oldposition = position;
101 pressed=0;
102
103 window_event_result rval = window_event_result::ignored;
104 if (dlg.keyboard_focus_gadget == this)
105 {
106 switch( keypress )
107 {
108 case 0:
109 break;
110 case (KEY_LEFT):
111 case (KEY_BACKSP):
112 if (position > 0)
113 position--;
114 text[position] = 0;
115 if (first_time) first_time = 0;
116 rval = window_event_result::handled;
117 break;
118 case (KEY_ENTER):
119 pressed=1;
120 if (first_time) first_time = 0;
121 rval = window_event_result::handled;
122 break;
123 default:
124 {
125 const uint8_t ascii = key_ascii();
126 if ((ascii < 255 ) && (position < length-2))
127 {
128 if (first_time) {
129 first_time = 0;
130 position = 0;
131 }
132 text[position++] = ascii;
133 text[position] = 0;
134 rval = window_event_result::handled;
135 }
136 break;
137 }
138 }
139 } else {
140 first_time = 1;
141 }
142 if (pressed)
143 {
144 rval = ui_gadget_send_event(dlg, event_type::ui_gadget_pressed, *this);
145 if (rval == window_event_result::ignored)
146 rval = window_event_result::handled;
147 }
148 if (event.type == event_type::window_draw)
149 ui_draw_inputbox(dlg, *this);
150
151 return rval;
152 }
153
154 void ui_inputbox_set_text(UI_GADGET_INPUTBOX *inputbox, const char *text)
155 {
156 auto ltext = std::min(static_cast<std::size_t>(inputbox->length), strlen(text));
157 memcpy(inputbox->text.get(), text, ltext);
158 inputbox->text[ltext] = 0;
159 inputbox->position = ltext;
160 inputbox->oldposition = inputbox->position;
161 inputbox->first_time = 1; // select all
162 }
163
164 }