home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / Triton / Source / src / dragndrop_prim.c < prev    next >
C/C++ Source or Header  |  1998-05-23  |  12KB  |  387 lines

  1. #ifdef oiehroishreoifh
  2.  
  3. /*
  4.  *  OpenTriton -- A free release of the triton.library source code
  5.  *  Copyright (C) 1993-1998  Stefan Zeiger
  6.  *
  7.  *  This program is free software; you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation; either version 2 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  This program is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License
  18.  *  along with this program; if not, write to the Free Software
  19.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  *
  21.  *  dragndrop_prim.c - Drag & Drop primitives
  22.  *
  23.  */
  24.  
  25.  
  26. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  27. //////////////////////////////////////////////////////////////////////////////////////// Include our stuff //
  28. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  29.  
  30. #define INTUI_V36_NAMES_ONLY
  31. #define TR_NOSUPPORT
  32. #define TR_THIS_IS_TRITON
  33.  
  34. #include <exec/types.h>
  35. #include <exec/lists.h>
  36. #include <exec/ports.h>
  37. #include <exec/libraries.h>
  38. #include <exec/memory.h>
  39. #include <exec/execbase.h>
  40. #include <graphics/gfx.h>
  41. #include <graphics/gels.h>
  42. #include <graphics/clip.h>
  43. #include <graphics/rastport.h>
  44. #include <graphics/view.h>
  45. #include <graphics/gfxbase.h>
  46. #include <graphics/text.h>
  47. #include <intuition/intuition.h>
  48. #include <intuition/intuitionbase.h>
  49.  
  50. #include <clib/exec_protos.h>
  51. #include <clib/dos_protos.h>
  52. #include <clib/alib_protos.h>
  53. #include <pragmas/exec_pragmas.h>
  54. #include <pragmas/dos_pragmas.h>
  55.  
  56. #include "include/libraries/triton.h"
  57. #include "include/clib/triton_protos.h"
  58. #include "dragndrop.h"
  59.  
  60.  
  61. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  62. ////////////////////////////////////////////////////////////////////////////////////// Extended bob object //
  63. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  64.  
  65. typedef struct TR_Bob
  66. {
  67.   struct TR_Project * project;       // Triton Project
  68.   struct GelsInfo     gelinfo;       // GelInfo for entire structure
  69.   WORD                nextline [8];  // Nextline data
  70.   WORD              * lastcolor [8]; // Last colour data
  71.   struct RastPort   * mainrast;      // Rastport bob is displayed in
  72.   struct collTable    colltable;     // Collision table
  73.   struct VSprite      vshead;        // Head sprite anchor
  74.   struct VSprite      vstail;        // Tail sprite anchor
  75.   struct VSprite      bobvsprite;    // Vsprite used for bob
  76.   struct Bob          bob;           // Data for a single bob
  77.   struct RastPort     rastport;      // Rastport for our BOB
  78.   struct BitMap       bitmap;        // Bitmap for our BOB
  79.   ULONG               planesize;     // Size of one plane in bob
  80.   UBYTE             * planedata;     // Pointer to first plane
  81.   UBYTE             * chipdata;      // Pointer to all CHIP RAM data
  82.   ULONG               chipsize;      // Total size of allocated CHIP
  83. } TR_Bob;
  84.  
  85.  
  86. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  87. //////////////////////////////////////////////////////////////////////////////////////// Private functions //
  88. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  89.  
  90. TR_Bob * TR_CreateBob(struct TR_Project *project, int width, int height, int transp);
  91. void     TR_FreeBob(TR_Bob *aBob);
  92. void     TR_UpdateBob(TR_Bob *aBob, ULONG x, ULONG y);
  93. int      TR_PickUpBob(TR_Bob *aBob, ULONG hit, ULONG x, ULONG y);
  94. void     TR_DropBob(TR_Bob *aBob);
  95.  
  96.  
  97. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  98. /////////////////////////////////////////////////////////////////////////////////// Drag & drop primitives //
  99. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  100.  
  101. /*
  102.  *        TR_CreateBob(project, width, height, transp)
  103.  *
  104.  *        This function does all the initialisation necessary to allow us
  105.  *        to use a single BOB in the given rastport. A new bitmap is allocated
  106.  *        of dimensions width x height into which data can be rendered.
  107.  *
  108.  *        If successful, this function returns a pointer to a TR_Bob structure
  109.  *        which contains, among other things, the rastport and bob structure
  110.  *        you will use when calling system routines to manipulate the bob.
  111.  *
  112.  *        Note that the bob must be removed from the Gel list before rendering
  113.  *        into it.
  114.  *
  115.  *        If transp is true, then the BOB will be transparent on colour zero.
  116.  *        In this case, you must call InitMasks(aBob->vsprite) whenever
  117.  *        you make a change to the image, to recalculate the mask.
  118.  */
  119.  
  120. TR_Bob *TR_CreateBob(struct TR_Project *project, int width, int height, int transp)
  121. {
  122.   struct GelsInfo * gInfo;
  123.   struct Bob      * bob;
  124.   struct VSprite  * vsprite;
  125.   struct RastPort * rport=project->trp_Window->RPort;
  126.   TR_Bob          * aBob;
  127.   UBYTE           * chipdata;
  128.   ULONG             linesize, planesize, imagesize, chipsize, wordwidth, depth, i;
  129.  
  130.   wordwidth    = (width + 15) >> 4;
  131.   depth        = rport->BitMap->Depth;
  132.  
  133.   if(!( aBob = (TR_Bob *)AllocMem(sizeof(TR_Bob), MEMF_CLEAR) )) return NULL;
  134.         
  135.   aBob->mainrast        = rport;
  136.   aBob->project                 = project;
  137.  
  138.   gInfo                = &aBob->gelinfo;
  139.   gInfo->nextLine        = aBob->nextline;
  140.   gInfo->lastColor    = aBob->lastcolor;
  141.   gInfo->collHandler    = &aBob->colltable;
  142.   gInfo->sprRsrvd        = 0x03;
  143.  
  144.   /*
  145.    *        Set left-most and top-most to 1 to better keep items
  146.    *        inside the display boundaries.
  147.    */
  148.   gInfo->leftmost   = 1;
  149.   gInfo->topmost      = 1;
  150.   gInfo->rightmost  = (rport->BitMap->BytesPerRow << 3) - 1;
  151.   gInfo->bottommost = rport->BitMap->Rows - 1;
  152.   rport->GelsInfo      = gInfo;
  153.   InitGels(&aBob->vshead, &aBob->vstail, gInfo);
  154.  
  155.   /*
  156.    *        Now allocate a new BOB to be linked into this structure.
  157.    *        We also allocate a bitmap associated with the bob, and
  158.    *        give it its own RastPort so that we can render into it.
  159.    */
  160.   linesize    = sizeof(WORD)  * wordwidth;
  161.   planesize    = linesize      * height;
  162.   imagesize    = planesize        * depth;
  163.   chipsize    = imagesize*2 + linesize + planesize;
  164.   bob            = &aBob->bob;
  165.   vsprite        = &aBob->bobvsprite;
  166.  
  167.   if (!(chipdata = AllocMem(chipsize, MEMF_CHIP | MEMF_CLEAR))) {
  168.     rport->GelsInfo = NULL;
  169.     FreeMem(aBob, sizeof(TR_Bob));
  170.     return (NULL);
  171.   }
  172.   aBob->chipdata        = (void *)(chipdata);
  173.   aBob->chipsize        = chipsize;
  174.  
  175.   aBob->planedata    = (void *)(chipdata);
  176.   bob->SaveBuffer        = (void *)(chipdata + imagesize);
  177.   vsprite->BorderLine = (void *)(chipdata + imagesize*2);
  178.   vsprite->CollMask    = (void *)(chipdata + imagesize*2 + linesize);
  179.  
  180.   vsprite->Y            = -9999;
  181.   vsprite->X            = -9999;
  182.   vsprite->Flags      = SAVEBACK | (transp ? OVERLAY : 0);
  183.   vsprite->Width      = wordwidth;
  184.   vsprite->Depth      = depth;
  185.   vsprite->Height     = height;
  186.   vsprite->MeMask     = 0;
  187.   vsprite->HitMask    = 0;
  188.   vsprite->ImageData  = (void *)aBob->planedata;
  189.   vsprite->SprColors  = NULL;
  190.   vsprite->PlanePick  = -1;
  191.   vsprite->PlaneOnOff = 0x00;
  192.   InitMasks(vsprite);
  193.  
  194.   vsprite->VSBob      = bob;
  195.   bob->BobVSprite     = vsprite;
  196.   bob->ImageShadow    = vsprite->CollMask;
  197.   bob->Flags          = 0;
  198.   bob->Before         = NULL;
  199.   bob->After          = NULL;
  200.   bob->BobComp        = NULL;
  201.   bob->DBuffer        = NULL;
  202.  
  203.   /*
  204.    *        Now setup the Rastport and Bitmap so we can render into the BOB
  205.    */
  206.   InitRastPort(&aBob->rastport);
  207.   InitBitMap(&aBob->bitmap, depth, width, height);
  208.   aBob->rastport.BitMap = &aBob->bitmap;
  209.  
  210.   for (i = 0; i < depth; i++)
  211.     aBob->bitmap.Planes[i] = aBob->planedata + i * planesize;
  212.  
  213.   return (aBob);
  214. }
  215.  
  216. /*
  217.  *        TR_FreeBob(aBob)
  218.  *
  219.  *        Frees the bob allocated earlier, along with all other associated info 
  220.  *        Also removes all GELs from the system rastport.
  221.  */
  222. void TR_FreeBob(TR_Bob *aBob)
  223. {
  224.     aBob->mainrast->GelsInfo = NULL;
  225.     FreeMem(aBob->chipdata, aBob->chipsize);
  226.     FreeMem(aBob, sizeof(TR_Bob));
  227. }
  228.  
  229. /*
  230.  *        TR_PickUpBob(aBob, hit, x, y)
  231.  *
  232.  *        Picks up the line and copies it into our bob's bitmap, then adds
  233.  *        the bob to the screen at the current mouse location so the
  234.  *        user can drag it around.
  235.  *
  236.  *        If the hit is correct, we can ignore Y since the field being
  237.  *        dragged is in the right box and we already know which line.
  238.  *        Otherwise, Y is a pixel co-ordinate that we use to calculate
  239.  *        the line of the box that's being chosen.
  240.  *
  241.  *        If hit is FBOX_SELECTLEFT, then we need to calculate which line
  242.  *        we are dealing with.
  243.  *
  244.  *        We also record the start line we use.
  245.  *
  246.  *        Returns true for success, false if we didn't have a hit after all.
  247.  */
  248. int TR_PickUpBob(TR_Bob *aBob, ULONG hit, ULONG x, ULONG y)
  249. {
  250.     struct RastPort *rport    = aBob->project->trp_Window->RPort;
  251.     struct RastPort *bobrport = &aBob->rastport;
  252.     char viewbuf[50];
  253.     int line;
  254.     int box;
  255.     int xpos;
  256.     int ypos;
  257.     int len;
  258.  
  259. //    if (hit == FBOX_NOSELECT) {
  260. //        return (FALSE);
  261. //    } else if (hit == FBOX_SELECTLEFT) {
  262. //        box  = FORMAT_LEFTBOX;
  263. //        line = (y - FBoxA_Top - 2);
  264. //        if (line < FBoxSpacing)
  265. //            line = 0;
  266. //        else
  267. //            line /= FBoxSpacing;
  268. //        if (line >= FBoxA_CurLines)
  269. //            return (FALSE);
  270. //    } else {
  271. //        /* Right box */
  272. //        box  = FORMAT_RIGHTBOX;
  273. //        line = hit;
  274. //        if (line >= FBoxB_CurLines)
  275. //            return (FALSE);
  276.     }
  277.  
  278.     /*
  279.      *        Okay, got a definite hit so go ahead and start the drag
  280.      */
  281.     FormatDragBox    = box;
  282.     FormatDragLine    = line;
  283.     
  284.     SetRast(bobrport, 0);                /* Erase rast bitmap     */
  285.     SetAPen(bobrport, 2);                /* White foreground        */
  286.     SetBPen(bobrport, 1);                /* Black background        */
  287.     SetDrMd(bobrport, JAM2);            /* Solid text            */
  288.     SetFont(bobrport, FormBufFont);
  289.  
  290.     if (box == FORMAT_LEFTBOX) {
  291.         FieldInit *fi = &FieldTypes[AvailableFields[line].type];
  292.  
  293.         mysprintf(viewbuf, FBoxA_FormString, MSG(fi->titlemsgid), fi->idchar);
  294.         len  = FBoxA_NumChars;
  295.         xpos = FBoxA_Left + 2;
  296.         ypos = FBoxA_Top  + 2 + line * FBoxSpacing;
  297.     } else {
  298.         FieldInit *fi = &FieldTypes[CurrentFields[line].type];
  299.  
  300.         mysprintf(viewbuf, FBoxB_FormString, MSG(fi->titlemsgid),
  301.                            CurrentFields[line].width, fi->idchar);
  302.         len  = FBoxB_NumChars;
  303.         xpos = FBoxB_Left + 2;
  304.         ypos = FBoxB_Top  + 2 + line * FBoxSpacing;
  305.     }
  306.     ABob->bob.BobVSprite->X = xpos;
  307.     ABob->bob.BobVSprite->Y = ypos;
  308.     FBoxDeltaX = xpos - x;
  309.     FBoxDeltaY = ypos - y;
  310.  
  311.     Move(bobrport, 0, FormBufFont->tf_Baseline+1);
  312.     Text(bobrport, viewbuf, len);
  313.  
  314.     /*
  315.      *        Now draw highlight above and below the text
  316.      */
  317.     SetAPen(bobrport, 1);
  318.     Move(bobrport, 0, 0);
  319.     Draw(bobrport, len * FormBufFont->tf_XSize - 1, 0);
  320.     Move(bobrport, 0, FormBufFont->tf_YSize + 1);
  321.     Draw(bobrport, len * FormBufFont->tf_XSize - 1, FormBufFont->tf_YSize + 1);
  322.  
  323.     InitMasks(ABob->bob.BobVSprite);
  324.     SortGList(rport);
  325.     DrawGList(rport, ViewPortAddress(aBob->project->trp_Window));
  326.     return (1);
  327. }
  328.  
  329.  
  330. /****i* triton.library/TR_DropBob ******
  331. *
  332. *   NAME    
  333. *    TR_DropBob -- Drop a Triton bob
  334. *
  335. *   SYNOPSIS
  336. *    TR_DropBob(aBob)
  337. *
  338. *    void TR_DropBob(TR_Bob *);
  339. *
  340. *   FUNCTION
  341. *    Drops the specified bob, triggering any necessary action.
  342. *
  343. *   SEE ALSO
  344. *    TR_CreateBob(), TR_FreeBob(), TR_PickUpBob(), TR_UpdateBob()
  345. *
  346. ******/
  347.  
  348. void TR_DropBob(TR_Bob *aBob)
  349. {
  350.   TR_UpdateBob(aBob, 0, -9999); // Kill any highlighted region
  351. }
  352.  
  353.  
  354. /****i* triton.library/TR_UpdateBob ******
  355. *
  356. *   NAME    
  357. *    TR_UpdateBob -- Updates the position of a Triton bob
  358. *
  359. *   SYNOPSIS
  360. *    TR_UpdateBob(aBob, x, y)
  361. *
  362. *    void TR_UpdateBob(TR_Bob *, ULONG, ULONG);
  363. *
  364. *   FUNCTION
  365. *    Moves a currently displayed Triton bob to a new position and
  366. *    draws any highlights etc. that are deemed necessary.
  367. *
  368. *   SEE ALSO
  369. *    TR_CreateBob(), TR_FreeBob(), TR_PickUpBob(), TR_DropBob()
  370. *
  371. ******/
  372.  
  373. void TR_UpdateBob(TR_Bob *aBob, ULONG x, ULONG y)
  374. {
  375.   // Move bob off-screen
  376.   //ABob->bob.BobVSprite->X = 0;
  377.   //ABob->bob.BobVSprite->Y = -9999;
  378.  
  379.   // Do some rendering in the window
  380.  
  381.   // Move bob back on screen
  382.   ABob->bob.BobVSprite->X = x;
  383.   ABob->bob.BobVSprite->Y = y;
  384. }
  385.  
  386. #endif
  387.