Updated PCI IDs to latest snapshot.
[tangerine.git] / workbench / libs / reqtools / general.c
blobf3464e8a9978d776034e12b578f9bbf4ec1a5e88
2 /* INCLUDES */
4 #include <exec/types.h>
5 #include <exec/memory.h>
6 #include <exec/execbase.h>
7 #include <libraries/dos.h>
8 #include <libraries/dosextens.h>
9 #include <graphics/gfxmacros.h>
10 #include <graphics/videocontrol.h>
11 #include <graphics/view.h>
12 #include <graphics/gfxbase.h>
13 #include <libraries/gadtools.h>
14 #include <intuition/intuition.h>
15 #include <intuition/intuitionbase.h>
16 #include <intuition/imageclass.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 <clib/macros.h>
24 #include <string.h>
26 #include <libraries/reqtools.h>
27 #include <proto/reqtools.h>
29 #ifdef __AROS__
30 #include <aros/asmcall.h>
31 #endif
33 #include "filereq.h"
35 /****************************************************************************************/
37 /* Use ColorMap.Count to find out number of palette entried on screen */
38 //#define DO_CM_DEPTH
40 /****************************************************************************************/
42 UWORD CHIP waitpointer[] =
44 0x0000,0x0000,
45 0x0400,0x07C0,0x0000,0x07C0,0x0100,0x0380,0x0000,0x07E0,
46 0x07C0,0x1FF8,0x1FF0,0x3FEC,0x3FF8,0x7FDE,0x3FF8,0x7FBE,
47 0x7FFC,0xFF7F,0x7EFC,0xFFFF,0x7FFC,0xFFFF,0x3FF8,0x7FFE,
48 0x3FF8,0x7FFE,0x1FF0,0x3FFC,0x07C0,0x1FF8,0x0000,0x07E0,
49 0x0000,0x0000,
52 /****************************************************************************************/
54 extern struct Library *GadToolsBase;
55 extern struct DosLibrary *DOSBase;
56 extern struct IntuitionBase *IntuitionBase;
57 extern struct GfxBase *GfxBase;
58 extern struct ReqToolsBase *ReqToolsBase;
59 #if defined(__AROS__) || defined(__GNUC__)
60 extern struct UtilityBase *UtilityBase;
61 #else
62 extern struct Library *UtilityBase;
63 #endif
65 /****************************************************************************************/
67 int ASM SAVEDS GetVScreenSize (
68 REGPARAM(a0, struct Screen *, scr),
69 REGPARAM(a1, int *, width),
70 REGPARAM(a2, int *, height))
72 struct ViewPortExtra *vpe;
73 struct Rectangle dispclip, *clip;
74 ULONG getvpetags[3];
75 int ht;
76 #ifndef USE_FORBID
77 struct Screen *pubscr;
78 ULONG ilock = 0L;
79 BOOL isfirst;
80 #endif
83 getvpetags[0] = VTAG_VIEWPORTEXTRA_GET;
84 getvpetags[1] = (ULONG)&vpe;
85 getvpetags[2] = TAG_END;
87 #ifdef USE_FORBID
89 Forbid();
91 #ifdef __AROS__
92 #warning No VideoControl in AROS, yet
93 #else
94 if (IntuitionBase->FirstScreen == scr &&
95 VideoControl (scr->ViewPort.ColorMap, (struct TagItem *)getvpetags) == 0)
97 clip = &((struct ViewPortExtra *)getvpetags[1])->DisplayClip;
99 else
101 #endif
102 QueryOverscan (GetVPModeID (&scr->ViewPort), &dispclip, OSCAN_TEXT);
103 clip = &dispclip;
104 #ifndef __AROS__
106 #endif
108 Permit();
110 #else
112 if ((pubscr = LockPubScreenByAddr(scr)))
114 /* Cool, we got a lock on the screen, we don't need to Forbid() */
115 ilock = LockIBase(0);
117 else
119 /* damn it, not a pubscreen, Forbid() as a last resort */
120 Forbid();
123 isfirst = (IntuitionBase->FirstScreen == scr);
125 if (pubscr)
127 UnlockIBase(ilock);
130 #ifdef __AROS__
131 #warning No VideoControl in AROS, yet
132 #else
133 if (isfirst &&
134 VideoControl (scr->ViewPort.ColorMap, (struct TagItem *)getvpetags) == 0)
136 clip = &((struct ViewPortExtra *)getvpetags[1])->DisplayClip;
138 else
140 #endif
141 QueryOverscan (GetVPModeID (&scr->ViewPort), &dispclip, OSCAN_TEXT);
142 clip = &dispclip;
143 #ifndef __AROS__
145 #endif
146 if (pubscr)
148 UnlockPubScreen(NULL, pubscr);
150 else
152 Permit();
155 #endif
157 *width = clip->MaxX - clip->MinX + 1;
158 *height = ht = clip->MaxY - clip->MinY + 1;
160 if (scr->Width < *width) *width = scr->Width;
161 if (scr->Height < *height) *height = scr->Height;
163 return ((ht >= 400) ? 4 : 2);
166 /****************************************************************************************/
168 struct Screen *REGARGS LockPubScreenByAddr (struct Screen *scr)
170 struct List *pubscreenlist;
171 struct PubScreenNode *pubscreennode;
172 UBYTE pubscreenname[MAXPUBSCREENNAME + 1];
174 pubscreenname[0] = '\0';
176 pubscreenlist = LockPubScreenList();
177 pubscreennode = (struct PubScreenNode *) pubscreenlist->lh_Head;
178 while (pubscreennode->psn_Node.ln_Succ)
180 if (pubscreennode->psn_Screen == scr)
182 strcpy(pubscreenname, pubscreennode->psn_Node.ln_Name);
183 break;
185 pubscreennode = (struct PubScreenNode *) pubscreennode->psn_Node.ln_Succ;
187 UnlockPubScreenList();
189 /* If we found a matching pubscreen try to lock it (note: can fail) */
190 if (pubscreenname[0])
192 scr = LockPubScreen(pubscreenname);
194 else
196 scr = NULL;
199 return scr;
202 /****************************************************************************************/
205 #undef ThisProcess
206 #define ThisProcess() ( ( APTR ) FindTask( NULL ) )
208 /****************************************************************************************/
210 struct Screen *REGARGS GetReqScreen (
211 struct NewWindow *nw, struct Window **prwin, struct Screen *scr, char *pubname)
213 struct Window *win = *prwin;
214 struct Process *proc;
216 if (!pubname && !scr && !win)
218 proc = ThisProcess();
219 if (proc->pr_Task.tc_Node.ln_Type == NT_PROCESS) win = proc->pr_WindowPtr;
222 nw->Type = CUSTOMSCREEN;
223 if (scr && !win)
225 struct Screen *pubscr;
227 /* Try to lock the screen as a public screen */
228 if ((pubscr = LockPubScreenByAddr(scr)))
230 scr = pubscr;
231 nw->Type = PUBLICSCREEN;
233 /* FIXME: probably should do something smart if the locking fail.
234 RT_Screen is more than dangerous if you ask me... - Piru */
236 else
238 if (win && (ULONG)win != ~0) scr = win->WScreen;
239 else
241 if (!(scr = LockPubScreen (pubname)))
242 if (!(scr = LockPubScreen (NULL))) return (NULL);
244 nw->Type = PUBLICSCREEN;
245 win = NULL;
248 *prwin = win;
249 return (nw->Screen = scr);
252 /****************************************************************************************/
254 #ifndef DO_CM_DEPTH
255 static int VpDepth (struct ViewPort *vp)
257 ULONG modeid = GetVPModeID (vp);
258 int depth;
260 depth = vp->RasInfo->BitMap->Depth;
262 if (modeid & HAM_KEY) depth -= 2;
263 if (modeid & EXTRAHALFBRITE_KEY) depth = 5;
265 if( depth > 8 )
267 depth = 8;
270 return (depth);
272 #endif
274 /****************************************************************************************/
276 int REGARGS
277 GetVpCM( struct ViewPort *vp, APTR *cmap)
280 #ifdef DO_CM_DEPTH
281 int numcols;
283 if( !vp->ColorMap )
285 return( 0 );
288 numcols = vp->ColorMap->Count;
289 #else
290 int depth, numcols;
292 if( !vp->ColorMap )
294 return( 0 );
297 depth = VpDepth (vp);
298 numcols = (1 << depth);
299 #endif
300 if( GfxBase->LibNode.lib_Version >= 39 )
302 if( ( *cmap = AllocVec( ( numcols * 3 + 2 ) * 4, MEMF_PUBLIC | MEMF_CLEAR ) ) )
304 ( ( UWORD * ) ( *cmap ) )[ 0 ] = numcols;
307 else
309 *cmap = AllocVec( numcols * 2, MEMF_PUBLIC | MEMF_CLEAR );
312 if( *cmap )
314 RefreshVpCM( vp, *cmap );
315 return( depth );
317 else
319 return( 0 );
323 /****************************************************************************************/
325 void REGARGS
326 RefreshVpCM( struct ViewPort *vp, APTR cmap )
328 #ifdef DO_CM_DEPTH
329 int numcols, i;
331 numcols = vp->ColorMap->Count;
332 #else
333 int i, depth, numcols;
335 depth = VpDepth( vp );
336 numcols = ( 1 << depth );
337 #endif
338 if( GfxBase->LibNode.lib_Version >= 39 )
340 GetRGB32( vp->ColorMap, 0, numcols, ( ( ULONG * ) cmap ) + 1 );
342 else
344 for( i = 0; i < numcols; i++ )
346 ( ( UWORD * ) cmap )[ i ] = GetRGB4( vp->ColorMap, i );
351 /****************************************************************************************/
353 void REGARGS LoadCMap (struct ViewPort *vp, APTR cmap)
355 if (GfxBase->LibNode.lib_Version >= 39) LoadRGB32 (vp, cmap);
356 #ifdef DO_CM_DEPTH
357 else LoadRGB4 (vp, cmap, vp->ColorMap->Count);
358 #else
359 else LoadRGB4 (vp, cmap, (1 << VpDepth (vp)));
360 #endif
363 /****************************************************************************************/
365 void REGARGS FreeVpCM (struct ViewPort *vp, APTR cmap, BOOL restore)
367 if (restore && cmap) LoadCMap (vp, cmap);
368 FreeVec (cmap);
371 /****************************************************************************************/
373 void REGARGS InitNewGadget (struct NewGadget *ng,
374 int x, int y, int w, int h, char *s, UWORD id)
376 ng->ng_LeftEdge = x; ng->ng_TopEdge = y; ng->ng_Width = w; ng->ng_Height = h;
377 ng->ng_GadgetText = s; ng->ng_GadgetID = id;
380 /****************************************************************************************/
382 struct TextFont * REGARGS GetReqFont (struct TextAttr *attr,
383 struct TextFont *deffont, int *fontheight, int *fontwidth, int allowprop)
385 struct TextFont *ft;
386 int forcedef;
388 forcedef = (rtLockPrefs()->Flags & RTPRF_DEFAULTFONT);
389 rtUnlockPrefs();
391 ft = OpenFont (attr);
392 if (!ft || forcedef || (!allowprop && (ft->tf_Flags & FPF_PROPORTIONAL)))
394 if (ft) CloseFont (ft);
395 if (deffont) ft = deffont;
396 else ft = GfxBase->DefaultFont;
398 attr->ta_Name = ft->tf_Message.mn_Node.ln_Name;
399 attr->ta_YSize = ft->tf_YSize;
400 attr->ta_Style = ft->tf_Style;
401 attr->ta_Flags = ft->tf_Flags;
402 ft = OpenFont (attr);
405 if (ft)
407 *fontheight = ft->tf_YSize;
408 *fontwidth = ft->tf_XSize;
411 return (ft);
414 /****************************************************************************************/
416 struct IntuiMessage *REGARGS GetWin_GT_Msg (struct Window *win,
417 struct Hook *hook, APTR req)
419 struct IntuiMessage *imsg, *reqmsg;
421 while ((imsg = (struct IntuiMessage *)GetMsg (win->UserPort)))
422 if ((reqmsg = ProcessWin_Msg (win, imsg, hook, req))) return (reqmsg);
424 return (NULL);
427 /****************************************************************************************/
429 struct IntuiMessage *REGARGS ProcessWin_Msg (struct Window *win,
430 struct IntuiMessage *imsg, struct Hook *hook, APTR req)
432 struct IntuiMessage *reqmsg;
434 if (imsg->IDCMPWindow == win)
436 reqmsg = GT_FilterIMsg (imsg);
437 if (reqmsg) return (reqmsg);
438 ReplyMsg ((struct Message *)imsg);
440 else
442 if (hook) CallHookPkt (hook, req, imsg);
443 ReplyMsg ((struct Message *)imsg);
446 return (NULL);
449 /****************************************************************************************/
451 void REGARGS Reply_GT_Msg (struct IntuiMessage *reqmsg)
453 ReplyMsg ((struct Message *)GT_PostFilterIMsg (reqmsg));
456 /****************************************************************************************/
458 void REGARGS DoScreenToFront (struct Screen *scr, int nopop, int popup)
460 ULONG noscrtofront;
462 if (nopop) return;
464 noscrtofront = (rtLockPrefs()->Flags & RTPRF_NOSCRTOFRONT);
465 rtUnlockPrefs();
467 if (noscrtofront) return;
468 if (popup)
469 ScreenToFront (scr);
470 else
471 rtScreenToFrontSafely (scr);
475 /****************************************************************************************/
477 void REGARGS DoCloseWindow (struct Window *win, int idcmpshared)
479 if (idcmpshared) rtCloseWindowSafely (win);
480 else CloseWindow (win);
483 /****************************************************************************************/
485 struct BackFillMsg
487 struct Layer *layer;
488 struct Rectangle bounds;
489 LONG offsetx;
490 LONG offsety;
493 /****************************************************************************************/
495 #ifdef __AROS__
496 AROS_UFH3(void, WinBackFill,
497 AROS_UFHA(struct Hook *, hook, A0),
498 AROS_UFHA(struct RastPort *, the_rp, A2),
499 AROS_UFHA(struct BackFillMsg *, msg, A1))
501 AROS_USERFUNC_INIT
502 #else
503 void SAVEDS ASM WinBackFill (
504 REGPARAM(a0, struct Hook *, hook),
505 REGPARAM(a2, struct RastPort *, the_rp),
506 REGPARAM(a1, struct BackFillMsg *, msg))
508 #endif
509 struct RastPort rp;
511 memcpy( &rp, the_rp, sizeof( rp ) );
512 rp.Layer = NULL;
513 SetAPen (&rp, ((UWORD *)hook->h_Data)[BACKGROUNDPEN]);
514 mySetWriteMask (&rp, ~0);
515 RectFill (&rp, msg->bounds.MinX, msg->bounds.MinY,
516 msg->bounds.MaxX, msg->bounds.MaxY);
518 #ifdef __AROS__
519 AROS_USERFUNC_EXIT
520 #endif
523 /****************************************************************************************/
525 #ifdef __AROS__
527 AROS_UFH3(void, PatternWinBackFill,
528 AROS_UFHA(struct Hook *, hook, A0),
529 AROS_UFHA(struct RastPort *, the_rp, A2),
530 AROS_UFHA(struct BackFillMsg *, msg, A1))
532 AROS_USERFUNC_INIT
534 struct RastPort rp;
535 UWORD pattern[] = {0xAAAA,0x5555};
537 memcpy( &rp, the_rp, sizeof( rp ) );
538 rp.Layer = NULL;
540 SetAPen (&rp, ((UWORD *)hook->h_Data)[BACKGROUNDPEN]);
541 SetBPen (&rp, ((UWORD *)hook->h_Data)[SHINEPEN]);
542 SetDrMd (&rp, JAM2);
544 mySetWriteMask (&rp, ~0);
546 SetAfPt(&rp, pattern, 1);
548 RectFill (&rp, msg->bounds.MinX, msg->bounds.MinY,
549 msg->bounds.MaxX, msg->bounds.MaxY);
551 SetAfPt(&rp, NULL, 0);
553 AROS_USERFUNC_EXIT
556 #endif
558 /****************************************************************************************/
560 void REGARGS mySetWriteMask (struct RastPort *rp, ULONG mask)
562 if (GfxBase->LibNode.lib_Version >= 39) SetWriteMask (rp, mask);
563 else SetWrMsk (rp, (UBYTE)mask);
566 /****************************************************************************************/
568 struct Window *REGARGS OpenWindowBF (struct NewWindow *nw,
569 struct Hook *hook, UWORD *pens, ULONG *maskptr, WORD *zoom,
570 BOOL backfillpattern)
572 struct RastPort *rp;
573 struct Window *win;
574 ULONG tags[5], mask;
575 UWORD maxpen = 0;
576 int i;
578 #ifdef __AROS__
579 if (backfillpattern)
581 hook->h_Entry = (HOOKFUNC)PatternWinBackFill;
582 } else
583 #endif
584 hook->h_Entry = (HOOKFUNC)WinBackFill;
586 hook->h_Data = (void *)pens;
588 tags[0] = WA_BackFill;
589 tags[1] = (ULONG)hook;
590 tags[2] = WA_Zoom;
591 tags[3] = (ULONG)zoom;
592 tags[4] = TAG_END;
594 if( zoom )
596 if (IntuitionBase->LibNode.lib_Version >= 39)
597 zoom[0] = zoom[1] = ~0;
598 else
600 zoom[0] = nw->LeftEdge;
601 zoom[1] = nw->TopEdge;
604 else
606 tags[2] = TAG_IGNORE;
609 if ((win = OpenWindowTagList (nw, (struct TagItem *)tags)))
611 rp = win->RPort;
612 for (i = 0; i <= HIGHLIGHTTEXTPEN; i++)
613 if (pens[i] > maxpen) maxpen = pens[i];
615 mask = 1;
616 while (mask < maxpen)
618 mask <<= 1;
619 mask |= 1;
622 mySetWriteMask (rp, mask);
623 if (maskptr) *maskptr = mask;
626 return (win);
630 /****************************************************************************************/
632 int CheckReqPos (int reqpos, int reqdefnum, struct NewWindow *newwin)
634 struct ReqDefaults *reqdefs;
636 if (reqpos == REQPOS_DEFAULT)
638 reqdefs = &rtLockPrefs()->ReqDefaults[reqdefnum];
639 reqpos = reqdefs->ReqPos;
641 if (reqpos <= REQPOS_CENTERSCR)
643 newwin->LeftEdge = 0;
644 newwin->TopEdge = 0;
646 else
648 newwin->LeftEdge = reqdefs->LeftOffset;
649 newwin->TopEdge = reqdefs->TopOffset;
652 rtUnlockPrefs();
655 return (reqpos);
658 /****************************************************************************************/
660 /*********
661 * Layout *
662 *********/
664 /****************************************************************************************/
666 int REGARGS StrWidth_noloc (struct IntuiText *itxt, UBYTE *str)
668 char labstr[100], *l;
670 if (!str) return (0);
672 /* Copy string, remove underscore */
673 l = labstr;
674 while (*str && *str != '_') *l++ = *str++;
676 if (*str) while ((*l++ = *++str)); else *l = 0;
678 itxt->IText = labstr;
680 return (IntuiTextLength (itxt));
683 /****************************************************************************************/
685 static int ObjectWidth (struct NewGadget *ng, int normalw, int normalh)
687 if ((GadToolsBase->lib_Version >= 39) && (ng->ng_TextAttr->ta_YSize > normalh))
688 return (normalw + (ng->ng_TextAttr->ta_YSize - normalh) * 2);
690 return (normalw);
693 /****************************************************************************************/
695 static int ObjectHeight (struct NewGadget *ng, int normalh)
697 if ((GadToolsBase->lib_Version >= 39) && (ng->ng_TextAttr->ta_YSize > normalh))
698 return (ng->ng_TextAttr->ta_YSize);
700 return (normalh);
703 /****************************************************************************************/
705 int CheckBoxWidth (struct NewGadget *ng)
707 return (ObjectWidth (ng, CHECKBOX_WIDTH, CHECKBOX_HEIGHT));
710 /****************************************************************************************/
712 int CheckBoxHeight (struct NewGadget *ng)
714 return (ObjectHeight (ng, CHECKBOX_HEIGHT));
717 /****************************************************************************************/
720 * SIZEGAD HEIGHT
722 LONG BottomBorderHeight (struct Screen *scr)
724 struct DrawInfo *dri;
725 APTR obj;
726 LONG h = 10;
728 if ((dri = GetScreenDrawInfo (scr)))
730 if((obj = NewObject (NULL, "sysiclass", SYSIA_DrawInfo, (IPTR) dri,
731 /* Must be SYSISIZE_MEDRES! */
732 SYSIA_Size, SYSISIZE_MEDRES,
733 SYSIA_Which, SIZEIMAGE,
734 TAG_DONE)))
736 if (!GetAttr (IA_Height, obj, (IPTR *)&h))
737 h = 10; // Probably not needed.. Or?
738 DisposeObject( obj );
740 FreeScreenDrawInfo (scr, dri);
742 return (h);
745 /****************************************************************************************/