home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-01-08 | 5.3 KB | 184 lines | [TEXT/CWIE] |
- // HuePopup.cp
- // written by Constantine Spathis
- // [see LDynamicPopupMenu.h for more info]
- //
- // 1/8/95 CS
- // Initial Iteration
-
- // Class Specific Headers
- #include "LDynamicPopupMenu.h"
-
- // Macintosh Headers
- #include <Menus.h>
-
- // ANSI Header
- #include <string.h> // Only included for memcpy.
-
- // Local Global
- int s_numMenu=20000;// Where to start MenuID's for the new menu's created. This must
- // be far away from other menus so they don't clash. Also since
- // MenuID's used in the NewMenu call are shorts we want a large
- // number of useful id's.
-
-
- // Local Function Definitions
- void c2pstrcpy(const char* in_pStr,StringPtr out_pStr);
-
-
- // LDynamicPopupMenu(
- // LStream *in_pStream);
- // 12/13/94 CS - Stream based constructor
- LDynamicPopupMenu::LDynamicPopupMenu(
- LStream* in_pStream) // input stream
- : LStdPopupMenu(in_pStream)
- {
- ;
- }
- // static LDynamicPopupMenu* CreateStdPopupMenuStream(
- // LStream *in_pStream);
- // 1/8/95 CS
- // The idea is to get rid of PowerPlants control and create our own.
- LDynamicPopupMenu*
- LDynamicPopupMenu::CreateLDynamicPopupMenuStream(
- LStream* in_pStream) // input stream
- {
- LDynamicPopupMenu *out_pMenu=new LDynamicPopupMenu(in_pStream);
- OSErr err=noErr;
-
- // Get old menu title
- Str255 sMenuTitle;
- memcpy(sMenuTitle,(**(out_pMenu->mMacControlH)).contrlTitle,
- *((**(out_pMenu->mMacControlH)).contrlTitle)+1);
-
- // Get old menu Control Refcon
- long lRefCon=GetCRefCon(out_pMenu->mMacControlH);
-
- // Dispose of the control PowerPlant made.
- DisposeControl(out_pMenu->mMacControlH);
-
- // Insert a new menu for this control exclusive use.
- MenuHandle hTempMenu=NewMenu(s_numMenu,sMenuTitle);
-
- // Here you really should add some code to check that menu was properly made
- // because NewMenu can fail. Just a suggestion :-)
-
- // xassert(ValidHandle(Handle(hTempMenu)));
-
- InsertMenu(hTempMenu,hierMenu);
-
- out_pMenu->mMinValue=s_numMenu++; // MinValue holds the MenuID of the menu to link the
- // resource to. Make sure to increment the s_numMenu
- // so that next time through we get a unique id.
-
- out_pMenu->mValue=255; // Enable the Menu
-
- out_pMenu->mMaxValue=4+StringWidth(sMenuTitle); // Size of the Control Title string.
- // I like a 2-pixel border around the title
- // for mousedowns.
-
- // And make our own Control using PowerPlants StdControl maker
- out_pMenu->InitStdControl(out_pMenu->mControlKind,
- out_pMenu->mTextTraitsID,
- sMenuTitle,
- lRefCon);
- /***** Plagirism On ********/
- // Popups use the initial values for other purposes. Control Manager
- // determines min/max from the size of the Menu. So now we have to
- // adjust the value, min, and max stored by LControl.
-
- out_pMenu->mValue = ::GetControlValue(out_pMenu->mMacControlH);
- out_pMenu->mMinValue = ::GetControlMinimum(out_pMenu->mMacControlH);
- out_pMenu->mMaxValue = ::GetControlMaximum(out_pMenu->mMacControlH);
-
- if (0 != out_pMenu->mValue) {
- ::SetControlValue(out_pMenu->mMacControlH, 0);
- out_pMenu->mValue = ::GetControlValue(out_pMenu->mMacControlH);
- }
- /***** Plagirism Off ********/
-
- return out_pMenu;
- }
- // LDynamicPopupMenu::~LDynamicPopupMenu()
- // 1/8/95 CS
- // Nothing to do, PowerPlant will clean up
- LDynamicPopupMenu::~LDynamicPopupMenu()
- {
- ;
- }
- // void BroadcastValueMessage()
- // 12/14/94 CS - This overrides the LControl:BroadcastValueMessage() member function to
- // send appropriate messages to the Listener. In your code you should rip this out
- // and do what is appropriate.
- void
- LDynamicPopupMenu::BroadcastValueMessage()
- {
- MenuHandle hMenu=GetMacMenuH();
- BroadcastMessage(mValue, this );
- }
- // void LDynamicPopupMenu::AddCMenuItem(
- // const char* in_cpStr)
- // 1/8/95 CS
- // Adds an item to a popupmenu. The in_cpStr is a c-style string
- void
- LDynamicPopupMenu::AddCMenuItem(
- const char* in_csStr) // C-String for new menu item this translates to p-string
- {
- Str255 menuString;
- c2pstrcpy(in_csStr,menuString);
- AddPMenuItem(menuString);
- }
- // void LDynamicPopupMenu::AddPMenuItem(
- // const char* in_cpStr)
- // 1/8/95 CS
- // Adds an item to a popupmenu. The in_cpStr is a P-style string
- void
- LDynamicPopupMenu::AddPMenuItem(
- const StringPtr in_cpStr) // P-String for new menu item
- {
- MenuHandle hTmpMenu=GetMacMenuH();
-
- AppendMenu(hTmpMenu,in_cpStr);
- short numMenuItems=CountMItems(hTmpMenu);
- mMinValue=1;
- mMaxValue=numMenuItems;
- }
- // void LDynamicPopypMenu::FinishedCreatingMenu(
- // Int32 in_initialItem)
- // 1/8/95 CS
- // Don't forget to call this function when your done adding things
- // to your menu.
- void
- LDynamicPopupMenu::FinishedCreatingMenu(
- Int32 in_initialItem) // Initial Menu Item to show
- {
- SetStdMinAndMax();
- SetValue(in_initialItem);
- }
- /**************** Utility Functions **********************/
-
- // void c2pstrcpy(
- // StringPtr out_pStr,
- // const char* in_pStr)
- // 1/8/95 CS
- // Utility function that takes an c-string as input and copies it to out_pStr
- // converting it in the process to a p-string.
- // NOTE: This is not a world-script ready function & the storage for
- // out_pStr is not allocated it is assumed to be there.
- void c2pstrcpy(
- const char* in_pStr, // Input c string
- StringPtr out_pStr) // Output p-string
- {
- unsigned char len=0;
- char* dstLoc=(char*)(out_pStr+1);
-
- while(*in_pStr){
- *dstLoc++=*in_pStr++;
- len++;
- // You may want to handle this different.
- if (len>255){
- Debugger();
- }
- }
- *out_pStr=len;
- }
-