home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 594a.lha / maker_v0.1 / maker.c < prev    next >
C/C++ Source or Header  |  1991-10-18  |  12KB  |  590 lines

  1. #define MAIN        //this tells global.h to declare globals, rather than making them externs
  2.  
  3. #include "global.h"
  4.  
  5. #include <libraries/asl.h>
  6. #include <graphics/gfxmacros.h>
  7. #include <intuition/gadgetclass.h>
  8. #include <exec/memory.h>
  9. #include <exec/nodes.h>
  10. #include <exec/lists.h>
  11.  
  12. #include <proto/intuition.h>
  13. #include <proto/graphics.h>
  14. #include <proto/asl.h>
  15. #include <proto/exec.h>
  16. #include <proto/gadtools.h>
  17. #include <proto/layers.h>
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22.  
  23. #define    SIZEBOX_X    8
  24. #define    SIZEBOX_Y    6
  25.  
  26. Point        minRectSize = { 5, 5 },
  27.             maxRectSize = { 1000, 1000 };
  28.  
  29. void main()
  30. {
  31.     IntuiMessage    *message;
  32.     BOOL                done = FALSE;
  33.     USHORT            code;
  34.     Gadget            *gadgetPtr;
  35.     ULONG                class;
  36.     Point                mousePt;
  37.     TimeStamp        timeStamp;
  38.     
  39.     
  40.     init();
  41.     
  42.     while (!done)
  43.     {
  44.         Wait(1 << gWindPtr->UserPort->mp_SigBit);
  45.         
  46.         while (message = GT_GetIMsg( gWindPtr->UserPort ))
  47.         {
  48.             class = message->Class;
  49.             code = message->Code;
  50.             mousePt.x = message->MouseX;
  51.             mousePt.y = message->MouseY;
  52.             timeStamp.seconds = message->Seconds;
  53.             timeStamp.micros = message->Micros;
  54.             if (class == GADGETUP || class == GADGETDOWN)
  55.                 gadgetPtr = (Gadget *) message->IAddress;
  56.             GT_ReplyIMsg(message);
  57.             switch(class)
  58.             {
  59.                 case IDCMP_MOUSEBUTTONS:
  60.                     DoMouse(&mousePt, code, &timeStamp);
  61.                     break;
  62.                 case IDCMP_GADGETUP:
  63.                     DoGadget(gadgetPtr, code);
  64.                     break;
  65.                 case IDCMP_MENUPICK:
  66.                     DoMenu(code);
  67.                     break;
  68.                 case IDCMP_CLOSEWINDOW:
  69.                     done = TRUE;
  70.                     break;
  71.                 case IDCMP_VANILLAKEY:
  72.                     DoKey(code);
  73.                     break;
  74.                 case IDCMP_RAWKEY:
  75.                     break;
  76.                 case IDCMP_NEWSIZE:
  77.                     SetClip();
  78.                     ReDraw();
  79.                     break;
  80.             }
  81.         }
  82.         
  83.     }
  84.  
  85.     quit("Normal Termination\n");
  86. }
  87.  
  88. void DoGadget(Gadget *gadgetPtr, USHORT code)
  89. {
  90.     GadgetObj        *objPtr = gadgetPtr->UserData;
  91.     
  92.     if (!objPtr)
  93.         return;
  94.     
  95.     switch(objPtr->type)
  96.     {
  97.         case OTYPE_Cycle:
  98. //            GT_SetGadgetAttrs(gadgetPtr, gWindPtr, 0L,
  99. //                                                    GTCY_Active, ( (CycleObj *) objPtr)->tempValue,
  100. //                                                    TAG_DONE);
  101.             break;
  102.     }
  103. }
  104.  
  105. /* ===================================================================================== */
  106.  
  107. void    DoKey(USHORT code)
  108. {
  109.     switch (code)
  110.     {
  111.         case KEYCODE_Delete:
  112.         case KEYCODE_BackSpace:
  113.             if (gSelObj)
  114.             {
  115.                 DisposeObj(gSelObj);
  116.                 gSelObj = 0L;
  117.                 ReDraw();
  118.             }
  119.             break;
  120.         default:
  121.             printf("got key code %d\n", code);
  122.             break;
  123.     }
  124. }
  125.  
  126. /* ===================================================================================== */
  127.  
  128. void    DoMouse(Point *mousePt, USHORT code, TimeStamp *timeStamp)
  129. {
  130.     LinkNode        *nodePtr,
  131.                     *oldSelObj = gSelObj;
  132.     Rect            sizeRect;
  133.     BOOL            changedFlag = FALSE;
  134.     
  135.     if (code != SELECTDOWN)
  136.         return;
  137.         
  138.     //    Erase the current selection rectangle
  139.     
  140.     if (gSelObj)
  141.         SelectObj(gSelObj);
  142.     gSelObj = 0L;
  143.  
  144.     nodePtr = FindObj(mousePt);
  145.     
  146.     if (nodePtr)
  147.     {
  148.         sizeRect = nodePtr->rect;
  149.         sizeRect.minX = sizeRect.maxX - SIZEBOX_X;
  150.         sizeRect.minY = sizeRect.maxY - SIZEBOX_Y;
  151.         
  152.         // first, check for a double-click
  153.         
  154.         if (oldSelObj == nodePtr && DoubleClick(gSelTime.seconds, gSelTime.micros,
  155.                     timeStamp->seconds, timeStamp->micros))
  156.         {
  157.             HandleDoubleClick(nodePtr);
  158.         }
  159.             
  160.         // second, check for a click in the size box
  161.         
  162.         else if (CanResize(nodePtr) && PtinRect( mousePt, &sizeRect ))
  163.         {
  164.             UpdateInfoItem(nodePtr);
  165.             if (SizeRect(gWindPtr, mousePt, &nodePtr->rect, &minRectSize, &maxRectSize))
  166.                 changedFlag = TRUE; 
  167.         }
  168.             
  169.         // otherwise, it's an ordinary drag operation
  170.         
  171.         else
  172.         {
  173.             UpdateInfoItem(nodePtr);
  174.             if (DragRect(gWindPtr, mousePt, &nodePtr->rect))
  175.                 changedFlag = TRUE; 
  176.         }
  177.         
  178.         gSelObj = nodePtr;
  179.         gSelTime = *timeStamp;
  180.     }
  181.     
  182.     //    Draw the selection rectangle
  183.     
  184.     if (changedFlag)
  185.     {
  186.         if (GetGadgetData(nodePtr, 0L, 0L, 0L, 0L, 0L))    // just verify that it's a gadget object
  187.             MakeGadget( nodePtr );
  188.         
  189.         // if there are any current ListView connections to this gadget, update them
  190.     
  191.         UpdateLVConnect( nodePtr, FALSE );
  192.     
  193.         ReDraw();
  194.     }
  195.     else if (gSelObj)
  196.     {
  197.         SelectObj(gSelObj);
  198.         UpdateInfoItem(gSelObj);
  199.         UpdateInfoWindow(&gSelObj->rect);
  200.     }
  201.     else
  202.     {
  203.         UpdateInfoItem(0L);
  204.         UpdateInfoWindow(0L);
  205.     }
  206.  
  207. }
  208.  
  209. LinkNode *FindObj(Point *mousePt)
  210. {
  211.     LinkNode        *nodePtr;
  212.     
  213.     for (nodePtr=gObjList; nodePtr; nodePtr=nodePtr->next)
  214.     {
  215.         if (PtinRect( mousePt, &nodePtr->rect ))
  216.         {
  217.             return nodePtr;
  218.         }
  219.     }
  220.     
  221.     return 0L;
  222. }
  223.  
  224. void PickLVString(USHORT code)
  225. {
  226.     USHORT    itemNum;
  227.     LinkNode    *nodePtr;
  228.     
  229.     if (gSelObj && gSelObj->type == OTYPE_ListView)
  230.     {
  231.         printf("Enter the item number of the string gadget that you wish to connect:\n");
  232.         scanf("%hd", &itemNum);
  233.         nodePtr = FindNode(&gObjList, itemNum);
  234.         if (nodePtr && nodePtr->type == OTYPE_String)
  235.         {
  236.             if (!UpdateLVConnect(nodePtr, TRUE))    // clear any current connections to this string
  237.                                                                 // gadget
  238.             {
  239.                 printf("Selection failed\n");
  240.                 return;
  241.             }
  242.             ( (ListViewObj *) gSelObj)->stringObj = nodePtr;
  243.             
  244.             if (MakeGadget(gSelObj) != 0)
  245.             {
  246.                 DisposeObj(gSelObj);
  247.                 gSelObj = 0L;
  248.                 printf("Selection failed\n");
  249.             }
  250.             else
  251.                 printf("Selection successful\n");
  252.             ReDraw();
  253.         }
  254.         else if (nodePtr)
  255.             printf("Error: the selected item is not a string gadget (operation aborted)\n");
  256.         else
  257.             printf("Error: invalid item number (operation aborted)\n");
  258.     }
  259. }
  260.  
  261. /* ===================================================================================== */
  262.  
  263. void SelectObj(LinkNode *selObj)
  264. {
  265.     RastPort        *rp = gWindPtr->RPort;
  266.     Rect            tempRect = selObj->rect;
  267.     
  268.     if (!selObj)
  269.         return;
  270.     
  271.     SetDrPt(rp, DASH);
  272.     SetDrMd(rp, COMPLEMENT);
  273.     
  274.     Move(rp,    tempRect.minX,    tempRect.minY);
  275.     Draw(rp,    tempRect.maxX,    tempRect.minY);
  276.     Draw(rp,    tempRect.maxX,    tempRect.maxY);
  277.     Draw(rp,    tempRect.minX,    tempRect.maxY);
  278.     Draw(rp,    tempRect.minX,    tempRect.minY);
  279.     
  280.     if (CanResize(selObj))
  281.     {
  282.         tempRect.minX = tempRect.maxX - SIZEBOX_X;
  283.         tempRect.minY = tempRect.maxY - SIZEBOX_Y;
  284.         
  285.         RectFill( rp, tempRect.minX, tempRect.minY, tempRect.maxX, tempRect.maxY );
  286.     }
  287.     
  288.     SetDrMd(rp, JAM1);
  289.     SetDrPt(rp, SOLID);
  290. }
  291.  
  292. /* ===================================================================================== */
  293.  
  294. BOOL CanResize(LinkNode *objPtr)
  295. {
  296.     switch(objPtr->type)
  297.     {
  298.         case OTYPE_Rect:
  299.         case OTYPE_Button:
  300.         case OTYPE_Cycle:
  301.         case OTYPE_String:
  302.         case OTYPE_Palette:
  303.         case OTYPE_ListView:
  304.         case OTYPE_Scroller:
  305.         case OTYPE_Text:
  306.             return TRUE;
  307.         default:
  308.             return FALSE;
  309.     }
  310. }
  311.  
  312. /* ===================================================================================== */
  313.  
  314. void ReDraw()
  315. {
  316.     LinkNode        *nodePtr;
  317.     RastPort        *rp = gWindPtr->RPort;
  318.     Rect            *rectPtr;
  319.     
  320.     SetAPen(rp, grayPen);
  321.     RectFill(rp,    gWindPtr->BorderLeft,
  322.                         gWindPtr->BorderTop,
  323.                         gWindPtr->Width - gWindPtr->BorderRight - 1,
  324.                         gWindPtr->Height - gWindPtr->BorderBottom - 1);
  325.  
  326.     for (nodePtr=gObjList; nodePtr; nodePtr=nodePtr->next)
  327.     {
  328.         rectPtr = &( (RectObj *) nodePtr)->rect;
  329.         
  330.         switch(nodePtr->type)
  331.         {
  332.             case OTYPE_Rect:
  333.                 SetAPen( rp, ( (RectObj *) nodePtr)->color );
  334.                 Move(rp, rectPtr->minX, rectPtr->minY);
  335.                 Draw(rp, rectPtr->maxX, rectPtr->minY);
  336.                 Draw(rp, rectPtr->maxX, rectPtr->maxY);
  337.                 Draw(rp, rectPtr->minX, rectPtr->maxY);
  338.                 Draw(rp, rectPtr->minX, rectPtr->minY);
  339.                 break;
  340.             case OTYPE_IText:
  341.                 PrintIText(rp, &( (ITextObj *) nodePtr)->iText, rectPtr->minX, rectPtr->minY);
  342.                 break;
  343.             case OTYPE_Button:
  344. //                RefreshGList( ((ButtonObj *) nodePtr)->gadget, gWindPtr, NULL, 1);
  345.                 break;
  346.             case OTYPE_String:
  347. //                RefreshGList( ((StringObj *) nodePtr)->gadget, gWindPtr, NULL, 1);
  348.                 break;
  349.     
  350.         }
  351.     }
  352.     
  353.     SetAPen(rp, blackPen);
  354.     
  355.     if (!gGadgetsOn)
  356.     {
  357.         AddGList(gWindPtr, gGadgetContext, -1L, -1L, 0L);
  358.         gGadgetsOn = TRUE;
  359.     }
  360.         
  361.     RefreshGList(gGadgetContext, gWindPtr, NULL, -1);
  362.     GT_RefreshWindow(gWindPtr, NULL);
  363.     
  364.     if (gEditMode)
  365.     {
  366.         RemoveGList(gWindPtr, gGadgetContext, -1L);
  367.         gGadgetsOn = FALSE;
  368.     }
  369.  
  370.     if (gSelObj)
  371.     {
  372.         SelectObj(gSelObj);
  373.         UpdateInfoItem(gSelObj);
  374.         UpdateInfoWindow(&gSelObj->rect);
  375.     }
  376.     else
  377.     {
  378.         UpdateInfoItem(0L);
  379.         UpdateInfoWindow(0L);
  380.     }
  381. }
  382.  
  383. /* ===================================================================================== */
  384.  
  385. void DoMenu(USHORT code)
  386. {
  387.     struct MenuItem    *itemPtr;
  388.     void                    (*fPtr)(USHORT);
  389.     USHORT                itemNum;
  390.  
  391.     while (code != MENUNULL)
  392.     {
  393.         itemPtr = ItemAddress(gMenuPtr, code);
  394.         fPtr = MENU_USERDATA(itemPtr);
  395.         if (fPtr)
  396.         {
  397.             itemNum = ITEMNUM(code);
  398.             fPtr(itemNum);
  399.         }
  400.         code = itemPtr->NextSelect;
  401.     }
  402. }
  403.  
  404. void About(USHORT code)
  405. {
  406.     printf("Maker version 0.1 (7/9/91)\n by John Champion\n\n");
  407. }
  408.  
  409. /* ===================================================================================== */
  410.  
  411. void Quit(USHORT code)
  412. {
  413.     quit("Quit by User\n");
  414. }
  415.  
  416. /* ===================================================================================== */
  417.  
  418. void HandleDoubleClick(LinkNode *objPtr)
  419. {
  420.     if (GetGadgetData( objPtr, 0L, 0L, 0L, 0L, 0L ) || objPtr->type == OTYPE_IText )
  421.         DoubleIText(objPtr);
  422.     else
  423.         printf("got doubleclick!\n");
  424. }
  425.  
  426. /* ===================================================================================== */
  427.  
  428. void DoWindSize(USHORT code)
  429. {
  430.     USHORT    left = gWindPtr->LeftEdge,
  431.                 top = gWindPtr->TopEdge,
  432.                 width = gWindPtr->Width,
  433.                 height = gWindPtr->Height;
  434.     
  435.     gWindSizeable = ! gWindSizeable;
  436.     
  437.     NewWindow(left, top, width, height, gWindSizeable);
  438.     
  439.     ReDraw();
  440. }
  441.  
  442. BOOL NewWindow(USHORT left, USHORT top, USHORT width, USHORT height, BOOL sizeable)
  443. {
  444.     if (gWindPtr)
  445.     {
  446.         if (gGadgetsOn)
  447.         {
  448.             RemoveGList(gWindPtr, gGadgetContext, -1L);        // detach the gadget list
  449.             gGadgetsOn = FALSE;
  450.         }
  451.  
  452.         ClearMenuStrip(gWindPtr);
  453.         CloseWindow(gWindPtr);
  454.     }
  455.     
  456.     gWindPtr = 
  457.             OpenWindowTags(0L,
  458.                                 WA_Left,        left,
  459.                                 WA_Top,        top,
  460.                                 WA_Width,    width,
  461.                                 WA_Height,    height,
  462.                                 WA_IDCMP,    WIND_IDCMP,
  463.                                 WA_Flags,    WIND_FLAG | (gWindSizeable ? WFLG_SIZEGADGET : 0),
  464.                                 WA_Title,    gWindTitle,
  465.                                 WA_PubScreen, gPubScreen,
  466.                                 WA_MinWidth,    70,
  467.                                 WA_MinHeight,    30,
  468.                                 WA_MaxWidth,    -1L,
  469.                                 WA_MaxHeight,    -1L,
  470.                                 TAG_DONE);
  471.                                 
  472.     if (!gWindPtr)
  473.         quit("failed to open a window\n");
  474.     
  475.     SetClip();
  476.     SetFont(gWindPtr->RPort, gFont);
  477.                                 
  478.     SetMenuStrip(gWindPtr, gMenuPtr);
  479.  
  480.     if (!gEditMode)
  481.     {
  482.         AddGList(gWindPtr, gGadgetContext, -1L, -1L, 0L);        // reattach the gadgets
  483.         gGadgetsOn = TRUE;
  484.     }
  485.     
  486.     return TRUE;
  487. }
  488.  
  489. void SetClip()
  490. {
  491.     struct Rectangle    tempRect;
  492.     
  493.     if (!gClipRegion)
  494.         gClipRegion = NewRegion();
  495.  
  496.     if (!gClipRegion)
  497.     {
  498.         printf("failed to allocate a region!\n");
  499.         return;
  500.     }
  501.  
  502.     ClearRegion(gClipRegion);
  503.  
  504.     tempRect.MinX = gWindPtr->BorderLeft;
  505.     tempRect.MinY = gWindPtr->BorderTop;
  506.     tempRect.MaxX = gWindPtr->Width - gWindPtr->BorderRight - 1;
  507.     tempRect.MaxY = gWindPtr->Height - gWindPtr->BorderBottom - 1;
  508.  
  509.     OrRectRegion(gClipRegion, &tempRect);
  510.  
  511.     InstallClipRegion(gWindPtr->WLayer, gClipRegion);
  512. }
  513.  
  514. void DoEditMode(USHORT code)
  515. {
  516.     gEditMode = !gEditMode;
  517.     
  518.     if (!gEditMode)
  519.         gSelObj = 0L;
  520.     
  521.     ReDraw();
  522. }
  523.  
  524. void UpdateInfoItem(LinkNode *nodePtr)
  525. {
  526.     RastPort        *rp = gInfoWindPtr->RPort;
  527.     char            buf[100];
  528.     short            itemNum;
  529.     
  530.     if (nodePtr)
  531.         itemNum = NodeNum( &gObjList, nodePtr );
  532.     else
  533.         itemNum = -1;
  534.         
  535.     if (itemNum >= 0)
  536.     {
  537.         sprintf(buf, "  Item: %3d", itemNum);
  538.         Move(rp, 10, 20);
  539.         Text(rp, buf, 11);
  540.     }
  541.     else
  542.     {
  543.         sprintf(buf, "  Item:    ");
  544.         Move(rp, 10, 20);
  545.         Text(rp, buf, 11);
  546.     }
  547. }
  548.     
  549. void UpdateInfoWindow(Rect *rectPtr)
  550. {
  551.     RastPort        *rp = gInfoWindPtr->RPort;
  552.     char            buf[100];
  553.     
  554.     if (rectPtr)
  555.     {
  556.         sprintf(buf, "  Left: %3d", rectPtr->minX);
  557.         Move(rp, 10, 30);
  558.         Text(rp, buf, 11);
  559.         
  560.         sprintf(buf, "   Top: %3d", rectPtr->minY);
  561.         Move(rp, 10, 40);
  562.         Text(rp, buf, 11);
  563.         
  564.         sprintf(buf, " Width: %3d", RectWidth(*rectPtr));
  565.         Move(rp, 10, 50);
  566.         Text(rp, buf, 11);
  567.         
  568.         sprintf(buf, "Height: %3d", RectHeight(*rectPtr));
  569.         Move(rp, 10, 60);
  570.         Text(rp, buf, 11);
  571.     }
  572.     else
  573.     {
  574.         sprintf(buf, "  Left:    ");
  575.         Move(rp, 10, 30);
  576.         Text(rp, buf, 11);
  577.         
  578.         sprintf(buf, "   Top:    ");
  579.         Move(rp, 10, 40);
  580.         Text(rp, buf, 11);
  581.         
  582.         sprintf(buf, " Width:    ");
  583.         Move(rp, 10, 50);
  584.         Text(rp, buf, 11);
  585.         
  586.         sprintf(buf, "Height:    ");
  587.         Move(rp, 10, 60);
  588.         Text(rp, buf, 11);
  589.     }
  590. }