turbo-vision 1.0.7

A Rust implementation of the classic Borland Turbo Vision text-mode UI framework
Documentation
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// (C) 2025 - Enzo Lombardi

//! EditWindow view - window container for editor with title bar showing filename.
// EditWindow - Window wrapper for Editor
//
// Matches Borland: TEditWindow (teditor.h)
//
// A simple window that contains an Editor with scrollbars and indicator.
// Provides a ready-to-use editor window for text editing.

use crate::core::geometry::{Point, Rect};
use crate::core::event::{Event, EventType};
use crate::core::state::StateFlags;
use crate::terminal::Terminal;
use super::window::Window;
use super::editor::Editor;
use super::scrollbar::ScrollBar;
use super::indicator::Indicator;
use super::view::View;
use std::rc::Rc;
use std::cell::RefCell;

/// Wrapper that allows ScrollBar to be a child view
struct SharedScrollBar(Rc<RefCell<ScrollBar>>);

impl View for SharedScrollBar {
    fn bounds(&self) -> Rect {
        self.0.borrow().bounds()
    }

    fn set_bounds(&mut self, bounds: Rect) {
        self.0.borrow_mut().set_bounds(bounds);
    }

    fn draw(&mut self, terminal: &mut Terminal) {
        self.0.borrow_mut().draw(terminal);
    }

    fn handle_event(&mut self, event: &mut Event) {
        self.0.borrow_mut().handle_event(event);
    }

    fn get_palette(&self) -> Option<crate::core::palette::Palette> {
        self.0.borrow().get_palette()
    }

    fn get_owner_type(&self) -> super::view::OwnerType {
        self.0.borrow().get_owner_type()
    }

    fn set_owner_type(&mut self, owner_type: super::view::OwnerType) {
        self.0.borrow_mut().set_owner_type(owner_type);
    }
}

/// Wrapper that allows Indicator to be a child view
struct SharedIndicator(Rc<RefCell<Indicator>>);

impl View for SharedIndicator {
    fn bounds(&self) -> Rect {
        self.0.borrow().bounds()
    }

    fn set_bounds(&mut self, bounds: Rect) {
        self.0.borrow_mut().set_bounds(bounds);
    }

    fn draw(&mut self, terminal: &mut Terminal) {
        self.0.borrow_mut().draw(terminal);
    }

    fn handle_event(&mut self, _event: &mut Event) {
        // Indicator doesn't handle events
    }

    fn get_palette(&self) -> Option<crate::core::palette::Palette> {
        self.0.borrow().get_palette()
    }

    fn get_owner_type(&self) -> super::view::OwnerType {
        self.0.borrow().get_owner_type()
    }

    fn set_owner_type(&mut self, owner_type: super::view::OwnerType) {
        self.0.borrow_mut().set_owner_type(owner_type);
    }
}

/// Wrapper that allows Editor to be shared between window and EditWindow
struct SharedEditor(Rc<RefCell<Editor>>);

impl View for SharedEditor {
    fn bounds(&self) -> Rect {
        self.0.borrow().bounds()
    }

    fn set_bounds(&mut self, bounds: Rect) {
        self.0.borrow_mut().set_bounds(bounds);
    }

    fn draw(&mut self, terminal: &mut Terminal) {
        self.0.borrow_mut().draw(terminal);
    }

    fn handle_event(&mut self, event: &mut Event) {
        self.0.borrow_mut().handle_event(event);
    }

    fn can_focus(&self) -> bool {
        self.0.borrow().can_focus()
    }

    fn set_focus(&mut self, focused: bool) {
        self.0.borrow_mut().set_focus(focused);
    }

    fn is_focused(&self) -> bool {
        self.0.borrow().is_focused()
    }

    fn options(&self) -> u16 {
        self.0.borrow().options()
    }

    fn set_options(&mut self, options: u16) {
        self.0.borrow_mut().set_options(options);
    }

    fn state(&self) -> StateFlags {
        self.0.borrow().state()
    }

    fn set_state(&mut self, state: StateFlags) {
        self.0.borrow_mut().set_state(state);
    }

    fn update_cursor(&self, terminal: &mut Terminal) {
        self.0.borrow().update_cursor(terminal);
    }

    fn get_palette(&self) -> Option<crate::core::palette::Palette> {
        self.0.borrow().get_palette()
    }

    fn get_owner_type(&self) -> super::view::OwnerType {
        self.0.borrow().get_owner_type()
    }

    fn set_owner_type(&mut self, owner_type: super::view::OwnerType) {
        self.0.borrow_mut().set_owner_type(owner_type);
    }
}

/// EditWindow - Window containing an Editor
///
/// Matches Borland: TEditWindow (parent-child hierarchy)
/// The Editor is inserted as a child of the Window, matching Borland's structure.
pub struct EditWindow {
    window: Window,
    editor: Rc<RefCell<Editor>>,  // Shared reference for API access
    #[allow(dead_code)] // Used for initialization, stored for lifetime management
    h_scrollbar: Rc<RefCell<ScrollBar>>,
    #[allow(dead_code)] // Used for initialization, stored for lifetime management
    v_scrollbar: Rc<RefCell<ScrollBar>>,
    #[allow(dead_code)] // Used for initialization, stored for lifetime management
    indicator: Rc<RefCell<Indicator>>,
    // Indices in window.frame_children for direct updates
    h_scrollbar_idx: usize,
    v_scrollbar_idx: usize,
    indicator_idx: usize,
}

impl EditWindow {
    /// Create a new edit window
    ///
    /// Matches Borland: TEditWindow constructor creates TWindow and inserts scrollbars+editor as children
    pub fn new(bounds: Rect, title: &str) -> Self {
        let mut window = Window::new(bounds, title);

        // Calculate window size (matching Borland's size.x, size.y)
        let window_width = bounds.width();
        let window_height = bounds.height();

        // Calculate interior size (relative coordinates)
        let interior_width = window_width - 2;  // Subtract frame
        let interior_height = window_height - 2;

        // Create scrollbars at frame edges (matching Borland's TEditWindow)
        // Positions are relative to window frame (0,0 = top-left of frame)
        let h_bounds = Rect::new(18, window_height - 1, window_width - 2, window_height);
        let h_scrollbar = Rc::new(RefCell::new(ScrollBar::new_horizontal(h_bounds)));

        let v_bounds = Rect::new(window_width - 1, 1, window_width, window_height - 2);
        let v_scrollbar = Rc::new(RefCell::new(ScrollBar::new_vertical(v_bounds)));

        let ind_bounds = Rect::new(2, window_height - 1, 16, window_height);
        let indicator = Rc::new(RefCell::new(Indicator::new(ind_bounds)));

        // Create editor with bounds relative to interior
        // Interior is the window content area (inset by 1 from frame)
        // Editor bounds are RELATIVE, so start at (0,0) within interior
        // The editor will overlap with scrollbars at the edges, scrollbars draw on top
        let editor_bounds = Rect::new(0, 0, interior_width, interior_height);
        let editor = Rc::new(RefCell::new(Editor::with_scrollbars(
            editor_bounds,
            Some(Rc::clone(&h_scrollbar)),
            Some(Rc::clone(&v_scrollbar)),
            Some(Rc::clone(&indicator)),
        )));

        // IMPORTANT: Insert editor into interior (relative to interior bounds)
        // But insert scrollbars/indicator as frame children (relative to window frame)
        window.add(Box::new(SharedEditor(Rc::clone(&editor))));
        let h_scrollbar_idx = window.add_frame_child(Box::new(SharedScrollBar(Rc::clone(&h_scrollbar))));
        let v_scrollbar_idx = window.add_frame_child(Box::new(SharedScrollBar(Rc::clone(&v_scrollbar))));
        let indicator_idx = window.add_frame_child(Box::new(SharedIndicator(Rc::clone(&indicator))));

        // Set initial indicator value to cursor position (1:1)
        // Editor cursor starts at (0,0) internally, displayed as (1,1) for user
        indicator.borrow_mut().set_value(
            Point::new(1, 1),
            false,
        );

        let mut edit_window = Self {
            window,
            editor,
            h_scrollbar,
            v_scrollbar,
            indicator,
            h_scrollbar_idx,
            v_scrollbar_idx,
            indicator_idx,
        };

        // Give the editor focus immediately when the EditWindow is created
        edit_window.window.set_focus(true);

        edit_window
    }

    /// Load a file into the editor
    pub fn load_file(&mut self, path: impl AsRef<std::path::Path>) -> std::io::Result<()> {
        self.editor.borrow_mut().load_file(path)
    }

    /// Save the editor content
    pub fn save_file(&mut self) -> std::io::Result<()> {
        self.editor.borrow_mut().save_file()
    }

    /// Save as a different file
    pub fn save_as(&mut self, path: impl AsRef<std::path::Path>) -> std::io::Result<()> {
        self.editor.borrow_mut().save_as(path)
    }

    /// Get the editor's filename
    pub fn get_filename(&self) -> Option<String> {
        self.editor.borrow().get_filename().map(|s| s.to_string())
    }

    /// Check if editor is modified
    pub fn is_modified(&self) -> bool {
        self.editor.borrow().is_modified()
    }

    /// Get a cloned Rc to the editor for advanced access
    pub fn editor_rc(&self) -> Rc<RefCell<Editor>> {
        Rc::clone(&self.editor)
    }

    /// Set the window title
    pub fn set_title(&mut self, title: &str) {
        self.window.set_title(title);
    }

    /// Synchronize frame children (scrollbars, indicator) positions with window bounds
    /// Called from draw() to ensure positions are always correct, preventing visual lag during resize
    /// IMPORTANT: Always update positions regardless of size to prevent elements "staying behind"
    fn sync_frame_children_positions(&mut self) {
        let bounds = self.window.bounds();
        let window_width = bounds.width();
        let window_height = bounds.height();

        // Always update horizontal scrollbar position (even if window is too small to display it properly)
        // This prevents it from "staying behind" during rapid resizing near minimum size
        if window_height >= 3 {
            let h_bounds = Rect::new(
                bounds.a.x + 18.min(window_width.saturating_sub(2)),
                bounds.a.y + window_height - 1,
                bounds.a.x + window_width - 2,
                bounds.a.y + window_height,
            );
            self.window.update_frame_child(self.h_scrollbar_idx, h_bounds);
        }

        // Always update vertical scrollbar position
        if window_width >= 3 && window_height >= 4 {
            let v_bounds = Rect::new(
                bounds.a.x + window_width - 1,
                bounds.a.y + 1,
                bounds.a.x + window_width,
                bounds.a.y + window_height - 2,
            );
            self.window.update_frame_child(self.v_scrollbar_idx, v_bounds);
        }

        // Always update indicator position (even if window is too small)
        if window_height >= 3 {
            let ind_bounds = Rect::new(
                bounds.a.x + 2,
                bounds.a.y + window_height - 1,
                bounds.a.x + 16.min(window_width - 2),
                bounds.a.y + window_height,
            );
            self.window.update_frame_child(self.indicator_idx, ind_bounds);
            // Note: Indicator value (cursor position) is updated by the Editor itself
        }
    }
}

impl View for EditWindow {
    fn bounds(&self) -> Rect {
        self.window.bounds()
    }

    fn set_bounds(&mut self, bounds: Rect) {
        // Window handles updating all children (including scrollbars, indicator, and editor)
        self.window.set_bounds(bounds);

        // Recalculate scrollbar positions based on NEW window size
        let window_width = bounds.width();
        let window_height = bounds.height();

        // Update scrollbar bounds to new window edges
        let h_bounds = Rect::new(
            bounds.a.x + 18,
            bounds.a.y + window_height - 1,
            bounds.a.x + window_width - 2,
            bounds.a.y + window_height,
        );
        self.window.update_frame_child(self.h_scrollbar_idx, h_bounds);

        let v_bounds = Rect::new(
            bounds.a.x + window_width - 1,
            bounds.a.y + 1,
            bounds.a.x + window_width,
            bounds.a.y + window_height - 2,
        );
        self.window.update_frame_child(self.v_scrollbar_idx, v_bounds);

        let ind_bounds = Rect::new(
            bounds.a.x + 2,
            bounds.a.y + window_height - 1,
            bounds.a.x + 16,
            bounds.a.y + window_height,
        );
        self.window.update_frame_child(self.indicator_idx, ind_bounds);
    }

    fn draw(&mut self, terminal: &mut Terminal) {
        // IMPORTANT: Update frame children positions BEFORE drawing to prevent visual lag
        // During rapid resizing, this ensures scrollbars are always at correct positions
        self.sync_frame_children_positions();

        // Draw frame and interior first
        self.window.frame_mut().draw(terminal);
        self.window.interior_mut().draw(terminal);

        // Check if scrollbars are needed based on content size
        let editor = self.editor.borrow();
        let needs_h_scrollbar = editor.needs_horizontal_scrollbar();
        let needs_v_scrollbar = editor.needs_vertical_scrollbar();
        drop(editor); // Release borrow before drawing

        // Conditionally draw scrollbars only if needed
        if needs_h_scrollbar {
            if let Some(child) = self.window.get_frame_child_mut(self.h_scrollbar_idx) {
                child.draw(terminal);
            }
        }
        if needs_v_scrollbar {
            if let Some(child) = self.window.get_frame_child_mut(self.v_scrollbar_idx) {
                child.draw(terminal);
            }
        }
        // Always draw indicator
        if let Some(child) = self.window.get_frame_child_mut(self.indicator_idx) {
            child.draw(terminal);
        }

        // Draw shadow if enabled
        if self.window.has_shadow() {
            self.window.draw_shadow(terminal);
        }
    }

    fn handle_event(&mut self, event: &mut Event) {
        // Save old bounds before Window processes the event
        let old_bounds = self.window.bounds();

        // Pass mouse events to scrollbars (if they're visible and the event hasn't been handled)
        // IMPORTANT: Only mouse events! Keyboard events (UP/DOWN/etc.) should go to the Editor,
        // not to scrollbars. This allows cursor movement before scrolling.
        if event.what == EventType::MouseDown || event.what == EventType::MouseMove || event.what == EventType::MouseUp {
            let editor = self.editor.borrow();
            let needs_h_scrollbar = editor.needs_horizontal_scrollbar();
            let needs_v_scrollbar = editor.needs_vertical_scrollbar();
            drop(editor);

            let mut scrollbar_handled = false;

            // Let horizontal scrollbar handle event if visible
            if needs_h_scrollbar {
                if let Some(child) = self.window.get_frame_child_mut(self.h_scrollbar_idx) {
                    child.handle_event(event);
                    if event.what == EventType::Nothing {
                        scrollbar_handled = true;
                    }
                }
            }

            // Let vertical scrollbar handle event if visible (and not already handled)
            if !scrollbar_handled && needs_v_scrollbar {
                if let Some(child) = self.window.get_frame_child_mut(self.v_scrollbar_idx) {
                    child.handle_event(event);
                    if event.what == EventType::Nothing {
                        scrollbar_handled = true;
                    }
                }
            }

            // If scrollbar handled the event, sync editor delta from scrollbar values
            if scrollbar_handled {
                self.editor.borrow_mut().sync_from_scrollbars();
                return;
            }
        }

        // Let Window handle the event (drag, resize, etc.)
        self.window.handle_event(event);

        // Check if bounds changed (resize or move)
        let new_bounds = self.window.bounds();
        if old_bounds != new_bounds {
            // Bounds changed - update editor size and frame children positions
            let window_width = new_bounds.width();
            let window_height = new_bounds.height();

            // Update Editor bounds to match new interior size
            // Editor is a child of the interior Group, which uses ABSOLUTE coordinates
            // Interior starts at (window.x + 1, window.y + 1), accounting for frame
            let interior_width = window_width.saturating_sub(2);  // Subtract frame
            let interior_height = window_height.saturating_sub(2);

            if interior_width > 0 && interior_height > 0 {
                let interior_a = Point::new(new_bounds.a.x + 1, new_bounds.a.y + 1);  // Interior top-left
                let editor_bounds = Rect::new(
                    interior_a.x,
                    interior_a.y,
                    interior_a.x + interior_width,
                    interior_a.y + interior_height,
                );
                self.editor.borrow_mut().set_bounds(editor_bounds);
            }

            // Note: Frame children (scrollbars, indicator) will be synced in draw()
            // No need to update them here - this prevents duplicate work
        }
    }

    fn can_focus(&self) -> bool {
        true
    }

    fn options(&self) -> u16 {
        self.window.options()
    }

    fn set_options(&mut self, options: u16) {
        self.window.set_options(options);
    }

    fn state(&self) -> StateFlags {
        self.window.state()
    }

    fn set_state(&mut self, state: StateFlags) {
        self.window.set_state(state);
    }

    fn get_palette(&self) -> Option<crate::core::palette::Palette> {
        self.window.get_palette()
    }

    fn get_end_state(&self) -> crate::core::command::CommandId {
        self.window.get_end_state()
    }

    fn set_end_state(&mut self, command: crate::core::command::CommandId) {
        self.window.set_end_state(command);
    }

    fn set_owner(&mut self, owner: *const dyn View) {
        self.window.set_owner(owner);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::NamedTempFile;
    use std::io::Write;

    #[test]
    fn test_edit_window_creation() {
        let bounds = Rect::new(0, 0, 80, 25);
        let window = EditWindow::new(bounds, "Test Editor");

        assert_eq!(window.bounds(), bounds);
        assert!(!window.is_modified());
    }

    #[test]
    fn test_edit_window_file_operations() {
        let bounds = Rect::new(0, 0, 80, 25);
        let mut window = EditWindow::new(bounds, "Test Editor");

        // Create temp file
        let mut file = NamedTempFile::new().unwrap();
        writeln!(file, "Test content").unwrap();
        file.flush().unwrap();

        // Load file
        let path = file.path().to_str().unwrap();
        window.load_file(path).unwrap();

        assert_eq!(window.get_filename(), Some(path.to_string()));
        assert!(!window.is_modified());

        // Save as
        let file2 = NamedTempFile::new().unwrap();
        let path2 = file2.path().to_str().unwrap();
        window.save_as(path2).unwrap();

        assert_eq!(window.get_filename(), Some(path2.to_string()));
    }

    #[test]
    fn test_edit_window_editor_access() {
        let bounds = Rect::new(0, 0, 80, 25);
        let window = EditWindow::new(bounds, "Test Editor");

        // Test access via Rc
        let editor = window.editor_rc();
        editor.borrow_mut().set_text("Hello, World!");
        assert_eq!(editor.borrow().get_text(), "Hello, World!");
    }
}

/// Builder for creating edit windows with a fluent API.
pub struct EditWindowBuilder {
    bounds: Option<Rect>,
    title: Option<String>,
}

impl EditWindowBuilder {
    pub fn new() -> Self {
        Self { bounds: None, title: None }
    }

    #[must_use]
    pub fn bounds(mut self, bounds: Rect) -> Self {
        self.bounds = Some(bounds);
        self
    }

    #[must_use]
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    pub fn build(self) -> EditWindow {
        let bounds = self.bounds.expect("EditWindow bounds must be set");
        let title = self.title.expect("EditWindow title must be set");
        EditWindow::new(bounds, &title)
    }

    pub fn build_boxed(self) -> Box<EditWindow> {
        Box::new(self.build())
    }
}

impl Default for EditWindowBuilder {
    fn default() -> Self {
        Self::new()
    }
}