home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / dev / gui / guicreator / demos / calculator_functions.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-04  |  12.3 KB  |  312 lines

  1. /*************************************************************************/
  2. /*                                                                       */
  3. /*   Includes                                                            */
  4. /*                                                                       */
  5. /*************************************************************************/
  6.  
  7. #include "Calculator_Includes.h"
  8. #include "Calculator_Structures.h"
  9.  
  10. /*************************************************************************/
  11. /*                                                                       */
  12. /*   Variables and Structures                                            */
  13. /*                                                                       */
  14. /*************************************************************************/
  15.  
  16. extern struct IntuitionBase  *IntuitionBase;
  17. extern struct GfxBase        *GfxBase;
  18.  
  19. extern struct Library *GadToolsBase;
  20. extern struct Library *AslBase;
  21. extern struct Library *DataTypesBase;
  22.  
  23. /*************************************************************************/
  24. /*                                                                       */
  25. /*   Defines                                                             */
  26. /*                                                                       */
  27. /*************************************************************************/
  28.  
  29. #define RASTERX (GfxBase->DefaultFont->tf_XSize)
  30. #define RASTERY (GfxBase->DefaultFont->tf_YSize)
  31.  
  32. #define XSIZE(x)  ((x)*RASTERX)
  33. #define YSIZE(x)  ((x)*RASTERY)
  34.  
  35. #define XPOS(x)   (XSIZE(x)+customscreen->WBorLeft)
  36. #define YPOS(x)   (YSIZE(x)+customscreen->BarHeight+1)
  37.  
  38. /*************************************************************************/
  39. /*                                                                       */
  40. /*   SleepWindow() und WakenWindow()                                     */
  41. /*                                                                       */
  42. /*************************************************************************/
  43.  
  44. static struct Requester waitrequest;
  45.  
  46. void SleepWindow(struct Window *win)
  47. {
  48.     InitRequester(&waitrequest);
  49.     Request(&waitrequest,win);
  50.     SetWindowPointer(win,WA_BusyPointer,1L,TAG_DONE);
  51. }
  52.  
  53. void WakenWindow(struct Window *win)
  54. {
  55.     EndRequest(&waitrequest,win);
  56.     SetWindowPointer(win,WA_Pointer,0L,TAG_DONE);
  57. }
  58.  
  59. /*************************************************************************/
  60. /*                                                                       */
  61. /*   GUICreatorErrorReport()                                             */
  62. /*                                                                       */
  63. /*************************************************************************/
  64.  
  65. void GUICreatorErrorReport(struct Window *win,ULONG type)
  66. {
  67.     char error[256];
  68.     struct EasyStruct easystruct = { sizeof(struct EasyStruct),0,"Caution:",error,"OK" };
  69.  
  70.     if (type == ERROR_NO_WINDOW_OPENED)
  71.         strcpy(error,"Could not open window (no memory?)");
  72.     else if (type == ERROR_NO_PUBSCREEN_LOCKED)
  73.         strcpy(error,"Could not lock pubscreen");
  74.     else if (type == ERROR_NO_GADGETS_CREATED)
  75.         strcpy(error,"Could not create gadgets");
  76.     else if (type == ERROR_NO_GADGETLIST_CREATED)
  77.         strcpy(error,"Could not create gadgetlist");
  78.     else if (type == ERROR_NO_VISUALINFO)
  79.         strcpy(error,"Could not read visualinfo from screen");
  80.     else if (type == ERROR_NO_PICTURE_LOADED)
  81.         strcpy(error,"Could not read picture data");
  82.     else if (type == ERROR_NO_WINDOW_MENU)
  83.         strcpy(error,"Could not create menu");
  84.     else if (type == ERROR_SCREEN_TOO_SMALL)
  85.         strcpy(error,"This screen is too small for the window");
  86.     else Fault(type,"Error",error,sizeof(error));
  87.  
  88.     if (win && !win->FirstRequest)
  89.         {
  90.         SleepWindow(win);
  91.         EasyRequestArgs(win,&easystruct,NULL,NULL);
  92.         WakenWindow(win);
  93.         }
  94.     else EasyRequestArgs(win,&easystruct,NULL,NULL);
  95.  
  96. }
  97.  
  98. /*************************************************************************/
  99. /*                                                                       */
  100. /*   CreateBevelFrames()                                                 */
  101. /*                                                                       */
  102. /*************************************************************************/
  103.  
  104. void CreateBevelFrames(struct Window *win,APTR visualinfo,ULONG bevelcount,struct BevelFrame bevels[])
  105. {
  106.     ULONG i;
  107.     for (i=0;i<bevelcount;i++)
  108.         {
  109.         DrawBevelBox(win->RPort,bevels[i].bb_LeftEdge,bevels[i].bb_TopEdge,bevels[i].bb_Width,bevels[i].bb_Height,GT_VisualInfo,visualinfo,GTBB_Recessed,TRUE,TAG_END);
  110.         DrawBevelBox(win->RPort,bevels[i].bb_LeftEdge+2,bevels[i].bb_TopEdge+1,bevels[i].bb_Width-4,bevels[i].bb_Height-2,GT_VisualInfo,visualinfo,TAG_END);
  111.         if (bevels[i].bb_Title)
  112.             {
  113.             char title[64];
  114.             sprintf(title," %s ",bevels[i].bb_Title);
  115.             Move(win->RPort,bevels[i].bb_LeftEdge+(bevels[i].bb_Width-XSIZE(strlen(title)))/2,bevels[i].bb_TopEdge+2);
  116.             SetAPen(win->RPort,bevels[i].bb_Color);
  117.             Text(win->RPort,title,strlen(title));
  118.             }
  119.         /* end-if */
  120.         }
  121.     /* end-for */
  122. }
  123.  
  124. /*************************************************************************/
  125. /*                                                                       */
  126. /*   CreateLines()                                                       */
  127. /*                                                                       */
  128. /*************************************************************************/
  129.  
  130. void CreateLines(struct Window *win,int linecount,struct Line lines[])
  131. {
  132.     ULONG i;
  133.     for (i=0;i<linecount;i++)
  134.         {
  135.         SetAPen(win->RPort,lines[i].li_Color);
  136.         Move(win->RPort,lines[i].li_LeftEdge,lines[i].li_TopEdge);
  137.         Draw(win->RPort,lines[i].li_Width>0?lines[i].li_LeftEdge+lines[i].li_Width-1:lines[i].li_LeftEdge+lines[i].li_Width,lines[i].li_Height>0?lines[i].li_TopEdge+lines[i].li_Height-1:lines[i].li_TopEdge+lines[i].li_Height);
  138.         }
  139.     /* end-for */
  140. }
  141.  
  142. /*************************************************************************/
  143. /*                                                                       */
  144. /*   CreateTexts()                                                       */
  145. /*                                                                       */
  146. /*************************************************************************/
  147.  
  148. void CreateTexts(struct Window *win,int textcount,struct Text texts[], long double xscale,long double yscale)
  149. {
  150.     ULONG i;
  151.     for (i=0;i<textcount;i++)
  152.         {
  153.         SetAPen(win->RPort,texts[i].tx_Color);
  154.         Move(win->RPort,texts[i].tx_LeftEdge,texts[i].tx_TopEdge+(ULONG)(yscale*GfxBase->DefaultFont->tf_Baseline));
  155.         Text(win->RPort,texts[i].tx_Text,strlen(texts[i].tx_Text));
  156.         }
  157.     /* end-for */
  158. }
  159.  
  160. /*************************************************************************/
  161. /*                                                                       */
  162. /*   About()                                                             */
  163. /*                                                                       */
  164. /*************************************************************************/
  165.  
  166. void About(struct Window *hostwin)
  167. {
  168.     APTR visualinfo=GetVisualInfo(hostwin->WScreen,TAG_DONE);
  169.     if (visualinfo)
  170.         {
  171.         Object *o=NewDTObject("/About.IFF",DTA_SourceType,DTST_FILE,DTA_GroupID,GID_PICTURE,TAG_DONE);
  172.         if (o)
  173.             {
  174.             struct Gadget  *gadgetlist   = NULL;
  175.             struct Screen  *customscreen = hostwin->WScreen;
  176.             struct Gadget  *gadget       = CreateContext(&gadgetlist);
  177.             if (gadget)
  178.                 {
  179.                 ULONG height=25,width=34,left=0,top=0;
  180.                 ULONG textcount=18;
  181.  
  182.                 struct Gadget *wingad;
  183.  
  184.                 STRPTR title                  = "About";
  185.                 struct Window     *win        = NULL;
  186.  
  187.                 struct TextAttr   textattr    = { GfxBase->DefaultFont->tf_Message.mn_Node.ln_Name,GfxBase->DefaultFont->tf_YSize,FS_NORMAL,FPF_DISKFONT };
  188.  
  189.                 struct NewGadget  newgad      = { XPOS(1),YPOS(22),XSIZE(32)+200,YSIZE(2),"_OK",&textattr,2,PLACETEXT_IN,visualinfo,NULL };
  190.  
  191.                 struct Text       texts[]     = {
  192.                                                              200+XPOS(4),YPOS(2),"****************************",1,
  193.                                                              200+XPOS(4),YPOS(3),"*                          *",1,
  194.                                                              200+XPOS(4),YPOS(4),"*  This  GUI was designed  *",1,
  195.                                                              200+XPOS(4),YPOS(5),"*                          *",1,
  196.                                                              200+XPOS(4),YPOS(6),"*  with GUI-Creator V 1.0  *",1,
  197.                                                              200+XPOS(4),YPOS(7),"*                          *",1,
  198.                                                              200+XPOS(4),YPOS(8),"*  © 1995 by               *",1,
  199.                                                              200+XPOS(4),YPOS(9),"*  Markus Hillenbrand      *",1,
  200.                                                              200+XPOS(4),YPOS(10),"*                          *",1,
  201.                                                              200+XPOS(4),YPOS(11),"* ------------------------ *",1,
  202.                                                              200+XPOS(4),YPOS(12),"*                          *",1,
  203.                                                              200+XPOS(4),YPOS(13),"* GUI-Creator is Shareware *",1,
  204.                                                              200+XPOS(4),YPOS(14),"*                          *",1,
  205.                                                              200+XPOS(4),YPOS(15),"* Please read the docs for *",1,
  206.                                                              200+XPOS(4),YPOS(16),"*                          *",1,
  207.                                                              200+XPOS(4),YPOS(17),"* more information!        *",1,
  208.                                                              200+XPOS(4),YPOS(18),"*                          *",1,
  209.                                                              200+XPOS(4),YPOS(19),"****************************",1,
  210.                                                              };
  211.  
  212.                 height= YSIZE(height);
  213.                 width = XSIZE(width)+200;
  214.                 left  = (customscreen->Width-width)/2;
  215.                 top   = (customscreen->Height-height)/2;
  216.  
  217.                 wingad = gadget = CreateGadget(BUTTON_KIND,gadget,&newgad,GT_Underscore,'_',TAG_END);
  218.  
  219.                 if (height>customscreen->Height || width>customscreen->Width) GUICreatorErrorReport(hostwin,ERROR_SCREEN_TOO_SMALL);
  220.                 win=OpenWindowTags(NULL,WA_Activate,         TRUE,
  221.                                                 WA_CloseGadget,      TRUE,
  222.                                                 WA_DepthGadget,      TRUE,
  223.                                                 WA_SizeGadget,       FALSE,
  224.                                                 WA_DragBar,          TRUE,
  225.                                                 WA_Gadgets,          gadgetlist,
  226.                                                 WA_InnerHeight,      height,
  227.                                                 WA_InnerWidth,       width,
  228.                                                 WA_IDCMP,            IDCMP_CLOSEWINDOW|BUTTONIDCMP|IDCMPUPDATE|IDCMP_VANILLAKEY,
  229.                                                 WA_Left,             left,
  230.                                                 WA_Top,              top,
  231.                                                 WA_SmartRefresh,     TRUE,
  232.                                                 WA_Title,            title,
  233.                                                 WA_CustomScreen,     customscreen,
  234.                                                 TAG_END);
  235.                 if (win)
  236.                     {
  237.                     struct IntuiMessage  *imessage   = NULL;
  238.                     struct Gadget        *idcmpgad   = NULL;
  239.                     struct BitMap        *bmcopy     = NULL;
  240.                     struct TagItem       *tag        = NULL;
  241.                     struct TagItem       *tstate     = NULL;
  242.                     ULONG  idcmpclass                = 0;
  243.                     UWORD  messagecode               = 0;
  244.                     BOOL   running                   = TRUE;
  245.  
  246.                     SleepWindow(hostwin);
  247.                     SetFont(win->RPort,GfxBase->DefaultFont);
  248.  
  249.                     DrawBevelBox(win->RPort,XPOS(1),YPOS(1),204,102,GT_VisualInfo,visualinfo,GTBB_Recessed,TRUE,TAG_END);
  250.                     DrawBevelBox(win->RPort,XPOS(3)+200,YPOS(1),XSIZE(30),YSIZE(20),GT_VisualInfo,visualinfo,GTBB_Recessed,TRUE,TAG_END);
  251.  
  252.                     SetDTAttrs(o,NULL,NULL,GA_Left,XPOS(1)+2,GA_Top,YPOS(1)+1,GA_Width,200,GA_Height,100,PDTA_Remap,TRUE,PDTA_DestBitMap,&bmcopy,ICA_TARGET,ICTARGET_IDCMP,TAG_DONE);
  253.                     AddDTObject(win,NULL,o,-1L);
  254.  
  255.                     CreateTexts(win,textcount,texts,1.0,1.0);
  256.  
  257.                     GT_RefreshWindow(win,NULL);
  258.  
  259.                     while (running)
  260.                         {
  261.                         Wait(1L << win->UserPort->mp_SigBit);
  262.                         while (imessage=GT_GetIMsg(win->UserPort))
  263.                             {
  264.                             idcmpgad=(struct Gadget *)imessage->IAddress;
  265.                             idcmpclass=imessage->Class;
  266.                             messagecode =imessage->Code;
  267.  
  268.                             GT_ReplyIMsg(imessage);
  269.  
  270.                             switch(idcmpclass)
  271.                                 {
  272.                                 case IDCMP_VANILLAKEY:
  273.                                     if (messagecode == 27 || messagecode == 'o' || messagecode == 'O') running=FALSE;
  274.                                     break;
  275.                                 case IDCMP_REFRESHWINDOW:
  276.                                     GT_BeginRefresh(win);
  277.                                     GT_EndRefresh(win,TRUE);
  278.                                     break;
  279.                                 case IDCMP_CLOSEWINDOW:
  280.                                     running=FALSE;
  281.                                     break;
  282.                                 case BUTTONIDCMP:
  283.                                     running=FALSE;
  284.                                     break;
  285.                                 case IDCMP_IDCMPUPDATE:
  286.                                     tstate=(struct TagItem*)imessage->IAddress;
  287.                                     while (tag=NextTagItem(&tstate)) if (tag->ti_Tag == DTA_Sync) RefreshDTObjectA(o,win,NULL,NULL);
  288.                                     break;
  289.                                 }
  290.                             /* end-switch */
  291.  
  292.                             }
  293.                         /* end-while */
  294.                         }
  295.                     /* end-while */
  296.  
  297.                     CloseWindow(win);
  298.                     WakenWindow(hostwin);
  299.                     }
  300.                 else GUICreatorErrorReport(hostwin,ERROR_NO_WINDOW_OPENED);
  301.                 FreeGadgets(gadgetlist);
  302.                 }
  303.             else GUICreatorErrorReport(hostwin,ERROR_NO_GADGETLIST_CREATED);
  304.             DisposeDTObject(o);
  305.             }
  306.         else GUICreatorErrorReport(hostwin,ERROR_NO_PICTURE_LOADED);
  307.         FreeVisualInfo(visualinfo);
  308.         }
  309.     else GUICreatorErrorReport(hostwin,ERROR_NO_VISUALINFO);
  310.  
  311. }
  312.