File: menu.c

package info (click to toggle)
libcdk 4.9.9-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,876 kB
  • ctags: 2,249
  • sloc: ansic: 28,664; sh: 6,966; makefile: 211; cpp: 42
file content (601 lines) | stat: -rw-r--r-- 16,750 bytes parent folder | download | duplicates (2)
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
#include "cdk.h"

/*
 * $Author: glovem $
 * $Date: 1997/04/25 12:50:55 $
 * $Revision: 1.51 $
 */

/*
 * Declare file local prototypes.
 */
void cleanUpMenu (CDKMENU *menu);

/*
 * This creates a new menu widget.
 */
CDKMENU *newCDKMenu (CDKSCREEN *cdkscreen, char *menulist[MAX_MENU_ITEMS][MAX_SUB_ITEMS], int menuItems, int *subsize, int *menuloc, int menuPos, chtype titleAttr, chtype subtitleAttr)
{
   /* Declare local variables.  */
   CDKMENU *menu	= (CDKMENU *)malloc (sizeof (CDKMENU));
   int rightcount	= menuItems-1;
   int rightloc		= (cdkscreen->window->_maxx) - 1;
   int leftloc		= 0;
   int maxwidth[MAX_MENU_ITEMS];
   int x, y, max, junk;

   /* Start making a copy of the information. */
   rightcount			= menuItems-1;
   menu->parent			= cdkscreen->window;
   menu->menuItems		= menuItems;
   menu->titleAttr		= titleAttr;
   menu->subtitleAttr		= subtitleAttr;
   menu->currentTitle		= 0;
   menu->currentSubtitle	= 0;
   menu->lastSelection		= -1;
   menu->preProcessFunction	= (PROCESSFN)NULL;
   menu->preProcessData		= (void *)NULL;
   menu->postProcessFunction	= (PROCESSFN)NULL;
   menu->postProcessData	= (void *)NULL;
   menu->menuPos		= menuPos;
   menu->exitType		= vNEVER_ACTIVATED;

   /* Create the pull down menus. */
   for (x=0; x < menuItems; x++)
   {
      max = -1;
      for (y=1; y < subsize[x] ; y++)
      {
         if (menuloc[x] == LEFT)
         {
            menu->sublist[x][y-1] = char2Chtype (menulist[x][y], &menu->sublistLen[x][y-1], &junk);
            max	= MAXIMUM (max, menu->sublistLen[x][y-1]);
         }
         else
         {
            menu->sublist[rightcount][y-1] = char2Chtype (menulist[x][y], &menu->sublistLen[rightcount][y-1], &junk);
            max	= MAXIMUM (max, menu->sublistLen[rightcount][y-1]);
         }
      }

      if (menuloc[x] == LEFT)
      {
        /*
         * Its a menu item on the left, add it to the left side
         * of the menu.
         */
         menu->title[x]		= char2Chtype (menulist[x][0], &menu->titleLen[x], &junk);
         menu->subsize[x]	= subsize[x] - 1;
         maxwidth[x] 		= max+1;
         if (menu->menuPos == BOTTOM)
         {
            menu->titleWin[x]	= subwin (cdkscreen->window, 1, menu->titleLen[x]+2, LINES-1, leftloc);
            menu->pullWin[x]	= subwin (cdkscreen->window, subsize[x]+1, max+2, LINES-subsize[x]-2, leftloc);
         }
         else
         {
            menu->titleWin[x]	= subwin (cdkscreen->window, 1, menu->titleLen[x]+2, 0, leftloc);
            menu->pullWin[x]	= subwin (cdkscreen->window, subsize[x]+1, max+2, 1, leftloc);
         }
         leftloc += menu->titleLen[x] + 1;

         /* Allow the windows to use the keypad. */
         keypad (menu->titleWin[x], TRUE);
         keypad (menu->pullWin[x], TRUE);
      }
      else 
      {
        /*
         * Its a menu item on the right, add it to the right side
         * of the menu.
         */
         rightloc			-= max + 3;
         menu->title[rightcount]	= char2Chtype (menulist[x][0], &menu->titleLen[rightcount], &junk);
         menu->subsize[rightcount]	= subsize[x] - 1;
         maxwidth[rightcount] 		= max+1;
         if (menu->menuPos == BOTTOM)
         {
            menu->titleWin[rightcount]	= subwin (cdkscreen->window, 1, menu->titleLen[x]+2, LINES-1, rightloc);
            menu->pullWin[rightcount]	= subwin (cdkscreen->window, subsize[x]+1, max+2, LINES-subsize[x]-2, rightloc);
         }
         else
         {
            menu->titleWin[rightcount]	= subwin (cdkscreen->window, 1, menu->titleLen[x]+2, 0, rightloc);
            menu->pullWin[rightcount]	= subwin (cdkscreen->window, subsize[x]+1, max+2, 1, rightloc);
         }

         /* Allow the windows to use the keypad. */
         keypad (menu->titleWin[rightcount], TRUE);
         keypad (menu->pullWin[rightcount], TRUE);
         rightcount--;
      }
   }

   /* Clean the key bindings. */
   cleanCDKObjectBindings (vMENU, menu);

   /* Register this baby. */
   registerCDKObject (cdkscreen, vMENU, menu);

   /* Return the menu object. */
   return (menu);
}

/*
 * This activates the CDK Menu.
 */
int activateCDKMenu (CDKMENU *menu, chtype *actions)
{
   /* Declare local variables. */
   chtype input;
   int ret;

   /* Draw in the screen. */
   refreshCDKScreen (menu->screen);

   /* Display the menu titles. */
   drawCDKMenu(menu);

   /* Highlight the current title and window. */
   drawCDKMenuSubwin (menu);

   /* If the input string is NULL, this is an interactive activate. */
   if (actions == (chtype *)NULL)
   {
      /* Start taking input from the keyboard. */
      for (;;)
      {
         input = wgetch (menu->titleWin[menu->currentTitle]);

         /* Inject the character into the widget. */
         ret = injectCDKMenu (menu, input);
         if (menu->exitType != vEARLY_EXIT)
         {
            return ret;
         }
      }
   }  
   else
   {
      int count = chlen (actions);
      int x = 0;

      for (x=0; x < count; x++)
      {
         ret = injectCDKMenu (menu, actions[x]);
         if (menu->exitType != vEARLY_EXIT)
         {
            return ret;
         }
      }
   }

   /* Set the exit type and return. */
   menu->exitType = vEARLY_EXIT;
   return -1;
}

/*
 * This injects a character into the menu widget.
 */
int injectCDKMenu (CDKMENU *menu, chtype input)
{
   /* Declare local variables. */
   int ppReturn = 1;

   /* Set the exit type. */
   menu->exitType = vEARLY_EXIT;

   /* Check if there is a pre-process function to be called. */
   if (menu->preProcessFunction != (PROCESSFN)NULL)
   {
      /* Call the pre-process function. */
      ppReturn = ((PROCESSFN)(menu->preProcessFunction)) (vMENU, menu, menu->preProcessData, input);
   }

   /* Should we continue? */
   if (ppReturn != 0)
   {
      /* Check for key bindings. */
      if (checkCDKObjectBind (vMENU, menu, input) != 0)
      {
         menu->exitType = vESCAPE_HIT;
         return -1;
      }
      else
      {
         switch (input)
         {
            case KEY_LEFT :
                 /* Erase the menu sub-window */
                 eraseCDKMenuSubwin (menu);
                 refreshCDKScreen (menu->screen);
   
                 /* Set the values. */
                 menu->currentSubtitle = 0;
                 if (menu->currentTitle == 0)
                 {
                    menu->currentTitle = menu->menuItems - 1;
                 }
                 else
                 {
                    menu->currentTitle--;
                 }
   
                 /* Draw the new menu sub-window. */
                 drawCDKMenuSubwin (menu);
                 break;
   
            case KEY_RIGHT : case KEY_TAB :
                 /* Erase the menu sub-window. */
                 eraseCDKMenuSubwin (menu);
                 refreshCDKScreen (menu->screen);
   
                 /* Set the values. */
                 menu->currentSubtitle = 0;
                 if (menu->currentTitle == menu->menuItems-1)
                 {
                       menu->currentTitle = 0;
                 }
                 else
                 {
                    menu->currentTitle++;
                 }
   
                 /* Draw the new menu sub-window. */
                 drawCDKMenuSubwin (menu);
                 break;
   
            case KEY_UP :
                 /* Erase the old subtitle. */
                 writeChtype (menu->pullWin[menu->currentTitle],
   				1, menu->currentSubtitle+1,
   				menu->sublist[menu->currentTitle][menu->currentSubtitle],
   				HORIZONTAL,
   				0, menu->sublistLen[menu->currentTitle][menu->currentSubtitle]);
   
                 /* Set the values. */
                 if (menu->currentSubtitle == 0)
                 {
                    menu->currentSubtitle	= menu->subsize[menu->currentTitle] - 1;
                 }
                 else
                 {
                    menu->currentSubtitle--;
                 }
   
                 /* Draw the new sub-title. */
                 writeChtypeAttrib (menu->pullWin[menu->currentTitle],
   					1, menu->currentSubtitle+1,
   					menu->sublist[menu->currentTitle][menu->currentSubtitle],
   					menu->subtitleAttr,
   					HORIZONTAL,
   					0, menu->sublistLen[menu->currentTitle][menu->currentSubtitle]);
                 wrefresh (menu->pullWin[menu->currentTitle]);
                 break;
   
            case KEY_DOWN : case SPACE :
                 /* Erase the old subtitle. */
                 writeChtype (menu->pullWin[menu->currentTitle],
   				1, menu->currentSubtitle+1,
   				menu->sublist[menu->currentTitle][menu->currentSubtitle],
   				HORIZONTAL,
   				0, menu->sublistLen[menu->currentTitle][menu->currentSubtitle]);
   
                 /* Set the values. */
                 if (menu->currentSubtitle == menu->subsize[menu->currentTitle]-1)
                 {
                    menu->currentSubtitle = 0;
                 }
                 else
                 {
                    menu->currentSubtitle++;
                 }
   
                 /* Draw the new sub-title. */
                 writeChtypeAttrib (
   				menu->pullWin[menu->currentTitle],
   				1, menu->currentSubtitle+1,
   				menu->sublist[menu->currentTitle][menu->currentSubtitle],
   				menu->subtitleAttr,
   				HORIZONTAL,
   				0, menu->sublistLen[menu->currentTitle][menu->currentSubtitle]);
                 wrefresh (menu->pullWin[menu->currentTitle]);
                 break;
    
            case KEY_RETURN : case KEY_ENTER :
                 cleanUpMenu (menu);
                 menu->exitType = vNORMAL;
                 menu->lastSelection = ((menu->currentTitle * 100) + menu->currentSubtitle);
                 return menu->lastSelection;
                 break;
   
            case KEY_ESC :
                 cleanUpMenu (menu);
                 menu->exitType = vESCAPE_HIT;
                 menu->lastSelection = -1;
                 return menu->lastSelection;
                 break;
        
            case CDK_REFRESH :
                 eraseCDKScreen (menu->screen);
                 refreshCDKScreen (menu->screen);
                 break;
         }
      }

      /* Should we call a post-process? */
      if (menu->postProcessFunction != (PROCESSFN)NULL)
      {
         ((PROCESSFN)(menu->postProcessFunction)) (vMENU, menu, menu->postProcessData, input);
      }
   }

   /* Set the exit type and exit */
   menu->exitType = vEARLY_EXIT;
   return -1;
}

/*
 * This draws a menu item subwindow.
 */
void drawCDKMenuSubwin (CDKMENU *menu)
{
   /* Declare local variables. */
   int x;

   /* Box the window. */
   box (menu->pullWin[menu->currentTitle], ACS_VLINE, ACS_HLINE);
   if (menu->menuPos == BOTTOM)
   {
      mvwaddch (menu->pullWin[menu->currentTitle],
		menu->subsize[menu->currentTitle]+1, 0, ACS_LTEE);
   }
   else
   {
      mvwaddch (menu->pullWin[menu->currentTitle], 0, 0, ACS_LTEE);
   }

   /* Draw in the items. */
   for (x=0; x < menu->subsize[menu->currentTitle] ; x++)
   {
      writeChtype (menu->pullWin[menu->currentTitle],
			1, x+1,
			menu->sublist[menu->currentTitle][x],
			HORIZONTAL,
			0, menu->sublistLen[menu->currentTitle][x]);
   }

   /* Highlight the current sub-menu item. */
   writeChtypeAttrib (menu->pullWin[menu->currentTitle],
			1, menu->currentSubtitle+1,
			menu->sublist[menu->currentTitle][menu->currentSubtitle],
			menu->subtitleAttr, HORIZONTAL, 0,
			menu->sublistLen[menu->currentTitle][menu->currentSubtitle]);
   touchwin (menu->pullWin[menu->currentTitle]);
   wrefresh (menu->pullWin[menu->currentTitle]);

   /* Highlight the title. */
   writeChtypeAttrib (menu->titleWin[menu->currentTitle],
			0, 0, menu->title[menu->currentTitle],
			menu->titleAttr, HORIZONTAL, 0,
			menu->titleLen[menu->currentTitle]);
   touchwin (menu->titleWin[menu->currentTitle]);
   wrefresh (menu->titleWin[menu->currentTitle]);
}

/*
 * This erases a menu item subwindow.
 */
void eraseCDKMenuSubwin (CDKMENU *menu)
{
   eraseCursesWindow (menu->pullWin[menu->currentTitle]);

   /* Redraw the sub-menu title. */
   writeChtype (menu->titleWin[menu->currentTitle],
		0, 0, menu->title[menu->currentTitle],
		HORIZONTAL,
		0, menu->titleLen[menu->currentTitle]);
   touchwin (menu->titleWin[menu->currentTitle]);
   wrefresh (menu->titleWin[menu->currentTitle]);
}

/*
 * This function draws the menu.
 */
void drawCDKMenu (CDKMENU *menu)
{
   /* Declare local variables. */
   int x = 0;

   /* Erase the old object. */
   /*eraseCDKMenu (menu);*/

   /* Draw in the menu titles. */
   for (x=0; x < menu->menuItems; x++)
   {
      writeChtype (menu->titleWin[x], 0, 0, menu->title[x], HORIZONTAL, 0, menu->titleLen[x]);
      touchwin (menu->titleWin[x]);
      wrefresh (menu->titleWin[x]);
   }
}

/*
 * This sets the background color of the widget.
 */ 
void setCDKMenuBackgroundColor (CDKMENU *menu, char *color)
{
   chtype *holder = (chtype *)NULL;
   int x, junk1, junk2;

   /* Make sure the color isn't NULL. */
   if (color == (char *)NULL)
   {
      return;
   }

   /* Convert the value of the environment variable to a chtype. */
   holder = char2Chtype (color, &junk1, &junk2);

   /* Set the widgets background color. */
   for (x=0; x < menu->menuItems; x++)
   {
      wbkgd (menu->titleWin[x], holder[0]);
      wbkgd (menu->pullWin[x], holder[0]);
   }

   /* Clean up. */
   freeChtype (holder);
}

/*
 * This function destroys a menu widget.
 */
void destroyCDKMenu (CDKMENU *menu)
{
   int x, y;

   /* Erase the object. */
   eraseCDKMenu (menu);

   /* Clean up both the winodws and the char pointers. */
   for (x=0; x < menu->menuItems; x++)
   {
      /* Clean the windows. */
      deleteCursesWindow (menu->titleWin[x]);
      deleteCursesWindow (menu->pullWin[x]);

      /* Delete the character pointers. */
      freeChtype (menu->title[x]);
      for (y=0; y < menu->subsize[x] ; y++)
      {
         freeChtype (menu->sublist[x][y]);
      }
   }

   /* Unregister this object. */
   unregisterCDKObject (vMENU, menu);

   /* Finish cleaning up. */
   free (menu);
}

/*
 * This function erases the menu widget from the screen.
 */
void eraseCDKMenu (CDKMENU *menu)
{
   /* Declare local variables. */
   int x = 0;

   /* Erase the menu items. */
   for (x=0 ; x < menu->menuItems; x++)
   {
      werase (menu->titleWin[x]);
      wrefresh (menu->titleWin[x]);
      werase (menu->pullWin[x]);
      wrefresh (menu->pullWin[x]);
   }
}

/*
 * This sets multiple features of the menu.
 */
void setCDKMenu (CDKMENU *menu, int menuItem, int subMenuItem, chtype titleHighlight, chtype subTitleHighlight)
{
   setCDKMenuCurrentItem (menu, menuItem, subMenuItem);
   setCDKMenuTitleHighlight (menu, titleHighlight);
   setCDKMenuSubTitleHighlight (menu, subTitleHighlight);
}

/*
 * This sets the current menu itme to highlight.
 */
void setCDKMenuCurrentItem (CDKMENU *menu, int menuitem, int submenuitem)
{
   /* Set the menu item position values. */
   if (menuitem < 0)
   {
      menu->currentTitle = 0;
   }
   else if (menuitem >= menu->menuItems)
   {
      menu->currentTitle = menu->menuItems - 1;
   }
   else
   {
      menu->currentTitle = menuitem;
   }
   if (submenuitem < 0)
   {
      menu->currentSubtitle = 0;
   }
   else if (submenuitem >= menu->subsize[menu->currentTitle])
   {
      menu->currentSubtitle = menu->subsize[menu->currentTitle]-1;
   }
   else
   {
      menu->currentSubtitle = submenuitem;
   }
}
void getCDKMenuCurrentItem (CDKMENU *menu, int *menuItem, int *subMenuItem)
{
   (*menuItem)		= menu->currentTitle;
   (*subMenuItem)	= menu->currentSubtitle;
}

/*
 * This sets the attribute of the menu titles.
 */
void setCDKMenuTitleHighlight (CDKMENU *menu, chtype highlight)
{
   menu->titleAttr = highlight;
}
chtype getCDKMenuTitleHighlight (CDKMENU *menu)
{
   return menu->titleAttr;
}

/*
 * This sets the attribute of the sub-title.
 */
void setCDKMenuSubTitleHighlight (CDKMENU *menu, chtype highlight)
{
   menu->subtitleAttr = highlight;
}
chtype getCDKMenuSubTitleHighlight (CDKMENU *menu)
{
   return menu->subtitleAttr;
}

/* 
 * This exits the menu.
 */
void cleanUpMenu (CDKMENU *menu)
{
   /* Erase the sub-menu. */
   eraseCDKMenuSubwin (menu);
   wrefresh (menu->pullWin[menu->currentTitle]);

   /* Refresh the screen. */
   refreshCDKScreen (menu->screen);
}

/*
 * This function sets the pre-process function.
 */
void setCDKMenuPreProcess (CDKMENU *menu, PROCESSFN callback, void *data)
{
   menu->preProcessFunction = callback;
   menu->preProcessData = data;
}
 
/*
 * This function sets the post-process function.
 */
void setCDKMenuPostProcess (CDKMENU *menu, PROCESSFN callback, void *data)
{
   menu->postProcessFunction = callback;
   menu->postProcessData = data;
}