summaryrefslogtreecommitdiff
path: root/textscreen/txt_window.c
blob: 5aa35edfdaaeb63ad67a6b36791fb02361138cda (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
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
//
// 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 <stdarg.h>
#include <string.h>

#include "doomkeys.h"

#include "txt_label.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"

void TXT_SetWindowAction(txt_window_t *window,
                         txt_horiz_align_t position, 
                         txt_window_action_t *action)
{
    if (window->actions[position] != NULL)
    {
        TXT_DestroyWidget(window->actions[position]);
    }

    window->actions[position] = action;

    // Maintain parent pointer.

    if (action != NULL)
    {
        action->widget.parent = &window->table.widget;
    }
}

txt_window_t *TXT_NewWindow(char *title)
{
    int i;

    txt_window_t *win;

    win = malloc(sizeof(txt_window_t));

    TXT_InitTable(&win->table, 1);

    if (title == NULL)
    {
        win->title = NULL;
    }
    else
    {
        win->title = strdup(title);
    }

    win->x = TXT_SCREEN_W / 2;
    win->y = TXT_SCREEN_H / 2;
    win->horiz_align = TXT_HORIZ_CENTER;
    win->vert_align = TXT_VERT_CENTER;
    win->key_listener = NULL;
    win->mouse_listener = NULL;
    win->help_url = NULL;

    TXT_AddWidget(win, TXT_NewSeparator(NULL));

    for (i=0; i<3; ++i)
        win->actions[i] = NULL;

    TXT_AddDesktopWindow(win);

    // Default actions

    TXT_SetWindowAction(win, TXT_HORIZ_LEFT, TXT_NewWindowEscapeAction(win));
    TXT_SetWindowAction(win, TXT_HORIZ_RIGHT, TXT_NewWindowSelectAction(win));

    return win;
}

void TXT_CloseWindow(txt_window_t *window)
{
    int i;

    TXT_EmitSignal(window, "closed");
    TXT_RemoveDesktopWindow(window);

    free(window->title);

    // Destroy all actions

    for (i=0; i<3; ++i)
    {
        if (window->actions[i] != NULL)
        {
            TXT_DestroyWidget(window->actions[i]);
        }
    }

    // Destroy table and window

    TXT_DestroyWidget(window);
}

static void CalcWindowPosition(txt_window_t *window)
{
    switch (window->horiz_align)
    {
        case TXT_HORIZ_LEFT:
            window->window_x = window->x;
            break;
        case TXT_HORIZ_CENTER:
            window->window_x = window->x - (window->window_w / 2);
            break;
        case TXT_HORIZ_RIGHT:
            window->window_x = window->x - (window->window_w - 1);
            break;
    }

    switch (window->vert_align)
    {
        case TXT_VERT_TOP:
            window->window_y = window->y;
            break;
        case TXT_VERT_CENTER:
            window->window_y = window->y - (window->window_h / 2);
            break;
        case TXT_VERT_BOTTOM:
            window->window_y = window->y - (window->window_h - 1);
            break;
    }
}

static void LayoutActionArea(txt_window_t *window)
{
    txt_widget_t *widget;
    int space_available;
    int space_left_offset;

    // We need to calculate the available horizontal space for the center
    // action widget, so that we can center it within it.
    // To start with, we have the entire action area available.

    space_available = window->window_w;
    space_left_offset = 0;

    // Left action

    if (window->actions[TXT_HORIZ_LEFT] != NULL)
    {
        widget = (txt_widget_t *) window->actions[TXT_HORIZ_LEFT];

        TXT_CalcWidgetSize(widget);

        widget->x = window->window_x + 1;
        widget->y = window->window_y + window->window_h - widget->h - 1;

        // Adjust available space:

        space_available -= widget->w;
        space_left_offset += widget->w;
    }

    // Draw the right action

    if (window->actions[TXT_HORIZ_RIGHT] != NULL)
    {
        widget = (txt_widget_t *) window->actions[TXT_HORIZ_RIGHT];

        TXT_CalcWidgetSize(widget);

        widget->x = window->window_x + window->window_w - 1 - widget->w;
        widget->y = window->window_y + window->window_h - widget->h - 1;

        // Adjust available space:

        space_available -= widget->w;
    }

    // Draw the center action

    if (window->actions[TXT_HORIZ_CENTER] != NULL)
    {
        widget = (txt_widget_t *) window->actions[TXT_HORIZ_CENTER];

        TXT_CalcWidgetSize(widget);

        // The left and right widgets have left a space sandwiched between
        // them.  Center this widget within that space.

        widget->x = window->window_x
                  + space_left_offset
                  + (space_available - widget->w) / 2;
        widget->y = window->window_y + window->window_h - widget->h - 1;
    }
}

static void DrawActionArea(txt_window_t *window)
{
    int i;

    for (i=0; i<3; ++i)
    {
        if (window->actions[i] != NULL)
        {
            TXT_DrawWidget(window->actions[i]);
        }
    }
}

static void CalcActionAreaSize(txt_window_t *window, 
                               unsigned int *w, unsigned int *h)
{
    txt_widget_t *widget;
    int i;

    *w = 0;
    *h = 0;

    // Calculate the width of all the action widgets and use this
    // to create an overall min. width of the action area

    for (i=0; i<3; ++i)
    {
        widget = (txt_widget_t *) window->actions[i];

        if (widget != NULL)
        {
            TXT_CalcWidgetSize(widget);
            *w += widget->w;

            if (widget->h > *h)
            {
                *h = widget->h;
            }
        }
    }
}

// Sets size and position of all widgets in a window

void TXT_LayoutWindow(txt_window_t *window)
{
    txt_widget_t *widgets = (txt_widget_t *) window;
    unsigned int widgets_w;
    unsigned int actionarea_w, actionarea_h;

    // Calculate size of table
    
    TXT_CalcWidgetSize(window);

    // Widgets area: add one character of padding on each side
    widgets_w = widgets->w + 2;

    // Calculate the size of the action area
    // Make window wide enough to action area
  
    CalcActionAreaSize(window, &actionarea_w, &actionarea_h);
    
    if (actionarea_w > widgets_w)
        widgets_w = actionarea_w;

    // Set the window size based on widgets_w
   
    window->window_w = widgets_w + 2;
    window->window_h = widgets->h + 1;

    // If the window has a title, add an extra two lines

    if (window->title != NULL)
    {
        window->window_h += 2;
    }

    // If the window has an action area, add extra lines

    if (actionarea_h > 0)
    {
        window->window_h += actionarea_h + 1;
    }

    // Use the x,y position as the centerpoint and find the location to 
    // draw the window.

    CalcWindowPosition(window);

    // Set the table size and position

    widgets->w = widgets_w - 2;
    // widgets->h        (already set)
    widgets->x = window->window_x + 2;
    widgets->y = window->window_y;

    if (window->title != NULL)
    {
        widgets->y += 2;
    }

    // Layout the table and action area

    LayoutActionArea(window);
    TXT_LayoutWidget(widgets);
}

void TXT_DrawWindow(txt_window_t *window)
{
    txt_widget_t *widgets;

    TXT_LayoutWindow(window);

    if (window->table.widget.focused)
    {
        TXT_BGColor(TXT_ACTIVE_WINDOW_BACKGROUND, 0);
    }
    else
    {
        TXT_BGColor(TXT_INACTIVE_WINDOW_BACKGROUND, 0);
    }

    TXT_FGColor(TXT_COLOR_BRIGHT_WHITE);

    // Draw the window

    TXT_DrawWindowFrame(window->title, 
                        window->window_x, window->window_y,
                        window->window_w, window->window_h);

    // Draw all widgets

    TXT_DrawWidget(window);

    // Draw an action area, if we have one

    widgets = (txt_widget_t *) window;

    if (widgets->y + widgets->h < window->window_y + window->window_h - 1)
    {
        // Separator for action area

        TXT_DrawSeparator(window->window_x, widgets->y + widgets->h, 
                          window->window_w);

        // Action area at the window bottom

        DrawActionArea(window);
    }
}

void TXT_SetWindowPosition(txt_window_t *window,
                           txt_horiz_align_t horiz_align,
                           txt_vert_align_t vert_align,
                           int x, int y)
{
    window->vert_align = vert_align;
    window->horiz_align = horiz_align;
    window->x = x;
    window->y = y;
}

static int MouseButtonPress(txt_window_t *window, int b)
{
    int x, y;
    int i;
    txt_widget_t *widgets;
    txt_widget_t *widget;

    // Lay out the window, set positions and sizes of all widgets

    TXT_LayoutWindow(window);

    // Get the current mouse position

    TXT_GetMousePosition(&x, &y);

    // Try the mouse button listener
    // This happens whether it is in the window range or not

    if (window->mouse_listener != NULL)
    {
        // Mouse listener can eat button presses

        if (window->mouse_listener(window, x, y, b, 
                                   window->mouse_listener_data))
        {
            return 1;
        }
    }

    // Is it within the table range?

    widgets = (txt_widget_t *) window;

    if (x >= widgets->x && x < (signed) (widgets->x + widgets->w)
     && y >= widgets->y && y < (signed) (widgets->y + widgets->h))
    {
        TXT_WidgetMousePress(window, x, y, b);
        return 1;
    }

    // Was one of the action area buttons pressed?

    for (i=0; i<3; ++i)
    {
        widget = (txt_widget_t *) window->actions[i];

        if (widget != NULL
         && x >= widget->x && x < (signed) (widget->x + widget->w)
         && y >= widget->y && y < (signed) (widget->y + widget->h))
        {
            int was_focused;

            // Main table temporarily loses focus when action area button
            // is clicked. This way, any active input boxes that depend
            // on having focus will save their values before the
            // action is performed.

            was_focused = window->table.widget.focused;
            TXT_SetWidgetFocus(window, 0);
            TXT_SetWidgetFocus(window, was_focused);

            // Pass through mouse press.

            TXT_WidgetMousePress(widget, x, y, b);
            return 1;
        }
    }

    return 0;
}

int TXT_WindowKeyPress(txt_window_t *window, int c)
{
    int i;

    // Is this a mouse button ?

    if (c >= TXT_MOUSE_BASE && c < TXT_MOUSE_BASE + TXT_MAX_MOUSE_BUTTONS)
    {
        return MouseButtonPress(window, c);
    }

    // Try the window key spy

    if (window->key_listener != NULL)
    {
        // key listener can eat keys

        if (window->key_listener(window, c, window->key_listener_data))
        {
            return 1;
        }
    }

    // Send to the currently selected widget:

    if (TXT_WidgetKeyPress(window, c))
    {
        return 1;
    }

    // Try all of the action buttons

    for (i=0; i<3; ++i)
    {
        if (window->actions[i] != NULL
         && TXT_WidgetKeyPress(window->actions[i], c))
        {
            return 1;
        }
    }

    return 0;
}

void TXT_SetKeyListener(txt_window_t *window, TxtWindowKeyPress key_listener, 
                        void *user_data)
{
    window->key_listener = key_listener;
    window->key_listener_data = user_data;
}

void TXT_SetMouseListener(txt_window_t *window, 
                          TxtWindowMousePress mouse_listener,
                          void *user_data)
{
    window->mouse_listener = mouse_listener;
    window->mouse_listener_data = user_data;
}

void TXT_SetWindowFocus(txt_window_t *window, int focused)
{
    TXT_SetWidgetFocus(window, focused);
}

void TXT_SetWindowHelpURL(txt_window_t *window, char *help_url)
{
    window->help_url = help_url;
}

void TXT_OpenURL(char *url)
{
    char *cmd;
    size_t cmd_len;

    cmd_len = strlen(url) + 30;
    cmd = malloc(cmd_len);

#if defined(_WIN32)
    TXT_snprintf(cmd, cmd_len, "cmd /c start \"%s\"", url);
#elif defined(__MACOSX__)
    TXT_snprintf(cmd, cmd_len, "open \"%s\"", url);
#else
    // The Unix situation sucks as usual, but the closest thing to a
    // standard that exists is the xdg-utils package.
    if (system("xdg-open --version 2>/dev/null") != 0)
    {
        fprintf(stderr,
                "xdg-utils is not installed. Can't open this URL:\n%s\n", url);
        return;
    }

    TXT_snprintf(cmd, cmd_len, "xdg-open \"%s\"", url);
#endif

    system(cmd);
    free(cmd);
}

void TXT_OpenWindowHelpURL(txt_window_t *window)
{
    if (window->help_url != NULL)
    {
        TXT_OpenURL(window->help_url);
    }
}

txt_window_t *TXT_MessageBox(char *title, char *message, ...)
{
    txt_window_t *window;
    char buf[256];
    va_list args;

    va_start(args, message);
    TXT_vsnprintf(buf, sizeof(buf), message, args);
    va_end(args);

    window = TXT_NewWindow(title);
    TXT_AddWidget(window, TXT_NewLabel(buf));

    TXT_SetWindowAction(window, TXT_HORIZ_LEFT, NULL);
    TXT_SetWindowAction(window, TXT_HORIZ_CENTER, 
                        TXT_NewWindowEscapeAction(window));
    TXT_SetWindowAction(window, TXT_HORIZ_RIGHT, NULL);

    return window;
}