added concrete implementations of putc(), getc(), getchar() and gets()
[tangerine.git] / workbench / demos / colorwheel.c
blob81e9e6370810b917b9a032f845df35fe5281c865
1 /*
2 Copyright © 2000-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 */
7 /*****************************************************************************************/
9 #include <dos/dos.h>
10 #include <intuition/intuition.h>
11 #include <intuition/gadgetclass.h>
12 #include <intuition/icclass.h>
13 #include <intuition/imageclass.h>
14 #include <gadgets/gradientslider.h>
15 #include <gadgets/colorwheel.h>
16 #include <proto/exec.h>
17 #include <proto/intuition.h>
18 #include <proto/colorwheel.h>
19 #include <proto/graphics.h>
20 #include <proto/utility.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
26 /*****************************************************************************************/
28 struct IntuitionBase *IntuitionBase;
29 struct GfxBase *GfxBase;
30 struct UtilityBase *UtilityBase;
31 struct Library *GradientSliderBase;
32 struct Library *ColorWheelBase;
34 static struct Screen *scr;
35 static struct DrawInfo *dri;
36 static struct ViewPort *vp;
37 static struct ColorMap *cm;
38 static struct Window *win;
39 static struct Gadget *gradgad, *wheelgad;
40 static WORD pen1 = -1, pen2 = -1;
41 static BOOL truecolor;
43 static WORD pens[] = {1, 2, ~0};
45 /*****************************************************************************************/
47 static void cleanup(char *msg)
49 if (msg) printf("colorwheel: %s\n", msg);
51 if (win)
53 if (wheelgad) RemoveGadget(win, wheelgad);
54 if (gradgad) RemoveGadget(win, gradgad);
55 CloseWindow(win);
58 if (wheelgad) DisposeObject((Object *)wheelgad);
59 if (gradgad) DisposeObject((Object *)gradgad);
61 if (pen1 != -1) ReleasePen(cm, pen1);
62 if (pen2 != -1) ReleasePen(cm, pen2);
64 if (dri) FreeScreenDrawInfo(scr, dri);
65 if (scr) UnlockPubScreen(0, scr);
67 if (ColorWheelBase) CloseLibrary(ColorWheelBase);
68 if (GradientSliderBase) CloseLibrary(GradientSliderBase);
69 if (UtilityBase) CloseLibrary((struct Library *)UtilityBase);
70 if (GfxBase) CloseLibrary((struct Library *)GfxBase);
71 if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
73 exit(0);
76 /*****************************************************************************************/
78 static void openlibs(void)
80 if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 39)))
82 cleanup("Can't open intuition.library V39!");
85 if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 39)))
87 cleanup("Can't open graphics.library V39!");
90 if (!(UtilityBase = (struct UtilityBase *)OpenLibrary("utility.library", 39)))
92 cleanup("Can't open utility.library V39!");
95 if (!(GradientSliderBase = OpenLibrary("Gadgets/gradientslider.gadget", 0)))
97 cleanup("Can't open gradientslider.gadget!");
100 if (!(ColorWheelBase = OpenLibrary("Gadgets/colorwheel.gadget", 0)))
102 cleanup("Can't open colorwheel.gadget!");
106 /*****************************************************************************************/
108 static void getvisual(void)
110 if (!(scr = LockPubScreen(0)))
112 cleanup("Can't lock pub screen!");
115 if (!(dri = GetScreenDrawInfo(scr)))
117 cleanup("Can't get screen drawinfo!");
120 vp = &scr->ViewPort;
121 cm = vp->ColorMap;
123 pen1 = ObtainPen(cm, -1, 0, 0, 0, PENF_EXCLUSIVE);
124 pen2 = ObtainPen(cm, -1, 0, 0, 0, PENF_EXCLUSIVE);
126 pens[0] = pen1;
127 pens[1] = pen2;
129 if ((pen1 == -1) || (pen2 == -1)) cleanup("Can't obtain 2 pens!");
131 if (GetBitMapAttr(scr->RastPort.BitMap, BMA_DEPTH) >= 15) truecolor = TRUE;
134 /*****************************************************************************************/
136 #define BORDERX 4
137 #define BORDERY 4
138 #define SPACINGX 4
139 #define SPACINGY 4
140 #define GRADWIDTH 20
142 static void makegads(void)
144 struct ColorWheelRGB rgb;
145 struct ColorWheelHSB hsb;
146 Object *im;
147 WORD sizeheight = 14;
148 WORD gradx, grady, gradw, gradh;
149 WORD wheelx, wheely, wheelw, wheelh;
151 im = NewObject(NULL, SYSICLASS, SYSIA_DrawInfo, (IPTR)dri, SYSIA_Which, SIZEIMAGE, TAG_DONE);
152 if (im)
154 sizeheight = ((struct Image *)im)->Height;
155 DisposeObject(im);
158 wheelx = scr->WBorLeft + BORDERX;
159 wheely = scr->WBorTop + scr->Font->ta_YSize + 1 + BORDERY;
160 wheelw = -(scr->WBorLeft + scr->WBorRight + BORDERX * 2 + SPACINGX + GRADWIDTH);
161 wheelh = -(scr->WBorTop + scr->Font->ta_YSize + 1 + sizeheight + BORDERY * 2);
163 gradx = -(scr->WBorRight + BORDERX + GRADWIDTH) + 1;
164 grady = scr->WBorTop + scr->Font->ta_YSize + 1 + BORDERY;
165 gradw = GRADWIDTH;
166 gradh = -(scr->WBorTop + scr->Font->ta_YSize + 1 + sizeheight + BORDERY * 2);
168 gradgad = (struct Gadget *)NewObject(0, "gradientslider.gadget", GA_RelRight , gradx,
169 GA_Top , grady,
170 GA_Width , gradw,
171 GA_RelHeight , gradh,
172 GRAD_PenArray , (IPTR)pens,
173 GRAD_KnobPixels , 10,
174 PGA_Freedom , LORIENT_VERT,
175 TAG_DONE);
177 if (!gradgad) cleanup("Can't create gradientslider gadget!");
179 wheelgad = (struct Gadget *)NewObject(0, "colorwheel.gadget", GA_Left , wheelx,
180 GA_Top , wheely,
181 GA_RelWidth , wheelw,
182 GA_RelHeight , wheelh,
183 GA_RelVerify , TRUE,
184 WHEEL_Screen , (IPTR)scr,
185 WHEEL_BevelBox , TRUE,
186 WHEEL_GradientSlider , (IPTR)gradgad,
187 GA_Previous , (IPTR)gradgad,
188 ICA_TARGET , ICTARGET_IDCMP,
189 TAG_DONE);
191 if (!wheelgad) cleanup("Can't create colorwheel gadget!");
193 GetAttr(WHEEL_HSB, (Object *)wheelgad, (IPTR *)&hsb);
194 hsb.cw_Brightness = 0xFFFFFFFF;
195 ConvertHSBToRGB(&hsb, &rgb);
197 SetRGB32(vp, pen1, rgb.cw_Red, rgb.cw_Green, rgb.cw_Blue);
200 /*****************************************************************************************/
202 static void makewin(void)
204 win = OpenWindowTags(0, WA_PubScreen , (IPTR)scr,
205 WA_Left , 10,
206 WA_Top , 20,
207 WA_Width , 200,
208 WA_Height , 190,
209 WA_MinWidth , 50,
210 WA_MinHeight , 50,
211 WA_MaxWidth , 4000,
212 WA_MaxHeight , 4000,
213 WA_AutoAdjust , TRUE,
214 WA_Title , (IPTR)"ColorWheel",
215 WA_CloseGadget , TRUE,
216 WA_DragBar , TRUE,
217 WA_DepthGadget , TRUE,
218 WA_SizeGadget , TRUE,
219 WA_SizeBBottom , TRUE,
220 WA_Activate , TRUE,
221 WA_ReportMouse , TRUE,
222 WA_IDCMP , IDCMP_CLOSEWINDOW | IDCMP_IDCMPUPDATE,
223 WA_Gadgets , (IPTR)gradgad,
224 TAG_DONE);
226 if (!win) cleanup("Can't open window!");
230 /*****************************************************************************************/
232 static void handleall(void)
234 struct IntuiMessage *msg;
235 BOOL quitme = FALSE;
237 while(!quitme)
239 WaitPort(win->UserPort);
241 while ((msg = (struct IntuiMessage *)GetMsg(win->UserPort)))
243 switch(msg->Class)
245 case IDCMP_CLOSEWINDOW:
246 quitme = TRUE;
247 break;
249 case IDCMP_IDCMPUPDATE:
251 struct ColorWheelRGB rgb;
252 struct ColorWheelHSB hsb;
253 struct TagItem *tags = (struct TagItem *)msg->IAddress;
255 hsb.cw_Hue = GetTagData(WHEEL_Hue, 0, tags);
256 hsb.cw_Saturation = GetTagData(WHEEL_Saturation, 0, tags);
257 hsb.cw_Brightness = 0xFFFFFFFF;
259 ConvertHSBToRGB(&hsb, &rgb);
261 SetRGB32(vp, pen1, rgb.cw_Red, rgb.cw_Green, rgb.cw_Blue);
263 if (truecolor)
265 RefreshGList(gradgad, win, 0, 1);
268 break;
270 } /* switch(msg->Class) */
272 ReplyMsg((struct Message *)msg);
274 } /* while ((msg = (struct IntuiMessage *)GetMsg(win->UserPort))) */
276 } /* while(!quitme) */
280 /*****************************************************************************************/
282 int main(void)
284 openlibs();
285 getvisual();
286 makegads();
287 makewin();
288 handleall();
289 cleanup(0);
291 return 0;
294 /*****************************************************************************************/