File: menu.c

package info (click to toggle)
libcdk5 5.0.20050424-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,504 kB
  • ctags: 2,620
  • sloc: ansic: 29,645; sh: 4,283; makefile: 634; cpp: 42; sed: 40
file content (614 lines) | stat: -rw-r--r-- 14,527 bytes parent folder | download
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
#include <cdk_int.h>

/*
 * $Author: tom $
 * $Date: 2004/08/30 00:26:17 $
 * $Revision: 1.93 $
 */

#define TITLELINES 1

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

DeclareCDKObjects(MENU, Menu, setCdk, Int);

/*
 * 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)
{
   CDKMENU *menu	= 0;
   int rightcount	= menuItems - 1;
   int rightloc		= getmaxx(cdkscreen->window);
   int leftloc		= 0;
   int x, y, max, junk;
   int xpos		= getbegx(cdkscreen->window);
   int ypos		= getbegy(cdkscreen->window);
   int ymax		= getmaxy(cdkscreen->window);

   if ((menu = newCDKObject(CDKMENU, &my_funcs)) == 0)
      return (0);

   /* Start making a copy of the information. */
   ScreenOf(menu)		= cdkscreen;
   ObjOf(menu)->box		= FALSE;
   ObjOf(menu)->acceptsFocus    = FALSE;
   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->menuPos		= menuPos;
   initExitType(menu);

   /* Create the pull down menus. */
   for (x=0; x < menuItems; x++)
   {
      int x1 = (menuloc[x] == LEFT) ? x : rightcount--;
      int x2;
      int y1 = (menuPos == BOTTOM) ? (ymax - 1) : 0;
      int y2 = (menuPos == BOTTOM) ? (ymax - subsize[x] - 2) : TITLELINES;
      int high = subsize[x] + TITLELINES;

      /*
       * Limit the menu height to fit on the screen.
       */
      if (high + y2 > ymax)
      {
	 high = ymax - TITLELINES;
      }

      max = -1;
      for (y = TITLELINES; y < subsize[x] ; y++)
      {
	 int y0 = y - TITLELINES;
	 menu->sublist[x1][y0] = char2Chtype (menulist[x][y], &menu->sublistLen[x1][y0], &junk);
	 max = MAXIMUM (max, menu->sublistLen[x1][y0]);
      }

      if (menuloc[x] == LEFT)
      {
	 x2 = leftloc;
      }
      else
      {
	 x2 = (rightloc -= max + 2);
      }
      menu->title[x1]	 = char2Chtype (menulist[x][0], &menu->titleLen[x1], &junk);
      menu->subsize[x1]	 = subsize[x] - TITLELINES;
      menu->titleWin[x1] = subwin (cdkscreen->window, TITLELINES, menu->titleLen[x1]+2, ypos + y1, xpos + x2);
      menu->pullWin[x1]  = subwin (cdkscreen->window, high, max+2, ypos + y2, xpos + x2);
      if (menu->titleWin[x1] == 0 || menu->pullWin[x1] == 0)
      {
	 destroyCDKMenu(menu);
	 return (0);
      }

      leftloc += menu->titleLen[x] + 1;

      keypad (menu->titleWin[x1], TRUE);
      keypad (menu->pullWin[x1], TRUE);
   }
   ObjOf(menu)->inputWindow = menu->titleWin[menu->currentTitle];

   /* 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)
{
   chtype input;
   int ret;

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

   /* Display the menu titles. */
   drawCDKMenu(menu, ObjOf(menu)->box);

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

   /* If the input string is null, this is an interactive activate. */
   if (actions == 0)
   {
      ObjOf(menu)->inputWindow = menu->titleWin[menu->currentTitle];

      /* Start taking input from the keyboard. */
      for (;;)
      {
	 input = getcCDKObject (ObjOf(menu));

	 /* 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. */
   setExitType(menu, 0);
   return -1;
}

/*
 * The "%" operator is simpler but does not handle negative values.
 */
static int wrapped (int within, int limit)
{
   if (within < 0)
      within = limit - 1;
   else if (within >= limit)
      within = 0;
   return within;
}

static void drawTitle (CDKMENU *menu, int item)
{
   writeChtype (menu->titleWin[item],
		0, 0, menu->title[item],
		HORIZONTAL,
		0, menu->titleLen[item]);
}

static void drawItem (CDKMENU *menu, int item, int offset)
{
   writeChtype (menu->pullWin[menu->currentTitle],
	       1, item + TITLELINES - offset,
	       menu->sublist[menu->currentTitle][item],
	       HORIZONTAL,
	       0, menu->sublistLen[menu->currentTitle][item]);
}

/* Highlight the current sub-menu item. */
static void selectItem (CDKMENU *menu, int item, int offset)
{
   writeChtypeAttrib (menu->pullWin[menu->currentTitle],
	       1, item + TITLELINES - offset,
	       menu->sublist[menu->currentTitle][item],
	       menu->subtitleAttr,
	       HORIZONTAL,
	       0, menu->sublistLen[menu->currentTitle][item]);
}

static void withinSubmenu (CDKMENU *menu, int step)
{
   int next = wrapped(menu->currentSubtitle + step, menu->subsize[menu->currentTitle]);

   if (next != menu->currentSubtitle)
   {
      CDKSCREEN *screen = ScreenOf(menu);
      int ymax = getmaxy(screen->window);

      if (1 +
	 getbegy (menu->pullWin[menu->currentTitle]) +
	 menu->subsize[menu->currentTitle] >= ymax)
      {
	 menu->currentSubtitle = next;
	 drawCDKMenuSubwin (menu);
      }
      else
      {
	 /* Erase the old subtitle. */
	 drawItem (menu, menu->currentSubtitle, 0);

	 /* Set the values. */
	 menu->currentSubtitle = next;

	 /* Draw the new sub-title. */
	 selectItem (menu, menu->currentSubtitle, 0);

	 wrefresh (menu->pullWin[menu->currentTitle]);
      }

      ObjOf(menu)->inputWindow = menu->titleWin[menu->currentTitle];
   }
}

static void acrossSubmenus (CDKMENU *menu, int step)
{
   int next = wrapped(menu->currentTitle + step, menu->menuItems);

   if (next != menu->currentTitle)
   {
      /* Erase the menu sub-window. */
      eraseCDKMenuSubwin (menu);
      refreshCDKScreen (ScreenOf(menu));

      /* Set the values. */
      menu->currentTitle = next;
      menu->currentSubtitle = 0;

      /* Draw the new menu sub-window. */
      drawCDKMenuSubwin (menu);
      ObjOf(menu)->inputWindow = menu->titleWin[menu->currentTitle];
   }
}

/*
 * This injects a character into the menu widget.
 */
static int _injectCDKMenu (CDKOBJS *object, chtype input)
{
   CDKMENU *menu = (CDKMENU *)object;
   int ppReturn = 1;
   int ret = unknownInt;
   bool complete = FALSE;

   /* Set the exit type. */
   setExitType(menu, 0);

   /* Check if there is a pre-process function to be called. */
   if (PreProcessFuncOf(menu) != 0)
   {
      /* Call the pre-process function. */
      ppReturn = PreProcessFuncOf(menu) (vMENU, menu, PreProcessDataOf(menu), input);
   }

   /* Should we continue? */
   if (ppReturn != 0)
   {
      /* Check for key bindings. */
      if (checkCDKObjectBind (vMENU, menu, input) != 0)
      {
	 checkEarlyExit(menu);
	 complete = TRUE;
      }
      else
      {
	 switch (input)
	 {
	    case KEY_LEFT :
		 acrossSubmenus(menu, -1);
		 break;

	    case KEY_RIGHT : case KEY_TAB :
		 acrossSubmenus(menu, 1);
		 break;

	    case KEY_UP :
		 withinSubmenu (menu, -1);
		 break;

	    case KEY_DOWN : case SPACE :
		 withinSubmenu (menu, 1);
		 break;

	    case KEY_ENTER :
		 cleanUpMenu (menu);
		 setExitType(menu, input);
		 menu->lastSelection = ((menu->currentTitle * 100) + menu->currentSubtitle);
		 ret = menu->lastSelection;
		 complete = TRUE;
		 break;

	    case KEY_ESC :
		 cleanUpMenu (menu);
		 setExitType(menu, input);
		 menu->lastSelection = -1;
		 ret = menu->lastSelection;
		 complete = TRUE;
		 break;

	    case CDK_REFRESH :
		 eraseCDKScreen (ScreenOf(menu));
		 refreshCDKScreen (ScreenOf(menu));
		 break;
	 }
      }

      /* Should we call a post-process? */
      if (!complete && (PostProcessFuncOf(menu) != 0))
      {
	 PostProcessFuncOf(menu) (vMENU, menu, PostProcessDataOf(menu), input);
      }
   }

   if (!complete)
   {
      setExitType(menu, 0);
   }

   ResultOf(menu).valueInt = ret;
   return (ret != unknownInt);
}

/*
 * This draws a menu item subwindow.
 */
void drawCDKMenuSubwin (CDKMENU *menu)
{
   int x;
   int high = getmaxy(menu->pullWin[menu->currentTitle]) - 2;
   int x0 = 0;
   int x1 = menu->subsize[menu->currentTitle];

   if (x1 > high)
      x1 = high;

   if (menu->currentSubtitle >= x1)
   {
      x0 = (menu->currentSubtitle - x1) + 1;
      x1 += x0;
   }

   /* Box the window. */
   werase(menu->pullWin[menu->currentTitle]);
   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 = x0; x < x1 ; x++)
   {
      drawItem (menu, x, x0);
   }

   selectItem (menu, menu->currentSubtitle, x0);
   refreshCDKWindow (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]);
   refreshCDKWindow (menu->titleWin[menu->currentTitle]);
}

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

   /* Redraw the sub-menu title. */
   drawTitle (menu, menu->currentTitle);
   refreshCDKWindow (menu->titleWin[menu->currentTitle]);
}

/*
 * This function draws the menu.
 */
static void _drawCDKMenu (CDKOBJS *object, boolean Box GCC_UNUSED)
{
   CDKMENU *menu = (CDKMENU *)object;
   int x;

   /* Draw in the menu titles. */
   for (x=0; x < menu->menuItems; x++)
   {
      drawTitle (menu, x);
      refreshCDKWindow (menu->titleWin[x]);
   }
}

/*
 * This moves the menu to the given location.
 */
static void _moveCDKMenu (CDKOBJS *object, int xplace, int yplace, boolean relative, boolean refresh_flag)
{
   CDKMENU *menu = (CDKMENU *)object;
   int currentX = getbegx(WindowOf(menu));
   int currentY = getbegy(WindowOf(menu));
   int xpos	= xplace;
   int ypos	= yplace;
   int xdiff	= 0;
   int ydiff	= 0;
   int x;

   /*
    * If this is a relative move, then we will adjust where we want
    * to move to.
    */
   if (relative)
   {
      xpos = getbegx(WindowOf(menu)) + xplace;
      ypos = getbegy(WindowOf(menu)) + yplace;
   }

   /* Adjust the window if we need to. */
   alignxy (WindowOf(menu), &xpos, &ypos, getmaxx(WindowOf(menu)), getmaxy(WindowOf(menu)));

   /* Get the difference. */
   xdiff = currentX - xpos;
   ydiff = currentY - ypos;

   /* Move the windows to the new location. */
   moveCursesWindow(WindowOf(menu), -xdiff, -ydiff);
   for (x=0; x < menu->menuItems; x++)
   {
      moveCursesWindow(menu->titleWin[x], -xdiff, -ydiff);
   }

   /* Touch the windows so they 'move'. */
   refreshCDKWindow (WindowOf(menu));

   /* Redraw the window, if they asked for it. */
   if (refresh_flag)
   {
      drawCDKMenu (menu, ObjOf(menu)->box);
   }
}

/*
 * This sets the background attribute of the widget.
 */
static void _setBKattrMenu (CDKOBJS *object, chtype attrib)
{
   if (object != 0)
   {
      CDKMENU *widget = (CDKMENU *)object;
      int x;

      for (x=0; x < widget->menuItems; x++)
      {
	 wbkgd (widget->titleWin[x], attrib);
	 wbkgd (widget->pullWin[x], attrib);
      }
   }
}

/*
 * This function destroys a menu widget.
 */
static void _destroyCDKMenu (CDKOBJS *object)
{
   if (object != 0)
   {
      CDKMENU *menu = (CDKMENU *)object;
      int x, y;

      /* 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);
   }
}

/*
 * This function erases the menu widget from the screen.
 */
static void _eraseCDKMenu (CDKOBJS *object)
{
   if (validCDKObject (object))
   {
      CDKMENU *menu = (CDKMENU *)object;
      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 item to highlight.
 */
void setCDKMenuCurrentItem (CDKMENU *menu, int menuitem, int submenuitem)
{
   menu->currentTitle    = wrapped(menuitem,    menu->menuItems);
   menu->currentSubtitle = wrapped(submenuitem, menu->subsize[menu->currentTitle]);
}
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.
 */
static void cleanUpMenu (CDKMENU *menu)
{
   /* Erase the sub-menu. */
   eraseCDKMenuSubwin (menu);
   wrefresh (menu->pullWin[menu->currentTitle]);

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

static void _focusCDKMenu(CDKOBJS *object)
{
   CDKMENU *menu = (CDKMENU *)object;

   drawCDKMenuSubwin(menu);
   ObjOf(menu)->inputWindow = menu->titleWin[menu->currentTitle];
}

dummyUnfocus(Menu)

dummyRefreshData(Menu)

dummySaveData(Menu)