home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / iffconverter / handleintuimessages.c < prev    next >
C/C++ Source or Header  |  1997-01-07  |  19KB  |  547 lines

  1. /*
  2. **     $VER: HandleIntuiMessage.c V0.01 (14-06-95)
  3. **
  4. **     Author:  Gerben Venekamp
  5. **     Updates: 14-06-95  Version 0.01      Initial module
  6. **
  7. **  HandleIntuiMessages.c waits for a message and acts accordingly to it.
  8. **
  9. */
  10.  
  11. #include <exec/types.h>
  12. #include <intuition/intuition.h>
  13. #include <proto/exec.h>
  14. #include <proto/gadtools.h>
  15. #include <proto/graphics.h>
  16. #include <proto/intuition.h>
  17.  
  18. #include "IFFConverter.h"
  19.  
  20. #define MouseCoordsTextX 140L
  21. #define MouseCoordsTextY 1L
  22.  
  23. // Defining Variables
  24. BOOL PictureValid = FALSE;
  25. BOOL Clipping = FALSE;
  26. BOOL ChangeActivationWindow = TRUE;
  27. BOOL DrawHairCross;
  28.  
  29. enum ByteBoundry ByteBoundry = BB_None;   // initialise with default value.
  30.  
  31. UWORD PicWidth;
  32. UWORD PicHeight;
  33. UWORD PicDepth;
  34.  
  35. ULONG PicSize;
  36.  
  37. // This is a structure, because we need to pass these variables to another module.
  38. IFFClip_s IFFClip;
  39.  
  40. // Set 'OldClipLeft' to a value < 0, so when drawing the clip rectangle
  41. // for the first time, we don't REMOVE the rectangle. Because none
  42. // has been drawn.
  43. WORD OldClipLeft = -1;
  44. WORD OldClipTop;
  45. WORD OldClipRight;
  46. WORD OldClipBottom;
  47.  
  48. // Same here for 'OldMouseX' as 'OldClipLeft' above. Just that it is
  49. // for the hot cursor point cross.
  50. WORD OldMouseX = -1;
  51. WORD OldMouseY;
  52.  
  53. UBYTE *MouseCoords;
  54. char NoMouseCoords[] = "---- × ----";     // String length should be exactly the same as in IText_MouseCoords
  55.  
  56. struct IntuiText IText_MouseCoords = {
  57.    1, 2,
  58.    JAM2,
  59.    0, 0,
  60.    NULL,
  61.    "---- × ----",
  62.    NULL,
  63. };
  64.  
  65. // Defining protos
  66. void HandleIntuiMessages(void);
  67. void HandleClipping_Mouse(WORD, WORD);
  68. void HandleClipping_Gadget(void);
  69. void DrawClipRect(struct RastPort *, UWORD, UWORD, UWORD, UWORD);
  70. void DrawCross(struct RastPort *, UWORD, UWORD);
  71. void MakeByteBoundry(WORD *, BOOL);
  72. void UpdateClipping(void);
  73.  
  74.  
  75. /*
  76. **  HandleIntuiMessages()
  77. **
  78. **     Waits for an events to happen. This done through the Wait(...)
  79. **     call. If a signal comes in, 'HandleIntuiMessages' gets the message
  80. **     associated with the signal from the right messageport. The signal
  81. **     can come from either the 'PanelWindow' or 'ViewWindow'. The same
  82. **     code is used to handle the message from both screens. This is SAVE,
  83. **     because all messages generated by the 'ViewWindow' are shared by
  84. **     the 'PanelWindow'. The additional messages generated by the
  85. **     'PanelWindow' are only generated by pressing gadgets. These
  86. **     gadgets ONLY appear on the 'PanelWindow'.
  87. **
  88. **  pre:  None.
  89. **  post: None.
  90. **
  91. */
  92. void HandleIntuiMessages()
  93. {
  94.    BOOL ExitLoop = FALSE;
  95.    BOOL PanelInFront = TRUE;
  96.    BOOL RemoveInfoWindow;
  97.    
  98.    ULONG Signals;
  99.    ULONG PanelSignal = 1L << PanelWindow->UserPort->mp_SigBit;
  100.    ULONG ViewSignal  = 1L << ViewWindow ->UserPort->mp_SigBit;
  101.    ULONG InfoSignal  = NULL;
  102.  
  103.    struct IntuiMessage *ConverterMessage = NULL;
  104.    SavePicStruct_t PictureToSave;
  105.  
  106.    // Make 'MouseCoords' point to the right text.
  107.    MouseCoords = IText_MouseCoords.IText;
  108.  
  109.    // Print the mouse coordinates one time. This is because when we
  110.    // startup, no picture is loaded and 'PrintIText' inside
  111.    // 'case IDCMP_MOUSEMOVE:' will never be called, until a good picture
  112.    // is loaded.
  113.    PrintIText(&(PanelScreen->RastPort), &IText_MouseCoords, MouseCoordsTextX, MouseCoordsTextY);
  114.  
  115.    do {
  116.       Signals = Wait(PanelSignal | ViewSignal | InfoSignal);
  117.       RemoveInfoWindow = FALSE;
  118.       
  119.       do {
  120.          if( Signals & PanelSignal )    //  Check if 'PanelWindow' sent the message.
  121.             ConverterMessage = GT_GetIMsg(PanelWindow->UserPort);
  122.          else
  123.             if( Signals & ViewSignal )  //  Check if 'ViewWindow' sent the message.
  124.                ConverterMessage = GT_GetIMsg(ViewWindow->UserPort);
  125.             else
  126.                if( Signals & InfoSignal )  // Check if 'InfoWindow' sent the message.
  127.                   ConverterMessage = GT_GetIMsg(InfoWindow->UserPort);
  128.                
  129.          if(ConverterMessage )   // Did a valid message appear?
  130.          {
  131.             switch( ((struct Gadget *) ConverterMessage->IAddress)->GadgetID )
  132.             {
  133.                LONG TempLong, TempLong2;
  134.                
  135.                case GD_Quit:
  136.                   FadeColours( FADE_DOWN, 320, ViewScreen );
  137.                   ExitLoop = TRUE;
  138.                   break;
  139.                   
  140.                case GD_Load:
  141.                   CloseThisWindow(&InfoWindow);
  142.                   InfoSignal = NULL;
  143.                   PositionScreen(PanelScreen, 0, 12);
  144.                   GetGadgetStatus(GD_FileMode, GTCY_Active, &TempLong,
  145.                                                TAG_DONE);
  146.                   LoadPicture( (enum FileModeType) TempLong);
  147.                   PositionScreen(PanelScreen, PubScreenHeight-PanelHeight, 12);
  148.                   PrintIText( &(PanelScreen->RastPort), &IText_MouseCoords, MouseCoordsTextX, MouseCoordsTextY );
  149.                   break;
  150.                   
  151.                case GD_Save:                  
  152.                   CloseThisWindow(&InfoWindow);
  153.                   InfoSignal = NULL;
  154.                   PositionScreen(PanelScreen, 0, 12);
  155.                   GetGadgetStatus(GD_FileMode, GTCY_Active, &TempLong,
  156.                                                TAG_DONE);
  157.                   GetGadgetStatus(GD_RenderMode, GTCY_Active, &TempLong2,
  158.                                                  TAG_DONE);
  159.                   SavePicture( &PictureToSave );
  160. //                  SavePicture(struct Screen *, enum FileModeType, enum RenderModeType, IFFClip_s *);
  161.                   PositionScreen(PanelScreen, PubScreenHeight-PanelHeight, 12);
  162.                   PrintIText(&(PanelScreen->RastPort), &IText_MouseCoords, MouseCoordsTextX, MouseCoordsTextY);
  163.                   break;
  164.                   
  165.                case GD_ClipWidth:
  166.                   GetGadgetStatus(GD_ClipWidth, GTIN_Number, &TempLong,
  167.                                                 TAG_DONE);
  168.                   if(TempLong != 0)
  169.                   {
  170.                      if( TempLong >= 1 )
  171.                         IFFClip.ClipWidth = TempLong - 1;
  172.                      else
  173.                         IFFClip.ClipWidth = TempLong + 1;
  174.                         
  175.                      HandleClipping_Gadget();
  176.                   }
  177.                   break;
  178.                   
  179.                case GD_ClipHeight:
  180.                   GetGadgetStatus(GD_ClipHeight, GTIN_Number, &TempLong,
  181.                                                  TAG_DONE);
  182.                   if(TempLong != 0)
  183.                   {
  184.                      if( TempLong >= 1 )
  185.                         IFFClip.ClipHeight = TempLong - 1;
  186.                      else
  187.                         IFFClip.ClipHeight = TempLong + 1;
  188.                         
  189.                      HandleClipping_Gadget();
  190.                   }
  191.                   break;
  192.                   
  193.                case GD_ClipLeft:
  194.                   GetGadgetStatus(GD_ClipLeft, GTIN_Number, &TempLong,
  195.                                                TAG_DONE);
  196.                   IFFClip.ClipWidth = IFFClip.ClipWidth - TempLong + IFFClip.ClipLeft;
  197.                   IFFClip.ClipLeft = TempLong;
  198.                   HandleClipping_Gadget();
  199.                   break;
  200.                   
  201.                case GD_ClipTop:
  202.                   GetGadgetStatus(GD_ClipTop, GTIN_Number, &TempLong,
  203.                                               TAG_DONE);
  204.                   IFFClip.ClipHeight = IFFClip.ClipHeight - TempLong + IFFClip.ClipTop;
  205.                   IFFClip.ClipTop = TempLong;
  206.                   HandleClipping_Gadget();
  207.                   break;
  208.                   
  209.                case GD_DrawCross:
  210.                   GetGadgetStatus(GD_DrawCross, GTCB_Checked, &TempLong,
  211.                                                 TAG_DONE);
  212.                   DrawHairCross = (BOOL)TempLong;
  213.                   break;
  214.                
  215.                case GD_Info:
  216.                   InfoSignal = DisplayInfo();
  217.                   break;
  218.                   
  219.                case GD_ByteBoundry:
  220.                   GetGadgetStatus(GD_ByteBoundry, GTMX_Active, &ByteBoundry,
  221.                                                   TAG_DONE);
  222.                   break;
  223.             }
  224.             
  225.             switch(ConverterMessage->Class)
  226.             {
  227.                case IDCMP_MOUSEMOVE:
  228.                   if( ((PanelWindow->MouseY < 0) || Clipping) && PictureValid )
  229.                   {
  230.                      WORD MouseX = ViewWindow->MouseX;
  231.                      WORD MouseY = ViewWindow->MouseY;
  232.                      
  233.                      MakeByteBoundry( &MouseX, Clipping );
  234.                         
  235.                      if(MouseX < 0         ) MouseX = 0;
  236.                      if(MouseX >= PicWidth ) MouseX = PicWidth-1;
  237.                      if(MouseY < 0         ) MouseY = 0;
  238.                      if(MouseY >= PicHeight) MouseY = PicHeight-1;
  239.                      
  240.                      MakeDecimal(MouseX, MouseCoords,   4);
  241.                      MakeDecimal(MouseY, MouseCoords+7, 4);
  242.                      PrintIText(&(PanelScreen->RastPort), &IText_MouseCoords, MouseCoordsTextX, MouseCoordsTextY);
  243.                      
  244.                      if( !Clipping )
  245.                      {
  246.                         if( OldMouseX >= 0 )
  247.                            DrawCross( &(ViewScreen->RastPort), OldMouseX, OldMouseY );
  248.                         DrawCross( &(ViewScreen->RastPort), MouseX, MouseY );
  249.                         
  250.                         OldMouseX = MouseX;
  251.                         OldMouseY = MouseY;
  252.                      }
  253.                      else
  254.                         HandleClipping_Mouse( MouseX, MouseY );
  255.                   }
  256.                   else
  257.                   {
  258.                      UWORD i;
  259.                      UBYTE *copy1 = MouseCoords;
  260.                      UBYTE *copy2 = NoMouseCoords;
  261.                      
  262.                      for(i=0; i<sizeof(NoMouseCoords); i++)
  263.                         *copy1++ = *copy2++;
  264.                         
  265.                      PrintIText(&(PanelScreen->RastPort), &IText_MouseCoords, MouseCoordsTextX, MouseCoordsTextY);
  266.                      
  267.                      if( OldMouseX >= 0 )
  268.                         DrawCross( &(ViewScreen->RastPort), OldMouseX, OldMouseY );
  269.                      OldMouseX = -1;
  270.                   }
  271.                   break;
  272.                   
  273.                case IDCMP_MOUSEBUTTONS:
  274.                   if( ConverterMessage->Code == IECODE_RBUTTON )
  275.                   {
  276.                      ULONG register ScreenDepthFlag;
  277.                      
  278.                      if( PanelInFront )
  279.                         ScreenDepthFlag = SDEPTH_TOBACK;
  280.                      else
  281.                         ScreenDepthFlag = SDEPTH_TOFRONT;
  282.                      
  283.                      ScreenDepth(PanelScreen, ScreenDepthFlag | SDEPTH_INFAMILY, NULL);
  284.                      PanelInFront = !PanelInFront;
  285.                   }
  286.                   
  287.                   if( (ConverterMessage->Code == IECODE_LBUTTON) && PictureValid )
  288.                   {
  289.                      if( PanelWindow->MouseY < 0 )
  290.                      {
  291.                         IFFClip.ClipPointX = ViewWindow->MouseX;
  292.                         IFFClip.ClipPointY = ViewWindow->MouseY;
  293.                         
  294.                         Clipping = TRUE;
  295.                         
  296.                         DrawCross( &(ViewScreen->RastPort), OldMouseX, OldMouseY ); 
  297.                         HandleClipping_Mouse( IFFClip.ClipPointX, IFFClip.ClipPointY );
  298.                      }
  299.                   }
  300.                   else
  301.                      if( ConverterMessage->Code == (IECODE_LBUTTON | IECODE_UP_PREFIX) )
  302.                      {
  303.                         if( Clipping == TRUE )
  304.                         {
  305.                            OldMouseX = ViewWindow->MouseX;
  306.                            OldMouseY = ViewWindow->MouseY;
  307.                            DrawCross( &(ViewScreen->RastPort), OldMouseX, OldMouseY ); 
  308.                         }
  309.                         Clipping = FALSE;
  310.                      }
  311.                   break;
  312.                   
  313.                case IDCMP_ACTIVEWINDOW:
  314.                   RemoveInfoWindow = TRUE;
  315.                   break;
  316.             }  // END switch
  317.          }   // END if
  318.  
  319.          GT_ReplyIMsg(ConverterMessage);
  320.          
  321.       } while (ConverterMessage);   // More messages?
  322.       
  323.       if( RemoveInfoWindow )
  324.       {
  325.          CloseThisWindow(&InfoWindow);
  326.          InfoSignal = NULL;
  327.       }
  328.       
  329.    } while (!ExitLoop);   // Loop until exit.
  330. }
  331.  
  332.  
  333. /*
  334. **  HandlClipping_Mouse(MouseX, MouseY)
  335. **
  336. **     Handle clipping when the mouse moved.
  337. **
  338. **  pre:  MouseX, MouseY - Current x,y coordinates of the mouse.
  339. **  post: None.
  340. **
  341. */
  342. void HandleClipping_Mouse(register WORD MouseX, register WORD MouseY)
  343. {
  344.    if(MouseX < 0         ) MouseX = 0;
  345.    if(MouseX >= PicWidth ) MouseX = PicWidth-1;
  346.    if(MouseY < 0         ) MouseY = 0;
  347.    if(MouseY >= PicHeight) MouseY = PicHeight-1;
  348.    
  349.    if( MouseX >= IFFClip.ClipPointX )
  350.       IFFClip.ClipLeft = IFFClip.ClipPointX;
  351.    else
  352.       IFFClip.ClipLeft = MouseX;
  353.       
  354.    if( MouseY >= IFFClip.ClipPointY )
  355.       IFFClip.ClipTop  = IFFClip.ClipPointY;
  356.    else
  357.       IFFClip.ClipTop  = MouseY;
  358.    
  359.    if( (IFFClip.ClipWidth = MouseX - IFFClip.ClipPointX) < 0 )
  360.       IFFClip.ClipWidth = -IFFClip.ClipWidth;
  361.       
  362.    if( (IFFClip.ClipHeight = MouseY - IFFClip.ClipPointY) < 0 )
  363.       IFFClip.ClipHeight = -IFFClip.ClipHeight;
  364.    
  365.    IFFClip.ClipSize = ((IFFClip.ClipWidth + 8) >> 3) * (IFFClip.ClipHeight + 1) * PicDepth;
  366.    
  367.    UpdateClipping();
  368.    
  369. }
  370.  
  371.  
  372. /*
  373. **  HandleClipping_Gadget()
  374. **
  375. **     Handle all clipping when one or more of the clipping gadgets has changed.
  376. **
  377. **  pre:  None.
  378. **  post: None.
  379. **
  380. */
  381. void HandleClipping_Gadget()
  382. {
  383.    // Check if 'ClipWidth' is negative.
  384.    if( IFFClip.ClipWidth < 0 )
  385.    {
  386.       // Negative 'ClipWidth' makes little sence, so make it postive
  387.       // and adjust 'ClipLeft' to compensate for negative width.
  388.       IFFClip.ClipWidth = -IFFClip.ClipWidth;
  389.       IFFClip.ClipLeft = IFFClip.ClipLeft - IFFClip.ClipWidth;
  390.    }
  391.    
  392.    // Check if 'ClipHeight' is negative.
  393.    if( IFFClip.ClipHeight < 0 )
  394.    {
  395.       // Negative 'ClipHeight' makes little sence, so make it postive
  396.       // and adjust 'ClipTop' to compensate for negative height.
  397.       IFFClip.ClipHeight = -IFFClip.ClipHeight;
  398.       IFFClip.ClipTop = IFFClip.ClipTop - IFFClip.ClipHeight;
  399.    }
  400.    
  401.    if( IFFClip.ClipLeft < 0 )
  402.       // 'ClipLeft' cannot be smaller than 0! So, when nagative, make it 0.
  403.       IFFClip.ClipLeft = 0;
  404.    else
  405.       if( IFFClip.ClipLeft >= PicWidth )
  406.          // 'ClipLeft' cannot be greater than 'PicWidth'.
  407.          IFFClip.ClipLeft = PicWidth - 1;
  408.    
  409.    if( IFFClip.ClipTop < 0 )
  410.       // 'ClipTop' cannot be negative. Make it 0.
  411.       IFFClip.ClipTop = 0;
  412.    else
  413.       if( IFFClip.ClipTop >= PicHeight )
  414.          // 'ClipTop' cannot be greater than 'PicHeight'.
  415.          IFFClip.ClipTop = PicHeight - 1;
  416.    
  417.    // See whether 'ClipWidth' and 'ClipHeight' still fits the screen boundry.
  418.    if( (IFFClip.ClipWidth + IFFClip.ClipPointX) > PicWidth )
  419.       IFFClip.ClipWidth = PicWidth - IFFClip.ClipLeft - 1;
  420.    
  421.    if( (IFFClip.ClipHeight + IFFClip.ClipPointY) > PicHeight )
  422.       IFFClip.ClipHeight = PicHeight - IFFClip.ClipTop - 1;
  423.    
  424.    IFFClip.ClipSize = ((IFFClip.ClipWidth + 8) >> 3) * (IFFClip.ClipHeight + 1) * PicDepth;
  425.    UpdateClipping();
  426. }
  427.  
  428.  
  429. /*
  430. **  UpdateClipping()
  431. **
  432. **     'UpdateClipping' preforms a total update of the clipping. This
  433. **     means that the old Clip rectangle is ereased and the new one
  434. **     drawn. Also, the gadget containing size and offset, are updated.
  435. **
  436. **  pre:  None.
  437. **  post: None.
  438. **
  439. */
  440. void UpdateClipping()
  441. {
  442.    UpdateDimensions(GD_ClipLeft,   IFFClip.ClipLeft,
  443.                     GD_ClipTop,    IFFClip.ClipTop,
  444.                     GD_ClipWidth,  IFFClip.ClipWidth + 1,
  445.                     GD_ClipHeight, IFFClip.ClipHeight + 1,
  446.                     GD_ClipSize,   IFFClip.ClipSize,
  447.                     GD_Sentinal);
  448.  
  449.    if( OldClipLeft >= 0 )
  450.       DrawClipRect( &(ViewScreen->RastPort), OldClipLeft, OldClipTop, OldClipRight, OldClipBottom );
  451.    DrawClipRect( &(ViewScreen->RastPort), IFFClip.ClipLeft, IFFClip.ClipTop, IFFClip.ClipLeft + IFFClip.ClipWidth, IFFClip.ClipTop + IFFClip.ClipHeight );
  452.    
  453.    OldClipLeft   = IFFClip.ClipLeft;
  454.    OldClipTop    = IFFClip.ClipTop;
  455.    OldClipRight  = IFFClip.ClipLeft + IFFClip.ClipWidth;
  456.    OldClipBottom = IFFClip.ClipTop  + IFFClip.ClipHeight;
  457. }
  458.  
  459.  
  460. /*
  461. **  DrawClipRect(rp, X1, Y1, X2, Y2)
  462. **
  463. **     Draws a rectangle.
  464. **
  465. **  pre:  rp - pointer to the destination RastPort.
  466. **        X1, Y1 - x, y coordinates of rectangle.
  467. **        X2, Y2 - x, y coordinates of rectangle.
  468. **  post: None.
  469. **
  470. */
  471. void DrawClipRect(struct RastPort *rp, UWORD X1, UWORD Y1, UWORD X2, UWORD Y2)
  472. {
  473.    register UBYTE ViewDrawMode = GetDrMd( &(ViewScreen->RastPort) );
  474.  
  475.    SetDrMd( &(ViewScreen->RastPort), COMPLEMENT );
  476.  
  477.    Move(rp, X1, Y1);
  478.    
  479.    Draw(rp, X2, Y1);
  480.    Draw(rp, X2, Y2);
  481.    Draw(rp, X1, Y2);
  482.    Draw(rp, X1, Y1);
  483.  
  484.    SetDrMd( &(ViewScreen->RastPort), ViewDrawMode );
  485. }
  486.  
  487.  
  488. /*
  489. **  DrawCross(rp, X, Y)
  490. **
  491. **     Draw a cross to indicate the cursor hot point.
  492. **
  493. **  pre:  rp- pointer to the destination RastPort
  494. **        X, Y - x, y coordinates of cursor hot point.
  495. **  post: None.
  496. **
  497. */
  498. void DrawCross(struct RastPort *rp, UWORD X, UWORD Y)
  499. {
  500.    register UBYTE ViewDrawMode;
  501.  
  502.    if( DrawHairCross )
  503.    {
  504.       ViewDrawMode = GetDrMd( &(ViewScreen->RastPort) );
  505.       
  506.       SetDrMd( &(ViewScreen->RastPort), COMPLEMENT );
  507.       
  508.       Move(rp, 0, Y);
  509.       Draw(rp, (ViewScreen->Width) - 1, Y);
  510.       
  511.       Move(rp, X, 0);
  512.       Draw(rp, X, (ViewScreen->Height) - 1);
  513.  
  514.       SetDrMd( &(ViewScreen->RastPort), ViewDrawMode );
  515.    }
  516. }
  517.  
  518.  
  519. /*
  520. **  MakeByteBoundry( coord )
  521. **
  522. **     Depending on the type of Boundry coord gets a new value.
  523. **     There are several different Boundry types:
  524. **     None   - No changes to coord.
  525. **     Type 1 - The width of the clip reagon is a multple of 8.
  526. **     Type 2 - The left coordinate of the clip reagon is a multiple of 8.
  527. **     Type 3 - The left and width of the clip reagon are both a multiple of 8.
  528. **     
  529. **  pre:  coord - Pixel values.
  530. **  post: coord - Pixel values according to ByteBoundry.
  531. **
  532. */
  533. void MakeByteBoundry(WORD *coord, BOOL Clipping)
  534. {
  535.  
  536.    switch( ByteBoundry )   
  537.    {
  538.       case BB_Type1:
  539.          *coord = (*coord +7) & 0xfff8;
  540.       case BB_Type2:
  541.          if( Clipping )
  542.             *coord = (*coord +7) & 0xfff8;
  543.       case BB_Type3:
  544.          break;      
  545.    }
  546. }
  547.