2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
5 Desc: Basic help functions needed by gadtools.library.
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))
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
,
55 struct TagItem
*taglist
)
58 struct DrawInfo
*dri
= ((struct VisualInfo
*)ng
->ng_VisualInfo
)->vi_dri
;
59 struct TextFont
*font
= NULL
;
60 struct RastPort temprp
;
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
);
80 font
= OpenFont(ng
->ng_TextAttr
);
81 if (!font
) return NULL
;
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
;
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
);
107 if (fontopened
) CloseFont(font
);
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
= >it
->ta
;
127 gtit
->it
.IText
= gtit
->text
+ fontnamelen
+ 1;
128 gtit
->it
.NextText
= NULL
;
130 if (gadgettextlen
) CopyMem(ng
->ng_GadgetText
, gtit
->it
.IText
, gadgettextlen
);
135 underscorelen
= underscorepos
- ng
->ng_GadgetText
;
137 gtit
->it
.IText
= gtit
->text
+ fontnamelen
+ 1;
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
= >it
->it2
;
149 gtit
->it2
= gtit
->it
;
150 gtit
->it2
.ITextFont
= >it
->ta2
;
151 gtit
->it2
.IText
= gtit
->it
.IText
+ gadgettextlen
+ 1;
152 gtit
->it2
.NextText
= NULL
;
154 gtit
->ta2
= gtit
->ta
;
157 gtit
->it2
.FrontPen
= dri
->dri_Pens
[(ng
->ng_Flags
& NG_HIGHLABEL
) ? TEXTPEN
: HIGHLIGHTTEXTPEN
];
159 gtit
->ta2
.ta_Style
|= FSF_UNDERLINED
;
165 gtit
->it2
.LeftEdge
= 0;
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
);
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
);
197 SetFont(rport
, font
);
198 SetSoftStyle(rport
, itext
->ITextFont
->ta_Style
, 0xffffffff);
206 SetABPenDrMd(rport
, itext
->FrontPen
, itext
->BackPen
, itext
->DrawMode
);
211 /**********************************************************************************************/
213 void closefont(struct GadToolsBase_intern
*GadToolsBase
,
214 struct RastPort
*rport
,
215 struct TextFont
*font
, struct TextFont
*oldfont
)
219 SetFont(rport
, oldfont
);
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
;
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
;
243 gadleft
= gad
->LeftEdge
;
244 gadtop
= gad
->TopEdge
;
245 gadwidth
= gad
->Width
;
246 gadheight
= gad
->Height
;
251 /* Calculate offsets. */
252 if ((gad
->Flags
& GFLG_LABELSTRING
))
253 text
= (STRPTR
)gad
->GadgetText
;
254 else if ((gad
->Flags
& GFLG_LABELIMAGE
))
258 /* GFLG_LABELITEXT */
259 text
= gad
->GadgetText
->IText
;
260 font
= preparefont(GadToolsBase
,
261 rport
, gad
->GadgetText
, &oldfont
);
269 TextExtent(rport
, text
, len
, &te
);
271 height
= te
.te_Height
;
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
);
304 Text(rport
, text
, len
);
305 } else if (gad
->Flags
& GFLG_LABELIMAGE
)
306 DrawImage(rport
, (struct Image
*)gad
->GadgetText
, x
, y
);
309 PrintIText(rport
, gad
->GadgetText
, x
, y
);
310 closefont(GadToolsBase
, rport
, font
, oldfont
);
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 };
325 SetAfPt( rp
, pattern
, 1);
327 /* render disable pattern */
328 RectFill(rp
, x1
, y1
, x2
, y2
);
330 SetAfPt (rp
, NULL
, 0);
334 /**********************************************************************************************/