2 Copyright © 1999, David Le Corfec.
3 Copyright © 2002, The AROS Development Team.
10 * notify2.c : Test the MUI_Notify class, and show how to connect actions
11 * to attributes changes.
12 * Copied from a MUI2C example. Greetings to Jason Birch, MUI2C author.
15 #include <exec/types.h>
17 //#include <libraries/mui.h>
18 #include <clib/alib_protos.h>
19 #include <proto/exec.h>
20 #include <proto/intuition.h>
21 #include <proto/muimaster.h>
23 #include <libraries/mui.h>
25 #include <proto/utility.h>
28 struct Library
*MUIMasterBase
;
35 /* On AmigaOS we build a fake library base, because it's not compiled as sharedlibrary yet */
36 #include "muimaster_intern.h"
38 int openmuimaster(void)
40 static struct MUIMasterBase_intern MUIMasterBase_instance
;
41 MUIMasterBase
= (struct Library
*)&MUIMasterBase_instance
;
43 MUIMasterBase_instance
.sysbase
= *((struct ExecBase
**)4);
44 MUIMasterBase_instance
.dosbase
= OpenLibrary("dos.library",37);
45 MUIMasterBase_instance
.utilitybase
= OpenLibrary("utility.library",37);
46 MUIMasterBase_instance
.aslbase
= OpenLibrary("asl.library",37);
47 MUIMasterBase_instance
.gfxbase
= OpenLibrary("graphics.library",37);
48 MUIMasterBase_instance
.layersbase
= OpenLibrary("layers.library",37);
49 MUIMasterBase_instance
.intuibase
= OpenLibrary("intuition.library",37);
50 MUIMasterBase_instance
.cxbase
= OpenLibrary("commodities.library",37);
51 MUIMasterBase_instance
.keymapbase
= OpenLibrary("keymap.library",37);
52 __zune_prefs_init(&__zprefs
);
57 void closemuimaster(void)
63 int openmuimaster(void)
65 if ((MUIMasterBase
= OpenLibrary("muimaster.library", 0))) return 1;
69 void closemuimaster(void)
71 if (MUIMasterBase
) CloseLibrary(MUIMasterBase
);
77 #define TAGBASE (TAG_USER | (9853 << 16))
81 * This example uses 2 custom classes, Test and ExtendedTest.
82 * Hierarchy : MUIC_Notify <-- Test <-- ExtendedTest.
83 * Setting public attributes of these classes will trigger notifications.
87 /* Test : This is a simple class with 2 numeric attributes A and B. */
90 * Private data structure for instances of class Test.
98 * Public attributes of class Test
104 MUIA_Test_A
= TAGBASE
, /* isg */
105 MUIA_Test_B
, /* isg */
109 * Public methods of class Test
112 MUIM_Test_Print
= TAGBASE
,
117 * Special parameter structures for Test methods.
119 struct MUIP_Test_Print
{ ULONG MsgID
; };
120 struct MUIP_Test_GetBoth
{ ULONG MsgID
; int *x
; int *y
;};
125 * Constructor of Test object.
128 Test_New(struct IClass
*cl
, Object
*obj
, struct opSet
*msg
)
130 struct TestData
*data
;
133 * Call constructor of superclass.
135 obj
= (Object
*)DoSuperMethodA(cl
, obj
, (Msg
)msg
);
139 * Set default values to attributes.
141 data
= INST_DATA(cl
, obj
);
147 if ((tag
= FindTagItem(MUIA_Test_A
, msg
->ops_AttrList
)))
148 data
->a
= tag
->ti_Data
;
149 if ((tag
= FindTagItem(MUIA_Test_B
, msg
->ops_AttrList
)))
150 data
->b
= tag
->ti_Data
;
152 * Return newly constructed object.
159 * Setting public attributes. The tags in the message may not be ours,
160 * so do't forget to pass them to the super class.
163 Test_Set(struct IClass
*cl
, Object
*obj
, struct opSet
*msg
)
165 struct TestData
*data
= INST_DATA(cl
, obj
);
166 struct TagItem
*tags
= msg
->ops_AttrList
;
169 /* There are many ways to find out what tag items provided by set()
170 ** we do know. The best way should be using NextTagItem() and simply
171 ** browsing through the list.
173 while ((tag
= NextTagItem(&tags
)) != NULL
)
178 data
->a
= tag
->ti_Data
;
181 data
->b
= tag
->ti_Data
;
186 * To handle unkown attributes and notifications.
188 return(DoSuperMethodA(cl
, obj
, (Msg
) msg
));
193 * Getting public attributes.
196 Test_Get(struct IClass
*cl
, Object
*obj
, struct opGet
*msg
)
198 struct TestData
*data
= INST_DATA(cl
, obj
);
199 #define STORE *(msg->opg_Storage)
201 switch(msg
->opg_AttrID
)
204 STORE
= (ULONG
) data
->a
;
207 STORE
= (ULONG
) data
->b
;
211 /* Our handler didn't understand the attribute, we simply pass
212 ** it to our superclass now.
214 return(DoSuperMethodA(cl
, obj
, (Msg
) msg
));
220 * Special get method to get both attributes.
223 Test_GetBoth(struct IClass
*cl
, Object
*obj
, struct MUIP_Test_GetBoth
*msg
)
225 struct TestData
*data
= INST_DATA(cl
, obj
);
234 * Print attributes value.
237 Test_Print(struct IClass
*cl
, Object
*obj
, struct MUIP_Test_Print
*msg
)
239 struct TestData
*data
= INST_DATA(cl
, obj
);
240 printf("A value: %d. B value: %d.\n", data
->a
, data
->b
);
246 * Test class method dispatcher.
249 __asm IPTR
Test_Dispatcher(register __a0 Class
*cl
, register __a2 Object
*obj
, register __a1 Msg msg
)
251 AROS_UFH3S(IPTR
, Test_Dispatcher
,
252 AROS_UFHA(Class
*, cl
, A0
),
253 AROS_UFHA(Object
*, obj
, A2
),
254 AROS_UFHA(Msg
, msg
, A1
))
260 * Watch out for methods we do understand.
262 switch (msg
->MethodID
)
264 /* Whenever an object shall be created using NewObject(), it will be
265 ** sent a OM_NEW method.
268 return(Test_New(cl
, obj
, (struct opSet
*) msg
));
270 return(Test_Set(cl
, obj
, (struct opSet
*)msg
));
272 return(Test_Get(cl
, obj
, (struct opGet
*)msg
));
273 case MUIM_Test_Print
:
274 return(Test_Print(cl
, obj
, (APTR
)msg
));
275 case MUIM_Test_GetBoth
:
276 return(Test_GetBoth(cl
, obj
, (APTR
)msg
));
279 * We didn't understand the last method, so call our superclass.
281 return(DoSuperMethodA(cl
, obj
, msg
));
287 /******************************************************************************/
288 /* Extended Test : holds statistics about Test attributes, and update them */
289 /* automatically when they change. */
290 /******************************************************************************/
293 * Public methods of class ExtendedTest
296 MUIM_ExtendedTest_Update
= TAGBASE
+11,
297 MUIM_ExtendedTest_Print
,
300 struct MUIP_ExtendedTest_Update
{ ULONG MsgID
; };
301 struct MUIP_ExtendedTest_Print
{ ULONG MsgID
; };
304 * Internal attributes of class ExtendedTest
306 struct ExtendedTestData
{
313 * Constructor of ExtendedTest object.
316 ExtendedTest_New(struct IClass
*cl
, Object
*obj
, struct opSet
*msg
)
318 struct ExtendedTestData
*data
;
320 * Call constructor of superclass.
322 obj
= (Object
*)DoSuperMethodA(cl
, obj
, (Msg
)msg
);
326 * Set default values to attributes.
328 data
= INST_DATA(cl
, obj
);
329 DoMethod(obj
, MUIM_ExtendedTest_Update
);
332 * Setup notifications on our attributes.
334 DoMethod(obj
, MUIM_Notify
,
335 MUIA_Test_A
, /* attribute to watch */
336 MUIV_EveryTime
, /* notify when setting to everything */
337 (IPTR
)obj
, /* object to call on notification */
338 1, /* number of parameters following */
339 MUIM_ExtendedTest_Update
); /* method to invoke */
341 DoMethod(obj
, MUIM_Notify
,
346 MUIM_ExtendedTest_Update
);
348 * Return newly constructed object.
355 * Recalculate sum and average.
358 ExtendedTest_Update(struct IClass
*cl
, Object
*obj
,
359 struct MUIP_ExtendedTest_Update
*noMsg
)
361 struct ExtendedTestData
*data
= INST_DATA(cl
, obj
);
365 DoMethod(obj
, MUIM_Test_GetBoth
, (IPTR
)&a
, (IPTR
)&b
);
367 data
->average
= (a
+ b
) / 2;
377 ExtendedTest_Print(struct IClass
*cl
, Object
*obj
,
378 struct MUIP_ExtendedTest_Print
*noMsg
)
380 struct ExtendedTestData
*data
= INST_DATA(cl
, obj
);
382 DoMethod(obj
, MUIM_Test_Print
);
383 printf("Sum: %d. Average: %d. Usecount: %d.\n",
393 * ExtendedTest class method dispatcher.
396 __asm IPTR
ExtendedTest_Dispatcher(register __a0 Class
*cl
, register __a2 Object
*obj
, register __a1 Msg msg
)
398 AROS_UFH3S(IPTR
, ExtendedTest_Dispatcher
,
399 AROS_UFHA(Class
*, cl
, A0
),
400 AROS_UFHA(Object
*, obj
, A2
),
401 AROS_UFHA(Msg
, msg
, A1
))
407 * Watch out for methods we do understand.
409 switch (msg
->MethodID
)
411 /* Whenever an object shall be created using NewObject(), it will be
412 ** sent a OM_NEW method.
415 return(ExtendedTest_New(cl
, obj
, (struct opSet
*) msg
));
416 case MUIM_ExtendedTest_Print
:
417 return(ExtendedTest_Print(cl
, obj
, (APTR
)msg
));
418 case MUIM_ExtendedTest_Update
:
419 return(ExtendedTest_Update(cl
, obj
, (APTR
)msg
));
422 * We didn't understand the last method, so call our superclass.
424 return(DoSuperMethodA(cl
, obj
, msg
));
431 * Create both classes, create an ExtendedTest object,
432 * changes attributes and print values.
437 struct MUI_CustomClass
*testClass
;
438 struct MUI_CustomClass
*extendedTestClass
;
441 if (!openmuimaster()) return 20;
443 testClass
= MUI_CreateCustomClass(NULL
, MUIC_Notify
, NULL
,
444 sizeof(struct TestData
),
448 printf("cannot create Test class\n");
452 extendedTestClass
= MUI_CreateCustomClass(NULL
, NULL
, testClass
,
453 sizeof(struct ExtendedTestData
),
454 ExtendedTest_Dispatcher
);
455 if (!extendedTestClass
)
457 MUI_DeleteCustomClass(testClass
);
458 printf("cannot create ExtendedTest class\n");
463 obj
= NewObject(extendedTestClass
->mcc_Class
, NULL
,
472 printf("\nSum and Average will be automatically calculated"
473 " after each change of A and B values.\n\n");
474 printf("- Show the contents of the object:\n");
475 DoMethod(obj
, MUIM_ExtendedTest_Print
);
477 printf("\n- Set the A attribute of the object to 5 and check its new contents:\n");
478 set(obj
, MUIA_Test_A
, 5);
479 DoMethod(obj
, MUIM_ExtendedTest_Print
);
481 printf("\n- Set the B attribute of the object to 10 and check its new contents:\n");
482 set(obj
, MUIA_Test_B
, 10);
483 DoMethod(obj
, MUIM_ExtendedTest_Print
);
485 printf("\n- Get the A and B attributes using MUIP structure:\n");
486 DoMethod(obj
, MUIM_Test_GetBoth
, (IPTR
)&x
, (IPTR
)&y
);
487 printf("Values returned: %d %d.\n", x
, y
);
490 MUI_DeleteCustomClass(extendedTestClass
);
491 MUI_DeleteCustomClass(testClass
);