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/
21 ***************************************************************************/
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"
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
68 #define MUIM_Backfill 0x80428d73
69 struct MUIP_Backfill
{ ULONG MethodID
; LONG left
; LONG top
; LONG right
; LONG bottom
; LONG xoffset
; LONG yoffset
; };
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,
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
;
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
;
115 CoerceMethod(cl
, obj
, OM_DISPOSE
);
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
))
140 if(DoMethod(obj, MUIM_GetConfigItem, 132, &color))
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
);
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
);
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
;
198 ULONG width
= data
->Width
* MyTextLength(font
, "n", 1);
200 msg
->MinMaxInfo
->MinWidth
+= width
;
201 msg
->MinMaxInfo
->DefWidth
+= width
;
202 msg
->MinMaxInfo
->MaxWidth
+= width
;
206 msg
->MinMaxInfo
->MinWidth
+= 10;
207 msg
->MinMaxInfo
->DefWidth
+= 100;
208 msg
->MinMaxInfo
->MaxWidth
= MUI_MAXMAX
;
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
;
236 ULONG
Hide(struct IClass
*cl
, Object
*obj
, Msg msg
)
238 struct InstData
*data
= (struct InstData
*)INST_DATA(cl
, obj
);
241 if (data
->Flags
& FLG_Shown
)
243 DeinitRastPort(&data
->rport
);
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
);
269 DISPATCHERPROTO(_Dispatcher
)
273 struct InstData
*data
= (struct InstData
*)INST_DATA(cl
, obj
);
276 switch(msg
->MethodID
)
279 return(New(cl
, obj
, (struct opSet
*)msg
));
281 return(Setup(cl
, obj
, (struct MUI_RenderInfo
*)msg
));
283 return(Show(cl
, obj
, msg
));
285 return(AskMinMax(cl
, obj
, (struct MUIP_AskMinMax
*)msg
));
287 return(mDraw(cl
, obj
, (struct MUIP_Draw
*)msg
));
289 return(Get(cl
, obj
, (struct opGet
*)msg
));
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
))
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
)
310 switch(((struct MUIP_HandleEvent
*)msg
)->muikey
)
313 attr
= MUIV_List_Active_Top
;
316 attr
= MUIV_List_Active_Bottom
;
319 attr
= MUIV_List_Active_Up
;
322 attr
= MUIV_List_Active_Down
;
325 attr
= MUIV_List_Active_PageUp
;
327 case MUIKEY_PAGEDOWN
:
328 attr
= MUIV_List_Active_PageDown
;
333 SetAttrs(data
->ForwardObject
, MUIA_List_Active
, attr
, TAG_DONE
);
334 result
= MUI_EventHandlerRC_Eat
;
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);
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
);
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
);
374 return(Hide(cl
, obj
, msg
));
376 return(Cleanup(cl
, obj
, msg
));
378 return(Dispose(cl
, obj
, msg
));
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
);
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
);
402 set(obj
, MUIA_String_Contents
, contents
);
408 case MUIM_BetterString_ClearSelected
:
411 data
->Flags
&= ~FLG_BlockEnabled
;
415 case MUIM_BetterString_Insert
:
418 struct MUIP_BetterString_Insert
*ins_msg
= (struct MUIP_BetterString_Insert
*)msg
;
422 /* case MUIV_BetterString_Insert_StartOfString:
426 case MUIV_BetterString_Insert_EndOfString
:
427 pos
= strlen(data
->Contents
);
430 case MUIV_BetterString_Insert_BufferPos
:
431 pos
= data
->BufferPos
;
438 Overwrite(ins_msg
->text
, pos
, 0, data
);
439 data
->Flags
&= ~FLG_BlockEnabled
;
440 MUI_Redraw(obj
, MADF_DRAWUPDATE
);
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
);
460 return(DoSuperMethodA(cl
, obj
, msg
));