home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************
- Slider.cpp
- ©1997 Eric Cole
-
- A simple init that dynamically positions the desktop
- pattern in response to a control-shift-click-drag in
- the desktop area
-
- shift-click-arrowkey will cycle the current patterns
- from the desktop pattern or desktop picture prefs
- ***********************************************************/
-
-
- typedef UniversalProcPtr GetNextEventUPP;
-
-
- enum {
- uppGetNextEventProcInfo = kPascalStackBased
- | RESULT_SIZE(SIZE_CODE(sizeof(Boolean)))
- | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(short)))
- | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(EventRecord *)))
- };
-
-
- #define NewGetNextEventProc(userRoutine) \
- (GetNextEventUPP)NewRoutineDescriptor((ProcPtr)(userRoutine),uppGetNextEventProcInfo,GetCurrentArchitecture())
- #define CallGetNextEvent(upp,mask,event) \
- CallUniversalProc( upp , uppGetNextEventProcInfo , mask , event );
-
-
- pascal Boolean MyGetNextEvent( short mask , EventRecord *event );
- int ScrollCurrentDesktop( int h , int v );
- int ScrollDesktop( Point start );
- int CyclePattern( void );
- void InitDesktopScroll( void );
-
-
- GetNextEventUPP gGetNextEvent;
-
- int gHDelta = 0 , gVDelta = 0;
- int gIndex = 0;
-
-
- extern "C" {
-
- void __start( void );
- void __start( void ) {
- // Debugger();
- DetachResource( Get1Resource( 'INIT' , 0 ) );
- InitDesktopScroll();
-
- THz zone = GetZone();
- SetZone( SystemZone() );
-
- gGetNextEvent = GetToolTrapAddress( _GetNextEvent );
- SetToolTrapAddress( NewGetNextEventProc( MyGetNextEvent ) , _GetNextEvent );
-
- SetZone( zone );
- }
-
- }
-
-
- pascal Boolean MyGetNextEvent( short mask , EventRecord *event ) {
- Boolean result = CallGetNextEvent( gGetNextEvent , mask , event );
-
- switch ( event->what ) {
- case nullEvent:
- if ( gHDelta || gVDelta ) ScrollCurrentDesktop( gHDelta , gVDelta );
- break;
- case mouseDown:
- if ( ( event->modifiers & shiftKey ) && ( event->modifiers & controlKey ) )
- result = !ScrollDesktop( event->where );
- break;
- case keyDown: case autoKey:
- if ( ( event->modifiers & shiftKey ) && ( event->modifiers & controlKey ) ) switch ( (char)event->message ) {
- case 0x1C: gIndex -= 1; result = !CyclePattern(); break;
- case 0x1D: gIndex += 1; result = !CyclePattern(); break;
- case 0x1E: gIndex = 1; result = !CyclePattern(); break;
- case 0x1F: gIndex = 0; result = !CyclePattern(); break;
- }
- break;
- }
-
- return result;
- }
-
-
- #pragma mark -
-
-
- #include <CursorDevices.h>
- #include <Displays.h>
-
-
- int TrapGood( UInt16 trap );
- void SetGMouse( Point mouse );
- void GetGMouse( Point *mouse );
- OSErr GetDesktopLessIcons( RgnHandle *desk );
- OSErr FillDesktopRegion( PixPatHandle pat );
-
-
- void ShiftPixPat( PixPatHandle pixpat , long horz , long vert );
- void FlipPixPat( PixPatHandle pixpat , long horz , long vert );
- void InvertPixPat( PixPatHandle pixpat );
-
-
- inline int ToolTraps( void ) {
- return ( GetToolTrapAddress( _InitGraf ) == GetToolTrapAddress( 0xAA6E ) ) ? 0x0200 : 0x0400;
- }
-
-
- int TrapGood( UInt16 trap ) {
- if ( trap & 0x0800 ) { // toolbox trap
- if ( ( trap & 0x07FF ) > ToolTraps() ) {
- return false;
- } else return GetToolTrapAddress( trap ) != GetToolTrapAddress( _Unimplemented );
- } else return GetOSTrapAddress( trap ) != GetToolTrapAddress( _Unimplemented );
- }
-
-
- #if !__powerc
- pascal void CallJCrsrTask( void ) = { 0x2078 , 0x08EE , 0x4E90 };
- #endif
-
-
- void GetGMouse( Point *mouse ) {
- GetMouse( mouse );
- LocalToGlobal( mouse );
- }
-
-
- void SetGMouse( Point mouse ) {
- #if 0
- if ( TrapGood( 0xAADB ) ) {
- CursorDevicePtr curs = nil;
- if ( CursorDeviceNextDevice( &curs ) == noErr ) {
- CursorDeviceMoveTo( curs , mouse.h , mouse.v );
- }
- #if !__powerc
- } else {
- Point *mice = (Point *)0x0828; // mTemp , rawMouse , Mouse
- mice[2] = mice[1] = mice[0] = mouse; // set all them little mice
- *(SInt8 *)0x08CE = *(SInt8 *)0x08CF; // set cursorNew if coupled
- CallJCrsrTask(); // tell the system about it
- #endif
- }
- #else
- Point *mice = (Point *)0x0828; // mTemp , rawMouse , Mouse
- mice[2] = mice[1] = mice[0] = mouse; // set all them little mice
- *(SInt8 *)0x08CE = *(SInt8 *)0x08CF; // set cursorNew if coupled
- #endif
- }
-
-
- #pragma mark -
-
-
- inline void PrimeDesktopIconRegion( void ) {
- (*(SInt16 **)(0x02B6))[252] = 1;
- }
-
-
- inline RgnHandle GetDesktopIconRegion( void ) {
- return (*(RgnHandle **)(0x02B6))[125];
- }
-
-
- void InitDesktopScroll( void ) {
- PrimeDesktopIconRegion();
- }
-
-
- OSErr GetDesktopLessIcons( RgnHandle *desk ) {
- if ( !TrapGood( 0xABEB ) ) return paramErr;
- if ( desk == nil ) return paramErr;
- *desk = nil;
-
- PrimeDesktopIconRegion();
- RgnHandle icon = GetDesktopIconRegion();
- if ( icon == nil ) return -1;
-
- RgnHandle rgn = nil;
- OSErr err = DMGetDeskRegion( &rgn );
- if ( err ) return err;
-
- *desk = NewRgn();
- if ( ( err = QDError() ) == noErr && *desk == nil ) err = memFullErr;
- if ( err ) return err;
-
- DiffRgn( rgn , icon , *desk );
- err = QDError();
-
- return err;
- }
-
-
- OSErr FillDesktopRegion( PixPatHandle pat ) {
- RgnHandle clip = nil , rgn = nil;
- OSErr err = GetDesktopLessIcons( &rgn );
- if ( err ) return err;
-
- GrafPtr save , draw;
-
- GetPort( &save );
- GetCWMgrPort( (CGrafPtr *)&draw );
- SetPort( draw );
-
- clip = NewRgn();
- if ( clip ) {
- GetClip( clip );
- SetClip( rgn );
- }
-
- FillCRgn( rgn , pat );
- DisposeRgn( rgn );
-
- if ( clip ) {
- SetClip( clip );
- DisposeRgn( clip );
- }
-
- SetPort( save );
-
- return err;
- }
-
-
- int ScrollDesktop( Point start ) {
- GrafPtr save , draw;
- GetCWMgrPort( (CGrafPtr *)&draw );
- GetPort( &save );
- SetPort( draw );
-
- Rect rect;
- Point focus , mouse;
- RgnHandle clip , desk = nil;
-
- if ( GetDesktopLessIcons( &desk ) != noErr ) clip = nil;
- else clip = NewRgn();
-
- if ( !clip || !desk || !PtInRgn( start , desk ) ) {
- if ( desk ) DisposeRgn( desk );
- if ( clip ) DisposeRgn( clip );
- return false;
- }
-
- PixPatHandle ppat = LMGetDeskCPat();
-
- GetClip( clip );
- rect = (**desk).rgnBBox;
- ClipRect( &rect );
-
- rect = (**GetMainDevice()).gdRect;
- focus.h = ( rect.left + rect.right ) / 2;
- focus.v = ( rect.top + rect.bottom ) / 2;
-
- HideCursor();
- SetGMouse( focus );
-
- while ( WaitMouseUp() ) {
- GetGMouse( &mouse );
-
- if ( focus.h - mouse.h || focus.v - mouse.v ) {
- ShiftPixPat( ppat , mouse.h - focus.h , mouse.v - focus.v );
- FillCRgn( desk , ppat );
- SetGMouse( focus );
- }
-
- gHDelta = mouse.h - focus.h;
- gVDelta = mouse.v - focus.v;
- }
-
- SetClip( clip );
- SetPort( save );
-
- if ( desk ) DisposeRgn( desk );
- if ( clip ) DisposeRgn( clip );
-
- SetGMouse( start );
- InitCursor();
-
- return true;
- }
-
-
- int ScrollCurrentDesktop( int h , int v ) {
- GrafPtr save , draw;
- GetCWMgrPort( (CGrafPtr *)&draw );
- GetPort( &save );
- SetPort( draw );
-
- Rect rect;
- RgnHandle clip , desk = nil;
-
- if ( GetDesktopLessIcons( &desk ) != noErr ) clip = nil;
- else clip = NewRgn();
-
- if ( clip && desk ) {
- PixPatHandle ppat = LMGetDeskCPat();
-
- GetClip( clip );
- rect = (**desk).rgnBBox;
- ClipRect( &rect );
-
- ShiftPixPat( ppat , h , v );
- FillCRgn( desk , ppat );
- }
-
- SetClip( clip );
- SetPort( save );
-
- if ( desk ) DisposeRgn( desk );
- if ( clip ) DisposeRgn( clip );
-
- return true;
- }
-
-
- #pragma mark -
-
-
- int CyclePattern( void ) {
- FSSpec spec;
- OSErr err = FindFolder( kOnSystemDisk , kPreferencesFolderType , kDontCreateFolder , &spec.vRefNum , &spec.parID );
- if ( err ) return false;
-
- // should use a resource for the name
- BlockMoveData( "\pDesktop Pictures Prefs" , spec.name , 64 ); // try system 8
- short ref = FSpOpenResFile( &spec , fsRdPerm );
-
- if ( ref <= 0 ) {
- BlockMoveData( "\pDesktop Pattern Prefs" , spec.name , 64 ); // try system 7
- ref = FSpOpenResFile( &spec , fsRdPerm );
- }
-
- if ( ref <= 0 ) return false;
-
- int count = Count1Resources( 'ppat' );
- if ( gIndex > count ) gIndex = 1;
- if ( gIndex <= 0 ) gIndex = count;
-
- short id;
- OSType type;
-
- SetResLoad( false );
- Handle res = Get1IndResource( 'ppat' , gIndex );
- GetResInfo( res , &id , &type , nil );
- SetResLoad( true );
-
- SetDeskCPat( GetPixPat( id ) );
- CloseResFile( ref );
- return true;
- }
-
-
-
-
-
-
-