home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Programming / TextEditorExample / TextEditorExample.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-26  |  9.2 KB  |  256 lines

  1. /* TextEditorExample.c -- TextEditor class Example.
  2.  * Adapted from CheckBoxExample.c and Integer.c.
  3.  *
  4.  * Version 1.0.
  5.  * By James Jacobs of Amigan Software. Freely distributable.
  6.  *
  7.  * This is a simple example testing some of the capabilities of the
  8.  * TextEditor gadget class.
  9.  *
  10.  * This code opens a window and then creates 2 TextEditor gadgets which
  11.  * are subsequently attached to the window's gadget list.
  12.  */
  13.  
  14. // system includes
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18.  
  19. #include <exec/types.h>
  20. #include <exec/memory.h>
  21. #include <intuition/intuition.h>
  22. #include <intuition/gadgetclass.h>
  23. #include <libraries/gadtools.h>
  24. #include <graphics/gfxbase.h>
  25. #include <graphics/text.h>
  26. #include <graphics/gfxmacros.h>
  27. #include <utility/tagitem.h>
  28. #include <workbench/startup.h>
  29. #include <workbench/workbench.h>
  30.  
  31. #include <proto/intuition.h>
  32. #include <proto/graphics.h>
  33. #include <proto/exec.h>
  34. #include <proto/dos.h>
  35. #include <proto/utility.h>
  36. #include <proto/wb.h>
  37. #include <proto/icon.h>
  38.  
  39. #include <clib/alib_protos.h>
  40. #include <clib/texteditor_protos.h>
  41.  
  42. // ReAction includes
  43. #define ALL_REACTION_CLASSES
  44. #define ALL_REACTION_MACROS
  45. #include <reaction/reaction.h>
  46.  
  47. #include <gadgets/texteditor.h>
  48. #include <pragmas/texteditor_pragmas.h>
  49.  
  50. enum
  51. {   GID_MAIN         = 0,
  52.     GID_TEXTEDITOR1,
  53.     GID_TEXTEDITOR2,
  54.     GID_DOWN,
  55.     GID_UP,
  56.     GID_QUIT,
  57.     GID_LAST
  58. };
  59.  
  60. enum
  61. {   WID_MAIN = 0,
  62.     WID_LAST
  63. };
  64.  
  65. enum
  66. {   OID_MAIN = 0,
  67.     OID_LAST
  68. };
  69.  
  70. struct Library *WindowBase,
  71.                *LayoutBase,
  72.                *ButtonBase,
  73.                *TextEditorBase,
  74.                *LabelBase;
  75.  
  76. void openlibs(void)
  77. {   /* Open the classes - typically not required to be done manually.
  78.      * SAS/C or DICE AutoInit can do this for you if linked with the
  79.      * supplied reaction.lib */
  80.  
  81.     if (!(WindowBase = OpenLibrary("window.class", 44)))
  82.         Printf("OpenLibrary(\"window.class\") failed!\n");
  83.     if (!(LayoutBase = OpenLibrary("gadgets/layout.gadget", 44)))
  84.         Printf("OpenLibrary(\"gadgets/layout.gadget\") failed!\n");
  85.     if (!(ButtonBase = OpenLibrary("gadgets/button.gadget", 44)))
  86.         Printf("OpenLibrary(\"gadgets/button.gadget\") failed!\n");
  87.     if (!(TextEditorBase = OpenLibrary("gadgets/texteditor.gadget", 0)))
  88.         Printf("OpenLibrary(\"gadgets/texteditor.gadget\") failed!\n");
  89.     if (!(LabelBase = OpenLibrary("images/label.image", 44)))
  90.         Printf("OpenLibrary(\"images/label.image\") failed!\n");
  91. }
  92.  
  93. void closelibs(void) 
  94. {   // Close the classes.
  95.     CloseLibrary(LabelBase);
  96.     CloseLibrary(TextEditorBase);
  97.     CloseLibrary(ButtonBase);
  98.     CloseLibrary(LayoutBase);
  99.     CloseLibrary(WindowBase);
  100. }
  101.  
  102. void main(void)
  103. {   struct MsgPort* AppPort;
  104.     struct Window*  windows[WID_LAST];
  105.     struct Gadget*  gadgets[GID_LAST];
  106.            Object*  objects[OID_LAST];
  107.  
  108.     APTR Buffer1, Buffer2;
  109.  
  110.     if (0) // that is, never
  111.         Printf("$VER: TextEditorExample 1.0 (26.8.100)");
  112.  
  113.     /* make sure our classes opened... */
  114.     openlibs();
  115.  
  116.     if (AppPort = CreateMsgPort())
  117.     {   /* Create the window object. */
  118.         objects[OID_MAIN] = NewObject
  119.         (   WINDOW_GetClass(),               NULL,
  120.             WA_ScreenTitle,                  "ReAction",
  121.             WA_Title,                        "ReAction TextEditor Example",
  122.             WA_Activate,                     TRUE,
  123.             WA_DepthGadget,                  TRUE,
  124.             WA_DragBar,                      TRUE,
  125.             WA_CloseGadget,                  TRUE,
  126.             WA_SizeGadget,                   TRUE,
  127.             WINDOW_IconifyGadget,            TRUE,
  128.             WINDOW_IconTitle,                "TextEditor",
  129.             WINDOW_AppPort,                  AppPort,
  130.             WINDOW_Position,                 WPOS_CENTERSCREEN,
  131.             WINDOW_ParentGroup,              gadgets[GID_MAIN] = NewObject
  132.             (   LAYOUT_GetClass(),           NULL,
  133.                 LAYOUT_Orientation,          LAYOUT_ORIENT_VERT,
  134.                 LAYOUT_SpaceOuter,           TRUE,
  135.                 LAYOUT_DeferLayout,          TRUE,
  136.  
  137.                 LAYOUT_AddChild,             gadgets[GID_TEXTEDITOR1] = NewObject
  138.                 (   TEXTEDITOR_GetClass(),   NULL,
  139.                     GA_ID,                   GID_TEXTEDITOR1,
  140.                     TAG_END
  141.                 ),   
  142.                 CHILD_NominalSize,           TRUE,
  143.  
  144.                 LAYOUT_AddChild,             gadgets[GID_TEXTEDITOR2] = NewObject
  145.                 (   TEXTEDITOR_GetClass(),   NULL,
  146.                     GA_ID,                   GID_TEXTEDITOR2,
  147.                     TAG_END
  148.                 ),
  149.                 CHILD_NominalSize,           TRUE,
  150.  
  151.                 LAYOUT_AddChild,             NewObject
  152.                 (   LAYOUT_GetClass(),       NULL,
  153.                     LAYOUT_Orientation,      LAYOUT_ORIENT_VERT,
  154.                     GA_BackFill,             NULL,
  155.                     LAYOUT_SpaceOuter,       TRUE,
  156.                     LAYOUT_VertAlignment,    LALIGN_CENTER,
  157.                     LAYOUT_HorizAlignment,   LALIGN_CENTER,
  158.                     LAYOUT_BevelStyle,       BVS_FIELD,
  159.  
  160.                     LAYOUT_AddImage,         NewObject
  161.                     (   LABEL_GetClass(),    NULL,
  162.                         LABEL_Text,          "This is an example of how to use the\n",
  163.                         LABEL_Text,          "ReAction texteditor.gadget class.\n",
  164.                         LABEL_Text,          " \n",
  165.                         LABEL_Text,          "As you can see, it creates two\n",
  166.                         LABEL_Text,          "instances of the texteditor.gadget.\n",
  167.                         LABEL_Justification, LJ_CENTRE,
  168.                         TAG_END
  169.                     ),
  170.                     TAG_END
  171.                 ),
  172.  
  173.                 LAYOUT_AddChild,             NewObject
  174.                 (   NULL,                    "button.gadget",
  175.                     GA_ID,                   GID_QUIT,
  176.                     GA_RelVerify,            TRUE,
  177.                     GA_Text,                 "_Quit",
  178.                     TAG_END
  179.                 ),
  180.                 CHILD_WeightedHeight,        0,
  181.                 TAG_END
  182.             ),
  183.             TAG_END
  184.         );
  185.  
  186.         // Object creation successful?
  187.         if (objects[OID_MAIN])
  188.         {   // Open the window.
  189.             if (windows[WID_MAIN] = (struct Window *) RA_OpenWindow(objects[OID_MAIN]))
  190.             {   ULONG wait, signal, app = (1L << AppPort->mp_SigBit);
  191.                 ULONG done = FALSE;
  192.                 ULONG result;
  193.                 UWORD code;
  194.  
  195.                 // Obtain the window wait signal mask.
  196.                 GetAttr(WINDOW_SigMask, objects[OID_MAIN], &signal);
  197.  
  198.                 // Activate the first texteditor gadget.
  199.                 ActivateLayoutGadget(gadgets[GID_MAIN], windows[WID_MAIN], NULL, (Object) gadgets[GID_TEXTEDITOR1]);
  200.  
  201.                 // Input Event Loop
  202.                 while (!done)
  203.                 {   wait = Wait( signal | SIGBREAKF_CTRL_C | app );
  204.  
  205.                     if ( wait & SIGBREAKF_CTRL_C )
  206.                         done = TRUE;
  207.                     else
  208.                     {   while ( (result = RA_HandleInput(objects[OID_MAIN], &code) ) != WMHI_LASTMSG )
  209.                         {   switch (result & WMHI_CLASSMASK)
  210.                             {
  211.                             case WMHI_CLOSEWINDOW:
  212.                                 // windows[WID_MAIN] = NULL;
  213.                                 done = TRUE;
  214.                             break;
  215.                             case WMHI_GADGETUP:
  216.                                 switch (result & WMHI_GADGETMASK)
  217.                                 {
  218.                                 case GID_QUIT:
  219.                                     done = TRUE;
  220.                                 break;
  221.                                 default:
  222.                                 break;
  223.                                 }
  224.                             break;
  225.                             case WMHI_ICONIFY:
  226.                                 RA_Iconify(objects[OID_MAIN]);
  227.                                 // windows[WID_MAIN] = NULL;
  228.                             break;
  229.                             case WMHI_UNICONIFY:
  230.                                 windows[WID_MAIN] = (struct Window *) RA_OpenWindow(objects[OID_MAIN]);
  231.                                 if (windows[WID_MAIN])
  232.                                     GetAttr(WINDOW_SigMask, objects[OID_MAIN], &signal);
  233.                                 else done = TRUE; // error re-opening window!
  234.                             break;
  235.                             default:
  236.                             break;
  237.             }   }   }   }   }
  238.  
  239.             Buffer1 = DoGadgetMethod(gadgets[GID_TEXTEDITOR1], (struct Window *) objects[WID_MAIN], NULL, GM_TEXTEDITOR_ExportText, NULL);
  240.             Buffer2 = DoGadgetMethod(gadgets[GID_TEXTEDITOR2], (struct Window *) objects[WID_MAIN], NULL, GM_TEXTEDITOR_ExportText, NULL);
  241.             Printf("1: %s\n2: %s\n", Buffer1, Buffer2);
  242.             FreeVec(Buffer1);
  243.             FreeVec(Buffer2);
  244.  
  245.             /* Disposing of the window object will also close the window if it is
  246.              * already opened, and it will dispose of the layout object attached to it.
  247.              */
  248.             DisposeObject(objects[OID_MAIN]);
  249.         } else Printf("NewObject() failed!\n");
  250.         DeleteMsgPort(AppPort);
  251.     } else Printf("CreateMsgPort() failed!\n");
  252.     closelibs();
  253.  
  254.     return;
  255. }
  256.