home *** CD-ROM | disk | FTP | other *** search
/ Amiga Inside! / Amiga FD Inside (1995)(Ultramax).iso / berndspd / devtools / precognition / src / library / intuition_utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-18  |  8.1 KB  |  368 lines

  1. #include "Intuition_utils.h"
  2.  
  3. #ifndef __GNUC__
  4. #include <clib/exec_protos.h>
  5. #include <clib/intuition_protos.h>
  6. #endif
  7. #ifdef __GNUC__
  8. #include <proto/exec.h>
  9. #include <proto/intuition.h>
  10. #endif
  11. #ifdef  __SASC
  12. #include <proto/exec.h>
  13. #include <proto/intuition.h>
  14. #endif
  15.  
  16.  
  17. #define DONT_KNOW -2
  18.  
  19. #define FIRST_V2_LIB 36
  20. #define FIRST_V2_1_LIB 38
  21. #define FIRST_V3_LIB 39
  22. #define FIRST_V3_1_LIB 40
  23.  
  24. BOOL is_Workbench_v2( void )
  25.    /* returns TRUE if you're running Workbench 2.0 */
  26. {
  27.    static BOOL is_Wb2 = DONT_KNOW;
  28.    struct Library *lib = NULL;
  29.  
  30.    if( DONT_KNOW == is_Wb2 )
  31.    {
  32.       lib = OpenLibrary( "intuition.library", FIRST_V2_LIB );
  33.       if( lib != NULL )
  34.       {
  35.          is_Wb2 = TRUE;
  36.          CloseLibrary( lib );
  37.       }
  38.       else
  39.          is_Wb2 = FALSE;
  40.    }
  41.  
  42.    return (BOOL) is_Wb2;
  43. }
  44.  
  45.  
  46. BOOL is_Workbench_v2_1( void )
  47.    /* returns TRUE if you're running Workbench 2.0 */
  48. {
  49.    static BOOL is_Wb2_1 = DONT_KNOW;
  50.    struct Library *lib = NULL;
  51.  
  52.    if( DONT_KNOW == is_Wb2_1 )
  53.    {
  54.       lib = OpenLibrary( "intuition.library", FIRST_V2_1_LIB );
  55.       if( lib != NULL )
  56.       {
  57.          is_Wb2_1 = TRUE;
  58.          CloseLibrary( lib );
  59.       }
  60.       else
  61.          is_Wb2_1 = FALSE;
  62.    }
  63.  
  64.    return (BOOL) is_Wb2_1;
  65. }
  66.  
  67. BOOL is_Workbench_v3( void )
  68.    /* returns TRUE if you're running Workbench 2.0 */
  69. {
  70.    static BOOL is_Wb3 = DONT_KNOW;
  71.    struct Library *lib = NULL;
  72.  
  73.    if( DONT_KNOW == is_Wb3 )
  74.    {
  75.       lib = OpenLibrary( "intuition.library", FIRST_V3_LIB );
  76.       if( lib != NULL )
  77.       {
  78.          is_Wb3 = TRUE;
  79.          CloseLibrary( lib );
  80.       }
  81.       else
  82.          is_Wb3 = FALSE;
  83.    }
  84.  
  85.    return (BOOL) is_Wb3;
  86. }
  87.  
  88. BOOL is_Workbench_v3_1( void )
  89.    /* returns TRUE if you're running Workbench 2.0 */
  90. {
  91.    static BOOL is_Wb3_1 = DONT_KNOW;
  92.    struct Library *lib = NULL;
  93.  
  94.    if( DONT_KNOW == is_Wb3_1 )
  95.    {
  96.       lib = OpenLibrary( "intuition.library", FIRST_V3_1_LIB );
  97.       if( lib != NULL )
  98.       {
  99.          is_Wb3_1 = TRUE;
  100.          CloseLibrary( lib );
  101.       }
  102.       else
  103.          is_Wb3_1 = FALSE;
  104.    }
  105.  
  106.    return (BOOL) is_Wb3_1;
  107. }
  108.  
  109.  
  110. void GadgetRelativeCoords( struct Gadget       *gadget,
  111.                            struct IntuiMessage *event,
  112.                            Point               *point )
  113. {
  114.    struct Window *window;
  115.  
  116.    window = event->IDCMPWindow;
  117.  
  118.    point->x  = event->MouseX - gadget->LeftEdge;
  119.    point->y  = event->MouseY - gadget->TopEdge;
  120.  
  121.    if( window->Flags & GIMMEZEROZERO )
  122.    {
  123.       point->x -= window->BorderLeft;
  124.       point->y -= window->BorderTop;
  125.    }
  126.  
  127. }
  128.  
  129.  
  130. #ifndef __GNUC__   /* no CHIP keyword */
  131. static USHORT __chip BusyPointerData[] =
  132. {
  133.    0x0000,0x0000,
  134.    0x0400,0x07C0,0x0000,0x07C0,0x0100,0x0380,0x0000,0x07E0,
  135.    0x07C0,0x1FF8,0x1FF0,0x3FEC,0x3FF8,0x7FDE,0x3FF8,0x7FBE,
  136.    0x7FFC,0xFF7F,0x7EFC,0xFFFF,0x7FFC,0xFFFF,0x3FF8,0x7FFE,
  137.    0x3FF8,0x7FFE,0x1FF0,0x3FFC,0x07C0,0x1FF8,0x0000,0x07E0,
  138.    0x0000,0x0000,
  139. };
  140. #endif
  141.  
  142. #ifndef __GNUC__   /* need to allocate the CHIPMEM manually for Busy pointer */
  143. void SetWaitPointer( struct Window *w )
  144. {
  145.    if( is_Workbench_v3() )   /* use system busy pointer V39 and up */
  146.      SetWindowPointer( w, WA_BusyPointer, TRUE );
  147.    else
  148.      SetPointer( w, BusyPointerData, 16, 16, -6, 0 );
  149. }
  150. #endif
  151. #ifdef __GNUC__
  152. USHORT *BusyPointerData = NULL; /* must be CHIP ram */
  153.  
  154. static USHORT fastBusyPointerData[] =
  155. {
  156.    0x0000,0x0000,
  157.    0x0400,0x07C0,0x0000,0x07C0,0x0100,0x0380,0x0000,0x07E0,
  158.    0x07C0,0x1FF8,0x1FF0,0x3FEC,0x3FF8,0x7FDE,0x3FF8,0x7FBE,
  159.    0x7FFC,0xFF7F,0x7EFC,0xFFFF,0x7FFC,0xFFFF,0x3FF8,0x7FFE,
  160.    0x3FF8,0x7FFE,0x1FF0,0x3FFC,0x07C0,0x1FF8,0x0000,0x07E0,
  161.    0x0000,0x0000,
  162. };
  163.  
  164. void SetWaitPointer( struct Window *w )
  165. {
  166.    if( is_Workbench_v3() )
  167.      SetWindowPointer( w, WA_BusyPointer, TRUE );
  168.    else
  169.      ; /* do nothing for now */
  170.      /* should probably check an extern pointer for NULL
  171.       *  and if so allocate the chipmem to the pointer
  172.       *  but then we need to cleanup when done???
  173.       */
  174.  
  175. #if 0  /* not ready ready for primetime */
  176.    {
  177.      if( BusyPointerData == NULL )  /* first time called */
  178.      {
  179.        BusyPointerData = AllocMem( sizeof( fastBusyPointerData[], MEMF_CHIP|MEMF_CLEAR );
  180.        CopyMem(BusyPointerData, fastBusyPointerData, (sizeof( fastBusyPointerData[] );
  181.      }
  182.      SetPointer( w, BusyPointerData, 16, 16, -6, 0 );
  183.    }
  184. #endif /* not ready for prime time */
  185.  
  186. }
  187. #endif /* __GNUC__ */
  188.  
  189. struct IntuiMessage *WaitForMessage( struct MsgPort *mport )
  190. {
  191.    struct IntuiMessage *imsg;
  192.  
  193.    for( ;; )
  194.    {
  195.       imsg = (struct IntuiMessage *)GetMsg( mport );
  196.       if( imsg )
  197.         return imsg;
  198.       else   /* EDB added else to make action more apparent */
  199.         WaitPort( mport );
  200.    }
  201. }
  202.  
  203. struct Window *OpenWindowWithSharedUserPort( struct NewWindow *nw,
  204.                                              struct MsgPort   *shared )
  205. {
  206.    ULONG IDCMPbuf;
  207.    struct Window *w;
  208.  
  209.    IDCMPbuf       = nw->IDCMPFlags;
  210.    nw->IDCMPFlags = 0;  /* no IDCMP flags. */
  211.  
  212.    if( w = SmartOpenWindow( nw ) )
  213.    {
  214.       w->UserPort = shared;         /* assign UserPort. */
  215.       ModifyIDCMP( w, IDCMPbuf );   /* turn on IDCMP    */
  216.    }
  217.    nw->IDCMPFlags = IDCMPbuf; /* return 'nw' to its previous state. */
  218.    return w;
  219. }
  220.  
  221.  
  222. void StripIntuiMessages( struct Window *w )
  223. /*
  224.    *ASSUMES* a Forbid() has been called!
  225.  
  226.    Taken from 1.3 RKM:L&D, page 171
  227. */
  228. {
  229.    struct MsgPort *mp;
  230.    struct IntuiMessage *msg, *succ;
  231.  
  232.    mp = w->UserPort;
  233.  
  234.    /*
  235.     * Loop through all messages waiting at this port, remove any
  236.     * which are for Window 'w'.
  237.     */
  238.  
  239.    msg = (struct IntuiMessage*) mp->mp_MsgList.lh_Head;
  240.    while( succ = (struct IntuiMessage*) msg->ExecMessage.mn_Node.ln_Succ )
  241.    {
  242.       if( msg->IDCMPWindow == w )
  243.       {
  244.          Remove( (struct Node *) msg );
  245.          ReplyMsg( (struct Message *) msg );
  246.       }
  247.  
  248.       msg = succ;
  249.    }
  250. }
  251.  
  252. void CloseWindowWithSharedUserPort( struct Window *w )
  253.    /* Taken from 1.3 RKM:L&D, page 171, 'CloseWindowSafely()' */
  254. {
  255.    Forbid();               /* Turn off multitasking */
  256.  
  257.    StripIntuiMessages( w );  /* remove all messages for this window. */
  258.  
  259.    w->UserPort = NULL;
  260.    ModifyIDCMP( w, 0 ); /* prevents new messages from occuring. */
  261.    Permit();
  262.  
  263.    CloseWindow( w );
  264. }
  265.  
  266.  
  267. BOOL WindowSanityCheck( struct Screen *screen,
  268.                         Point         *location,
  269.                         Point         *size )
  270.    /* returns FALSE if location or size was changed. */
  271. {
  272.    BOOL ok = TRUE;
  273.  
  274.  
  275.    if( size->x > screen->Width )
  276.    {
  277.       ok = FALSE;
  278.       size->x = screen->Width;
  279.    }
  280.  
  281.    if( size->y > screen->Height )
  282.    {
  283.       ok = FALSE;
  284.       size->y = screen->Height;
  285.    }
  286.  
  287.    /* size is now ok, so check location. */
  288.    if( location->x + size->x >= screen->Width )
  289.    {
  290.       location->x = screen->Width - size->x;
  291.       ok = FALSE;
  292.    }
  293.    if( location->y + size->y >= screen->Height )
  294.    {
  295.       location->y = screen->Height - size->y;
  296.       ok = FALSE;
  297.    }
  298.  
  299.    return ok;
  300. }
  301.  
  302. #include <utility/tagitem.h>
  303.  
  304. UWORD minimal_pens [1] = { (USHORT)~0 };
  305.  
  306. /* EDB -- This should probably clone Workbench mode as a default */
  307. struct TagItem screen_tags_v2[] =
  308. {
  309.    { SA_Pens, (ULONG) minimal_pens },
  310.    { TAG_END, 0 }
  311. };
  312.  
  313. struct TagItem screen_tags_v3[] =
  314. {
  315.    { SA_Pens, (ULONG) minimal_pens },
  316.    { SA_LikeWorkbench, TRUE },
  317.    { TAG_END, 0 }
  318. };
  319.  
  320. struct Screen *SmartOpenScreen( struct NewScreen *newscreen )
  321. {
  322.    if( is_Workbench_v2() )
  323.    {
  324.  
  325.    if( is_Workbench_v3() )
  326.       return OpenScreenTagList( newscreen, screen_tags_v3 );
  327.  
  328.    else /* should try harder to clone workbench screen mode? */
  329.       return OpenScreenTagList( newscreen, screen_tags_v2 );
  330.    }
  331.    else
  332.       return OpenScreen( newscreen );
  333. }
  334.  
  335. /* I'm not sure if there are other cases which require OpenWindowTags? -- EDB */
  336. struct TagItem window_tags[] =
  337. {
  338.     { WA_NewLookMenus, TRUE }
  339. };
  340.  
  341. struct Window *SmartOpenWindow( struct NewWindow *newwindow )
  342. {
  343.    if( is_Workbench_v3() )
  344.    {
  345.       return OpenWindowTagList( newwindow, window_tags );
  346.    }
  347.    else
  348.       return OpenWindow( newwindow );
  349. }
  350.  
  351.  
  352. void RemapImage( struct Image *image )
  353. {
  354.    UWORD *plane1, *plane2, temp;
  355.    short i, planesize;
  356.  
  357.    planesize = ( ( image->Width + 15 )/16 ) * image->Height;
  358.  
  359.    plane1 = &image->ImageData[0];
  360.    plane2 = &image->ImageData[planesize];
  361.    for( i = 0; i < planesize; i++ )
  362.    {
  363.       temp      = plane1[i];
  364.       plane1[i] = plane2[i];
  365.       plane2[i] = temp;
  366.    }
  367. }
  368.