home *** CD-ROM | disk | FTP | other *** search
- /* zSample.c
- * Copyright (c) 1993-1994 Reality Check, Inc.
- * Written by David N. Junod
- *
- * This is a sample of how a C program can interface to the
- * same library that the ZedREXX REXX Language Extension uses.
- *
- * This requires the header files that are included with
- * the ZedREXX SDK. The SDK does NOT come with the ZedREXX
- * registration, but is instead an additional product
- * available through Reality Check, Inc. Direct inquiries
- * to David N. Junod.
- *
- */
-
- /*****************************************************************************/
-
- #include <dos/dos.h>
- #include <exec/types.h>
- #include <exec/memory.h>
- #include <exec/execbase.h>
- #include <exec/libraries.h>
- #include <graphics/text.h>
- #include <libraries/diskfont.h>
- #include <rci/rciface.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #include <clib/dos_protos.h>
- #include <clib/diskfont_protos.h>
- #include <clib/exec_protos.h>
- #include <clib/graphics_protos.h>
- #include <clib/utility_protos.h>
- #include <clib/rciface_protos.h>
-
- #include <pragmas/dos_pragmas.h>
- #include <pragmas/diskfont_pragmas.h>
- #include <pragmas/exec_pragmas.h>
- #include <pragmas/graphics_pragmas.h>
- #include <pragmas/utility_pragmas.h>
- #include <pragmas/rciface_pragmas.h>
-
- /*****************************************************************************/
-
- struct Library *SysBase, *DOSBase, *GfxBase, *DiskfontBase, *UtilityBase;
- struct Library *RCIFaceBase;
-
- /*****************************************************************************/
-
- struct TextAttr topazAttr = {"topaz.font", 8,};
- struct TextAttr helvAttr = {"helvetica.font", 13,};
-
- /*****************************************************************************/
-
- /* This is our user interface description, the exact same as in a ZedREXX
- * script. In fact, usually you would prototype your user interface in
- * ZedREXX and then convert it to C. */
- STRPTR data[] =
- {
- "zInterface zSample",
- "zWindow Main Label=\"zSample\" Open Centered Vertical CloseEvent Justification=Center Alignment=Center",
- "zMenu Project",
- "zObject Button IconM Label=\"&Iconify\" Cause=\"zDoMethod zSample Deactivate\"",
- "zObject Line",
- "zObject Button Quit Label=\"&Quit\" SelectEvent",
- "zEndMenu",
- "zObject TextDisplay Value=\"zHello zSilly zWorld\" Justification=Center",
- "zObject Line",
- "zObject Button OK Label=\"&OK\" SelectEvent",
- "zEndWindow",
- "zEndInterface"
- };
-
- /*****************************************************************************/
-
- /* Functions used by our user interface */
- ULONG HOOK QuitF (PA0 struct zHook *h, PA1 CmdP c, PA2 struct EventMsg *em);
- ULONG HOOK DefaultF (PA0 struct zHook *h, PA1 CmdP c, PA2 struct EventMsg *em);
-
- /*****************************************************************************/
-
- /* The function table used by our user interface */
- static struct Cmd cmdArray[] =
- {
- /* The following is sent when the Quit menu item is selected */
- {"Quit_Select", QuitF, NULL, ",", 1,},
-
- /* The following is sent when the Main window is closed */
- {"Main_Close", QuitF, NULL, ",", 1,},
-
- /* The following is sent when closed by Exchange */
- {"zSample_Quit", QuitF, NULL, ",", 1,},
- {NULL,},
- };
-
- static struct Cmd DefaultCmd = {"Default", DefaultF, NULL, "ATTRS/F", 1,};
-
- /*****************************************************************************/
-
- /* Global data used by our application */
- typedef struct GlobalData
- {
- APTR gd_IH;
- ULONG gd_Going;
-
- } GlobalData, *GlobalDataP;
-
- /*****************************************************************************/
-
- /* The "main" function should be the only function that contains
- * direct OS calls. ZedREXX applications are portable to OS/2, Windows
- * and MS-DOS. ZedREXX is the simplistic multi-platform toolkit that
- * Reality Check has to offer. For larger products, the Viper Toolkit
- * is recommended. */
- LONG main (void)
- {
- LONG failureLevel = RETURN_FAIL;
- struct WBStartup *wbm = NULL;
- LONG failureCode = 0;
- struct Process *pr;
-
- ULONG error, res, res2;
- struct EventMsg *em;
- ULONG zsig, sigr;
- GlobalDataP gd;
- APTR ih;
-
- struct TextAttr *ta = &topazAttr;
- struct TextFont *tf;
-
- SysBase = (*((struct Library **) 4));
-
- /* I don't like using compiler startup code */
- pr = (struct Process *) ((struct ExecBase *)SysBase)->ThisTask;
- if (!(pr->pr_CLI))
- {
- WaitPort (&pr->pr_MsgPort);
- wbm = (struct WBStartup *) GetMsg (&pr->pr_MsgPort);
- }
-
- if (SysBase->lib_Version < 37)
- {
- failureCode = ERROR_INVALID_RESIDENT_LIBRARY;
- }
- else if (gd = AllocVec (sizeof (struct GlobalData), MEMF_CLEAR))
- {
- /* Open ROM libraries */
- DOSBase = OpenLibrary ("dos.library", 37);
- GfxBase = OpenLibrary ("graphics.library", 37);
- DiskfontBase = OpenLibrary ("diskfont.library", 37);
- UtilityBase = OpenLibrary ("utility.library", 37);
-
- /* Open the RCI user interface library */
- if (RCIFaceBase = OpenLibrary ("rciface.library", 0))
- {
- /* The font must be loaded into RAM before it can be used. */
- if ((tf = OpenFont (&helvAttr)) == NULL)
- tf = OpenDiskFont (&helvAttr);
- if (tf)
- ta = &helvAttr;
-
- /* Open the user interface */
- if (ih = zOpenInterface (
- ZIA_Interface, (ULONG) data,
- ZIA_Functions, (ULONG) cmdArray,
- ZIA_DefaultFunction, (ULONG) & DefaultCmd,
- ZIA_FunctionData, gd,
- ZIA_TextAttr, ta,
- TAG_DONE))
- {
- failureLevel = RETURN_OK;
-
- /* Initialize our global variables. */
- gd->gd_IH = ih;
- gd->gd_Going = TRUE;
-
- /* Get the signal bit for the user interface */
- zsig = 1L << zInterfaceSignal (ih);
-
- /* Activate the user interface */
- res = zSendCommand (ih, "zDoMethod zSample Activate", &res2);
-
- /* Clear a few necessaries */
- res = res2 = 0;
-
- Printf ("Activate the shell that started this example and press ^F\n");
-
- /* Look until there is no reason to continue */
- while (gd->gd_Going)
- {
- /* Wait for something to happen */
- sigr = Wait (zsig | SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_E | SIGBREAKF_CTRL_F);
-
- /* Was it a RCI message */
- if (sigr & zsig)
- {
- /* Pull the message */
- while (em = zGetEventMsg (ih))
- {
- /* Handle the message */
- zInterpret (ih, em, &error);
-
- /* Reply to the message */
- zReplyEventMsg (ih, em, res, res2);
- }
- }
-
- /* Was it a ^C */
- if (sigr & SIGBREAKF_CTRL_C)
- gd->gd_Going = FALSE;
-
- /* Was it a ^E */
- if (sigr & SIGBREAKF_CTRL_E)
- res = zSendCommand (ih, "zDoMethod zSample Activate", &res2);
-
- /* Was it a ^F */
- if (sigr & SIGBREAKF_CTRL_F)
- {
- UBYTE otitle[64], buffer[32];
- LONG i;
-
- /* Get the current title */
- zGetAttr (ih, "Main", "Label", otitle, sizeof (otitle));
-
- Printf ("Click on the OK button to return to the main loop\n");
-
- i = 1;
- while (i)
- {
- /* Wait around for a little while */
- Delay (10);
-
- /* Change the window title bar to show the current value */
- sprintf (buffer, "%06ld", i++);
- zSetAttr (ih, "Main", "Label", buffer);
-
- /* See if anything has happened */
- if (zCheckForEvent (ih))
- i = 0;
- }
-
- /* Restore the original title */
- zSetAttr (ih, "Main", "Label", otitle);
- }
- }
-
- /* Close the user interface */
- zCloseInterface (ih);
- }
- CloseFont (tf);
- CloseLibrary (RCIFaceBase);
- }
- else
- {
- Printf ("couldn't open rciface.library\n");
- }
-
- /* Close libraries */
- CloseLibrary (UtilityBase);
- CloseLibrary (DiskfontBase);
- CloseLibrary (GfxBase);
- CloseLibrary (DOSBase);
-
- FreeVec (gd);
- }
- else
- failureCode = ERROR_NO_FREE_STORE;
-
- if (wbm)
- {
- Forbid ();
- ReplyMsg ((struct Message *) wbm);
- }
- pr->pr_Result2 = failureCode;
- return (failureLevel);
- }
-
- /*****************************************************************************/
- /*****************************************************************************/
- /*****************************************************************************/
-
- ULONG HOOK QuitF (PA0 struct zHook * h, PA1 CmdP c, PA2 struct EventMsg * em)
- {
- GlobalDataP gd = (GlobalDataP) h->h_UserData;
-
- /* Tell peoples what is going on around here */
- Printf ("quit : win='%s', obj='%s', event='%s'\n", em->em_Window, em->em_Object, em->em_Event);
-
- gd->gd_Going = FALSE;
- return 1;
- }
-
- /*****************************************************************************/
-
- ULONG HOOK DefaultF (PA0 struct zHook * h, PA1 CmdP c, PA2 struct EventMsg * em)
- {
- Printf ("def : win='%s', obj='%s', event='%s'\n", em->em_Window, em->em_Object, em->em_Event);
-
- return 1;
- }
-