2 Copyright © 2003-2004, The AROS Development Team. All rights reserved.
6 #define MUIMASTER_YES_INLINE_STDARG
8 #include <exec/types.h>
9 #include <utility/tagitem.h>
10 #include <libraries/asl.h>
11 #include <libraries/mui.h>
12 #include <prefs/prefhdr.h>
13 #include <prefs/font.h>
14 #include <zune/customclasses.h>
15 #include <zune/prefseditor.h>
17 #include <proto/exec.h>
18 #include <proto/intuition.h>
19 #include <proto/utility.h>
20 #include <proto/muimaster.h>
21 #include <proto/dos.h>
22 #include <proto/iffparse.h>
31 /* Data is stored on disk in this format */
34 UBYTE fp_Reserved
[4 * 3];
35 UBYTE fp_Reserved2
[2];
41 UBYTE fp_TextAttr_ta_Name
[4];
42 UBYTE fp_TextAttr_ta_YSize
[2];
43 UBYTE fp_TextAttr_ta_Style
;
44 UBYTE fp_TextAttr_ta_Flags
;
45 BYTE fp_Name
[FONTNAMESIZE
];
48 /*** Instance Data **********************************************************/
49 #define FP_COUNT (3) /* Number of entries in fped_FontPrefs array */
53 struct FontPrefs fped_FontPrefs
[FP_COUNT
];
54 Object
*fped_IconsString
,
59 /*** Macros *****************************************************************/
60 #define SETUP_INST_DATA struct FPEditor_DATA *data = INST_DATA(CLASS, self)
61 #define FP(i) (&(data->fped_FontPrefs[(i)]))
63 /*** Utility Functions ******************************************************/
65 void FontPrefs_Default(struct FontPrefs
*fp
[FP_COUNT
])
69 for (i
= 0; i
< FP_COUNT
; i
++)
72 fp
[i
]->fp_FrontPen
= 0; /* FIXME: Is this (really) default? Look it up! */
73 fp
[i
]->fp_BackPen
= 0; /* FIXME: Is this (really) default? Look it up! */
74 fp
[i
]->fp_DrawMode
= 0; /* FIXME: Is this (really) default? Look it up! */
76 fp
[i
]->fp_TextAttr
.ta_YSize
= 8; /* FIXME: Is this (really) default? Look it up! */
77 fp
[i
]->fp_TextAttr
.ta_Style
= FS_NORMAL
;
78 fp
[i
]->fp_TextAttr
.ta_Flags
= FPB_DISKFONT
; /* FIXME: Is this (really) default? Look it up! */
80 fp
[i
]->fp_Name
[0] = '\0';
81 strlcat(fp
[i
]->fp_Name
, "topaz.font", FONTNAMESIZE
); /* FIXME: Is this (really) default? Check it up! */
82 fp
[i
]->fp_TextAttr
.ta_Name
= fp
[i
]->fp_Name
;
87 STATIC VOID
convertEndian(struct FontPrefs
*fp
)
91 for (i
= 0; i
<= 2; i
++)
93 fp
->fp_Reserved
[i
] = AROS_BE2LONG(fp
->fp_Reserved
[i
]);
96 fp
->fp_Reserved2
= AROS_BE2WORD(fp
->fp_Reserved2
);
97 fp
->fp_Type
= AROS_BE2WORD(fp
->fp_Type
);
98 fp
->fp_TextAttr
.ta_YSize
= AROS_BE2WORD(fp
->fp_TextAttr
.ta_YSize
);
101 VOID
FileFontPrefs2FontPrefs(struct FileFontPrefs
*ffp
, struct FontPrefs
*fp
)
103 /* Copy field by field to avoid any alignment problems whatsoever */
104 CopyMem(&ffp
->fp_Reserved
, &fp
->fp_Reserved
, sizeof(fp
->fp_Reserved
));
105 CopyMem(&ffp
->fp_Reserved2
, &fp
->fp_Reserved2
, sizeof(fp
->fp_Reserved2
));
106 CopyMem(&ffp
->fp_Type
, &fp
->fp_Type
, sizeof(fp
->fp_Type
));
107 fp
->fp_FrontPen
= ffp
->fp_FrontPen
;
108 fp
->fp_BackPen
= ffp
->fp_BackPen
;
109 fp
->fp_DrawMode
= ffp
->fp_Drawmode
;
110 fp
->fp_TextAttr
.ta_Name
= fp
->fp_Name
;
113 &ffp
->fp_TextAttr_ta_YSize
,
114 &fp
->fp_TextAttr
.ta_YSize
,
115 sizeof(fp
->fp_TextAttr
.ta_YSize
)
117 fp
->fp_TextAttr
.ta_Style
= ffp
->fp_TextAttr_ta_Style
;
118 fp
->fp_TextAttr
.ta_Flags
= ffp
->fp_TextAttr_ta_Flags
;
119 CopyMem(&ffp
->fp_Name
, &fp
->fp_Name
, FONTNAMESIZE
);
122 VOID
FontPrefs2FileFontPrefs(struct FontPrefs
*fp
, struct FileFontPrefs
*ffp
)
124 /* Copy field by field to avoid any alignment problems whatsoever */
125 CopyMem(&fp
->fp_Reserved
, &ffp
->fp_Reserved
, sizeof(fp
->fp_Reserved
));
126 CopyMem(&fp
->fp_Reserved2
, &ffp
->fp_Reserved2
, sizeof(fp
->fp_Reserved2
));
127 CopyMem(&fp
->fp_Type
, &ffp
->fp_Type
, sizeof(fp
->fp_Type
));
128 ffp
->fp_FrontPen
= fp
->fp_FrontPen
;
129 ffp
->fp_BackPen
= fp
->fp_BackPen
;
130 ffp
->fp_Drawmode
= fp
->fp_DrawMode
;
131 /* fp->fp_TextAttr.ta_Name is not copied, it may have different sizes on
132 different architectures and contains only a pointer, so I guess there's
133 no need to write it on disk. */
136 &fp
->fp_TextAttr
.ta_YSize
,
137 &ffp
->fp_TextAttr_ta_YSize
,
138 sizeof(fp
->fp_TextAttr
.ta_YSize
)
140 ffp
->fp_TextAttr_ta_Style
= fp
->fp_TextAttr
.ta_Style
;
141 ffp
->fp_TextAttr_ta_Flags
= fp
->fp_TextAttr
.ta_Flags
;
142 CopyMem(&fp
->fp_Name
, &ffp
->fp_Name
, FONTNAMESIZE
);
145 VOID FontPrefs2FontString
147 STRPTR buffer
, ULONG buffersize
, struct FontPrefs
*fp
152 buffer
, buffersize
, "%.*s/%d",
153 strlen(fp
->fp_TextAttr
.ta_Name
) - 5 /* strlen(".font") */,
154 fp
->fp_TextAttr
.ta_Name
, fp
->fp_TextAttr
.ta_YSize
158 BOOL
FontString2FontPrefs(struct FontPrefs
*fp
, CONST_STRPTR buffer
)
160 STRPTR separator
= PathPart((STRPTR
) buffer
);
161 ULONG nameLength
= separator
- buffer
;
162 ULONG suffixLength
= 5; /* strlen(".font") */
165 if (nameLength
+ suffixLength
>= FONTNAMESIZE
)
167 /* Not enough space for the font name */
173 fp
->fp_Name
, nameLength
+ suffixLength
+ 1, "%.*s.font",
174 (int) nameLength
, buffer
176 fp
->fp_TextAttr
.ta_Name
= fp
->fp_Name
;
178 StrToLong(FilePart((STRPTR
) buffer
), &size
);
179 fp
->fp_TextAttr
.ta_YSize
= size
;
184 BOOL Gadgets2FontPrefs
186 struct FPEditor_DATA
*data
191 // FIXME: error checking
192 GET(data
->fped_IconsString
, MUIA_String_Contents
, &str
);
193 FontString2FontPrefs(FP(FP_WBFONT
), str
);
194 FP(FP_WBFONT
)->fp_Type
= FP_WBFONT
;
196 GET(data
->fped_SystemString
, MUIA_String_Contents
, &str
);
197 FontString2FontPrefs(FP(FP_SYSFONT
), str
);
198 FP(FP_SYSFONT
)->fp_Type
= FP_SYSFONT
;
200 GET(data
->fped_ScreenString
, MUIA_String_Contents
, &str
);
201 FontString2FontPrefs(FP(FP_SCREENFONT
), str
);
202 FP(FP_SCREENFONT
)->fp_Type
= FP_SCREENFONT
;
207 BOOL FontPrefs2Gadgets
209 struct FPEditor_DATA
*data
212 TEXT buffer
[FONTNAMESIZE
+ 8];
214 // FIXME: error checking
215 FontPrefs2FontString(buffer
, FONTNAMESIZE
+ 8, FP(FP_WBFONT
));
216 NNSET(data
->fped_IconsString
, MUIA_String_Contents
, (IPTR
) buffer
);
218 FontPrefs2FontString(buffer
, FONTNAMESIZE
+ 8, FP(FP_SYSFONT
));
219 NNSET(data
->fped_SystemString
, MUIA_String_Contents
, (IPTR
) buffer
);
221 FontPrefs2FontString(buffer
, FONTNAMESIZE
+ 8, FP(FP_SCREENFONT
));
222 NNSET(data
->fped_ScreenString
, MUIA_String_Contents
, (IPTR
) buffer
);
227 /*** Methods ****************************************************************/
228 Object
*FPEditor__OM_NEW(Class
*CLASS
, Object
*self
, struct opSet
*message
)
230 Object
*iconsString
, *screenString
, *systemString
;
232 self
= (Object
*) DoSuperNewTags
236 MUIA_PrefsEditor_Name
, __(MSG_NAME
),
237 MUIA_PrefsEditor_Path
, (IPTR
) "SYS/Font.prefs",
239 Child
, (IPTR
) ColGroup(2),
240 Child
, (IPTR
) Label2(_(MSG_ICONS
)),
241 Child
, (IPTR
) PopaslObject
,
242 MUIA_Popasl_Type
, ASL_FontRequest
,
243 ASLFO_MaxHeight
, 100,
244 MUIA_Popstring_String
, (IPTR
) (iconsString
= (Object
*)StringObject
,
246 MUIA_Background
, MUII_TextBack
,
248 MUIA_Popstring_Button
, (IPTR
) PopButton(MUII_PopUp
),
250 Child
, (IPTR
) Label2(_(MSG_SCREEN
)),
251 Child
, (IPTR
) PopaslObject
,
252 MUIA_Popasl_Type
, ASL_FontRequest
,
253 ASLFO_MaxHeight
, 100,
254 MUIA_Popstring_String
, (IPTR
) (screenString
= (Object
*)StringObject
,
256 MUIA_Background
, MUII_TextBack
,
258 MUIA_Popstring_Button
, (IPTR
) PopButton(MUII_PopUp
),
260 Child
, (IPTR
) Label2(_(MSG_SYSTEM
)),
261 Child
, (IPTR
) PopaslObject
,
262 MUIA_Popasl_Type
, ASL_FontRequest
,
263 ASLFO_FixedWidthOnly
, TRUE
,
264 ASLFO_MaxHeight
, 100,
265 MUIA_Popstring_String
, (IPTR
) (systemString
= (Object
*)StringObject
,
267 MUIA_Background
, MUII_TextBack
,
269 MUIA_Popstring_Button
, (IPTR
) PopButton(MUII_PopUp
),
279 data
->fped_IconsString
= iconsString
;
280 data
->fped_ScreenString
= screenString
;
281 data
->fped_SystemString
= systemString
;
283 /*-- Setup notifications -------------------------------------------*/
286 iconsString
, MUIM_Notify
, MUIA_String_Acknowledge
, MUIV_EveryTime
,
287 (IPTR
) self
, 3, MUIM_Set
, MUIA_PrefsEditor_Changed
, TRUE
291 screenString
, MUIM_Notify
, MUIA_String_Acknowledge
, MUIV_EveryTime
,
292 (IPTR
) self
, 3, MUIM_Set
, MUIA_PrefsEditor_Changed
, TRUE
296 systemString
, MUIM_Notify
, MUIA_String_Acknowledge
, MUIV_EveryTime
,
297 (IPTR
) self
, 3, MUIM_Set
, MUIA_PrefsEditor_Changed
, TRUE
304 IPTR FPEditor__MUIM_PrefsEditor_ImportFH
306 Class
*CLASS
, Object
*self
,
307 struct MUIP_PrefsEditor_ImportFH
*message
311 struct ContextNode
*context
;
312 struct IFFHandle
*handle
;
316 if (!(handle
= AllocIFF()))
318 ShowError(_(MSG_CANT_ALLOCATE_IFFPTR
));
322 handle
->iff_Stream
= (IPTR
) message
->fh
;
323 InitIFFasDOS(handle
);
325 if ((error
= OpenIFF(handle
, IFFF_READ
)) == 0)
329 // FIXME: We want some sanity checking here!
330 for (i
= 0; i
< FP_COUNT
; i
++)
332 if ((error
= StopChunk(handle
, ID_PREF
, ID_FONT
)) == 0)
334 if ((error
= ParseIFF(handle
, IFFPARSE_SCAN
)) == 0)
336 struct FileFontPrefs ffp
;
337 context
= CurrentChunk(handle
);
339 error
= ReadChunkBytes
341 handle
, &ffp
, sizeof(struct FileFontPrefs
)
346 printf("Error: ReadChunkBytes() returned %ld!\n", error
);
349 FileFontPrefs2FontPrefs(&ffp
, FP(i
));
351 convertEndian(FP(i
));
355 printf("ParseIFF() failed, returncode %ld!\n", error
);
362 printf("StopChunk() failed, returncode %ld!\n", error
);
371 ShowError(_(MSG_CANT_OPEN_STREAM
));
376 if (success
) FontPrefs2Gadgets(data
);
381 IPTR FPEditor__MUIM_PrefsEditor_ExportFH
383 Class
*CLASS
, Object
*self
,
384 struct MUIP_PrefsEditor_ExportFH
*message
388 struct PrefHeader header
;
389 struct IFFHandle
*handle
;
393 Gadgets2FontPrefs(data
);
395 memset(&header
, 0, sizeof(struct PrefHeader
));
397 if ((handle
= AllocIFF()))
399 handle
->iff_Stream
= (IPTR
) message
->fh
;
401 InitIFFasDOS(handle
);
403 if (!(error
= OpenIFF(handle
, IFFF_WRITE
))) /* NULL = successful! */
407 PushChunk(handle
, ID_PREF
, ID_FORM
, IFFSIZE_UNKNOWN
); /* FIXME: IFFSIZE_UNKNOWN? */
409 header
.ph_Version
= PHV_CURRENT
;
412 PushChunk(handle
, ID_PREF
, ID_PRHD
, IFFSIZE_UNKNOWN
); /* FIXME: IFFSIZE_UNKNOWN? */
414 WriteChunkBytes(handle
, &header
, sizeof(struct PrefHeader
));
418 for (i
= 0; i
< FP_COUNT
; i
++)
420 struct FileFontPrefs ffp
;
421 error
= PushChunk(handle
, ID_PREF
, ID_FONT
, sizeof(struct FileFontPrefs
));
423 if (error
!= 0) // TODO: We need some error checking here!
425 printf("error: PushChunk() = %ld\n", error
);
428 convertEndian(FP(i
)); // Convert to m68k endian
429 FontPrefs2FileFontPrefs(FP(i
), &ffp
);
431 error
= WriteChunkBytes(handle
, &ffp
, sizeof(struct FileFontPrefs
));
432 error
= PopChunk(handle
);
434 convertEndian(FP(i
)); // Revert to initial endian
436 if (error
!= 0) // TODO: We need some error checking here!
438 printf("error: PopChunk() = %ld\n", error
);
442 // Terminate the FORM
447 ShowError(_(MSG_CANT_OPEN_STREAM
));
456 // Do something more here - if IFF allocation has failed, something isn't right
457 ShowError(_(MSG_CANT_ALLOCATE_IFFPTR
));
464 /*** Setup ******************************************************************/
467 FPEditor
, NULL
, MUIC_PrefsEditor
, NULL
,
468 OM_NEW
, struct opSet
*,
469 MUIM_PrefsEditor_ImportFH
, struct MUIP_PrefsEditor_ImportFH
*,
470 MUIM_PrefsEditor_ExportFH
, struct MUIP_PrefsEditor_ExportFH
*