home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.bin / SourceCode / ShellPanel / Menu.m < prev    next >
Encoding:
Text File  |  1992-11-10  |  2.3 KB  |  94 lines

  1. /* File: Menu.m - (Interactive) Unix shell version of Menu
  2.  *
  3.  * By: Christopher Lane (lane@sumex-aim.stanford.edu)
  4.  *
  5.  * Date: 10 November 1992
  6.  *
  7.  * Copyright: 1990, 1991 & 1992 by The Leland Stanford Junior University.
  8.  * This program may be distributed without restriction for non-commercial use.
  9.  */
  10.  
  11. #import <stdlib.h>
  12. #import <getopt.h>
  13.  
  14. #import <appkit/Menu.h>
  15. #import <appkit/Matrix.h>
  16. #import <appkit/MenuCell.h>
  17. #import <appkit/Application.h>
  18.  
  19. #define SELECTOR @selector(stop:)
  20. #define KEYCODE '1'
  21. #define TIMEOUT (30)
  22.  
  23. #define USAGE "usage: %s [-c char] [-t title] [-x xpos -y ypos] [-T seconds] item [item [...]]\n"
  24. #define EXIT_USAGE (2)
  25.  
  26. @interface MenuApp : Application { }
  27.  
  28. - appDidInit:sender;
  29.  
  30. @end
  31.  
  32. @implementation MenuApp : Application
  33.  
  34. - appDidInit:sender
  35. {
  36.     [[self appIcon] orderOut:self];
  37.  
  38.     return self;
  39. }
  40.  
  41. @end
  42.  
  43. void timer(DPSTimedEntry teNumber, double now, int status) { exit(status); }
  44.  
  45. void main(int argc, char *argv[])
  46. {
  47.     const char *title = NULL;
  48.     double timeout = TIMEOUT;
  49.     unsigned short keyCode = KEYCODE;
  50.     int option, status = EXIT_SUCCESS;
  51.     NXPoint point = { -1.0, -1.0 };
  52.     DPSTimedEntry teNumber = NULL;
  53.     
  54.     while ((option = getopt(argc, argv, "c:t:x:y:T:")) != EOF)
  55.         switch (option) {
  56.         case 'c': keyCode = optarg[0]; break;
  57.         case 't': title = optarg; break;
  58.         case 'x': point.x = atof(optarg); break;
  59.         case 'y': point.y = atof(optarg); break;
  60.         case 'T': timeout = atol(optarg); break;
  61.         default : status = EXIT_USAGE;
  62.         }
  63.     if(optind == argc) status = EXIT_USAGE;
  64.  
  65.     NXApp = [MenuApp new];
  66.  
  67.     if (status == EXIT_USAGE) (void) fprintf(stderr, USAGE, [NXApp appName]);
  68.     else {
  69.         int context = [NXApp activateSelf:YES];
  70.         id menu = [Menu newTitle:(title == NULL) ? [NXApp appName] : title];
  71.  
  72.         if(point.x >= 0.0 && point.y >= 0.0) [menu moveTopLeftTo:point.x :point.y];
  73.  
  74.         for(option = optind; option < argc; option++)
  75.             [[menu addItem:argv[option] action:SELECTOR keyEquivalent:keyCode++] setTarget:NXApp];
  76.  
  77.         [NXApp setMainMenu:[menu display]];
  78.  
  79.         if(timeout) teNumber = DPSAddTimedEntry(timeout, (DPSTimedEntryProc) &timer, (void *) EXIT_FAILURE, NX_BASETHRESHOLD);
  80.  
  81.         [NXApp run];
  82.  
  83.         if(timeout) DPSRemoveTimedEntry(teNumber);
  84.  
  85.         (void) printf("%s\n", [[[menu itemList] selectedCell] title]);
  86.  
  87.         if(context) (void) [NXApp activate:context];
  88.         }
  89.  
  90.     [NXApp free];
  91.  
  92.     exit(status);
  93. }
  94.