summaryrefslogtreecommitdiff
path: root/app/fvwm/modules/FvwmWinList/ButtonArray.c
blob: e9b54b71419cc793509bc4da16c789fb0b39c462 (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
500
/* FvwmWinList Module for Fvwm.
 *
 *  Copyright 1994,  Mike Finger (mfinger@mermaid.micro.umn.edu or
 *                               Mike_Finger@atk.com)
 *
 * The functions in this source file that are the original work of Mike Finger.
 *
 * 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.
 *
 *  Things to do:  Convert to C++  (In Progress)
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <X11/Xlib.h>

#include "../../libs/fvwmlib.h"

#include "ButtonArray.h"
#include "Mallocs.h"


extern XFontStruct *ButtonFont;
extern Display *dpy;
extern Window win;
extern GC shadow[MAX_COLOUR_SETS],hilite[MAX_COLOUR_SETS];
extern GC graph[MAX_COLOUR_SETS],background[MAX_COLOUR_SETS];
extern int LeftJustify, TruncateLeft, ShowFocus;

extern long CurrentDesk;
extern int ShowCurrentDesk;


/*************************************************************************
 *                                                                       *
 *  Button handling functions and procedures                             *
 *                                                                       *
 *************************************************************************/

/* -------------------------------------------------------------------------
   ButtonNew - Allocates and fills a new button structure
   ------------------------------------------------------------------------- */
Button *ButtonNew(char *title, FvwmPicture *p, int up)
{
  Button *new;

  new = (Button *)safemalloc(sizeof(Button));
  new->title = safemalloc(strlen(title)+1);
  strcpy(new->title, title);
  if (p != NULL)
  {
    new->p.picture = p->picture;
    new->p.mask = p->mask;
    new->p.width = p->width;
    new->p.height = p->height;
    new->p.depth = p->depth;
  }
  else

  new->p.picture = 0;

  new->up = up;
  new->next = NULL;
  new->needsupdate = 1;

  return new;
}

/******************************************************************************
  InitArray - Initialize the arrary of buttons
******************************************************************************/
void InitArray(ButtonArray *array,int x,int y,int w,int h)
{
  array->count=0;
  array->head=array->tail=NULL;
  array->x=x;
  array->y=y;
  array->w=w;
  array->h=h;
}

/******************************************************************************
  UpdateArray - Update the array specifics.  x,y, width, height
******************************************************************************/
void UpdateArray(ButtonArray *array,int x,int y,int w, int h)
{
  Button *temp;

  if (x!=-1) array->x=x;
  if (y!=-1) array->y=y;
  if (w!=-1) array->w=w;
  if (h!=-1) array->h=h;
  for(temp=array->head;temp!=NULL;temp=temp->next) temp->needsupdate=1;
}

/******************************************************************************
  AddButton - Allocate space for and add the button to the bottom
******************************************************************************/
int AddButton(ButtonArray *array, char *title, FvwmPicture *p, int up)
{
  Button *new;

  new = ButtonNew(title, p, up);
  if (array->head == NULL)
  {
    array->head = array->tail = new;
  }
  else
  {
    array->tail->next = new;
    array->tail = new;
  }
  array->count++;

/* in Taskbar this replaces below  ArrangeButtonArray (array);
*/

  new->tw=XTextWidth(ButtonFont,title,strlen(title));
  new->truncatewidth=0;
  new->next=NULL;
  new->needsupdate=1;
  new->set=0;

  return (array->count-1);
}

/******************************************************************************
  UpdateButton - Change the name/stae of a button
******************************************************************************/
int UpdateButton(ButtonArray *array, int butnum, char *title, int up)
{
  Button *temp;

  temp=find_n(array,butnum);
  if (temp!=NULL)
  {
    if (title!=NULL)
    {
      temp->title=(char *)saferealloc(temp->title,strlen(title)+1);
      strcpy(temp->title,title);
      temp->tw=XTextWidth(ButtonFont,title,strlen(title));
      temp->truncatewidth = 0;
    }
    if (up!=-1) temp->up=up;
  } else return -1;
  temp->needsupdate=1;
  return 1;
}

/* -------------------------------------------------------------------------
   UpdateButtonPicture - Change the picture of a button
   ------------------------------------------------------------------------- */
int UpdateButtonPicture(ButtonArray *array, int butnum, FvwmPicture *p)
{
  Button *temp;
  temp=find_n(array,butnum);
  if (temp == NULL) return -1;
  if (temp->p.picture != p->picture || temp->p.mask != p->mask)
  {
    temp->p.picture = p->picture;
    temp->p.mask = p->mask;
    temp->p.width = p->width;
    temp->p.height = p->height;
    temp->p.depth = p->depth;
    temp->needsupdate = 1;
  }
  return 1;
}

/******************************************************************************
  UpdateButtonSet - Change colour set of a button between odd and even
******************************************************************************/
int UpdateButtonSet(ButtonArray *array, int butnum, int set)
{
  Button *btn;

  btn=find_n(array, butnum);
  if (btn != NULL)
  {
    if ((btn->set & 1) != set)
    {
      btn->set = (btn->set & 2) | set;
      btn->needsupdate = 1;
    }
  } else return -1;
  return 1;
}

/******************************************************************************
  UpdateButtonDesk - Change desk of a button
******************************************************************************/
int UpdateButtonDesk(ButtonArray *array, int butnum, long desk )
{
  Button *btn;

  btn = find_n(array, butnum);
  if (btn != NULL)
  {
    btn->desk = desk;
  } else return -1;
  return 1;
}


/******************************************************************************
  RemoveButton - Delete a button from the list
******************************************************************************/
void RemoveButton(ButtonArray *array, int butnum)
{
  Button *temp,*temp2;

  if (butnum==0)
  {
    temp2=array->head;
    temp=array->head=array->head->next;
  }
  else
  {
    temp=find_n(array,butnum-1);
    if (temp==NULL) return;
    temp2=temp->next;
    temp->next=temp2->next;
  }

  if (array->tail==temp2) array->tail=temp;

  FreeButton(temp2);

  if (temp!=array->head) temp=temp->next;
  for(;temp!=NULL;temp=temp->next) temp->needsupdate=1;
}

/******************************************************************************
  find_n - Find the nth button in the list (Use internally)
******************************************************************************/
Button *find_n(ButtonArray *array, int n)
{
  Button *temp;
  int i;

  temp=array->head;
  for(i=0;i<n && temp!=NULL;i++,temp=temp->next);
  return temp;
}

/******************************************************************************
  FreeButton - Free space allocated to a button
******************************************************************************/
void FreeButton(Button *ptr)
{
  if (ptr != NULL) {
    if (ptr->title!=NULL) free(ptr->title);
    free(ptr);
  }
}

/******************************************************************************
  FreeAllButtons - Free the whole array of buttons
******************************************************************************/
void FreeAllButtons(ButtonArray *array)
{
Button *temp,*temp2;
  for(temp=array->head;temp!=NULL;)
  {
    temp2=temp;
    temp=temp->next;
    FreeButton(temp2);
  }
}

/******************************************************************************
  DoButton - Draw the specified button.  (Used internally)
******************************************************************************/
void DoButton(Button *button, int x, int y, int w, int h)
{
  int up,Fontheight,newx,set;
  GC topgc;
  GC bottomgc;
  char *string;
  XGCValues gcv;
  unsigned long gcm;
  XFontStruct *font;

  up=button->up;
  set=button->set;
  topgc = up ? hilite[set] : shadow[set];
  bottomgc = up ? shadow[set] : hilite[set];
  font = ButtonFont;

  gcm = GCFont;
  gcv.font = font->fid;
  XChangeGC(dpy, graph[set], gcm, &gcv);


  Fontheight=ButtonFont->ascent+ButtonFont->descent;

 /*? XClearArea(dpy,win,x,y,w,h,False);*/
  XFillRectangle(dpy,win,background[set],x,y,w,h+1);

  if ((button->p.picture != 0)/* &&
      (w + button->p.width + w3p + 3 > MIN_BUTTON_SIZE)*/) {

    gcm = GCClipMask|GCClipXOrigin|GCClipYOrigin;
    gcv.clip_mask = button->p.mask;
    gcv.clip_x_origin = x + 4;
    gcv.clip_y_origin = y + ((h-button->p.height) >> 1);
    XChangeGC(dpy, hilite[set], gcm, &gcv);
    XCopyArea(dpy, button->p.picture, win, hilite[set], 0, 0,
                   button->p.width, button->p.height,
                   gcv.clip_x_origin, gcv.clip_y_origin);
    gcm = GCClipMask;
    gcv.clip_mask = None;
    XChangeGC(dpy, hilite[set], gcm, &gcv);

    newx = button->p.width+6;
  }
  else
  {
    if (LeftJustify)
      newx=4;
    else
      newx=max((w-button->tw)/2,4);
  }

  string=button->title;

  if (!LeftJustify) {
    if (TruncateLeft && (w-button->tw)/2 < 4) {
      if (button->truncatewidth == w)
	string=button->truncate_title;
      else {
	string=button->title;
	while(*string && (w-XTextWidth(ButtonFont,string,strlen(string)))/2 < 4)
	    string++;
	button->truncatewidth = w;
	button->truncate_title=string;
      }
    }
  }
  XDrawString(dpy,win,graph[set],x+newx,y+3+ButtonFont->ascent,string,strlen(string));
  button->needsupdate=0;

  /* Draw relief last, don't forget that XDrawLine doesn't do the last pixel */
  XDrawLine(dpy,win,topgc,x,y,x+w-1,y);
  XDrawLine(dpy,win,topgc,x+1,y+1,x+w-2,y+1);
  XDrawLine(dpy,win,topgc,x,y+1,x,y+h+1);
  XDrawLine(dpy,win,topgc,x+1,y+2,x+1,y+h);
  XDrawLine(dpy,win,bottomgc,x+1,y+h,x+w,y+h);
  XDrawLine(dpy,win,bottomgc,x+2,y+h-1,x+w-1,y+h-1);
  XDrawLine(dpy,win,bottomgc,x+w-1,y,x+w-1,y+h);
  XDrawLine(dpy,win,bottomgc,x+w-2,y+1,x+w-2,y+h-1);

}

/******************************************************************************
  DrawButtonArray - Draw the whole array (all=1), or only those that need.
******************************************************************************/
void DrawButtonArray(ButtonArray *barray, int all)
{
  Button *btn;
  int i = 0;		/* buttons displayed */

  for(btn = barray->head; btn != NULL; btn = btn->next)
  {
    if((!ShowCurrentDesk) || ( btn->desk == CurrentDesk ) )
    {
      if (btn->needsupdate || all)
      {
        DoButton
        (
  		btn,barray->x,
  		barray->y+(i*(barray->h+1)),
  		barray->w,barray->h
    	);
      }
      i++;
    }
  }
}

/******************************************************************************
  SwitchButton - Alternate the state of a button
******************************************************************************/
void SwitchButton(ButtonArray *array, int butnum)
{
  Button *btn;

  btn = find_n(array, butnum);
  btn->up =!btn->up;
  btn->needsupdate=1;
  DrawButtonArray(array, 0);
}

/* -------------------------------------------------------------------------
   RadioButton - Enable button i and verify all others are disabled
   ------------------------------------------------------------------------- */
void RadioButton(ButtonArray *array, int butnum)
{
  Button *temp;
  int i;

  for(temp=array->head,i=0; temp!=NULL; temp=temp->next,i++)
  {
    if (i == butnum)
    {
      if (ShowFocus && temp->up)
      {
        temp->up = 0;
        temp->needsupdate=1;
      }
      if (!(temp->set & 2))
      {
        temp->set |= 2;
        temp->needsupdate=1;
      }
    }
    else
    {
      if (ShowFocus && !temp->up)
      {
        temp->up = 1;
        temp->needsupdate = 1;
      }
      if (temp->set & 2)
      {
        temp->set &= 1;
        temp->needsupdate = 1;
      }
    }
  }
}

/******************************************************************************
  WhichButton - Based on x,y which button was pressed
******************************************************************************/
int WhichButton(ButtonArray *array,int x, int y)
{
  int num;

  num=y/(array->h+1);
  if (x<array->x || x>array->x+array->w || num<0 || num>array->count-1) num=-1;

  /* Current Desk Hack */

  if(ShowCurrentDesk)
  {
    Button *temp;
    int i, n;

    temp=array->head;
    for(i=0, n = 0;n < (num + 1) && temp != NULL;temp=temp->next, i++)
    {
       if(temp->desk == CurrentDesk)
         n++;
    }
    num = i-1;
  }
  return(num);
}

/******************************************************************************
  ButtonName - Return the name of the button
******************************************************************************/
char *ButtonName(ButtonArray *array, int butnum)
{
  Button *temp;

  temp=find_n(array,butnum);
  return temp->title;
}

/******************************************************************************
  PrintButtons - Print the array of button names to the console. (Debugging)
******************************************************************************/
void PrintButtons(ButtonArray *array)
{
  Button *temp;

  ConsoleMessage("List of Buttons:\n");
  for(temp=array->head;temp!=NULL;temp=temp->next)
    ConsoleMessage("   %s is %s\n",temp->title,(temp->up) ? "Up":"Down");
}

#if 0
/******************************************************************************
  ButtonArrayMaxWidth - Calculate the width needed for the widest title
******************************************************************************/
int ButtonArrayMaxWidth(ButtonArray *array)
{
Button *temp;
int x=0;
  for(temp=array->head;temp!=NULL;temp=temp->next)
    x=max(temp->tw,x);
  return x;
}
#endif