Updated PCI IDs to latest snapshot.
[tangerine.git] / workbench / libs / muimaster / penspec.c
blobbb3267fbd9efc3bdff17e469e1f39c3f9e0635a8
1 /*
2 Copyright 2003, The AROS Development Team.
3 All rights reserved.
5 $Id$
6 */
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <proto/dos.h>
11 #include <proto/graphics.h>
13 /* #define MYDEBUG 1 */
14 #include "debug.h"
15 #include "penspec.h"
16 #include "muimaster_intern.h"
17 #include "mui.h"
18 #include "support.h"
20 extern struct Library *MUIMasterBase;
22 /* From ASCII to internal representation */
23 BOOL zune_pen_spec_to_intern (const struct MUI_PenSpec *spec,
24 struct MUI_PenSpec_intern *intern)
26 if (!spec || !intern)
27 return FALSE;
29 memset(intern, 0, sizeof(*intern));
31 D(bug("zune_pen_spec_to_intern: parsing %s\n", spec->ps_buf));
32 switch (spec->ps_buf[0])
34 case 'm':
35 if (!StrToLong(spec->ps_buf + 1, &intern->p_mui))
36 return FALSE;
37 intern->p_type = PST_MUI;
38 break;
40 case 'p':
41 if (!StrToLong(spec->ps_buf + 1, &intern->p_cmap))
42 return FALSE;
43 intern->p_type = PST_CMAP;
44 break;
46 case 's':
47 if (!StrToLong(spec->ps_buf + 1, &intern->p_sys))
48 return FALSE;
49 intern->p_type = PST_SYS;
50 break;
52 case 'r':
53 default:
55 const char *s;
56 const char *t;
58 s = spec->ps_buf;
59 if (*s == 'r')
61 s++;
63 t = s;
64 intern->p_rgb.red = strtoul(s, (char **)&s, 16);
65 if (s == t)
66 return FALSE;
68 s++;
69 t = s;
70 intern->p_rgb.green = strtoul(s, (char **)&s, 16);
71 if (s == t)
72 return FALSE;
74 s++;
75 t = s;
76 intern->p_rgb.blue = strtoul(s, (char **)&s, 16);
77 if (s == t)
78 return FALSE;
80 intern->p_type = PST_RGB;
81 break;
84 return TRUE;
87 BOOL zune_pen_string_to_intern (CONST_STRPTR spec,
88 struct MUI_PenSpec_intern *intern)
90 return zune_pen_spec_to_intern((const struct MUI_PenSpec *)spec, 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->ps_buf[0] = 'm';
103 sprintf(spec->ps_buf + 1, "%ld", intern->p_mui);
104 break;
106 case PST_CMAP:
107 spec->ps_buf[0] = 'p';
108 sprintf(spec->ps_buf + 1, "%ld", intern->p_cmap);
109 break;
111 case PST_RGB:
112 spec->ps_buf[0] = 'r';
113 sprintf(spec->ps_buf + 1, "%08lx,%08lx,%08lx",
114 intern->p_rgb.red, intern->p_rgb.green, intern->p_rgb.blue);
115 break;
117 case PST_SYS:
118 spec->ps_buf[0] = 's';
119 sprintf(spec->ps_buf + 1, "%ld", intern->p_sys);
120 break;
122 default:
123 return FALSE;
125 return TRUE;
128 void zune_penspec_fill_muipen(struct MUI_PenSpec_intern *psi, LONG muipen)
130 if (psi)
132 psi->p_type = PST_MUI;
133 psi->p_mui = muipen;
137 void zune_penspec_fill_rgb(struct MUI_PenSpec_intern *psi, ULONG r, ULONG g, ULONG b)
139 if (psi)
141 psi->p_type = PST_RGB;
142 psi->p_rgb.red = r;
143 psi->p_rgb.green = g;
144 psi->p_rgb.blue = b;
148 static void set_pen_from_rgb (struct MUI_PenSpec_intern *psi, struct ColorMap *cm,
149 ULONG r, ULONG g, ULONG b)
151 LONG pen;
152 struct TagItem obp_tags[] =
154 { OBP_FailIfBad, FALSE },
155 { TAG_DONE, 0L }
158 pen = ObtainBestPenA(cm, r, g, b, obp_tags);
159 if (pen == -1)
161 psi->p_is_allocated = FALSE;
162 psi->p_pen = FindColor(cm, r, g, b, -1);
164 else
166 psi->p_is_allocated = TRUE;
167 psi->p_pen = pen;
171 BOOL zune_penspec_setup(struct MUI_PenSpec_intern *psi, struct MUI_RenderInfo *mri)
173 if (!psi || !mri)
174 return FALSE;
176 D(bug("zune_penspec_setup(%lx) type=%ld\n", psi, psi->p_type));
177 psi->p_mri = mri;
179 switch (psi->p_type)
181 case PST_MUI:
182 if ((psi->p_mui >= 0) && (psi->p_mui < MPEN_COUNT))
183 psi->p_pen = mri->mri_Pens[psi->p_mui];
184 else
185 return FALSE;
186 break;
188 case PST_CMAP:
190 psi->p_pen = (psi->p_cmap >= 0) ?
191 psi->p_cmap :
192 mri->mri_Colormap->Count + psi->p_cmap;
193 if ((psi->p_pen >= 0) && (psi->p_pen < mri->mri_Colormap->Count))
194 psi->p_is_allocated = FALSE;
195 else
196 return FALSE;
197 break;
199 case PST_RGB:
200 set_pen_from_rgb(psi, mri->mri_Colormap,
201 psi->p_rgb.red, psi->p_rgb.green, psi->p_rgb.blue);
202 D(bug("zune_penspec_setup(%lx)=%ld RGB(%lx,%lx,%lx)\n", psi,
203 psi->p_pen, psi->p_rgb.red, psi->p_rgb.green, psi->p_rgb.blue));
204 break;
206 case PST_SYS:
207 break;
209 default:
210 return FALSE;
212 return TRUE;
215 BOOL zune_penspec_cleanup(struct MUI_PenSpec_intern *psi)
217 if (!psi || !psi->p_mri)
218 return FALSE;
220 switch (psi->p_type)
222 case PST_MUI:
223 break;
225 case PST_CMAP:
226 if (psi->p_is_allocated)
227 ReleasePen(psi->p_mri->mri_Colormap, psi->p_pen);
228 break;
230 case PST_RGB:
231 if (psi->p_is_allocated)
232 ReleasePen(psi->p_mri->mri_Colormap, psi->p_pen);
233 break;
235 case PST_SYS:
236 break;
238 default:
239 return FALSE;
241 return TRUE;
244 void zune_penspec_drawdirect(struct MUI_PenSpec_intern *psi, struct RastPort *rp, struct MUI_RenderInfo *mri,
245 LONG left, LONG top, LONG right, LONG bottom)
247 if (!psi || !mri || !rp)
248 return;
250 if ( psi->p_type == PST_RGB)
252 D(bug("drawing with %lx, pen=%ld, at %ld, %ld => %ld, %ld\n",
253 psi, psi->p_pen, left, top, right, bottom));
255 SetAPen(rp, psi->p_pen);
256 RectFill(rp, left, top, right, bottom);
259 void zune_penspec_draw(struct MUI_PenSpec_intern *psi, struct MUI_RenderInfo *mri,
260 LONG left, LONG top, LONG right, LONG bottom)
262 zune_penspec_drawdirect(psi, mri->mri_RastPort, mri, left, top, right, bottom);