-> simplegtgadget.e - Simple example of a GadTools gadget. OPT OSVERSION=37 MODULE 'gadtools', 'exec/ports', 'graphics/text', 'intuition/intuition', 'intuition/screens', 'libraries/gadtools' ENUM ERR_NONE, ERR_GAD, ERR_LIB, ERR_PUB, ERR_VIS, ERR_WIN RAISE ERR_GAD IF CreateGadgetA()=NIL, ERR_LIB IF OpenLibrary()=NIL, ERR_PUB IF LockPubScreen()=NIL, ERR_VIS IF GetVisualInfoA()=NIL, ERR_WIN IF OpenWindowTagList()=NIL CONST MYGAD_BUTTON=4 -> Open all libraries and run. Clean up when finished or on error.. PROC main() HANDLE gadtoolsbase:=OpenLibrary('gadtools.library', 37) gadtoolsWindow() EXCEPT DO IF gadtoolsbase THEN CloseLibrary(gadtoolsbase) SELECT exception CASE ERR_GAD; WriteF('Error: Could not create gadget\n') CASE ERR_LIB; WriteF('Error: Could not open gadtools.library\n') CASE ERR_PUB; WriteF('Error: Could not lock public screen\n') CASE ERR_VIS; WriteF('Error: Could not get visual info\n') CASE ERR_WIN; WriteF('Error: Could not open window\n') ENDSELECT ENDPROC -> Prepare for using GadTools, set up gadgets and open window. -> Clean up and when done or on error. PROC gadtoolsWindow() HANDLE DEF mysc=NIL:PTR TO screen, mywin=NIL, glist=NIL, gad, vi=NIL mysc:=LockPubScreen(NIL) vi:=GetVisualInfoA(mysc, [NIL]) -> GadTools gadgets require this step to be taken gad:=CreateContext({glist}) -> Create a button gadget centered bel3, G 2գ9֍K>Pv&[{Mu&ʚ+Hh6nPM5 ^8j-ɴV~ƠR=x}⡉fſ0 C(1ޔXoz#=њkJ7֏KFr"YC ńS#}kN4Ap^8{R}u.C)XSz1fj/ )^Ҹ]5"j E³w  Bqybi Iّk E3%H vfɿSC}HcvW>e+O4 G.u>ʐW,>i.NZ{M5-iLGu3!_ 2U9 E9g%+DsH%Bq$o[l;mmK47WVpD$3[U`N4`h|caNδTwf*z#Ɗ&+LX5.`Y:Γmm-]9]yᅈc&[@;pd Es$%[{KcwiW@`0 0, argInt(ttypes, 'CX_PRIORITY', 0), 0, broker_mp, 0]:newbroker, NIL) -> argString() works just like argInt(), except it returns a pointer to a -> string rather than an integer. In the example below, if there is no -> ToolType 'HOTKEY', the function returns a pointer to 'rawkey control esc'. hotkey:=argString(ttypes, 'HOTKEY', 'rawkey control esc') -> CxFilter() is a macro that creates a filter CxObject. This filter passes -> input events that match the string pointed to by hotkey. filter:=CxFilter(hotkey) -> Add a CxObject to another's personal list AttachCxObj(broker, filter) -> CxSender() creates a sender CxObject. Every time a sender gets a -> CxMessage, it sends a new CxMessage to the port pointed to in the first -> argument. CxSender()'s second argument will be the ID of any CxMessages -> the sender sends to the port. The data pointer associated with the -> CxMessage will point to a *COPY* of the InputEvent structure associated -> with the orginal CxMessage. sender:=CxSender(broker_mp, EVT_HOTKEY) AttachCxObj(filter, sender) -> CxTranslate() creates a translate CxObject. When a translate CxObject -> gets a CxMessage, it deletes the original CxMessage and adds a new input -> event to the input.device's input stream after the Commodities input -> handler. CxTranslate's argument points to an InputEvent structure from -> which to create the new input event. In this example, the pointer is NIL, -> meaning no new event should be introduced, which causes any event that -> reaches this object to disappear from the input stream. translate:=CxTranslate(NIL) AttachCxObj(filter, translate) -> CxObjError() is a commodities.library function that returns the internal -> accumulated error code of a CxObject. IF CxObjError(filter)<>FALSE THEN Raise(ERR_CXERR) ActivateCxObj(broker, TRUE) processMsg() EXCEPT DO -> DeleteCxObjAll() is a commodities.library function that not only deletes -> the CxObject pointed to in its argument, but it deletes all of the -> CxObjects that are attached to it. IF broker THEN DeleteCxObjAll(broker) IF broker_mp WHILE msg:=GetMsg(broker_mp) DO ReplyMsg(msg) DeleteMsgPort(broker_mp) -> E-Note: C version incorrectly uses DeletePort() ENDIF IF ttypes THEN argArrayDone() IF iconbase THEN CloseLibrary(iconbase) IF cxbase THEN CloseLibrary(cxbase) SELECT exception CASE ERR_ARG; WriteF('Error: Could not init arg array\n') CASE ERR_BRKR; WriteF('Error: Could not create broker\n') CASE ERR_CRCX; WriteF('Error: Could not create CX object\n') CASE ERR_CXERR; WriteF('Error: Could not activate broker\n') CASE ERR_LIB; WriteF('Error: Could not open required library\n') CASE ERR_PORT; WriteF('Error: Could not create message port\n') ENDSELECT ENDPROC PROC processMsg() DEF msg, sigrcvd, msgid, msgtype, done=FALSE REPEAT sigrcvd:=Wait(SIGBREAKF_CTRL_C OR cxsigflag) WHILE msg:=GetMsg(broker_mp) msgid:=CxMsgID(msg) msgtype:=CxMsgType(msg) ReplyMsg(msg) SELECT msgtype CASE CXM_IEVENT WriteF('A CXM_IEVENT, ') SELECT msgid CASE EVT_HOTKEY -> We got the message from the sender CxObject WriteF('You hit the HotKey.\n') DEFAULT WriteF('unknown.\n') ENDSELECT CASE CXM_COMMAND WriteF('A command: ') SELECT msgid CASE CXCMD_DISABLE WriteF('CXCMD_DISABLE\n') ActivateCxObj(broker, FALSE) CASE CXCMD_ENABLE WriteF('CXCMD_ENABLE\n') ActivateCxObj(broker, TRUE) CASE CXCMD_KILL WriteF('CXCMD_KILL\n') done:=TRUE CASE CXCMD_UNIQUE -> Commodities Exchange can be told not only to refuse to launch a -> commodity with a name already in use but also can notify the -> already running commodity that it happened. It does this by -> sending a CXM_COMMAND with the ID set to CXMCMD_UNIQUE. If the -> user tries to run a windowless commodity that is already running, -> the user wants the commodity to shut down. WriteF('CXCMD_UNIQUE\n') done:=TRUE DEFAULT WriteF('Unknown msgid\n') ENDSELECT DEFAULT WriteF('Unknown msgtype\n') ENDSELECT ENDWHILE IF sigrcvd AND SIGBREAKF_CTRL_C WriteF('CTRL C signal break\n') done:=TRUE ENDIF UNTIL done ENDPROC