home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1997 #3 / amigamamagazinepolishissue03-1 / ma_1995 / 10 / ami031a.txt < prev    next >
Text File  |  1997-04-07  |  6KB  |  437 lines

  1. /* Listing 9 */
  2.  
  3. #include <graphics/gfxbase.h>
  4.  
  5. #include <intuition/intuition.h>
  6.  
  7.  
  8.  
  9. #include <proto/exec.h>
  10.  
  11. #include <proto/graphics.h>
  12.  
  13. #include <proto/intuition.h>
  14.  
  15.  
  16.  
  17. #include <ctype.h>
  18.  
  19. #include <stdio.h>
  20.  
  21. #include <string.h>
  22.  
  23.  
  24.  
  25. struct operacja
  26.  
  27. {
  28.  
  29.     char *name;
  30.  
  31.     char key;
  32.  
  33.     void (*fun)(struct IntuiMessage*, struct Window*);
  34.  
  35. };
  36.  
  37.  
  38.  
  39. char *names[]=
  40.  
  41. {
  42.  
  43.     "JAM1", "JAM2", "COMPLEMENT", "JAM2 | COMPLEMENT", "INVERSID", "JAM2 | INVERSID", "COMPLEMENT | INVERSID", "JAM2 | COMPLEMENT | INVERSID"
  44.  
  45. };
  46.  
  47.  
  48.  
  49. struct GfxBase *GfxBase;
  50.  
  51. struct IntuitionBase *IntuitionBase;
  52.  
  53.  
  54.  
  55. ULONG dajKolorA(struct RastPort*);
  56.  
  57. ULONG dajKolorB(struct RastPort*);
  58.  
  59.  
  60.  
  61. void linia(struct IntuiMessage*, struct Window*);
  62.  
  63. void punkt(struct IntuiMessage*, struct Window*);
  64.  
  65. void napis(struct IntuiMessage*, struct Window*);
  66.  
  67. void drmd(struct IntuiMessage*, struct Window*);
  68.  
  69. void color(struct IntuiMessage*, struct Window*);
  70.  
  71. void kwadrat(struct IntuiMessage*, struct Window*);
  72.  
  73. void elipsa(struct IntuiMessage*, struct Window*);
  74.  
  75.  
  76.  
  77. struct operacja op_tab[]=
  78.  
  79. {
  80.  
  81.     "Punkt - P", 'P', &punkt,
  82.  
  83.     "Color - C", 'C', &color,
  84.  
  85.     "Elipsa - E", 'E', &elipsa,
  86.  
  87.     "Kwadrat - K", 'K', &kwadrat,
  88.  
  89.     "Linia - L", 'L', &linia,
  90.  
  91.     "DrawMode - M", 'M', &drmd,
  92.  
  93.     "Napis - N",'N', &napis
  94.  
  95. };
  96.  
  97.  
  98.  
  99. int main(int argc, char *argv[])
  100.  
  101. {
  102.  
  103.     check_os(OS_20);
  104.  
  105.     if (IntuitionBase=(struct IntuitionBase*)OpenLibrary("intuition.library", 37))
  106.  
  107.     {
  108.  
  109.         if (GfxBase=(struct GfxBase*)OpenLibrary("graphics.library", 37))
  110.  
  111.         {
  112.  
  113.             struct Window *window;
  114.  
  115.             short op_pos=0;
  116.  
  117.             if (window=OpenWindowTags(0,
  118.  
  119.                     WA_Left, 10,
  120.  
  121.                     WA_Top, 10,
  122.  
  123.                     WA_Width, 610,
  124.  
  125.                     WA_Height, 220,
  126.  
  127.                     WA_Flags, WFLG_DRAGBAR | WFLG_CLOSEGADGET | WFLG_DEPTHGADGET | WFLG_ACTIVATE | WFLG_RMBTRAP | WFLG_REPORTMOUSE,
  128.  
  129.                     WA_IDCMP, IDCMP_CLOSEWINDOW  | IDCMP_MOUSEBUTTONS | IDCMP_MOUSEMOVE | IDCMP_VANILLAKEY,
  130.  
  131.                     WA_ScreenTitle, "Double-click na gadûecie zamykania zamyka okno",
  132.  
  133.                     WA_Title, op_tab[op_pos].name,
  134.  
  135.                     TAG_END))
  136.  
  137.             {
  138.  
  139.                 ULONG last_sec=0, last_micro=0;
  140.  
  141.                 char koniec;
  142.  
  143.                 for (koniec=0; !koniec;)
  144.  
  145.                 {
  146.  
  147.                     struct IntuiMessage *msg_ptr;
  148.  
  149.                     WaitPort(window->UserPort);
  150.  
  151.                     if (msg_ptr=(struct IntuiMessage*)GetMsg(window->UserPort))
  152.  
  153.                     {
  154.  
  155.                         struct IntuiMessage msg;
  156.  
  157.                         /* Wykonanie kopii */
  158.  
  159. #if __STDC__
  160.  
  161.                         msg=*msg_ptr; /* ANSI C */
  162.  
  163. #else
  164.  
  165.                         CopyMem(msg_ptr, &msg, sizeof(struct IntuiMessage));
  166.  
  167. #endif
  168.  
  169.                         ReplyMsg((struct Message*)msg_ptr);
  170.  
  171.                         switch (msg.Class)
  172.  
  173.                         {
  174.  
  175.                             case IDCMP_VANILLAKEY:
  176.  
  177.                                 if (msg.Code==27) /* Kod znaku Esc */
  178.  
  179.                                     koniec=1;
  180.  
  181.                                 else
  182.  
  183.                                 {
  184.  
  185.                                     short pom;
  186.  
  187.                                     for (pom=0; pom<sizeof op_tab/sizeof op_tab[0]; pom++)
  188.  
  189.                                         if (toupper(msg.Code)==op_tab[pom].key)
  190.  
  191.                                         {
  192.  
  193.                                             op_pos=pom;
  194.  
  195.                                             SetWindowTitles(window, op_tab[op_pos].name, (STRPTR)-1);
  196.  
  197.                                             break;
  198.  
  199.                                         }
  200.  
  201.                                 }
  202.  
  203.                                 break;
  204.  
  205.  
  206.  
  207.                             case IDCMP_CLOSEWINDOW:
  208.  
  209.                                 if (DoubleClick(last_sec, last_micro, msg.Seconds, msg.Micros))
  210.  
  211.                                 {
  212.  
  213.                                     koniec=1;
  214.  
  215.                                     break;
  216.  
  217.                                 }
  218.  
  219.                                 last_sec=msg.Seconds;
  220.  
  221.                                 last_micro=msg.Micros;
  222.  
  223.  
  224.  
  225.                                 if (++op_pos==sizeof op_tab/sizeof op_tab[0])
  226.  
  227.                                     op_pos=0;
  228.  
  229.                                 SetWindowTitles(window, op_tab[op_pos].name, (STRPTR)-1);
  230.  
  231.                                 break;
  232.  
  233.  
  234.  
  235.                             case IDCMP_MOUSEBUTTONS:
  236.  
  237.                                 op_tab[op_pos].fun(&msg, window);
  238.  
  239.                                 break;
  240.  
  241.                             case IDCMP_MOUSEMOVE:
  242.  
  243.                                 /* Miejsce na rozszerzenia czytelnika. */
  244.  
  245.                                 break;
  246.  
  247.  
  248.  
  249.                         }
  250.  
  251.                     }
  252.  
  253.                 }
  254.  
  255.                 CloseWindow(window);
  256.  
  257.             }
  258.  
  259.             CloseLibrary((struct Library*)GfxBase);
  260.  
  261.         }
  262.  
  263.         CloseLibrary((struct Library*)IntuitionBase);
  264.  
  265.     }
  266.  
  267.     return 0;
  268.  
  269. }
  270.  
  271.  
  272.  
  273. void linia(struct IntuiMessage *msg_ptr, struct Window *win)
  274.  
  275. {
  276.  
  277.     if (msg_ptr->Code == SELECTDOWN)
  278.  
  279.         Draw(win->RPort, msg_ptr->MouseX, msg_ptr->MouseY);
  280.  
  281. }
  282.  
  283.  
  284.  
  285. void punkt(struct IntuiMessage *msg_ptr, struct Window *win)
  286.  
  287. {
  288.  
  289.     if (msg_ptr->Code == SELECTDOWN)
  290.  
  291.     {
  292.  
  293.         WritePixel(win->RPort, msg_ptr->MouseX, msg_ptr->MouseY);
  294.  
  295.         Move(win->RPort, msg_ptr->MouseX, msg_ptr->MouseY);
  296.  
  297.     }
  298.  
  299. }
  300.  
  301.  
  302.  
  303. void napis(struct IntuiMessage *msg_ptr, struct Window *win)
  304.  
  305. {
  306.  
  307.     char *tekst = "Przykîadowy napis";
  308.  
  309.  
  310.  
  311.     if (msg_ptr->Code ==    SELECTDOWN)
  312.  
  313.     {
  314.  
  315.         Move(win->RPort, msg_ptr->MouseX, msg_ptr->MouseY);
  316.  
  317.         Text(win->RPort, tekst, strlen(tekst));
  318.  
  319.     }
  320.  
  321. }
  322.  
  323.  
  324.  
  325. void drmd(struct IntuiMessage *msg_ptr, struct Window *win)
  326.  
  327. {
  328.  
  329.     if (msg_ptr->Code == SELECTDOWN)
  330.  
  331.     {
  332.  
  333.         char pom;
  334.  
  335.         SetDrMd(win->RPort, pom = 7 & 1+(GfxBase->LibNode.lib_Version<39 ? win->RPort->DrawMode : GetDrMd(win->RPort)));
  336.  
  337.         SetWindowTitles(win, names[pom], (STRPTR)-1);
  338.  
  339.     }
  340.  
  341. }
  342.  
  343.  
  344.  
  345. void color(struct IntuiMessage *msg_ptr, struct Window *win)
  346.  
  347. {
  348.  
  349.     switch (msg_ptr->Code)
  350.  
  351.     {
  352.  
  353.         case SELECTDOWN:
  354.  
  355.             SetAPen(win->RPort, dajKolorA(win->RPort)+1);
  356.  
  357.             break;
  358.  
  359.         case MENUDOWN:
  360.  
  361.             SetBPen(win->RPort, dajKolorB(win->RPort)+1);
  362.  
  363.             break;
  364.  
  365.     }
  366.  
  367. }
  368.  
  369.  
  370.  
  371. void kwadrat(struct IntuiMessage *msg_ptr, struct Window *win)
  372.  
  373. {
  374.  
  375.     short xMin, xMax, yMin, yMax;
  376.  
  377.     if (msg_ptr->Code == SELECTDOWN)
  378.  
  379.     {
  380.  
  381.         xMin=(win->RPort->cp_x<msg_ptr->MouseX ? win->RPort->cp_x : msg_ptr->MouseX);
  382.  
  383.         xMax=(win->RPort->cp_x>msg_ptr->MouseX ? win->RPort->cp_x : msg_ptr->MouseX);
  384.  
  385.         yMin=(win->RPort->cp_y<msg_ptr->MouseY ? win->RPort->cp_y : msg_ptr->MouseY);
  386.  
  387.         yMax=(win->RPort->cp_y>msg_ptr->MouseY ? win->RPort->cp_y : msg_ptr->MouseY);
  388.  
  389.         RectFill(win->RPort, xMin, yMin, xMax, yMax);
  390.  
  391.     }
  392.  
  393. }
  394.  
  395.  
  396.  
  397. void elipsa(struct IntuiMessage *msg_ptr, struct Window *win)
  398.  
  399. {
  400.  
  401.     short a, b;
  402.  
  403.     if (msg_ptr->Code == SELECTDOWN)
  404.  
  405.     {
  406.  
  407.         a=((a=win->RPort->cp_x-msg_ptr->MouseX)>0 ? a : -a);
  408.  
  409.         b=((b=win->RPort->cp_y-msg_ptr->MouseY)>0 ? b : -b);
  410.  
  411.         DrawEllipse(win->RPort, win->RPort->cp_x, win->RPort->cp_y, a, b);
  412.  
  413.     }
  414.  
  415. }
  416.  
  417.  
  418.  
  419. ULONG dajKolorA(struct RastPort *rp)
  420.  
  421. {
  422.  
  423.     return (GfxBase->LibNode.lib_Version<39 ? rp->FgPen : GetAPen(rp));
  424.  
  425. }
  426.  
  427.  
  428.  
  429. ULONG dajKolorB(struct RastPort *rp)
  430.  
  431. {
  432.  
  433.     return (GfxBase->LibNode.lib_Version<39 ? rp->BgPen : GetBPen(rp));
  434.  
  435. }
  436.  
  437.