home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks96 / FontMancer.sit / FontMancer / Common / StandardEvent.c < prev    next >
C/C++ Source or Header  |  1996-06-21  |  7KB  |  320 lines

  1. #include "FontMancer.h"
  2. #include "FontDisplay.h"
  3. #include "Standard.h"
  4. #include "StandardEvent.h"
  5. #include "StandardMenu.h"
  6. #include "Display.h"
  7. #include "AEHandler.h"
  8.  
  9. extern Boolean        gInBackground;
  10. extern Boolean        gQuit;
  11. extern Boolean        gDManager;
  12. extern WindowPtr    gMainWindow;
  13. extern long            gLastKeyTime;
  14. extern Str255        gListNavString;
  15.  
  16. void CtlHandleToItem(ControlHandle ctlHandle, short *itemHit)
  17. {
  18.  
  19.     FMStuff                *FMStore;
  20.     ControlHandle        checkHandle;
  21.  
  22.     FMStore = (FMStuff *) GetWRefCon(gMainWindow);
  23.     *itemHit = 0;
  24.     checkHandle = FMStore->ctlHandles[*itemHit];
  25.     while ((ctlHandle != checkHandle) & (*itemHit <= iSizePopUp)) {
  26.         (*itemHit)++;
  27.         checkHandle = FMStore->ctlHandles[*itemHit];
  28.     }
  29.     if (*itemHit > iSizePopUp)
  30.         *itemHit = -1;
  31. }
  32.  
  33. short PlainButtonCheck(ControlHandle ctlHandle)
  34. {
  35.  
  36.     FMStuff    *FMStore;
  37.  
  38.     FMStore = (FMStuff *) GetWRefCon(gMainWindow);
  39.     if (ctlHandle == FMStore->ctlHandles[iPlainButton])
  40.         return(GetControlValue(ctlHandle));
  41.     else
  42.         return(0);
  43.  
  44. }
  45.  
  46. void ActivateControls(short activate)
  47. {
  48.     FMStuff                *FMStore;
  49.     short                hiliteCode,loop;
  50.  
  51.     FMStore = (FMStuff *) GetWRefCon(gMainWindow);
  52.     hiliteCode = activate ? 0 : 255;
  53.     for (loop=iPlainButton; loop <=iSizePopUp; loop++)
  54.         HiliteControl(FMStore->ctlHandles[loop],hiliteCode); 
  55. }
  56.  
  57. void DoContentClick(EventRecord *event)
  58. {
  59.  
  60.     Point                cursorLoc;
  61.     WindowPtr            theWindow = FrontWindow();
  62.     FMStuff                *FMStore;
  63.  
  64.     SetPt(&cursorLoc,event->where.h,event->where.v);
  65.     GlobalToLocal(&cursorLoc);
  66.     FMStore = (FMStuff *) GetWRefCon(theWindow);
  67.     if (PtInRect(cursorLoc,&(FMStore->itemRects[iButtonRect])))
  68.         DoButtonClick(event);
  69.     if (PtInRect(cursorLoc,&(FMStore->itemRects[iListRect])))
  70.         HandleListSelect(event);
  71. }
  72.  
  73. void DoButtonClick(EventRecord *event)
  74. {
  75.  
  76. ControlHandle            ctlHandle;
  77. Point                    cursorLoc;
  78. short                    itemHit;
  79.  
  80.  
  81. SetPt(&cursorLoc,event->where.h,event->where.v);
  82. GlobalToLocal(&cursorLoc);
  83. if (!(FindControl(cursorLoc,gMainWindow,&ctlHandle))) return;
  84. if (PlainButtonCheck(ctlHandle)) return;
  85. if (TrackControl(ctlHandle,cursorLoc,(ControlActionUPP) -1L)) {
  86.     CtlHandleToItem(ctlHandle,&itemHit);
  87.     switch (itemHit) {
  88.         case iPlainButton:
  89.             SetPlainDisplay();
  90.             break;
  91.         case iBoldButton:
  92.             SetBoldDisplay();
  93.             break;
  94.         case iItalicButton:
  95.             SetItalicDisplay();
  96.             break;
  97.         case iUnderlineButton:
  98.             SetUnderlineDisplay();
  99.             break;
  100.         case iOutlineButton:
  101.             SetOutlineDisplay();
  102.             break;
  103.         case iShadowButton:
  104.             SetShadowDisplay();
  105.             break;
  106.         case iSizePopUp:
  107.             HandleSizePopUp();
  108.             break;
  109.         };
  110.     }
  111. }
  112.  
  113. void DoKeyPress(EventRecord *event)
  114. {
  115.     FMStuff            *FMStore;
  116.     Point            newCell,oldCell = {0,0};
  117.     Rect            cellRect,ignoreRect;
  118.     ListHandle        theList;
  119.     
  120.     FMStore = (FMStuff *) GetWRefCon(gMainWindow);
  121.     theList = FMStore->fontList;
  122.     HLock((Handle) theList);
  123.     LGetSelect(TRUE,&oldCell,theList);
  124.     SetPt(&newCell,oldCell.h,oldCell.v);
  125.  
  126.     GetNewCell(event,FMStore->fontList, &newCell);
  127.     LSetSelect(FALSE,oldCell,theList);
  128.     LSetSelect(TRUE,newCell,theList);
  129.     LRect(&cellRect,newCell,theList);
  130.     LAutoScroll(theList);
  131.     HUnlock((Handle) theList);
  132.     SetDisplayFont(newCell,FMStore);
  133.     UpdateSampleRect(FMStore);
  134. }
  135.  
  136. void GetNewCell(EventRecord *event, ListHandle theList, Point *cell)
  137. {
  138.     short            listRows;
  139.     char            key;
  140.  
  141.     key = event->message & charCodeMask;
  142.     if (!gLastKeyTime)
  143.         gLastKeyTime = event->when;
  144.     listRows = ((**theList).dataBounds.bottom - (**theList).dataBounds.top) - 1;
  145.     switch(key) {
  146.         case kUpArrow:
  147.             if (cell->v)
  148.                 cell->v -= 1;
  149.             break;
  150.         case kDownArrow:
  151.             if (cell->v != listRows)
  152.                 cell->v += 1;
  153.             break;
  154.         case kPageUp:
  155.             cell->v -= 5;
  156.             if (cell->v < 0)
  157.                 cell->v = 0;
  158.             break;
  159.         case kPageDown:
  160.             cell->v += 5;
  161.             if (cell->v > listRows)
  162.                 cell->v = listRows;
  163.             break;
  164.         case kHome:
  165.             cell->v = 0;
  166.             break;
  167.         case kEnd:
  168.             cell->v = listRows;
  169.             break;
  170.         default:
  171.             if (event->when - gLastKeyTime >= 120)
  172.                 gListNavString[0] = 0;
  173.             gLastKeyTime = event->when;
  174.             if (gListNavString[0] < 255)
  175.                 gListNavString[++gListNavString[0]] = key;
  176.             SetPt(cell,0,0);
  177.             LSearch(&gListNavString[1],gListNavString[0],NewListSearchProc(CustomListSearch),cell,theList);
  178.             if (cell->v > listRows)
  179.                 cell->v = listRows;
  180.             break;
  181.     }
  182. }
  183.  
  184. pascal short CustomListSearch(char *cellData, char *testData, short cellLen, short testLen)
  185. {
  186.     short    result, index;
  187.     char    cellChar, testChar;
  188.  
  189.     result = index = 0;
  190.     while ((index < testLen) && !result) {
  191.         cellChar = cellData[index];
  192.         testChar = testData[index];
  193.         if (cellChar >= 'a' && cellChar <='z')
  194.             cellChar -= 0x20;
  195.         if (testChar >= 'a' && testChar <='z')
  196.             testChar -= 0x20;
  197.         if ((cellChar != testChar) && (cellChar < testChar))
  198.             result = 1;
  199.         if ((index == 0) && (cellChar > testChar))
  200.             break;
  201.         index++;
  202.     }
  203.     return(result);
  204. }
  205.  
  206.  
  207. void HandleEvent(EventRecord *event)
  208. {
  209.     switch(event->what) {
  210.         case mouseDown:
  211.             HandleMouseDown(event);
  212.             break;
  213.         case keyDown:
  214.         case autoKey:
  215.             HandleKeyPress(event);
  216.             break;
  217.         case diskEvt:
  218.             HandleDiskInsert(event);
  219.             break;
  220.         case osEvt:
  221.             HandleOSEvent(event);
  222.             break;
  223.         case updateEvt:
  224.             HandleUpdate(event);
  225.             break;
  226.         case kHighLevelEvent:
  227.             AEProcessAppleEvent(event);
  228.             break;
  229.         }
  230. }
  231.  
  232. void HandleDiskInsert(EventRecord *event)
  233. {
  234.     Point    aPoint = {100, 100};
  235.     
  236.     if (HiWrd(event->message) != noErr)
  237.         (void) DIBadMount(aPoint,event->message);
  238. }
  239.  
  240. void HandleKeyPress(EventRecord *event)
  241. {
  242.     char    key;
  243.     
  244.     key = event->message & charCodeMask;
  245.     if (event->modifiers & cmdKey)
  246.         HandleMenuCommand(MenuKey(key));
  247.     else
  248.         DoKeyPress(event);
  249. }
  250.  
  251. void HandleMouseDown(EventRecord *event)
  252. {
  253.     WindowPtr    theWindow;
  254.     short        part = FindWindow(event->where, &theWindow);
  255.     
  256.     switch (part) {
  257.         case inMenuBar:
  258.             HandleMenuCommand(MenuSelect(event->where));
  259.             break;
  260.         case inSysWindow:
  261.             SystemClick(event,theWindow);
  262.             break;
  263.         case inContent:
  264.             if (theWindow != FrontWindow())
  265.                 SelectWindow(theWindow);
  266.             else
  267.                 DoContentClick(event);
  268.             break;
  269.         case inDrag:
  270.             DragWindow(theWindow,event->where,&qd.screenBits.bounds);
  271.             break;
  272.         }
  273. }
  274.  
  275. void HandleOSEvent(EventRecord *event)
  276. {
  277.     switch ((event->message >> 24) & 0x0FF) {
  278.         case suspendResumeMessage:
  279.             gInBackground = (event->message & resumeFlag) == 0;
  280.             DoActivateMainWindow();
  281.             break;
  282.     }
  283. }
  284.  
  285. void DoActivateMainWindow()
  286. {
  287.     WindowPtr        OldPort;
  288.     FMStuff            *FMStore;
  289.  
  290.     FMStore = (FMStuff *) GetWRefCon(gMainWindow);
  291.     GetPort(&OldPort);
  292.     SetPort(gMainWindow);
  293.     if (gInBackground) {
  294.         ActivateControls(FALSE);
  295.         LActivate(FALSE,FMStore->fontList);
  296.         }
  297.     else {
  298.         SelectWindow(gMainWindow);
  299.         ActivateControls(TRUE);
  300.         LActivate(TRUE,FMStore->fontList);
  301.         }
  302.     SetPort(OldPort);
  303. }
  304.  
  305. void HandleUpdate(EventRecord *event)
  306. {
  307.     WindowPtr            theWindow = (WindowPtr) event->message;
  308.     WindowPtr            oldPort;
  309.     FMStuff                *FMStore;
  310.     
  311.     GetPort(&oldPort);
  312.     SetPort(theWindow);
  313.     FMStore = (FMStuff *) GetWRefCon(theWindow);
  314.     BeginUpdate(theWindow);
  315.         UpdateFontList(theWindow);
  316.         UpdateControls(theWindow,theWindow->visRgn);
  317.         UpdateMisc(theWindow);
  318.     EndUpdate(theWindow);
  319.     SetPort(oldPort);
  320. }