home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / scrollingeditfield / scrollingtextuserpane.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  14.7 KB  |  460 lines

  1. /*
  2.     File: ScrollingTextUserPane.c
  3.     
  4.     Description:
  5.         This file contains the main application program for the ScrollingTextUserPane
  6.     example.  This application creates a dialog window and installs a scrolling
  7.     text user pane in the dialog.  You will notice that since the implementation
  8.     of these scrolling text fields is based on the user pane control manager
  9.     api, this program makes very few calls for maintaining the edit
  10.     fields.  those calls are made by the control manager. 
  11.     
  12.     Routines in this file are responsible for handling events directed
  13.     at the application.
  14.     
  15.     Where calls are explicitly made to routines defined in the
  16.     file STUPControl.h, I have added comments beginning with
  17.     the phrase:
  18.         Call to STUPControl.h
  19.  
  20.     Copyright:
  21.         © Copyright 2000 Apple Computer, Inc. All rights reserved.
  22.     
  23.     Disclaimer:
  24.         IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  25.         ("Apple") in consideration of your agreement to the following terms, and your
  26.         use, installation, modification or redistribution of this Apple software
  27.         constitutes acceptance of these terms.  If you do not agree with these terms,
  28.         please do not use, install, modify or redistribute this Apple software.
  29.  
  30.         In consideration of your agreement to abide by the following terms, and subject
  31.         to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  32.         copyrights in this original Apple software (the "Apple Software"), to use,
  33.         reproduce, modify and redistribute the Apple Software, with or without
  34.         modifications, in source and/or binary forms; provided that if you redistribute
  35.         the Apple Software in its entirety and without modifications, you must retain
  36.         this notice and the following text and disclaimers in all such redistributions of
  37.         the Apple Software.  Neither the name, trademarks, service marks or logos of
  38.         Apple Computer, Inc. may be used to endorse or promote products derived from the
  39.         Apple Software without specific prior written permission from Apple.  Except as
  40.         expressly stated in this notice, no other rights or licenses, express or implied,
  41.         are granted by Apple herein, including but not limited to any patent rights that
  42.         may be infringed by your derivative works or by other works in which the Apple
  43.         Software may be incorporated.
  44.  
  45.         The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  46.         WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  47.         WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  48.         PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  49.         COMBINATION WITH YOUR PRODUCTS.
  50.  
  51.         IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  52.         CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  53.         GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  54.         ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  55.         OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  56.         (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  57.         ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  58.  
  59.     Change History (most recent first):
  60.         Fri, Jan 28, 2000 -- created
  61. */
  62.  
  63.  
  64. #include "STUPControl.h"
  65.  
  66. #include "ScrollingTextUserPane.h"
  67.  
  68. #include <Menus.h>
  69. #include <Windows.h>
  70. #include <Dialogs.h>
  71. #include <Events.h>
  72. #include <Fonts.h>
  73. #include <SegLoad.h>
  74. #include <Resources.h>
  75. #include <Balloons.h>
  76. #include <Devices.h>
  77. #include <AppleEvents.h>
  78. #include <StdIO.h>
  79. #include <StdArg.h>
  80. #include <string.h>
  81. #include <ToolUtils.h>
  82. #include <Appearance.h>
  83. #include <Navigation.h>
  84. #include <StandardFile.h>
  85. #include <PLStringFuncs.h>
  86. #include <TextUtils.h>
  87. #include <ControlDefinitions.h>
  88. #include <LowMem.h>
  89. #include <TextEdit.h>
  90. #include <Errors.h>
  91. #include <Scrap.h>
  92.  
  93.  
  94. Boolean gRunning = true;
  95.  
  96.  
  97. /* DoDialogSelect is called whenever DialogSelect indicates that an event
  98.     has occured in our dialog.  In this routine, we only look for clicks
  99.     in the ok button.  All of the other processing necessary for maintaining
  100.     the appearance and behavior of the scrolling edit fields is taken care of
  101.     by the control manager. */
  102. static void DoDialogSelect(EventRecord *ev, DialogPtr theDialog, short itemHit) {
  103.     if (itemHit == ok) gRunning = false;
  104. }
  105.  
  106.  
  107.  
  108. /* ResetMenus is called immediately before all calls to
  109.     MenuSelect or MenuKey.  In this routine, we re-build
  110.     or enable the menus as appropriate depending on the
  111.     current environment */
  112. static void ResetMenus(void) {
  113.     Boolean doEnableEdit;
  114.     MenuHandle theEditMenu;
  115.     WindowPtr target;
  116.     ControlHandle theFocus;
  117.     static Boolean gEditMenuEnabled = false;
  118.         /* set up locals */
  119.     doEnableEdit = false;
  120.         /* if the frontmost window's keyboard focus is a
  121.         STUP control, then we enable the edit menu. */
  122.     if ((target = FrontWindow()) != NULL)
  123.         if (GetKeyboardFocus(target, &theFocus) == noErr) {
  124.             if (IsSTUPControl(theFocus)) {        /* Call to STUPControl.h */
  125.                 doEnableEdit = true;
  126.             }
  127.         }
  128.         /* enable/disable the edit menu and re-draw it
  129.         only if the enable state has changed. */
  130.     if (doEnableEdit != gEditMenuEnabled) {
  131.         gEditMenuEnabled = doEnableEdit;
  132.         theEditMenu = GetMenuHandle(mEdit);
  133.         if (gEditMenuEnabled)
  134.             EnableMenuItem(theEditMenu, 0);
  135.         else DisableMenuItem(theEditMenu, 0);
  136.         DrawMenuBar();
  137.     }
  138. }
  139.  
  140.  
  141. /* DoMenuCommand is called in response to MenuKey
  142.     or MenuSelect.  Here, we dispatch the menu command
  143.     to its appropriate handler, or if it's a small action
  144.     we do it here. */
  145. static void DoMenuCommand(long rawMenuSelectResult) {
  146.     short menu, item;
  147.         /* decode the MenuSelect result */
  148.     menu = (rawMenuSelectResult >> 16);
  149.     if (menu == 0) return;
  150.     item = (rawMenuSelectResult & 0x0000FFFF);
  151.         /* dispatch on result */
  152.     switch (menu) {
  153.     
  154.             /* apple menu commands */
  155.         case mApple:
  156.             if (item == iAbout)
  157.                 ParamAlert(kAboutBoxAlert, NULL, NULL);
  158.             break;
  159.             
  160.                 /* file menu commands */
  161.         case mFile:
  162.             if (item == iQuit)
  163.                 gRunning = false;
  164.             break;
  165.             
  166.                 /* edit menu commands.  If the current keyboard focus
  167.                 in the frontmost window is a STUP control, then we send
  168.                 the edit command to the control.  */
  169.         case mEdit:
  170.             {    WindowPtr target;
  171.                 ControlHandle theFocus;
  172.                     /* get the front window */
  173.                 target = FrontWindow();
  174.                 if (target != NULL) {
  175.                         /* find the focus  and check to see if it's
  176.                         a STUP control */
  177.                     if (GetKeyboardFocus(target, &theFocus) == noErr) {
  178.                         if (IsSTUPControl(theFocus)) {        /* Call to STUPControl.h */
  179.                                 /* pass the editing command along to the STUP control */
  180.                             switch (item) {
  181.                                 case iCut:        STUPDoEditCommand(theFocus, kSTUPCut); break;
  182.                                 case iCopy:    STUPDoEditCommand(theFocus, kSTUPCopy); break;
  183.                                 case iPaste:    STUPDoEditCommand(theFocus, kSTUPPaste); break;
  184.                                 case iClear:    STUPDoEditCommand(theFocus, kSTUPClear); break;
  185.                             }
  186.                         }
  187.                     }
  188.                 }
  189.             }
  190.             break;
  191.     }
  192.         /* unhilite the menu bar */
  193.     HiliteMenu(0);
  194. }
  195.  
  196.  
  197.  
  198.  
  199. /* HandleMouseDown is called for mouse down events.  Processing of
  200.     mouse down events in the HTML rendering area of windows is 
  201.     handled by the HTMLRenderinLib, but clicks in the controls and
  202.     other parts of the windows are handled here. */
  203. static void HandleMouseDown(EventRecord *ev) {
  204.     WindowPtr theWindow;
  205.     short partcode;
  206.     partcode = FindWindow(ev->where, &theWindow);
  207.     switch (partcode) {
  208.             /* inside the window's content area */
  209.         case inContent:
  210.             if (theWindow != FrontWindow()) {
  211.                     /* if it's not the frontmost window,
  212.                     then make it the frontmost window. */
  213.                 SelectWindow(theWindow);
  214.             } else {
  215.                     /* otherwise, if it's a rendering window,
  216.                     pass the click along to the window. */
  217.                 Point where;
  218.                 SetPort(GetWindowPort(theWindow));
  219.                 where = ev->where;
  220.                 GlobalToLocal(&where);
  221.             }
  222.             break;
  223.             
  224.             /* menu bar clicks */
  225.         case inMenuBar:
  226.             ResetMenus();
  227.             DoMenuCommand(MenuSelect(ev->where));
  228.             break;
  229.             
  230.             /* track clicks in the close box */
  231.         case inGoAway:
  232.             if (TrackGoAway(theWindow, ev->where)) {
  233.                 gRunning = false;
  234.             }
  235.             break;
  236.             
  237.             /* allow window drags */
  238.         case inDrag:
  239.             {    Rect boundsRect = {0,0, 32000, 32000};
  240.                 DragWindow(theWindow, ev->where, &boundsRect);
  241.             }
  242.             break;
  243.             
  244.             /* allow window drags */
  245.         case inGrow:
  246.             {    Rect sizerect;
  247.                 long grow_result;
  248.                 SetRect(&sizerect, 300, 150, 32767, 32767);
  249.                 grow_result = GrowWindow(theWindow, ev->where, &sizerect);
  250.                 if (grow_result != 0) {
  251.                     SizeWindow(theWindow, LoWord(grow_result), HiWord(grow_result), true);
  252.                 }
  253.             }
  254.             break;
  255.             
  256.             /* zoom box clicks.  NOTE:  since the rendering window
  257.             always sets the standard rectangle to the 'best size' for
  258.             displaying the current HTML window, the inZoomOut partcode
  259.             will zoom the window to that size rather than the entire screen.*/
  260.         case inZoomIn:
  261.         case inZoomOut:
  262.             if (TrackBox(theWindow, ev->where, partcode)) {
  263.                 Rect r;
  264.                 CGrafPtr gp;
  265.                 gp = GetWindowPort(theWindow);
  266.                 GetPortBounds(gp, &r);
  267.                 SetPort(gp);
  268.                 EraseRect(&r);
  269.                 ZoomWindow(theWindow, partcode, true);
  270.             }
  271.             break;
  272.             
  273.     }
  274. }
  275.  
  276.  
  277.  
  278.  
  279. /* HandleEvent is the main event handling routine for the
  280.     application.  ev points to an event record returned by
  281.     WaitNextEvent. */
  282. void HandleEvent(EventRecord *ev) {
  283.     WindowPtr target;
  284.         /* redraw the edit menu if needed... */
  285.     ResetMenus();
  286.         /* process menu key events */
  287.     if (((ev->what == keyDown) || (ev->what == autoKey)) && ((ev->modifiers & cmdKey) != 0)) {
  288.         ResetMenus();
  289.         DoMenuCommand(MenuKey((char) (ev->message & charCodeMask)));
  290.         ev->what = nullEvent;
  291.     }
  292.     
  293.         /* for dialog events, we simply pass the event along to
  294.         our dialog event handler.  NOTE:  because we defined the editable
  295.         text fields as user pane items, the control manager takes care
  296.         of calling all of the routines necessary for maintaining the controls. */
  297.     if (IsDialogEvent(ev)) {
  298.         DialogPtr theDialog;
  299.         short itemHit;
  300.         if (DialogSelect(ev, &theDialog, &itemHit))
  301.             DoDialogSelect(ev, theDialog, itemHit);
  302.     }
  303.  
  304.         /* process other event types */
  305.     switch (ev->what) {
  306.     
  307.             /* for null events, track the cursor. */
  308.         case nullEvent:
  309.             break;
  310.             
  311.             /* the application may be switching in to the forground
  312.             or into the background.  Either way, we need to activate
  313.             the frontmost window accordingly. */
  314.         case osEvt:
  315.                 
  316.             if (((ev->message >> 24) & 255) == mouseMovedMessage) {
  317.                 /* do some cursor checking */
  318.             } else if (((ev->message >> 24) & 255) == suspendResumeMessage) {
  319.                 Boolean switchingIn;
  320.                 switchingIn = ((ev->message & resumeFlag) != 0);
  321.                     /* send an activate event to the frontmost window */
  322.                 target = FrontWindow();
  323.             }
  324.             break;
  325.             
  326.             /* for activate events we call the window's activate event
  327.             handler. */
  328.         case activateEvt:
  329.             target = (WindowPtr) ev->message;
  330.             break;
  331.             
  332.             /* for update events we call the window's update event
  333.             handler. if the window is of an unknown type, we ignore the
  334.             event. */
  335.         case updateEvt:
  336.             target = (WindowPtr) ev->message;
  337.             BeginUpdate(target);
  338.             EndUpdate(target);
  339.             break;    
  340.             
  341.             /* for mouse events we call the the HandleMouseDown routine
  342.             defined above. */
  343.         case mouseDown:
  344.             HandleMouseDown(ev);
  345.             break;
  346.         
  347.             /* for key down events we call the window's key down event
  348.             handler. */
  349.         case keyDown:
  350.         case autoKey:
  351.             target = FrontWindow();
  352.             break;
  353.         
  354.             /* Apple events. */
  355.         case kHighLevelEvent:
  356.             AEProcessAppleEvent(ev);
  357.             break;
  358.     }
  359. }
  360.  
  361.  
  362. /* MyIdleInteractProc is the idle procedure called by AEInteractWithUser while we are waiting
  363.     for the application to be pulled into the forground.  It simply passes the event along
  364.     to HandleNextEvent */
  365. static pascal Boolean MyIdleInteractProc(EventRecord *theEvent, long *sleepTime, RgnHandle *mouseRgn) {
  366.     HandleEvent(theEvent);
  367.     return ( ! gRunning ); /* quit waiting if we're not running */
  368. }
  369.  
  370.  
  371. /* ParamAlert is a general alert handling routine.  If Apple events exist, then it
  372.     calls AEInteractWithUser to ensure the application is in the forground, and then
  373.     it displays an alert after passing the s1 and s2 parameters to ParamText. */
  374. short ParamAlert(short alertID, StringPtr s1, StringPtr s2) {
  375.     AEIdleUPP aeIdleProc;
  376.     OSStatus err;
  377.     aeIdleProc = NewAEIdleUPP(MyIdleInteractProc);
  378.     if (aeIdleProc == NULL) { err = memFullErr; goto bail; }
  379.     err = AEInteractWithUser(kNoTimeOut, NULL, aeIdleProc);
  380.     if (err != noErr) goto bail;
  381.     ParamText(s1, s2, NULL, NULL);
  382.     err = Alert(alertID, NULL);
  383.     DisposeAEIdleUPP(aeIdleProc);
  384.     return err;
  385. bail:
  386.     if (aeIdleProc != NULL) DisposeAEIdleUPP(aeIdleProc);
  387.     return err;
  388. }
  389.  
  390.  
  391.  
  392.  
  393.  
  394. /* QuitAppleEventHandler is our quit Apple event handler.  this routine
  395.     is called when a quit Apple event is sent to our application.  Here,
  396.     we set the gRunning flag to false. NOTE:  it is not appropriate to
  397.     call ExitToShell here.  Instead, by setting the flag to false we
  398.     fall through the bottom of our main event loop.  */
  399. static pascal OSErr QuitAppleEventHandler(const AppleEvent *appleEvt, AppleEvent* reply, unsigned long refcon) {
  400.  
  401.     gRunning = false;    
  402.             
  403.     return noErr;
  404. }
  405.  
  406.  
  407.  
  408.  
  409.  
  410.     /* the main program */
  411.  
  412. int main(void) {
  413.     ControlHandle field1, field2, field3;
  414.     DialogPtr dialog;
  415.         /* set up the cursor, register */
  416.     InitCursor();
  417.     RegisterAppearanceClient();
  418.         
  419.         /* install our event handlers */
  420.     AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(QuitAppleEventHandler), 0, false);
  421.  
  422.         /* set up the menu bar */
  423.     SetMenuBar(GetNewMBar(kMenuBarID));
  424.     DrawMenuBar();
  425.         /* open the dialog window */
  426.     dialog = GetNewDialog(kMainDialogBox, NULL, (WindowPtr)(-1));
  427.         /* get the user pane controls from the dialog record */
  428.     GetDialogItemAsControl(dialog, kEditItemOne, &field1);
  429.     GetDialogItemAsControl(dialog, kEditItemTwo, &field2);
  430.     GetDialogItemAsControl(dialog, kEditItemThree, &field3);
  431.         /* initialize the user pane controls for use as scrolling text fields */
  432.     STUPOpenControl(field1);        /* Call to STUPControl.h */
  433.     STUPOpenControl(field2);
  434.     STUPOpenControl(field3);
  435.         /* set the text displayed in them */
  436.     STUPFillControl(field1, kEditItemOneText);
  437.     STUPFillControl(field2, kEditItemTwoText);
  438.     STUPFillControl(field3, kEditItemThreeText);
  439.         /* show the window */
  440.     ShowWindow(GetDialogWindow(dialog));
  441.         /* loop processing events until.... */
  442.     do {    EventRecord ev;
  443.             /* process the next event */
  444.         if ( ! WaitNextEvent(everyEvent, &ev, GetCaretTime(), NULL) ) ev.what = nullEvent;
  445.         HandleEvent(&ev);
  446.     } while (gRunning);
  447.         /* free up our scrolling text items.  NOTE:  we only clean up
  448.         the stuff we allocated.  We don't actually dispose of the user
  449.         pane controls themselves because they are owned by the
  450.         dialog manager. */
  451.     STUPCloseControl(field1);    /* Call to STUPControl.h */
  452.     STUPCloseControl(field2);
  453.     STUPCloseControl(field3);
  454.         /* get rid of the dialog */
  455.     DisposeDialog(dialog);
  456.         /* done */
  457.     UnregisterAppearanceClient();
  458.     ExitToShell();
  459.     return 0;
  460. }