Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / libs / reqtools / palettereq.c
blob5dcbfca0b74d6e77c40f470c8f86f68e6d373c64
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, *tstate = taglist;
307 struct TextFont *deffont = NULL;
308 struct TextAttr *fontattr = NULL;
309 struct Locale *locale = NULL;
310 char *pubname = NULL;
311 IPTR tagdata;
312 BOOL 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 glob->screenres = displayinfo.Resolution;
405 #endif
409 glob->maxcolval[RED_ID] = (1 << glob->redbits) - 1;
410 glob->maxcolval[GREEN_ID] = (1 << glob->greenbits) - 1;
411 glob->maxcolval[BLUE_ID] = (1 << glob->bluebits) - 1;
413 if (!(glob->visinfo = GetVisualInfoA (glob->scr, NULL)) ||
414 !(glob->drinfo = GetScreenDrawInfo (glob->scr)))
416 FreeAll (glob);
417 return (-1);
420 #ifdef COLORWHEEL
422 struct ReqToolsPrefs *prefs;
424 prefs = rtLockPrefs();
425 glob->dowheel = ( prefs->Flags & RTPRF_DOWHEEL ) && glob->os30;
426 glob->fancywheel = glob->dowheel && ( prefs->Flags & RTPRF_FANCYWHEEL );
427 rtUnlockPrefs();
430 if( glob->dowheel )
432 if ((ColorWheelBase = OpenLibrary ("gadgets/colorwheel.gadget", 39)))
434 GradientSliderBase = OpenLibrary ("gadgets/gradientslider.gadget", 39);
437 #endif
439 SelectColor (glob, glob->color);
441 retryopenwin:
442 if (!(glob->reqfont = GetReqFont (&glob->font, deffont, &glob->fontheight,
443 &glob->fontwidth, glob->os30)))
445 FreeAll (glob);
446 return (-1);
449 if (!SetupPalWindow (glob, title))
451 if( glob->dowheel )
453 glob->dowheel = FALSE;
454 #ifdef COLORWHEEL
455 DisposeObject( glob->wheel );
456 DisposeObject( glob->wheel_slider );
457 glob->wheel = NULL;
458 glob->wheel_slider = NULL;
459 CloseLibrary( ColorWheelBase );
460 CloseLibrary( GradientSliderBase );
461 ColorWheelBase = NULL;
462 GradientSliderBase = NULL;
463 #endif
464 goto retryopenwin;
467 if (glob->font.ta_YSize > 8)
469 glob->font = topaz80;
470 CloseFont (glob->reqfont);
471 goto retryopenwin;
474 FreeAll (glob);
475 return (-1);
478 #ifdef COLORWHEEL
479 if( glob->fancywheel )
481 /* Try to get a fresh undo color map (in case the wheel allocates some) */
482 RefreshVpCM( glob->vp, glob->undomap );
484 /* And make sure selected color still is correct */
485 SelectColor( glob, glob->color );
487 #endif
489 glob->winlock = DoLockWindow (glob->prwin, glob->lockwindow, NULL, TRUE);
490 DoWaitPointer (glob->prwin, glob->waitpointer, TRUE);
492 glob->frontscr = IntuitionBase->FirstScreen;
493 DoScreenToFront (glob->scr, glob->noscreenpop, TRUE);
495 /* fill in RealHandlerInfo */
496 glob->func = (LONG (*)())PalReqHandler;
497 glob->rthi_WaitMask = (1 << glob->palwin->UserPort->mp_SigBit);
499 if (reqhandler) return (CALL_HANDLER);
501 return ((LONG)LoopReqHandler ((struct rtHandlerInfo *)glob));
504 /****************************************************************************************/
506 static LONG ASM SAVEDS PalReqHandler (
507 REGPARAM(a1, struct RealHandlerInfo *, glob),
508 REGPARAM(d0, ULONG, sigs),
509 REGPARAM(a0, struct TagItem *, taglist))
511 struct IntuiMessage *palmsg;
512 struct Gadget *gad;
513 struct TagItem *tag;
514 struct TagItem *tstate = taglist;
515 ULONG rgb[3], rgbcol;
516 ULONG tagdata, class;
517 UWORD code, qual;
518 int i, gadid, shifted, alt;
519 char key;
521 /* uncomment if sigs is no longer ignored */
522 //if (glob->rthi_DoNotWait) sigs = 0;
524 /* parse tags */
525 while ((tag = NextTagItem (&tstate)))
527 tagdata = tag->ti_Data;
528 if (tag->ti_Tag > RT_TagBase)
530 switch (tag->ti_Tag)
532 case RTRH_EndRequest:
533 if (tagdata == REQ_OK)
535 FreeAll (glob);
536 return (0);
538 RestorePaletteFreeAll (glob);
539 return (-1);
544 while ((palmsg = GetWin_GT_Msg (glob->palwin, glob->imsghook, glob->reqinfo)))
546 class = palmsg->Class;
547 code = palmsg->Code;
548 qual = palmsg->Qualifier;
549 gad = (struct Gadget *)palmsg->IAddress;
550 Reply_GT_Msg (palmsg);
552 switch (class)
554 case IDCMP_REFRESHWINDOW:
555 GT_BeginRefresh (glob->palwin);
556 GT_EndRefresh (glob->palwin, TRUE);
557 break;
559 case IDCMP_CLOSEWINDOW:
560 RestorePaletteFreeAll (glob);
561 return (-1);
563 case IDCMP_MOUSEMOVE:
564 case IDCMP_GADGETDOWN:
565 if (gad->GadgetID <= BLUE_ID)
567 glob->cols[gad->GadgetID] = code;
568 SetColor (glob, glob->color, glob->cols);
569 #ifdef COLORWHEEL
570 UpdateWheel( glob, glob->cols );
571 #endif
573 break;
575 case IDCMP_RAWKEY:
576 case IDCMP_GADGETUP:
577 if (class == IDCMP_RAWKEY)
579 if (!(gadid = CheckGadgetKey (code, qual, &key, &glob->buttoninfo)))
581 /* key press was not for gadget, so check other cases */
582 shifted = qual & (IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT);
583 alt = qual & (IEQUALIFIER_LALT|IEQUALIFIER_RALT);
584 if (key == glob->key[RED_ID])
585 DoColorShortcut (glob, RED_ID, shifted, alt);
586 else if (key == glob->key[GREEN_ID])
587 DoColorShortcut (glob, GREEN_ID, shifted, alt);
588 else if (key == glob->key[BLUE_ID])
589 DoColorShortcut (glob, BLUE_ID, shifted, alt);
590 else if (key == glob->palettekey)
592 code = glob->color;
593 if (!shifted)
595 code++;
596 if (code >= glob->colcount) code = 0;
598 else
600 if (code <= 0) code = glob->colcount;
601 code--;
603 myGT_SetGadgetAttrs (glob->palgad, glob->palwin, NULL,
604 GTPA_Color, code, TAG_END);
605 gadid = PALETTE_ID;
606 glob->mode = 0;
611 } /* if (class == IDCMP_RAWKEY) */
612 else gadid = gad->GadgetID;
614 if (gadid) switch (gadid)
616 case CANCEL_ID:
617 RestorePaletteFreeAll (glob);
618 return (-1);
620 case OK_ID:
621 code = glob->color;
622 FreeAll (glob);
623 return ((LONG)code);
625 case PALETTE_ID:
626 RefreshVpCM (glob->vp, glob->undomap);
628 if (!glob->os30)
630 rgbcol = GetRGB4 (glob->cm, code);
631 for (i = 2; i >= 0; i--)
633 rgb[i] = rgbcol & 0xf;
634 rgbcol >>= 4;
637 else
639 GetRGB32 (glob->cm, code, 1, rgb);
640 rgb[0] >>= (32 - glob->redbits);
641 rgb[1] >>= (32 - glob->greenbits);
642 rgb[2] >>= (32 - glob->bluebits);
645 switch (glob->mode)
647 case SWAP_ID:
648 SetColor (glob, glob->color, rgb);
649 case COPY_ID:
650 SetColor (glob, code, glob->cols);
651 break;
652 case SPREAD_ID:
653 SpreadColors (glob, glob->color, code, rgb);
654 break;
657 SelectColor (glob, code);
658 glob->mode = 0;
659 SetWindowTitles( glob->palwin, glob->newpalwin.Title, NO_TITLE );
660 #ifdef COLORWHEEL
661 UpdateWheel( glob, glob->cols );
662 #endif
663 break;
665 case UNDO_ID:
666 LoadCMap (glob->vp, glob->undomap);
667 SelectColor (glob, glob->color);
668 #ifdef COLORWHEEL
669 UpdateWheel( glob, glob->cols );
670 #endif
671 break;
673 default:
674 if( ( gadid == SWAP_ID ) || ( gadid == COPY_ID ) || ( gadid == SPREAD_ID ) )
676 glob->mode = gadid;
677 SetWindowTitles( glob->palwin, GetStr( glob->catalog, ModeTitles[ gadid - MODE_TITLE_OFFSET ] ), NO_TITLE );
680 break;
682 } /* if (gadid) switch (gadid) */
683 break;
685 #ifdef COLORWHEEL
686 case IDCMP_IDCMPUPDATE:
687 SetWheelColor( glob, ( struct TagItem * ) gad );
688 break;
689 #endif
692 } /* while ((palmsg = GetWin_GT_Msg (glob->palwin, glob->imsghook, glob->reqinfo))) */
694 return (CALL_HANDLER);
697 /****************************************************************************************/
699 static void REGARGS DoColorShortcut (GlobData *glob, int id, int shifted, int alt)
701 int t = glob->cols[id];
703 if (shifted)
705 if (alt) t -= 8; else t--;
706 if (t < 0) t = 0;
708 else
710 if (alt) t += 8; else t++;
711 if (t >= glob->maxcolval[id]) t = glob->maxcolval[id];
714 glob->cols[id] = t;
715 SetColor (glob, glob->color, glob->cols);
716 SelectColor (glob, glob->color);
719 /****************************************************************************************/
721 static void REGARGS SetColor (GlobData *glob, int col, ULONG *rgb)
723 if( !glob->os30 )
725 SetRGB4( glob->vp, col, rgb[ 0 ], rgb[ 1 ], rgb[ 2 ] );
727 else
729 SetRGB32( glob->vp, col,
730 MakeColVal( rgb[ 0 ], glob->redbits ),
731 MakeColVal( rgb[ 1 ], glob->greenbits ),
732 MakeColVal( rgb[ 2 ], glob->bluebits) );
736 /****************************************************************************************/
738 #ifdef COLORWHEEL
740 /****************************************************************************************/
742 #ifdef GRADIENT
744 /****************************************************************************************/
746 static void REGARGS
747 UpdateGrad( GlobData *glob )
749 if( glob->numgradpens )
751 LONG i;
753 GetAttr( WHEEL_HSB, (Object *)glob->wheel, ( IPTR * ) &glob->wheel_hsb );
755 for( i = 0; i < glob->numgradpens; ++i )
757 glob->wheel_hsb.cw_Brightness = 0xffffffff - ( ( 0xffffffff / ( glob->numgradpens - 1 ) ) * i );
758 ConvertHSBToRGB( &glob->wheel_hsb, &glob->wheel_rgb );
759 SetRGB32( &glob->scr->ViewPort, glob->wheel_pens[ i ],
760 glob->wheel_rgb.cw_Red, glob->wheel_rgb.cw_Green,
761 glob->wheel_rgb.cw_Blue );
766 /****************************************************************************************/
768 #endif
770 /****************************************************************************************/
772 static void REGARGS
773 UpdateWheel( GlobData *glob, ULONG *cols )
775 if( glob->wheel )
777 glob->wheel_rgb.cw_Red = MakeColVal( cols[ 0 ], glob->redbits );
778 glob->wheel_rgb.cw_Green = MakeColVal( cols[ 1 ], glob->greenbits );
779 glob->wheel_rgb.cw_Blue = MakeColVal( cols[ 2 ], glob->bluebits );
781 SetGadgetAttrs( glob->wheel, glob->palwin, NULL, WHEEL_RGB, (IPTR) &glob->wheel_rgb,
782 TAG_DONE );
784 #ifdef GRADIENT
785 UpdateGrad( glob );
786 #endif
790 /****************************************************************************************/
792 static void REGARGS
793 SetWheelColor( GlobData *glob, struct TagItem *tag )
795 if( glob->wheel )
797 ULONG i;
799 GetAttr( WHEEL_HSB, (Object *)glob->wheel, ( IPTR * ) &glob->wheel_hsb );
800 ConvertHSBToRGB( &glob->wheel_hsb, &glob->wheel_rgb );
801 glob->cols[ RED_ID ] = glob->wheel_rgb.cw_Red >> ( 32 - glob->redbits );
802 glob->cols[ GREEN_ID ] = glob->wheel_rgb.cw_Green >> ( 32 - glob->greenbits );
803 glob->cols[ BLUE_ID ] = glob->wheel_rgb.cw_Blue >> ( 32 - glob->bluebits );
804 SetColor( glob, glob->color, glob->cols );
806 for( i = 0; i < 3; ++i )
808 if( glob->colgad[ i ] )
810 myGT_SetGadgetAttrs( glob->colgad[ i ], glob->palwin, NULL, GTSL_Level, glob->cols[ i ],
811 TAG_END );
815 #ifdef GRADIENT
816 UpdateGrad( glob );
817 #endif
821 /****************************************************************************************/
823 #endif
825 /****************************************************************************************/
827 static void REGARGS SelectColor (GlobData *glob, int col)
829 ULONG rgb[3], rgbcol = 0;
830 int i;
832 if (!glob->os30) rgbcol = GetRGB4 (glob->cm, col);
833 else GetRGB32 (glob->cm, col, 1, rgb);
835 for (i = 2; i >= 0; i--)
837 if (!glob->os30)
839 glob->cols[i] = rgbcol & 0xF;
840 rgbcol >>= 4;
842 else glob->cols[i] = rgb[i] >> (32 - glob->col.colbits[i]);
844 if (glob->colgad[i])
845 myGT_SetGadgetAttrs (glob->colgad[i], glob->palwin, NULL,
846 GTSL_Level, glob->cols[i], TAG_END);
849 glob->color = col;
852 /****************************************************************************************/
854 static void REGARGS RestorePaletteFreeAll (GlobData *glob)
856 LoadCMap (glob->vp, glob->colormap);
857 FreeAll (glob);
860 /****************************************************************************************/
862 static void REGARGS FreeAll (GlobData *glob)
864 #ifdef COLORWHEEL
865 #ifdef GRADIENT
866 LONG i;
867 #endif
868 #endif
869 if (glob->newpalwin.Type == PUBLICSCREEN) UnlockPubScreen (NULL, glob->scr);
870 DoScreenToFront (glob->frontscr, glob->noscreenpop, FALSE);
872 if (glob->palwin)
874 DoLockWindow (glob->prwin, glob->lockwindow, glob->winlock, FALSE);
875 DoWaitPointer (glob->prwin, glob->waitpointer, FALSE);
876 DoCloseWindow (glob->palwin, glob->shareidcmp);
879 RT_CloseCatalog (glob->catalog);
880 my_FreeGadgets (glob->buttoninfo.glist);
881 my_FreeLabelImages (&glob->labelimages);
883 #ifdef COLORWHEEL
884 DisposeObject (glob->wheel);
885 DisposeObject (glob->wheel_slider);
887 #ifdef GRADIENT
888 if( glob->numgradpens )
890 for( i = 0; glob->wheel_pens[ i ] != (UWORD)~0; i++ )
892 ReleasePen( glob->scr->ViewPort.ColorMap, glob->wheel_pens[ i ] );
895 #endif
897 CloseLibrary (GradientSliderBase);
898 CloseLibrary (ColorWheelBase);
899 #endif
901 FreeVisualInfo (glob->visinfo);
903 if (glob->drinfo) FreeScreenDrawInfo (glob->scr, glob->drinfo);
904 if (glob->reqfont) CloseFont (glob->reqfont);
906 FreeVpCM (glob->vp, glob->colormap, FALSE);
907 FreeVpCM (glob->vp, glob->undomap, FALSE);
908 FreeVec (glob);
911 /****************************************************************************************/
913 char *colstr[] = { MSG_RED, MSG_GREEN, MSG_BLUE };
914 char *gadtxt[] = { MSG_COPY, MSG_SWAP, MSG_SPREAD, MSG_OK, MSG_UNDO, MSG_CANCEL };
916 /****************************************************************************************/
918 static int REGARGS SetupPalWindow (GlobData *glob, char *title)
920 struct NewGadget ng;
921 struct Gadget *gad;
922 struct Image *img;
923 int val, i, top, buttonheight, winheight;
924 int spacing, scrwidth, scrheight, maxwidth;
925 int winwidth, width1, width2, reqpos, levelwidth;
926 #ifdef COLORWHEEL
927 int wheelwidth = 0, wheelheight = 0, wheeltopoff;
928 #endif
929 int wheeloff = 0;
930 int leftoff, rightoff;
931 ULONG gadlen[6], gadpos[6];
932 char *str, *string[6];
935 spacing = rtGetVScreenSize (glob->scr, (ULONG *)&scrwidth, (ULONG *)&scrheight);
936 leftoff = glob->scr->WBorLeft + 5;
937 rightoff = glob->scr->WBorRight + 5;
939 top = (glob->scr->WBorTop + glob->scr->Font->ta_YSize + 1) + spacing / 2 + 1;
940 glob->itxt.ITextFont = &glob->font;
942 #ifdef COLORWHEEL
943 wheeltopoff = top;
944 #endif
946 width1 = width2 = 0;
948 for (i = 0; i < 6; i++)
950 string[i] = GetStr (glob->catalog, gadtxt[i]);
951 val = StrWidth_noloc (&glob->itxt, string[i]) + 16;
952 if (i < 3)
954 if (val > width1) width1 = val;
956 else
958 if (val > width2) width2 = val;
962 for (i = 0; i < 3; i++) gadlen[i] = width1;
963 for (i = 3; i < 6; i++) gadlen[i] = width2;
965 width1 *= 3;
966 width2 *= 3;
968 winwidth = (leftoff + rightoff) + 25 + width1 + 2 * 8;
969 val = (leftoff + rightoff) + width2 + 2 * 8;
970 if (val > winwidth) winwidth = val;
971 if (winwidth < 256) winwidth = 256;
973 val = glob->fontheight * 2 + 4;
974 #ifdef __AROS__
975 val += 5;
976 #endif
978 if (glob->colcount >= 64) val *= 2;
979 if (glob->colcount >= 128) val *= 2;
982 #ifdef COLORWHEEL
983 if( glob->dowheel && glob->screenres.x && glob->screenres.y && ColorWheelBase && GradientSliderBase )
985 LONG maxheight = 120;
987 if( scrheight > 600 )
989 maxheight = 160;
991 else if( scrheight < 300 )
993 maxheight = 75;
996 wheelheight = val + glob->fontheight * 4 + 6 + 3 + spacing * 3;
998 if( wheelheight > maxheight )
1000 wheelheight = maxheight;
1003 #ifndef __AROS__
1004 #warning Changed, because gcc produced wrong code! gcc 2.95.1 compiled under UAE JIT for Linux!?
1005 wheelwidth = glob->screenres.y;
1006 wheelwidth *= wheelheight;
1007 wheelwidth /= glob->screenres.x;
1008 #else
1009 wheelwidth = wheelheight * glob->screenres.y / glob->screenres.x;
1010 #endif
1012 if( ( scrwidth - winwidth > wheelwidth + 8 ) &&
1013 ( scrheight > 200 ) )
1015 wheeloff = wheelwidth + 8;
1016 winwidth += wheeloff;
1018 else
1020 glob->dowheel = FALSE;
1023 } /* if( glob->dowheel && glob->screenres.x && glob->screenres.y && ColorWheelBase && GradientSliderBase ) */
1024 else
1026 glob->dowheel = FALSE;
1028 #endif
1031 rtSpread (gadpos, gadlen, width1, leftoff + wheeloff + 25, winwidth - rightoff, 3);
1032 rtSpread (gadpos + 3, gadlen + 3, width2, leftoff, winwidth - rightoff, 3);
1035 gad = (struct Gadget *)CreateContext (&glob->buttoninfo.glist);
1036 img = &glob->labelimages;
1037 ng.ng_Flags = 0;
1038 ng.ng_VisualInfo = glob->visinfo;
1039 ng.ng_TextAttr = &glob->font;
1041 str = GetStr (glob->catalog, MSG_PALETTE_COLORS);
1042 glob->palettekey = KeyFromStr (str, '_');
1044 i = leftoff + 25 + ( winwidth - ( leftoff + rightoff + 25 ) -
1045 StrWidth_noloc( &glob->itxt, str ) ) / 2;
1047 #ifdef COLORWHEEL
1048 if( i < wheeloff )
1050 i = wheeloff;
1052 #endif
1054 img = my_CreateGadgetLabelImage (img, &ng, str, i, top, HIGHLIGHTTEXTPEN);
1055 top += glob->fontheight + 1 + spacing / 2;
1057 InitNewGadget (&ng, leftoff + wheeloff + 25, top, winwidth - ( wheeloff + leftoff + rightoff + 25),
1058 val, NULL, PALETTE_ID);
1060 gad = glob->palgad = myCreateGadget (PALETTE_KIND, gad, &ng,
1061 GTPA_Depth, (IPTR)glob->coldepth,
1062 GTPA_IndicatorWidth, 38,
1063 GTPA_Color, (IPTR)glob->color, TAG_END);
1065 if (glob->os30) top += gad->Height + spacing;
1066 else top += val + spacing;
1068 buttonheight = glob->fontheight + 6;
1070 for (i = 0; i < 3; i++)
1072 InitNewGadget (&ng, gadpos[i], top, gadlen[i],
1073 buttonheight, string[i], COPY_ID + i);
1075 gad = my_CreateButtonGadget (gad, '_', &ng);
1078 top += buttonheight + spacing;
1080 ng.ng_Flags |= NG_HIGHLABEL;
1082 maxwidth = 0;
1084 for (i = 0; i < 3; i++)
1086 string[i] = GetStr (glob->catalog, colstr[i]);
1087 glob->key[i] = KeyFromStr (string[i], '_');
1088 val = StrWidth_noloc (&glob->itxt, string[i]);
1089 if (val > maxwidth) maxwidth = val;
1092 levelwidth = StrWidth_noloc (&glob->itxt, "000 ");
1093 maxwidth += levelwidth;
1096 for (i = 0; i < 3; i++)
1099 val = leftoff + wheeloff + 2 + maxwidth + 8;
1100 InitNewGadget (&ng, val, top, winwidth - val - rightoff,
1101 glob->fontheight + 6, NULL, RED_ID + i);
1103 glob->colgad[i] = gad = myCreateGadget (SLIDER_KIND, gad, &ng,
1104 GTSL_LevelFormat, (IPTR) "%3ld",
1105 GTSL_LevelPlace, PLACETEXT_LEFT,
1106 GTSL_MaxLevelLen, 3, GA_RelVerify, TRUE, GA_Immediate, TRUE,
1107 GTSL_Level, glob->cols[i],
1108 GTSL_Max, glob->maxcolval[i],
1109 ((GfxBase->LibNode.lib_Version >= 40) ? GTSL_Justification : TAG_IGNORE), GTJ_RIGHT,
1110 GTSL_MaxPixelLen, levelwidth,
1111 TAG_END);
1113 img = my_CreateGadgetLabelImage (img, &ng, string[i],
1114 leftoff + wheeloff + 2, top + 2, HIGHLIGHTTEXTPEN);
1115 top += glob->fontheight + 6 + spacing / 2;
1119 top += spacing / 2;
1120 ng.ng_Flags &= ~NG_HIGHLABEL;
1123 #ifdef COLORWHEEL
1124 if( glob->dowheel )
1126 int wheeltop = top - ( wheelheight + glob->fontheight + 3 + spacing * 2 );
1128 if( wheeltop < wheeltopoff )
1130 top += wheeltopoff - wheeltop;
1131 wheeltop = wheeltopoff;
1134 #ifdef GRADIENT
1136 if( glob->fancywheel )
1139 glob->numgradpens = LOWGRADPENS;
1141 if( glob->scr->ViewPort.ColorMap
1142 && glob->scr->ViewPort.ColorMap->PalExtra
1143 && ( glob->scr->ViewPort.ColorMap->PalExtra->pe_NFree > FREEFORHIGH ) )
1145 glob->numgradpens = HIGHGRADPENS;
1148 /* get the RGB components of active color */
1149 glob->wheel_rgb.cw_Red = glob->cols[ RED_ID ];
1150 glob->wheel_rgb.cw_Green = glob->cols[ GREEN_ID ];
1151 glob->wheel_rgb.cw_Blue = glob->cols[ BLUE_ID ];
1153 /* now convert the RGB values to HSB, and max out B component */
1154 ConvertRGBToHSB( &glob->wheel_rgb, &glob->wheel_hsb );
1155 glob->wheel_hsb.cw_Brightness = 0xffffffff;
1158 /* Set up colors for gradient slider */
1159 for( i = 0; i < glob->numgradpens; i++ )
1161 glob->wheel_hsb.cw_Brightness = 0xffffffff
1162 - ( ( 0xffffffff / ( glob->numgradpens - 1 ) ) * ( ULONG ) i );
1163 ConvertHSBToRGB( &glob->wheel_hsb, &glob->wheel_rgb );
1164 glob->wheel_pens[ i ] = ObtainPen( glob->scr->ViewPort.ColorMap,
1165 -1, glob->wheel_rgb.cw_Red, glob->wheel_rgb.cw_Green,
1166 glob->wheel_rgb.cw_Blue, PEN_EXCLUSIVE );
1168 if( glob->wheel_pens[ i ] == (UWORD)~0 )
1170 break;
1175 glob->wheel_pens[ i ] = ( UWORD ) ~0;
1177 if( i != glob->numgradpens )
1179 for( i = 0; glob->wheel_pens[ i ] != (UWORD)~0; i++ )
1181 ReleasePen( glob->scr->ViewPort.ColorMap, glob->wheel_pens[ i ] );
1184 glob->numgradpens = 0;
1187 } /* if( glob->fancywheel ) */
1188 #endif
1191 struct TagItem slider_tags[] =
1193 {GA_ID , SLIDER_ID },
1194 {GA_Top , wheeltop + wheelheight + spacing },
1195 {GA_Left , leftoff },
1196 {GA_Width , wheelwidth },
1197 {GA_Height , glob->fontheight + 3 },
1198 #ifdef GRADIENT
1199 {glob->numgradpens ?
1200 GRAD_PenArray :
1201 TAG_IGNORE , (IPTR)glob->wheel_pens },
1202 #endif
1203 {GRAD_KnobPixels , 8 },
1204 {PGA_Freedom , LORIENT_HORIZ },
1205 {ICA_TARGET , ICTARGET_IDCMP },
1206 {TAG_END }
1209 glob->wheel_slider = (struct Gadget *)NewObjectA(NULL, "gradientslider.gadget", slider_tags);
1213 if( glob->wheel_slider )
1215 struct TagItem wheel_tags[] =
1217 {GA_Top , wheeltop },
1218 {GA_Left , leftoff },
1219 {GA_Width , wheelwidth },
1220 {GA_Height , wheelheight },
1221 {GA_ID , WHEEL_ID },
1222 {WHEEL_Screen , (IPTR)glob->scr },
1223 {glob->fancywheel ?
1224 TAG_IGNORE :
1225 WHEEL_MaxPens , 0 },
1226 {WHEEL_GradientSlider , (IPTR)glob->wheel_slider },
1227 #ifdef __AROS__
1228 /* Need this, because without BevelBox AROS colorwheel gadget renders through mask
1229 which because of bugs in gfx library functions (!?) does not work yet and instead
1230 causes mem trashes/crashes/etc (in AmigaOS the AROS colorwheel gadget works fine
1231 even without BevelBox, that is: with mask rendering) */
1232 {WHEEL_BevelBox , TRUE },
1233 #endif
1234 {GA_Previous , (IPTR)glob->wheel_slider },
1235 {ICA_TARGET , ICTARGET_IDCMP },
1236 {TAG_END }
1239 glob->wheel = (struct Gadget *)NewObjectA(NULL, "colorwheel.gadget", wheel_tags);
1243 } /* if( glob->dowheel )*/
1244 #endif
1247 for (i = 3; i < 6; i++)
1249 InitNewGadget (&ng, gadpos[i], top, gadlen[i],
1250 buttonheight, string[i], COPY_ID + i);
1252 gad = my_CreateButtonGadget (gad, '_', &ng);
1255 top += buttonheight + spacing;
1257 winheight = top + glob->scr->WBorBottom;
1258 glob->newpalwin.Height = winheight;
1259 glob->newpalwin.Width = winwidth;
1260 glob->newpalwin.IDCMPFlags = glob->shareidcmp ? 0 :
1261 SLIDERIDCMP|PALETTEIDCMP|BUTTONIDCMP|IDCMP_CLOSEWINDOW|IDCMP_RAWKEY
1262 |IDCMP_REFRESHWINDOW|IDCMP_IDCMPUPDATE;
1263 glob->newpalwin.Flags = WFLG_DEPTHGADGET|WFLG_DRAGBAR|WFLG_ACTIVATE
1264 |WFLG_SIMPLE_REFRESH|WFLG_RMBTRAP|WFLG_CLOSEGADGET;
1265 glob->newpalwin.DetailPen = glob->drinfo->dri_Pens[BACKGROUNDPEN];
1266 glob->newpalwin.BlockPen = glob->drinfo->dri_Pens[SHADOWPEN];
1267 glob->newpalwin.Title = title;
1268 glob->newpalwin.LeftEdge = glob->leftedge;
1269 glob->newpalwin.TopEdge = glob->topedge;
1271 reqpos = CheckReqPos (glob->reqpos, RTPREF_PALETTEREQ, &glob->newpalwin);
1273 if (reqpos == REQPOS_POINTER)
1275 glob->newpalwin.LeftEdge = -winwidth / 2;
1276 glob->newpalwin.TopEdge = -winheight / 2;
1279 rtSetReqPosition (reqpos, &glob->newpalwin, glob->scr, glob->prwin);
1281 ng.ng_LeftEdge = ng.ng_TopEdge = ng.ng_Width = ng.ng_Height = 0;
1282 ng.ng_GadgetText = NULL;
1283 gad = myCreateGadget (GENERIC_KIND, gad, &ng, TAG_END);
1285 if( gad )
1287 gad->GadgetType |= GTYP_BOOLGADGET;
1288 gad->Flags |= GFLG_GADGIMAGE|GFLG_GADGHNONE;
1289 gad->GadgetRender = ( APTR ) glob->labelimages.NextImage;
1293 glob->zoom[ 2 ] = glob->newpalwin.Width;
1294 glob->zoom[ 3 ] = glob->scr->WBorTop + glob->scr->Font->ta_YSize + 1;
1296 if( ( glob->dowheel && !glob->wheel ) || !img || !gad ||
1297 !(glob->palwin = OpenWindowBF( &glob->newpalwin,
1298 &glob->backfillhook, glob->drinfo->dri_Pens, NULL, glob->zoom, FALSE ) ) )
1300 my_FreeGadgets( glob->buttoninfo.glist );
1301 glob->buttoninfo.glist = NULL;
1302 my_FreeLabelImages( &glob->labelimages );
1303 glob->labelimages.NextImage = NULL;
1304 DisposeObject( glob->wheel );
1305 glob->wheel = NULL;
1306 DisposeObject( glob->wheel_slider );
1307 glob->wheel_slider = NULL;
1308 return( 0 );
1311 glob->buttoninfo.win = glob->palwin;
1313 if( glob->shareidcmp )
1315 glob->palwin->UserPort = glob->prwin->UserPort;
1316 ModifyIDCMP( glob->palwin,
1317 SLIDERIDCMP | PALETTEIDCMP | BUTTONIDCMP | IDCMP_CLOSEWINDOW |
1318 IDCMP_RAWKEY | IDCMP_REFRESHWINDOW | IDCMP_IDCMPUPDATE );
1321 #ifdef COLORWHEEL
1322 if( glob->wheel )
1324 AddGList( glob->palwin, glob->wheel_slider, -1, -1, NULL );
1325 RefreshGList( glob->wheel_slider, glob->palwin, NULL, -1 );
1327 #endif
1329 AddGList( glob->palwin, glob->buttoninfo.glist, -1, -1, NULL );
1330 RefreshGList( glob->buttoninfo.glist, glob->palwin, NULL, -1 );
1331 GT_RefreshWindow( glob->palwin, NULL );
1332 UpdateWheel( glob, glob->cols );
1334 return( 1 );
1337 /****************************************************************************************/