turbo-vision 0.10.0

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
# Theoretical Customizations: Deriving from TProgram


This document shows examples of custom application classes that derive from `TProgram` instead of `TApplication`, demonstrating selective subsystem initialization.

## Background


In Borland Turbo Vision, `TApplication` derives from `TProgram` and adds five subsystems:
1. Memory manager (`InitMemory/DoneMemory`)
2. Video manager (`InitVideo/DoneVideo`)
3. Event manager (`InitEvents/DoneEvents`)
4. System error handler (`InitSysError/DoneSysError`)
5. History list manager (`InitHistory/DoneHistory`)

By deriving directly from `TProgram`, you can selectively enable only the subsystems you need.

---

## Example 1: Minimal Application (No History Lists)


**Use Case:** A simple utility that doesn't need input field history.

```cpp
// C++ (Borland Turbo Vision style)
#include <tv.h>


class TMinimalApp : public TProgram
{
public:
    TMinimalApp();
    virtual ~TMinimalApp();

    static TStatusLine *initStatusLine(TRect r);
    static TMenuBar *initMenuBar(TRect r);
    static TDeskTop *initDeskTop(TRect r);
};

TMinimalApp::TMinimalApp() :
    TProgInit(&TMinimalApp::initStatusLine,
              &TMinimalApp::initMenuBar,
              &TMinimalApp::initDeskTop)
{
    // Initialize only the subsystems we need
    InitMemory();   // Always need memory manager for safety pool
    InitVideo();    // Need video for screen management
    InitEvents();   // Need events for keyboard/mouse
    InitSysError(); // Need error handling

    // SKIP InitHistory() - we don't need input field history!

    // Now call TProgram constructor behavior
    // (This is already done by TProgram's constructor)
}

TMinimalApp::~TMinimalApp()
{
    // Clean up subsystems in reverse order
    DoneSysError();
    DoneEvents();
    DoneVideo();
    DoneMemory();

    // NO DoneHistory() call - we never initialized it
}

TStatusLine *TMinimalApp::initStatusLine(TRect r)
{
    r.a.y = r.b.y - 1;
    return new TStatusLine(r,
        *new TStatusDef(0, 0xFFFF) +
            *new TStatusItem("~F10~ Menu", kbF10, cmMenu) +
            *new TStatusItem("~Alt-X~ Exit", kbAltX, cmQuit)
    );
}

TMenuBar *TMinimalApp::initMenuBar(TRect r)
{
    r.b.y = r.a.y + 1;
    return new TMenuBar(r,
        *new TSubMenu("~F~ile", kbAltF) +
            *new TMenuItem("E~x~it", cmQuit, kbAltX, hcNoContext, "Alt-X")
    );
}

TDeskTop *TMinimalApp::initDeskTop(TRect r)
{
    r.a.y++;
    r.b.y--;
    return new TDeskTop(r);
}

// Usage
int main()
{
    TMinimalApp app;
    app.run();
    return 0;
}
```

**Memory Savings:** Without history lists, saves ~4KB of heap space (default `HistorySize`).

---

## Example 2: Read-Only Display Application


**Use Case:** A log viewer or status monitor that only displays information (no user input).

```cpp
#include <tv.h>


class TDisplayApp : public TProgram
{
public:
    TDisplayApp();
    virtual ~TDisplayApp();

    // Override getEvent to only handle quit keys
    virtual void getEvent(TEvent& event);

    static TStatusLine *initStatusLine(TRect r);
    static TMenuBar *initMenuBar(TRect r) { return 0; } // No menu
    static TDeskTop *initDeskTop(TRect r);
};

TDisplayApp::TDisplayApp() :
    TProgInit(&TDisplayApp::initStatusLine,
              &TDisplayApp::initMenuBar,
              &TDisplayApp::initDeskTop)
{
    InitMemory();   // Need memory manager
    InitVideo();    // Need video for display

    // SKIP InitEvents() - minimal event handling
    // SKIP InitSysError() - we'll handle errors ourselves
    // SKIP InitHistory() - no user input
}

TDisplayApp::~TDisplayApp()
{
    DoneVideo();
    DoneMemory();
}

void TDisplayApp::getEvent(TEvent& event)
{
    // Simplified event handling - only check for quit keys
    TProgram::getEvent(event);

    if (event.what == evKeyDown)
    {
        if (event.keyDown.keyCode == kbEsc ||
            event.keyDown.keyCode == kbAltX)
        {
            event.what = evCommand;
            event.message.command = cmQuit;
        }
    }
}

TStatusLine *TDisplayApp::initStatusLine(TRect r)
{
    r.a.y = r.b.y - 1;
    return new TStatusLine(r,
        *new TStatusDef(0, 0xFFFF) +
            *new TStatusItem("~Esc~ Exit", kbEsc, cmQuit)
    );
}

TDeskTop *TDisplayApp::initDeskTop(TRect r)
{
    r.a.y = 1;  // No menu bar
    r.b.y--;
    return new TDeskTop(r);
}

// Usage: Display-only application
int main()
{
    TDisplayApp app;

    // Create a read-only window
    TRect r(10, 5, 70, 20);
    TWindow *w = new TWindow(r, "System Status", wnNoNumber);

    // Add static text (no input controls)
    TStaticText *st = new TStaticText(r, "Monitoring system...");
    w->insert(st);

    app.deskTop->insert(w);
    app.run();

    return 0;
}
```

---

## Example 3: Embedded System Application


**Use Case:** Running on embedded hardware with limited memory and no persistent storage.

```cpp
#include <tv.h>


class TEmbeddedApp : public TProgram
{
private:
    bool lowMemoryMode;

public:
    TEmbeddedApp(unsigned availableMemory);
    virtual ~TEmbeddedApp();

    virtual void idle();
    virtual void outOfMemory();

    static TStatusLine *initStatusLine(TRect r);
    static TMenuBar *initMenuBar(TRect r);
    static TDeskTop *initDeskTop(TRect r);
};

TEmbeddedApp::TEmbeddedApp(unsigned availableMemory) :
    TProgInit(&TEmbeddedApp::initStatusLine,
              &TEmbeddedApp::initMenuBar,
              &TEmbeddedApp::initDeskTop)
{
    // Determine which subsystems we can afford
    lowMemoryMode = (availableMemory < 64 * 1024); // Less than 64KB

    if (lowMemoryMode)
    {
        // Minimal subsystems
        InitMemory();  // Essential
        InitVideo();   // Essential for display
        InitEvents();  // Essential for input

        // SKIP InitSysError() - handle inline
        // SKIP InitHistory() - no room for history
    }
    else
    {
        // Full subsystems
        InitMemory();
        InitVideo();
        InitEvents();
        InitSysError();
        InitHistory();
    }
}

TEmbeddedApp::~TEmbeddedApp()
{
    if (!lowMemoryMode)
    {
        DoneHistory();
        DoneSysError();
    }

    DoneEvents();
    DoneVideo();
    DoneMemory();
}

void TEmbeddedApp::idle()
{
    TProgram::idle();

    // In embedded systems, yield CPU to other tasks
    // or enter low-power mode during idle
    #ifdef EMBEDDED_RTOS
        taskYield();
    #endif
}

void TEmbeddedApp::outOfMemory()
{
    // Embedded-specific out-of-memory handling
    messageBox("Critical: Out of memory! Restarting...",
               mfError | mfOKButton);

    // In embedded systems, might trigger a watchdog reset
    #ifdef EMBEDDED_SYSTEM
        systemReset();
    #else
        exit(1);
    #endif
}

TStatusLine *TEmbeddedApp::initStatusLine(TRect r)
{
    r.a.y = r.b.y - 1;
    return new TStatusLine(r,
        *new TStatusDef(0, 0xFFFF) +
            *new TStatusItem("", kbF10, cmMenu) +
            *new TStatusItem("Quit", kbAltX, cmQuit)
    );
}

TMenuBar *TEmbeddedApp::initMenuBar(TRect r)
{
    r.b.y = r.a.y + 1;
    return new TMenuBar(r,
        *new TSubMenu("System", kbAltS) +
            *new TMenuItem("Status", cmStatus, kbF1) +
            *new TMenuItem("Reset", cmReset, kbCtrlR) +
            *new TMenuLine() +
            *new TMenuItem("Exit", cmQuit, kbAltX)
    );
}

TDeskTop *TEmbeddedApp::initDeskTop(TRect r)
{
    r.a.y++;
    r.b.y--;
    return new TDeskTop(r);
}
```

---

## Example 4: Testing Framework Application


**Use Case:** Automated testing harness that doesn't need full UI subsystems.

```cpp
#include <tv.h>


class TTestApp : public TProgram
{
private:
    bool headlessMode;
    int testsPassed;
    int testsFailed;

public:
    TTestApp(bool headless = false);
    virtual ~TTestApp();

    void runTest(const char *name, bool (*testFunc)());
    void printResults();

    static TStatusLine *initStatusLine(TRect r);
    static TMenuBar *initMenuBar(TRect r);
    static TDeskTop *initDeskTop(TRect r);
};

TTestApp::TTestApp(bool headless) :
    TProgInit(&TTestApp::initStatusLine,
              &TTestApp::initMenuBar,
              &TTestApp::initDeskTop),
    headlessMode(headless),
    testsPassed(0),
    testsFailed(0)
{
    InitMemory();  // Need memory management

    if (!headlessMode)
    {
        InitVideo();   // Only init video if not headless
    }

    InitEvents();  // Need event system for test coordination

    // SKIP InitSysError() - tests handle their own errors
    // SKIP InitHistory() - not needed for testing
}

TTestApp::~TTestApp()
{
    DoneEvents();

    if (!headlessMode)
    {
        DoneVideo();
    }

    DoneMemory();
}

void TTestApp::runTest(const char *name, bool (*testFunc)())
{
    if (!headlessMode)
    {
        // Visual feedback
        TRect r(10, 5, 70, 10);
        TWindow *w = new TWindow(r, name, wnNoNumber);
        deskTop->insert(w);
    }

    bool result = testFunc();

    if (result)
    {
        testsPassed++;
    }
    else
    {
        testsFailed++;
    }

    if (!headlessMode)
    {
        // Remove test window
        deskTop->current->setState(sfVisible, False);
    }
}

void TTestApp::printResults()
{
    printf("Tests Passed: %d\n", testsPassed);
    printf("Tests Failed: %d\n", testsFailed);
}

TStatusLine *TTestApp::initStatusLine(TRect r)
{
    r.a.y = r.b.y - 1;
    return new TStatusLine(r,
        *new TStatusDef(0, 0xFFFF) +
            *new TStatusItem("Running tests...", kbNoKey, cmNone)
    );
}

TMenuBar *TTestApp::initMenuBar(TRect r)
{
    return 0; // No menu in test mode
}

TDeskTop *TTestApp::initDeskTop(TRect r)
{
    r.a.y = 1;
    r.b.y--;
    return new TDeskTop(r);
}

// Usage
bool testAddition() { return (2 + 2 == 4); }
bool testSubtraction() { return (5 - 3 == 2); }

int main(int argc, char *argv[])
{
    bool headless = (argc > 1 && strcmp(argv[1], "--headless") == 0);

    TTestApp app(headless);

    app.runTest("Addition Test", testAddition);
    app.runTest("Subtraction Test", testSubtraction);

    app.printResults();

    return 0;
}
```

---

## Summary


These examples demonstrate why `TProgram` exists as an intermediate class:

| Example | Skipped Subsystems | Benefit |
|---------|-------------------|---------|
| Minimal App | History | ~4KB memory savings |
| Display-Only | Events, SysError, History | Simpler code, faster startup |
| Embedded | SysError, History (conditional) | Adapts to memory constraints |
| Test Framework | Video (conditional), SysError, History | Headless testing support |

**In Practice:** These use cases are rare. Most developers just used `TApplication` because:
1. The memory overhead is negligible on modern systems
2. You almost always need all subsystems
3. Selective initialization is error-prone
4. The flexibility isn't worth the complexity

This is why the Rust port correctly merges `TProgram` and `TApplication` into a single `Application` struct - the theoretical flexibility of `TProgram` provides no practical value in modern development.