home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-11-30 | 11.4 KB | 316 lines | [TEXT/KAHL] |
- /******************************************************************************
- CChoicesButton.c
-
- The ChoicesButton Class
-
- A button that can make a window/dialog bigger (“More Choices”) or
- smaller (“Fewer Choices”). When More Choices is clicked, a range of
- view items, set with the SetViewsToHide method, will be hidden. Another
- range of items, specified with the SetViewsToMoveUp method, will move
- upwards so that the top of the highest of the items to be moved
- is where the highest of the items that were hidden was.
- The bottom of the window enclosing the items will move up as
- well. These effects will be reversed when the button is hit again.
- This approach allows you to have a choices button that hides items
- in the middle of the dialog window.
-
- Ordinarily, you will have at least one item to be moved up, the choices
- button itself. However, if the choices button for some reason is on the
- top of the dialog, and the items to be hidden are at the bottom, then
- to ensure that the window itself shrinks when items are hidden, you should
- add an invisible item to the window, such that the top of the item is the
- bottom of the lowest item that you want hidden. Then specify this item as
- the only item to be moved up.
-
- If you wish the ChoicesButton dialog to do more than simply hide and
- move items (for example, you want some items to change into other items,
- or you want a whole new dialog setup) then you can override this class’s
- DoFewerChoices and DoMoreChoices methods.
-
- You can create such a button in three ways: through a control template,
- dynamically, or through an overloaded dialog item. With only the last
- of these methods can you specify in the resource file the views that
- should be hidden and moved up.
-
- You should always create a window in the more choices state (i.e., so
- that the button reads “Fewer Choices”). If you want the window to be
- in the fewer choices state initially, then call the DoFewerChoices
- method before the window becomes visible.
-
- SUPERCLASS = CButton
-
- Copyright © 1993 Michael Abramowicz. All rights reserved.
-
- ******************************************************************************/
-
- #include "CChoicesButton.h"
-
- /**** C O N S T R U C T I O N M E T H O D S ****/
-
- /******************************************************************************
- IChoicesButton
-
- Initialize a ChoicesButton object from a control resource. Does not
- automatically specify which dialog items to hide when More Choices
- is clicked, and which buttons to move up when that button is clicked.
- ******************************************************************************/
-
- void CChoicesButton::IChoicesButton(
- short CNTLid, /* CNTL resource ID */
- CView *anEnclosure, /* Enclosing view */
- CBureaucrat *aSupervisor) /* Boss in chain of command */
- {
- inherited::IButton(CNTLid, anEnclosure, aSupervisor);
- IChoicesButtonX();
- }
-
- /******************************************************************************
- INewChoicesButton
-
- Initialize a Choices button from dynamic parameters, rather than from a
- CNTL resource. The button defaults to the “More Choices” setting.
- ******************************************************************************/
-
- void CChoicesButton::INewChoicesButton( short aWidth, short aHeight,
- short aHEncl, short aVEncl, StringPtr title, Boolean
- fVisible, short procID, CView *anEnclosure,
- CBureaucrat *aSupervisor)
- {
- inherited::INewButton( aWidth, aHeight, aHEncl, aVEncl, title,
- fVisible, procID, anEnclosure, aSupervisor);
- IChoicesButtonX();
- } /* CChoicesButton::INewButton */
-
- /******************************************************************************
- IChoicesButtonX
-
- Set default values for the choices button. By default, the dialog is set
- to be big (i.e., “Fewer Choices” is the button text).
- ******************************************************************************/
-
- void CChoicesButton::IChoicesButtonX( void)
- {
- SetClickCmd(cmdToggleChoicesButton);
-
- firstToHide = -1;
- lastToHide = -2;
-
- firstToMoveUp = -1;
- lastToMoveUp = -2;
-
- dialogIsBig = TRUE;
- UpdateButtonText();
- }
-
- /******************************************************************************
- IViewTemp {OVERRIDE}
-
- Initialize a choices button using a template
- ******************************************************************************/
- void CChoicesButton::IViewTemp(
- CView *anEnclosure, /* Enclosing view */
- CBureaucrat *aSupervisor, /* Boss in chain of command */
- Ptr viewData) /* Template with View data */
- {
- ChoicesButtonTempP choicesTemp = (ChoicesButtonTempP) viewData;
- PaneTemp thePaneTemp = choicesTemp->sPaneTemp;
-
- INewChoicesButton(thePaneTemp.width, thePaneTemp.height,
- thePaneTemp.hEncl, thePaneTemp.vEncl, "\p",
- thePaneTemp.sViewTemp.visible != 0, pushButProc, anEnclosure,
- aSupervisor);
-
- SetViewsToHide(choicesTemp->firstToHide, choicesTemp->lastToHide);
- SetViewsToMoveUp(choicesTemp->firstToMoveUp, choicesTemp->lastToMoveUp);
- }
-
- /**** I N F O R M A T I O N A N D C O N T R O L M E T H O D S ****/
-
- /******************************************************************************
- UpdateButtonText
-
- Updates the text of the button to read either “Fewer Choices” or “More
- Choices,” as appropriate.
- ******************************************************************************/
-
- void CChoicesButton::UpdateButtonText (void)
- {
- Str255 buttonString; /* “Fewer Choices” or “More Choices” */
-
- GetIndString(buttonString, kChoicesButtonStrings, dialogIsBig ? 1 : 2);
- SetTitle(buttonString);
- }
-
- /******************************************************************************
- IsDialogBig
- ******************************************************************************/
-
- Boolean CChoicesButton::IsDialogBig (void)
- {
- return dialogIsBig;
- }
-
- /******************************************************************************
- SetViewsToHide
-
- Indicate which views to hide when the “Fewer Choices” button is hit. These
- views must be numbered consecutively.
- ******************************************************************************/
-
- void CChoicesButton::SetViewsToHide (long first, long last)
- {
- firstToHide = first;
- lastToHide = last;
- }
-
- /******************************************************************************
- SetViewsToMoveUp
-
- Indicate which views to move up when the “Fewer Choices” button is hit. These
- views must be numbered consecutively. This button should be included among
- these views.
- ******************************************************************************/
-
- void CChoicesButton::SetViewsToMoveUp (long first, long last)
- {
- firstToMoveUp = first;
- lastToMoveUp = last;
- }
-
- /**** C L I C K H A N D L I N G M E T H O D S ****/
-
- /******************************************************************************
- DoFewerChoices
-
- Hide items that should not be visible in the fewer choices mode, and move up
- the items beneath those items so that there is no empty space in the dialog.
- ******************************************************************************/
-
- void CChoicesButton::DoFewerChoices (void)
- {
- CWindow *theWindow;
- CPane *itemToHide, *itemToMove;
- long count;
- long highestItemToHide;
- long highestItemToMove;
- LongRect windowSizeLongRect;
- Rect windowSize;
-
- // Find the window enclosing the button by taking the item’s
- // enclosure and then that item’s enclosure, etc. until a window
- // is found.
- theWindow = (CWindow *) itsEnclosure;
- while (!member(theWindow, CWindow))
- theWindow = (CWindow *) itsEnclosure->itsEnclosure;
-
- // Determine distance to move up by calculating the difference
- // between the top of the highest item to be hidden and the highest
- // item to be moved.
- if ((firstToHide <= lastToHide) && (firstToMoveUp <= lastToMoveUp)) {
- /* Default variables determining the highest item in each range
- to the first item in each range. This is necessary since we need
- something to compare other items’ tops to. */
- highestItemToHide = ((CPane *) theWindow->FindViewByID(firstToHide))->vEncl;
- highestItemToMove = ((CPane *) theWindow->FindViewByID(firstToMoveUp))->vEncl;
- /* Determine the top of the items to hide. */
- for (count = firstToHide + 1; count <= lastToHide; count++) {
- itemToHide = (CPane *) theWindow->FindViewByID(count);
- if (itemToHide->vEncl < highestItemToHide)
- highestItemToHide = itemToHide->vEncl;
- }
- /* Determine the top of the items to move up. */
- for (count = firstToMoveUp + 1; count <= lastToMoveUp; count++) {
- itemToMove = (CPane *) theWindow->FindViewByID(count);
- if (itemToMove->vEncl < highestItemToMove)
- highestItemToHide = itemToMove->vEncl;
- }
- /* Put the difference in an instance variable. */
- distanceToMove = highestItemToMove - highestItemToHide;
- }
- else
- distanceToMove = 0; /* Can’t compute distance to move. */
-
- // Adjust the bottom of the window containing the items
- theWindow->GetInterior(&windowSizeLongRect);
- LongToQDRect(&windowSizeLongRect, &windowSize);
- theWindow->ChangeSize(windowSize.right - windowSize.left,
- windowSize.bottom - windowSize.top - distanceToMove);
-
- // Switch the choices button to say “More Choices”
- dialogIsBig = FALSE;
- UpdateButtonText();
-
- // Hide the items to be hidden.
- for (count = firstToHide; count <= lastToHide; count++) {
- itemToHide = (CPane *) theWindow->FindViewByID(count);
- itemToHide->Hide();
- }
-
- // Move the items to be moved.
- for (count = firstToMoveUp; count <= lastToMoveUp; count++) {
- itemToMove = (CPane *) theWindow->FindViewByID(count);
- itemToMove->Offset(0, 0 - distanceToMove, TRUE);
- }
- }
-
- /******************************************************************************
- DoMoreChoices
-
- Restore all hidden items, and move the items that moved up back down.
- ******************************************************************************/
-
- void CChoicesButton::DoMoreChoices (void)
- {
- CWindow *theWindow;
- CPane *itemToShow, *itemToMove;
- long count;
- LongRect windowSizeLongRect;
- Rect windowSize;
-
- // Find the window enclosing the button by taking the item’s
- // enclosure and then that item’s enclosure, etc. until a window
- // is found.
- theWindow = (CWindow *) itsEnclosure;
- while (!member(theWindow, CWindow))
- theWindow = (CWindow *) itsEnclosure->itsEnclosure;
-
- // Adjust the bottom of the window containing the items
- theWindow->GetInterior(&windowSizeLongRect);
- LongToQDRect(&windowSizeLongRect, &windowSize);
- theWindow->ChangeSize(windowSize.right - windowSize.left,
- windowSize.bottom - windowSize.top + distanceToMove);
-
- // Switch the choices button to say “Fewer Choices”
- dialogIsBig = TRUE;
- UpdateButtonText();
-
- // Show the items that need to be shown.
- for (count = firstToHide; count <= lastToHide; count++) {
- itemToShow = (CPane *) theWindow->FindViewByID(count);
- itemToShow->Show();
- }
-
- // Move the items to be moved.
- for (count = firstToMoveUp; count <= lastToMoveUp; count++) {
- itemToMove = (CPane *) theWindow->FindViewByID(count);
- itemToMove->Offset(0, 0 + distanceToMove, TRUE);
- }
- }
-
- /******************************************************************************
- DoGoodClick {OVERRIDE}
-
- Respond to a click in the button by changing the dialog as appropriate.
- ******************************************************************************/
-
- void CChoicesButton::DoGoodClick(
- short whichPart) /* Will always be inButton */
- {
- if (clickCmd == cmdToggleChoicesButton) {
- /* Issue the appropriate command */
- if (dialogIsBig)
- DoFewerChoices();
- else
- DoMoreChoices();
- }
- }