home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / Invisible JEDI / source code / Slider.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-28  |  7.9 KB  |  361 lines  |  [TEXT/CWIE]

  1. /***********************************************************
  2.     Slider.cpp
  3.     ©1997 Eric Cole
  4.     
  5.     A simple init that dynamically positions the desktop
  6.     pattern in response to a control-shift-click-drag in
  7.     the desktop area
  8.     
  9.     shift-click-arrowkey will cycle the current patterns
  10.     from the desktop pattern or desktop picture prefs
  11. ***********************************************************/
  12.  
  13.  
  14. typedef UniversalProcPtr GetNextEventUPP;
  15.  
  16.  
  17. enum {
  18.     uppGetNextEventProcInfo = kPascalStackBased
  19.         | RESULT_SIZE(SIZE_CODE(sizeof(Boolean)))
  20.         | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(short)))
  21.         | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(EventRecord *)))
  22. };
  23.  
  24.  
  25. #define NewGetNextEventProc(userRoutine) \
  26.     (GetNextEventUPP)NewRoutineDescriptor((ProcPtr)(userRoutine),uppGetNextEventProcInfo,GetCurrentArchitecture())
  27. #define CallGetNextEvent(upp,mask,event) \
  28.     CallUniversalProc( upp , uppGetNextEventProcInfo , mask , event );
  29.  
  30.  
  31. pascal Boolean MyGetNextEvent( short mask , EventRecord *event );
  32. int ScrollCurrentDesktop( int h , int v );
  33. int ScrollDesktop( Point start );
  34. int CyclePattern( void );
  35. void InitDesktopScroll( void );
  36.  
  37.  
  38. GetNextEventUPP        gGetNextEvent;
  39.  
  40. int                    gHDelta = 0 , gVDelta = 0;
  41. int                    gIndex = 0;
  42.  
  43.  
  44. extern "C" {
  45.  
  46. void __start( void );
  47. void __start( void ) {
  48. //    Debugger();
  49.     DetachResource( Get1Resource( 'INIT' , 0 ) );
  50.     InitDesktopScroll();
  51.     
  52.     THz        zone = GetZone();
  53.     SetZone( SystemZone() );
  54.     
  55.     gGetNextEvent = GetToolTrapAddress( _GetNextEvent );
  56.     SetToolTrapAddress( NewGetNextEventProc( MyGetNextEvent ) , _GetNextEvent );
  57.     
  58.     SetZone( zone );
  59. }
  60.  
  61. }
  62.  
  63.  
  64. pascal Boolean MyGetNextEvent( short mask , EventRecord *event ) {
  65.     Boolean        result = CallGetNextEvent( gGetNextEvent , mask , event );
  66.     
  67.     switch ( event->what ) {
  68.     case nullEvent:
  69.         if ( gHDelta || gVDelta ) ScrollCurrentDesktop( gHDelta , gVDelta );
  70.         break;
  71.     case mouseDown:
  72.         if ( ( event->modifiers & shiftKey ) && ( event->modifiers & controlKey ) )
  73.             result = !ScrollDesktop( event->where );
  74.         break;
  75.     case keyDown: case autoKey:
  76.         if ( ( event->modifiers & shiftKey ) && ( event->modifiers & controlKey ) ) switch ( (char)event->message ) {
  77.             case 0x1C: gIndex -= 1; result = !CyclePattern(); break;
  78.             case 0x1D: gIndex += 1; result = !CyclePattern(); break;
  79.             case 0x1E: gIndex = 1; result = !CyclePattern(); break;
  80.             case 0x1F: gIndex = 0; result = !CyclePattern(); break;
  81.         }
  82.         break;
  83.     }
  84.     
  85.     return result;
  86. }
  87.  
  88.  
  89. #pragma mark -
  90.  
  91.  
  92. #include <CursorDevices.h>
  93. #include <Displays.h>
  94.  
  95.  
  96. int TrapGood( UInt16 trap );
  97. void SetGMouse( Point mouse );
  98. void GetGMouse( Point *mouse );
  99. OSErr GetDesktopLessIcons( RgnHandle *desk );
  100. OSErr FillDesktopRegion( PixPatHandle pat );
  101.  
  102.  
  103. void ShiftPixPat( PixPatHandle pixpat , long horz , long vert );
  104. void FlipPixPat( PixPatHandle pixpat , long horz , long vert );
  105. void InvertPixPat( PixPatHandle pixpat );
  106.  
  107.  
  108. inline int ToolTraps( void ) {
  109.     return ( GetToolTrapAddress( _InitGraf ) == GetToolTrapAddress( 0xAA6E ) ) ? 0x0200 : 0x0400;
  110. }
  111.  
  112.  
  113. int TrapGood( UInt16 trap ) {
  114.     if ( trap & 0x0800 ) {    //    toolbox trap
  115.         if ( ( trap & 0x07FF ) > ToolTraps() ) {
  116.             return false;
  117.         } else return GetToolTrapAddress( trap ) != GetToolTrapAddress( _Unimplemented );
  118.     } else return GetOSTrapAddress( trap ) != GetToolTrapAddress( _Unimplemented );
  119. }
  120.  
  121.  
  122. #if !__powerc
  123. pascal void CallJCrsrTask( void ) = { 0x2078 , 0x08EE , 0x4E90 };
  124. #endif
  125.  
  126.  
  127. void GetGMouse( Point *mouse ) {
  128.     GetMouse( mouse );
  129.     LocalToGlobal( mouse );
  130. }
  131.  
  132.  
  133. void SetGMouse( Point mouse ) {
  134. #if 0
  135.     if ( TrapGood( 0xAADB ) ) {
  136.         CursorDevicePtr        curs = nil;
  137.         if ( CursorDeviceNextDevice( &curs ) == noErr ) {
  138.             CursorDeviceMoveTo( curs , mouse.h , mouse.v );
  139.         }
  140. #if !__powerc
  141.     } else {
  142.         Point        *mice = (Point *)0x0828;        //    mTemp , rawMouse , Mouse
  143.         mice[2] = mice[1] = mice[0] = mouse;        //    set all them little mice
  144.         *(SInt8 *)0x08CE = *(SInt8 *)0x08CF;        //    set cursorNew if coupled
  145.         CallJCrsrTask();                            //    tell the system about it
  146. #endif
  147.     }
  148. #else
  149.     Point        *mice = (Point *)0x0828;            //    mTemp , rawMouse , Mouse
  150.     mice[2] = mice[1] = mice[0] = mouse;            //    set all them little mice
  151.     *(SInt8 *)0x08CE = *(SInt8 *)0x08CF;            //    set cursorNew if coupled
  152. #endif
  153. }
  154.  
  155.  
  156. #pragma mark -
  157.  
  158.  
  159. inline void PrimeDesktopIconRegion( void ) {
  160.     (*(SInt16 **)(0x02B6))[252] = 1;
  161. }
  162.  
  163.  
  164. inline RgnHandle GetDesktopIconRegion( void ) {
  165.     return (*(RgnHandle **)(0x02B6))[125];
  166. }
  167.  
  168.  
  169. void InitDesktopScroll( void ) {
  170.     PrimeDesktopIconRegion();
  171. }
  172.  
  173.  
  174. OSErr GetDesktopLessIcons( RgnHandle *desk ) {
  175.     if ( !TrapGood( 0xABEB ) ) return paramErr;
  176.     if ( desk == nil ) return paramErr;
  177.     *desk = nil;
  178.     
  179.     PrimeDesktopIconRegion();
  180.     RgnHandle        icon = GetDesktopIconRegion();
  181.     if ( icon == nil ) return -1;
  182.     
  183.     RgnHandle        rgn = nil;
  184.     OSErr            err = DMGetDeskRegion( &rgn );
  185.     if ( err ) return err;
  186.     
  187.     *desk = NewRgn();
  188.     if ( ( err = QDError() ) == noErr && *desk == nil ) err = memFullErr;
  189.     if ( err ) return err;
  190.     
  191.     DiffRgn( rgn , icon , *desk );
  192.     err = QDError();
  193.     
  194.     return err;
  195. }
  196.  
  197.  
  198. OSErr FillDesktopRegion( PixPatHandle pat ) {
  199.     RgnHandle        clip = nil , rgn = nil;
  200.     OSErr            err = GetDesktopLessIcons( &rgn );
  201.     if ( err ) return err;
  202.     
  203.     GrafPtr            save , draw;
  204.     
  205.     GetPort( &save );
  206.     GetCWMgrPort( (CGrafPtr *)&draw );
  207.     SetPort( draw );
  208.     
  209.     clip = NewRgn();
  210.     if ( clip ) {
  211.         GetClip( clip );
  212.         SetClip( rgn );
  213.     }
  214.     
  215.     FillCRgn( rgn , pat );
  216.     DisposeRgn( rgn );
  217.     
  218.     if ( clip ) {
  219.         SetClip( clip );
  220.         DisposeRgn( clip );
  221.     }
  222.     
  223.     SetPort( save );
  224.     
  225.     return err;
  226. }
  227.  
  228.  
  229. int ScrollDesktop( Point start ) {
  230.     GrafPtr            save , draw;
  231.     GetCWMgrPort( (CGrafPtr *)&draw );
  232.     GetPort( &save );
  233.     SetPort( draw );
  234.     
  235.     Rect            rect;
  236.     Point            focus , mouse;
  237.     RgnHandle        clip , desk = nil;
  238.     
  239.     if ( GetDesktopLessIcons( &desk ) != noErr ) clip = nil;
  240.     else clip = NewRgn();
  241.     
  242.     if ( !clip || !desk || !PtInRgn( start , desk ) ) {
  243.         if ( desk ) DisposeRgn( desk );
  244.         if ( clip ) DisposeRgn( clip );
  245.         return false;
  246.     }
  247.     
  248.     PixPatHandle    ppat = LMGetDeskCPat();
  249.     
  250.     GetClip( clip );
  251.     rect = (**desk).rgnBBox;
  252.     ClipRect( &rect );
  253.     
  254.     rect = (**GetMainDevice()).gdRect;
  255.     focus.h = ( rect.left + rect.right ) / 2;
  256.     focus.v = ( rect.top + rect.bottom ) / 2;
  257.     
  258.     HideCursor();
  259.     SetGMouse( focus );
  260.     
  261.     while ( WaitMouseUp() ) {
  262.         GetGMouse( &mouse );
  263.         
  264.         if ( focus.h - mouse.h || focus.v - mouse.v ) {
  265.             ShiftPixPat( ppat , mouse.h - focus.h , mouse.v - focus.v );
  266.             FillCRgn( desk , ppat );
  267.             SetGMouse( focus );
  268.         }
  269.         
  270.         gHDelta = mouse.h - focus.h;
  271.         gVDelta = mouse.v - focus.v;
  272.     }
  273.     
  274.     SetClip( clip );
  275.     SetPort( save );
  276.     
  277.     if ( desk ) DisposeRgn( desk );
  278.     if ( clip ) DisposeRgn( clip );
  279.     
  280.     SetGMouse( start );
  281.     InitCursor();
  282.     
  283.     return true;
  284. }
  285.  
  286.  
  287. int ScrollCurrentDesktop( int h , int v ) {
  288.     GrafPtr            save , draw;
  289.     GetCWMgrPort( (CGrafPtr *)&draw );
  290.     GetPort( &save );
  291.     SetPort( draw );
  292.     
  293.     Rect            rect;
  294.     RgnHandle        clip , desk = nil;
  295.     
  296.     if ( GetDesktopLessIcons( &desk ) != noErr ) clip = nil;
  297.     else clip = NewRgn();
  298.     
  299.     if ( clip && desk ) {
  300.         PixPatHandle    ppat = LMGetDeskCPat();
  301.         
  302.         GetClip( clip );
  303.         rect = (**desk).rgnBBox;
  304.         ClipRect( &rect );
  305.         
  306.         ShiftPixPat( ppat , h , v );
  307.         FillCRgn( desk , ppat );
  308.     }
  309.     
  310.     SetClip( clip );
  311.     SetPort( save );
  312.     
  313.     if ( desk ) DisposeRgn( desk );
  314.     if ( clip ) DisposeRgn( clip );
  315.     
  316.     return true;
  317. }
  318.  
  319.  
  320. #pragma mark -
  321.  
  322.  
  323. int CyclePattern( void ) {
  324.     FSSpec        spec;
  325.     OSErr        err = FindFolder( kOnSystemDisk , kPreferencesFolderType , kDontCreateFolder , &spec.vRefNum , &spec.parID );
  326.     if ( err ) return false;
  327.     
  328.     //    should use a resource for the name
  329.     BlockMoveData( "\pDesktop Pictures Prefs" , spec.name , 64 );        //    try system 8
  330.     short        ref = FSpOpenResFile( &spec , fsRdPerm );
  331.     
  332.     if ( ref <= 0 ) {
  333.         BlockMoveData( "\pDesktop Pattern Prefs" , spec.name , 64 );    //    try system 7
  334.         ref = FSpOpenResFile( &spec , fsRdPerm );
  335.     }
  336.     
  337.     if ( ref <= 0 ) return false;
  338.     
  339.     int            count = Count1Resources( 'ppat' );
  340.     if ( gIndex > count ) gIndex = 1;
  341.     if ( gIndex <= 0 ) gIndex = count;
  342.     
  343.     short        id;
  344.     OSType        type;
  345.     
  346.     SetResLoad( false );
  347.     Handle        res = Get1IndResource( 'ppat' , gIndex );
  348.     GetResInfo( res , &id , &type , nil );
  349.     SetResLoad( true );
  350.     
  351.     SetDeskCPat( GetPixPat( id ) );
  352.     CloseResFile( ref );
  353.     return true;
  354. }
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.