home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / System / Swatch / Development / swatch 1.7 / swatch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-08  |  5.1 KB  |  281 lines  |  [TEXT/KAHL]

  1. /**
  2.  
  3.     swatch.c
  4.     Copyright (c) 1990-1992, joe holt
  5.  
  6.  **/
  7.  
  8.  
  9. /**-----------------------------------------------------------------------------
  10.  **
  11.  **    Headers
  12.  **
  13.  **/
  14.  
  15. #ifndef __about__
  16. #include "about.h"
  17. #endif
  18. #ifndef __content__
  19. #include "content.h"
  20. #endif
  21. #ifndef __ctypes__
  22. #include "ctypes.h"
  23. #endif
  24. #ifndef __display__
  25. #include "display.h"
  26. #endif
  27. #ifndef __heap__
  28. #include "heap.h"
  29. #endif
  30. #ifndef __heap_list__
  31. #include "heap_list.h"
  32. #endif
  33. #ifndef __prefs__
  34. #include "prefs.h"
  35. #endif
  36. #ifndef __pstring__
  37. #include "pstring.h"
  38. #endif
  39. #ifndef __resources__
  40. #include "resources.h"
  41. #endif
  42. #ifndef __swatch__
  43. #include "swatch.h"
  44. #endif
  45. #ifndef __window__
  46. #include "window.h"
  47. #endif
  48.  
  49.  
  50. /**-----------------------------------------------------------------------------
  51.  **
  52.  ** Private Macros
  53.  **
  54.  **/
  55.  
  56. #define MacJmp    (*(int32 *)0x120)
  57.  
  58.  
  59. /**-----------------------------------------------------------------------------
  60.  **
  61.  ** Private Variables
  62.  **
  63.  **/
  64.  
  65. static Boolean in_foreground;
  66. static MenuHandle Apple_menu, File_menu, Edit_menu, Heap_menu;
  67.  
  68. static EventRecord the_event;
  69.  
  70. static unsigned char *strings;
  71.  
  72.  
  73. /**-----------------------------------------------------------------------------
  74.  **
  75.  ** Private Functions
  76.  **
  77.  **/
  78.  
  79. int main( void );
  80.  
  81. static Boolean handle_menu_select( uns32 menu_long );
  82.  
  83.  
  84. /*******************************************************************************
  85.  **
  86.  **    Public Variables
  87.  **
  88.  **/
  89.  
  90. SysEnvRec This_mac;
  91. Boolean Debugger_installed;
  92.  
  93.  
  94. /*******************************************************************************
  95.  **
  96.  **    Public Functions
  97.  **
  98.  **/
  99.  
  100. int main( void )
  101. {
  102.     int16 i;
  103.     Boolean quit, edit_enabled;
  104.     Handle h;
  105.  
  106.     MaxApplZone();
  107.     InitGraf( &thePort );
  108.     InitFonts();
  109.     InitWindows();
  110.     InitDialogs( NULL );
  111.     TEInit();
  112.     InitMenus();
  113.     InitCursor();
  114.  
  115.     for ( i = 5; i; --i )
  116.         EventAvail( 0, &the_event );
  117.     in_foreground = true;
  118.  
  119.     CouldAlert( OUT_OF_MEMORY_ALRT );
  120.  
  121.     SysEnvirons( 1, &This_mac );
  122.     if ( This_mac.systemVersion < 0x0700 ) Bail( BAD_SYSTEM_VERSION_ALRT );
  123.     Debugger_installed = MacJmp != 0;
  124.  
  125.  
  126.     h = GetResource( 'STR#', SWATCH_STR_ );
  127.     if ( !h ) Bail( OUT_OF_MEMORY_ALRT );
  128.     MoveHHi( h );
  129.     HLock( h );
  130.     strings = (unsigned char *) StripAddress( *h );
  131.  
  132.  
  133. // the order here is necessary...
  134.  
  135.     Prefs_init();
  136.     Display_init();
  137.     Heap_list_init();
  138.     Content_init();
  139.     Window_init();
  140.  
  141.  
  142.     if    (    !(Apple_menu = GetMenu(APPLE_MENU))
  143.             || !(File_menu = GetMenu(FILE_MENU))
  144.             || !(Edit_menu = GetMenu(EDIT_MENU))
  145.         ) Bail( OUT_OF_MEMORY_ALRT );
  146.  
  147.     AddResMenu( Apple_menu, 'DRVR' );
  148.     InsertMenu( Apple_menu, 0 );
  149.     InsertMenu( File_menu, 0 );
  150.     InsertMenu( Edit_menu, 0 );
  151.     DrawMenuBar();
  152.     edit_enabled = true;
  153.  
  154.     quit = false;
  155.     while ( !quit ) {
  156.  
  157.         WaitNextEvent( everyEvent, &the_event, (in_foreground ? Prefs.wne_in_foreground :
  158.                 Prefs.wne_in_background), NULL );
  159.         SetPort( App_window );
  160.         if ( FrontWindow() == App_window && edit_enabled ) {
  161.             DisableItem( Edit_menu, 0 );
  162.             DrawMenuBar();
  163.             edit_enabled = false;
  164.         }
  165.         else if ( FrontWindow() != App_window && !edit_enabled ) {
  166.             EnableItem( Edit_menu, 0 );
  167.             DrawMenuBar();
  168.             edit_enabled = true;
  169.         }
  170.         Window_idle();
  171.  
  172.         switch ( the_event.what ) {
  173.         case nullEvent:
  174.             GlobalToLocal( &the_event.where );
  175.             Content_set_cursor( &the_event );
  176.             break;
  177.  
  178.         case mouseDown:
  179.             i = FindWindow( the_event.where, (WindowPtr *) &the_event.message );
  180.             if ( i != inContent )
  181.                 InitCursor();
  182.             if ( i == inMenuBar )
  183.                 quit = handle_menu_select( MenuSelect( the_event.where ) );
  184.             else if ( (WindowPtr) the_event.message == App_window )
  185.                 quit = Window_mousedown( &the_event, i );
  186.             break;
  187.  
  188.         case keyDown:
  189.         case autoKey: {
  190.             char key;
  191.  
  192.             key = the_event.message & charCodeMask;
  193.             if ( the_event.modifiers & cmdKey )
  194.                 quit = handle_menu_select( MenuKey( key ) );
  195.             break;
  196.         }
  197.  
  198.         case updateEvt:
  199.             if ( (WindowPtr) the_event.message == App_window )
  200.                 Window_update( &the_event );
  201.             break;
  202.  
  203.         case activateEvt:
  204.             if ( (WindowPtr) the_event.message == App_window )
  205.                 Window_activate( &the_event, (the_event.modifiers & activeFlag) != 0 );
  206.             break;
  207.  
  208.         case app4Evt:
  209.             if ( *(unsigned char *) &the_event.message == 1 /* suspend/resume */ ) {
  210.                 if ( the_event.message & 1 /* resume */ )
  211.                     in_foreground = true;
  212.                 else
  213.                     in_foreground = false;
  214.                 HiliteWindow( App_window, in_foreground );
  215.                 Window_activate( &the_event, in_foreground );
  216.             }
  217.             break;
  218.  
  219.         default:
  220.             break;
  221.         }
  222.     }
  223.  
  224.     Window_close();
  225.     Prefs_save();
  226.     Heap_list_exit();
  227. }
  228.  
  229.  
  230. static Boolean handle_menu_select( uns32 menu_long )
  231. {
  232.     switch ( menu_long >> 16 ) {
  233.     case APPLE_MENU:
  234.         if ( (int16) menu_long == 1 ) {
  235.             Do_about( App_window );
  236.             UnloadSeg( Do_about );
  237.         }
  238.         else {
  239.             char DA_name[256];
  240.  
  241.             GetItem( Apple_menu, (int16) menu_long, (StringPtr) DA_name );
  242.             OpenDeskAcc( (StringPtr) DA_name );
  243.         }
  244.         break;
  245.  
  246.     case FILE_MENU:
  247.         return true;
  248.  
  249.     case EDIT_MENU:
  250.         break;
  251.  
  252.     default:
  253.         break;
  254.     }
  255.     HiliteMenu( 0 );
  256.     return false;
  257. }
  258.  
  259.  
  260. void Bail( int16 alert_number )
  261. {
  262.     StopAlert( alert_number, NULL );
  263.     ExitToShell();
  264. }
  265.  
  266.  
  267. unsigned char *pstr( int16 index )
  268. {
  269.     unsigned char *p;
  270.  
  271.     if ( index < 1 || index > *(int16 *)strings ) return "\p<bad string index>";
  272.  
  273.     for ( p = strings + 2; index > 1; --index, p += *p + 1 );
  274.     return p;
  275. }
  276.  
  277.  
  278. Boolean Swatch_in_foreground( void )
  279. {
  280.     return in_foreground;
  281. }