Allow multiple volumes with the same name if their creation dates differ.
[AROS.git] / workbench / libs / gadtools / basicfuncs.c
blob8d1db2cf91e80b4ebeaed2ca5d373ecf048a9a5e
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Basic help functions needed by gadtools.library.
6 Lang: English.
7 */
9 #include <string.h>
10 #include <proto/exec.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>
25 #include "gadtools_intern.h"
27 /**********************************************************************************************/
29 #define HIGH_COLOR 0 /* instead of underscore use different color to highlight key */
31 #define EG(x) ((struct ExtGadget *)(x))
33 struct GTIText
35 struct IntuiText it;
36 struct IntuiText it2;
37 struct TextAttr ta;
38 struct TextAttr ta2;
39 UBYTE text[0];
42 /**********************************************************************************************/
44 void freeitext(struct GadToolsBase_intern *GadToolsBase,
45 struct IntuiText *itext)
47 if (itext) FreeVec(itext);
50 /**********************************************************************************************/
52 /* Create a struct IntuiText accordings to a struct NewGadget */
53 struct IntuiText *makeitext(struct GadToolsBase_intern *GadToolsBase,
54 struct NewGadget *ng,
55 struct TagItem *taglist)
57 struct GTIText *gtit;
58 struct DrawInfo *dri = ((struct VisualInfo *)ng->ng_VisualInfo)->vi_dri;
59 struct TextFont *font = NULL;
60 struct RastPort temprp;
61 STRPTR underscorepos;
62 STRPTR fontname;
63 UWORD fontysize;
64 UBYTE fontstyle;
65 UBYTE fontflags;
66 WORD gadgettextlen;
67 WORD fontnamelen;
68 WORD underscorelen;
69 WORD alloclen;
70 BOOL fontopened = FALSE;
71 UBYTE underscore = 1; /* default for GT_Underscore = a char which hopefully a normal
72 string never contains */
74 underscore = (UBYTE)GetTagData(GT_Underscore, underscore, taglist);
75 underscorepos = strchr(ng->ng_GadgetText, underscore);
76 gadgettextlen = strlen(ng->ng_GadgetText);
78 if (ng->ng_TextAttr)
80 font = OpenFont(ng->ng_TextAttr);
81 if (!font) return NULL;
83 fontopened = TRUE;
85 fontname = ng->ng_TextAttr->ta_Name;
86 fontysize = ng->ng_TextAttr->ta_YSize;
87 fontstyle = ng->ng_TextAttr->ta_Style;
88 fontflags = ng->ng_TextAttr->ta_Flags;
89 } else {
90 font = dri->dri_Font;
92 fontname = dri->dri_Font->tf_Message.mn_Node.ln_Name;
93 fontysize = dri->dri_Font->tf_YSize;
94 fontstyle = dri->dri_Font->tf_Style;
95 fontflags = dri->dri_Font->tf_Flags;
98 if (!fontname) return NULL;
100 fontnamelen = strlen(fontname);
102 alloclen = sizeof(struct GTIText) + fontnamelen + 1 + gadgettextlen + 1 + 2; /* 2 for safety */
104 gtit = (struct GTIText *)AllocVec(alloclen, MEMF_PUBLIC | MEMF_CLEAR);
105 if (!gtit)
107 if (fontopened) CloseFont(font);
108 return NULL;
111 CopyMem(fontname, gtit->text, fontnamelen);
113 gtit->ta.ta_Name = gtit->text;
114 gtit->ta.ta_YSize = fontysize;
115 gtit->ta.ta_Style = fontstyle;
116 gtit->ta.ta_Flags = fontflags;
118 gtit->it.FrontPen = dri->dri_Pens[(ng->ng_Flags & NG_HIGHLABEL) ? HIGHLIGHTTEXTPEN : TEXTPEN];
119 gtit->it.BackPen = dri->dri_Pens[BACKGROUNDPEN];
120 gtit->it.DrawMode = JAM1;
121 gtit->it.LeftEdge = 0;
122 gtit->it.TopEdge = 0;
123 gtit->it.ITextFont = &gtit->ta;
125 if (!underscorepos)
127 gtit->it.IText = gtit->text + fontnamelen + 1;
128 gtit->it.NextText = NULL;
130 if (gadgettextlen) CopyMem(ng->ng_GadgetText, gtit->it.IText, gadgettextlen);
132 else
134 gadgettextlen--;
135 underscorelen = underscorepos - ng->ng_GadgetText;
137 gtit->it.IText = gtit->text + fontnamelen + 1;
138 if (underscorelen)
140 CopyMem(ng->ng_GadgetText, gtit->it.IText, underscorelen);
142 if (gadgettextlen - underscorelen)
144 CopyMem(underscorepos + 1, gtit->it.IText + underscorelen, gadgettextlen - underscorelen);
147 gtit->it.NextText = &gtit->it2;
149 gtit->it2 = gtit->it;
150 gtit->it2.ITextFont = &gtit->ta2;
151 gtit->it2.IText = gtit->it.IText + gadgettextlen + 1;
152 gtit->it2.NextText = NULL;
154 gtit->ta2 = gtit->ta;
156 #if HIGH_COLOR
157 gtit->it2.FrontPen = dri->dri_Pens[(ng->ng_Flags & NG_HIGHLABEL) ? TEXTPEN : HIGHLIGHTTEXTPEN];
158 #else
159 gtit->ta2.ta_Style |= FSF_UNDERLINED;
160 #endif
163 if (!underscorelen)
165 gtit->it2.LeftEdge = 0;
167 else
169 InitRastPort(&temprp);
170 SetFont(&temprp, font);
172 gtit->it2.LeftEdge = TextLength(&temprp, ng->ng_GadgetText, underscorelen);
175 gtit->it2.IText[0] = underscorepos[1];
178 if (fontopened) CloseFont(font);
180 return &gtit->it;
183 /**********************************************************************************************/
185 struct TextFont *preparefont(struct GadToolsBase_intern *GadToolsBase,
186 struct RastPort *rport, struct IntuiText *itext,
187 struct TextFont **oldfont)
189 struct TextFont *font;
191 if (itext->ITextFont)
193 *oldfont = rport->Font;
194 font = OpenFont(itext->ITextFont);
195 if (font)
197 SetFont(rport, font);
198 SetSoftStyle(rport, itext->ITextFont->ta_Style, 0xffffffff);
199 } else
200 font = rport->Font;
201 } else
203 *oldfont = NULL;
204 font = rport->Font;
206 SetABPenDrMd(rport, itext->FrontPen, itext->BackPen, itext->DrawMode);
208 return font;
211 /**********************************************************************************************/
213 void closefont(struct GadToolsBase_intern *GadToolsBase,
214 struct RastPort *rport,
215 struct TextFont *font, struct TextFont *oldfont)
217 if (oldfont)
219 SetFont(rport, oldfont);
220 CloseFont(font);
224 /**********************************************************************************************/
226 BOOL renderlabel(struct GadToolsBase_intern *GadToolsBase,
227 struct Gadget *gad, struct RastPort *rport, LONG labelplace)
229 struct TextFont *font = NULL, *oldfont;
230 struct TextExtent te;
231 STRPTR text;
232 int len = 0, x, y;
233 UWORD width, height;
234 WORD gadleft, gadtop, gadwidth, gadheight;
236 if (EG(gad)->MoreFlags & GMORE_BOUNDS)
238 gadleft = EG(gad)->BoundsLeftEdge;
239 gadtop = EG(gad)->BoundsTopEdge;
240 gadwidth = EG(gad)->BoundsWidth;
241 gadheight = EG(gad)->BoundsHeight;
242 } else {
243 gadleft = gad->LeftEdge;
244 gadtop = gad->TopEdge;
245 gadwidth = gad->Width;
246 gadheight = gad->Height;
249 if (gad->GadgetText)
251 /* Calculate offsets. */
252 if ((gad->Flags & GFLG_LABELSTRING))
253 text = (STRPTR)gad->GadgetText;
254 else if ((gad->Flags & GFLG_LABELIMAGE))
255 text = NULL;
256 else
258 /* GFLG_LABELITEXT */
259 text = gad->GadgetText->IText;
260 font = preparefont(GadToolsBase,
261 rport, gad->GadgetText, &oldfont);
262 if (!font)
263 return FALSE;
266 if (text)
268 len = strlen(text);
269 TextExtent(rport, text, len, &te);
270 width = te.te_Width;
271 height = te.te_Height;
272 } else
274 width = ((struct Image *)gad->GadgetText)->Width;
275 height = ((struct Image *)gad->GadgetText)->Height;
278 if (labelplace == GV_LabelPlace_Right)
280 x = gadleft + gadwidth + 5;
281 y = gadtop + (gadheight - height) / 2 + 1;
282 } else if (labelplace == GV_LabelPlace_Above)
284 x = gadleft + (gadwidth - width) / 2;
285 y = gadtop - height - 2;
286 } else if (labelplace == GV_LabelPlace_Below)
288 x = gadleft + (gadwidth - width) / 2;
289 y = gadtop + gadheight + 3;
290 } else if (labelplace == GV_LabelPlace_In)
292 x = gadleft + (gadwidth - width) / 2;
293 y = gadtop + (gadheight - height) / 2 + 1;
294 } else /* GV_LabelPlace_Left */
296 x = gadleft - width - 4;
297 y = gadtop + (gadheight - height) / 2 + 1;
300 if (gad->Flags & GFLG_LABELSTRING)
302 SetABPenDrMd(rport, 1, 0, JAM1);
303 Move(rport, x, y);
304 Text(rport, text, len);
305 } else if (gad->Flags & GFLG_LABELIMAGE)
306 DrawImage(rport, (struct Image *)gad->GadgetText, x, y);
307 else
309 PrintIText(rport, gad->GadgetText, x, y);
310 closefont(GadToolsBase, rport, font, oldfont);
313 return TRUE;
316 /**********************************************************************************************/
318 void DoDisabledPattern(struct RastPort *rp, WORD x1, WORD y1, WORD x2, WORD y2,
319 WORD pen, struct GadToolsBase_intern *GadToolsBase)
321 UWORD pattern[] = { 0x8888, 0x2222 };
323 SetDrMd( rp, JAM1 );
324 SetAPen( rp, pen );
325 SetAfPt( rp, pattern, 1);
327 /* render disable pattern */
328 RectFill(rp, x1, y1, x2, y2);
330 SetAfPt (rp, NULL, 0);
334 /**********************************************************************************************/