home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1998 August / macformat-066.iso / How-to's / Foreign Files / Creator Changer 2.8.4 / Code & Resource / Creator Changer.start.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-29  |  13.6 KB  |  570 lines  |  [TEXT/KAHL]

  1. /**********************************************************************
  2.  *    This file contains the functions which start off the program.  The
  3.  *    main event loop and Macintosh toolbox definitions are in this
  4.  *    file.
  5.  **********************************************************************/
  6.  
  7. #include    "Creator Changer.h"
  8. #include    "Creator Changer.start.h"
  9.  
  10.  
  11. /**********************************************************************
  12.  *    Function main(), this is the main function of the program.  The 
  13.  *    major parts of the program are called in this function.
  14.  **********************************************************************/
  15.  
  16. void main(void)
  17.     {
  18.     
  19.     Init_Toolbox();
  20.     
  21.     Check_Sys_Type();
  22.     Check_Drag_Manager();
  23.     Install_AE_Handlers();
  24.     
  25.     Get_Strings();
  26.     Open_Preferences();
  27.     Set_Up_Menu_Bar();
  28.     
  29.     while(!All_Done) Handle_One_Event();
  30.     
  31.     }
  32.     
  33.  
  34. /**********************************************************************
  35.  *    Function Handle_One_Event(), this function handles one event.  
  36.  *    Depending on what the event is, it gets directed to the 
  37.  *    appropriate place.
  38.  **********************************************************************/
  39.  
  40. void Handle_One_Event(void) 
  41.     {
  42.     
  43.     EventRecord        the_event;
  44.     
  45.     if(WaitNextEvent(everyEvent, &the_event, Sleep_Ticks, MOUSE_REGION))
  46.         {    
  47.         if(IsDialogEvent(&the_event)) Handle_Dialog_Event(&the_event);
  48.         switch(the_event.what)
  49.             {
  50.             case mouseDown:
  51.                 Handle_Mouse_Down(&the_event);
  52.                 break;
  53.             case keyDown:
  54.             case autoKey:
  55.                 {
  56.                 static char    command_key;
  57.                 
  58.                 Maintain_Menu_Items(FrontWindow());
  59.                 command_key=the_event.message & charCodeMask;
  60.                 if(the_event.modifiers & cmdKey) (void)Handle_Menu_Choice(MenuKey(command_key));
  61.                 }
  62.             case kHighLevelEvent:
  63.                 Option_Key_Down=Is_Pressed(58);
  64.                 AEProcessAppleEvent(&the_event);
  65.                 break;
  66.             }
  67.         }
  68.     else Do_Dialog_Null_Event(&the_event);
  69.  
  70.     
  71.     }
  72.  
  73.  
  74.  
  75. /**********************************************************************
  76.  *    Function Handle_Dialog_Event(), this function sends events to the dialog event 
  77.  *    loop, then if the event loop did not handle the event it then checks to see what was
  78.  *    was selected.
  79.  **********************************************************************/
  80.  
  81. void Handle_Dialog_Event(EventRecord *the_event)
  82.     {    
  83.     
  84.     if(Dialog_Event_Loop(the_event))
  85.         {
  86.         DialogRef    the_dialog;
  87.         short        the_item;
  88.     
  89.         if(DialogSelect(the_event, &the_dialog, &the_item))
  90.             {
  91.             if(the_dialog==Chng.dialog)    Switch_Chng_Dialog(&Chng, the_item);
  92.             else if(the_dialog==About.dialog) Switch_Abot_Dialog(&About, the_item);
  93.             else if(the_dialog==Pref.dialog) Switch_Pref_Dialog(&Pref, the_item);
  94.             }
  95.         }
  96.         
  97.     }
  98.  
  99.  
  100.  
  101. /**********************************************************************
  102.  *    Function Handle_Mouse_Down(), this function handles any mouse down
  103.  *    event, such as a window select, in the menu bar etc.
  104.  **********************************************************************/
  105.  
  106. void Handle_Mouse_Down(EventRecord *the_event)
  107.     {
  108.     
  109.     short        the_part;
  110.     long        menu_choice, the_volume;
  111.     Rect        drag_rect;
  112.     RgnHandle    the_gray_rgn;
  113.     WindowRef    which_window;
  114.     DialogRef    the_dialog=FrontWindow();
  115.                 
  116.     GetSysBeepVolume(&the_volume);
  117.     SetSysBeepVolume(the_volume);
  118.     the_part=FindWindow(the_event->where, &which_window);
  119.     the_gray_rgn=GetGrayRgn();  
  120.     
  121.     switch(the_part)
  122.         {
  123.         case inMenuBar:
  124.             Maintain_Menu_Items(the_dialog);
  125.             menu_choice=MenuSelect(the_event->where);
  126.             (void)Handle_Menu_Choice(menu_choice);              
  127.             break;
  128.         case inSysWindow: 
  129.             SystemClick(the_event, which_window);
  130.             break;
  131.         case inDesk:
  132.         case inContent:
  133.             if(the_dialog==About.dialog && which_window!=the_dialog) SysBeep(the_volume);
  134.             break;
  135.         case inDrag:
  136.             if(which_window==the_dialog) DragWindow(which_window, the_event->where, &((**the_gray_rgn).rgnBBox));
  137.             else SysBeep(the_volume);
  138.         default:
  139.             break;
  140.         }
  141.  
  142.     }
  143.  
  144.  
  145.  
  146. /**********************************************************************
  147.  *    Function Handle_Menu_Choice(), this function handles the menu 
  148.  *    choice made, then it directs it to the appropriate place.
  149.  **********************************************************************/
  150.  
  151. Boolean Handle_Menu_Choice(long menu_choice)
  152.     {
  153.     
  154.     short    the_menu=HiWord(menu_choice);
  155.     short    the_item=LoWord(menu_choice);
  156.     
  157.     if(menu_choice!=0)
  158.         {
  159.         switch(the_menu)
  160.             {
  161.             case APPLE_MENU_ID:
  162.                 Handle_Apple_Choice(the_item);
  163.                 HiliteMenu(0);
  164.                 return(TRUE);
  165.                 break;
  166.             case FILE_MENU_ID:
  167.                 Handle_File_Choice(the_item);
  168.                 HiliteMenu(0);
  169.                 return(TRUE);
  170.                 break;
  171.             case EDIT_MENU_ID:
  172.                 Handle_Edit_Choice(the_item);
  173.                 HiliteMenu(0);
  174.                 return(TRUE);
  175.             default:
  176.                 return(FALSE);
  177.                 break;
  178.             }
  179.         }
  180.         
  181.     }
  182.  
  183.  
  184.  
  185. /**********************************************************************
  186.  *    Function Is_Pressed(), this function determine what key is pressed.
  187.  **********************************************************************/
  188.  
  189. Boolean Is_Pressed(unsigned short key)
  190.     {
  191.     
  192.     unsigned char key_mask[16];
  193.  
  194.     GetKeys((unsigned long *)key_mask);
  195.     return((key_mask[key>>3]>>(key&7))&1);
  196.     
  197.     }
  198.  
  199.  
  200.  
  201. /**********************************************************************
  202.  *    Function  Do_Dialog_Null_Event(), this function keeps the  text edit cursor blinking
  203.  *    and makes sure that the arrow changes to an I beam when ove the edit boxes.
  204.  **********************************************************************/
  205.  
  206. void Do_Dialog_Null_Event(EventRecord *the_event)
  207.     {
  208.     
  209.     DialogRef    the_dialog=FrontWindow();
  210.     GrafPtr        old_port;
  211.     
  212.     GetPort(&old_port);
  213.     SetPort(the_dialog);
  214.     
  215.     if(the_dialog!=NIL_PTR)
  216.         {
  217.         if(!In_Background)
  218.             {
  219.             short        the_item;
  220.             
  221.             if(Check_Edit_Box(the_dialog))
  222.                 {
  223.                 CursHandle    the_cursor;
  224.             
  225.                 the_cursor=GetCursor(iBeamCursor);
  226.                 HLock((Handle)the_cursor);
  227.                 SetCursor(*the_cursor);
  228.                 HUnlock((Handle)the_cursor);
  229.                 }
  230.             else SetCursor(&qd.arrow);
  231.  
  232.             DialogSelect(the_event, &the_dialog, &the_item);
  233.             }
  234.         }
  235.     SetPort(old_port);
  236.     
  237.     }
  238.  
  239.  
  240.     
  241. /**********************************************************************
  242.  *    Function  Dialog_Event_Loop(), this function is the switch for 
  243.  *    any of the dialog events.
  244.  **********************************************************************/
  245.  
  246. Boolean Dialog_Event_Loop(EventRecord *the_event)
  247.     {
  248.     
  249.     switch(the_event->what)
  250.         {
  251.         case keyDown:
  252.             Maintain_Menu_Items(FrontWindow());
  253.             Handle_Key_Down(the_event);
  254.             return(FALSE);
  255.             break;
  256.         default:
  257.             return(TRUE);
  258.             break;
  259.         }
  260.     
  261.     }
  262.  
  263.  
  264.  
  265. /**********************************************************************
  266.  *    Function Check_Edit_Box(), this function  checks to see if the cursor
  267.  *    is in any of the text-edit boxes in any of the dialogs.
  268.  **********************************************************************/
  269.  
  270. Boolean Check_Edit_Box(DialogRef the_dialog)
  271.     {
  272.     
  273.     Point        the_point;
  274.     short        os=Pref.num_items;
  275.     
  276.     GetMouse(&the_point);
  277.     
  278.     if(the_dialog==Chng.dialog)
  279.         {
  280.         if(Get_Dialog_Item_In_Rect(the_dialog, CHNG_CREATOR, the_point)) return(YES);
  281.         if(Get_Dialog_Item_In_Rect(the_dialog, CHNG_FILE, the_point)) return(YES);
  282.         }
  283.     else if(the_dialog==Pref.dialog)
  284.         {
  285.         if(Pref.menu_id==P_EPTYPE_ITEM)
  286.             {
  287.             if(Get_Dialog_Item_In_Rect(the_dialog, os+P_PT_CT, the_point)) return(YES);
  288.             if(Get_Dialog_Item_In_Rect(the_dialog, os+P_PT_FT, the_point)) return(YES);
  289.             if(Get_Dialog_Item_In_Rect(the_dialog, os+P_PT_DS, the_point)) return(YES);
  290.             }
  291.         else if(Pref.menu_id==P_EATYPE_ITEM)
  292.             {
  293.             if(Get_Dialog_Item_In_Rect(the_dialog, os+P_AC_CHFT, the_point)) return(YES);
  294.             if(Get_Dialog_Item_In_Rect(the_dialog, os+P_AC_CTCT, the_point)) return(YES);
  295.             if(Get_Dialog_Item_In_Rect(the_dialog, os+P_AC_CTFT, the_point)) return(YES);
  296.             }
  297.         }
  298.     return(NO);
  299.     
  300.     }
  301.  
  302.  
  303.  
  304. /**********************************************************************
  305.  *    Function  Handle_Key_Down(), this function handles any key down event
  306.  *    when any of the dialog boxes are open.
  307.  **********************************************************************/
  308.  
  309. void Handle_Key_Down(EventRecord *the_event)
  310.     {
  311.     
  312.     static char    key_pressed;
  313.     short            os=Pref.num_items;
  314.     DialogRef        the_dialog=FrontWindow();
  315.     
  316.     key_pressed=the_event->message & charCodeMask;
  317.     if(!(the_event->modifiers & cmdKey))
  318.         {
  319.         if(the_dialog==Chng.dialog) switch(key_pressed)
  320.             {
  321.             case Escape_Key:
  322.                 Handle_Key_Pressed(Chng.dialog, CHNG_CANCEL);
  323.                 Switch_Chng_Dialog(&Chng, CHNG_CANCEL);
  324.                 break;
  325.             case Return_Key:
  326.             case Enter_Key:
  327.                 Handle_Key_Pressed(Chng.dialog, CHNG_OK);
  328.                 Switch_Chng_Dialog(&Chng, CHNG_OK);
  329.                 break;
  330.             default:
  331.                 {
  332.                 short    the_item;
  333.                 
  334.                 DialogSelect(the_event, &the_dialog, &the_item);
  335.                 }
  336.                 break;
  337.             }
  338.         else if(the_dialog==About.dialog || the_dialog==Pref.dialog) switch(key_pressed)
  339.             {
  340.             case Escape_Key:
  341.             case Return_Key:
  342.             case Enter_Key:
  343.                 if(the_dialog==About.dialog)
  344.                     {
  345.                     Handle_Key_Pressed(About.dialog, ABOUT_OK);
  346.                     Switch_Abot_Dialog(&About, ABOUT_OK);
  347.                     }
  348.                 else if(the_dialog==Pref.dialog)
  349.                     {
  350.                     Handle_Key_Pressed(Pref.dialog, PREF_DONE);
  351.                     Switch_Pref_Dialog(&Pref, PREF_DONE);
  352.                     }
  353.                 break;
  354.             default:
  355.                 {
  356.                 short    the_item;
  357.                 
  358.                 DialogSelect(the_event, &the_dialog, &the_item);
  359.                 }
  360.                 break;
  361.             }
  362.         PT_Item_To_Edit=AC_Item_To_Edit=0;
  363.         }
  364.     else if(the_event->modifiers & cmdKey)
  365.         if(!Handle_Menu_Choice(MenuKey(key_pressed))) Do_DLOG_Cmd_Key(the_dialog, key_pressed);
  366.     
  367.     }
  368.  
  369.  
  370.  
  371. /**********************************************************************
  372.  *    Function  Do_DLOG_Cmd_Key(), this function handles a command key
  373.  *    event for any of the open dialogs.
  374.  **********************************************************************/
  375.  
  376. void Do_DLOG_Cmd_Key(DialogRef the_dialog, char key_pressed)
  377.     {
  378.     
  379.     short        os=Pref.num_items;
  380.     
  381.     if(the_dialog==Chng.dialog) switch(key_pressed)
  382.         {
  383.         case 'M':
  384.         case 'm':
  385.             Handle_Key_Pressed(Chng.dialog, CHNG_MKLK);
  386.             Switch_Chng_Dialog(&Chng, CHNG_MKLK);
  387.             break;
  388.         case '.':
  389.             Handle_Key_Pressed(Chng.dialog, CHNG_CANCEL);
  390.             Switch_Chng_Dialog(&Chng, CHNG_CANCEL);
  391.             break;
  392.         }
  393.     else if(the_dialog==Pref.dialog)
  394.         {
  395.         if(Pref.menu_id==P_EPTYPE_ITEM) switch(key_pressed)
  396.             {
  397.             case 'D':
  398.             case 'd':
  399.                 Handle_Key_Pressed(Pref.dialog, os+P_PT_DEL);
  400.                 Switch_Pt_Dialog(&Pref, os+P_PT_DEL);
  401.                 break;
  402.             case 'A':
  403.             case 'a':
  404.                 Handle_Key_Pressed(Pref.dialog, os+P_PT_ADD);
  405.                 Switch_Pt_Dialog(&Pref, os+P_PT_ADD);
  406.                 break;
  407.             case 'G':
  408.             case 'g':
  409.                 Handle_Key_Pressed(Pref.dialog, os+P_PT_GFL);
  410.                 Switch_Pt_Dialog(&Pref, os+P_PT_GFL);
  411.                 break;
  412.             }
  413.         else if(Pref.menu_id==P_EATYPE_ITEM) switch(key_pressed)
  414.             {
  415.             case 'D':
  416.             case 'd':
  417.                 Handle_Key_Pressed(Pref.dialog, os+P_AC_DEL);
  418.                 Switch_Ac_Dialog(&Pref, os+P_AC_DEL);
  419.                 break;
  420.             case 'A':
  421.             case 'a':
  422.                 Handle_Key_Pressed(Pref.dialog, os+P_AC_ADD);
  423.                 Switch_Ac_Dialog(&Pref, os+P_AC_ADD);
  424.                 break;
  425.             case 'G':
  426.             case 'g':
  427.                 Handle_Key_Pressed(Pref.dialog, os+P_AC_GFL);
  428.                 Switch_Ac_Dialog(&Pref, os+P_AC_GFL);
  429.                 break;
  430.             }
  431.         else if(Pref.menu_id==P_GENERL_ITEM) switch(key_pressed)
  432.             {
  433.             case 'E':
  434.             case 'e':
  435.                 Handle_Key_Pressed(Pref.dialog, Pref.num_items+P_GN_OKAC);
  436.                 Switch_Gnrl_Dialog(&Pref, Pref.num_items+P_GN_OKAC);
  437.                 break;
  438.             case 'Q':
  439.             case 'q':
  440.                 Handle_Key_Pressed(Pref.dialog, Pref.num_items+P_GN_QADD);
  441.                 Switch_Gnrl_Dialog(&Pref, Pref.num_items+P_GN_QADD);
  442.                 break;
  443.             }
  444.         }
  445.         PT_Item_To_Edit=AC_Item_To_Edit=0;
  446.  
  447.     }
  448.  
  449.     
  450.  
  451. /**********************************************************************
  452.  *    Function Handle_Apple_Choice(), this function decides what item 
  453.  *    to execute under the apple menu.
  454.  **********************************************************************/
  455.  
  456. void Handle_Apple_Choice(short the_item)
  457.     {
  458.     
  459.     short    desk_acc_number;
  460.     Str32    desk_acc_name;
  461.     
  462.     switch(the_item)
  463.         {
  464.         case A_ABOUT_ITEM:
  465.             Open_DLOG(&About);
  466.             break;
  467.         default:
  468.             GetMenuItemText(Apple_Menu, the_item, desk_acc_name);
  469.             desk_acc_number=OpenDeskAcc(desk_acc_name);
  470.             break;
  471.         }
  472.     
  473.     }
  474.  
  475.  
  476.  
  477. /**********************************************************************
  478.  *    Function Handle_File_Choice(), this function decides what item
  479.  *    to execute under the options menu.
  480.  **********************************************************************/
  481.  
  482. void Handle_File_Choice(short the_item)
  483.     {
  484.     
  485.     switch(the_item)
  486.         {
  487.         case F_OPEN_ITEM:
  488.             if(Pick_File(&The_File_Spec, &File_Info, TRUE))
  489.                 {
  490.                 Multiple_Files=NO;
  491.                 Chng.num_files=1;
  492.                 Open_DLOG(&Chng);
  493.                 }
  494.             Maintain_Menu_Items(FrontWindow());
  495.             break;
  496.         case F_PREF_ITEM:
  497.             Open_DLOG(&Pref);
  498.             break;
  499.         case F_QUIT_ITEM:
  500.             All_Done=TRUE;
  501.             break;
  502.         default:
  503.             break;
  504.         }
  505.     
  506.     }
  507.     
  508.     
  509.     
  510. /**********************************************************************
  511.  *    Function Handle_Edit_Choice(), this function decides what item
  512.  *    to execute under the edit menu.
  513.  **********************************************************************/
  514.     
  515. void Handle_Edit_Choice(short the_item)
  516.     {
  517.     
  518.     DialogRef    the_dialog=FrontWindow();
  519.     
  520.     switch(the_item)
  521.         {
  522.         case E_CUT_ITEM:
  523.             HiliteMenu(EDIT_MENU_ID);
  524.             ZeroScrap();
  525.             DialogCopy(the_dialog);
  526.             DialogCut(the_dialog);
  527.             TEToScrap();
  528.             break;
  529.         case E_COPY_ITEM:
  530.             HiliteMenu(EDIT_MENU_ID);
  531.             ZeroScrap();
  532.             DialogCopy(the_dialog);
  533.             TEToScrap();
  534.             break;
  535.         case E_PASTE_ITEM:
  536.             TEFromScrap();
  537.             HiliteMenu(EDIT_MENU_ID);
  538.             DialogPaste(the_dialog);
  539.             break;
  540.         case E_CLEAR_ITEM:
  541.             HiliteMenu(EDIT_MENU_ID);
  542.             DialogDelete(the_dialog);
  543.             break;
  544.         }
  545.     PT_Item_To_Edit=AC_Item_To_Edit=0;
  546.         
  547.     }
  548.  
  549.  
  550.  
  551. /**********************************************************************
  552.  *    Function Handle_Key_Pressed(), this function handels what happens 
  553.  *    if a key is pressed.
  554.  **********************************************************************/
  555.  
  556. static void Handle_Key_Pressed(DialogRef the_dialog, short the_item)
  557.     {
  558.     
  559.     Handle    item_handle;
  560.     short    item_type;
  561.     long    delay;
  562.     Rect    item_rect;
  563.     
  564.     Get_Dialog_Item_Hndl(the_dialog, the_item, &item_handle);
  565.     HiliteControl((ControlHandle)item_handle, YES);
  566.     Delay(DELAY, &delay);
  567.     HiliteControl((ControlHandle)item_handle, NO);
  568.     
  569.     }
  570.