added concrete implementations of putc(), getc(), getchar() and gets()
[tangerine.git] / workbench / prefs / font / fpeditor.c
blobc7909968f9a5f92e418cec225badfd1a081e0770
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"
32 /*** Instance Data **********************************************************/
33 #define FP_COUNT (3) /* Number of entries in fped_FontPrefs array */
35 struct FPEditor_DATA
37 struct FontPrefs fped_FontPrefs[FP_COUNT];
38 Object *fped_IconsString,
39 *fped_ScreenString,
40 *fped_SystemString;
43 /*** Macros *****************************************************************/
44 #define SETUP_INST_DATA struct FPEditor_DATA *data = INST_DATA(CLASS, self)
45 #define FP(i) (&(data->fped_FontPrefs[(i)]))
47 /*** Utility Functions ******************************************************/
48 #if 0
49 void FontPrefs_Default(struct FontPrefs *fp[FP_COUNT])
51 UBYTE i;
53 for (i = 0; i < FP_COUNT; i++)
55 fp[i]->fp_Type = i;
56 fp[i]->fp_FrontPen = 0; /* FIXME: Is this (really) default? Look it up! */
57 fp[i]->fp_BackPen = 0; /* FIXME: Is this (really) default? Look it up! */
58 fp[i]->fp_DrawMode = 0; /* FIXME: Is this (really) default? Look it up! */
60 fp[i]->fp_TextAttr.ta_YSize = 8; /* FIXME: Is this (really) default? Look it up! */
61 fp[i]->fp_TextAttr.ta_Style = FS_NORMAL;
62 fp[i]->fp_TextAttr.ta_Flags = FPB_DISKFONT; /* FIXME: Is this (really) default? Look it up! */
64 fp[i]->fp_Name[0] = '\0';
65 strlcat(fp[i]->fp_Name, "topaz.font", FONTNAMESIZE); /* FIXME: Is this (really) default? Check it up! */
66 fp[i]->fp_TextAttr.ta_Name = fp[i]->fp_Name;
69 #endif
71 STATIC VOID convertEndian(struct FontPrefs *fp)
73 BYTE i;
75 for (i = 0; i <= 2; i++)
77 fp->fp_Reserved[i] = AROS_BE2LONG(fp->fp_Reserved[i]);
80 fp->fp_Reserved2 = AROS_BE2WORD(fp->fp_Reserved2);
81 fp->fp_Type = AROS_BE2WORD(fp->fp_Type);
82 fp->fp_TextAttr.ta_YSize = AROS_BE2WORD(fp->fp_TextAttr.ta_YSize);
86 VOID FontPrefs2FontString
88 STRPTR buffer, ULONG buffersize, struct FontPrefs *fp
91 snprintf
93 buffer, buffersize, "%.*s/%d",
94 strlen(fp->fp_TextAttr.ta_Name) - 5 /* strlen(".font") */,
95 fp->fp_TextAttr.ta_Name, fp->fp_TextAttr.ta_YSize
99 BOOL FontString2FontPrefs(struct FontPrefs *fp, CONST_STRPTR buffer)
101 STRPTR separator = PathPart((STRPTR) buffer);
102 ULONG nameLength = separator - buffer;
103 ULONG suffixLength = 5; /* strlen(".font") */
104 ULONG size;
106 if (nameLength + suffixLength >= FONTNAMESIZE)
108 /* Not enough space for the font name */
109 return FALSE;
112 snprintf
114 fp->fp_Name, nameLength + suffixLength + 1, "%.*s.font",
115 (int) nameLength, buffer
117 fp->fp_TextAttr.ta_Name = fp->fp_Name;
119 StrToLong(FilePart((STRPTR) buffer), &size);
120 fp->fp_TextAttr.ta_YSize = size;
122 return TRUE;
125 BOOL Gadgets2FontPrefs
127 struct FPEditor_DATA *data
130 STRPTR str = NULL;
132 // FIXME: error checking
133 GET(data->fped_IconsString, MUIA_String_Contents, &str);
134 FontString2FontPrefs(FP(FP_WBFONT), str);
136 GET(data->fped_SystemString, MUIA_String_Contents, &str);
137 FontString2FontPrefs(FP(FP_SYSFONT), str);
139 GET(data->fped_ScreenString, MUIA_String_Contents, &str);
140 FontString2FontPrefs(FP(FP_SCREENFONT), str);
142 return TRUE;
145 BOOL FontPrefs2Gadgets
147 struct FPEditor_DATA *data
150 TEXT buffer[FONTNAMESIZE + 8];
152 // FIXME: error checking
153 FontPrefs2FontString(buffer, FONTNAMESIZE + 8, FP(FP_WBFONT));
154 NNSET(data->fped_IconsString, MUIA_String_Contents, (IPTR) buffer);
156 FontPrefs2FontString(buffer, FONTNAMESIZE + 8, FP(FP_SYSFONT));
157 NNSET(data->fped_SystemString, MUIA_String_Contents, (IPTR) buffer);
159 FontPrefs2FontString(buffer, FONTNAMESIZE + 8, FP(FP_SCREENFONT));
160 NNSET(data->fped_ScreenString, MUIA_String_Contents, (IPTR) buffer);
162 return TRUE;
165 /*** Methods ****************************************************************/
166 Object *FPEditor__OM_NEW(Class *CLASS, Object *self, struct opSet *message)
168 Object *iconsString, *screenString, *systemString;
170 self = (Object *) DoSuperNewTags
172 CLASS, self, NULL,
174 MUIA_PrefsEditor_Name, __(MSG_NAME),
175 MUIA_PrefsEditor_Path, (IPTR) "SYS/Font.prefs",
177 Child, (IPTR) ColGroup(2),
178 Child, (IPTR) Label2(_(MSG_ICONS)),
179 Child, (IPTR) PopaslObject,
180 MUIA_Popasl_Type, ASL_FontRequest,
181 ASLFO_MaxHeight, 100,
182 MUIA_Popstring_String, (IPTR) (iconsString = StringObject,
183 TextFrame,
184 MUIA_Background, MUII_TextBack,
185 End),
186 MUIA_Popstring_Button, (IPTR) PopButton(MUII_PopUp),
187 End,
188 Child, (IPTR) Label2(_(MSG_SCREEN)),
189 Child, (IPTR) PopaslObject,
190 MUIA_Popasl_Type, ASL_FontRequest,
191 ASLFO_MaxHeight, 100,
192 MUIA_Popstring_String, (IPTR) (screenString = StringObject,
193 TextFrame,
194 MUIA_Background, MUII_TextBack,
195 End),
196 MUIA_Popstring_Button, (IPTR) PopButton(MUII_PopUp),
197 End,
198 Child, (IPTR) Label2(_(MSG_SYSTEM)),
199 Child, (IPTR) PopaslObject,
200 MUIA_Popasl_Type, ASL_FontRequest,
201 ASLFO_FixedWidthOnly, TRUE,
202 ASLFO_MaxHeight, 100,
203 MUIA_Popstring_String, (IPTR) (systemString = StringObject,
204 TextFrame,
205 MUIA_Background, MUII_TextBack,
206 End),
207 MUIA_Popstring_Button, (IPTR) PopButton(MUII_PopUp),
208 End,
209 End,
211 TAG_DONE
214 if (self != NULL)
216 SETUP_INST_DATA;
217 data->fped_IconsString = iconsString;
218 data->fped_ScreenString = screenString;
219 data->fped_SystemString = systemString;
221 /*-- Setup notifications -------------------------------------------*/
222 DoMethod
224 iconsString, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime,
225 (IPTR) self, 3, MUIM_Set, MUIA_PrefsEditor_Changed, TRUE
227 DoMethod
229 screenString, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime,
230 (IPTR) self, 3, MUIM_Set, MUIA_PrefsEditor_Changed, TRUE
232 DoMethod
234 systemString, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime,
235 (IPTR) self, 3, MUIM_Set, MUIA_PrefsEditor_Changed, TRUE
239 return self;
242 IPTR FPEditor__MUIM_PrefsEditor_ImportFH
244 Class *CLASS, Object *self,
245 struct MUIP_PrefsEditor_ImportFH *message
248 SETUP_INST_DATA;
249 struct ContextNode *context;
250 struct IFFHandle *handle;
251 BOOL success = TRUE;
252 LONG error;
254 if (!(handle = AllocIFF()))
256 ShowError(_(MSG_CANT_ALLOCATE_IFFPTR));
257 return(FALSE);
260 handle->iff_Stream = (IPTR) message->fh;
261 InitIFFasDOS(handle);
263 if ((error = OpenIFF(handle, IFFF_READ)) == 0)
265 BYTE i;
267 // FIXME: We want some sanity checking here!
268 for (i = 0; i < FP_COUNT; i++)
270 if ((error = StopChunk(handle, ID_PREF, ID_FONT)) == 0)
272 if ((error = ParseIFF(handle, IFFPARSE_SCAN)) == 0)
274 context = CurrentChunk(handle);
276 error = ReadChunkBytes
278 handle, FP(i), sizeof(struct FontPrefs)
281 if (error < 0)
283 printf("Error: ReadChunkBytes() returned %ld!\n", error);
286 FP(i)->fp_TextAttr.ta_Name = FP(i)->fp_Name;
288 convertEndian(FP(i));
290 else
292 printf("ParseIFF() failed, returncode %ld!\n", error);
293 success = FALSE;
294 break;
297 else
299 printf("StopChunk() failed, returncode %ld!\n", error);
300 success = FALSE;
304 CloseIFF(handle);
306 else
308 ShowError(_(MSG_CANT_OPEN_STREAM));
311 FreeIFF(handle);
313 if (success) FontPrefs2Gadgets(data);
315 return success;
318 IPTR FPEditor__MUIM_PrefsEditor_ExportFH
320 Class *CLASS, Object *self,
321 struct MUIP_PrefsEditor_ExportFH *message
324 SETUP_INST_DATA;
325 struct PrefHeader header;
326 struct IFFHandle *handle;
327 BOOL success = TRUE;
328 LONG error = 0;
330 Gadgets2FontPrefs(data);
332 memset(&header, 0, sizeof(struct PrefHeader));
334 if ((handle = AllocIFF()))
336 handle->iff_Stream = (IPTR) message->fh;
338 InitIFFasDOS(handle);
340 if (!(error = OpenIFF(handle, IFFF_WRITE))) /* NULL = successful! */
342 BYTE i;
344 PushChunk(handle, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN); /* FIXME: IFFSIZE_UNKNOWN? */
346 header.ph_Version = PHV_CURRENT;
347 header.ph_Type = 0;
349 PushChunk(handle, ID_PREF, ID_PRHD, IFFSIZE_UNKNOWN); /* FIXME: IFFSIZE_UNKNOWN? */
351 WriteChunkBytes(handle, &header, sizeof(struct PrefHeader));
353 PopChunk(handle);
355 for (i = 0; i < FP_COUNT; i++)
357 error = PushChunk(handle, ID_PREF, ID_FONT, sizeof(struct FontPrefs));
359 if (error != 0) // TODO: We need some error checking here!
361 printf("error: PushChunk() = %ld\n", error);
364 convertEndian(FP(i)); // Convert to m68k endian
366 error = WriteChunkBytes(handle, FP(i), sizeof(struct FontPrefs));
367 error = PopChunk(handle);
369 convertEndian(FP(i)); // Revert to initial endian
371 if (error != 0) // TODO: We need some error checking here!
373 printf("error: PopChunk() = %ld\n", error);
377 // Terminate the FORM
378 PopChunk(handle);
380 else
382 ShowError(_(MSG_CANT_OPEN_STREAM));
383 success = FALSE;
386 CloseIFF(handle);
387 FreeIFF(handle);
389 else // AllocIFF()
391 // Do something more here - if IFF allocation has failed, something isn't right
392 ShowError(_(MSG_CANT_ALLOCATE_IFFPTR));
393 success = FALSE;
396 return success;
399 /*** Setup ******************************************************************/
400 ZUNE_CUSTOMCLASS_3
402 FPEditor, NULL, MUIC_PrefsEditor, NULL,
403 OM_NEW, struct opSet *,
404 MUIM_PrefsEditor_ImportFH, struct MUIP_PrefsEditor_ImportFH *,
405 MUIM_PrefsEditor_ExportFH, struct MUIP_PrefsEditor_ExportFH *