Updated PCI IDs to latest snapshot.
[tangerine.git] / workbench / libs / reqtools / palettereq.c
blob4a022a83e5e2506828d03ff25298b0a8046360ed
2 /* INCLUDES */
4 #include <exec/types.h>
5 #include <exec/memory.h>
6 #include <libraries/dos.h>
7 #include <libraries/dosextens.h>
8 #include <libraries/gadtools.h>
9 #include <intuition/icclass.h>
10 #include <intuition/intuition.h>
11 #include <intuition/gadgetclass.h>
12 #include <intuition/intuitionbase.h>
13 #include <intuition/gadgetclass.h>
14 #include <gadgets/colorwheel.h>
15 #include <gadgets/gradientslider.h>
16 #include <graphics/gfxbase.h>
17 #include <proto/graphics.h>
18 #include <proto/exec.h>
19 #include <proto/intuition.h>
20 #include <proto/dos.h>
21 #include <proto/gadtools.h>
22 #include <proto/utility.h>
23 #include <proto/alib.h>
24 #include <proto/colorwheel.h>
25 #include <clib/macros.h>
26 #include <string.h>
28 #include <libraries/reqtools.h>
29 #include <proto/reqtools.h>
31 #include "general.h"
32 #include "gadstub.h"
33 #include "boopsigads.h"
35 #include "rtlocale.h"
37 /****************************************************************************************/
39 extern struct ReqToolsBase *ReqToolsBase;
41 /****************************************************************************************/
43 #define COLORWHEEL
44 #define GRADIENT
46 /****************************************************************************************/
48 extern ULONG ASM LoopReqHandler (ASM_REGPARAM(a1, struct rtHandlerInfo *,));
49 extern ULONG ASM myGetDisplayInfoData (
50 OPT_REGPARAM(a1, UBYTE *,),
51 OPT_REGPARAM(d0, unsigned long,),
52 OPT_REGPARAM(d1, unsigned long,),
53 OPT_REGPARAM(d2, unsigned long,));
55 /****************************************************************************************/
57 #define RED_ID 0 /* DO NOT CHANGE !!! */
58 #define GREEN_ID 1 /* DO NOT CHANGE !!! */
59 #define BLUE_ID 2 /* DO NOT CHANGE !!! */
61 #define PALETTE_ID 3
62 #define COPY_ID 4
63 #define SWAP_ID 5
64 #define SPREAD_ID 6
65 #define OK_ID 7
66 #define UNDO_ID 8
67 #define CANCEL_ID 9
68 #define SLIDER_ID 10
69 #define WHEEL_ID 11
71 /****************************************************************************************/
73 STRPTR ModeTitles[] =
75 MSG_COPY_TITLE,
76 MSG_SWAP_TITLE,
77 MSG_SPREAD_TITLE
80 /****************************************************************************************/
82 #define MODE_TITLE_OFFSET COPY_ID
85 #define LOWGRADPENS 4
86 #define HIGHGRADPENS 8
87 #define FREEFORHIGH 32
89 #define NO_TITLE ( ( STRPTR ) ~0 )
91 /****************************************************************************************/
93 extern struct Library *GadToolsBase;
94 extern struct IntuitionBase *IntuitionBase;
95 extern struct GfxBase *GfxBase;
97 extern struct TextAttr topaz80;
99 /****************************************************************************************/
101 typedef struct RealHandlerInfo GlobData;
103 struct RealHandlerInfo
105 LONG (*func)(); /* private */
106 ULONG rthi_WaitMask;
107 ULONG rthi_DoNotWait;
109 /* PRIVATE */
110 ULONG cols[3]; /* DO NOT MOVE, see cols offset in misc.asm */
111 struct ViewPort *vp; /* DO NOT MOVE, see vp offset in misc.asm */
112 union
113 { /* DO NOT MOVE, see offsets in misc.asm */
114 struct
116 ULONG red, green, blue;
117 } bits;
118 ULONG colbits[3];
119 } col;
120 struct NewWindow newpalwin;
121 struct KeyButtonInfo buttoninfo;
122 struct Screen *scr, *frontscr;
123 struct Gadget *colgad[3], *palgad;
124 struct Window *palwin, *win, *prwin;
125 struct ColorMap *cm;
126 struct TextAttr font;
127 struct TextFont *reqfont;
128 struct DrawInfo *drinfo;
129 struct Image labelimages;
130 struct rtReqInfo *reqinfo;
131 struct Hook *imsghook, backfillhook;
132 struct Catalog *catalog;
133 struct IntuiText itxt;
134 long color, coldepth, colcount, reqpos, leftedge, topedge;
135 int waitpointer, lockwindow, shareidcmp, noscreenpop;
136 int fontwidth, fontheight, mode, os30, maxcolval[3];
137 char key[3], palettekey;
138 APTR colormap, undomap;
139 APTR visinfo, winlock;
140 WORD zoom[4];
142 #ifdef COLORWHEEL
143 /* Color wheel */
144 struct Library *ColorWheelBase;
145 struct Library *GradientSliderBase;
146 struct ColorWheelRGB wheel_rgb;
147 struct ColorWheelHSB wheel_hsb;
148 struct Gadget *wheel;
149 struct Gadget *wheel_slider;
150 ULONG wheel_colortable[3];
152 #ifdef GRADIENT
153 UWORD wheel_pens[ HIGHGRADPENS + 1 ];
154 UWORD numgradpens;
155 #endif
156 WORD dowheel, fancywheel;
157 Point screenres;
158 #endif
161 /****************************************************************************************/
163 #define redbits col.bits.red
164 #define greenbits col.bits.green
165 #define bluebits col.bits.blue
167 #define ThisProcess() ( ( struct Process * ) FindTask( NULL ) )
169 /****************************************************************************************/
171 extern ULONG ASM MakeColVal (ASM_REGPARAM(d0, ULONG,), ASM_REGPARAM(d4, ULONG,));
172 extern void REGARGS SpreadColors (GlobData *, int, int, ULONG *);
174 static int REGARGS SetupPalWindow (GlobData *, char *);
175 static void REGARGS SelectColor (GlobData *, int);
176 static void REGARGS FreeAll (GlobData *);
177 static void REGARGS SetColor (GlobData *, int, ULONG *);
178 static void REGARGS SetWheelColor (GlobData *, struct TagItem * );
179 static void REGARGS UpdateWheel( GlobData *, ULONG * );
180 static void REGARGS RestorePaletteFreeAll (GlobData *);
181 static void REGARGS DoColorShortcut (GlobData *, int, int, int);
182 static LONG ASM SAVEDS PalReqHandler (
183 REGPARAM(a1, struct RealHandlerInfo *,),
184 REGPARAM(d0, ULONG,),
185 REGPARAM(a0, struct TagItem *,));
187 /****************************************************************************************/
189 #ifdef COLORWHEEL
190 #define ColorWheelBase glob->ColorWheelBase
191 #define GradientSliderBase glob->GradientSliderBase
192 #endif
195 /****************************************************************************************/
197 #if !USE_ASM_FUNCS
199 /****************************************************************************************/
201 ULONG MakeColVal(ULONG val, ULONG bits)
203 ULONG val2;
205 val2 = val << (32 - bits);
206 val = val2;
209 val2 >>= bits;
210 val |= bits;
211 } while(val2);
213 return val;
216 /****************************************************************************************/
218 void REGARGS SpreadColors (GlobData *glob, int from, int to, ULONG *rgb2)
220 LONG colstep;
221 LONG step[3];
222 LONG rgb[3];
223 WORD actcol, steps, gun;
225 colstep = 1;
227 steps = to - from;
228 if (!steps) return;
230 if (steps < 0)
232 steps = -steps;
233 colstep = -1;
236 for(gun = 0; gun < 3; gun++)
238 LONG diff = rgb2[gun] - glob->cols[gun];
239 step[gun] = (diff << 16L) / steps;
240 rgb[gun] = glob->cols[gun] << 16;
243 actcol = from;
245 for(actcol = from;
246 actcol != to;
247 actcol += colstep, rgb[0] += step[0], rgb[1] += step[1], rgb[2] += step[2])
249 ULONG red = (((ULONG)rgb[0]) + 0x8000) >> 16;
250 ULONG green = (((ULONG)rgb[1]) + 0x8000) >> 16;
251 ULONG blue = (((ULONG)rgb[2]) + 0x8000) >> 16;
253 if (GfxBase->LibNode.lib_Version >= 39)
255 SetRGB32(glob->vp, actcol, MakeColVal(red, glob->col.colbits[0]),
256 MakeColVal(green, glob->col.colbits[1]),
257 MakeColVal(blue, glob->col.colbits[2]));
259 else
261 SetRGB4(glob->vp, actcol, red, green, blue);
268 /****************************************************************************************/
270 #endif /* !USE_ASM_FUNCS */
272 /****************************************************************************************/
274 #ifdef DO_CM_DEPTH
275 static int
276 ColBits( int num )
278 int i, j;
280 if( num < 2 )
282 return( 1 );
285 for( i = 1, j = 4; i <= 8; ++i, j <<= 1 )
287 if( j > num )
289 return( i );
293 return( 8 );
295 #endif
297 /****************************************************************************************/
299 LONG ASM SAVEDS PaletteRequestA (
300 REGPARAM(a2, char *, title),
301 REGPARAM(a3, struct rtReqInfo *, reqinfo),
302 REGPARAM(a0, struct TagItem *, taglist))
304 GlobData *glob;
305 struct DisplayInfo displayinfo;
306 struct TagItem *tag;
307 const struct TagItem *tstate = taglist;
308 struct TextFont *deffont = NULL;
309 struct TextAttr *fontattr = NULL;
310 struct Locale *locale = NULL;
311 char *pubname = NULL;
312 ULONG tagdata, reqhandler = FALSE;
314 if (!(glob = AllocVec (sizeof(GlobData), MEMF_PUBLIC|MEMF_CLEAR)))
315 return (-1);
317 glob->os30 = (GfxBase->LibNode.lib_Version >= 39);
318 glob->color = 1;
319 glob->reqpos = REQPOS_DEFAULT;
321 if ((glob->reqinfo = reqinfo))
323 glob->reqpos = reqinfo->ReqPos;
324 glob->leftedge = reqinfo->LeftOffset;
325 glob->topedge = reqinfo->TopOffset;
326 deffont = reqinfo->DefaultFont;
327 glob->waitpointer = reqinfo->WaitPointer;
328 glob->lockwindow = reqinfo->LockWindow;
329 glob->shareidcmp = reqinfo->ShareIDCMP;
330 glob->imsghook = reqinfo->IntuiMsgFunc;
333 /* parse tags */
334 while ((tag = NextTagItem (&tstate)))
336 tagdata = tag->ti_Data;
337 if (tag->ti_Tag > RT_TagBase)
339 switch (tag->ti_Tag)
341 case RT_Window: glob->prwin = (struct Window *)tagdata; break;
342 case RT_ReqPos: glob->reqpos = tagdata; break;
343 case RT_LeftOffset: glob->leftedge = tagdata; break;
344 case RT_TopOffset: glob->topedge = tagdata; break;
345 case RT_PubScrName: pubname = (char *)tagdata; break;
346 case RT_Screen: glob->scr = (struct Screen *)tagdata; break;
347 case RT_ReqHandler: *(APTR *)tagdata = glob;
348 reqhandler = TRUE;
349 break;
350 case RT_DefaultFont: deffont = (struct TextFont *)tagdata; break;
351 case RT_WaitPointer: glob->waitpointer = tagdata; break;
352 case RT_ShareIDCMP: glob->shareidcmp = tagdata; break;
353 case RT_LockWindow: glob->lockwindow = tagdata; break;
354 case RT_ScreenToFront: glob->noscreenpop = !tagdata; break;
355 case RT_TextAttr: fontattr = (struct TextAttr *)tagdata; break;
356 case RT_IntuiMsgFunc: glob->imsghook = (struct Hook *)tagdata; break;
357 case RT_Locale: locale = (struct Locale *)tagdata; break;
358 case RTPA_Color: glob->color = tagdata; break;
363 glob->catalog = RT_OpenCatalog (locale);
365 if (!glob->prwin || !glob->prwin->UserPort ||
366 (glob->prwin->UserPort->mp_SigTask != ThisProcess()))
367 glob->shareidcmp = FALSE;
369 if (!(glob->scr = GetReqScreen (&glob->newpalwin, &glob->prwin, glob->scr, pubname)))
371 FreeAll (glob);
372 return (-1);
374 glob->vp = &glob->scr->ViewPort;
375 glob->cm = glob->vp->ColorMap;
377 if (!(glob->coldepth = GetVpCM (glob->vp, &glob->colormap)) ||
378 !GetVpCM (glob->vp, &glob->undomap))
380 FreeAll (glob);
381 return (-1);
384 glob->colcount = (1 << glob->coldepth);
385 glob->newpalwin.Screen = glob->scr;
387 if (fontattr)
388 glob->font = *fontattr;
389 else
390 glob->font = *glob->scr->Font;
392 glob->redbits = glob->greenbits = glob->bluebits = 4;
393 if (glob->os30)
395 if (myGetDisplayInfoData ((UBYTE *)&displayinfo, sizeof (struct DisplayInfo),
396 DTAG_DISP, GetVPModeID (glob->vp)) > 0)
398 glob->redbits = displayinfo.RedBits;
399 glob->greenbits = displayinfo.GreenBits;
400 glob->bluebits = displayinfo.BlueBits;
401 #ifdef COLORWHEEL
403 #ifdef __AROS__
404 #warning DisplayInfo.Resolution does not seem to have correct/any values yet in AROS
405 glob->screenres.x = glob->screenres.y = 22;
406 #else
407 glob->screenres = displayinfo.Resolution;
408 #endif
410 #endif
414 glob->maxcolval[RED_ID] = (1 << glob->redbits) - 1;
415 glob->maxcolval[GREEN_ID] = (1 << glob->greenbits) - 1;
416 glob->maxcolval[BLUE_ID] = (1 << glob->bluebits) - 1;
418 if (!(glob->visinfo = GetVisualInfoA (glob->scr, NULL)) ||
419 !(glob->drinfo = GetScreenDrawInfo (glob->scr)))
421 FreeAll (glob);
422 return (-1);
425 #ifdef COLORWHEEL
427 struct ReqToolsPrefs *prefs;
429 prefs = rtLockPrefs();
430 glob->dowheel = ( prefs->Flags & RTPRF_DOWHEEL ) && glob->os30;
431 glob->fancywheel = glob->dowheel && ( prefs->Flags & RTPRF_FANCYWHEEL );
432 rtUnlockPrefs();
435 if( glob->dowheel )
437 if ((ColorWheelBase = OpenLibrary ("gadgets/colorwheel.gadget", 39)))
439 GradientSliderBase = OpenLibrary ("gadgets/gradientslider.gadget", 39);
442 #endif
444 SelectColor (glob, glob->color);
446 retryopenwin:
447 if (!(glob->reqfont = GetReqFont (&glob->font, deffont, &glob->fontheight,
448 &glob->fontwidth, glob->os30)))
450 FreeAll (glob);
451 return (-1);
454 if (!SetupPalWindow (glob, title))
456 if( glob->dowheel )
458 glob->dowheel = FALSE;
459 #ifdef COLORWHEEL
460 DisposeObject( glob->wheel );
461 DisposeObject( glob->wheel_slider );
462 glob->wheel = NULL;
463 glob->wheel_slider = NULL;
464 CloseLibrary( ColorWheelBase );
465 CloseLibrary( GradientSliderBase );
466 ColorWheelBase = NULL;
467 GradientSliderBase = NULL;
468 #endif
469 goto retryopenwin;
472 if (glob->font.ta_YSize > 8)
474 glob->font = topaz80;
475 CloseFont (glob->reqfont);
476 goto retryopenwin;
479 FreeAll (glob);
480 return (-1);
483 #ifdef COLORWHEEL
484 if( glob->fancywheel )
486 /* Try to get a fresh undo color map (in case the wheel allocates some) */
487 RefreshVpCM( glob->vp, glob->undomap );
489 /* And make sure selected color still is correct */
490 SelectColor( glob, glob->color );
492 #endif
494 glob->winlock = DoLockWindow (glob->prwin, glob->lockwindow, NULL, TRUE);
495 DoWaitPointer (glob->prwin, glob->waitpointer, TRUE);
497 glob->frontscr = IntuitionBase->FirstScreen;
498 DoScreenToFront (glob->scr, glob->noscreenpop, TRUE);
500 /* fill in RealHandlerInfo */
501 glob->func = (LONG (*)())PalReqHandler;
502 glob->rthi_WaitMask = (1 << glob->palwin->UserPort->mp_SigBit);
504 if (reqhandler) return (CALL_HANDLER);
506 return ((LONG)LoopReqHandler ((struct rtHandlerInfo *)glob));
509 /****************************************************************************************/
511 static LONG ASM SAVEDS PalReqHandler (
512 REGPARAM(a1, struct RealHandlerInfo *, glob),
513 REGPARAM(d0, ULONG, sigs),
514 REGPARAM(a0, struct TagItem *, taglist))
516 struct IntuiMessage *palmsg;
517 struct Gadget *gad;
518 struct TagItem *tag;
519 const struct TagItem *tstate = taglist;
520 ULONG rgb[3], rgbcol;
521 ULONG tagdata, class;
522 UWORD code, qual;
523 int i, gadid, shifted, alt;
524 char key;
526 /* uncomment if sigs is no longer ignored */
527 //if (glob->rthi_DoNotWait) sigs = 0;
529 /* parse tags */
530 while ((tag = NextTagItem (&tstate)))
532 tagdata = tag->ti_Data;
533 if (tag->ti_Tag > RT_TagBase)
535 switch (tag->ti_Tag)
537 case RTRH_EndRequest:
538 if (tagdata == REQ_OK)
540 FreeAll (glob);
541 return (0);
543 RestorePaletteFreeAll (glob);
544 return (-1);
549 while ((palmsg = GetWin_GT_Msg (glob->palwin, glob->imsghook, glob->reqinfo)))
551 class = palmsg->Class;
552 code = palmsg->Code;
553 qual = palmsg->Qualifier;
554 gad = (struct Gadget *)palmsg->IAddress;
555 Reply_GT_Msg (palmsg);
557 switch (class)
559 case IDCMP_REFRESHWINDOW:
560 GT_BeginRefresh (glob->palwin);
561 GT_EndRefresh (glob->palwin, TRUE);
562 break;
564 case IDCMP_CLOSEWINDOW:
565 RestorePaletteFreeAll (glob);
566 return (-1);
568 case IDCMP_MOUSEMOVE:
569 case IDCMP_GADGETDOWN:
570 if (gad->GadgetID <= BLUE_ID)
572 glob->cols[gad->GadgetID] = code;
573 SetColor (glob, glob->color, glob->cols);
574 #ifdef COLORWHEEL
575 UpdateWheel( glob, glob->cols );
576 #endif
578 break;
580 case IDCMP_RAWKEY:
581 case IDCMP_GADGETUP:
582 if (class == IDCMP_RAWKEY)
584 if (!(gadid = CheckGadgetKey (code, qual, &key, &glob->buttoninfo)))
586 /* key press was not for gadget, so check other cases */
587 shifted = qual & (IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT);
588 alt = qual & (IEQUALIFIER_LALT|IEQUALIFIER_RALT);
589 if (key == glob->key[RED_ID])
590 DoColorShortcut (glob, RED_ID, shifted, alt);
591 else if (key == glob->key[GREEN_ID])
592 DoColorShortcut (glob, GREEN_ID, shifted, alt);
593 else if (key == glob->key[BLUE_ID])
594 DoColorShortcut (glob, BLUE_ID, shifted, alt);
595 else if (key == glob->palettekey)
597 code = glob->color;
598 if (!shifted)
600 code++;
601 if (code >= glob->colcount) code = 0;
603 else
605 if (code <= 0) code = glob->colcount;
606 code--;
608 myGT_SetGadgetAttrs (glob->palgad, glob->palwin, NULL,
609 GTPA_Color, code, TAG_END);
610 gadid = PALETTE_ID;
611 glob->mode = 0;
616 } /* if (class == IDCMP_RAWKEY) */
617 else gadid = gad->GadgetID;
619 if (gadid) switch (gadid)
621 case CANCEL_ID:
622 RestorePaletteFreeAll (glob);
623 return (-1);
625 case OK_ID:
626 code = glob->color;
627 FreeAll (glob);
628 return ((LONG)code);
630 case PALETTE_ID:
631 RefreshVpCM (glob->vp, glob->undomap);
633 if (!glob->os30)
635 rgbcol = GetRGB4 (glob->cm, code);
636 for (i = 2; i >= 0; i--)
638 rgb[i] = rgbcol & 0xf;
639 rgbcol >>= 4;
642 else
644 GetRGB32 (glob->cm, code, 1, rgb);
645 rgb[0] >>= (32 - glob->redbits);
646 rgb[1] >>= (32 - glob->greenbits);
647 rgb[2] >>= (32 - glob->bluebits);
650 switch (glob->mode)
652 case SWAP_ID:
653 SetColor (glob, glob->color, rgb);
654 case COPY_ID:
655 SetColor (glob, code, glob->cols);
656 break;
657 case SPREAD_ID:
658 SpreadColors (glob, glob->color, code, rgb);
659 break;
662 SelectColor (glob, code);
663 glob->mode = 0;
664 SetWindowTitles( glob->palwin, glob->newpalwin.Title, NO_TITLE );
665 #ifdef COLORWHEEL
666 UpdateWheel( glob, glob->cols );
667 #endif
668 break;
670 case UNDO_ID:
671 LoadCMap (glob->vp, glob->undomap);
672 SelectColor (glob, glob->color);
673 #ifdef COLORWHEEL
674 UpdateWheel( glob, glob->cols );
675 #endif
676 break;
678 default:
679 if( ( gadid == SWAP_ID ) || ( gadid == COPY_ID ) || ( gadid == SPREAD_ID ) )
681 glob->mode = gadid;
682 SetWindowTitles( glob->palwin, GetStr( glob->catalog, ModeTitles[ gadid - MODE_TITLE_OFFSET ] ), NO_TITLE );
685 break;
687 } /* if (gadid) switch (gadid) */
688 break;
690 #ifdef COLORWHEEL
691 case IDCMP_IDCMPUPDATE:
692 SetWheelColor( glob, ( struct TagItem * ) gad );
693 break;
694 #endif
697 } /* while ((palmsg = GetWin_GT_Msg (glob->palwin, glob->imsghook, glob->reqinfo))) */
699 return (CALL_HANDLER);
702 /****************************************************************************************/
704 static void REGARGS DoColorShortcut (GlobData *glob, int id, int shifted, int alt)
706 int t = glob->cols[id];
708 if (shifted)
710 if (alt) t -= 8; else t--;
711 if (t < 0) t = 0;
713 else
715 if (alt) t += 8; else t++;
716 if (t >= glob->maxcolval[id]) t = glob->maxcolval[id];
719 glob->cols[id] = t;
720 SetColor (glob, glob->color, glob->cols);
721 SelectColor (glob, glob->color);
724 /****************************************************************************************/
726 static void REGARGS SetColor (GlobData *glob, int col, ULONG *rgb)
728 if( !glob->os30 )
730 SetRGB4( glob->vp, col, rgb[ 0 ], rgb[ 1 ], rgb[ 2 ] );
732 else
734 SetRGB32( glob->vp, col,
735 MakeColVal( rgb[ 0 ], glob->redbits ),
736 MakeColVal( rgb[ 1 ], glob->greenbits ),
737 MakeColVal( rgb[ 2 ], glob->bluebits) );
741 /****************************************************************************************/
743 #ifdef COLORWHEEL
745 /****************************************************************************************/
747 #ifdef GRADIENT
749 /****************************************************************************************/
751 static void REGARGS
752 UpdateGrad( GlobData *glob )
754 if( glob->numgradpens )
756 LONG i;
758 GetAttr( WHEEL_HSB, (Object *)glob->wheel, ( IPTR * ) &glob->wheel_hsb );
760 for( i = 0; i < glob->numgradpens; ++i )
762 glob->wheel_hsb.cw_Brightness = 0xffffffff - ( ( 0xffffffff / ( glob->numgradpens - 1 ) ) * i );
763 ConvertHSBToRGB( &glob->wheel_hsb, &glob->wheel_rgb );
764 SetRGB32( &glob->scr->ViewPort, glob->wheel_pens[ i ],
765 glob->wheel_rgb.cw_Red, glob->wheel_rgb.cw_Green,
766 glob->wheel_rgb.cw_Blue );
771 /****************************************************************************************/
773 #endif
775 /****************************************************************************************/
777 static void REGARGS
778 UpdateWheel( GlobData *glob, ULONG *cols )
780 if( glob->wheel )
782 glob->wheel_rgb.cw_Red = MakeColVal( cols[ 0 ], glob->redbits );
783 glob->wheel_rgb.cw_Green = MakeColVal( cols[ 1 ], glob->greenbits );
784 glob->wheel_rgb.cw_Blue = MakeColVal( cols[ 2 ], glob->bluebits );
786 SetGadgetAttrs( glob->wheel, glob->palwin, NULL, WHEEL_RGB, (IPTR) &glob->wheel_rgb,
787 TAG_DONE );
789 #ifdef GRADIENT
790 UpdateGrad( glob );
791 #endif
795 /****************************************************************************************/
797 static void REGARGS
798 SetWheelColor( GlobData *glob, struct TagItem *tag )
800 if( glob->wheel )
802 ULONG i;
804 GetAttr( WHEEL_HSB, (Object *)glob->wheel, ( IPTR * ) &glob->wheel_hsb );
805 ConvertHSBToRGB( &glob->wheel_hsb, &glob->wheel_rgb );
806 glob->cols[ RED_ID ] = glob->wheel_rgb.cw_Red >> ( 32 - glob->redbits );
807 glob->cols[ GREEN_ID ] = glob->wheel_rgb.cw_Green >> ( 32 - glob->greenbits );
808 glob->cols[ BLUE_ID ] = glob->wheel_rgb.cw_Blue >> ( 32 - glob->bluebits );
809 SetColor( glob, glob->color, glob->cols );
811 for( i = 0; i < 3; ++i )
813 if( glob->colgad[ i ] )
815 myGT_SetGadgetAttrs( glob->colgad[ i ], glob->palwin, NULL, GTSL_Level, glob->cols[ i ],
816 TAG_END );
820 #ifdef GRADIENT
821 UpdateGrad( glob );
822 #endif
826 /****************************************************************************************/
828 #endif
830 /****************************************************************************************/
832 static void REGARGS SelectColor (GlobData *glob, int col)
834 ULONG rgb[3], rgbcol = 0;
835 int i;
837 if (!glob->os30) rgbcol = GetRGB4 (glob->cm, col);
838 else GetRGB32 (glob->cm, col, 1, rgb);
840 for (i = 2; i >= 0; i--)
842 if (!glob->os30)
844 glob->cols[i] = rgbcol & 0xF;
845 rgbcol >>= 4;
847 else glob->cols[i] = rgb[i] >> (32 - glob->col.colbits[i]);
849 if (glob->colgad[i])
850 myGT_SetGadgetAttrs (glob->colgad[i], glob->palwin, NULL,
851 GTSL_Level, glob->cols[i], TAG_END);
854 glob->color = col;
857 /****************************************************************************************/
859 static void REGARGS RestorePaletteFreeAll (GlobData *glob)
861 LoadCMap (glob->vp, glob->colormap);
862 FreeAll (glob);
865 /****************************************************************************************/
867 static void REGARGS FreeAll (GlobData *glob)
869 #ifdef COLORWHEEL
870 #ifdef GRADIENT
871 LONG i;
872 #endif
873 #endif
874 if (glob->newpalwin.Type == PUBLICSCREEN) UnlockPubScreen (NULL, glob->scr);
875 DoScreenToFront (glob->frontscr, glob->noscreenpop, FALSE);
877 if (glob->palwin)
879 DoLockWindow (glob->prwin, glob->lockwindow, glob->winlock, FALSE);
880 DoWaitPointer (glob->prwin, glob->waitpointer, FALSE);
881 DoCloseWindow (glob->palwin, glob->shareidcmp);
884 RT_CloseCatalog (glob->catalog);
885 my_FreeGadgets (glob->buttoninfo.glist);
886 my_FreeLabelImages (&glob->labelimages);
888 #ifdef COLORWHEEL
889 DisposeObject (glob->wheel);
890 DisposeObject (glob->wheel_slider);
892 #ifdef GRADIENT
893 if( glob->numgradpens )
895 for( i = 0; glob->wheel_pens[ i ] != (UWORD)~0; i++ )
897 ReleasePen( glob->scr->ViewPort.ColorMap, glob->wheel_pens[ i ] );
900 #endif
902 CloseLibrary (GradientSliderBase);
903 CloseLibrary (ColorWheelBase);
904 #endif
906 FreeVisualInfo (glob->visinfo);
908 if (glob->drinfo) FreeScreenDrawInfo (glob->scr, glob->drinfo);
909 if (glob->reqfont) CloseFont (glob->reqfont);
911 FreeVpCM (glob->vp, glob->colormap, FALSE);
912 FreeVpCM (glob->vp, glob->undomap, FALSE);
913 FreeVec (glob);
916 /****************************************************************************************/
918 char *colstr[] = { MSG_RED, MSG_GREEN, MSG_BLUE };
919 char *gadtxt[] = { MSG_COPY, MSG_SWAP, MSG_SPREAD, MSG_OK, MSG_UNDO, MSG_CANCEL };
921 /****************************************************************************************/
923 static int REGARGS SetupPalWindow (GlobData *glob, char *title)
925 struct NewGadget ng;
926 struct Gadget *gad;
927 struct Image *img;
928 int val, i, top, buttonheight, winheight;
929 int spacing, scrwidth, scrheight, maxwidth;
930 int winwidth, width1, width2, reqpos, levelwidth;
931 #ifdef COLORWHEEL
932 int wheelwidth = 0, wheelheight = 0, wheeltopoff;
933 #endif
934 int wheeloff = 0;
935 int leftoff, rightoff;
936 ULONG gadlen[6], gadpos[6];
937 char *str, *string[6];
940 spacing = rtGetVScreenSize (glob->scr, (ULONG *)&scrwidth, (ULONG *)&scrheight);
941 leftoff = glob->scr->WBorLeft + 5;
942 rightoff = glob->scr->WBorRight + 5;
944 top = (glob->scr->WBorTop + glob->scr->Font->ta_YSize + 1) + spacing / 2 + 1;
945 glob->itxt.ITextFont = &glob->font;
947 #ifdef COLORWHEEL
948 wheeltopoff = top;
949 #endif
951 width1 = width2 = 0;
953 for (i = 0; i < 6; i++)
955 string[i] = GetStr (glob->catalog, gadtxt[i]);
956 val = StrWidth_noloc (&glob->itxt, string[i]) + 16;
957 if (i < 3)
959 if (val > width1) width1 = val;
961 else
963 if (val > width2) width2 = val;
967 for (i = 0; i < 3; i++) gadlen[i] = width1;
968 for (i = 3; i < 6; i++) gadlen[i] = width2;
970 width1 *= 3;
971 width2 *= 3;
973 winwidth = (leftoff + rightoff) + 25 + width1 + 2 * 8;
974 val = (leftoff + rightoff) + width2 + 2 * 8;
975 if (val > winwidth) winwidth = val;
976 if (winwidth < 256) winwidth = 256;
978 val = glob->fontheight * 2 + 4;
979 #ifdef __AROS__
980 val += 5;
981 #endif
983 if (glob->colcount >= 64) val *= 2;
984 if (glob->colcount >= 128) val *= 2;
987 #ifdef COLORWHEEL
988 if( glob->dowheel && glob->screenres.x && glob->screenres.y && ColorWheelBase && GradientSliderBase )
990 LONG maxheight = 120;
992 if( scrheight > 600 )
994 maxheight = 160;
996 else if( scrheight < 300 )
998 maxheight = 75;
1001 wheelheight = val + glob->fontheight * 4 + 6 + 3 + spacing * 3;
1003 if( wheelheight > maxheight )
1005 wheelheight = maxheight;
1008 #ifndef __AROS__
1009 #warning Changed, because gcc produced wrong code! gcc 2.95.1 compiled under UAE JIT for Linux!?
1010 wheelwidth = glob->screenres.y;
1011 wheelwidth *= wheelheight;
1012 wheelwidth /= glob->screenres.x;
1013 #else
1014 wheelwidth = wheelheight * glob->screenres.y / glob->screenres.x;
1015 #endif
1017 if( ( scrwidth - winwidth > wheelwidth + 8 ) &&
1018 ( scrheight > 200 ) )
1020 wheeloff = wheelwidth + 8;
1021 winwidth += wheeloff;
1023 else
1025 glob->dowheel = FALSE;
1028 } /* if( glob->dowheel && glob->screenres.x && glob->screenres.y && ColorWheelBase && GradientSliderBase ) */
1029 else
1031 glob->dowheel = FALSE;
1033 #endif
1036 rtSpread (gadpos, gadlen, width1, leftoff + wheeloff + 25, winwidth - rightoff, 3);
1037 rtSpread (gadpos + 3, gadlen + 3, width2, leftoff, winwidth - rightoff, 3);
1040 gad = (struct Gadget *)CreateContext (&glob->buttoninfo.glist);
1041 img = &glob->labelimages;
1042 ng.ng_Flags = 0;
1043 ng.ng_VisualInfo = glob->visinfo;
1044 ng.ng_TextAttr = &glob->font;
1046 str = GetStr (glob->catalog, MSG_PALETTE_COLORS);
1047 glob->palettekey = KeyFromStr (str, '_');
1049 i = leftoff + 25 + ( winwidth - ( leftoff + rightoff + 25 ) -
1050 StrWidth_noloc( &glob->itxt, str ) ) / 2;
1052 #ifdef COLORWHEEL
1053 if( i < wheeloff )
1055 i = wheeloff;
1057 #endif
1059 img = my_CreateGadgetLabelImage (img, &ng, str, i, top, HIGHLIGHTTEXTPEN);
1060 top += glob->fontheight + 1 + spacing / 2;
1062 InitNewGadget (&ng, leftoff + wheeloff + 25, top, winwidth - ( wheeloff + leftoff + rightoff + 25),
1063 val, NULL, PALETTE_ID);
1065 gad = glob->palgad = myCreateGadget (PALETTE_KIND, gad, &ng,
1066 GTPA_Depth, (IPTR)glob->coldepth,
1067 GTPA_IndicatorWidth, 38,
1068 GTPA_Color, (IPTR)glob->color, TAG_END);
1070 if (glob->os30) top += gad->Height + spacing;
1071 else top += val + spacing;
1073 buttonheight = glob->fontheight + 6;
1075 for (i = 0; i < 3; i++)
1077 InitNewGadget (&ng, gadpos[i], top, gadlen[i],
1078 buttonheight, string[i], COPY_ID + i);
1080 gad = my_CreateButtonGadget (gad, '_', &ng);
1083 top += buttonheight + spacing;
1085 ng.ng_Flags |= NG_HIGHLABEL;
1087 maxwidth = 0;
1089 for (i = 0; i < 3; i++)
1091 string[i] = GetStr (glob->catalog, colstr[i]);
1092 glob->key[i] = KeyFromStr (string[i], '_');
1093 val = StrWidth_noloc (&glob->itxt, string[i]);
1094 if (val > maxwidth) maxwidth = val;
1097 levelwidth = StrWidth_noloc (&glob->itxt, "000 ");
1098 maxwidth += levelwidth;
1101 for (i = 0; i < 3; i++)
1104 val = leftoff + wheeloff + 2 + maxwidth + 8;
1105 InitNewGadget (&ng, val, top, winwidth - val - rightoff,
1106 glob->fontheight + 6, NULL, RED_ID + i);
1108 glob->colgad[i] = gad = myCreateGadget (SLIDER_KIND, gad, &ng,
1109 GTSL_LevelFormat, (IPTR) "%3ld",
1110 GTSL_LevelPlace, PLACETEXT_LEFT,
1111 GTSL_MaxLevelLen, 3, GA_RelVerify, TRUE, GA_Immediate, TRUE,
1112 GTSL_Level, glob->cols[i],
1113 GTSL_Max, glob->maxcolval[i],
1114 ((GfxBase->LibNode.lib_Version >= 40) ? GTSL_Justification : TAG_IGNORE), GTJ_RIGHT,
1115 GTSL_MaxPixelLen, levelwidth,
1116 TAG_END);
1118 img = my_CreateGadgetLabelImage (img, &ng, string[i],
1119 leftoff + wheeloff + 2, top + 2, HIGHLIGHTTEXTPEN);
1120 top += glob->fontheight + 6 + spacing / 2;
1124 top += spacing / 2;
1125 ng.ng_Flags &= ~NG_HIGHLABEL;
1128 #ifdef COLORWHEEL
1129 if( glob->dowheel )
1131 int wheeltop = top - ( wheelheight + glob->fontheight + 3 + spacing * 2 );
1133 if( wheeltop < wheeltopoff )
1135 top += wheeltopoff - wheeltop;
1136 wheeltop = wheeltopoff;
1139 #ifdef GRADIENT
1141 if( glob->fancywheel )
1144 glob->numgradpens = LOWGRADPENS;
1146 if( glob->scr->ViewPort.ColorMap
1147 && glob->scr->ViewPort.ColorMap->PalExtra
1148 && ( glob->scr->ViewPort.ColorMap->PalExtra->pe_NFree > FREEFORHIGH ) )
1150 glob->numgradpens = HIGHGRADPENS;
1153 /* get the RGB components of active color */
1154 glob->wheel_rgb.cw_Red = glob->cols[ RED_ID ];
1155 glob->wheel_rgb.cw_Green = glob->cols[ GREEN_ID ];
1156 glob->wheel_rgb.cw_Blue = glob->cols[ BLUE_ID ];
1158 /* now convert the RGB values to HSB, and max out B component */
1159 ConvertRGBToHSB( &glob->wheel_rgb, &glob->wheel_hsb );
1160 glob->wheel_hsb.cw_Brightness = 0xffffffff;
1163 /* Set up colors for gradient slider */
1164 for( i = 0; i < glob->numgradpens; i++ )
1166 glob->wheel_hsb.cw_Brightness = 0xffffffff
1167 - ( ( 0xffffffff / ( glob->numgradpens - 1 ) ) * ( ULONG ) i );
1168 ConvertHSBToRGB( &glob->wheel_hsb, &glob->wheel_rgb );
1169 glob->wheel_pens[ i ] = ObtainPen( glob->scr->ViewPort.ColorMap,
1170 -1, glob->wheel_rgb.cw_Red, glob->wheel_rgb.cw_Green,
1171 glob->wheel_rgb.cw_Blue, PEN_EXCLUSIVE );
1173 if( glob->wheel_pens[ i ] == (UWORD)~0 )
1175 break;
1180 glob->wheel_pens[ i ] = ( UWORD ) ~0;
1182 if( i != glob->numgradpens )
1184 for( i = 0; glob->wheel_pens[ i ] != (UWORD)~0; i++ )
1186 ReleasePen( glob->scr->ViewPort.ColorMap, glob->wheel_pens[ i ] );
1189 glob->numgradpens = 0;
1192 } /* if( glob->fancywheel ) */
1193 #endif
1196 struct TagItem slider_tags[] =
1198 {GA_ID , SLIDER_ID },
1199 {GA_Top , wheeltop + wheelheight + spacing },
1200 {GA_Left , leftoff },
1201 {GA_Width , wheelwidth },
1202 {GA_Height , glob->fontheight + 3 },
1203 #ifdef GRADIENT
1204 {glob->numgradpens ?
1205 GRAD_PenArray :
1206 TAG_IGNORE , (IPTR)glob->wheel_pens },
1207 #endif
1208 {GRAD_KnobPixels , 8 },
1209 {PGA_Freedom , LORIENT_HORIZ },
1210 {ICA_TARGET , ICTARGET_IDCMP },
1211 {TAG_END }
1214 glob->wheel_slider = (struct Gadget *)NewObjectA(NULL, "gradientslider.gadget", slider_tags);
1218 if( glob->wheel_slider )
1220 struct TagItem wheel_tags[] =
1222 {GA_Top , wheeltop },
1223 {GA_Left , leftoff },
1224 {GA_Width , wheelwidth },
1225 {GA_Height , wheelheight },
1226 {GA_ID , WHEEL_ID },
1227 {WHEEL_Screen , (IPTR)glob->scr },
1228 {glob->fancywheel ?
1229 TAG_IGNORE :
1230 WHEEL_MaxPens , 0 },
1231 {WHEEL_GradientSlider , (IPTR)glob->wheel_slider },
1232 #ifdef __AROS__
1233 /* Need this, because without BevelBox AROS colorwheel gadget renders through mask
1234 which because of bugs in gfx library functions (!?) does not work yet and instead
1235 causes mem trashes/crashes/etc (in AmigaOS the AROS colorwheel gadget works fine
1236 even without BevelBox, that is: with mask rendering) */
1237 {WHEEL_BevelBox , TRUE },
1238 #endif
1239 {GA_Previous , (IPTR)glob->wheel_slider },
1240 {ICA_TARGET , ICTARGET_IDCMP },
1241 {TAG_END }
1244 glob->wheel = (struct Gadget *)NewObjectA(NULL, "colorwheel.gadget", wheel_tags);
1248 } /* if( glob->dowheel )*/
1249 #endif
1252 for (i = 3; i < 6; i++)
1254 InitNewGadget (&ng, gadpos[i], top, gadlen[i],
1255 buttonheight, string[i], COPY_ID + i);
1257 gad = my_CreateButtonGadget (gad, '_', &ng);
1260 top += buttonheight + spacing;
1262 winheight = top + glob->scr->WBorBottom;
1263 glob->newpalwin.Height = winheight;
1264 glob->newpalwin.Width = winwidth;
1265 glob->newpalwin.IDCMPFlags = glob->shareidcmp ? 0 :
1266 SLIDERIDCMP|PALETTEIDCMP|BUTTONIDCMP|IDCMP_CLOSEWINDOW|IDCMP_RAWKEY
1267 |IDCMP_REFRESHWINDOW|IDCMP_IDCMPUPDATE;
1268 glob->newpalwin.Flags = WFLG_DEPTHGADGET|WFLG_DRAGBAR|WFLG_ACTIVATE
1269 |WFLG_SIMPLE_REFRESH|WFLG_RMBTRAP|WFLG_CLOSEGADGET;
1270 glob->newpalwin.DetailPen = glob->drinfo->dri_Pens[BACKGROUNDPEN];
1271 glob->newpalwin.BlockPen = glob->drinfo->dri_Pens[SHADOWPEN];
1272 glob->newpalwin.Title = title;
1273 glob->newpalwin.LeftEdge = glob->leftedge;
1274 glob->newpalwin.TopEdge = glob->topedge;
1276 reqpos = CheckReqPos (glob->reqpos, RTPREF_PALETTEREQ, &glob->newpalwin);
1278 if (reqpos == REQPOS_POINTER)
1280 glob->newpalwin.LeftEdge = -winwidth / 2;
1281 glob->newpalwin.TopEdge = -winheight / 2;
1284 rtSetReqPosition (reqpos, &glob->newpalwin, glob->scr, glob->prwin);
1286 ng.ng_LeftEdge = ng.ng_TopEdge = ng.ng_Width = ng.ng_Height = 0;
1287 ng.ng_GadgetText = NULL;
1288 gad = myCreateGadget (GENERIC_KIND, gad, &ng, TAG_END);
1290 if( gad )
1292 gad->GadgetType |= GTYP_BOOLGADGET;
1293 gad->Flags |= GFLG_GADGIMAGE|GFLG_GADGHNONE;
1294 gad->GadgetRender = ( APTR ) glob->labelimages.NextImage;
1298 glob->zoom[ 2 ] = glob->newpalwin.Width;
1299 glob->zoom[ 3 ] = glob->scr->WBorTop + glob->scr->Font->ta_YSize + 1;
1301 if( ( glob->dowheel && !glob->wheel ) || !img || !gad ||
1302 !(glob->palwin = OpenWindowBF( &glob->newpalwin,
1303 &glob->backfillhook, glob->drinfo->dri_Pens, NULL, glob->zoom, FALSE ) ) )
1305 my_FreeGadgets( glob->buttoninfo.glist );
1306 glob->buttoninfo.glist = NULL;
1307 my_FreeLabelImages( &glob->labelimages );
1308 glob->labelimages.NextImage = NULL;
1309 DisposeObject( glob->wheel );
1310 glob->wheel = NULL;
1311 DisposeObject( glob->wheel_slider );
1312 glob->wheel_slider = NULL;
1313 return( 0 );
1316 glob->buttoninfo.win = glob->palwin;
1318 if( glob->shareidcmp )
1320 glob->palwin->UserPort = glob->prwin->UserPort;
1321 ModifyIDCMP( glob->palwin,
1322 SLIDERIDCMP | PALETTEIDCMP | BUTTONIDCMP | IDCMP_CLOSEWINDOW |
1323 IDCMP_RAWKEY | IDCMP_REFRESHWINDOW | IDCMP_IDCMPUPDATE );
1326 #ifdef COLORWHEEL
1327 if( glob->wheel )
1329 AddGList( glob->palwin, glob->wheel_slider, -1, -1, NULL );
1330 RefreshGList( glob->wheel_slider, glob->palwin, NULL, -1 );
1332 #endif
1334 AddGList( glob->palwin, glob->buttoninfo.glist, -1, -1, NULL );
1335 RefreshGList( glob->buttoninfo.glist, glob->palwin, NULL, -1 );
1336 GT_RefreshWindow( glob->palwin, NULL );
1337 UpdateWheel( glob, glob->cols );
1339 return( 1 );
1342 /****************************************************************************************/