added concrete implementations of putc(), getc(), getchar() and gets()
[tangerine.git] / workbench / demos / Zune / mui_notify2.c
blob5e725aeb01747bdd387716253c3ac09de345b820
1 /*
2 Copyright © 1999, David Le Corfec.
3 Copyright © 2002, The AROS Development Team.
4 All rights reserved.
6 $Id$
7 */
9 /*
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>
22 #ifdef __AROS__
23 #include <libraries/mui.h>
24 #endif
25 #include <proto/utility.h>
26 #include <stdio.h>
28 struct Library *MUIMasterBase;
30 #ifndef __AROS__
32 #include <mui.h>
33 #undef SysBase
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);
54 return 1;
57 void closemuimaster(void)
61 #else
63 int openmuimaster(void)
65 if ((MUIMasterBase = OpenLibrary("muimaster.library", 0))) return 1;
66 return 0;
69 void closemuimaster(void)
71 if (MUIMasterBase) CloseLibrary(MUIMasterBase);
74 #endif
76 #ifndef TAGBASE
77 #define TAGBASE (TAG_USER | (9853 << 16))
78 #endif
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.
92 struct TestData {
93 int a;
94 int b;
98 * Public attributes of class Test
99 * i : init
100 * s : set
101 * g : get
103 enum {
104 MUIA_Test_A = TAGBASE, /* isg */
105 MUIA_Test_B, /* isg */
109 * Public methods of class Test
111 enum {
112 MUIM_Test_Print = TAGBASE,
113 MUIM_Test_GetBoth,
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.
127 static ULONG
128 Test_New(struct IClass *cl, Object *obj, struct opSet *msg)
130 struct TestData *data;
131 struct TagItem *tag;
133 * Call constructor of superclass.
135 obj = (Object *)DoSuperMethodA(cl, obj, (Msg)msg);
136 if (!obj)
137 return 0;
139 * Set default values to attributes.
141 data = INST_DATA(cl, obj);
142 data->a = 0;
143 data->b = 0;
145 * Init attributes.
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.
154 return (ULONG)obj;
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.
162 static ULONG
163 Test_Set(struct IClass *cl, Object *obj, struct opSet *msg)
165 struct TestData *data = INST_DATA(cl, obj);
166 const struct TagItem *tags = msg->ops_AttrList;
167 struct TagItem *tag;
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)
175 switch (tag->ti_Tag)
177 case MUIA_Test_A:
178 data->a = tag->ti_Data;
179 break;
180 case MUIA_Test_B:
181 data->b = tag->ti_Data;
182 break;
186 * To handle unkown attributes and notifications.
188 return(DoSuperMethodA(cl, obj, (Msg) msg));
193 * Getting public attributes.
195 static ULONG
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)
203 case MUIA_Test_A:
204 STORE = (ULONG) data->a;
205 return(TRUE);
206 case MUIA_Test_B:
207 STORE = (ULONG) data->b;
208 return(TRUE);
211 /* Our handler didn't understand the attribute, we simply pass
212 ** it to our superclass now.
214 return(DoSuperMethodA(cl, obj, (Msg) msg));
215 #undef STORE
220 * Special get method to get both attributes.
222 static ULONG
223 Test_GetBoth(struct IClass *cl, Object *obj, struct MUIP_Test_GetBoth *msg)
225 struct TestData *data = INST_DATA(cl, obj);
227 *(msg->x) = data->a;
228 *(msg->y) = data->b;
229 return TRUE;
234 * Print attributes value.
236 static ULONG
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);
241 return TRUE;
246 * Test class method dispatcher.
248 #ifndef __AROS__
249 __asm IPTR Test_Dispatcher(register __a0 Class *cl, register __a2 Object *obj, register __a1 Msg msg)
250 #else
251 AROS_UFH3S(IPTR, Test_Dispatcher,
252 AROS_UFHA(Class *, cl, A0),
253 AROS_UFHA(Object *, obj, A2),
254 AROS_UFHA(Msg , msg, A1))
255 #endif
257 AROS_USERFUNC_INIT
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.
267 case OM_NEW:
268 return(Test_New(cl, obj, (struct opSet *) msg));
269 case OM_SET:
270 return(Test_Set(cl, obj, (struct opSet *)msg));
271 case OM_GET:
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));
283 AROS_USERFUNC_EXIT
287 /******************************************************************************/
288 /* Extended Test : holds statistics about Test attributes, and update them */
289 /* automatically when they change. */
290 /******************************************************************************/
293 * Public methods of class ExtendedTest
295 enum {
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 {
307 int sum;
308 int average;
309 int usecount;
313 * Constructor of ExtendedTest object.
315 static ULONG
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);
323 if (!obj)
324 return 0;
326 * Set default values to attributes.
328 data = INST_DATA(cl, obj);
329 DoMethod(obj, MUIM_ExtendedTest_Update);
330 data->usecount = 0;
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,
342 MUIA_Test_B,
343 MUIV_EveryTime,
344 (IPTR)obj,
346 MUIM_ExtendedTest_Update);
348 * Return newly constructed object.
350 return (ULONG)obj;
355 * Recalculate sum and average.
357 static ULONG
358 ExtendedTest_Update(struct IClass *cl, Object *obj,
359 struct MUIP_ExtendedTest_Update *noMsg)
361 struct ExtendedTestData *data = INST_DATA(cl, obj);
362 int a;
363 int b;
365 DoMethod(obj, MUIM_Test_GetBoth, (IPTR)&a, (IPTR)&b);
366 data->sum = a + b;
367 data->average = (a + b) / 2;
368 data->usecount++;
369 return TRUE;
374 * Print values.
376 static ULONG
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",
384 data->sum,
385 data->average,
386 data->usecount);
388 return FALSE;
393 * ExtendedTest class method dispatcher.
395 #ifndef __AROS__
396 __asm IPTR ExtendedTest_Dispatcher(register __a0 Class *cl, register __a2 Object *obj, register __a1 Msg msg)
397 #else
398 AROS_UFH3S(IPTR, ExtendedTest_Dispatcher,
399 AROS_UFHA(Class *, cl, A0),
400 AROS_UFHA(Object *, obj, A2),
401 AROS_UFHA(Msg , msg, A1))
402 #endif
404 AROS_USERFUNC_INIT
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.
414 case OM_NEW:
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));
426 AROS_USERFUNC_EXIT
431 * Create both classes, create an ExtendedTest object,
432 * changes attributes and print values.
434 int main (void)
436 Object *obj;
437 struct MUI_CustomClass *testClass;
438 struct MUI_CustomClass *extendedTestClass;
439 int result = 0;
441 if (!openmuimaster()) return 20;
443 testClass = MUI_CreateCustomClass(NULL, MUIC_Notify, NULL,
444 sizeof(struct TestData),
445 Test_Dispatcher);
446 if (!testClass)
448 printf("cannot create Test class\n");
449 result = 5;
450 goto error;
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");
459 result = 5;
460 goto error;
463 obj = NewObject(extendedTestClass->mcc_Class, NULL,
464 MUIA_Test_A, 10,
465 MUIA_Test_B, 20,
466 TAG_DONE);
467 if (obj)
469 int x;
470 int y;
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);
488 DisposeObject(obj);
490 MUI_DeleteCustomClass(extendedTestClass);
491 MUI_DeleteCustomClass(testClass);
493 error:
495 closemuimaster();
497 return result;