Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / workbench / libs / reqtools / boopsi.c
blobcf43ae43c1c917da3f0b5190b1798da4459d5210
1 #include <graphics/gfxbase.h>
2 #include <graphics/rastport.h>
3 #include <graphics/text.h>
4 #include <intuition/intuition.h>
5 #include <intuition/imageclass.h>
6 #include <intuition/intuition.h>
7 #ifdef __AROS__
8 #include <aros/asmcall.h>
9 #endif
11 #include "filereq.h"
13 #include <string.h>
15 extern struct IClass *ButtonImgClass;
17 extern ULONG ASM myTextLength (ASM_REGPARAM(a1, char *,),
18 ASM_REGPARAM(a0, struct TextAttr *,),
19 ASM_REGPARAM(a3, UBYTE *,),
20 ASM_REGPARAM(a2, struct Image *,),
21 ASM_REGPARAM(d7, ULONG,));
23 const UWORD defaultpens[] =
25 1, 0, 1, 2, 1, 3, 1, 0, 2
28 /****************************************************************************************/
30 char KeyFromStr(char *string, char underchar)
32 char c, ret = 0;
34 while ((c = *string++))
36 if (c == underchar)
38 ret = ToUpper(*string);
39 break;
43 return ret;
46 /****************************************************************************************/
48 ULONG myTextLength(char *str, struct TextAttr *attr, UBYTE *underscore,
49 struct Image *im, ULONG do_lod)
51 struct RastPort temprp;
52 struct TextFont *font = NULL;
53 WORD pixellen = 0;
55 InitRastPort(&temprp);
57 if (attr)
59 font = OpenFont(attr);
60 if (font)
62 SetFont(&temprp, font);
64 else
66 SetFont(&temprp, GfxBase->DefaultFont);
69 else
71 SetFont(&temprp, GfxBase->DefaultFont);
74 if (str)
76 struct LocalObjData *lod = (struct LocalObjData *)0xDEADBEAF;
77 char c, *str2;
78 WORD len = strlen(str), underoff;
80 if (do_lod)
82 im->PlaneOnOff = 0;
84 lod = INST_DATA(ButtonImgClass, im);
85 lod->lod_UnderOff = len;
87 im->Height = temprp.TxHeight;
90 pixellen = TextLength(&temprp, str, len);
92 str2 = str;
94 underoff = -1;
96 while((c = *str2++))
98 underoff++;
100 if (c == *underscore)
102 pixellen -= TextLength(&temprp, underscore, 1);
104 if (do_lod)
106 im->PlaneOnOff = *str2; /* store code of underscored key */
107 lod->lod_UnderOff = underoff; /* store offset of underscore */
108 lod->lod_RestLen = len - underoff - 1; /* store len of remaining string */
109 lod->lod_UnderY = temprp.TxBaseline + 2; /* Y position of underscore */
111 lod->lod_UnderWidth = TextLength(&temprp, str2, 1);
113 break;
117 } /* if (str) */
119 #ifdef __AROS__
120 DeinitRastPort(&temprp);
121 #endif
123 if (font) CloseFont(font);
125 return pixellen;
128 /****************************************************************************************/
130 #define imsg ((struct impDraw *)msg)
132 #ifdef __AROS__
133 AROS_UFH3(IPTR, myBoopsiDispatch,
134 AROS_UFHA(Class *, cl, A0),
135 AROS_UFHA(struct Image *, im, A2),
136 AROS_UFHA(Msg, msg, A1))
138 AROS_USERFUNC_INIT
139 #else
140 IPTR myBoopsiDispatch(REGPARAM(a0, Class *, cl),
141 REGPARAM(a2, struct Image *, im),
142 REGPARAM(a1, Msg, msg))
144 #endif
145 struct LocalObjData *data;
146 struct TextFont *font, *oldfont;
147 struct RastPort *rp;
148 struct Gadget *gad;
149 UWORD *pens;
150 WORD xpos, ypos;
151 IPTR retval = 0;
153 switch(msg->MethodID)
155 case OM_NEW:
156 retval = DoSuperMethodA(cl, (Object *)im, msg);
157 if (retval)
159 UBYTE underscorechar;
160 union {
161 struct {
162 WORD Width;
163 WORD Height;
164 } size;
165 ULONG magic;
166 } __tmp;
168 im = (struct Image *)retval;
170 data = INST_DATA(cl, im);
171 data->lod_IData = *(struct InitData *)im->ImageData;
173 underscorechar = (UBYTE)data->lod_IData.idata_Underscore;
175 im->Width = myTextLength(data->lod_IData.idata_Label,
176 data->lod_IData.idata_TextAttr,
177 &underscorechar,
179 cl->cl_InstOffset /* for C Routines its not really more than a flag */
182 /* Calculate text position if we have a gadget */
184 if ((gad = data->lod_IData.idata_Gadget))
186 im->LeftEdge = (gad->Width - im->Width) / 2;
187 im->TopEdge = (gad->Height - im->Height) / 2;
191 im->ImageData = (UWORD *)im;
192 __tmp.magic = BUTTON_MAGIC_LONGWORD;
193 im->Width = __tmp.size.Width;
194 im->Height = __tmp.size.Height;
196 break;
198 case IM_DRAW:
199 data = INST_DATA(cl, im);
201 rp = imsg->imp_RPort;
203 SetDrMd(rp, JAM1);
204 oldfont = rp->Font;
205 font = OpenFont(data->lod_IData.idata_TextAttr);
207 if (font)
208 SetFont(rp, font);
209 else
210 SetFont(rp, GfxBase->DefaultFont);
212 SetSoftStyle(rp, data->lod_IData.idata_TextAttr->ta_Style, data->lod_IData.idata_TextAttr->ta_Style);
214 xpos = imsg->imp_Offset.X;
215 ypos = imsg->imp_Offset.Y;
217 pens = imsg->imp_DrInfo->dri_Pens;
218 if (!pens) pens = (UWORD *)defaultpens;
220 if ((gad = data->lod_IData.idata_Gadget))
222 xpos = gad->LeftEdge;
223 ypos = gad->TopEdge;
225 SetAPen(rp, pens[(imsg->imp_State == IDS_SELECTED) ? FILLPEN : BACKGROUNDPEN]);
227 RectFill(rp, xpos,
228 ypos,
229 gad->LeftEdge + gad->Width - 1,
230 gad->TopEdge + gad->Height -1 );
231 im->PlanePick = (imsg->imp_State == IDS_SELECTED) ? FILLTEXTPEN : TEXTPEN;
234 xpos += im->LeftEdge;
235 ypos += im->TopEdge;
237 SetAPen(rp, pens[im->PlanePick]);
239 Move(rp, xpos, ypos + rp->TxBaseline);
241 /* Draw first part of text (entire text if no underscore */
243 if (data->lod_UnderOff)
244 Text(rp, data->lod_IData.idata_Label, data->lod_UnderOff);
246 /* Is there an underscore in text */
248 if (im->PlaneOnOff)
250 /* First draw rest of text */
252 xpos = rp->cp_x;
254 Text(rp, data->lod_IData.idata_Label + data->lod_UnderOff + 1, data->lod_RestLen);
256 /* Draw underscore */
258 Move(rp, xpos, ypos + data->lod_UnderY);
259 Draw(rp, xpos + data->lod_UnderWidth - 1, ypos + data->lod_UnderY);
262 if (gad)
264 struct TagItem beveltags[] =
266 {GT_VisualInfo , (IPTR)data->lod_IData.idata_VisualInfo },
267 {GTBB_Recessed , TRUE },
268 {TAG_DONE }
271 if (imsg->imp_State != IDS_SELECTED) beveltags[1].ti_Tag = TAG_IGNORE;
273 /* draw bevelbox around gadget container (recessed if selected) */
275 DrawBevelBoxA(rp,
276 gad->LeftEdge,
277 gad->TopEdge,
278 gad->Width,
279 gad->Height,
280 beveltags);
283 SetFont(rp, oldfont);
284 if (font) CloseFont(font);
285 break;
287 default:
288 retval = DoSuperMethodA(cl, (Object *)im, msg);
289 break;
291 } /* switch(msg->MethodID) */
293 return retval;
294 #ifdef __AROS__
295 AROS_USERFUNC_EXIT
296 #endif
299 /****************************************************************************************/
300 /****************************************************************************************/