Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / workbench / prefs / font / fpeditor.c
blob0d865656a306dc61e3185784ea57ebcc9349541e
1 /*
2 Copyright © 2003-2004, The AROS Development Team. All rights reserved.
3 $Id$
4 */
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>
24 #include <string.h>
25 #include <stdio.h>
27 #include "misc.h"
28 #include "locale.h"
29 #include "fpeditor.h"
31 /* Data is stored on disk in this format */
32 struct FileFontPrefs
34 UBYTE fp_Reserved[4 * 3];
35 UBYTE fp_Reserved2[2];
36 UBYTE fp_Type[2];
37 UBYTE fp_FrontPen;
38 UBYTE fp_BackPen;
39 UBYTE fp_Drawmode;
40 UBYTE fp_pad;
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 */
51 struct FPEditor_DATA
53 struct FontPrefs fped_FontPrefs[FP_COUNT];
54 Object *fped_IconsString,
55 *fped_ScreenString,
56 *fped_SystemString;
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 ******************************************************/
64 #if 0
65 void FontPrefs_Default(struct FontPrefs *fp[FP_COUNT])
67 UBYTE i;
69 for (i = 0; i < FP_COUNT; i++)
71 fp[i]->fp_Type = 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;
85 #endif
87 STATIC VOID convertEndian(struct FontPrefs *fp)
89 BYTE i;
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;
111 CopyMem
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. */
134 CopyMem
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
150 snprintf
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") */
163 ULONG size;
165 if (nameLength + suffixLength >= FONTNAMESIZE)
167 /* Not enough space for the font name */
168 return FALSE;
171 snprintf
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;
181 return TRUE;
184 BOOL Gadgets2FontPrefs
186 struct FPEditor_DATA *data
189 STRPTR str = NULL;
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;
204 return TRUE;
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);
224 return TRUE;
227 /*** Methods ****************************************************************/
228 Object *FPEditor__OM_NEW(Class *CLASS, Object *self, struct opSet *message)
230 Object *iconsString, *screenString, *systemString;
232 self = (Object *) DoSuperNewTags
234 CLASS, self, NULL,
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,
245 TextFrame,
246 MUIA_Background, MUII_TextBack,
247 End),
248 MUIA_Popstring_Button, (IPTR) PopButton(MUII_PopUp),
249 End,
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,
255 TextFrame,
256 MUIA_Background, MUII_TextBack,
257 End),
258 MUIA_Popstring_Button, (IPTR) PopButton(MUII_PopUp),
259 End,
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,
266 TextFrame,
267 MUIA_Background, MUII_TextBack,
268 End),
269 MUIA_Popstring_Button, (IPTR) PopButton(MUII_PopUp),
270 End,
271 End,
273 TAG_DONE
276 if (self != NULL)
278 SETUP_INST_DATA;
279 data->fped_IconsString = iconsString;
280 data->fped_ScreenString = screenString;
281 data->fped_SystemString = systemString;
283 /*-- Setup notifications -------------------------------------------*/
284 DoMethod
286 iconsString, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime,
287 (IPTR) self, 3, MUIM_Set, MUIA_PrefsEditor_Changed, TRUE
289 DoMethod
291 screenString, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime,
292 (IPTR) self, 3, MUIM_Set, MUIA_PrefsEditor_Changed, TRUE
294 DoMethod
296 systemString, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime,
297 (IPTR) self, 3, MUIM_Set, MUIA_PrefsEditor_Changed, TRUE
301 return self;
304 IPTR FPEditor__MUIM_PrefsEditor_ImportFH
306 Class *CLASS, Object *self,
307 struct MUIP_PrefsEditor_ImportFH *message
310 SETUP_INST_DATA;
311 struct ContextNode *context;
312 struct IFFHandle *handle;
313 BOOL success = TRUE;
314 LONG error;
316 if (!(handle = AllocIFF()))
318 ShowError(_(MSG_CANT_ALLOCATE_IFFPTR));
319 return(FALSE);
322 handle->iff_Stream = (IPTR) message->fh;
323 InitIFFasDOS(handle);
325 if ((error = OpenIFF(handle, IFFF_READ)) == 0)
327 BYTE i;
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)
344 if (error < 0)
346 printf("Error: ReadChunkBytes() returned %ld!\n", error);
349 FileFontPrefs2FontPrefs(&ffp, FP(i));
351 convertEndian(FP(i));
353 else
355 printf("ParseIFF() failed, returncode %ld!\n", error);
356 success = FALSE;
357 break;
360 else
362 printf("StopChunk() failed, returncode %ld!\n", error);
363 success = FALSE;
367 CloseIFF(handle);
369 else
371 ShowError(_(MSG_CANT_OPEN_STREAM));
374 FreeIFF(handle);
376 if (success) FontPrefs2Gadgets(data);
378 return success;
381 IPTR FPEditor__MUIM_PrefsEditor_ExportFH
383 Class *CLASS, Object *self,
384 struct MUIP_PrefsEditor_ExportFH *message
387 SETUP_INST_DATA;
388 struct PrefHeader header;
389 struct IFFHandle *handle;
390 BOOL success = TRUE;
391 LONG error = 0;
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! */
405 BYTE i;
407 PushChunk(handle, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN); /* FIXME: IFFSIZE_UNKNOWN? */
409 header.ph_Version = PHV_CURRENT;
410 header.ph_Type = 0;
412 PushChunk(handle, ID_PREF, ID_PRHD, IFFSIZE_UNKNOWN); /* FIXME: IFFSIZE_UNKNOWN? */
414 WriteChunkBytes(handle, &header, sizeof(struct PrefHeader));
416 PopChunk(handle);
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
443 PopChunk(handle);
445 else
447 ShowError(_(MSG_CANT_OPEN_STREAM));
448 success = FALSE;
451 CloseIFF(handle);
452 FreeIFF(handle);
454 else // AllocIFF()
456 // Do something more here - if IFF allocation has failed, something isn't right
457 ShowError(_(MSG_CANT_ALLOCATE_IFFPTR));
458 success = FALSE;
461 return success;
464 /*** Setup ******************************************************************/
465 ZUNE_CUSTOMCLASS_3
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 *