grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / libs / muimaster / penspec.c
blob408adb2ca4c0e5a86390defee1c9e8e0c5470cec
1 /*
2 Copyright © 2003-2015, The AROS Development Team. All rights reserved.
4 $Id$
5 */
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <proto/dos.h>
10 #include <proto/graphics.h>
12 /* #define MYDEBUG 1 */
13 #include "debug.h"
14 #include "penspec.h"
15 #include "muimaster_intern.h"
16 #include "mui.h"
17 #include "support.h"
19 extern struct Library *MUIMasterBase;
21 /* From ASCII to internal representation */
22 BOOL zune_pen_spec_to_intern(const struct MUI_PenSpec *spec,
23 struct MUI_PenSpec_intern *intern)
25 if (!spec || !intern)
26 return FALSE;
28 memset(intern, 0, sizeof(*intern));
30 D(bug("zune_pen_spec_to_intern: parsing %s\n", spec->buf));
31 switch (spec->buf[0])
33 case 'm':
34 if (!StrToLong(spec->buf + 1, &intern->p_mui))
35 return FALSE;
36 intern->p_type = PST_MUI;
37 break;
39 case 'p':
40 if (!StrToLong(spec->buf + 1, &intern->p_cmap))
41 return FALSE;
42 intern->p_type = PST_CMAP;
43 break;
45 case 's':
46 if (!StrToLong(spec->buf + 1, &intern->p_sys))
47 return FALSE;
48 intern->p_type = PST_SYS;
49 break;
51 case 'r':
52 default:
54 const char *s;
55 const char *t;
57 s = spec->buf;
58 if (*s == 'r')
60 s++;
62 t = s;
63 intern->p_rgb.red = strtoul(s, (char **)&s, 16);
64 if (s == t)
65 return FALSE;
67 s++;
68 t = s;
69 intern->p_rgb.green = strtoul(s, (char **)&s, 16);
70 if (s == t)
71 return FALSE;
73 s++;
74 t = s;
75 intern->p_rgb.blue = strtoul(s, (char **)&s, 16);
76 if (s == t)
77 return FALSE;
79 intern->p_type = PST_RGB;
80 break;
83 return TRUE;
86 BOOL zune_pen_string_to_intern(CONST_STRPTR spec,
87 struct MUI_PenSpec_intern *intern)
89 return zune_pen_spec_to_intern((const struct MUI_PenSpec *)spec,
90 intern);
93 BOOL zune_pen_intern_to_spec(const struct MUI_PenSpec_intern *intern,
94 struct MUI_PenSpec *spec)
96 if (!spec || !intern)
97 return FALSE;
99 switch (intern->p_type)
101 case PST_MUI:
102 spec->buf[0] = 'm';
103 sprintf(spec->buf + 1, "%ld", (long)intern->p_mui);
104 break;
106 case PST_CMAP:
107 spec->buf[0] = 'p';
108 sprintf(spec->buf + 1, "%ld", (long)intern->p_cmap);
109 break;
111 case PST_RGB:
112 spec->buf[0] = 'r';
113 sprintf(spec->buf + 1, "%08lx,%08lx,%08lx",
114 (long)intern->p_rgb.red, (long)intern->p_rgb.green,
115 (long)intern->p_rgb.blue);
116 break;
118 case PST_SYS:
119 spec->buf[0] = 's';
120 sprintf(spec->buf + 1, "%ld", (long)intern->p_sys);
121 break;
123 default:
124 return FALSE;
126 return TRUE;
129 void zune_penspec_fill_muipen(struct MUI_PenSpec_intern *psi, LONG muipen)
131 if (psi)
133 psi->p_type = PST_MUI;
134 psi->p_mui = muipen;
138 void zune_penspec_fill_rgb(struct MUI_PenSpec_intern *psi, ULONG r, ULONG g,
139 ULONG b)
141 if (psi)
143 psi->p_type = PST_RGB;
144 psi->p_rgb.red = r;
145 psi->p_rgb.green = g;
146 psi->p_rgb.blue = b;
150 static void set_pen_from_rgb(struct MUI_PenSpec_intern *psi,
151 struct ColorMap *cm, ULONG r, ULONG g, ULONG b)
153 LONG pen;
154 struct TagItem obp_tags[] = {
155 {OBP_FailIfBad, FALSE},
156 {TAG_DONE, 0L}
159 pen = ObtainBestPenA(cm, r, g, b, obp_tags);
160 if (pen == -1)
162 psi->p_is_allocated = FALSE;
163 psi->p_pen = FindColor(cm, r, g, b, -1);
165 else
167 psi->p_is_allocated = TRUE;
168 psi->p_pen = pen;
172 BOOL zune_penspec_setup(struct MUI_PenSpec_intern *psi,
173 struct MUI_RenderInfo *mri)
175 if (!psi || !mri)
176 return FALSE;
178 D(bug("zune_penspec_setup(%lx) type=%ld\n", psi, psi->p_type));
179 psi->p_mri = mri;
181 switch (psi->p_type)
183 case PST_MUI:
184 if ((psi->p_mui >= 0) && (psi->p_mui < MPEN_COUNT))
185 psi->p_pen = mri->mri_Pens[psi->p_mui];
186 else
187 return FALSE;
188 break;
190 case PST_CMAP:
192 psi->p_pen = (psi->p_cmap >= 0) ?
193 psi->p_cmap : mri->mri_Colormap->Count + psi->p_cmap;
194 if ((psi->p_pen >= 0)
195 && (psi->p_pen < mri->mri_Colormap->Count))
196 psi->p_is_allocated = FALSE;
197 else
198 return FALSE;
199 break;
201 case PST_RGB:
202 set_pen_from_rgb(psi, mri->mri_Colormap,
203 psi->p_rgb.red, psi->p_rgb.green, psi->p_rgb.blue);
204 D(bug("zune_penspec_setup(%lx)=%ld RGB(%lx,%lx,%lx)\n", psi,
205 psi->p_pen, psi->p_rgb.red, psi->p_rgb.green,
206 psi->p_rgb.blue));
207 break;
209 case PST_SYS:
210 break;
212 default:
213 return FALSE;
215 return TRUE;
218 BOOL zune_penspec_cleanup(struct MUI_PenSpec_intern *psi)
220 if (!psi || !psi->p_mri)
221 return FALSE;
223 switch (psi->p_type)
225 case PST_MUI:
226 break;
228 case PST_CMAP:
229 if (psi->p_is_allocated)
230 ReleasePen(psi->p_mri->mri_Colormap, psi->p_pen);
231 break;
233 case PST_RGB:
234 if (psi->p_is_allocated)
235 ReleasePen(psi->p_mri->mri_Colormap, psi->p_pen);
236 break;
238 case PST_SYS:
239 break;
241 default:
242 return FALSE;
244 return TRUE;
247 void zune_penspec_drawdirect(struct MUI_PenSpec_intern *psi,
248 struct RastPort *rp, struct MUI_RenderInfo *mri, LONG left, LONG top,
249 LONG right, LONG bottom)
251 if (!psi || !mri || !rp)
252 return;
254 if (psi->p_type == PST_RGB)
256 D(bug("drawing with %lx, pen=%ld, at %ld, %ld => %ld, %ld\n",
257 psi, psi->p_pen, left, top, right, bottom));
259 SetAPen(rp, psi->p_pen);
260 RectFill(rp, left, top, right, bottom);
263 void zune_penspec_draw(struct MUI_PenSpec_intern *psi,
264 struct MUI_RenderInfo *mri, LONG left, LONG top, LONG right,
265 LONG bottom)
267 zune_penspec_drawdirect(psi, mri->mri_RastPort, mri, left, top, right,
268 bottom);