home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / new / dev / gui / visualarts / demo / source / console / console.c < prev   
Encoding:
C/C++ Source or Header  |  1995-01-01  |  7.2 KB  |  306 lines

  1. /* ******************************************************************** */
  2. /* C code generated by:                            */
  3. /* Visual Arts Version 2.1                        */
  4. /* Copyright 1994-95 Danny Y. Wong  All rights reserved            */
  5. /* Calgary, Alberta (CANADA)                                    */
  6. /* Partial of the code is copyright by Jaba Development            */
  7. /* ******************************************************************** */
  8.  
  9. #include <VisualArts.h> 
  10.  
  11. #define ID_ObjID1                                     0
  12.  
  13. #define MainNumGads                        1
  14.  
  15. #include "Console_func.c" 
  16.  
  17.  
  18. int GetPubScreen(void);
  19. void ClosePubScreen(void);
  20. int OpenMainWindow(char windtitle[]);
  21. void CloseMainWindow(void);
  22. int InitMainConsole(void);
  23. int MainConsoleHandler(void);
  24. int MainHandler(void);
  25. int MainMainHandler(void);
  26. void DrawMainObjs(void);
  27. int main(int argc, char *argv[]);
  28.  
  29. char                             myString[256];
  30. short                            counter=0;
  31. UBYTE                *PubScrName = "Workbench";
  32. struct DrawInfo            *ScrDrawInfo = NULL;
  33. APTR                 VisualInfo = NULL;
  34. struct Screen            *Scr = NULL;
  35. struct Window            *MainWnd = NULL;
  36. struct Window            *MainConWnd = NULL;
  37. struct MsgPort            *MainConWritePort = NULL;
  38. struct MsgPort            *MainConReadPort = NULL;
  39. struct IOStdReq            *MainConWriteReq = NULL;
  40. struct IOStdReq            *MainConReadReq = NULL;
  41. struct Gadget            *MainGList = NULL;
  42. struct Gadget            *MainGadgets[MainNumGads];
  43. struct IntuiMessage         MainMsg;
  44. UWORD                 MainLeft = 118;
  45. UWORD                 MainTop = 32;
  46. UWORD                 MainWidth = 314;
  47. UWORD                 MainHeight = 50;
  48. char                 character;
  49. struct TextAttr topaz8 = { (STRPTR)"topaz.font", 8, 0x00, 0x01 };
  50. struct TextAttr topaz800 = { (STRPTR)"topaz.font", 8, 0x00, 0x00 };
  51.  
  52. struct IntuiText MainIText[] = {
  53.   1, 0, JAM2, 71, 2, &topaz800, (UBYTE *)"Text from Console Window", NULL,
  54. };
  55.  
  56. WORD MainGadTypes[] = {
  57.     TEXT_KIND,
  58. };
  59.  
  60. struct NewGadget MainNGads[] = {
  61.     7, 12, 293, 22, (UBYTE *)"", &topaz800, ID_ObjID1, PLACETEXT_LEFT, NULL, NULL,
  62. };
  63.  
  64. ULONG MainNTags[] = {
  65.     (GTTX_Text),(ULONG)"", (GTTX_Border), TRUE, TAG_DONE,
  66. };
  67.  
  68.  
  69. int GetPubScreen(void)
  70. {
  71.   if (!(Scr = LockPubScreen(PubScrName)))
  72.     return(1L);
  73.  
  74.   if (!(VisualInfo = GetVisualInfo(Scr, TAG_DONE)))
  75.     return(2L);
  76.  
  77.   if (!(ScrDrawInfo = GetScreenDrawInfo(Scr)))
  78.     return(3L);
  79.   return(0L);
  80. }
  81.  
  82. void ClosePubScreen(void)
  83. {
  84.   if (VisualInfo)
  85.     FreeVisualInfo(VisualInfo);
  86.   if (Scr)
  87.     UnlockPubScreen(NULL,  Scr);
  88.   if (ScrDrawInfo)
  89.     FreeScreenDrawInfo(Scr,  ScrDrawInfo);
  90. }
  91.  
  92. int InitMainConsole(void)
  93. {
  94.   MainConWnd = OpenWindowTags( NULL,
  95.     WA_Left,        200,
  96.     WA_Top,            100,
  97.     WA_Width,        250,
  98.     WA_Height,        50,
  99.     WA_Title,        "Console Window",
  100.     WA_IDCMP,        NULL,
  101.     WA_Flags,        WFLG_DRAGBAR | WFLG_SIMPLE_REFRESH | WFLG_DEPTHGADGET,
  102.     WA_CustomScreen,    Scr,
  103.     TAG_DONE );
  104.  
  105.   if ( MainConWnd == NULL )
  106.     return(6L);
  107.  
  108.   MainConWritePort = CreatePort("Main.con.write", 0);
  109.   if ( !MainConWritePort)
  110.     return(1L);
  111.   MainConWriteReq = CreateStdIO(MainConWritePort);
  112.   if ( !MainConWriteReq)
  113.     return(2L);
  114.   MainConReadPort = CreatePort("Main.con.read", 0);
  115.   if ( !MainConReadPort)
  116.     return(3L);
  117.   MainConReadReq =  CreateStdIO(MainConReadPort);
  118.   if ( !MainConReadReq)
  119.     return(4L);
  120.   if ((OpenConsole(MainConWriteReq, MainConReadReq, MainConWnd)) != 0)
  121.     return(5L);
  122.  
  123.   return(0L);
  124. }
  125.  
  126.  
  127. int OpenMainWindow(char windtitle[80])
  128. {
  129.   struct NewGadget    NewGad;
  130.   struct Gadget       *Gad;
  131.   register UWORD i, j;
  132.   UWORD offsetx = Scr->WBorLeft;
  133.   UWORD offsety = Scr->WBorTop + Scr->RastPort.TxHeight + 1;
  134.  
  135.   if (!(Gad = CreateContext(&MainGList)))
  136.       return(1L);
  137.  
  138.   for (i=0, j=0; i < MainNumGads; i++)
  139.   {
  140.     CopyMem((char *)&MainNGads[i], (char *)&NewGad, (long)sizeof(struct NewGadget));
  141.  
  142.     NewGad.ng_VisualInfo = VisualInfo;
  143.     NewGad.ng_LeftEdge += offsetx;
  144.     NewGad.ng_TopEdge  += offsety;
  145.  
  146.     MainGadgets[i] = Gad = CreateGadgetA((ULONG)MainGadTypes[i], Gad, &NewGad,
  147.     (struct TagItem *)&MainNTags[j]);
  148.     while (MainNTags[j])
  149.         j +=2;
  150.     j++;
  151.     if (!Gad)
  152.        return(2L);
  153.   }
  154.   if (!(MainWnd = OpenWindowTags(NULL,
  155.       WA_Left,    MainLeft,
  156.       WA_Top,     MainTop,
  157.       WA_Width,    MainWidth,
  158.       WA_Height,    MainHeight,
  159.       WA_NewLookMenus, TRUE,
  160.       WA_IDCMP,    IDCMP_CLOSEWINDOW |  IDCMP_NEWSIZE ,
  161.       WA_Flags,    WFLG_CLOSEGADGET | WFLG_DEPTHGADGET  |  WFLG_SIMPLE_REFRESH | 
  162.              WFLG_ACTIVATE |  WFLG_DRAGBAR ,
  163.       WA_Gadgets,    MainGList,
  164.       WA_Title,    windtitle,
  165.       WA_ScreenTitle,    "Visual Arts V2.1 Copyright 1994-95 Danny Y. Wong  All Rights Reserved.",
  166.       WA_PubScreen,    Scr,
  167.       WA_MinWidth,    160,
  168.       WA_MinHeight,    50,
  169.       WA_MaxWidth,    640,
  170.       WA_MaxHeight,    200,
  171.       TAG_DONE)))
  172.           return(3L);
  173.  
  174.   DrawMainObjs();
  175.   GT_RefreshWindow(MainWnd, NULL);
  176.   RefreshGadgets(MainGadgets[0], MainWnd, NULL);
  177.   if (InitMainConsole())
  178.     return(4L);
  179.   return(0L);
  180. }
  181.  
  182. void CloseMainWindow(void)
  183. {
  184.   if (MainConWnd)
  185.     CloseWindow(MainConWnd);
  186.   if (MainConWriteReq)
  187.     CloseDevice((struct IORequest *)MainConWriteReq);
  188.   if (MainConReadReq)
  189.     DeleteStdIO(MainConReadReq);
  190.   if (MainConReadPort)
  191.     DeletePort(MainConReadPort);
  192.   if (MainConWriteReq)
  193.     DeleteStdIO(MainConWriteReq);
  194.   if (MainConWriteReq)
  195.     DeletePort(MainConWritePort);
  196.   if (MainWnd)
  197.     CloseWindow(MainWnd);
  198.   if (MainGList)
  199.     FreeGadgets(MainGList);
  200. }
  201.  
  202. int MainHandler(void)
  203. {
  204.   struct IntuiMessage    *msg;
  205.   struct VAobject    VAObject;
  206.   int running    = 1;
  207.   ULONG class;
  208.   UWORD code;
  209.  
  210.   while (msg=GT_GetIMsg(MainWnd->UserPort))
  211.   {
  212.     CopyMem((char *)msg, (char *)&MainMsg, (long)sizeof(struct IntuiMessage));
  213.     class = msg->Class;
  214.     code  = msg->Code;
  215.  
  216.     VAObject.va_Window = (struct Window *)MainWnd;
  217.     VAObject.va_Gadget = (struct Gadget *)msg->IAddress;
  218.     VAObject.va_IntuiMsg = (struct IntuiMessage *)msg;
  219.     VAObject.va_Flags = 0;
  220.     VAObject.va_UserData = 0;
  221.  
  222.     GT_ReplyIMsg(msg);
  223.     switch(class)
  224.     {
  225.  
  226.       case IDCMP_NEWSIZE:
  227.        break;
  228.  
  229.       case IDCMP_CLOSEWINDOW:
  230.          return(0);
  231.          break;
  232.     }
  233.   }
  234.   return(running);
  235. }
  236.  
  237. void DrawMainObjs(void)
  238. {
  239.   PrintIText(MainWnd->RPort, MainIText, MainWnd->BorderLeft, MainWnd->BorderTop);
  240.  
  241. }
  242.  
  243. int MainConsoleHandler(void)
  244. {
  245.   while( CheckIO((struct IORequest *)MainConReadReq))
  246.   {
  247.     WaitIO( (struct IORequest *)MainConReadReq);
  248.     QueueRead(MainConReadReq, &character);
  249.     ConPutChar(MainConWriteReq, character);
  250.         if ((int)character == 13 || counter > 255)
  251.         {
  252.           myString[counter]='\0';
  253.       ConPutChar(MainConWriteReq, (char)12);
  254.           GT_SetGadgetAttrs(MainGadgets[0], MainWnd,  NULL,
  255.                 GTTX_Text, "                                      ");
  256.           GT_SetGadgetAttrs(MainGadgets[0], MainWnd,  NULL,
  257.                 GTTX_Text, myString);
  258.           counter=0;
  259.         }
  260.         else
  261.         if (isprint(character))
  262.           myString[counter++] = character;
  263.   }
  264.   return(1);
  265. }
  266.  
  267.  
  268. int MainMainHandler(void)
  269. {
  270.   int running = 1;
  271.   ULONG windsig, signals;
  272.  
  273.   ULONG consolesig;
  274.   QueueRead(MainConReadReq, &character);
  275.   consolesig = 1L << MainConReadReq->io_Message.mn_ReplyPort->mp_SigBit;
  276.   windsig = 1L << MainWnd->UserPort->mp_SigBit;
  277.   while (running == 1)
  278.   {
  279.     signals = Wait( windsig | consolesig );
  280.     if (signals & windsig)
  281.     {
  282.       running = MainHandler();
  283.     }
  284.     else
  285.     if (signals & consolesig)
  286.       running = MainConsoleHandler();
  287.   }
  288.   return(running);
  289. }
  290.  
  291. int main(int argc, char *argv[])
  292. {
  293.   int rc;
  294.  
  295.   if (!(GetPubScreen()))
  296.   {
  297.     if (!(OpenMainWindow("Console Demo")))
  298.     {
  299.       rc = MainMainHandler();
  300.       CloseMainWindow();
  301.     }
  302.     ClosePubScreen();
  303.   }
  304.   return(0L);
  305. }
  306.