-#include "wx/defs.h"
-#include "wx/mac/uma.h"
-#include "wx/mac/aga.h"
-
-#ifdef __UNIX__
- #include <NavigationServices/Navigation.h>
-#else
- #include <Navigation.h>
-#endif
-
-// init
-
-static bool sUMAHasAppearance = false ;
-static long sUMAAppearanceVersion = 0 ;
-extern int gAGABackgroundColor ;
-bool UMAHasAppearance() { return sUMAHasAppearance ; }
-long UMAGetAppearanceVersion() { return sUMAAppearanceVersion ; }
-
-static bool sUMAHasWindowManager = false ;
-static long sUMAWindowManagerAttr = 0 ;
-
-bool UMAHasWindowManager() { return sUMAHasWindowManager ; }
-long UMAGetWindowManagerAttr() { return sUMAWindowManagerAttr ; }
-void UMACleanupToolbox()
-{
-#if UMA_USE_APPEARANCE
- if ( sUMAHasAppearance )
- {
- UnregisterAppearanceClient() ;
- }
-#endif
- if ( NavServicesAvailable() )
- {
- NavUnload() ;
- }
-}
-void UMAInitToolbox( UInt16 inMoreMastersCalls )
-{
-#if !TARGET_CARBON
- ::MaxApplZone();
- for (long i = 1; i <= inMoreMastersCalls; i++)
- ::MoreMasters();
-
- ::InitGraf(&qd.thePort);
- ::InitFonts();
- ::InitMenus();
- ::TEInit();
- ::InitDialogs(0L);
- ::FlushEvents(everyEvent, 0);
- ::InitCursor();
- long total,contig;
- PurgeSpace(&total, &contig);
-#else
- InitCursor();
-#endif
-
-#if UMA_USE_APPEARANCE
- long theAppearance ;
- if ( Gestalt( gestaltAppearanceAttr, &theAppearance ) == noErr )
- {
- sUMAHasAppearance = true ;
- RegisterAppearanceClient();
- if ( Gestalt( gestaltAppearanceVersion, &theAppearance ) == noErr )
- {
- sUMAAppearanceVersion = theAppearance ;
- }
- else
- {
- sUMAAppearanceVersion = 0x0100 ;
- }
- }
-#endif // UMA_USE_APPEARANCE
-#if UMA_USE_8_6
-#if UMA_USE_WINDOWMGR
- if ( Gestalt( gestaltWindowMgrAttr, &sUMAWindowManagerAttr ) == noErr )
- {
- sUMAHasWindowManager = sUMAWindowManagerAttr & gestaltWindowMgrPresent ;
- }
-#endif // UMA_USE_WINDOWMGR
-#endif
-
-#ifndef __UNIX__
- if ( sUMAHasWindowManager )
- InitFloatingWindows() ;
- else
- InitWindows();
-#endif
-
- if ( NavServicesAvailable() )
- {
- NavLoad() ;
- }
-}
-
-// process manager
-long UMAGetProcessMode()
-{
- OSErr err ;
- ProcessInfoRec processinfo;
- ProcessSerialNumber procno ;
-
- procno.highLongOfPSN = NULL ;
- procno.lowLongOfPSN = kCurrentProcess ;
- processinfo.processInfoLength = sizeof(ProcessInfoRec);
- processinfo.processName = NULL;
- processinfo.processAppSpec = NULL;
-
- err = ::GetProcessInformation( &procno , &processinfo ) ;
- wxASSERT( err == noErr ) ;
- return processinfo.processMode ;
-}
-
-bool UMAGetProcessModeDoesActivateOnFGSwitch()
-{
- return UMAGetProcessMode() & modeDoesActivateOnFGSwitch ;
-}
-
-// menu manager
-
-void UMASetMenuTitle( MenuRef menu , StringPtr title )
-{
-#if !TARGET_CARBON
- long size = GetHandleSize( (Handle) menu ) ;
- const long headersize = 14 ;
- int oldlen = (**menu).menuData[0] + 1;
- int newlen = title[0] + 1 ;
-
- if ( oldlen < newlen )
- {
- // enlarge before adjusting
- SetHandleSize( (Handle) menu , size + (newlen - oldlen ) );
- }
-
- if ( oldlen != newlen )
- memmove( (char*) (**menu).menuData + newlen , (char*) (**menu).menuData + oldlen , size - headersize - oldlen ) ;
-
- memcpy( (char*) (**menu).menuData , title , newlen ) ;
- if ( oldlen > newlen )
- {
- // shrink after
- SetHandleSize( (Handle) menu , size + (newlen - oldlen ) ) ;
- }
-#else
- SetMenuTitle( menu , title ) ;
-#endif
-}
-
-UInt32 UMAMenuEvent( EventRecord *inEvent )
-{
-#if UMA_USE_APPEARANCE
- if ( UMAHasAppearance() )
- {
- return MenuEvent( inEvent ) ;
- }
- else
-#endif
- {
- if ( inEvent->what == keyDown && inEvent->modifiers & cmdKey)
- {
- return MenuKey( inEvent->message & charCodeMask ) ;
- }
- return NULL ;
- }
-}
-
-void UMAEnableMenuItem( MenuRef inMenu , MenuItemIndex inItem )
-{
-#if UMA_USE_8_6 || TARGET_CARBON
- EnableMenuItem( inMenu , inItem ) ;
-#else
- EnableItem( inMenu , inItem ) ;
-#endif
-}
-
-void UMADisableMenuItem( MenuRef inMenu , MenuItemIndex inItem )
-{
-#if UMA_USE_8_6 || TARGET_CARBON
- DisableMenuItem( inMenu , inItem ) ;
-#else
- DisableItem( inMenu , inItem ) ;
-#endif
-}
-
-void UMAAppendSubMenuItem( MenuRef menu , StringPtr l , SInt16 id )
-{
- Str255 label ;
- memcpy( label , l , l[0]+1 ) ;
- // hardcoded adding of the submenu combination for mac
-
- int theEnd = label[0] + 1;
- if (theEnd > 251)
- theEnd = 251; // mac allows only 255 characters
- label[theEnd++] = '/';
- label[theEnd++] = hMenuCmd;
- label[theEnd++] = '!';
- label[theEnd++] = id ;
- label[theEnd] = 0x00;
- label[0] = theEnd;
- MacAppendMenu(menu, label);
-}
-
-void UMAInsertSubMenuItem( MenuRef menu , StringPtr l , MenuItemIndex item , SInt16 id )
-{
- Str255 label ;
- memcpy( label , l , l[0]+1 ) ;
- // hardcoded adding of the submenu combination for mac
-
- int theEnd = label[0] + 1;
- if (theEnd > 251)
- theEnd = 251; // mac allows only 255 characters
- label[theEnd++] = '/';
- label[theEnd++] = hMenuCmd;
- label[theEnd++] = '!';
- label[theEnd++] = id;
- label[theEnd] = 0x00;
- label[0] = theEnd;
- MacInsertMenuItem(menu, label , item);
-}
-
-void UMAAppendMenuItem( MenuRef menu , StringPtr l , SInt16 key, UInt8 modifiers )
-{
- Str255 label ;
- memcpy( label , l , l[0]+1 ) ;
- if ( key )
- {
- int pos = label[0] ;
- label[++pos] = '/';
- label[++pos] = toupper( key );
- label[0] = pos ;
- }
- MacAppendMenu( menu , label ) ;
-}
-
-void UMAInsertMenuItem( MenuRef menu , StringPtr l , MenuItemIndex item , SInt16 key, UInt8 modifiers )
-{
- Str255 label ;
- memcpy( label , l , l[0]+1 ) ;
- if ( key )
- {
- int pos = label[0] ;
- label[++pos] = '/';
- label[++pos] = toupper( key );
- label[0] = pos ;
- }
- MacInsertMenuItem( menu , label , item) ;
-}
-
-void UMADrawMenuBar()
-{
- DrawMenuBar() ;
-}
-
-
-void UMASetMenuItemText( MenuRef menu , MenuItemIndex item , StringPtr label )
-{
- ::SetMenuItemText( menu , item , label ) ;
-}
-
-MenuRef UMANewMenu( SInt16 menuid , StringPtr label )
-{
- return ::NewMenu(menuid, label);
-}
-
-void UMADisposeMenu( MenuRef menu )
-{
- DisposeMenu( menu ) ;
-}
-void UMADeleteMenu( SInt16 menuId )
-{
- ::DeleteMenu( menuId ) ;
-}
-
-void UMAInsertMenu( MenuRef insertMenu , SInt16 afterId )
-{
- ::InsertMenu( insertMenu , afterId ) ;
-}
-
-
-// quickdraw
-
-int gPrOpenCounter = 0 ;
-
-OSStatus UMAPrOpen()
-{
-#if !TARGET_CARBON
- OSErr err = noErr ;
- ++gPrOpenCounter ;
- if ( gPrOpenCounter == 1 )
- {
- PrOpen() ;
- err = PrError() ;
- wxASSERT( err == noErr ) ;
- }
- return err ;
-#else
- OSStatus err = noErr ;
- ++gPrOpenCounter ;
- if ( gPrOpenCounter == 1 )
- {
- err = PMBegin() ;
- wxASSERT( err == noErr ) ;
- }
- return err ;
-#endif
-}
-
-OSStatus UMAPrClose()
-{
-#if !TARGET_CARBON
- OSErr err = noErr ;
- wxASSERT( gPrOpenCounter >= 1 ) ;
- if ( gPrOpenCounter == 1 )
- {
- PrClose() ;
- err = PrError() ;
- wxASSERT( err == noErr ) ;
- }
- --gPrOpenCounter ;
- return err ;
-#else
- OSStatus err = noErr ;
- wxASSERT( gPrOpenCounter >= 1 ) ;
- if ( gPrOpenCounter == 1 )
- {
- err = PMEnd() ;
- }
- --gPrOpenCounter ;
- return err ;
-#endif
-}
-
-#if !TARGET_CARBON
-
-pascal QDGlobalsPtr GetQDGlobalsPtr (void)
-{
- return QDGlobalsPtr (* (Ptr*) LMGetCurrentA5 ( ) - 0xCA);
-}
-
-#endif
-
-void UMAShowWatchCursor()
-{
- OSErr err = noErr;
-
- CursHandle watchFob = GetCursor (watchCursor);
-
- if (!watchFob)
- err = nilHandleErr;
- else
- {
- #if TARGET_CARBON
- Cursor preservedArrow;
- GetQDGlobalsArrow (&preservedArrow);
- SetQDGlobalsArrow (*watchFob);
- InitCursor ( );
- SetQDGlobalsArrow (&preservedArrow);
- #else
- SetCursor (*watchFob);
- #endif
- }
-}
-
-void UMAShowArrowCursor()
-{
-#if TARGET_CARBON
- Cursor arrow;
- SetCursor (GetQDGlobalsArrow (&arrow));
-#else
- SetCursor (&(qd.arrow));
-#endif
-}
-
-// window manager
-
-GrafPtr UMAGetWindowPort( WindowRef inWindowRef )
-{
- wxASSERT( inWindowRef != NULL ) ;
-#if TARGET_CARBON
- return GetWindowPort( inWindowRef ) ;
-#else
- return (GrafPtr) inWindowRef ;
-#endif
-}
-
-void UMADisposeWindow( WindowRef inWindowRef )
-{
- wxASSERT( inWindowRef != NULL ) ;
- DisposeWindow( inWindowRef ) ;
-}
-
-void UMASetWTitleC( WindowRef inWindowRef , const char *title )
-{
- Str255 ptitle ;
- strncpy( (char*)ptitle , title , 96 ) ;
- ptitle[96] = 0 ;
-#if TARGET_CARBON
- c2pstrcpy( ptitle, (char *)ptitle ) ;
-#else
- c2pstr( (char*)ptitle ) ;
-#endif
- SetWTitle( inWindowRef , ptitle ) ;
-}
-
-void UMAGetWTitleC( WindowRef inWindowRef , char *title )
-{
- GetWTitle( inWindowRef , (unsigned char*)title ) ;
-#if TARGET_CARBON
- p2cstrcpy( title, (unsigned char *)title ) ;
-#else
- p2cstr( (unsigned char*)title ) ;
-#endif
-}
-
-void UMAShowWindow( WindowRef inWindowRef )
-{
- ShowWindow( inWindowRef ) ;
-}
-
-void UMAHideWindow( WindowRef inWindowRef )
-{
- HideWindow( inWindowRef) ;
-}
-
-void UMASelectWindow( WindowRef inWindowRef )
-{
- SelectWindow( inWindowRef ) ;
-}
-
-void UMABringToFront( WindowRef inWindowRef )
-{
- BringToFront( inWindowRef ) ;
-}
-
-void UMASendBehind( WindowRef inWindowRef , WindowRef behindWindow )
-{
- SendBehind( inWindowRef , behindWindow ) ;
-}
-
-void UMACloseWindow(WindowRef inWindowRef)
-{
-#if TARGET_CARBON
-#else
- CloseWindow( inWindowRef ) ;
-#endif
-}
-
-// appearance additions
-
-void UMAActivateControl( ControlHandle inControl )
-{
-#if UMA_USE_APPEARANCE
- if ( UMAHasAppearance() )
- {
- ::ActivateControl( inControl ) ;
- }
- else
-#endif
-#if !TARGET_CARBON
- {
- AGAActivateControl( inControl ) ;
- }
-#else
- {
- }
-#endif
-}
-
-void UMADrawControl( ControlHandle inControl )
-{
-#if UMA_USE_APPEARANCE
- if ( UMAHasAppearance() )
- {
- ::DrawControlInCurrentPort( inControl ) ;
- }
- else
-#endif
-#if !TARGET_CARBON
- {
- AGADrawControl( inControl ) ;
- }
-#else
- {
- }
-#endif
-}