summaryrefslogtreecommitdiff
path: root/textscreen/txt_window.c
blob: 1dcb2d88204f6c58ee6b4ee441473b78af03ebd3 (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
// Emacs style mode select   -*- C++ -*- 
//-----------------------------------------------------------------------------
//
// $Id$
//
// Copyright(C) 1993-1996 Id Software, Inc.
// Copyright(C) 2006 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.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//

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

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

txt_window_t *TXT_NewWindow(char *title)
{
    txt_window_t *win;

    win = malloc(sizeof(txt_window_t));

    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->widgets = NULL;
    win->num_widgets = 0;
    win->selected = 0;

    TXT_AddDesktopWindow(win);

    return win;
}

void TXT_CloseWindow(txt_window_t *window)
{
    int i;

    // Free all widgets

    for (i=0; i<window->num_widgets; ++i)
    {
        TXT_DestroyWidget(window->widgets[i]);
    }
    
    // Free window resources

    free(window->widgets);
    free(window->title);
    free(window);

    TXT_RemoveDesktopWindow(window);
}

static void CalcWindowSize(txt_window_t *window, int *w, int *h)
{
    txt_widget_t *widget;
    int max_width;
    int height;
    int i;
    int ww;

    max_width = 10;
    height = 0;

    for (i=0; i<window->num_widgets; ++i)
    {
        ww = TXT_WidgetWidth(window->widgets[i]);

        if (ww > max_width)
            max_width = ww;

        if (window->widgets[i]->visible)
        {
            height += 1;
        }
    }

    *w = max_width;
    *h = height;
}

static void CalcWindowPosition(txt_window_t *window,
                               int *x, int *y,
                               int w, int h)
{
    switch (window->horiz_align)
    {
        case TXT_HORIZ_LEFT:
            *x = window->x;
            break;
        case TXT_HORIZ_CENTER:
            *x = window->x - (w / 2);
            break;
        case TXT_HORIZ_RIGHT:
            *x = window->x - (w - 1);
            break;
    }

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

void TXT_DrawWindow(txt_window_t *window)
{
    int widgets_w, widgets_h;
    int window_w, window_h;
    int x, y;
    int window_x, window_y;
    int i;
    
    CalcWindowSize(window, &widgets_w, &widgets_h);

    // Actual window size after padding

    window_w = widgets_w + 2;
    window_h = widgets_h + 5;

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

    CalcWindowPosition(window, &window_x, &window_y, window_w, window_h);

    // Draw the window

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

    // Draw all widgets

    x = window_x + 1;
    y = window_y + 2;

    for (i=0; i<window->num_widgets; ++i)
    {
        TXT_GotoXY(x, y);
        TXT_DrawWidget(window->widgets[i], widgets_w, i == window->selected);

        if (window->widgets[i]->visible)
        {
            y += 1;
        }
    }

    // Separator for action area

    TXT_DrawSeparator(window_x, y, window_w);
}

void TXT_AddWidget(txt_window_t *window, void *uncast_widget)
{
    txt_widget_t *widget;

    widget = (txt_widget_t *) uncast_widget;

    if (window->num_widgets == 0)
    {
        // This is the first widget added.
        //
        // The first widget in a window should always be a separator.
        // If we are not adding a separator now, add one in first.

        if (widget->widget_class != &txt_separator_class)
        {
            txt_separator_t *separator;

            separator = TXT_NewSeparator(NULL);

            window->widgets = malloc(sizeof(txt_widget_t *));
            window->widgets[0] = &separator->widget;
            window->num_widgets = 1;
        }
    }
    
    window->widgets = realloc(window->widgets,
                              sizeof(txt_widget_t *) * (window->num_widgets + 1));
    window->widgets[window->num_widgets] = widget;
    ++window->num_widgets;
}

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;
}