home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 514b.lha / GadgetED_v2.3 / Source / GadgetEd.c < prev    next >
C/C++ Source or Header  |  1991-06-08  |  15KB  |  510 lines

  1. /*----------------------------------------------------------------------*
  2.   GadgetED.c version 2.3 - © Copyright 1990-91 Jaba Development
  3.  
  4.   Autor   : Jan van den Baard
  5.   Purpose : main control program.
  6.  *----------------------------------------------------------------------*/
  7.  
  8. USHORT pointer[] =
  9.  { 0x0000,0x0000,0xffff,0xffff,0xc007,0xffff,0xaffb,0xffff,
  10.    0xa439,0xffff,0xd77d,0xffff,0xf6fd,0xffff,0xf43d,0xffff,
  11.    0xf7fd,0xffff,0xf70d,0xffff,0xf7dd,0xffff,0xd7bd,0xffff,
  12.    0xa709,0xffff,0xaffb,0xffff,0xc003,0xffff,0xffff,0xffff,
  13.    0x0000,0x0000,0x0000,0x0000
  14.  };
  15. USHORT Colors[] =
  16.  { 0x0000,0x0ecb,0x0f00,0x0ff0,0x00f0,0x00c0,0x0090,0x0060,
  17.    0x000f,0x000c,0x0009,0x0ff0,0x0cc0,0x0990,0x0f0f,0x0c0c,
  18.    0x0909,0x0a00,0x0000,0x0f00,0x0444,0x0555,0x0666,0x0777,
  19.    0x0888,0x0999,0x0aaa,0x0bbb,0x0ccc,0x0ddd,0x0fff,0x0f00
  20.  };
  21. USHORT info_data[] =
  22.  { 0xee00,0xc600,0xaa00,0xee00,0xee00,0xee00,0xee00,0xaa00,
  23.    0xc600,0xee00,0xfff0,0xef70,0xdfb0,0x8010,0xdfb0,0xef70,
  24.    0xfff0,0xee00,0xc600,0xaa00,0xee00,0xee00,0xee00,0xee00,
  25.    0xee00,0xee00,0xee00,0xfff0,0xeff0,0xdff0,0x8010,0xdff0,
  26.    0xeff0,0xfff0,0xfff0,0xbdf0,0xdbf0,0xe610,0xeff0,0xde10,
  27.    0xbff0,0xfff0,0xfff8,0xbdf8,0xdbf8,0xe708,0xe7f8,0xdb08,
  28.    0xbdf8,0xfff8
  29.  };
  30.  
  31. struct Image HEIGHT_image =
  32.  { 247,0,7,10,1,NULL,0x01,0x00,NULL
  33.  };
  34. struct Image WIDTH_image  =
  35.  { 197,1,12,7,1,NULL,0x01,0x00,&HEIGHT_image
  36.  };
  37. struct Image TOP_image    =
  38.  { 150,0,7,10,1,NULL,0x01,0x00,&WIDTH_image
  39.  };
  40. struct Image LEFT_image   =
  41.  { 100,1,12,7,1,NULL,0x01,0x00,&TOP_image
  42.  };
  43. struct Image YC_image     =
  44.  { 50,1,12,8,1,NULL,0x01,0x00,&LEFT_image
  45.  };
  46. struct Image XC_image     =
  47.  { 0,1,13,8,1,NULL,0x01,0x00,&YC_image
  48.  };
  49.  
  50. #define DATA_SIZE_TOTAL 100
  51. #define HEIGHT_OFFSET   0
  52. #define WIDTH_OFFSET    10
  53. #define TOP_OFFSET      17
  54. #define LEFT_OFFSET     27
  55. #define YC_OFFSET       34
  56. #define XC_OFFSET       42
  57.  
  58. UBYTE wdt[80]       = "Work Window";
  59. UBYTE wlb[MAXLABEL] = "new_window";
  60.  
  61. struct TextAttr std =
  62.  { (STRPTR)"topaz.font",8,FS_NORMAL,FPF_ROMFONT
  63.  };
  64.  
  65. struct NewScreen ns_main =
  66.  { 0,0,640,STDSCREENHEIGHT,2,0,1,HIRES,CUSTOMSCREEN,&std,TITLE,NULL,NULL
  67.  };
  68. struct NewWindow nw_main =
  69.  { 50,25,175,50,0,1,NEWSIZE|GADGETUP|GADGETDOWN|INACTIVEWINDOW|ACTIVEWINDOW|MENUPICK|RAWKEY|MOUSEBUTTONS|CLOSEWINDOW,
  70.    WINDOWSIZING|WINDOWDRAG|WINDOWCLOSE|WINDOWDEPTH|NOCAREREFRESH|SMART_REFRESH|ACTIVATE,NULL,NULL,(UBYTE *)&wdt,NULL,NULL,150,50,
  71.    0,256,CUSTOMSCREEN
  72.  };
  73.  
  74. char name[512];
  75.  
  76. struct GadgetList     Gadgets;
  77. struct Requester      dw;
  78. struct FileRequester *IODir;
  79. struct MemoryChain    Memory;
  80. struct MemoryChain    Misc;
  81. struct ge_prefs       prefs  = { TRUE,TRUE,TRUE };
  82. struct Window        *sysreq;
  83. struct Window        *MainWindow = NULL;
  84. struct Screen        *MainScreen = NULL;
  85. struct RastPort      *MainRP;
  86. struct Gadget        *Gadget;
  87. struct Gadget        TextGadget = { NULL,0,0,1,1,GADGHNONE,NULL,BOOLGADGET,
  88.                                     NULL,NULL,NULL,NULL,NULL,NULL,NULL };
  89.  
  90. struct IntuitionBase *IntuitionBase;
  91. struct GfxBase       *GfxBase;
  92. struct ToolBase      *ToolBase;
  93.  
  94. BYTE    Xoff = -1, Yoff = 0;
  95. BOOL    Saved = TRUE;
  96. BOOL    REQUESTER = FALSE;
  97. BOOL    WORKBENCH = FALSE;
  98. BOOL    WBSCREEN  = FALSE;
  99. ULONG   WindowFlags = WINDOWSIZING+WINDOWDRAG+WINDOWCLOSE+WINDOWDEPTH+NOCAREREFRESH+SMART_REFRESH+ACTIVATE;
  100. ULONG   IDCMPFlags  = GADGETUP+GADGETDOWN+CLOSEWINDOW;
  101. ULONG   Class;
  102. USHORT  BackFill  = 1, WDBackFill = 0;
  103. USHORT  Code, Qualifier, WBColors[4], LightSide = 2, DarkSide = 1;
  104. USHORT *pd, *data_pointer;
  105. USHORT  FrontPen = 1, BackPen = 0, GadgetCount = 0, id = 0, DEPTH;
  106. SHORT   MainX, MainY;
  107.  
  108. extern struct MenuItem   SubItems[];
  109. extern struct Menu       Titles[];
  110. extern struct WBStartup *WBenchMsg;
  111.  
  112. /*
  113.  * free and close all resources
  114.  */
  115. VOID close_up(msg)
  116.  UBYTE *msg;
  117. {
  118.      struct Process *proc;
  119.  
  120.      FreeGList();
  121.      if(IODir)         FreeFreq(IODir);
  122.                        OpenWorkBench();
  123.      proc = (struct Process *)FindTask(NULL);
  124.      proc->pr_WindowPtr = (APTR)sysreq;
  125.      if(MainWindow)  { ClearMenuStrip(MainWindow); CloseWindow(MainWindow); }
  126.      if(MainScreen)    CloseScreen(MainScreen);
  127.      if(msg)           puts(msg);
  128.                        FreeMemoryChain(&Misc);
  129.      if(ToolBase)      CloseLibrary(ToolBase);
  130.      exit(0);
  131. }
  132.  
  133. /*
  134.  * allocate open and setup all resources
  135.  */
  136. VOID open_libs()
  137. {
  138.      int i;
  139.  
  140.      if(NOT(ToolBase = (struct ToolBase *)
  141.       OpenLibrary("tool.library",TOOL_VERSION)))
  142.         close_up("   - ERROR: Can't find the tool.library V8++!");
  143.  
  144.      GfxBase       = ToolBase->GfxBase;
  145.      IntuitionBase = ToolBase->IntuitionBase;
  146.  
  147.      for(i=0;i<4;i++) WBColors[i] = (USHORT)GetRGB4(IntuitionBase->ActiveScreen->ViewPort.ColorMap,i);
  148.  
  149.      if(NOT(IODir = AllocFreq()))
  150.          close_up("   - ERROR: Out of memory !");
  151.  
  152.      InitMemoryChain(&Misc,1024L);
  153.      InitMemoryChain(&Memory,(5*1024));
  154.  
  155.      if(NOT(pd = (USHORT *)
  156.       AllocItem(&Misc,(POINTERSIZE << 1),MEMF_CHIP+MEMF_CLEAR)))
  157.          close_up("   - ERROR: Out of memory !");
  158.      if(NOT(data_pointer = (USHORT *)
  159.       AllocItem(&Misc,DATA_SIZE_TOTAL,MEMF_CHIP+MEMF_CLEAR)))
  160.          close_up("   ERROR: Out of memory !");
  161.  
  162.      CopyMem((void *)&pointer,(void *)pd,(POINTERSIZE << 1));
  163.      CopyMem((void *)&info_data,(void *)data_pointer,DATA_SIZE_TOTAL);
  164.  
  165.      HEIGHT_image.ImageData = (USHORT *)&data_pointer[HEIGHT_OFFSET];
  166.      WIDTH_image.ImageData  = (USHORT *)&data_pointer[WIDTH_OFFSET];
  167.      TOP_image.ImageData    = (USHORT *)&data_pointer[TOP_OFFSET];
  168.      LEFT_image.ImageData   = (USHORT *)&data_pointer[LEFT_OFFSET];
  169.      YC_image.ImageData     = (USHORT *)&data_pointer[YC_OFFSET];
  170.      XC_image.ImageData     = (USHORT *)&data_pointer[XC_OFFSET];
  171. }
  172.  
  173. /*
  174.  * open up the main display
  175.  */
  176. VOID open_display()
  177. {
  178.      if(NOT MainScreen)
  179.      {   if(NOT(MainScreen = OpenScreen(&ns_main)))
  180.              close_up("   - ERROR: Can't open a work screen !");
  181.      }
  182.      nw_main.Screen    = MainScreen;
  183.      nw_main.MaxHeight = MainScreen->Height;
  184.      if(NOT(MainWindow = OpenWindow(&nw_main)))
  185.          close_up("   - ERROR: Can't open a work window !");
  186.      MainRP = MainWindow->RPort;
  187. }
  188.  
  189. /*
  190.  * disable the edit window by putting up a little requester
  191.  */
  192. VOID disable_window()
  193. {
  194.     InitRequester(&dw);
  195.     dw.LeftEdge = 0;
  196.     dw.TopEdge  = 0;
  197.     dw.Width    = 1;
  198.     dw.Height   = 1;
  199.     dw.BackFill = 1;
  200.     Request(&dw,MainWindow);
  201. }
  202.  
  203. /*
  204.  * enable the edit window (remove the requester)
  205.  */
  206. VOID enable_window()
  207. {
  208.     EndRequest(&dw,MainWindow);
  209. }
  210.  
  211. /*
  212.  * show buisy pointer
  213.  */
  214. VOID buisy()
  215. {
  216.     SetPointer(MainWindow,(void *)pd,16,16,Xoff,Yoff);
  217. }
  218.  
  219. /*
  220.  * erase buisy pointer
  221.  */
  222. VOID ok()
  223. {
  224.     ClearPointer(MainWindow);
  225. }
  226.  
  227. /*
  228.  * change the depth of the edit screen
  229.  */
  230. VOID change_depth(depth)
  231.     ULONG depth;
  232. {
  233.     if(MainWindow)
  234.     {   ClearMenuStrip(MainWindow);
  235.         CloseWindow(MainWindow);
  236.     }
  237.     if(MainScreen)
  238.     {   if(depth != MainScreen->BitMap.Depth)
  239.         {   CloseScreen(MainScreen);
  240.             MainScreen = NULL;
  241.         }
  242.     }
  243.     ns_main.Depth = depth;
  244.  
  245.     if(depth == 5)
  246.     {   ns_main.ViewModes = NULL;
  247.         ns_main.Width     = 320;
  248.         if(NOT nw_main.MaxWidth) nw_main.MaxWidth  = 320;
  249.     }
  250.     else
  251.     {   ns_main.ViewModes = HIRES;
  252.         ns_main.Width     = 640;
  253.         if(NOT nw_main.MaxWidth) nw_main.MaxWidth  = 640;
  254.     }
  255.     if(REQUESTER) nw_main.Flags |= (GIMMEZEROZERO+SIZEBRIGHT+SIZEBBOTTOM);
  256.     open_display();
  257.     if(REQUESTER)
  258.     {   SetDrMd(MainRP,JAM1);
  259.         SetAPen(MainRP,BackFill);
  260.         RectFill(MainRP,0,0,MainWindow->GZZWidth,MainWindow->GZZHeight);
  261.     }
  262.     SetMenu(MainWindow);
  263. }
  264.  
  265. /*
  266.  * load the preferences file (if available)
  267.  */
  268. VOID load_prefs()
  269. {
  270.     BPTR file;
  271.  
  272.     if((file = Open("DEVS:GadgetEd.PREFS",MODE_OLDFILE)))
  273.     {   if(Read(file,(char *)&prefs,sizeof(struct ge_prefs)) <= 0)
  274.         {   Close(file);
  275.             Error("Error reading preferences !");
  276.             return;
  277.         }
  278.         Close(file);
  279.     }
  280. }
  281.  
  282. /*
  283.  * if a gadget has GRELWITH or GRELHEIGHT set and it's
  284.  * a BOOL or STRGADGET gadget with a border this routine
  285.  * sizes the border to fit around the gadget again after
  286.  * a resizing of the window.
  287.  */
  288. VOID grl()
  289. {
  290.     register struct Gadget *g, *g1;
  291.     register SHORT *XY;
  292.  
  293.     if(Gadgets.TailPred == (struct MyGadget *)&Gadgets) return;
  294.  
  295.     g = &Gadgets.TailPred->Gadget;
  296.     disable_window();
  297.     while(1)
  298.     {   g1 = g->NextGadget;
  299.         un_grel(MainWindow,g);
  300.         if((TestBits((ULONG)g->Flags,GRELWIDTH)) AND
  301.            (NOT TestBits((ULONG)g->Flags,GADGIMAGE)))
  302.         {   if(NOT TestBits((ULONG)g->GadgetType,PROPGADGET))
  303.             {   XY = ((struct Border *)g->GadgetRender)->XY;
  304.                 if(g->Width < 9)
  305.                 {   XY[2] = XY[4] = 9;
  306.                     g->Width = 9;
  307.                 }
  308.                 else
  309.                 { XY[2] = XY[4] = g->Width;
  310.                 }
  311.             }
  312.         }
  313.         if((TestBits((ULONG)g->Flags,GRELHEIGHT)) AND
  314.            (NOT TestBits((ULONG)g->Flags,GADGIMAGE)))
  315.         {   if(NOT TestBits((ULONG)g->GadgetType,PROPGADGET))
  316.             {   XY = ((struct Border *)g->GadgetRender)->XY;
  317.                 if(g->Height < 9)
  318.                 {   XY[5] = XY[7] = 9;
  319.                     g->Height = 9;
  320.                 }
  321.                 else
  322.                 { XY[5] = XY[7] = g->Height;
  323.                 }
  324.             }
  325.         }
  326.         grel(MainWindow,g);
  327.         if(NOT g1) break;
  328.         g = g1;
  329.     }
  330.     enable_window();
  331. }
  332.  
  333. /*
  334.  * set screen title. This does not use SetWindowTitles() because if
  335.  * the edit window overlaps the screen title the message must still
  336.  * be displayed.
  337.  */
  338. VOID sst(title)
  339.     char *title;
  340. {
  341.     struct RastPort *rp;
  342.  
  343.     rp = &MainScreen->RastPort;
  344.     SetAPen(rp,1);
  345.     RectFill(rp,0,0,MainScreen->Width,9); /* clear the title bar */
  346.     if(title)
  347.     {   SetAPen(rp,0);
  348.         SetBPen(rp,1);
  349.         SetDrMd(rp,JAM2);
  350.         Move(rp,4,7);
  351.         Text(rp,title,strlen(title));
  352.     }
  353. }
  354.  
  355. /*
  356.  * setup the info display
  357.  */
  358. VOID set_info()
  359. {
  360.     struct RastPort *rp;
  361.  
  362.     rp = &MainScreen->RastPort;
  363.     sst(NULL);
  364.     DrawImage(rp,&XC_image,0,0);
  365. }
  366.  
  367. /*
  368.  * update the info line
  369.  */
  370. VOID do_info(x,y,x1,y1)
  371.     register SHORT x,y,x1,y1;
  372. {
  373.     char  mx[5],my[5],l[5],t[5],w[5],h[5];
  374.     SHORT xx,yy,tmp;
  375.     struct RastPort *rp;
  376.  
  377.     rp = &MainScreen->RastPort;
  378.  
  379.     get_xy(&xx,&yy);
  380.  
  381.     if(x1 < x) { tmp = x; x = x1; x1 = tmp; }
  382.     if(y1 < y) { tmp = y; y = y1; y1 = tmp; }
  383.  
  384.     SetAPen(rp,0);
  385.     SetBPen(rp,1);
  386.     SetDrMd(rp,JAM2);
  387.     Move(rp,15,7);
  388.     FormatText(rp,"%-4ld",xx);
  389.     Move(rp,64,7);
  390.     FormatText(rp,"%-4ld",yy);
  391.     Move(rp,114,7);
  392.     FormatText(rp,"%-4ld",x);
  393.     Move(rp,159,7);
  394.     FormatText(rp,"%-4ld",y);
  395.     Move(rp,211,7);
  396.     FormatText(rp,"%-3ld",x1-x);
  397.     Move(rp,256,7);
  398.     FormatText(rp,"%-3ld",y1-y);
  399. }
  400.  
  401. /*
  402.  * entry point
  403.  */
  404. VOID main(argc,argv)
  405.     ULONG argc;
  406.     char *argv[];
  407. {
  408.     SHORT x,y,x1,y1;
  409.     BOOL running = TRUE;
  410.     struct WBArg *wba;
  411.     struct Process *proc;
  412.  
  413.     proc = (struct Process *)FindTask(NULL);
  414.     sysreq = (struct Window *)proc->pr_WindowPtr;
  415.     proc->pr_WindowPtr = (APTR)-1L;
  416.  
  417.     open_libs();
  418.     NewList((void *)&Gadgets);
  419.  
  420.     if(NOT argc)
  421.     {   if(WBenchMsg->sm_NumArgs > 1)
  422.         {   wba = WBenchMsg->sm_ArgList;
  423.             wba++;
  424.             strcpy((char *)&name[0],(char *)wba->wa_Name);
  425.             CurrentDir((struct FileLock *)wba->wa_Lock);
  426.             ReadBinGadgets(TRUE);
  427.         }
  428.         else
  429.         {   get_config();
  430.             change_depth(DEPTH);
  431.             if(NOT WBSCREEN) LoadRGB4(&MainScreen->ViewPort,(void *)&Colors,(1 << DEPTH));
  432.             load_prefs();
  433.         }
  434.     }
  435.     else if(argc == 1)
  436.     {   get_config();
  437.         change_depth(DEPTH);
  438.         if(NOT WBSCREEN) LoadRGB4(&MainScreen->ViewPort,(void *)&Colors,(1 << DEPTH));
  439.         load_prefs();
  440.     }
  441.     else if(argc > 2) close_up("   - USAGE: GadgetEd [name]");
  442.     else
  443.     {   strcpy((char *)&name[0],argv[1]);
  444.         ReadBinGadgets(TRUE);
  445.     }
  446.  
  447.     do
  448.     {   SetWindowTitles(MainWindow,(char *)&wdt[0],(char *)TITLE);
  449.         Wait(1 << MainWindow->UserPort->mp_SigBit);
  450.         while(read_msg(MainWindow))
  451.         {   switch(Class)
  452.             {   case GADGETUP:
  453.                 case GADGETDOWN:     break;
  454.  
  455.                 case ACTIVEWINDOW:   while(read_msg(MainWindow));
  456.                                      break;
  457.  
  458.                 case INACTIVEWINDOW: if(NOT IntuitionBase->ActiveWindow)
  459.                                          ActivateWindow(MainWindow);
  460.                                      break;
  461.  
  462.                 case NEWSIZE:        grl();
  463.                                      refresh();
  464.                                      Saved = FALSE;
  465.                                      break;
  466.  
  467.                 case RAWKEY:         handle_keys(Code,Qualifier);
  468.                                      while(read_msg(MainWindow));
  469.                                      break;
  470.  
  471.                 case MENUPICK:       handle_menus(Code);
  472.                                      break;
  473.  
  474.                 case MOUSEBUTTONS:   if(Code == SELECTDOWN)
  475.                                      {   set_info();
  476.                                          get_xy(&x,&y);
  477.                                          MainX = x;
  478.                                          MainY = y;
  479.                                          SetAPen(MainRP,FrontPen);
  480.                                          SetDrMd(MainRP,COMPLEMENT);
  481.                                          draw_box(MainWindow,MainX,MainY,x,y);
  482.                                          do_info(MainX,MainY,x,y);
  483.                                          while(Code == SELECTDOWN)
  484.                                          {   while(read_msg(MainWindow));
  485.                                          }
  486.                                          while(Code != SELECTDOWN)
  487.                                          {   while(read_msg(MainWindow))
  488.                                              {   if(Class == MENUPICK) set_info();
  489.                                                  do_info(MainX,MainY,x,y);
  490.                                              }
  491.                                              get_xy(&x1,&y1);
  492.                                              if((x1 != x) OR (y1 != y))
  493.                                              {   draw_box(MainWindow,MainX,MainY,x,y);
  494.                                                  get_xy(&x,&y);
  495.                                                  draw_box(MainWindow,MainX,MainY,x,y);
  496.                                                  do_info(MainX,MainY,x,y);
  497.                                              }
  498.                                          }
  499.                                          add_gadget(MainWindow,MainX,MainY,x,y);
  500.                                          refresh();
  501.                                      }
  502.                                      break;
  503.  
  504.                 default:             break;
  505.             }
  506.         }
  507.     } while(running == TRUE);
  508.     close_up("  - BYE BYE");
  509. }
  510.