File: button.c

package info (click to toggle)
fvwm95 2.0.43ba-15
  • links: PTS
  • area: main
  • in suites: potato
  • size: 6,356 kB
  • ctags: 4,759
  • sloc: ansic: 46,398; makefile: 1,586; sh: 782; perl: 328
file content (616 lines) | stat: -rw-r--r-- 14,267 bytes parent folder | download | duplicates (3)
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*
   FvwmButtons v2.0.41-plural-Z-alpha, copyright 1996, Jarl Totland

 * This module, and the entire GoodStuff program, and the concept for
 * interfacing this module to the Window Manager, are all original work
 * by Robert Nation
 *
 * Copyright 1993, Robert Nation. No guarantees or warantees or anything
 * are provided or implied in any way whatsoever. Use this program at your
 * own risk. Permission to use this program for any purpose is given,
 * as long as the copyright is kept intact.

*/

#include <unistd.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <X11/Intrinsic.h>
#include "FvwmButtons.h"

extern char *MyName;

/**
*** buttonInfo()
*** Give lots of info for this button: XPos, YPos, XPad, YPad, Frame(signed)
**/
void buttonInfo(button_info *b,int *x,int *y,int *px,int *py,int *f)
{
  ushort w=b_Padding|b_Frame;
  *x=buttonXPos(b,b->n);
  *y=buttonYPos(b,b->n);
  *px=b->xpad;
  *py=b->ypad;
  *f=b->framew;
  w&=~(b->flags&(b_Frame|b_Padding));
  
  if(b->flags&b_Container && w&b_Frame)
    {
      *f=0;
      w&=~b_Frame;
    }
  if((b->flags&b_Container || b->flags&b_Swallow) && w&b_Padding)
    {
      *px=*py=0;
      w&=~b_Padding;
    }

  while(w && (b=b->parent))
    {
      if(w&b_Frame && b->c->flags&b_Frame)
	{
	  *f=b->c->framew;
	  w&=~b_Frame;
	}
      if(w&b_Padding && b->c->flags&b_Padding)
	{
	  *px=b->c->xpad;
	  *py=b->c->ypad;
	  w&=~b_Padding;
	}
    }
}

/** 
*** GetInternalSize()
**/
void GetInternalSize(button_info *b,int *x,int *y,int *w,int *h)
{
  int f;
  int px,py;
  buttonInfo(b,x,y,&px,&py,&f);
  f=abs(f);

  *w=buttonWidth(b)-2*(px+f);
  *h=buttonHeight(b)-2*(py+f);

  *x+=f+px;
  *y+=f+py;

  if(*w<=1 || *h<=1)
    *w=*h=1;
}

/**
*** buttonFrameSigned()
*** Give the signed framewidth for this button.
**/
int buttonFrameSigned(button_info *b)
{
  if(b->flags&b_Frame)
    return b->framew;
  if(b->flags&b_Container)              /* Containers usually gets 0 relief  */
    return 0;
  while((b=b->parent))
    if(b->c->flags&b_Frame)
      return b->c->framew;
#ifdef DEBUG
  fprintf(stderr,"%s: BUG: No relief width definition?\n",MyName);
#endif
  return 0;
}

/**
*** buttonXPad()
*** Give the x padding for this button
**/
int buttonXPad(button_info *b)
{
  if(b->flags&b_Padding)
    return b->xpad;
  if(b->flags&(b_Container|b_Swallow))  /* Normally no padding for these     */
    return 0;
  while((b=b->parent))
    if(b->c->flags&b_Padding)
      return b->c->xpad;
#ifdef DEBUG
  fprintf(stderr,"%s: BUG: No padding definition?\n",MyName);
#endif
  return 0;
}
      
/**
*** buttonYPad()
*** Give the y padding for this button
**/
int buttonYPad(button_info *b)
{
  if(b->flags&b_Padding)
    return b->ypad;
  if(b->flags&(b_Container|b_Swallow))  /* Normally no padding for these     */
    return 0;
  while((b=b->parent))
    if(b->c->flags&b_Padding)
      return b->c->ypad;
#ifdef DEBUG
  fprintf(stderr,"%s: BUG: No padding definition?\n",MyName);
#endif
  return 0;
}

/**
*** buttonFont()
*** Give the font pointer for this button
**/
XFontStruct *buttonFont(button_info *b)
{
  if(b->flags&b_Font)
    return b->font;
  while((b=b->parent))
    if(b->c->flags&b_Font)
      return b->c->font;
#ifdef DEBUG
  fprintf(stderr,"%s: BUG: No font definition?\n",MyName);
#endif
  return None;
}

#ifdef I18N
/**
*** buttonFontSet()
*** Give the font pointer for this button
**/
XFontSet buttonFontSet(button_info *b)
{
  if(b->flags&b_Font)
    return b->fontset;
  while((b=b->parent))
    if(b->c->flags&b_Font)
      return b->c->fontset;
#ifdef DEBUG
  fprintf(stderr,"%s: BUG: No fontset definition?\n",MyName);
#endif
  return None;
}
#endif

/**
*** buttonFore()
*** Give the foreground pixel of this button
**/
Pixel buttonFore(button_info *b)
{
  if(b->flags&b_Fore)
    return b->fc;
  while((b=b->parent))
    if(b->c->flags&b_Fore)
      return b->c->fc;
#ifdef DEBUG
  fprintf(stderr,"%s: BUG: No foreground definition?\n",MyName);
#endif
  return None;
}

/**
*** buttonBack()
*** Give the background pixel of this button
**/
Pixel buttonBack(button_info *b)
{
  if(b->flags&b_Back)
    return b->bc;
  while((b=b->parent))
    if(b->c->flags&b_Back)
      return b->c->bc;
#ifdef DEBUG
  fprintf(stderr,"%s: BUG: No background definition?\n",MyName);
#endif
  return None;
}

/**
*** buttonHilite()
*** Give the relief pixel of this button
**/
Pixel buttonHilite(button_info *b)
{
  if(b->flags&b_Back)
    return b->hc;
  while((b=b->parent))
    if(b->c->flags&b_Back)
      return b->c->hc;
#ifdef DEBUG
  fprintf(stderr,"%s: BUG: No background definition?\n",MyName);
#endif
  return None;
}

/**
*** buttonShadow()
*** Give the shadow pixel of this button
**/
Pixel buttonShadow(button_info *b)
{
  if(b->flags&b_Back)
    return b->sc;
  while((b=b->parent))
    if(b->c->flags&b_Back)
      return b->c->sc;
#ifdef DEBUG
  fprintf(stderr,"%s: BUG: No background definition?\n",MyName);
#endif
  return None;
}

/**
*** buttonSwallow()
*** Give the swallowing flags for this button
**/
byte buttonSwallow(button_info *b)
{
  byte s=0,t=0;
  if(b->flags&b_Swallow)
    {
      s=b->swallow;
      t=b->swallow_mask;
    }
  while((b=b->parent))
    if(b->c->flags&b_Swallow)
      {
	s&=~(b->c->swallow_mask&~t);
	s|=(b->c->swallow&b->c->swallow_mask&~t);
	t|=b->c->swallow_mask;
      }
  return s;
}

/**
*** buttonJustify()
*** Give the justify flags for this button
**/
byte buttonJustify(button_info *b)
{
  byte j=1,i=0;
  if(b->flags&b_Justify)
    {
      i=b->justify_mask;
      j=b->justify;
    }
  while((b=b->parent))
    if(b->c->flags&b_Justify)
      {
	j&=~(b->c->justify_mask&~i);
	j|=(b->c->justify&b->c->justify_mask&~i);
	i|=b->c->justify_mask;
      }
  return j;
}

/* ---------------------------- button creation ---------------------------- */

/**
*** alloc_buttonlist()
*** Makes sure the list of butten_info's is long enough, if not it reallocates
*** a longer one. This happens in steps of 32. Inital length is 0.
**/
void alloc_buttonlist(button_info *ub,int num)
{
  button_info **bb;
  int i,old;

  if(num>=ub->c->allocated_buttons)
    {
      old=ub->c->allocated_buttons;
      if(num<old || old+32<old)
	{
	  fprintf(stderr,"%s: Too many buttons, integer overflow\n",MyName);
	  exit(1);
	}
      while(ub->c->allocated_buttons<=num)
        ub->c->allocated_buttons+=32;
      bb=(button_info**)
	mymalloc(ub->c->allocated_buttons*sizeof(button_info*));
      for(i=old;i<ub->c->allocated_buttons;i++)
	bb[i]=NULL;
      if(ub->c->buttons)
	{
	  for(i=0;i<old;i++) bb[i]=ub->c->buttons[i];
          free(ub->c->buttons);
	}
      ub->c->buttons=bb;
    }
}

/**
*** alloc_button()
*** Allocates memory for a new button struct. Calles alloc_buttonlist to 
*** assure enough space is present. Also initiates most elements of the struct.
**/
button_info *alloc_button(button_info *ub,int num)
{
  button_info *b;
  if(num>=ub->c->allocated_buttons)
    alloc_buttonlist(ub,num);
  if(ub->c->buttons[num])
    {
      fprintf(stderr,"%s: Allocated button twice, report bug twice\n",MyName);
      exit(2);
    }

  b=(button_info*)mymalloc(sizeof(button_info));
  ub->c->buttons[num]=b;

  b->flags = 0;
  b->swallow = 0;
  b->BWidth = b->BHeight = 1;
  b->parent = ub;
  b->n = -1;

  b->framew = 1;
  b->xpad = 2;
  b->ypad = 4;
  b->w=1;
  b->h=1;
  b->bw=1;

  return(b);
}

/**
*** MakeContainer()
*** Allocs and sets the container-specific fields of a button.
**/
void MakeContainer(button_info *b)
{
  b->c=(container_info*)mymalloc(sizeof(container_info));
  b->flags|=b_Container;
  b->c->buttons=NULL;
  b->c->num_buttons=0;
  b->c->num_rows=0;
  b->c->num_columns=0;
  b->c->allocated_buttons=0;
  b->c->xpos=0;
  b->c->ypos=0;
  if(b->parent != NULL)
    b->c->flags=0;
  else /* This applies to the UberButton */
    {
      b->c->flags=b_Font|b_Padding|b_Frame|b_Back|b_Fore;
      b->c->font_string=strdup("fixed");
      b->c->xpad=2;
      b->c->ypad=4;
      b->c->back=strdup("#908090");
      b->c->fore=strdup("black");
      b->c->framew=2;
    }
}

/* -------------------------- button administration ------------------------ */

/**
*** NumberButtons()
*** Prepare the n fields in each button
**/
void NumberButtons(button_info *b)
{
  int i=-1;
  while(++i<b->c->num_buttons)
    if(b->c->buttons[i])
      {
	b->c->buttons[i]->n=i;
	if(b->c->buttons[i]->flags&b_Container)
	  NumberButtons(b->c->buttons[i]);
      }
}

/**
*** ShuffleButtons()
*** Orders and sizes the buttons in the UberButton, corrects num_rows and
*** num_columns in containers.
**/
void ShuffleButtons(button_info *ub)
{
  int i,j,k,tb,sb;
  int actual_buttons_used,first_avail_button;
  button_info *b;
  container_info *c=ub->c;

  /* Allow for multi-width/height buttons */
  actual_buttons_used = 0;
  for(i=0;i<c->num_buttons;i++)
    actual_buttons_used+=c->buttons[i]->BWidth*c->buttons[i]->BHeight;

  first_avail_button = c->num_buttons;
  alloc_buttonlist(ub,actual_buttons_used);
  c->num_buttons=actual_buttons_used;

  /* Size and create the window */
  if(c->num_rows==0 && c->num_columns==0)
    c->num_rows=2;
  if(c->num_columns==0)
    c->num_columns=1+(c->num_buttons-1)/c->num_rows;
  if(c->num_rows==0)
    c->num_rows=1+(c->num_buttons-1)/c->num_columns;
  while(c->num_rows * c->num_columns < c->num_buttons)
    c->num_columns++;
  while(c->num_rows * c->num_columns >= c->num_buttons + c->num_columns)
    c->num_rows--;

  for(i=0;i<c->num_buttons;i++)
    {
      b=c->buttons[i];
      /* Shuffle subcontainers recursively */
      if(b && b->flags&b_Container)
	ShuffleButtons(b);
      if(b && (b->BHeight>1 || b->BWidth>1))
	{
	  /* If not enough room underneath, give up */
	  if((c->num_rows - (i/c->num_columns)) < b->BHeight)
	    {
	      fprintf(stderr,"%s: Button too tall. Giving up\n",MyName);
	      fprintf(stderr,"Button=%d num_rows=%d bheight=%d h=%d\n",
		      i,c->num_rows,b->BHeight,
		      c->num_rows-(i/c->num_columns));
	      b->BHeight=c->num_rows-(i/c->num_columns);
	    }
	  if((c->num_columns - (i%c->num_columns)) < b->BWidth)
	    {
	      fprintf(stderr,"%s: Button too wide. Giving up.\n",MyName);
	      fprintf(stderr,"Button=%d num_columns=%d bwidth=%d w=%d\n",
		      i,c->num_columns,b->BWidth,
		      c->num_columns-(i%c->num_rows));
	      b->BWidth=c->num_columns-(i%c->num_rows);
	    } 
	  for(k=0;k<b->BHeight;k++)
	    for(j=0;j<b->BWidth;j++)
	      {
		if((j>0)||(k>0))
		  {
		    /* Is there a button in the way for our UberButton? */
 		    if(c->buttons[i+j+k*c->num_columns])
		      {
			first_avail_button = i+j+k*c->num_columns+1;
			/* Find first button not in use, not already claimed,
			   and not be the one we want to swap it with */
			while((first_avail_button<c->num_buttons)&&
			      (first_avail_button == i+j+k*c->num_columns||
			       c->buttons[first_avail_button]||
			       (button_belongs_to(ub,first_avail_button)<=i &&
			       button_belongs_to(ub,first_avail_button)!=-1)))
			  first_avail_button++;

			if(first_avail_button >= c->num_buttons)
			  {
			    fprintf(stderr,"%s: Button confusion! Quitting\n",
				    MyName);
			    exit(1);
			  }

			/* The patched swapping here was done by
			   palme@elphy.irz.hu-berlin.de */

			/* Slide the offending buttons nearer the end */
			tb=first_avail_button;
			sb=tb-1;
			while(sb>=i+j+k*c->num_columns)
			  {
			    /* Skip buttons which belongs to UberButtons */
			    while(!c->buttons[sb]) sb--;
			    c->buttons[tb--]=c->buttons[sb--];
			    while(!c->buttons[tb]) tb--;
			  }
			c->buttons[i+j+k*c->num_columns]=NULL;
		      }
		  }
	      }
	}
    }
}

/* ----------------------------- button iterator --------------------------- */

/**
*** NextButton()
*** Iterator to traverse buttontree. Start it with first argument a pointer
*** to the root UberButton, and the index int set to -1. Each subsequent call
*** gives the pointer to uberbutton, button and button index within uberbutton.
*** If all, also returns containers, apart from the UberButton.
**/
button_info *NextButton(button_info **ub,button_info **b,int *i,int all)
{
  /* Get next button */
  (*i)++;
  /* Skip fake buttons */
  while((*i)<(*ub)->c->num_buttons && !(*ub)->c->buttons[*i])
    (*i)++;
  /* End of contained buttons */
  if((*i)>=(*ub)->c->num_buttons)
    {
      *b=*ub;
      *ub=(*b)->parent;
      /* End of the world as we know it */
      if(!(*ub))
	{
	  *b=NULL;
	  return *b;
	}
      *i=(*b)->n;
      if((*i)>=(*ub)->c->num_buttons)
	{
	  fprintf(stderr,"%s: BUG: Couldn't return to uberbutton\n",MyName);
	  exit(2);
	}
      NextButton(ub,b,i,all);
      return *b;
    }
  *b=(*ub)->c->buttons[*i];

  /* Found new container */
  if((*b)->flags & b_Container)
    {
      *i=-1;
      *ub=*b;
      if(!all)
	NextButton(ub,b,i,all);
      return *b;
    }
  return *b;
}

/* --------------------------- button navigation --------------------------- */

/**
*** button_belongs_to()
*** Function that finds out which button a given position belongs to.
*** Returns -1 is not part of any, button if a proper button.
**/
int button_belongs_to(button_info *ub,int button)
{
  int x,y,xx,yy;
  button_info *b;
  if(!ub || button<0 || button>ub->c->num_buttons)
    return -1;
  if(ub->c->buttons[button])
    return button;
  yy=button/ub->c->num_columns;
  xx=button%ub->c->num_columns;
  for(y=yy;y>=0;y--)
    for(x=xx;x>=0;x--)
      {
	b=ub->c->buttons[x+y*ub->c->num_columns];
	if(b && (x+b->BWidth > xx) && (y+b->BHeight > yy))
	  {
	    return x+y*ub->c->num_columns;
	  }
      }
  return -1;
}

/**
*** select_button()
*** Given (x,y) and uberbutton, returns pointer to referred button, or NULL
**/
button_info *select_button(button_info *ub,int x,int y)
{
  int i;
  button_info *b;
  if(!(ub->flags&b_Container))
    return ub;

  x-=buttonXPad(ub)+buttonFrame(ub);
  y-=buttonYPad(ub)+buttonFrame(ub);

  if(x >= ub->c->ButtonWidth * ub->c->num_columns || x<0 ||
     y >= ub->c->ButtonHeight * ub->c->num_rows || y<0)
    return ub;

  i=x/ub->c->ButtonWidth + (y/ub->c->ButtonHeight)*ub->c->num_columns;
  i=button_belongs_to(ub,i);
  if(i==-1)return ub;
  b=ub->c->buttons[i];
  return select_button(b,x-(i%ub->c->num_columns)*ub->c->ButtonWidth,
		       y-(i/ub->c->num_columns)*ub->c->ButtonHeight);
}