summaryrefslogtreecommitdiff
path: root/textscreen/txt_desktop.c
blob: e56a5ee418f3d81484dd342fdfb50897930df724 (plain)
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
//
// Copyright(C) 2005-2014 Simon Howard
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "doomkeys.h"

#include "txt_desktop.h"
#include "txt_gui.h"
#include "txt_io.h"
#include "txt_main.h"
#include "txt_separator.h"
#include "txt_window.h"

#define HELP_KEY KEY_F1
#define MAXWINDOWS 128

static char *desktop_title;
static txt_window_t *all_windows[MAXWINDOWS];
static int num_windows = 0;
static int main_loop_running = 0;

static TxtIdleCallback periodic_callback = NULL;
static void *periodic_callback_data;
static unsigned int periodic_callback_period;

void TXT_AddDesktopWindow(txt_window_t *win)
{
    // Previously-top window loses focus:

    if (num_windows > 0)
    {
        TXT_SetWindowFocus(all_windows[num_windows - 1], 0);
    }

    all_windows[num_windows] = win;
    ++num_windows;

    // New window gains focus:

    TXT_SetWindowFocus(win, 1);
}

void TXT_RemoveDesktopWindow(txt_window_t *win)
{
    int from, to;

    // Window must lose focus if it's being removed:

    TXT_SetWindowFocus(win, 0);

    for (from=0, to=0; from<num_windows; ++from)
    {
        if (all_windows[from] != win)
        {
            all_windows[to] = all_windows[from];
            ++to;
        }
    }

    num_windows = to;

    // Top window gains focus:

    if (num_windows > 0)
    {
        TXT_SetWindowFocus(all_windows[num_windows - 1], 1);
    }
}

txt_window_t *TXT_GetActiveWindow(void)
{
    if (num_windows == 0)
    {
        return NULL;
    }

    return all_windows[num_windows - 1];
}

int TXT_RaiseWindow(txt_window_t *window)
{
    int i;

    for (i = 0; i < num_windows - 1; ++i)
    {
        if (all_windows[i] == window)
        {
            all_windows[i] = all_windows[i + 1];
            all_windows[i + 1] = window;

            if (i == num_windows - 2)
            {
                TXT_SetWindowFocus(all_windows[i], 0);
                TXT_SetWindowFocus(window, 1);
            }

            return 1;
        }
    }

    // Window not in the list, or at the end of the list (top) already.

    return 0;
}

int TXT_LowerWindow(txt_window_t *window)
{
    int i;

    for (i = 0; i < num_windows - 1; ++i)
    {
        if (all_windows[i + 1] == window)
        {
            all_windows[i + 1] = all_windows[i];
            all_windows[i] = window;

            if (i == num_windows - 2)
            {
                TXT_SetWindowFocus(window, 0);
                TXT_SetWindowFocus(all_windows[i + 1], 1);
            }

            return 1;
        }
    }

    // Window not in the list, or at the start of the list (bottom) already.

    return 0;
}

static void DrawDesktopBackground(const char *title)
{
    int i;
    unsigned char *screendata;
    unsigned char *p;

    screendata = TXT_GetScreenData();
    
    // Fill the screen with gradient characters

    p = screendata;
    
    for (i=0; i<TXT_SCREEN_W * TXT_SCREEN_H; ++i)
    {
        *p++ = 0xb1;
        *p++ = TXT_COLOR_GREY | (TXT_COLOR_BLUE << 4);
    }

    // Draw the top and bottom banners

    p = screendata;

    for (i=0; i<TXT_SCREEN_W; ++i)
    {
        *p++ = ' ';
        *p++ = TXT_COLOR_BLACK | (TXT_COLOR_GREY << 4);
    }

    p = screendata + (TXT_SCREEN_H - 1) * TXT_SCREEN_W * 2;

    for (i=0; i<TXT_SCREEN_W; ++i)
    {
        *p++ = ' ';
        *p++ = TXT_COLOR_BLACK | (TXT_COLOR_GREY << 4);
    }

    // Print the title

    TXT_GotoXY(0, 0);
    TXT_FGColor(TXT_COLOR_BLACK);
    TXT_BGColor(TXT_COLOR_GREY, 0);

    TXT_PutChar(' ');
    TXT_Puts(title);
}

static void DrawHelpIndicator(void)
{
    char keybuf[10];
    int fgcolor;
    int x, y;

    TXT_GetKeyDescription(HELP_KEY, keybuf, sizeof(keybuf));

    TXT_GetMousePosition(&x, &y);

    if (y == 0 && x >= TXT_SCREEN_W - 9)
    {
        fgcolor = TXT_COLOR_GREY;
        TXT_BGColor(TXT_COLOR_BLACK, 0);
    }
    else
    {
        fgcolor = TXT_COLOR_BLACK;
        TXT_BGColor(TXT_COLOR_GREY, 0);
    }

    TXT_GotoXY(TXT_SCREEN_W - 9, 0);

    TXT_FGColor(TXT_COLOR_BRIGHT_WHITE);
    TXT_DrawString(" ");
    TXT_DrawString(keybuf);

    TXT_FGColor(fgcolor);
    TXT_DrawString("=Help ");
}

void TXT_SetDesktopTitle(char *title)
{
    free(desktop_title);
    desktop_title = strdup(title);
    TXT_SetWindowTitle(title);
}

void TXT_DrawDesktop(void)
{
    txt_window_t *active_window;
    const char *title;
    int i;

    TXT_InitClipArea();

    if (desktop_title == NULL)
        title = "";
    else
        title = desktop_title;

    DrawDesktopBackground(title);

    active_window = TXT_GetActiveWindow();
    if (active_window != NULL && active_window->help_url != NULL)
    {
        DrawHelpIndicator();
    }

    for (i=0; i<num_windows; ++i)
    {
        TXT_DrawWindow(all_windows[i]);
    }

    TXT_UpdateScreen();
}

// Fallback function to handle key/mouse events that are not handled by
// the active window.
static void DesktopInputEvent(int c)
{
    txt_window_t *active_window;
    int x, y;

    switch (c)
    {
        case TXT_MOUSE_LEFT:
            TXT_GetMousePosition(&x, &y);

            // Clicking the top-right of the screen is equivalent
            // to pressing the help key.
            if (y == 0 && x >= TXT_SCREEN_W - 9)
            {
                DesktopInputEvent(HELP_KEY);
            }
            break;

        case HELP_KEY:
            active_window = TXT_GetActiveWindow();
            if (active_window != NULL)
            {
                TXT_OpenWindowHelpURL(active_window);
            }
            break;

        default:
            break;
    }


}

void TXT_DispatchEvents(void)
{
    txt_window_t *active_window;
    int c;

    while ((c = TXT_GetChar()) > 0)
    {
        active_window = TXT_GetActiveWindow();

        if (active_window != NULL && !TXT_WindowKeyPress(active_window, c))
        {
            DesktopInputEvent(c);
        }
    }
}

void TXT_ExitMainLoop(void)
{
    main_loop_running = 0;
}

void TXT_DrawASCIITable(void)
{
    unsigned char *screendata;
    char buf[10];
    int x, y;
    int n;

    screendata = TXT_GetScreenData();

    TXT_FGColor(TXT_COLOR_BRIGHT_WHITE);
    TXT_BGColor(TXT_COLOR_BLACK, 0);

    for (y=0; y<16; ++y)
    {
        for (x=0; x<16; ++x)
        {
            n = y * 16 + x;

            TXT_GotoXY(x * 5, y);
            TXT_snprintf(buf, sizeof(buf), "%02x   ", n);
            TXT_Puts(buf);

            // Write the character directly to the screen memory buffer:

            screendata[(y * TXT_SCREEN_W + x * 5 + 3) * 2] = n;
        }
    }

    TXT_UpdateScreen();
}

void TXT_SetPeriodicCallback(TxtIdleCallback callback,
                             void *user_data,
                             unsigned int period)
{
    periodic_callback = callback;
    periodic_callback_data = user_data;
    periodic_callback_period = period;
}

void TXT_GUIMainLoop(void)
{
    main_loop_running = 1;

    while (main_loop_running)
    {
        TXT_DispatchEvents();

        // After the last window is closed, exit the loop

        if (num_windows <= 0)
        {
            TXT_ExitMainLoop();
            continue;
        }

        TXT_DrawDesktop();
//        TXT_DrawASCIITable();

        if (periodic_callback == NULL)
        {
            TXT_Sleep(0);
        }
        else
        {
            TXT_Sleep(periodic_callback_period);

            periodic_callback(periodic_callback_data);
        }
    }
}