;/* Execute me to compile with DICE V3.0.
dcc cxdemo.c -proto -mi -ms -mRR -lbgui
quit
*/
/*
**      CXDEMO.C
**
**      (C) Copyright 1995 Jaba Development.
**      (C) Copyright 1995 Jan van den Baard.
**          All Rights Reserved.
**/

#include "democode.h"

/*
**      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.";

VOID StartDemo( void )
{
        Object          *CM_Broker, *WN_Window, *GA_Hide, *GA_Quit;
        ULONG            signal = 0L, winsig = 0L, sigrec, type, id, rc;
        BOOL             running = TRUE;

        /*
        **      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;

        /*
        **      Object OK?
        **/
        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_AutoAspect,      TRUE,
                        WINDOW_MasterGroup,
                                VGroupObject, HOffset( 4 ), VOffset( 4 ), Spacing( 4 ), GROUP_BackFill, SHINE_RASTER,
                                        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;

                /*
                **      Window OK?
                **/
                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:
                                                                                                Tell( "bye bye\n" );
                                                                                                running = FALSE;
                                                                                                break;

                                                                                        case    CXCMD_DISABLE:
                                                                                                Tell( "broker disabled\n" );
                                                                                                DisableBroker( CM_Broker );
                                                                                                break;

                                                                                        case    CXCMD_ENABLE:
                                                                                                Tell( "broker enabled\n" );
                                                                                                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.
                                                                                **/
                                                                                Tell( "bye bye\n" );
                                                                                running = FALSE;
                                                                                break;
                                                                }
                                                        }
                                                }

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