about summary refs log tree commit diff
path: root/converter/other/fiasco/buttons.c
blob: fc54d84a145f22d646b562df19bf6c73f2f603e5 (plain) (blame)
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
/*
 *  buttons.c:		Draw MWFA player buttons in X11 window	
 *
 *  Written by:		Ullrich Hafner
 *		
 *  This file is part of FIASCO (Fractal Image And Sequence COdec)
 *  Copyright (C) 1994-2000 Ullrich Hafner
 */

/*
 *  $Date: 2000/06/15 17:23:11 $
 *  $Author: hafner $
 *  $Revision: 5.2 $
 *  $State: Exp $
 */

#include "config.h"

#ifndef X_DISPLAY_MISSING

#include <X11/Xlib.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>

#if STDC_HEADERS
#	include <stdlib.h>
#endif /* not STDC_HEADERS */

#include "types.h"
#include "macros.h"

#include "display.h"
#include "binerror.h"
#include "buttons.h"

/*****************************************************************************

			     local variables
  
*****************************************************************************/

static const int EVENT_MASK = (KeyPressMask | ButtonPressMask |
			       ButtonReleaseMask | ExposureMask);

/*****************************************************************************

				prototypes
  
*****************************************************************************/

static void
draw_progress_bar (x11_info_t *xinfo, binfo_t *binfo, unsigned n,
		   unsigned n_frames);
static void
draw_button (x11_info_t *xinfo, binfo_t *binfo,
	     buttons_t button, bool_t pressed);
static void
draw_control_panel (x11_info_t *xinfo, binfo_t *binfo,
		    unsigned n, unsigned n_frames);

/*****************************************************************************

				public code
  
*****************************************************************************/

binfo_t * 
init_buttons (x11_info_t *xinfo, unsigned n, unsigned n_frames,
	      unsigned buttons_height, unsigned progbar_height)
/*
 *  Initialize a toolbar with the typical collection of video player
 *  buttons (pause, play, record, next, etc.) in the window given by 'xinfo'.
 *  'n' gives the current frame, 'whereas' n_frames is the total number of
 *  frames of the video stream.
 *  The size of the button toolbar is given by 'buttons_height',
 *  the size of the progressbar is given by 'progbar_height'.
 *
 *  Return value:
 *	struct managing the toolbar and progressbar information
 */
{
   XGCValues  values;
   XEvent     event;
   Colormap   cmap;
   XColor     gray, dgray, lgray, red;
   XColor     graye, dgraye, lgraye, rede;
   buttons_t  button;			/* counter */
   binfo_t   *binfo = calloc (1, sizeof (binfo_t));

   if (!binfo)
      error ("Out of memory.");
   
   binfo->width            = xinfo->ximage->width;
   binfo->height           = buttons_height;
   binfo->progbar_height   = progbar_height;
   binfo->record_is_rewind = NO;

   /*
    *  Generate sub-window for control panel
    */
   binfo->window = XCreateSimpleWindow (xinfo->display, xinfo->window,
					0, xinfo->ximage->height,
					binfo->width, binfo->height, 0,
					BlackPixel (xinfo->display,
						    xinfo->screen),
					WhitePixel (xinfo->display,
						    xinfo->screen));
   XSelectInput(xinfo->display, binfo->window, StructureNotifyMask);
   XMapWindow (xinfo->display, binfo->window);
   do
   {
      XNextEvent (xinfo->display, &event);
   }
   while (event.type != MapNotify || event.xmap.event != binfo->window);
   XSelectInput (xinfo->display, binfo->window, EVENT_MASK);

   /*
    *  Generate graphic contexts for different colors.
    */
   cmap = DefaultColormap (xinfo->display, xinfo->screen);
   XAllocNamedColor (xinfo->display, cmap, "#404040", &dgray, &dgraye);
   XAllocNamedColor (xinfo->display, cmap, "white", &lgray, &lgraye);
   XAllocNamedColor (xinfo->display, cmap, "#a8a8a8", &gray, &graye);
   XAllocNamedColor (xinfo->display, cmap, "red", &red, &rede);
   
   values.foreground = BlackPixel (xinfo->display, xinfo->screen);
   values.background = WhitePixel (xinfo->display, xinfo->screen);
   binfo->gc [BLACK] = XCreateGC (xinfo->display,
				  RootWindow (xinfo->display, xinfo->screen),
				  (GCForeground | GCBackground), &values);
   values.foreground = BlackPixel (xinfo->display, xinfo->screen);
   values.background = WhitePixel (xinfo->display, xinfo->screen);
   values.line_width = 3;
   values.join_style = JoinRound;
   binfo->gc [THICKBLACK] = XCreateGC (xinfo->display,
				       RootWindow (xinfo->display,
						   xinfo->screen),
				       (GCForeground | GCBackground
					| GCLineWidth | GCJoinStyle), &values);
   values.foreground = gray.pixel;
   values.background = WhitePixel (xinfo->display, xinfo->screen);
   binfo->gc [NGRAY] = XCreateGC (xinfo->display,
				  RootWindow (xinfo->display, xinfo->screen),
				  (GCForeground | GCBackground), &values);
   values.foreground = lgray.pixel;
   values.background = WhitePixel (xinfo->display, xinfo->screen);
   binfo->gc [LGRAY] = XCreateGC (xinfo->display,
				  RootWindow (xinfo->display, xinfo->screen),
				  (GCForeground | GCBackground), &values);
   values.foreground = dgray.pixel;
   values.background = WhitePixel (xinfo->display, xinfo->screen);
   binfo->gc [DGRAY] = XCreateGC (xinfo->display,
				  RootWindow (xinfo->display, xinfo->screen),
				  (GCForeground | GCBackground), &values);
   values.foreground = red.pixel;
   values.background = WhitePixel (xinfo->display, xinfo->screen);
   binfo->gc [RED]   = XCreateGC (xinfo->display,
				  RootWindow (xinfo->display, xinfo->screen),
				  (GCForeground | GCBackground), &values);

   for (button = 0; button < NO_BUTTON; button++)
      binfo->pressed [button] = NO;

   draw_control_panel (xinfo, binfo, n, n_frames); 
   
   return binfo;
}

void
wait_for_input (x11_info_t *xinfo)
/*
 *  Wait for key press or mouse click in window 'xinfo'.
 *  Redraw 'image' if event other then ButtonPress or KeyPress occurs.
 *  Enlarge or reduce size of image by factor 2^'enlarge_factor'.
 *
 *  No return value.
 *
 *  Side effect:
 *	program is terminated after key press or mouse click.
 */
{
   bool_t leave_loop = NO;
   
   XSelectInput (xinfo->display, xinfo->window, EVENT_MASK);

   while (!leave_loop)
   {
      XEvent event;

      XMaskEvent (xinfo->display, EVENT_MASK, &event);
      switch (event.type)
      {
	 case ButtonPress:
	 case KeyPress:
	    leave_loop = YES;
	    break;
	 default:
	    display_image (0, 0, xinfo);
	    break;
      }
   }
}

void
check_events (x11_info_t *xinfo, binfo_t *binfo, unsigned n, unsigned n_frames)
/*
 *  Check the X11 event loop. If the PAUSE buttonin the of panel 'binfo'
 *  is activated wait until next event occurs.
 *  Redraw 'image' if event other then ButtonPress or ButtonRelease occurs.
 *  Enlarge or reduce size of image by factor 2^'enlarge_factor'.
 *  'n' gives the current frame, 'whereas' n_frames is the total number of
 *  frames of the video stream.
 *
 *  No return values.
 *
 *  Side effects:
 *	status of buttons (binfo->pressed [button]) is changed accordingly.
 */
{
   bool_t leave_eventloop;

   leave_eventloop = (!binfo->pressed [PAUSE_BUTTON]
		      && binfo->pressed [PLAY_BUTTON])
		     || (!binfo->pressed [PAUSE_BUTTON]
			 && binfo->record_is_rewind
			 && binfo->pressed [RECORD_BUTTON])
		     || binfo->pressed [RECORD_BUTTON];
   draw_progress_bar (xinfo, binfo, n, n_frames);

   if (binfo->pressed [PAUSE_BUTTON] && binfo->pressed [PLAY_BUTTON])
   {
      XFlush (xinfo->display);
      draw_button (xinfo, binfo, PLAY_BUTTON, NO); /* clear PLAY mode */
      XFlush (xinfo->display);
   }
   if (binfo->pressed [PAUSE_BUTTON]
       && binfo->record_is_rewind && binfo->pressed [RECORD_BUTTON])
   {
      XFlush (xinfo->display);
      draw_button (xinfo, binfo, RECORD_BUTTON, NO); /* clear PLAY mode */
      XFlush (xinfo->display);
   }

   if (binfo->pressed [STOP_BUTTON])
   {
      XFlush (xinfo->display);
      draw_button (xinfo, binfo, STOP_BUTTON, NO); /* clear STOP button */
      XFlush (xinfo->display);
   }

   do
   {
      XEvent event;
      int    button;
      bool_t wait_release = NO;
	 
      
      if (XCheckMaskEvent (xinfo->display, EVENT_MASK, &event))
      {
	 switch (event.type)
	 {
	    case ButtonPress:
	       wait_release = NO;
	       if (!(binfo->pressed [RECORD_BUTTON] &&
		     !binfo->record_is_rewind))
		  for (button = 0; button < NO_BUTTON; button++)
		  {
		     int x0, y0, x1, y1; /* button coordinates */
		  
		     x0 = button * (binfo->width / NO_BUTTON);
		     y0 = binfo->progbar_height;
		     x1 = x0 + binfo->width / NO_BUTTON;
		     y1 = y0 + binfo->height - binfo->progbar_height - 1;
		     if (event.xbutton.x > x0 && event.xbutton.x < x1
			 && event.xbutton.y > y0 && event.xbutton.y < y1) 
		     {
			draw_button (xinfo, binfo, button,
				     !binfo->pressed [button]);
			wait_release = YES;
			break;
		     }
		  }
	       break;
	    case ButtonRelease:
	       wait_release = NO;
	       break;
	    default:
	       wait_release = NO;
	       draw_control_panel (xinfo, binfo, n, n_frames);
	       display_image (0, 0, xinfo);
	       break;
	 }
	 leave_eventloop = !wait_release
			   && (binfo->pressed [PLAY_BUTTON]
			       || binfo->pressed [STOP_BUTTON]
			       || binfo->pressed [RECORD_BUTTON]
			       || binfo->pressed [QUIT_BUTTON]);
      }
   } while (!leave_eventloop);

   if ((binfo->pressed [RECORD_BUTTON] && !binfo->record_is_rewind)
       && n == n_frames - 1)
   {
      binfo->record_is_rewind = YES;
      draw_button (xinfo, binfo, RECORD_BUTTON, NO);
   }
}

/*****************************************************************************

				private code
  
*****************************************************************************/

static void
draw_control_panel (x11_info_t *xinfo, binfo_t *binfo,
		    unsigned n, unsigned n_frames)
/*
 *  Draw control panel 'binfo' with all buttons and progressbar in
 *  the given 'window'.
 *  'n' gives the current frame, 'whereas' n_frames is the total number of
 *  frames of the video stream.
 *
 *  No return value.
 */
{
   buttons_t button;
   
   XFillRectangle (xinfo->display, binfo->window, binfo->gc [NGRAY],
		   0, 0, binfo->width, binfo->height);
   draw_progress_bar (xinfo, binfo, n, n_frames);
   for (button = 0; button < NO_BUTTON; button++)
      draw_button (xinfo, binfo, button, binfo->pressed [button]);
}

static void
draw_progress_bar (x11_info_t *xinfo, binfo_t *binfo, unsigned n,
		   unsigned n_frames)
/*
 *  Draw progressbar of control panel 'binfo' in the given 'window'.
 *  'n' gives the current frame, whereas 'n_frames' is the total number of
 *  frames of the video stream.
 *
 *  No return value.
 */
{
   unsigned x, y, width, height;

   x 	  = 2;
   y 	  = 1;
   width  = binfo->width - 5;
   height = binfo->progbar_height - 3;
   
   XDrawLine (xinfo->display, binfo->window, binfo->gc [DGRAY],
	      x, y, x + width, y);
   XDrawLine (xinfo->display, binfo->window, binfo->gc [DGRAY],
	      x, y, x, y + height - 1);
   XDrawLine (xinfo->display, binfo->window, binfo->gc [LGRAY],
	      x + width, y + 1, x + width, y + height);
   XDrawLine (xinfo->display, binfo->window, binfo->gc [LGRAY],
	      x, y + height, x + width, y + height);

   x++; y++; width  -= 2; height -= 2;
   XFillRectangle (xinfo->display, binfo->window, binfo->gc [NGRAY],
		   x, y, width, height);

   XFillRectangle (xinfo->display, binfo->window, binfo->gc [BLACK],
		   x + n * max (1, width / n_frames), y,
		   max (1, width / n_frames), height);
}

static void
draw_button (x11_info_t *xinfo, binfo_t *binfo,
	     buttons_t button, bool_t pressed)
/*
 *  Draw 'button' of control panel 'binfo' in the given 'window'.
 *  'pressed' indicates whether the button is pressed or not.
 *
 *  No return value.
 */
{
   grayscale_t top, bottom;		/* index of GC */
   unsigned    x, y, width, height;	/* coordinates of button */
   
   x 	  = button * (binfo->width / NO_BUTTON);
   y 	  = binfo->progbar_height;
   width  = binfo->width / NO_BUTTON;
   height = binfo->height - binfo->progbar_height - 1;
   
   if (width < 4 || height < 4)
      return;
   
   if (pressed)
   {
      top    = DGRAY;
      bottom = LGRAY;
   }
   else
   {
      top    = LGRAY;
      bottom = DGRAY;
   }

   x 	 += 2;
   width -= 4;
   
   XDrawLine (xinfo->display, binfo->window, binfo->gc [top],
	      x, y, x + width, y);
   XDrawLine (xinfo->display, binfo->window, binfo->gc [top],
	      x, y, x, y + height - 1);
   XDrawLine (xinfo->display, binfo->window, binfo->gc [bottom],
	      x + width, y + 1, x + width, y + height);
   XDrawLine (xinfo->display, binfo->window, binfo->gc [bottom],
	      x, y + height, x + width, y + height);

   x++; y++; width  -= 2; height -= 2;
   XFillRectangle (xinfo->display, binfo->window, binfo->gc [NGRAY],
		   x, y, width, height);

   switch (button)
   {
      case STOP_BUTTON:
	 XFillRectangle (xinfo->display, binfo->window, binfo->gc [BLACK],
			 x + width / 2 - 6, y + height / 2 - 4, 11, 11);
	 if (pressed && !binfo->pressed [STOP_BUTTON])
	 {
	    draw_button (xinfo, binfo, PLAY_BUTTON, NO);
	    draw_button (xinfo, binfo, PAUSE_BUTTON, NO); 
	    draw_button (xinfo, binfo, RECORD_BUTTON, NO); 
	 }
	 break;
      case PAUSE_BUTTON:
	 XFillRectangle (xinfo->display, binfo->window, binfo->gc [BLACK],
			 x + width / 2 - 6, y + height / 2 - 4, 5, 11);
	 XFillRectangle (xinfo->display, binfo->window, binfo->gc [BLACK],
			 x + width / 2 + 1, y + height / 2 - 4, 5, 11);
	 break;
      case PLAY_BUTTON:
	 {
	    XPoint triangle [3];

	    triangle [0].x = x + width / 2 - 5;
	    triangle [0].y = y + height / 2 - 5;
	    triangle [1].x = 10;
	    triangle [1].y = 6;
	    triangle [2].x = -10;
	    triangle [2].y = 6;

	    XFillPolygon (xinfo->display, binfo->window, binfo->gc [BLACK],
			  triangle, 3, Convex, CoordModePrevious);
	    if (pressed && !binfo->pressed [PLAY_BUTTON]
		&& binfo->pressed [RECORD_BUTTON])
	       draw_button (xinfo, binfo, RECORD_BUTTON, NO);
	 }
	 break;
      case RECORD_BUTTON:
	 if (!binfo->record_is_rewind)
	 {
	    XFillArc (xinfo->display, binfo->window, binfo->gc [RED],
		      x + width / 2 - 5, y + height / 2 - 5, 11, 11, 0,
		      360 * 64);
	    if (pressed && !binfo->pressed [RECORD_BUTTON])
	    {
	       draw_button (xinfo, binfo, STOP_BUTTON, YES);
	       draw_button (xinfo, binfo, PLAY_BUTTON, NO);
	       draw_button (xinfo, binfo, PAUSE_BUTTON, NO); 
	    }
	 }
	 else
	 {
	    XPoint triangle [3];

	    triangle [0].x = x + width / 2 + 5;
	    triangle [0].y = y + height / 2 - 5;
	    triangle [1].x = -10;
	    triangle [1].y = 6;
	    triangle [2].x = 10;
	    triangle [2].y = 6;

	    XFillPolygon (xinfo->display, binfo->window, binfo->gc [BLACK],
			  triangle, 3, Convex, CoordModePrevious);
	    if (pressed && !binfo->pressed [RECORD_BUTTON]
		&& binfo->pressed [PLAY_BUTTON])
	       draw_button (xinfo, binfo, PLAY_BUTTON, NO);
	 }
	 break;
      case QUIT_BUTTON:
	 {
	    XPoint triangle [3];

	    triangle [0].x = x + width / 2 - 6;
	    triangle [0].y = y + height / 2 + 2;
	    triangle [1].x = 6;
	    triangle [1].y = -7;
	    triangle [2].x = 6;
	    triangle [2].y = 7;

	    XFillPolygon (xinfo->display, binfo->window, binfo->gc [BLACK],
			  triangle, 3, Convex, CoordModePrevious);
	    XFillRectangle (xinfo->display, binfo->window, binfo->gc [BLACK],
			    x + width / 2 - 5, y + height / 2 + 4, 11, 3);
	 }
	 break;
      default:
	 break;
   }
   binfo->pressed [button] = pressed;
}

#endif /* not X_DISPLAY_MISSING */