added concrete implementations of putc(), getc(), getchar() and gets()
[tangerine.git] / workbench / classes / zune / betterstring / mcc / Dispatcher.c
blobe6d3928fa93a34d49a6a8c5896ae71b5aeb8b3fd
1 /***************************************************************************
3 BetterString.mcc - A better String gadget MUI Custom Class
4 Copyright (C) 1997-2000 Allan Odgaard
5 Copyright (C) 2005 by BetterString.mcc Open Source Team
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 BetterString class Support Site: http://www.sf.net/projects/bstring-mcc/
19 $Id$
21 ***************************************************************************/
23 #include <string.h>
25 #include <clib/alib_protos.h>
26 #include <libraries/gadtools.h>
27 #include <libraries/mui.h>
28 #include <graphics/gfxbase.h>
29 #include <proto/graphics.h>
30 #include <proto/exec.h>
31 #include <proto/intuition.h>
32 #include <proto/locale.h>
33 #include <proto/muimaster.h>
34 #include <proto/utility.h>
36 #include "BetterString_mcc.h"
37 #include "private.h"
40 struct NewMenu PopupMenuData[] =
42 NM_TITLE, "Stringgadget", NULL, 0L, 0, (APTR)0,
43 NM_ITEM, "Cut", NULL, 0L, 0, (APTR)1,
44 NM_ITEM, "Copy", NULL, 0L, 0, (APTR)2,
45 NM_ITEM, "Paste", NULL, 0L, 0, (APTR)3,
46 NM_ITEM, NM_BARLABEL, NULL, 0L, 0, (APTR)0,
47 NM_ITEM, "Undo", NULL, 0L, 0, (APTR)4,
48 NM_ITEM, "Redo", NULL, 0L, 0, (APTR)5,
49 NM_ITEM, "Original", NULL, 0L, 0, (APTR)6,
50 NM_ITEM, NM_BARLABEL, NULL, 0L, 0, (APTR)0,
51 NM_ITEM, "Complete path", NULL, 0L, 0, (APTR)7,
52 NM_ITEM, "Toggle case", NULL, 0L, 0, (APTR)0,
53 NM_SUB, "Character", NULL, 0L, 0, (APTR)8,
54 NM_SUB, "Word", NULL, 0L, 0, (APTR)9,
55 NM_ITEM, "Arithmetic", NULL, 0L, 0, (APTR)0,
56 NM_SUB, "Increase", NULL, 0L, 0, (APTR)10,
57 NM_SUB, "Decrease", NULL, 0L, 0, (APTR)11,
58 NM_SUB, "To decimal", NULL, 0L, 0, (APTR)12,
59 NM_SUB, "To hexadecimal", NULL, 0L, 0, (APTR)13,
60 NM_END, NULL, NULL, 0L, 0, (APTR)0
64 #ifndef MUIA_CustomBackfill
65 #define MUIA_CustomBackfill 0x80420a63
66 #endif
67 #ifndef MUIM_Backfill
68 #define MUIM_Backfill 0x80428d73
69 struct MUIP_Backfill { ULONG MethodID; LONG left; LONG top; LONG right; LONG bottom; LONG xoffset; LONG yoffset; };
70 #endif
72 ULONG New(struct IClass *cl, Object *obj, struct opSet *msg)
74 if((obj = (Object *)DoSuperMethodA(cl, obj, (Msg)msg)))
76 struct InstData *data = (struct InstData *)INST_DATA(cl, obj);
78 if((data->Pool = CreatePool(MEMF_ANY, 512, 256)))
80 // kprintf("OM_NEW by %s\n", FindTask(NULL)->tc_Node.ln_Name);
82 data->locale = OpenLocale(NULL);
83 data->Contents = (STRPTR)MyAllocPooled(data->Pool, 40);
84 *data->Contents = '\0';
86 // data->PopupMenu = MUI_MakeObject(MUIO_MenustripNM, PopupMenuData, NULL);
87 SetAttrs(obj, MUIA_FillArea, FALSE,
88 // MUIA_ContextMenu, data->PopupMenu,
89 // MUIA_CustomBackfill, TRUE,
90 TAG_DONE );
92 if(FindTagItem(MUIA_Font, msg->ops_AttrList))
93 data->Flags |= FLG_OwnFont;
95 if(FindTagItem(MUIA_Background, msg->ops_AttrList))
96 data->Flags |= FLG_OwnBackground;
99 struct TagItem *tag;
101 if((tag = FindTagItem(MUIA_Frame, msg->ops_AttrList)))
103 if(tag->ti_Data == MUIV_Frame_String)
104 data->Flags |= FLG_SetFrame;
108 msg->MethodID = OM_SET;
109 Set(cl, obj, (struct opSet *)msg);
110 msg->MethodID = OM_NEW;
111 data->BufferPos = 0;
113 return((ULONG)obj);
115 CoerceMethod(cl, obj, OM_DISPOSE);
117 return(FALSE);
120 ULONG Dispose(struct IClass *cl, Object *obj, Msg msg)
122 struct InstData *data = (struct InstData *)INST_DATA(cl, obj);
124 // kprintf("OM_DISPOSE by %s\n", FindTask(NULL)->tc_Node.ln_Name);
125 /* if(data->PopupMenu)
126 MUI_DisposeObject(data->PopupMenu);
127 */ DeletePool(data->Pool);
128 CloseLocale(data->locale);
129 return(DoSuperMethodA(cl, obj, msg));
132 ULONG Setup(struct IClass *cl, Object *obj, struct MUI_RenderInfo *rinfo)
134 struct InstData *data = (struct InstData *)INST_DATA(cl, obj);
136 InitConfig(obj, data);
137 if(DoSuperMethodA(cl, obj, (Msg)rinfo))
139 /* ULONG color;
140 if(DoMethod(obj, MUIM_GetConfigItem, 132, &color))
142 UBYTE image[6];
144 strcpy(image, "2:");
145 strcat(image, (STRPTR)color);
146 set(obj, MUIA_Background, image);
150 _flags(obj) |= (1<<7);
152 data->ehnode.ehn_Priority = 0;
153 data->ehnode.ehn_Flags = MUI_EHF_GUIMODE;
154 data->ehnode.ehn_Object = obj;
155 data->ehnode.ehn_Class = cl;
156 data->ehnode.ehn_Events = IDCMP_MOUSEBUTTONS | IDCMP_RAWKEY;
157 DoMethod(_win(obj), MUIM_Window_AddEventHandler, &data->ehnode);
159 return(TRUE);
161 return(FALSE);
164 ULONG Cleanup(struct IClass *cl, Object *obj, Msg msg)
166 struct InstData *data = (struct InstData *)INST_DATA(cl, obj);
168 DoMethod(_win(obj), MUIM_Window_RemEventHandler, &data->ehnode);
170 /* if(data->Flags & FLG_Active)
172 data->Flags &= ~FLG_Active;
173 data->ehnode.ehn_Events &= ~IDCMP_MOUSEMOVE;
175 if(!(data->Flags & FLG_OwnBackground))
176 set(obj, MUIA_Background, data->InactiveBackground);
179 FreeConfig(muiRenderInfo(obj), data);
180 return(DoSuperMethodA(cl, obj, msg));
183 ULONG AskMinMax (struct IClass *cl, Object *obj, struct MUIP_AskMinMax *msg)
185 struct InstData *data = (struct InstData *)INST_DATA(cl, obj);
186 WORD Height;
188 DoSuperMethodA(cl, obj, (Msg)msg);
190 struct TextFont *font = data->Font ? data->Font : muiAreaData(obj)->mad_Font;
191 Height = font->tf_YSize;
192 msg->MinMaxInfo->MinHeight += Height;
193 msg->MinMaxInfo->DefHeight += Height;
194 msg->MinMaxInfo->MaxHeight += Height;
196 if(data->Width)
198 ULONG width = data->Width * MyTextLength(font, "n", 1);
200 msg->MinMaxInfo->MinWidth += width;
201 msg->MinMaxInfo->DefWidth += width;
202 msg->MinMaxInfo->MaxWidth += width;
204 else
206 msg->MinMaxInfo->MinWidth += 10;
207 msg->MinMaxInfo->DefWidth += 100;
208 msg->MinMaxInfo->MaxWidth = MUI_MAXMAX;
210 return(0);
213 ULONG Show(struct IClass *cl, Object *obj, Msg msg)
215 struct InstData *data = (struct InstData *)INST_DATA(cl, obj);
216 struct MUI_AreaData *ad = muiAreaData(obj);
217 struct BitMap *friendBMp = muiRenderInfo(obj)->mri_RastPort->BitMap;
218 WORD width, height, depth;
219 struct TextFont *font = data->Font ? data->Font : ad->mad_Font;
221 DoSuperMethodA(cl, obj, msg);
223 width = ad->mad_Box.Width - ad->mad_subwidth;
224 height = font->tf_YSize;
225 depth = ((struct Library *)GfxBase)->lib_Version >= 39 ? GetBitMapAttr(friendBMp, BMA_DEPTH) : friendBMp->Depth;
227 InitRastPort(&data->rport);
228 data->rport.BitMap = MUIG_AllocBitMap(width+40, height, depth, (ULONG)NULL, friendBMp);
229 SetFont(&data->rport, font);
230 SetDrMd(&data->rport, JAM1);
232 data->Flags |= FLG_Shown;
233 return(TRUE);
236 ULONG Hide(struct IClass *cl, Object *obj, Msg msg)
238 struct InstData *data = (struct InstData *)INST_DATA(cl, obj);
240 #ifdef __AROS__
241 if (data->Flags & FLG_Shown)
243 DeinitRastPort(&data->rport);
245 #endif
247 data->Flags &= ~FLG_Shown;
248 MUIG_FreeBitMap(data->rport.BitMap);
249 return(DoSuperMethodA(cl, obj, msg));
252 ULONG mDraw(struct IClass *cl, Object *obj, struct MUIP_Draw *msg)
254 DoSuperMethodA(cl, obj, (Msg)msg);
256 if(msg->flags & MADF_DRAWUPDATE)
258 PrintString(cl, obj);
261 if(msg->flags & MADF_DRAWOBJECT)
263 PrintString(cl, obj);
266 return(0);
269 DISPATCHERPROTO(_Dispatcher)
271 DISPATCHER_INIT
273 struct InstData *data = (struct InstData *)INST_DATA(cl, obj);
274 ULONG result = TRUE;
276 switch(msg->MethodID)
278 case OM_NEW:
279 return(New(cl, obj, (struct opSet *)msg));
280 case MUIM_Setup:
281 return(Setup(cl, obj, (struct MUI_RenderInfo *)msg));
282 case MUIM_Show:
283 return(Show(cl, obj, msg));
284 case MUIM_AskMinMax:
285 return(AskMinMax(cl, obj, (struct MUIP_AskMinMax *)msg));
286 case MUIM_Draw:
287 return(mDraw(cl, obj, (struct MUIP_Draw *)msg));
288 case OM_GET:
289 return(Get(cl, obj, (struct opGet *)msg));
290 case OM_SET:
291 Set(cl, obj, (struct opSet *)msg);
292 return(DoSuperMethodA(cl, obj, msg));
293 case MUIM_HandleEvent:
295 if(data->Flags & FLG_Ghosted || !(data->Flags & FLG_Shown))
297 result = 0;
299 else
301 ULONG display_pos = data->DisplayPos;
302 result = HandleInput(cl, obj, (struct MUIP_HandleEvent *)msg);
303 if(display_pos != data->DisplayPos)
304 SetAttrs(obj, MUIA_String_DisplayPos, data->DisplayPos, TAG_DONE);
306 if(!result && data->ForwardObject)
308 ULONG attr = 0;
310 switch(((struct MUIP_HandleEvent *)msg)->muikey)
312 case MUIKEY_TOP:
313 attr = MUIV_List_Active_Top;
314 break;
315 case MUIKEY_BOTTOM:
316 attr = MUIV_List_Active_Bottom;
317 break;
318 case MUIKEY_UP:
319 attr = MUIV_List_Active_Up;
320 break;
321 case MUIKEY_DOWN:
322 attr = MUIV_List_Active_Down;
323 break;
324 case MUIKEY_PAGEUP:
325 attr = MUIV_List_Active_PageUp;
326 break;
327 case MUIKEY_PAGEDOWN:
328 attr = MUIV_List_Active_PageDown;
329 break;
331 if(attr)
333 SetAttrs(data->ForwardObject, MUIA_List_Active, attr, TAG_DONE);
334 result = MUI_EventHandlerRC_Eat;
339 break;
341 case MUIM_GoActive:
343 data->Flags |= FLG_Active;
344 /* DoMethod(_win(obj), MUIM_Window_RemEventHandler, &data->ehnode);
345 data->ehnode.ehn_Events |= IDCMP_RAWKEY;
346 DoMethod(_win(obj), MUIM_Window_AddEventHandler, &data->ehnode);
348 if(data->Original)
349 MyFreePooled(data->Pool, (APTR)data->Original);
350 if((data->Original = (STRPTR)MyAllocPooled(data->Pool, strlen(data->Contents)+1)))
351 strcpy(data->Original, data->Contents);
353 if(!(data->Flags & FLG_OwnBackground))
354 set(obj, MUIA_Background, data->ActiveBackground);
355 else MUI_Redraw(obj, MADF_DRAWUPDATE);
357 break;
359 case MUIM_GoInactive:
361 // kprintf("0x%lx: GoInactive\n", obj);
362 data->Flags &= ~FLG_Active;
363 DoMethod(_win(obj), MUIM_Window_RemEventHandler, &data->ehnode);
364 data->ehnode.ehn_Events &= ~IDCMP_MOUSEMOVE;
365 DoMethod(_win(obj), MUIM_Window_AddEventHandler, &data->ehnode);
367 if(!(data->Flags & FLG_OwnBackground))
368 set(obj, MUIA_Background, data->InactiveBackground);
369 else MUI_Redraw(obj, MADF_DRAWUPDATE);
371 break;
373 case MUIM_Hide:
374 return(Hide(cl, obj, msg));
375 case MUIM_Cleanup:
376 return(Cleanup(cl, obj, msg));
377 case OM_DISPOSE:
378 return(Dispose(cl, obj, msg));
380 case MUIM_Export:
382 ULONG id;
383 struct MUIP_Export *exp_msg = (struct MUIP_Export *)msg;
385 if((id = (muiNotifyData(obj)->mnd_ObjectID)))
386 DoMethod(exp_msg->dataspace, MUIM_Dataspace_Add, data->Contents, strlen(data->Contents)+1, id);
388 result = 0;
390 break;
392 case MUIM_Import:
394 ULONG id;
395 struct MUIP_Import *imp_msg = (struct MUIP_Import *)msg;
397 if((id = (muiNotifyData(obj)->mnd_ObjectID)))
399 STRPTR contents = (STRPTR)DoMethod(imp_msg->dataspace, MUIM_Dataspace_Find, id);
401 // if(contents)
402 set(obj, MUIA_String_Contents, contents);
404 result = 0;
406 break;
408 case MUIM_BetterString_ClearSelected:
410 DeleteBlock(data);
411 data->Flags &= ~FLG_BlockEnabled;
413 break;
415 case MUIM_BetterString_Insert:
417 UWORD pos;
418 struct MUIP_BetterString_Insert *ins_msg = (struct MUIP_BetterString_Insert *)msg;
420 switch(ins_msg->pos)
422 /* case MUIV_BetterString_Insert_StartOfString:
423 pos = 0;
424 break;
426 case MUIV_BetterString_Insert_EndOfString:
427 pos = strlen(data->Contents);
428 break;
430 case MUIV_BetterString_Insert_BufferPos:
431 pos = data->BufferPos;
432 break;
434 default:
435 pos = ins_msg->pos;
436 break;
438 Overwrite(ins_msg->text, pos, 0, data);
439 data->Flags &= ~FLG_BlockEnabled;
440 MUI_Redraw(obj, MADF_DRAWUPDATE);
442 break;
444 case MUIM_Backfill:
446 struct MUIP_Backfill *fill_msg = (struct MUIP_Backfill *)msg;
448 if(!(data->Flags & FLG_Active))
449 DoMethod(obj, MUIM_DrawBackground, fill_msg->left, fill_msg->top, fill_msg->right-fill_msg->left+1, fill_msg->bottom-fill_msg->top+1, fill_msg->xoffset, fill_msg->yoffset);
450 // printf("%ld, %ld, %ld, %ld\n%ld, %ld\n", fill_msg->left, fill_msg->top, fill_msg->right, fill_msg->bottom, fill_msg->xoffset, fill_msg->yoffset);
453 case MUIM_BetterString_FileNameStart:
455 result = FileNameStart((struct MUIP_BetterString_FileNameStart *)msg);
457 break;
459 default:
460 return(DoSuperMethodA(cl, obj, msg));
462 return(result);
464 DISPATCHER_EXIT