home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / gui / macutil.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  16.2 KB  |  486 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /*
  20.     Random Utilities
  21. */
  22. #pragma once
  23.  
  24. #include <Drag.h>
  25. #include <LCaption.h>
  26. #include <LControl.h>
  27. #include <LPane.h>
  28. #include <LGAPopup.h>
  29. #include <Processes.h>
  30.  
  31. #include "PascalString.h"
  32. #include "CWindowMediator.h"
  33. #include "CSimpleTextView.h"
  34.  
  35. class LTextEngine;
  36. class LStdPopupMenu;
  37. class LWindow;
  38.  
  39. #define        MOUSE_HYSTERESIS        6
  40. #define        MOUSE_DRAGGING            1
  41. #define        MOUSE_TIMEOUT            2
  42. #define        MOUSE_UP_EARLY            3
  43.  
  44. typedef struct LO_ImageStruct_struct LO_ImageStruct;
  45. typedef union LO_Element_struct LO_Element;
  46. typedef struct lo_FormElementSelectData_struct lo_FormElementSelectData;
  47.  
  48. // Layout Utilities
  49.  
  50. Boolean TooDifferent( const Point& a, const Point& b );
  51.  
  52. short WaitForMouseAction( const Point& initial, long clickStart, long delay );
  53.  
  54. SPoint32 LocalToImagePoint( LView* view, const Point& local );
  55.  
  56. void RevealInImage( LView* view, SPoint32 selTopLeft, SPoint32 selBotRight );
  57.  
  58. int CalcCurrentSelected( lo_FormElementSelectData* selectData, Boolean fromDefaults );
  59.  
  60. // Utilities
  61.  
  62. void DrawBorder( const Rect& frame, Boolean topleft, int size );
  63.  
  64. Rect CenterInRect( Point thing, Rect destSpace );
  65.  
  66. /*-----------------------------------------------------------------------------
  67.     Panes/Views Coordinate Translation
  68. -----------------------------------------------------------------------------*/
  69. inline Rect PortToLocalRect( LView* view, const Rect& portRect )
  70. {
  71.     Rect        localRect;
  72.     localRect = portRect;
  73.     view->PortToLocalPoint( topLeft( localRect ) );
  74.     view->PortToLocalPoint( botRight( localRect ) );
  75.     return localRect;
  76. }
  77.  
  78. inline Rect LocalToPortRect( LPane* self, const Rect& localRect )
  79. {
  80.     Rect        portRect;
  81.     portRect = localRect;
  82.     self->LocalToPortPoint( topLeft( portRect ) );
  83.     self->LocalToPortPoint( botRight( portRect ) );
  84.     return portRect;
  85. }
  86.  
  87. inline Rect PortToGlobalRect( LPane* self, const Rect& portRect )
  88. {
  89.     Rect        globalRect;
  90.     globalRect = portRect;
  91.     self->PortToGlobalPoint( topLeft( globalRect ) );
  92.     self->PortToGlobalPoint( botRight( globalRect ) );
  93.     return globalRect;
  94. }
  95.  
  96. inline Rect GlobalToPortRect( LPane* self, const Rect& globalRect )
  97. {
  98.     Rect        portRect;
  99.     portRect = globalRect;
  100.     self->GlobalToPortPoint( topLeft( portRect ) );
  101.     self->GlobalToPortPoint( botRight( portRect ) );
  102.     return portRect;
  103. }
  104.  
  105. void StringParamText(CStr255& ioFormatString, // containing ^0, ^1, etc
  106.     const char* ioReplaceText0,
  107.     const char* inReplaceText1 = nil,
  108.     const char* inReplaceText2 = nil,
  109.     const char* inReplaceText3 = nil
  110.     );
  111.  
  112. void StringParamText(CStr255& ioFormatString,
  113.     SInt32 inReplaceNumber0,
  114.     SInt32 inReplaceNumber1,
  115.     SInt32 inReplaceNumber2,
  116.     SInt32 inReplaceNumber3
  117.     ); // as above, but calls ::NumToString on the parameters first.
  118.  
  119. /*-----------------------------------------------------------------------------
  120.     String Copying
  121.     
  122.     CopyString will copy strings. The format is (destination, source) which is
  123.     backwards from the way many functions have it but happens to be the same
  124.     order as operator= does it. IE since most copies look like this...
  125.         foo = bar
  126.     ...I wanted the string assignment/copy to look the same.
  127.         CopyString (foo, bar)
  128.     Also it makes converting between CString-using and Str255-using code easy.
  129.     
  130.     This stuff is overloaded for all pascal/c combinations.
  131. -----------------------------------------------------------------------------*/
  132. const short kCommandKey = 55;
  133. const short kCtlKey = 0x3B;
  134. const short kOptionKey = 58;
  135. const short kShiftKey = 56;
  136.  
  137. unsigned char* CopyString( unsigned char* destination, const unsigned char* source );
  138. unsigned char* CopyString( unsigned char* destination, const char* source );
  139. char* CopyString( char* destination, const unsigned char* source );
  140. char* CopyString( char* destination, const char* source );
  141.  
  142. // Ñ also frees the original if not NULL
  143. void CopyAlloc( char** destination, StringPtr source );
  144.  
  145. // Ñ╩takes colons out of a string, useful for saving files
  146. void StripColons( CStr31& fileName );
  147.  
  148. // Ñ╩strips the given character from a string; if it
  149. // appears twice in a row the second char isn't removed
  150. void StripChar( CStr255& str, char ch );
  151.  
  152. // Ñ Takes a string of text (dragged/pasted), and turns
  153. // it into a URL (strips CR, whitespace preceeding/trailing <>
  154. void CleanUpTextToURL(char* text);
  155.  
  156. // Ñ strips spaces & returns at end of url (happens often
  157. void CleanUpLocationString( CStr255& url );
  158.  
  159. // Ñ TRUE if the key is down. Bypasses the event loop
  160. Boolean IsThisKeyDown( const UInt8 theKey );
  161.  
  162. // Ñ CmdPeriod -  returns true if cmd-. is pressed
  163. Boolean CmdPeriod();
  164.  
  165. // Ñ╩enable/disable pane and refresh it
  166. void SetEnable( LPane* button, Boolean enable );
  167.  
  168. // Ñ changes the string to something acceptable to mac menu manager
  169. void CreateMenuString( CStr255& itemName );
  170.  
  171. // Ñ converts TextEdit handle to text handle that has hard carriage returns. 
  172. Handle TextHandleToHardLineBreaks(CSimpleTextView &inTextView);
  173.  
  174. // Ñ╩sets the number of entries in popup to be shouldBe
  175. void SetMenuSize( LStdPopupMenu* popup, short shouldBe );
  176. void SetMenuSizeForLGAPopup( LGAPopup* popup, short shouldBe );
  177.  
  178. void SetMenuItem( CommandT whichItem, Boolean toState );
  179. // Ñ╩constrain value to be between min and max
  180. void ConstrainTo( const long& min, const long& max, long& value );
  181.  
  182. // Ñ makes copy of the struct. Throws on failure
  183. void * StructCopy(const void * struc, UInt32 size);
  184.  
  185. // Ñ return the free space available on the volume referenced by vRefNum in bytes
  186. unsigned long GetFreeSpaceInBytes( short vRefNum );
  187.  
  188. // Ñ╩sets the std poup to the named item
  189. Boolean SetPopupToNamedItem( LStdPopupMenu* whichMenu, const CStr255& itemText );
  190. Boolean SetLGAPopupToNamedItem( LGAPopup* whichMenu, const CStr255& itemText );
  191.  
  192. // Ñ
  193. void TurnOn( LControl* control );
  194.  
  195. // Ñ
  196. void myStringToNum( const CStr255& inString, Int32* outNum );
  197.  
  198. // Ñ╩sets the captions descriptor to newText
  199. void SetCaptionDescriptor( LCaption* captionToSet, const CStr255& newText,
  200.     const TruncCode    truncWhere );
  201.  
  202. // Ñ get the descriptor as char *
  203. char* GetPaneDescriptor( LPane* pane );
  204.  
  205. // Ñ Use to display the NetHelp window, given the appropriate help topic
  206. void ShowHelp ( const char* inHelpTopic );
  207.  
  208. // Ñ force the window onto the desktop
  209. void ForceWindowOnScreen( LWindow* window );
  210.  
  211. // Ñ grow the window to the height of the screen it is on
  212. void GrowWindowToScreenHeight( LWindow* win );
  213.  
  214. // Ñ╩will all of the window be visible?
  215. Boolean WillBeVisible( const Rect& windowRect );
  216.  
  217. // Ñ stagger the window
  218. void StaggerWindow( LWindow* win );
  219.  
  220. // Ñ is the window fully on a single device?
  221. GDHandle WindowOnSingleDevice( LWindow* win );
  222.  
  223. // Ñ Saving/restoring of the window's default state.
  224. Boolean RestoreDefaultWindowState( LWindow* win );
  225. void StoreDefaultWindowState( LWindow* win );
  226.  
  227. // Ñ Is front window modal? Used for command enabling
  228. Boolean IsFrontWindowModal();
  229.  
  230. // Ñ╩Fetch a window title resource and fill in the current profile name
  231. void GetUserWindowTitle(short id, CStr255& title);
  232.  
  233. // Ñ╩returns the number of pixels (which is inPoints / 120 * iRes)
  234. short PointsToPixels( short inPoints, short iRes );
  235.  
  236. // Ñ returns true if we are the front application
  237. Boolean IsFrontApplication();
  238.  
  239. // Makes Netscape the frontmost application
  240. void MakeFrontApplication();
  241.  
  242. void FrameSubpaneSubtle( LView* view, LPane* pane );
  243.  
  244. // Ñ╩gets the are of window occupied by pane as a rgn
  245. void GetSubpaneRgn( LView* view, LPane* pane, RgnHandle rgn );
  246.  
  247. void GetSubpaneRect( LView* view, LPane* pane, Rect& frame );
  248.  
  249. void WriteVersionTag( LStream* stream, Int32 version );
  250.  
  251. Boolean ReadVersionTag( LStream* stream, Int32 version );
  252.  
  253. // Ñ tries to set a cursor
  254. void TrySetCursor( int whichCursor );
  255.  
  256. void StripNewline( CStr255& msg );
  257. void StripDoubleCRs( CStr255& msg );
  258. // Ñ Converts LF to CR in strings (used to cleanup Netlib stuff). Returns the number of conversions
  259. int ConvertCRtoLF( CStr255& msg );
  260.  
  261. // Ñ Gets font info in a safe way
  262. FontInfo SafeGetFontInfo(ResIDT textTraits);
  263.  
  264. void DrawAntsRect( Rect&r, short mode );
  265. Rect RectFromTwoPoints( Point p1, Point p2 );
  266. Boolean RectInRect(const Rect *inCheckRect, const Rect *inEnclosingRect);
  267.  
  268. void GetDateTimeString( CStr255& dateTime );
  269.  
  270. void FrameButton( const Rect& box, Boolean pushed );
  271. void HiliteRect( const Rect& r );
  272. void HiliteRgn( const RgnHandle& r );
  273. LPane* FindPaneHitBy( const Point& globalMouse );
  274.  
  275.  
  276. //        Good 'ol Mac File Utilities
  277. #if 0
  278. StringPtr    PathnameFromWD (short VRef, StringPtr s);
  279. StringPtr    PathnameFromDirID (long DirID, short VRef, StringPtr s);
  280. #endif
  281.  
  282. char*        PathURLFromProcessSignature (OSType sig, OSType type);
  283.     
  284. // This code is taken from June 97 Dev.CD
  285. OSErr GetHFSFlavorFromPromise
  286.     (DragReference dragRef, ItemReference itemRef,
  287.         HFSFlavor *hfs, Boolean isSupposedlyFromFindFile);
  288.  
  289. // Ñ given a process signature, gets its serial number
  290. ProcessSerialNumber GetPSNBySig( OSType sig );
  291. // Ñ creates an NoProcess serial number
  292. ProcessSerialNumber MakeNoProcessPSN();
  293. // Ñ do we have a process
  294. Boolean HasProcess( const ProcessSerialNumber& psn );
  295. // Ñ given an application signature, finds the process if it is running. From MacApp
  296. OSErr FindProcessBySignature( OSType sig, OSType processType, 
  297.                     ProcessSerialNumber& psn, FSSpec* fileSpec );
  298. // Ñ creates an apple event 
  299. OSErr CreateAppleEvent( OSType suite, OSType id, AppleEvent &event, ProcessSerialNumber targetPSN );
  300. // Ñ do we have an error reply in this event (noErr means that there is no error)
  301. OSErr EventHasErrorReply( AppleEvent& event );
  302. // Drag'n'drop
  303.  
  304. // finds where the file goes in drag to Finder
  305. OSErr GetDropLocationDirectory (AEDesc *dropLocation, long *directoryID, short *volumeID);
  306. // Did we drag to the trash
  307. Boolean DragTargetWasTrash (DragReference dragRef);
  308.  
  309. #define        mTopHalf        1
  310. #define        mBottomHalf        2
  311. #define        mArrowsPict        -4047
  312. #define        mArrowsTopLit    -4046
  313. #define        mArrowsBotLit    -4045
  314. class LArrowControl: public LControl
  315. {
  316. public:
  317.                             LArrowControl( const SPaneInfo& inPaneInfo, MessageT valueMessage );
  318.     void                    HotSpotAction( Int16 inHotSpot, Boolean inCurrInside, Boolean inPrevInside );
  319.     void                    HiliteControl( Int16 inHotSpot, Boolean inCurrInside );
  320.     Boolean                    PointInHotSpot( Point inPoint, Int16 inHotSpot );
  321.     Int16                    FindHotSpot( Point inPoint );
  322.     void                    HotSpotResult( Int16 inHotSpot );
  323.  
  324.     void                    DrawSelf();
  325. protected:
  326.     Int16                    mCurrPict;
  327.     static long                fLastTicks;
  328.     static long                fDelay;
  329.     static long                fHits;
  330. };
  331.  
  332. #if 0
  333. /******************************************************************************
  334.  * class CWindowIterator.
  335.  * Iterates over all the windows
  336.  *****************************************************************************/
  337. class CWindowIterator
  338. {
  339. public:
  340.                 CWindowIterator( OSType sig );    // If '****', iterates over all windows
  341.     
  342.     Boolean        Next( LWindow*& outWindow );
  343. protected:
  344.     virtual Boolean        DoThisWindow( const LWindow* window );
  345.     
  346.     WindowPtr    fNextWindow;
  347.     OSType        fSig;
  348. };
  349.  
  350. class CHyperWin;
  351.  
  352. /******************************************************************************
  353.  * class CHyperIterator.
  354.  * Legacy class that iterates over all the hyperwindows
  355.  *****************************************************************************/
  356. class CHyperIterator: public CWindowIterator
  357. {
  358. public:
  359.                 CHyperIterator();
  360.  
  361.     Boolean        Next( CHyperWin*& outWindow );
  362. };
  363.  
  364. class CHyperMailNewsIterator: public CHyperIterator
  365. {
  366. public:
  367.                 CHyperMailNewsIterator();
  368. protected:
  369.     virtual Boolean        DoThisWindow( const LWindow* window );
  370. };
  371.  
  372. #endif
  373.  
  374. //----------------------------------------------------------------------------------------
  375. // CStringListRsrc: A simple class for managing and accessing strings in STR# resources.
  376. //
  377. // Limitations: this class has no notion of which resource file to be used for accessing
  378. // the strings from the CString list resource.  This could be added as an enhancement.
  379. //----------------------------------------------------------------------------------------
  380.  
  381. class CStringListRsrc {
  382.  
  383. protected:
  384.     short        fStrListID;                    // The ID of the STR# rsrc.
  385.     
  386.     CStr255        fStrListRsrcName;            // The name of the STR# rsrc, this is
  387.                                             // only used when adding the STR# rsrc.
  388.  
  389. public:
  390.     //----------------------------------------------------------------------------------------
  391.     // class-scoped constants
  392.     //----------------------------------------------------------------------------------------
  393.     enum { kDontAddString, kAddString };
  394.  
  395.     //----------------------------------------------------------------------------------------
  396.     // constructors
  397.     //----------------------------------------------------------------------------------------
  398.     CStringListRsrc(short strListID, const CStr255& strListRsrcName) :
  399.             fStrListID(strListID),
  400.             fStrListRsrcName(strListRsrcName)
  401.             {}
  402.         // inline constructor
  403.  
  404.     CStringListRsrc(short strListID) :
  405.             fStrListID(strListID)
  406.             { fStrListRsrcName = CStr255::sEmptyString; }
  407.         // inline constructor
  408.  
  409.     //----------------------------------------------------------------------------------------
  410.     // accessors
  411.     //----------------------------------------------------------------------------------------
  412.     short AppendString(const CStr255& theString);
  413.         // append theString to the STR# resource whose id is fStrListID and return its index
  414.     
  415.     void ClearAll();
  416.         // clears all strings in the STR# resource
  417.  
  418.     short CountStrings() const;
  419.         // returns the number of strings in the STR# resource whose id is fStrListID
  420.  
  421.     short FindString(const CStr255& theString, Boolean addString = kDontAddString);
  422.         // find theString in the STR# resource whose id is fStrListID and return its index
  423.         // if theString is not found in the STR# resource, add it if addString is kAddString
  424.     
  425.     void GetListName(CStr255& itsName);
  426.         //    Returns the name of the STR# list either from its field or if that is empty
  427.         //    from the actual resource
  428.  
  429.     void GetString(short index, CStr255& theString) const;
  430.         // return in theString the "index" CString in the STR# resource whose id is fStrListID
  431.  
  432.     void RemoveAt(short index);
  433.         // removes the CString at the specified index.
  434.     
  435.     void ReplaceAt(const CStr255& theString, short index);
  436.         // replace the CString at "index" with "theString"
  437. };
  438.  
  439. /*-----------------------------------------------------------------------------
  440.     StOpenResFile opens the resource fork and closes it later
  441. -----------------------------------------------------------------------------*/
  442.  
  443. class StOpenResFile
  444. {
  445. public:
  446.                     StOpenResFile( LFile* file, short perms );
  447.                     ~StOpenResFile ();
  448.     operator        short () { return fFile->GetResourceForkRefNum(); }
  449. protected:
  450.     LFile*            fFile;
  451. };
  452.  
  453. /*-----------------------------------------------------------------------------
  454.     StUseResFile uses an open resource fork and stops using it later.
  455. -----------------------------------------------------------------------------*/
  456.  
  457. class StUseResFile
  458. {
  459. public:
  460.                     StUseResFile( short refnum );
  461.                     ~StUseResFile();
  462. protected:
  463.     short            fPrevResFile;
  464. };
  465.  
  466.  
  467.  
  468. /* sets to watch on construction, sets to arrow on destruction */
  469. class StWatchCursor
  470. {
  471. public:
  472.     StWatchCursor();
  473.     ~StWatchCursor();
  474. };
  475.  
  476. /* sets to beachball on construction, spin asynchronously, sets to arrow on destruction */
  477. class StSpinningBeachBallCursor
  478. {
  479. public:
  480.     StSpinningBeachBallCursor();
  481.     ~StSpinningBeachBallCursor();
  482. };
  483. // Routine for making an icon suite for a file given the creator, type and wether you want
  484. // Large,small/. Note that these are DTIcon Types
  485. void GetDesktopIconSuiteFor( OSType inFileCreator, OSType inFileType, short inSize, Handle** ioHandle );
  486.