add casts to zune macros to silence some warnings
[tangerine.git] / workbench / libs / gadtools / basicfuncs.c
blobbb513735326d7c1bd822b387682b32b77674496e
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Basic help functions needed by gadtools.library.
6 Lang: English.
7 */
8 #include <string.h>
9 #include <proto/exec.h>
10 #include <exec/types.h>
11 #include <exec/memory.h>
12 #include <intuition/intuition.h>
13 #include <intuition/screens.h>
14 #include <proto/graphics.h>
15 #include <graphics/rastport.h>
16 #include <graphics/rpattr.h>
17 #include <graphics/text.h>
18 #include <graphics/gfxmacros.h>
19 #include <proto/utility.h>
20 #include <utility/tagitem.h>
21 #include <libraries/gadtools.h>
22 #include <aros/debug.h>
23 #include <intuition/gadgetclass.h>
24 #include "gadtools_intern.h"
26 /**********************************************************************************************/
28 #define HIGH_COLOR 0 /* instead of underscore use different color to highlight key */
30 #define EG(x) ((struct ExtGadget *)(x))
32 struct GTIText
34 struct IntuiText it;
35 struct IntuiText it2;
36 struct TextAttr ta;
37 struct TextAttr ta2;
38 UBYTE text[0];
41 /**********************************************************************************************/
43 void freeitext(struct GadToolsBase_intern *GadToolsBase,
44 struct IntuiText *itext)
46 if (itext) FreeVec(itext);
49 /**********************************************************************************************/
51 /* Create a struct IntuiText accordings to a struct NewGadget */
52 struct IntuiText *makeitext(struct GadToolsBase_intern *GadToolsBase,
53 struct NewGadget *ng,
54 struct TagItem *taglist)
56 struct GTIText *gtit;
57 struct DrawInfo *dri = ((struct VisualInfo *)ng->ng_VisualInfo)->vi_dri;
58 struct TextFont *font = NULL;
59 struct RastPort temprp;
60 STRPTR underscorepos;
61 STRPTR fontname;
62 UWORD fontysize;
63 UBYTE fontstyle;
64 UBYTE fontflags;
65 WORD gadgettextlen;
66 WORD fontnamelen;
67 WORD underscorelen;
68 WORD alloclen;
69 BOOL fontopened = FALSE;
70 UBYTE underscore = 1; /* default for GT_Underscore = a char which hopefully a normal
71 string never contains */
73 underscore = (UBYTE)GetTagData(GT_Underscore, underscore, taglist);
74 underscorepos = strchr(ng->ng_GadgetText, underscore);
75 gadgettextlen = strlen(ng->ng_GadgetText);
77 if (ng->ng_TextAttr)
79 font = OpenFont(ng->ng_TextAttr);
80 if (!font) return NULL;
82 fontopened = TRUE;
84 fontname = ng->ng_TextAttr->ta_Name;
85 fontysize = ng->ng_TextAttr->ta_YSize;
86 fontstyle = ng->ng_TextAttr->ta_Style;
87 fontflags = ng->ng_TextAttr->ta_Flags;
88 } else {
89 font = dri->dri_Font;
91 fontname = dri->dri_Font->tf_Message.mn_Node.ln_Name;
92 fontysize = dri->dri_Font->tf_YSize;
93 fontstyle = dri->dri_Font->tf_Style;
94 fontflags = dri->dri_Font->tf_Flags;
97 if (!fontname) return NULL;
99 fontnamelen = strlen(fontname);
101 alloclen = sizeof(struct GTIText) + fontnamelen + 1 + gadgettextlen + 1 + 2; /* 2 for safety */
103 gtit = (struct GTIText *)AllocVec(alloclen, MEMF_PUBLIC | MEMF_CLEAR);
104 if (!gtit)
106 if (fontopened) CloseFont(font);
107 return NULL;
110 CopyMem(fontname, gtit->text, fontnamelen);
112 gtit->ta.ta_Name = gtit->text;
113 gtit->ta.ta_YSize = fontysize;
114 gtit->ta.ta_Style = fontstyle;
115 gtit->ta.ta_Flags = fontflags;
117 gtit->it.FrontPen = dri->dri_Pens[(ng->ng_Flags & NG_HIGHLABEL) ? HIGHLIGHTTEXTPEN : TEXTPEN];
118 gtit->it.BackPen = dri->dri_Pens[BACKGROUNDPEN];
119 gtit->it.DrawMode = JAM1;
120 gtit->it.LeftEdge = 0;
121 gtit->it.TopEdge = 0;
122 gtit->it.ITextFont = &gtit->ta;
124 if (!underscorepos)
126 gtit->it.IText = gtit->text + fontnamelen + 1;
127 gtit->it.NextText = NULL;
129 if (gadgettextlen) CopyMem(ng->ng_GadgetText, gtit->it.IText, gadgettextlen);
131 else
133 gadgettextlen--;
134 underscorelen = underscorepos - ng->ng_GadgetText;
136 gtit->it.IText = gtit->text + fontnamelen + 1;
137 if (underscorelen)
139 CopyMem(ng->ng_GadgetText, gtit->it.IText, underscorelen);
141 if (gadgettextlen - underscorelen)
143 CopyMem(underscorepos + 1, gtit->it.IText + underscorelen, gadgettextlen - underscorelen);
146 gtit->it.NextText = &gtit->it2;
148 gtit->it2 = gtit->it;
149 gtit->it2.ITextFont = &gtit->ta2;
150 gtit->it2.IText = gtit->it.IText + gadgettextlen + 1;
151 gtit->it2.NextText = NULL;
153 gtit->ta2 = gtit->ta;
155 #if HIGH_COLOR
156 gtit->it2.FrontPen = dri->dri_Pens[(ng->ng_Flags & NG_HIGHLABEL) ? TEXTPEN : HIGHLIGHTTEXTPEN];
157 #else
158 gtit->ta2.ta_Style |= FSF_UNDERLINED;
159 #endif
162 if (!underscorelen)
164 gtit->it2.LeftEdge = 0;
166 else
168 InitRastPort(&temprp);
169 SetFont(&temprp, font);
171 gtit->it2.LeftEdge = TextLength(&temprp, ng->ng_GadgetText, underscorelen);
173 DeinitRastPort(&temprp);
176 gtit->it2.IText[0] = underscorepos[1];
179 if (fontopened) CloseFont(font);
181 return &gtit->it;
184 /**********************************************************************************************/
186 struct TextFont *preparefont(struct GadToolsBase_intern *GadToolsBase,
187 struct RastPort *rport, struct IntuiText *itext,
188 struct TextFont **oldfont)
190 struct TextFont *font;
192 if (itext->ITextFont)
194 *oldfont = rport->Font;
195 font = OpenFont(itext->ITextFont);
196 if (font)
198 SetFont(rport, font);
199 SetSoftStyle(rport, itext->ITextFont->ta_Style, 0xffffffff);
200 } else
201 font = rport->Font;
202 } else
204 *oldfont = NULL;
205 font = rport->Font;
207 SetABPenDrMd(rport, itext->FrontPen, itext->BackPen, itext->DrawMode);
209 return font;
212 /**********************************************************************************************/
214 void closefont(struct GadToolsBase_intern *GadToolsBase,
215 struct RastPort *rport,
216 struct TextFont *font, struct TextFont *oldfont)
218 if (oldfont)
220 SetFont(rport, oldfont);
221 CloseFont(font);
225 /**********************************************************************************************/
227 BOOL renderlabel(struct GadToolsBase_intern *GadToolsBase,
228 struct Gadget *gad, struct RastPort *rport, LONG labelplace)
230 struct TextFont *font = NULL, *oldfont;
231 struct TextExtent te;
232 STRPTR text;
233 int len = 0, x, y;
234 UWORD width, height;
235 WORD gadleft, gadtop, gadwidth, gadheight;
237 if (EG(gad)->MoreFlags & GMORE_BOUNDS)
239 gadleft = EG(gad)->BoundsLeftEdge;
240 gadtop = EG(gad)->BoundsTopEdge;
241 gadwidth = EG(gad)->BoundsWidth;
242 gadheight = EG(gad)->BoundsHeight;
243 } else {
244 gadleft = gad->LeftEdge;
245 gadtop = gad->TopEdge;
246 gadwidth = gad->Width;
247 gadheight = gad->Height;
250 if (gad->GadgetText)
252 /* Calculate offsets. */
253 if ((gad->Flags & GFLG_LABELSTRING))
254 text = (STRPTR)gad->GadgetText;
255 else if ((gad->Flags & GFLG_LABELIMAGE))
256 text = NULL;
257 else
259 /* GFLG_LABELITEXT */
260 text = gad->GadgetText->IText;
261 font = preparefont(GadToolsBase,
262 rport, gad->GadgetText, &oldfont);
263 if (!font)
264 return FALSE;
267 if (text)
269 len = strlen(text);
270 TextExtent(rport, text, len, &te);
271 width = te.te_Width;
272 height = te.te_Height;
273 } else
275 width = ((struct Image *)gad->GadgetText)->Width;
276 height = ((struct Image *)gad->GadgetText)->Height;
279 if (labelplace == GV_LabelPlace_Right)
281 x = gadleft + gadwidth + 5;
282 y = gadtop + (gadheight - height) / 2 + 1;
283 } else if (labelplace == GV_LabelPlace_Above)
285 x = gadleft + (gadwidth - width) / 2;
286 y = gadtop - height - 2;
287 } else if (labelplace == GV_LabelPlace_Below)
289 x = gadleft + (gadwidth - width) / 2;
290 y = gadtop + gadheight + 3;
291 } else if (labelplace == GV_LabelPlace_In)
293 x = gadleft + (gadwidth - width) / 2;
294 y = gadtop + (gadheight - height) / 2 + 1;
295 } else /* GV_LabelPlace_Left */
297 x = gadleft - width - 4;
298 y = gadtop + (gadheight - height) / 2 + 1;
301 if (gad->Flags & GFLG_LABELSTRING)
303 SetABPenDrMd(rport, 1, 0, JAM1);
304 Move(rport, x, y);
305 Text(rport, text, len);
306 } else if (gad->Flags & GFLG_LABELIMAGE)
307 DrawImage(rport, (struct Image *)gad->GadgetText, x, y);
308 else
310 PrintIText(rport, gad->GadgetText, x, y);
311 closefont(GadToolsBase, rport, font, oldfont);
314 return TRUE;
317 /**********************************************************************************************/
319 void DoDisabledPattern(struct RastPort *rp, WORD x1, WORD y1, WORD x2, WORD y2,
320 WORD pen, struct GadToolsBase_intern *GadToolsBase)
322 UWORD pattern[] = { 0x8888, 0x2222 };
324 SetDrMd( rp, JAM1 );
325 SetAPen( rp, pen );
326 SetAfPt( rp, pattern, 1);
328 /* render disable pattern */
329 RectFill(rp, x1, y1, x2, y2);
331 SetAfPt (rp, NULL, 0);
335 /**********************************************************************************************/