1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// (C) 2025 - Enzo Lombardi
//! InputLine view - single-line text input with editing and history support.
use crate::core::geometry::Rect;
use crate::core::event::{Event, EventType, KB_ENTER, KB_BACKSPACE, KB_LEFT, KB_RIGHT, KB_HOME, KB_END, KB_DEL};
use crate::core::draw::DrawBuffer;
use crate::core::palette::colors;
use crate::core::clipboard;
use crate::core::state::StateFlags;
use crate::terminal::Terminal;
use super::view::{View, write_line_to_terminal};
use super::validator::ValidatorRef;
use std::rc::Rc;
use std::cell::RefCell;
// Control key codes
const KB_CTRL_A: u16 = 0x0001; // Ctrl+A - Select All
const KB_CTRL_C: u16 = 0x0003; // Ctrl+C - Copy
const KB_CTRL_V: u16 = 0x0016; // Ctrl+V - Paste
const KB_CTRL_X: u16 = 0x0018; // Ctrl+X - Cut
pub struct InputLine {
bounds: Rect,
data: Rc<RefCell<String>>,
cursor_pos: usize,
max_length: usize,
sel_start: usize, // Selection start position
sel_end: usize, // Selection end position
first_pos: usize, // First visible character position for horizontal scrolling
validator: Option<ValidatorRef>, // Optional validator for input validation
state: StateFlags, // View state flags (including SF_FOCUSED)
}
impl InputLine {
pub fn new(bounds: Rect, max_length: usize, data: Rc<RefCell<String>>) -> Self {
let cursor_pos = data.borrow().len();
Self {
bounds,
data,
cursor_pos,
max_length,
sel_start: 0,
sel_end: 0,
first_pos: 0,
validator: None,
state: 0,
}
}
/// Create an InputLine with a validator
/// Matches Borland's TInputLine with validator attachment pattern
pub fn with_validator(bounds: Rect, max_length: usize, data: Rc<RefCell<String>>, validator: ValidatorRef) -> Self {
let mut input_line = Self::new(bounds, max_length, data);
input_line.validator = Some(validator);
input_line
}
/// Set the validator for this InputLine
pub fn set_validator(&mut self, validator: ValidatorRef) {
self.validator = Some(validator);
}
/// Validate the current input
/// Returns true if valid or no validator is set
pub fn validate(&self) -> bool {
if let Some(ref validator) = self.validator {
validator.borrow().valid(&self.data.borrow())
} else {
true
}
}
pub fn set_text(&mut self, text: String) {
*self.data.borrow_mut() = text;
self.cursor_pos = self.data.borrow().len();
self.sel_start = 0;
self.sel_end = 0;
self.first_pos = 0;
}
pub fn get_text(&self) -> String {
self.data.borrow().clone()
}
// set_focused() removed - use set_focus() from View trait instead
/// Select all text
pub fn select_all(&mut self) {
let len = self.data.borrow().len();
self.sel_start = 0;
self.sel_end = len;
self.cursor_pos = len;
}
/// Check if there's an active selection
pub fn has_selection(&self) -> bool {
self.sel_start != self.sel_end
}
/// Get the selected text
pub fn get_selection(&self) -> Option<String> {
if !self.has_selection() {
return None;
}
let text = self.data.borrow();
let start = self.sel_start.min(self.sel_end);
let end = self.sel_start.max(self.sel_end);
Some(text[start..end].to_string())
}
/// Delete the current selection
fn delete_selection(&mut self) {
if !self.has_selection() {
return;
}
let start = self.sel_start.min(self.sel_end);
let end = self.sel_start.max(self.sel_end);
let mut text = self.data.borrow_mut();
text.replace_range(start..end, "");
drop(text);
self.cursor_pos = start;
self.sel_start = 0;
self.sel_end = 0;
}
/// Ensure cursor is visible by adjusting first_pos
fn make_cursor_visible(&mut self) {
let width = self.bounds.width() as usize;
// If cursor is before the visible area
if self.cursor_pos < self.first_pos {
self.first_pos = self.cursor_pos;
}
// If cursor is after the visible area
else if self.cursor_pos >= self.first_pos + width {
self.first_pos = self.cursor_pos - width + 1;
}
}
}
impl View for InputLine {
fn bounds(&self) -> Rect {
self.bounds
}
fn set_bounds(&mut self, bounds: Rect) {
self.bounds = bounds;
}
fn draw(&mut self, terminal: &mut Terminal) {
let width = self.bounds.width() as usize;
let mut buf = DrawBuffer::new(width);
let attr = if self.is_focused() {
colors::INPUT_FOCUSED
} else {
colors::INPUT_NORMAL
};
buf.move_char(0, ' ', attr, width);
// Get text and calculate visible portion
let text = self.data.borrow();
let text_len = text.len();
// Calculate visible range
let visible_start = self.first_pos;
let visible_end = (visible_start + width).min(text_len);
// Draw text
if visible_start < text_len {
let visible_text = &text[visible_start..visible_end];
// If there's a selection, draw it with selection color
if self.has_selection() {
let sel_start = self.sel_start.min(self.sel_end);
let sel_end = self.sel_start.max(self.sel_end);
// Draw characters one by one to handle selection highlighting
for (i, ch) in visible_text.chars().enumerate() {
let pos = visible_start + i;
let char_attr = if pos >= sel_start && pos < sel_end {
colors::INPUT_SELECTED
} else {
attr
};
buf.move_char(i, ch, char_attr, 1);
}
} else {
buf.move_str(0, visible_text, attr);
}
// Show left arrow if text is scrolled
if self.first_pos > 0 {
buf.move_char(0, '<', attr, 1);
}
// Show right arrow if there's more text beyond the visible area
if visible_end < text_len {
buf.move_char(width - 1, '>', attr, 1);
}
}
write_line_to_terminal(terminal, self.bounds.a.x, self.bounds.a.y, &buf);
}
fn handle_event(&mut self, event: &mut Event) {
// Handle broadcasts even when not focused
if event.what == EventType::Broadcast {
use crate::core::command::CM_FILE_FOCUSED;
// Handle cmFileFocused broadcast from FileDialog
// Matches Borland: TFileInputLine::handleEvent() (tfileinp.cc:35-45)
if event.command == CM_FILE_FOCUSED {
// Only update display if user isn't currently typing
// Matches Borland: if( !(state & sfSelected) )
if !self.is_focused() {
// The data has already been updated by FileDialog
// Just need to update our cursor position and clear selection
self.cursor_pos = self.data.borrow().len();
self.sel_start = 0;
self.sel_end = 0;
self.first_pos = 0;
// Note: Event is NOT cleared - other views may need it
}
}
return;
}
if !self.is_focused() {
return;
}
if event.what == EventType::Keyboard {
match event.key_code {
KB_BACKSPACE => {
if self.has_selection() {
self.delete_selection();
self.make_cursor_visible();
event.clear();
} else if self.cursor_pos > 0 {
{
let mut text = self.data.borrow_mut();
text.remove(self.cursor_pos - 1);
}
self.cursor_pos -= 1;
self.make_cursor_visible();
event.clear();
}
}
KB_DEL => {
if self.has_selection() {
self.delete_selection();
self.make_cursor_visible();
event.clear();
} else if self.cursor_pos < self.data.borrow().len() {
let mut text = self.data.borrow_mut();
text.remove(self.cursor_pos);
event.clear();
}
}
KB_LEFT => {
if self.cursor_pos > 0 {
self.cursor_pos -= 1;
self.sel_start = 0;
self.sel_end = 0;
self.make_cursor_visible();
event.clear();
}
}
KB_RIGHT => {
if self.cursor_pos < self.data.borrow().len() {
self.cursor_pos += 1;
self.sel_start = 0;
self.sel_end = 0;
self.make_cursor_visible();
event.clear();
}
}
KB_HOME => {
self.cursor_pos = 0;
self.sel_start = 0;
self.sel_end = 0;
self.make_cursor_visible();
event.clear();
}
KB_END => {
self.cursor_pos = self.data.borrow().len();
self.sel_start = 0;
self.sel_end = 0;
self.make_cursor_visible();
event.clear();
}
KB_ENTER => {
// Don't handle Enter - let dialog handle it for default button
// Just pass through without clearing
}
KB_CTRL_A => {
self.select_all();
event.clear();
}
KB_CTRL_C => {
// Copy to clipboard
if let Some(selection) = self.get_selection() {
clipboard::set_clipboard(&selection);
}
event.clear();
}
KB_CTRL_X => {
// Cut to clipboard
if let Some(selection) = self.get_selection() {
clipboard::set_clipboard(&selection);
self.delete_selection();
self.make_cursor_visible();
}
event.clear();
}
KB_CTRL_V => {
// Paste from clipboard
let clipboard_text = clipboard::get_clipboard();
if !clipboard_text.is_empty() {
// Delete selection if any
if self.has_selection() {
self.delete_selection();
}
// Insert clipboard text at cursor position
{
let mut text = self.data.borrow_mut();
let remaining_space = self.max_length.saturating_sub(text.len());
let insert_text = if clipboard_text.len() <= remaining_space {
clipboard_text.as_str()
} else {
&clipboard_text[..remaining_space]
};
text.insert_str(self.cursor_pos, insert_text);
self.cursor_pos += insert_text.len();
}
self.make_cursor_visible();
}
event.clear();
}
// Regular character input
key_code => {
if (32..127).contains(&key_code) {
// Delete selection if any
if self.has_selection() {
self.delete_selection();
}
let text_len = self.data.borrow().len();
if text_len < self.max_length {
let ch = key_code as u8 as char;
// Check validator before inserting
// Matches Borland's TValidator::IsValidInput() pattern
if let Some(ref validator) = self.validator {
// Create test string with new character
let mut test_text = self.data.borrow().clone();
test_text.insert(self.cursor_pos, ch);
// Check if valid input during typing
if !validator.borrow().is_valid_input(&test_text, true) {
// Invalid character - reject it
event.clear();
return;
}
}
// Character is valid, insert it
{
let mut text = self.data.borrow_mut();
text.insert(self.cursor_pos, ch);
}
self.cursor_pos += 1;
self.make_cursor_visible();
event.clear();
}
}
}
}
}
}
fn can_focus(&self) -> bool {
true
}
// set_focus() now uses default implementation from View trait
// which sets/clears SF_FOCUSED flag
fn state(&self) -> StateFlags {
self.state
}
fn set_state(&mut self, state: StateFlags) {
self.state = state;
}
fn update_cursor(&self, terminal: &mut Terminal) {
if self.is_focused() {
// Calculate cursor position on screen
let cursor_x = self.bounds.a.x as usize + (self.cursor_pos - self.first_pos);
let cursor_y = self.bounds.a.y;
// Show cursor at the position
let _ = terminal.show_cursor(cursor_x as u16, cursor_y as u16);
}
// Note: cursor is already hidden by Group if not focused
}
}