summaryrefslogtreecommitdiff
path: root/twin_window.c
blob: 0ca180be629c63686285ed71756befd4e2a6f915 (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
/*
 * 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"

#define TWIN_ACTIVE_BG	    0xd03b80ae
#define TWIN_INACTIVE_BG    0xff808080
#define TWIN_FRAME_TEXT	    0xffffffff
#define TWIN_ACTIVE_BORDER  0xff606060
#define TWIN_BW		    0
#define TWIN_TITLE_HEIGHT   20
#define TWIN_RESIZE_SIZE    ((TWIN_TITLE_HEIGHT + 4) / 5)
#define TWIN_TITLE_BW	    ((TWIN_TITLE_HEIGHT + 11) / 12)

twin_window_t *
twin_window_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)
{
    twin_window_t   *window = malloc (sizeof (twin_window_t));
    twin_coord_t    left, top, right, bottom;

    if (!window) return NULL;
    window->screen = screen;
    window->style = style;
    switch (window->style) {
    case TwinWindowApplication:
	left = TWIN_BW;
	top = TWIN_BW + TWIN_TITLE_HEIGHT + TWIN_BW;
	right = TWIN_BW + TWIN_RESIZE_SIZE;
	bottom = TWIN_BW + TWIN_RESIZE_SIZE;
	break;
    case TwinWindowPlain:
    default:
	left = 0;
	top = 0;
	right = 0;
	bottom = 0;
	break;
    }
    width += left + right;
    height += top + bottom;
    window->client.left = left;
    window->client.top = top;
    window->client.right = width - right;
    window->client.bottom = height - bottom;
    window->pixmap = twin_pixmap_create (format, width, height);
    twin_pixmap_clip (window->pixmap,
		      window->client.left, window->client.top,
		      window->client.right, window->client.bottom);
    twin_pixmap_origin_to_clip (window->pixmap);
    window->pixmap->window = window;
    twin_pixmap_move (window->pixmap, x, y);
    window->damage = window->client;
    window->client_grab = TWIN_FALSE;
    window->want_focus = TWIN_FALSE;
    window->draw_queued = TWIN_FALSE;
    window->client_data = 0;
    window->name = 0;
    
    window->draw = 0;
    window->event = 0;
    window->destroy = 0;
    return window;
}

void
twin_window_destroy (twin_window_t *window)
{
    twin_window_hide (window);
    twin_pixmap_destroy (window->pixmap);
    if (window->name) free (window->name);
    free (window);
}

void
twin_window_show (twin_window_t *window)
{
    if (window->pixmap != window->screen->top)
	twin_pixmap_show (window->pixmap, window->screen, window->screen->top);
}

void
twin_window_hide (twin_window_t *window)
{
    twin_pixmap_hide (window->pixmap);
}

void
twin_window_configure (twin_window_t	    *window,
		       twin_window_style_t  style,
		       twin_coord_t	    x,
		       twin_coord_t	    y,
		       twin_coord_t	    width,
		       twin_coord_t	    height)
{
    twin_bool_t	need_repaint = TWIN_FALSE;
    
    twin_pixmap_disable_update (window->pixmap);
    if (style != window->style)
    {
	window->style = style;
	need_repaint = TWIN_TRUE;
    }
    if (width != window->pixmap->width || height != window->pixmap->height)
    {
	twin_pixmap_t	*old = window->pixmap;
	int		i;

	window->pixmap = twin_pixmap_create (old->format, width, height);
	window->pixmap->window = window;
	twin_pixmap_move (window->pixmap, x, y);
	if (old->screen)
	    twin_pixmap_show (window->pixmap, window->screen, old);
	for (i = 0; i < old->disable; i++)
	    twin_pixmap_disable_update (window->pixmap);
	twin_pixmap_destroy (old);
	twin_pixmap_reset_clip (window->pixmap);
	twin_pixmap_clip (window->pixmap,
			  window->client.left, window->client.top,
			  window->client.right, window->client.bottom);
	twin_pixmap_origin_to_clip (window->pixmap);
    }
    if (x != window->pixmap->x || y != window->pixmap->y)
	twin_pixmap_move (window->pixmap, x, y);
    if (need_repaint)
	twin_window_draw (window);
    twin_pixmap_enable_update (window->pixmap);
}

void
twin_window_style_size (twin_window_style_t style,
			twin_rect_t	    *size)
{
    switch (style) {
    case TwinWindowPlain:
    default:
	size->left = size->right = size->top = size->bottom = 0;
	break;
    case TwinWindowApplication:
	size->left = TWIN_BW;
	size->right = TWIN_BW;
	size->top = TWIN_BW + TWIN_TITLE_HEIGHT + TWIN_BW;
	size->bottom = TWIN_BW;
	break;
    }
}

void
twin_window_set_name (twin_window_t	*window,
		      const char	*name)
{
    if (window->name) free (window->name);
    window->name = malloc (strlen (name) + 1);
    if (window->name) strcpy (window->name, name);
    twin_window_draw (window);
}

static void
twin_window_frame (twin_window_t *window)
{
    twin_fixed_t	bw = twin_int_to_fixed (TWIN_TITLE_BW);
    twin_path_t		*path;
    twin_fixed_t	bw_2 = bw / 2;
    twin_pixmap_t	*pixmap = window->pixmap;
    twin_fixed_t	w_top = bw_2;
    twin_fixed_t	c_left = bw_2;
    twin_fixed_t	t_h = twin_int_to_fixed (window->client.top) - bw;
    twin_fixed_t	t_arc_1 = t_h / 3;
    twin_fixed_t	t_arc_2 = t_h * 2 / 3;
    twin_fixed_t	c_right = twin_int_to_fixed (window->client.right) - bw_2;
    twin_fixed_t	c_top = twin_int_to_fixed (window->client.top) - bw_2;
    
    twin_fixed_t	name_height = t_h - bw - bw_2;
    twin_fixed_t	icon_size = name_height * 8 / 10;
    twin_fixed_t	icon_y = (twin_int_to_fixed (window->client.top) - 
				  icon_size) / 2;
    twin_fixed_t	menu_x = t_arc_2;
    twin_fixed_t	text_x = menu_x + icon_size + bw;
    twin_fixed_t	text_y = icon_y + icon_size;
    twin_fixed_t	text_width;
    twin_fixed_t	title_right;
    twin_fixed_t	close_x;
    twin_fixed_t	max_x;
    twin_fixed_t	min_x;
    twin_fixed_t	resize_x;
    twin_fixed_t	resize_y;
    const char		*name;
    
    twin_pixmap_reset_clip (pixmap);
    twin_pixmap_origin_to_clip (pixmap);
    
    twin_fill (pixmap, 0x00000000, TWIN_SOURCE, 
	       0, 0, pixmap->width, window->client.top);

    path = twin_path_create ();

		      
    /* name */
    name = window->name;
    if (!name)
	name = "Sans un nom!";
    twin_path_set_font_size (path, name_height);
    twin_path_set_font_style (path, TWIN_TEXT_OBLIQUE | TWIN_TEXT_UNHINTED);
    text_width = twin_width_utf8 (path, name);
	
    title_right = (text_x + text_width + 
		   bw + icon_size +
		   bw + icon_size +
		   bw + icon_size + 
		   t_arc_2);
    
    if (title_right < c_right)
	c_right = title_right;
    
							  
    close_x = c_right - t_arc_2 - icon_size;
    max_x = close_x - bw - icon_size;
    min_x = max_x - bw - icon_size;
    resize_x = twin_int_to_fixed (window->client.right);
    resize_y = twin_int_to_fixed (window->client.bottom);

    /* border */
     
    twin_path_move (path, c_left, c_top);
    twin_path_draw (path, c_right, c_top);
    twin_path_curve (path,
		     c_right, w_top + t_arc_1,
		     c_right - t_arc_1, w_top,
		     c_right - t_h, w_top);
    twin_path_draw (path, c_left + t_h, w_top);
    twin_path_curve (path,
		     c_left + t_arc_1, w_top,
		     c_left, w_top + t_arc_1,
		     c_left, c_top);
    twin_path_close (path);

    twin_paint_path (pixmap, TWIN_ACTIVE_BG, path);
    
    twin_paint_stroke (pixmap, TWIN_ACTIVE_BORDER, path, bw_2 * 2);

    twin_path_empty (path);
    
    twin_pixmap_clip (pixmap, 
		      twin_fixed_to_int (twin_fixed_floor (menu_x)),
		      0,
		      twin_fixed_to_int (twin_fixed_ceil (c_right - t_arc_2)),
		      window->client.top);
    twin_pixmap_origin_to_clip (pixmap);

    twin_path_move (path, text_x - twin_fixed_floor (menu_x), text_y);
    twin_path_utf8 (path, window->name);
    twin_paint_path (pixmap, TWIN_FRAME_TEXT, path);

    twin_pixmap_reset_clip (pixmap);
    twin_pixmap_origin_to_clip (pixmap);
	
    /* widgets */

    {
	twin_matrix_t	m;
	
	twin_matrix_identity (&m);
	twin_matrix_translate (&m, menu_x, icon_y);
	twin_matrix_scale (&m, icon_size, icon_size);
	twin_icon_draw (pixmap, TwinIconMenu, m);
	
	twin_matrix_identity (&m);
	twin_matrix_translate (&m, min_x, icon_y);
	twin_matrix_scale (&m, icon_size, icon_size);
	twin_icon_draw (pixmap, TwinIconMinimize, m);
	
	twin_matrix_identity (&m);
	twin_matrix_translate (&m, max_x, icon_y);
	twin_matrix_scale (&m, icon_size, icon_size);
	twin_icon_draw (pixmap, TwinIconMaximize, m);

	twin_matrix_identity (&m);
	twin_matrix_translate (&m, close_x, icon_y);
	twin_matrix_scale (&m, icon_size, icon_size);
	twin_icon_draw (pixmap, TwinIconClose, m);
	
	twin_matrix_identity (&m);
	twin_matrix_translate (&m, resize_x, resize_y);
	twin_matrix_scale (&m, 
			   twin_int_to_fixed (TWIN_TITLE_HEIGHT),
			   twin_int_to_fixed (TWIN_TITLE_HEIGHT));
	twin_icon_draw (pixmap, TwinIconResize, m);
    }

    twin_pixmap_clip (pixmap,
		      window->client.left, window->client.top,
		      window->client.right, window->client.bottom);
    twin_pixmap_origin_to_clip (pixmap);

    twin_path_destroy (path);
}

void
twin_window_draw (twin_window_t *window)
{
    twin_pixmap_t *pixmap = window->pixmap;

    switch (window->style) {
    case TwinWindowPlain:
    default:
	break;
    case TwinWindowApplication:
	twin_window_frame (window);
	break;
    }

    /* if no draw function or no damage, return */
    if (window->draw == NULL ||
	(window->damage.left >= window->damage.right ||
	 window->damage.top >= window->damage.bottom))
	return;

    /* clip to damaged area and draw */
    twin_pixmap_reset_clip (pixmap);
    twin_pixmap_clip(pixmap,
		     window->damage.left, window->damage.top,
		     window->damage.right, window->damage.bottom);
    (*window->draw) (window);

    /* damage matching screen area */
    twin_pixmap_damage(pixmap,
		     window->damage.left, window->damage.top,
		     window->damage.right, window->damage.bottom);

    /* clear damage and restore clip */
    window->damage.left = window->damage.right = 0;
    window->damage.top = window->damage.bottom = 0;
    twin_pixmap_reset_clip (pixmap);
    twin_pixmap_clip (pixmap,
		      window->client.left, window->client.top,
		      window->client.right, window->client.bottom);
}

/* window keep track of local damage */
void twin_window_damage (twin_window_t *window,
			 twin_coord_t left, twin_coord_t top,
			 twin_coord_t right, twin_coord_t bottom)
{
    if (left < window->client.left)
	left = window->client.left;
    if (top < window->client.top)
	top = window->client.top;
    if (right > window->client.right)
	right = window->client.right;
    if (bottom > window->client.bottom)
	bottom = window->client.bottom;

    if (window->damage.left == window->damage.right)
    {
	window->damage.left = left;
	window->damage.right = right;
	window->damage.top = top;
	window->damage.bottom = bottom;
    }
    else
    {
	if (left < window->damage.left)
	    window->damage.left = left;
	if (top < window->damage.top)
	    window->damage.top = top;
	if (window->damage.right < right)
	    window->damage.right = right;
	if (window->damage.bottom < bottom)
	    window->damage.bottom = bottom;
    }
}

static twin_bool_t _twin_window_repaint (void *closure)
{
    twin_window_t *window = closure;

    window->draw_queued = TWIN_FALSE;
    twin_window_draw(window);

    return TWIN_FALSE;
}

void twin_window_queue_paint (twin_window_t *window)
{
    if (!window->draw_queued) {
	window->draw_queued = TWIN_TRUE;
	twin_set_work (_twin_window_repaint, TWIN_WORK_PAINT, window);
    }
}

twin_bool_t
twin_window_dispatch (twin_window_t *window, twin_event_t *event)
{
    twin_event_t    ev = *event;
    twin_bool_t	    delegate = TWIN_TRUE;

    switch (ev.kind) {
    case TwinEventButtonDown:
	if (window->client.left <= ev.u.pointer.x &&
	    ev.u.pointer.x < window->client.right &&
	    window->client.top  <= ev.u.pointer.y &&
	    ev.u.pointer.y < window->client.bottom)
	{
	    delegate = TWIN_TRUE;
	    window->client_grab = TWIN_TRUE;
	    ev.u.pointer.x -= window->client.left;
	    ev.u.pointer.y -= window->client.top;
	}
	else
	    delegate = TWIN_FALSE;
	break;
    case TwinEventButtonUp:
	if (window->client_grab)
	{
	    delegate = TWIN_TRUE;
	    window->client_grab = TWIN_FALSE;
	    ev.u.pointer.x -= window->client.left;
	    ev.u.pointer.y -= window->client.top;
	}
	else
	    delegate = TWIN_FALSE;
	break;
    case TwinEventMotion:
	if (window->client_grab ||
	    (window->client.left <= ev.u.pointer.x &&
	     ev.u.pointer.x < window->client.right &&
	     window->client.top  <= ev.u.pointer.y &&
	     ev.u.pointer.y < window->client.bottom))
	{
	    delegate = TWIN_TRUE;
	    ev.u.pointer.x -= window->client.left;
	    ev.u.pointer.y -= window->client.top;
	}
	else
	    delegate = TWIN_FALSE;
	break;
    default:
	break;
    }
    if (!window->event)
	delegate = TWIN_FALSE;
    
    if (delegate && (*window->event) (window, &ev))
	return TWIN_TRUE;
    
    /*
     * simple window management
     */
    switch (event->kind) {
    case TwinEventButtonDown:
	twin_window_show (window);
	window->screen->button_x = event->u.pointer.x;
	window->screen->button_y = event->u.pointer.y;
	return TWIN_TRUE;
    case TwinEventButtonUp:
	window->screen->button_x = -1;
	window->screen->button_y = -1;
    case TwinEventMotion:
	if (window->screen->button_x >= 0)
	{
	    twin_coord_t    x, y;

	    x = event->u.pointer.screen_x - window->screen->button_x;
	    y = event->u.pointer.screen_y - window->screen->button_y;
	    twin_window_configure (window, 
				   window->style,
				   x, y,
				   window->pixmap->width,
				   window->pixmap->height);
	}
	return TWIN_TRUE;
    default:
	break;
    }
    return TWIN_FALSE;
}