;/* Execute me to compile with DICE V3.0.
dcc cxdemo.c -proto -mi -ms -mRR -lbgui
quit
*/
/*
**         $RCSfile: cxdemo.c,v $
**      Description: Small BGUI commodity class demonstration.
**        Copyright: (C) Copyright 1994 Jaba Development.
**                   (C) Copyright 1994 Jan van den Baard.
**                   All Rights Reserved.
**
**          $Author: jaba $
**        $Revision: 1.2 $
**            $Date: 1994/08/03 11:19:36 $
**/

#include <exec/types.h>
#include <libraries/gadtools.h>
#include <dos/dos.h>
#include <libraries/bgui.h>
#include <libraries/bgui_macros.h>

#include <clib/alib_protos.h>

#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/bgui.h>

#include <stdio.h>

/*
**      Library base pointer.
**/
struct Library  *BGUIBase       = NULL;

/*
**      Key ID.
**/
#define CX_F1_PRESSED           1L

/*
**      Gadget ID's.
**/
#define ID_HIDE                 1L
#define ID_QUIT                 2L

/*
**      Information text.
**/
UBYTE *InfoTxt = ISEQ_C ISEQ_B ISEQ_HIGHLIGHT
                 "CxDemo\n\n" ISEQ_TEXT ISEQ_N
                 "This is a small \"do-nothing\" example of how\n"
                 "to use the BGUI commodity class.\n"
                 "In this example F1 is the Hotkey used to\n"
                 "signal the broker to open the window.";

int main( int argc, char *argv[] )
{
        Object          *CM_Broker, *WN_Window, *GA_Hide, *GA_Quit;
        ULONG            signal = 0L, winsig = 0L, sigrec, type, id, rc;
        BOOL             running = TRUE;

        /*
        **      Open the library.
        **/
        if ( BGUIBase = OpenLibrary( BGUINAME, BGUIVERSION )) {
                /*
                **      Setup a commodity object.
                **/
                CM_Broker = CommodityObject,
                        COMM_Name,              "CxDemo",
                        COMM_Title,             "Simple BGUI broker.",
                        COMM_Description,       "Does not do anything usefull.",
                        COMM_ShowHide,          TRUE,
                EndObject;

                if ( CM_Broker ) {
                        /*
                        **      Create a small window.
                        **/
                        WN_Window = WindowObject,
                                WINDOW_Title,           "CxDemo",
                                WINDOW_RMBTrap,         TRUE,
                                WINDOW_SizeGadget,      FALSE,  /* No use in this window. */
                                WINDOW_MasterGroup,
                                        VGroupObject, HOffset( 4 ), VOffset( 4 ), Spacing( 4 ),
                                                StartMember,
                                                        InfoObject, ButtonFrame,
                                                                FRM_Flags,              FRF_RECESSED,
                                                                INFO_TextFormat,        InfoTxt,
                                                                INFO_FixTextWidth,      TRUE,
                                                                INFO_MinLines,          6,
                                                        EndObject,
                                                EndMember,
                                                StartMember,
                                                        HGroupObject, Spacing( 4 ),
                                                                StartMember, GA_Hide = KeyButton( "_Hide", ID_HIDE ), EndMember,
                                                                VarSpace( DEFAULT_WEIGHT ),
                                                                StartMember, GA_Quit = KeyButton( "_Quit", ID_QUIT ), EndMember,
                                                        EndObject, FixMinHeight,
                                                EndMember,
                                        EndObject,
                        EndObject;

                        if ( WN_Window ) {
                                /*
                                **      Add F1 as hotkey.
                                **/
                                if ( AddHotkey( CM_Broker, "f1", CX_F1_PRESSED, 0L )) {
                                        /*
                                        **      Add gadget keys.
                                        **/
                                        GadgetKey( WN_Window, GA_Hide, "h" );
                                        GadgetKey( WN_Window, GA_Quit, "q" );
                                        /*
                                        **      Obtain broker signal mask.
                                        **/
                                        GetAttr( COMM_SigMask, CM_Broker, &signal );
                                        /*
                                        **      Activate the broker.
                                        **/
                                        EnableBroker( CM_Broker );
                                        /*
                                        **      Open up the window.
                                        **/
                                        if ( WindowOpen( WN_Window )) {
                                                /*
                                                **      Obtain window sigmask.
                                                **/
                                                GetAttr( WINDOW_SigMask, WN_Window, &winsig );
                                                /*
                                                **      Wait for messages.
                                                **/
                                                do {
                                                        sigrec = Wait( signal | winsig | SIGBREAKF_CTRL_C );
                                                        /*
                                                        **      Broker signal?
                                                        **/
                                                        if ( sigrec & signal ) {
                                                                /*
                                                                **      Obtain the messages from the
                                                                **      broker.
                                                                **/
                                                                while ( MsgInfo( CM_Broker, &type, &id, NULL ) != CMMI_NOMORE ) {
                                                                        /*
                                                                        **      Evaluate message.
                                                                        **/
                                                                        switch ( type ) {

                                                                                case    CXM_IEVENT:
                                                                                        switch ( id ) {

                                                                                                case    CX_F1_PRESSED:
                                                                                                        goto openUp;
                                                                                        }
                                                                                        break;

                                                                                case    CXM_COMMAND:
                                                                                        switch ( id ) {

                                                                                                case    CXCMD_KILL:
                                                                                                        puts( "bye bye" );
                                                                                                        running = FALSE;
                                                                                                        break;

                                                                                                case    CXCMD_DISABLE:
                                                                                                        puts( "broker disabled" );
                                                                                                        DisableBroker( CM_Broker );
                                                                                                        break;

                                                                                                case    CXCMD_ENABLE:
                                                                                                        puts( "broker enabled" );
                                                                                                        EnableBroker( CM_Broker );
                                                                                                        break;

                                                                                                case    CXCMD_UNIQUE:
                                                                                                case    CXCMD_APPEAR:
                                                                                                        openUp:
                                                                                                        if ( WindowOpen( WN_Window ))
                                                                                                                GetAttr( WINDOW_SigMask, WN_Window, &winsig );
                                                                                                        break;

                                                                                                case    CXCMD_DISAPPEAR:
                                                                                                        WindowClose( WN_Window );
                                                                                                        winsig = 0L;
                                                                                                        break;
                                                                                        }
                                                                                        break;
                                                                        }
                                                                }
                                                        }

                                                        /*
                                                        **      Window signal?
                                                        **/
                                                        if ( sigrec & winsig ) {
                                                                while ( WN_Window && (( rc = HandleEvent( WN_Window )) != WMHI_NOMORE )) {
                                                                        switch ( rc ) {

                                                                                case    ID_HIDE:
                                                                                case    WMHI_CLOSEWINDOW:
                                                                                        /*
                                                                                        **      Hide the window.
                                                                                        **/
                                                                                        WindowClose( WN_Window );
                                                                                        winsig = 0L;
                                                                                        break;

                                                                                case    ID_QUIT:
                                                                                        /*
                                                                                        **      The end.
                                                                                        **/
                                                                                        puts( "bye bye" );
                                                                                        running = FALSE;
                                                                                        break;
                                                                        }
                                                                }
                                                        }

                                                        /*
                                                        **      CTRL+C?
                                                        **/
                                                        if ( sigrec & SIGBREAKF_CTRL_C ) {
                                                                puts( "bye bye" );
                                                                running = FALSE;
                                                        }
                                                } while ( running );
                                        } else
                                                puts( "unable to open the window" );
                                } else
                                        puts( "unable to add the hotkey" );
                                DisposeObject( WN_Window );
                        } else
                                puts( "unable to create a window object" );
                        DisposeObject( CM_Broker );
                } else
                        puts( "unable to create a commodity object" );
                CloseLibrary( BGUIBase );
        }
        return( 0 );
}

#ifdef _DCC
int wbmain( struct wbStartup *wbs )
{
        return( main( 0, NULL ));
}
#endif

/*
 *      $Log: cxdemo.c,v $
 * Revision 1.2  1994/08/03  11:19:36  jaba
 * Switched to proto/ instead of clib/.
 *
 * Revision 1.1  1994/06/20  16:49:25  jaba
 * Initial revision
 *
 */
