make sure retval is not used uninitialized
[AROS-Contrib.git] / vpdf / settings.c
blob4a195880f900addc476811273c7b526d252050dd
1 /// System includes
2 #define AROS_ALMOST_COMPATIBLE
3 #include <proto/muimaster.h>
4 #include <libraries/mui.h>
6 #include <libraries/asl.h>
7 #include <workbench/workbench.h>
9 #include <proto/exec.h>
10 #include <intuition/intuition.h>
11 #include <graphics/gfx.h>
12 #include <exec/libraries.h>
13 #include <proto/graphics.h>
14 #include <proto/intuition.h>
15 #include <proto/icon.h>
16 #include <dos/dos.h>
17 #include <proto/dos.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <math.h>
23 #include <memory.h>
25 #include <proto/alib.h>
26 #include <proto/utility.h>
28 #include <proto/datatypes.h>
29 #include <proto/dtclass.h>
30 #include <datatypes/pictureclass.h>
31 #include <devices/rawkeycodes.h>
33 #include <libraries/gadtools.h>
35 #if defined(__MORPHOS__)
36 #include <emul/emulregs.h>
37 #include <emul/emulinterface.h>
38 #endif
39 ////
41 #include <private/vapor/vapor.h>
42 #include "util.h"
43 #include "poppler.h"
44 #include "settings.h"
45 #include "locale.h"
46 #include "system/functions.h"
48 #include "mcc/documentview_class.h"
49 #include "mcc/documentlayout_class.h"
50 #include "mcc/toolbar_class.h"
51 #include "mcc/renderer_class.h"
52 #include "mcc/title_class.h"
54 #define ATTR(a) a, #a
56 struct Data
58 ULONG dummy;
59 int groupsmodified;
63 static const char *layouts[] = {"Single page", "Continuous", "Facing pages", "Continuous facing pages", NULL};
64 static const char *scalings[] = {"Fit to Window", "Fit to Window Width", "Original", NULL};
65 static char *ps_levels[] = {"Level 2", "Level 3", NULL};
67 struct attribute
69 Object *object;
70 unsigned int attribute;
71 int group;
72 unsigned int value;
73 char svalue[512]; // TODO: group them in an union? fix length limit? -- kiero
74 int etype;
75 char name[64];
78 struct attribute attributes[MUIA_VPDFSettings_Last - MUIA_VPDFSettings_First + 1] = {0};
79 struct attribute attributes_old[MUIA_VPDFSettings_Last - MUIA_VPDFSettings_First + 1] = {0};
83 Object *register_attribute(int id, char *name, Object *obj, unsigned int attribute, int group, int etype, unsigned int initial)
85 id = id - MUIA_VPDFSettings_First;
87 attributes[id].object = obj;
88 attributes[id].attribute = attribute;
89 attributes[id].group = group;
90 attributes[id].value = 0;
91 attributes[id].svalue[0] = 0;
92 attributes[id].etype = etype;
93 stccpy(attributes[id].name, name, sizeof(attributes[id].name));
94 if (strlen(name) >= sizeof(attributes[id].name))
95 printf("************SETTINGS ERROR: Attribute name too long:%s****************\n", name);
97 switch(etype)
99 case ETYPE_NUMERIC:
100 attributes[id].value = initial;
101 break;
102 case ETYPE_STRING:
103 stccpy(attributes[id].svalue, (char*)initial, sizeof(attributes[id].svalue));
104 break;
107 return obj;
110 /* save settings to disk */
112 static int setWrite(char *fname)
114 FILE *f = fopen(fname, "w");
116 if (f != NULL)
118 int i;
120 for(i=MUIA_VPDFSettings_First+1; i<MUIA_VPDFSettings_Last; i++)
122 int id = i - MUIA_VPDFSettings_First;
123 if (attributes[id].object != NULL)
125 switch (attributes[id].etype)
128 case ETYPE_NUMERIC:
129 fprintf(f, "%s 0x%lx\n", attributes[id].name, attributes[id].value);
130 break;
131 case ETYPE_STRING:
133 char *enter;
134 if ((enter = strrchr(attributes[id].svalue, '\n')))
135 *enter = '\0';
136 fprintf(f, "%s %s\n", attributes[id].name, attributes[id].svalue);
137 break;
142 fclose(f);
143 return TRUE;
145 return FALSE;
148 static int setRead(char *fname)
150 FILE *f = fopen(fname, "r");
152 if (f != NULL)
154 int i;
155 char buffer[2048];
157 while(fgets(buffer, sizeof(buffer), f))
159 char *delim = strchr(buffer, ' ');
160 if (delim != NULL)
162 char *name = buffer;
163 char *value = delim + 1;
165 *delim = '\0';
167 /* lookup attribute and set value */
169 for(i=MUIA_VPDFSettings_First+1; i<MUIA_VPDFSettings_Last; i++)
171 int id = i - MUIA_VPDFSettings_First;
173 if (attributes[id].object != NULL && 0 == strcmp(name, attributes[id].name))
175 switch (attributes[id].etype)
177 case ETYPE_NUMERIC:
179 unsigned int lvalue = strtol(value, NULL, 0); /* values are written as hex, but in case user modifies them... */
180 attributes[id].value = lvalue;
181 break;
183 case ETYPE_STRING:
185 stccpy(attributes[id].svalue, value, sizeof(attributes[id].svalue));
186 break;
194 fclose(f);
195 return TRUE;
197 return FALSE;
201 static int setSave(void)
203 return setWrite("ENVARC:VPDF.config");
206 static int setUse(void)
208 return setWrite("ENV:VPDF.config");
211 static Object *buildGroupSeparator(char *title)
213 Object *p1;
214 Object *grp = HGroup,
215 MUIA_InnerBottom, 0,
216 MUIA_InnerTop, 0,
217 MUIA_Group_VertSpacing, 0,
218 Child, p1 = MUI_MakeObject(MUIO_HBar,1),
219 Child, LLabel1(title),
220 Child, MUI_MakeObject(MUIO_HBar,1),
221 End;
223 set(p1, MUIA_Weight, 10);
224 return grp;
228 DEFNEW
230 obj = DoSuperNew(cl, obj,
231 Child, PageGroup,
232 // MUIA_Group_ActivePage, 1,
233 Child, VGroup,
234 MUIA_Background, MUII_GroupBack,
235 Child, buildGroupSeparator(LOCSTR(MSG_SETTINGS_DOCUMENT)),
236 Child, ColGroup(3),
237 Child, LLabel1(LOCSTR(MSG_SETTINGS_LAYOUT)),
238 Child, register_attribute(ATTR(MUIA_VPDFSettings_Layout),
239 CycleObject, MUIA_Cycle_Entries, layouts, End,
240 MUIA_Cycle_Active, VPDF_SGROUP_LAYOUT, ETYPE_NUMERIC, MUIV_DocumentView_Layout_Single),
241 Child, HSpace(0),
242 Child, LLabel1(LOCSTR(MSG_SETTINGS_SCALING)),
243 Child, register_attribute(ATTR(MUIA_VPDFSettings_Scaling),
244 CycleObject, MUIA_Cycle_Entries, scalings, End,
245 MUIA_Cycle_Active, VPDF_SGROUP_SCALING, ETYPE_NUMERIC, MUIV_DocumentLayout_Scaling_FitPage),
246 Child, HSpace(0),
247 End,
249 Child, buildGroupSeparator(LOCSTR(MSG_WINDOWS)),
251 Child, ColGroup(3),
253 Child, HGroup,
254 Child, register_attribute(ATTR(MUIA_VPDFSettings_Outline),
255 CheckMark(TRUE),
256 MUIA_Selected, VPDF_SGROUP_GUI, ETYPE_NUMERIC, TRUE),
257 End,
258 Child, LLabel1(LOCSTR(MSG_SETTINGS_OUTLINE)),
260 Child, HSpace(0),
262 Child, HGroup,
263 Child, register_attribute(ATTR(MUIA_VPDFSettings_OpenLog),
264 CheckMark(FALSE),
265 MUIA_Selected, VPDF_SGROUP_GUI, ETYPE_NUMERIC, FALSE),
266 End,
267 Child, LLabel1(LOCSTR(MSG_SETTINGS_LOGWINDOW)),
268 Child, HSpace(0),
271 End,
273 Child, HVSpace,
274 End,
275 Child, VGroup,
276 Child, register_attribute(ATTR(MUIA_VPDFSettings_PSName),
277 StringObject,
278 MUIA_Frame, MUIV_Frame_String,
279 MUIA_String_Contents, "PS:",
280 MUIA_String_MaxLen, 512,
281 End,
282 MUIA_String_Contents, VPDF_SGROUP_PRINTER, ETYPE_STRING, "PS:"),
284 Child, register_attribute(ATTR(MUIA_VPDFSettings_PrintingMode),
285 CheckMark(TRUE),
286 MUIA_Selected, VPDF_SGROUP_PRINTER, ETYPE_NUMERIC, 1),
288 Child, register_attribute(ATTR(MUIA_VPDFSettings_PSMode),
289 CycleObject, MUIA_Cycle_Entries, ps_levels, End,
290 MUIA_Cycle_Active, VPDF_SGROUP_PRINTER, ETYPE_NUMERIC, 0),
291 End,
293 End,
294 TAG_MORE, INITTAGS);
296 if (obj != NULL)
299 /* load from file */
301 setRead("ENV:VPDF.config");
303 /* move to objects and make a snapshot */
305 DoMethod(obj, MUIM_VPDFSettings_ToObjects);
306 memcpy(attributes_old, attributes, sizeof(attributes));
308 setWrite("ENVARC:VPDF.config");
311 return (ULONG)obj;
314 DEFGET
316 if (msg->opg_AttrID > MUIA_VPDFSettings_First && msg->opg_AttrID < MUIA_VPDFSettings_Last)
318 int id = msg->opg_AttrID - MUIA_VPDFSettings_First;
319 switch (attributes[id].etype)
321 case ETYPE_NUMERIC:
322 *(ULONG*)msg->opg_Storage = (ULONG)attributes[id].value;
323 break;
324 case ETYPE_STRING:
325 *(ULONG*)msg->opg_Storage = (ULONG)attributes[id].svalue;
326 break;
328 return TRUE;
331 return DOSUPER;
335 DEFSET
337 //GETDATA;
339 struct TagItem *tag, *_tags = INITTAGS;
340 while ((tag = NextTagItem(&_tags)))
342 if (tag->ti_Tag > MUIA_VPDFSettings_First && tag->ti_Tag < MUIA_VPDFSettings_Last)
344 int id = tag->ti_Tag - MUIA_VPDFSettings_First;
346 switch (attributes[id].etype)
348 case ETYPE_NUMERIC:
349 attributes[id].value = tag->ti_Data;
350 break;
351 case ETYPE_STRING:
352 stccpy(attributes[id].svalue, (char*)tag->ti_Data, sizeof(attributes[id].svalue));
353 break;
356 if (attributes[id].object != NULL)
357 nnset(attributes[id].object, attributes[id].attribute, tag->ti_Data);
361 return DOSUPER;
365 DEFMMETHOD(VPDFSettings_FromObjects)
367 GETDATA;
368 int i;
370 data->groupsmodified = 0;
372 for(i=MUIA_VPDFSettings_First+1; i<MUIA_VPDFSettings_Last; i++)
374 int id = i - MUIA_VPDFSettings_First;
375 if (attributes[id].object != NULL)
377 switch(attributes[id].etype)
379 case ETYPE_NUMERIC:
380 attributes[id].value = xget(attributes[id].object, attributes[id].attribute);
381 if (attributes_old[id].value != attributes[id].value)
382 data->groupsmodified |= attributes[id].group;
383 break;
384 case ETYPE_STRING:
385 stccpy(attributes[id].svalue, (char*)xget(attributes[id].object, attributes[id].attribute), sizeof(attributes[id].svalue));
386 if (strcmp(attributes_old[id].svalue, attributes[id].svalue))
387 data->groupsmodified |= attributes[id].group;
388 break;
393 return data->groupsmodified;
396 DEFMMETHOD(VPDFSettings_ToObjects)
398 GETDATA;
399 int i;
400 int modified = 0;
402 for(i=MUIA_VPDFSettings_First+1; i<MUIA_VPDFSettings_Last; i++)
404 int id = i - MUIA_VPDFSettings_First;
405 if (attributes[id].object != NULL) /* always set. no matter what old state looked like. snapshot might be invalid */
407 switch(attributes[id].etype)
409 case ETYPE_NUMERIC:
410 nnset(attributes[id].object, attributes[id].attribute, attributes[id].value);
411 break;
412 case ETYPE_STRING:
413 nnset(attributes[id].object, attributes[id].attribute, attributes[id].svalue);
414 break;
418 switch(attributes[id].etype)
420 case ETYPE_NUMERIC:
421 if (attributes[id].value != attributes_old[id].value)
422 modified |= attributes[id].group;
423 break;
424 case ETYPE_STRING:
425 if (strcmp(attributes[id].svalue, attributes_old[id].svalue))
426 modified |= attributes[id].group;
427 break;
431 data->groupsmodified = 0;
433 return modified;
436 DEFMMETHOD(VPDFSettings_Use)
438 unsigned int modified = DoMethod(obj, MUIM_VPDFSettings_FromObjects);
439 setUse();
440 memcpy(attributes_old, attributes, sizeof(attributes));
441 return modified;
444 DEFMMETHOD(VPDFSettings_Save)
446 unsigned int modified = DoMethod(obj, MUIM_VPDFSettings_FromObjects);
447 setUse();
448 setSave();
449 memcpy(attributes_old, attributes, sizeof(attributes));
450 return modified;
453 DEFMMETHOD(VPDFSettings_Cancel)
455 unsigned int modified;
456 memcpy(attributes, attributes_old, sizeof(attributes));
457 return DoMethod(obj, MUIM_VPDFSettings_ToObjects);
460 BEGINMTABLE
461 DECNEW
462 DECGET
463 DECSET
464 DECMMETHOD(VPDFSettings_FromObjects)
465 DECMMETHOD(VPDFSettings_ToObjects)
466 DECMMETHOD(VPDFSettings_Use)
467 DECMMETHOD(VPDFSettings_Save)
468 DECMMETHOD(VPDFSettings_Cancel)
469 ENDMTABLE
471 DECSUBCLASS_NC(MUIC_Group, VPDFSettingsClass)