summaryrefslogtreecommitdiff
path: root/twin_toplevel.c
blob: a8fd246d4aca9951c401b707d97ded0c2dd84c7c (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
/*
 * Twin - A Tiny Window System
 * Copyright © 2004 Keith Packard <keithp@keithp.com>
 * All rights reserved.
 *
 * This Library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with the Twin Library; see the file COPYING.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "twinint.h"

twin_dispatch_result_t
_twin_toplevel_dispatch (twin_widget_t *widget, twin_event_t *event)
{
    twin_toplevel_t *toplevel = (twin_toplevel_t *) widget;
    twin_event_t    ev = *event;
    
    switch (ev.kind) {
    case TwinEventConfigure:
	ev.u.configure.extents.left = 0;
	ev.u.configure.extents.top = 0;
	ev.u.configure.extents.right = (event->u.configure.extents.right -
					event->u.configure.extents.left);
	ev.u.configure.extents.bottom = (event->u.configure.extents.bottom -
					 event->u.configure.extents.top);
	break;
    default:
	break;
    }
    return _twin_box_dispatch (&toplevel->box.widget, &ev);
}

static twin_bool_t
_twin_toplevel_event (twin_window_t   *window,
		      twin_event_t    *event)
{
    twin_toplevel_t   *toplevel = window->client_data;

    return (*toplevel->box.widget.dispatch) (&toplevel->box.widget, event) == TwinDispatchDone;
}

static void
_twin_toplevel_draw (twin_window_t    *window)
{
    twin_toplevel_t   *toplevel = window->client_data;
    twin_event_t	event;

    twin_screen_disable_update(window->screen);
    event.kind = TwinEventPaint;
    (*toplevel->box.widget.dispatch) (&toplevel->box.widget, &event);
    twin_screen_enable_update(window->screen);
}

static void
_twin_toplevel_destroy (twin_window_t *window)
{
    twin_toplevel_t   *toplevel = window->client_data;
    twin_event_t	event;

    event.kind = TwinEventDestroy;
    (*toplevel->box.widget.dispatch) (&toplevel->box.widget, &event);
}

void
_twin_toplevel_init (twin_toplevel_t	    *toplevel,
		     twin_dispatch_proc_t   dispatch,
		     twin_window_t	    *window,
		     const char		    *name)
{
    twin_rect_t	extents;

    twin_window_set_name (window, name);
    extents.left = 0;
    extents.top = 0;
    extents.right = window->client.right - window->client.left;
    extents.bottom =window->client.bottom - window->client.top;
    window->draw = _twin_toplevel_draw;
    window->destroy = _twin_toplevel_destroy;
    window->event = _twin_toplevel_event;
    window->client_data = toplevel;
    _twin_box_init (&toplevel->box, 0, window, TwinBoxVert, dispatch);
}

twin_toplevel_t *
twin_toplevel_create (twin_screen_t	    *screen,
		      twin_format_t	    format,
		      twin_window_style_t   style,
		      twin_coord_t	    x,
		      twin_coord_t	    y,
		      twin_coord_t	    width,
		      twin_coord_t	    height,
		      const char	    *name)
{
    twin_toplevel_t *toplevel;
    twin_window_t   *window = twin_window_create (screen, format, style,
						  x, y, width, height);
    
    if (!window)
	return 0;
    toplevel = malloc (sizeof (twin_toplevel_t) + strlen (name) + 1);
    if (!toplevel)
    {
	twin_window_destroy (window);
	return 0;
    }
    _twin_toplevel_init (toplevel, _twin_toplevel_dispatch, window, name);
    return toplevel;
}

static twin_bool_t
_twin_toplevel_paint (void *closure)
{
    twin_toplevel_t *toplevel = closure;
    twin_event_t    ev;
    
    twin_screen_disable_update(toplevel->box.widget.window->screen);
    ev.kind = TwinEventPaint;
    (*toplevel->box.widget.dispatch) (&toplevel->box.widget, &ev);
    twin_screen_enable_update(toplevel->box.widget.window->screen);
    return TWIN_FALSE;
}

void
_twin_toplevel_queue_paint (twin_widget_t *widget)
{
    twin_toplevel_t *toplevel = (twin_toplevel_t *) widget;

    if (!toplevel->box.widget.paint)
    {
	toplevel->box.widget.paint = TWIN_TRUE;
	twin_set_work (_twin_toplevel_paint,
		       TWIN_WORK_PAINT,
		       toplevel);
	
    }
}

static twin_bool_t
_twin_toplevel_layout (void *closure)
{
    twin_toplevel_t *toplevel = closure;
    twin_event_t    ev;
    twin_window_t   *window = toplevel->box.widget.window;

    ev.kind = TwinEventQueryGeometry;
    (*toplevel->box.widget.dispatch) (&toplevel->box.widget, &ev);
    ev.kind = TwinEventConfigure;
    ev.u.configure.extents.left = 0;
    ev.u.configure.extents.top = 0;
    ev.u.configure. extents.right = window->client.right - window->client.left;
    ev.u.configure.extents.bottom =window->client.bottom - window->client.top;
    (*toplevel->box.widget.dispatch) (&toplevel->box.widget, &ev);
    return TWIN_FALSE;
}

void
_twin_toplevel_queue_layout (twin_widget_t *widget)
{
    twin_toplevel_t *toplevel = (twin_toplevel_t *) widget;

    if (!toplevel->box.widget.layout)
    {
	toplevel->box.widget.layout = TWIN_TRUE;
	twin_set_work (_twin_toplevel_layout,
		       TWIN_WORK_LAYOUT,
		       toplevel);
	_twin_toplevel_queue_paint (widget);
    }
}

void
twin_toplevel_show (twin_toplevel_t *toplevel)
{
    _twin_toplevel_layout (toplevel);
    _twin_toplevel_paint (toplevel);
    twin_window_show (toplevel->box.widget.window);
}