revert between 56095 -> 55830 in arch
[AROS.git] / workbench / c / iprefs / wbpatternprefs.c
blobc238b07215cc5e87d00518df2833788ffca0f7c8
1 /*
2 Copyright © 2004, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: English
7 */
9 /*********************************************************************************************/
11 #include "global.h"
13 #include <prefs/prefhdr.h>
14 #include <prefs/wbpattern.h>
15 #include <datatypes/pictureclass.h>
17 #define DEBUG 0
18 #include <aros/debug.h>
20 /*********************************************************************************************/
22 struct FileWBPatternPrefs
24 UBYTE wbp_Reserved[4 * 4];
25 UBYTE wbp_Which[2];
26 UBYTE wbp_Flags[2];
27 UBYTE wbp_Revision;
28 UBYTE wbp_Depth;
29 UBYTE wbp_DataLength[2];
32 struct LayerHookMsg
34 struct Layer *lay; /* not valid for layerinfo backfill hook!!! */
35 struct Rectangle bounds;
36 LONG offsetx;
37 LONG offsety;
40 struct LayerHookData
42 struct BitMap *pat_bm;
43 ULONG pat_width;
44 ULONG pat_height;
47 /*********************************************************************************************/
49 static LONG stopchunks[] =
51 ID_PREF, ID_PTRN
54 /*********************************************************************************************/
56 static void RootPatternSetup(STRPTR filename);
58 /*********************************************************************************************/
60 void WBPatternPrefs_Handler(STRPTR filename)
62 struct IFFHandle *iff;
64 D(bug("In IPrefs:WBPatternPrefs_Handler\n"));
66 if ((iff = CreateIFF(filename, stopchunks, 1)))
68 while(ParseIFF(iff, IFFPARSE_SCAN) == 0)
70 struct ContextNode *cn;
71 struct FileWBPatternPrefs wbpatternprefs;
73 cn = CurrentChunk(iff);
75 D(bug("WBPatternPrefs_Handler: ParseIFF okay. Chunk Type = %c%c%c%c ChunkID = %c%c%c%c\n",
76 cn->cn_Type >> 24,
77 cn->cn_Type >> 16,
78 cn->cn_Type >> 8,
79 cn->cn_Type,
80 cn->cn_ID >> 24,
81 cn->cn_ID >> 16,
82 cn->cn_ID >> 8,
83 cn->cn_ID));
85 if ((cn->cn_ID == ID_PTRN) && (cn->cn_Size > sizeof(wbpatternprefs)))
87 D(bug("WBPatternPrefs_Handler: ID_FONT chunk with correct size found.\n"));
89 if (ReadChunkBytes(iff, &wbpatternprefs, sizeof(wbpatternprefs)) == sizeof(wbpatternprefs))
91 UWORD type;
92 UWORD len;
93 STRPTR filename;
95 D(bug("WBPatternPrefs_Handler: Reading of ID_PTRN chunk okay.\n"));
97 type = (wbpatternprefs.wbp_Which[0] << 8) + wbpatternprefs.wbp_Which[1];
98 len = (wbpatternprefs.wbp_DataLength[0] << 8) + wbpatternprefs.wbp_DataLength[1];
100 D(bug("Type = %d Len = %d\n", type, len));
102 if (sizeof(wbpatternprefs) + len == cn->cn_Size)
104 filename = (STRPTR)AllocVec(len + 1, MEMF_ANY);
105 if (filename != NULL)
107 if (ReadChunkBytes(iff, filename, len) == len)
109 filename[len] = 0;
110 D(bug("Filename = %s\n", filename));
111 switch(type)
113 case WBP_ROOT:
114 RootPatternCleanup();
115 RootPatternSetup(filename);
116 break;
117 case WBP_DRAWER:
118 break;
119 case WBP_SCREEN:
120 break;
121 } /* switch(type) */
122 } /* if (ReadChunkBytes(iff, filename, len + 1) == len + 1) */
123 FreeVec(filename);
124 } /* if (filename != NULL) */
125 } /* if (sizeof(wbpatternprefs) + len + 1 == cn->cn_Size) */
126 } /* if (ReadChunkBytes(iff, &wbpatternprefs, sizeof(wbpatternprefs)) == sizeof(wbpatternprefs)) */
128 } /* if ((cn->cn_ID == ID_FONT) && (cn->cn_Size == sizeof(wbpatternprefs))) */
130 } /* while(ParseIFF(iff, IFFPARSE_SCAN) == 0) */
132 KillIFF(iff);
134 } /* if ((iff = CreateIFF(filename))) */
137 D(bug("In IPrefs:WBPatternPrefs_Handler. Done.\n", filename));
140 /****************************************************************************************/
142 static void mybackfillfunc(struct Hook *hook,struct RastPort *rp, struct LayerHookMsg *msg)
144 struct LayerHookData *data = (struct LayerHookData *)hook->h_Data;
145 WORD x1,y1,x2,y2,px,py,pw,ph;
147 x1 = msg->bounds.MinX;
148 y1 = msg->bounds.MinY;
149 x2 = msg->bounds.MaxX;
150 y2 = msg->bounds.MaxY;
152 px = x1 % data->pat_width;
154 pw = data->pat_width - px;
158 y1 = msg->bounds.MinY;
159 py = y1 % data->pat_height;
161 ph = data->pat_height - py;
163 if (pw > (x2 - x1 + 1)) pw = x2 - x1 + 1;
167 if (ph > (y2 - y1 + 1)) ph = y2 - y1 + 1;
169 BltBitMap(data->pat_bm,
172 rp->BitMap,
177 192,
178 255,
181 y1 += ph;
183 py = 0;
184 ph = data->pat_height;
186 } while (y1 <= y2); /* while(y1 < y2) */
188 x1 += pw;
190 px = 0;
191 pw = data->pat_width;
193 } while (x1 <= x2); /* while (x1 < x2) */
198 /****************************************************************************************/
200 static struct Hook *installbackfillhook(struct Screen *scr, struct Hook *backfillhook,
201 struct LayerHookData *data)
203 struct Window *tempwin;
204 struct Hook *oldhook;
206 struct TagItem wintags[] =
208 {WA_PubScreen ,(IPTR)scr },
209 {WA_Left ,0 },
210 {WA_Top ,0 },
211 {WA_Width ,scr->Width },
212 {WA_Height ,scr->Height },
213 {WA_Borderless ,TRUE },
214 {WA_Backdrop ,TRUE },
215 {WA_BackFill ,(IPTR)LAYERS_NOBACKFILL },
216 {TAG_DONE }
219 backfillhook->h_Entry = HookEntry;
220 backfillhook->h_SubEntry = (HOOKFUNC)mybackfillfunc;
221 backfillhook->h_Data = data;
223 oldhook = InstallLayerInfoHook(&scr->LayerInfo, backfillhook);
224 tempwin = OpenWindowTagList(NULL, wintags);
225 if (tempwin) CloseWindow(tempwin);
226 return oldhook;
229 /****************************************************************************************/
231 static void removebackfillhook(struct Screen *scr, struct Hook *oldhook)
233 struct TagItem wintags[] =
235 { WA_PubScreen , (IPTR)scr },
236 { WA_Left , 0 },
237 { WA_Top , 0 },
238 { WA_Width , scr->Width },
239 { WA_Height , scr->Height },
240 { WA_Borderless, TRUE },
241 { WA_Backdrop , TRUE },
242 { WA_BackFill , (IPTR)LAYERS_NOBACKFILL },
243 { TAG_DONE }
245 struct Window *tempwin;
247 InstallLayerInfoHook(&scr->LayerInfo, oldhook);
248 tempwin = OpenWindowTagList(NULL, wintags);
249 if (tempwin) CloseWindow(tempwin);
252 /*********************************************************************************************/
254 static struct BitMap *LoadDTBitmap (CONST_STRPTR filename, struct Screen *scr,
255 Object **obj, ULONG *width, ULONG *height)
257 struct BitMapHeader *bmhd;
258 struct BitMap *bitmap = NULL;
259 Object *o;
260 struct Process *myproc = (struct Process *)FindTask(NULL);
261 APTR oldwindowptr = myproc->pr_WindowPtr;
263 myproc->pr_WindowPtr = (APTR)-1;
265 o = NewDTObject((APTR)filename,
266 DTA_GroupID , GID_PICTURE,
267 OBP_Precision , PRECISION_EXACT,
268 PDTA_Screen , (IPTR)scr,
269 PDTA_FreeSourceBitMap, TRUE,
270 PDTA_DestMode , PMODE_V43,
271 PDTA_UseFriendBitMap , TRUE,
272 TAG_DONE);
274 myproc->pr_WindowPtr = oldwindowptr;
276 if (o)
278 struct FrameInfo fri = {0};
279 DoMethod(o, DTM_FRAMEBOX, (IPTR) NULL, (IPTR) &fri, (IPTR) &fri,
280 sizeof(struct FrameInfo), 0);
282 if (fri.fri_Dimensions.Depth > 0)
284 if (DoMethod(o, DTM_PROCLAYOUT, (IPTR) NULL, 1))
286 *obj = o;
287 GetDTAttrs(o, PDTA_BitMapHeader, (IPTR)&bmhd, TAG_DONE);
288 if (bmhd)
290 *width = bmhd->bmh_Width;
291 *height = bmhd->bmh_Height;
292 GetDTAttrs(o, PDTA_DestBitMap, (IPTR)&bitmap, TAG_DONE);
293 if (NULL == bitmap)
294 GetDTAttrs(o, PDTA_BitMap, (IPTR)&bitmap, TAG_DONE);
295 if (bitmap)
296 return bitmap;
301 DisposeDTObject(o);
303 return NULL;
306 /*********************************************************************************************/
308 static Object *myDTObject = NULL;
309 static struct Hook *myOldHook = (APTR)-1; // NULL is a valid value
310 static struct Screen *myScreen = NULL;
311 static struct LayerHookData myData;
312 static struct Hook myBackFillHook;
314 /*********************************************************************************************/
316 static void RootPatternSetup(STRPTR filename)
318 struct BitMap *patternbm = NULL;
319 ULONG w;
320 ULONG h;
322 if ((myScreen = LockPubScreen(NULL)) != NULL)
324 D(bug("loading '%s'\n", filename));
325 patternbm = LoadDTBitmap(filename, myScreen, &myDTObject, &w, &h);
327 if (patternbm)
329 myData.pat_bm = patternbm;
330 myData.pat_width = w;
331 myData.pat_height = h;
333 myOldHook = installbackfillhook(myScreen, &myBackFillHook, &myData);
334 D(bug("oldhook=%p\n", myOldHook));
339 /*********************************************************************************************/
341 void RootPatternCleanup (void)
343 if (myOldHook != (APTR)-1)
345 D(bug("Reinstalling old backfillhook\n"));
346 removebackfillhook(myScreen, myOldHook);
347 myOldHook = (APTR)-1;
349 if (myDTObject)
351 D(bug("Disposing DT obj\n"));
352 DisposeDTObject(myDTObject);
353 myDTObject = NULL;
355 if (myScreen)
357 D(bug("Unlock PubScreen\n"));
358 UnlockPubScreen(NULL, myScreen);
359 myScreen = NULL;