Imported from antiword-0.30.tar.gz.
[antiword.git] / options.c
blobb0dc2edfd338ba76082e3dee19fc88816f818804
1 /*
2 * options.c
3 * Copyright (C) 1998,1999 A.J. van Os
5 * Description:
6 * Read and write the options
7 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <limits.h>
13 #if defined(__riscos)
14 #include "wimpt.h"
15 #else
16 #include <stdlib.h>
17 #include <unistd.h>
18 #endif /* __riscos */
19 #include "antiword.h"
21 #define PARAGRAPH_BREAK "set paragraph_break=%d"
22 #define AUTOFILETYPE "set autofiletype_allowed=%d"
23 #define USE_OUTLINEFONTS "set use_outlinefonts=%d"
24 #define HIDE_HIDDEN_TEXT "set hide_hidden_text=%d"
26 /* Current values for options */
27 static options_type tOptionsCurr;
28 #if defined(__riscos)
29 /* Temporary values for options */
30 static options_type tOptionsTemp;
31 #else
32 static struct papersize_tag {
33 char szName[16];
34 int iWidth; /* In points */
35 int iHeight; /* In points */
36 } atPaperSizes[] = {
37 { "10x14", 720, 1008 },
38 { "a3", 842, 1191 },
39 { "a4", 595, 842 },
40 { "a5", 420, 595 },
41 { "b4", 729, 1032 },
42 { "b5", 516, 729 },
43 { "executive", 540, 720 },
44 { "folio", 612, 936 },
45 { "legal", 612, 1008 },
46 { "letter", 612, 792 },
47 { "note", 540, 720 },
48 { "quarto", 610, 780 },
49 { "statement", 396, 612 },
50 { "tabloid", 792, 1224 },
51 { "", -1, -1 },
53 #endif /* __riscos */
54 /* Default values for options */
55 static const options_type tOptionsDefault = {
56 DEFAULT_SCREEN_WIDTH,
57 TRUE,
58 #if defined(__riscos)
59 TRUE,
60 #else
61 FALSE,
62 #endif /* __riscos */
63 TRUE,
64 TRUE,
65 INT_MAX,
70 * iReadOptions - read options
72 * returns: -1: error
73 * 0: help
74 * >0: index first file argument
76 int
77 iReadOptions(int argc, char **argv)
79 #if !defined(__riscos)
80 extern char *optarg;
81 extern int optind, optopt;
82 struct papersize_tag *pPaperSize;
83 char *pcChar, *szTmp;
84 int iChar;
85 #endif /* __riscos */
86 FILE *pFile;
87 int iTmp;
88 char szLine[81];
90 DBG_MSG("iReadOptions");
92 /* Defaults */
93 tOptionsCurr = tOptionsDefault;
95 /* Environment */
96 #if !defined(__riscos)
97 szTmp = getenv("COLUMNS");
98 if (szTmp != NULL) {
99 DBG_MSG(szTmp);
100 iTmp = (int)strtol(szTmp, &pcChar, 10);
101 if (pcChar == '\0') {
102 iTmp -= 4; /* This is the edge */
103 if (iTmp < MIN_SCREEN_WIDTH) {
104 iTmp = MIN_SCREEN_WIDTH;
105 } else if (iTmp > MAX_SCREEN_WIDTH) {
106 iTmp = MAX_SCREEN_WIDTH;
108 tOptionsCurr.iParagraphBreak = iTmp;
109 DBG_DEC(tOptionsCurr.iParagraphBreak);
112 #endif /* __riscos */
114 /* Choices file */
115 pFile = fopen(OPTIONS_FILE_R, "r");
116 DBG_MSG_C(pFile == NULL, "Choices file not found");
117 DBG_HEX_C(pFile != NULL, pFile);
118 if (pFile != NULL) {
119 while (fgets(szLine, sizeof(szLine), pFile) != NULL) {
120 DBG_MSG(szLine);
121 if (szLine[0] == OPTIONS_COMMENT_CHAR) {
122 continue;
124 if (sscanf(szLine, PARAGRAPH_BREAK, &iTmp) == 1 &&
125 (iTmp == 0 ||
126 (iTmp >= MIN_SCREEN_WIDTH &&
127 iTmp <= MAX_SCREEN_WIDTH))) {
128 tOptionsCurr.iParagraphBreak = iTmp;
129 DBG_DEC(tOptionsCurr.iParagraphBreak);
130 } else if (sscanf(szLine, AUTOFILETYPE, &iTmp)
131 == 1) {
132 tOptionsCurr.bAutofiletypeAllowed =
133 iTmp != 0;
134 DBG_DEC(tOptionsCurr.bAutofiletypeAllowed);
135 } else if (sscanf(szLine, USE_OUTLINEFONTS, &iTmp)
136 == 1) {
137 tOptionsCurr.bUseOutlineFonts = iTmp != 0;
138 DBG_DEC(tOptionsCurr.bUseOutlineFonts);
139 } else if (sscanf(szLine, HIDE_HIDDEN_TEXT, &iTmp)
140 == 1) {
141 tOptionsCurr.bHideHiddenText = iTmp != 0;
142 DBG_DEC(tOptionsCurr.bHideHiddenText);
145 (void)fclose(pFile);
148 /* Command line */
149 #if defined(__riscos)
150 return 1;
151 #else
152 while ((iChar = getopt(argc, argv, "cghp:stw:")) != -1) {
153 switch (iChar) {
154 case 'c':
155 tOptionsCurr.bUseColour = TRUE;
156 break;
157 case 'g':
158 tOptionsCurr.bUseColour = FALSE;
159 break;
160 case 'h':
161 return 0;
162 case 'p':
163 tOptionsCurr.bUseOutlineFonts = TRUE;
164 for (pPaperSize = atPaperSizes;
165 pPaperSize->szName[0] != '\0';
166 pPaperSize++) {
167 if (StrEq(pPaperSize->szName, optarg)) {
168 tOptionsCurr.iPageHeight =
169 iMilliPoints2DrawUnits(
170 pPaperSize->iHeight * 1000);
171 DBG_DEC(pPaperSize->iHeight);
172 DBG_DEC(tOptionsCurr.iPageHeight);
173 tOptionsCurr.iParagraphBreak =
174 iMilliPoints2Char(
175 pPaperSize->iWidth * 1000 -
176 iDrawUnits2MilliPoints(
177 PS_LEFT_MARGIN +
178 PS_RIGHT_MARGIN));
179 DBG_DEC(pPaperSize->iWidth);
180 DBG_DEC(tOptionsCurr.iParagraphBreak);
183 case 's':
184 tOptionsCurr.bHideHiddenText = FALSE;
185 break;
186 case 't':
187 tOptionsCurr.bUseOutlineFonts = FALSE;
188 break;
189 case 'w':
190 iTmp = (int)strtol(optarg, &pcChar, 10);
191 if (*pcChar == '\0' &&
192 (iTmp == 0 ||
193 (iTmp >= MIN_SCREEN_WIDTH &&
194 iTmp <= MAX_SCREEN_WIDTH))) {
195 tOptionsCurr.iParagraphBreak = iTmp;
196 DBG_DEC(tOptionsCurr.iParagraphBreak);
198 break;
199 default:
200 return -1;
203 return optind;
204 #endif /* __riscos */
205 } /* end of iReadOptions */
208 * vGetOptions - get a copy of the current option values
210 void
211 vGetOptions(options_type *ptOptions)
213 fail(ptOptions == NULL);
215 *ptOptions = tOptionsCurr;
216 } /* end of vGetOptions */
218 #if defined(__riscos)
220 * vWriteOptions - write the current options to the Options file
222 static void
223 vWriteOptions(void)
225 FILE *pFile;
226 char *szOptionsFile;
228 DBG_MSG("vWriteOptions");
230 szOptionsFile = getenv(OPTIONS_FILE_W);
231 if (szOptionsFile == NULL) {
232 werr(0, "Warning: Name of the Choices file not found");
233 return;
235 if (!bMakeDirectory(szOptionsFile)) {
236 werr(0,
237 "Warning: I can't make a directory for the Choices file");
238 return;
240 pFile = fopen(szOptionsFile, "w");
241 if (pFile == NULL) {
242 werr(0, "Warning: I can't write the Choices file");
243 return;
245 (void)fprintf(pFile, PARAGRAPH_BREAK"\n",
246 tOptionsCurr.iParagraphBreak);
247 (void)fprintf(pFile, AUTOFILETYPE"\n",
248 tOptionsCurr.bAutofiletypeAllowed);
249 (void)fprintf(pFile, USE_OUTLINEFONTS"\n",
250 tOptionsCurr.bUseOutlineFonts);
251 (void)fprintf(pFile, HIDE_HIDDEN_TEXT"\n",
252 tOptionsCurr.bHideHiddenText);
253 (void)fclose(pFile);
254 } /* end of vWriteOptions */
257 * vChoicesOpenAction - action to be taken when the Choices window opens
259 void
260 vChoicesOpenAction(wimp_w tWindow)
262 DBG_MSG("vChoicesOpenAction");
264 tOptionsTemp = tOptionsCurr;
265 if (tOptionsTemp.iParagraphBreak == 0) {
266 vUpdateRadioButton(tWindow, CHOICES_BREAK_BUTTON, FALSE);
267 vUpdateRadioButton(tWindow, CHOICES_NO_BREAK_BUTTON, TRUE);
268 vUpdateWriteableNumber(tWindow, CHOICES_BREAK_WRITEABLE,
269 DEFAULT_SCREEN_WIDTH);
270 } else {
271 vUpdateRadioButton(tWindow, CHOICES_BREAK_BUTTON, TRUE);
272 vUpdateRadioButton(tWindow, CHOICES_NO_BREAK_BUTTON, FALSE);
273 vUpdateWriteableNumber(tWindow,
274 CHOICES_BREAK_WRITEABLE,
275 tOptionsTemp.iParagraphBreak);
277 vUpdateRadioButton(tWindow, CHOICES_AUTOFILETYPE_BUTTON,
278 tOptionsTemp.bAutofiletypeAllowed);
279 vUpdateRadioButton(tWindow, CHOICES_HIDDEN_TEXT_BUTTON,
280 tOptionsTemp.bHideHiddenText);
281 if (tOptionsTemp.bUseOutlineFonts) {
282 vUpdateRadioButton(tWindow,
283 CHOICES_OUTLINEFONT_BUTTON, TRUE);
284 vUpdateRadioButton(tWindow,
285 CHOICES_SYSTEMFONT_BUTTON, FALSE);
286 } else {
287 vUpdateRadioButton(tWindow,
288 CHOICES_OUTLINEFONT_BUTTON, FALSE);
289 vUpdateRadioButton(tWindow,
290 CHOICES_SYSTEMFONT_BUTTON, TRUE);
292 } /* end of vChoicesOpenAction */
295 * vDefaultButtonAction - action when the default button is clicked
297 static void
298 vDefaultButtonAction(wimp_w tWindow)
300 DBG_MSG("vDefaultButtonAction");
302 tOptionsTemp = tOptionsDefault;
303 vUpdateRadioButton(tWindow, CHOICES_BREAK_BUTTON, TRUE);
304 vUpdateRadioButton(tWindow, CHOICES_NO_BREAK_BUTTON, FALSE);
305 vUpdateWriteableNumber(tWindow, CHOICES_BREAK_WRITEABLE,
306 tOptionsTemp.iParagraphBreak);
307 vUpdateRadioButton(tWindow, CHOICES_AUTOFILETYPE_BUTTON,
308 tOptionsTemp.bAutofiletypeAllowed);
309 vUpdateRadioButton(tWindow, CHOICES_HIDDEN_TEXT_BUTTON,
310 tOptionsTemp.bHideHiddenText);
311 vUpdateRadioButton(tWindow, CHOICES_OUTLINEFONT_BUTTON, TRUE);
312 vUpdateRadioButton(tWindow, CHOICES_SYSTEMFONT_BUTTON, FALSE);
313 } /* end of vDefaultButtonAction */
316 * vApplyButtonAction - action to be taken when the OK button is clicked
318 static void
319 vApplyButtonAction(void)
321 DBG_MSG("vApplyButtonAction");
323 tOptionsCurr = tOptionsTemp;
324 } /* end of vApplyButtonAction */
327 * vSaveButtonAction - action to be taken when the save button is clicked
329 static void
330 vSaveButtonAction(void)
332 DBG_MSG("vSaveButtonAction");
334 vApplyButtonAction();
335 vWriteOptions();
336 } /* end of vSaveButtonAction */
339 * vSetParagraphBreak - set the paragraph break to the given number
341 static void
342 vSetParagraphBreak(wimp_w tWindow, int iNumber)
344 tOptionsTemp.iParagraphBreak = iNumber;
345 if (tOptionsTemp.iParagraphBreak == 0) {
346 return;
348 vUpdateWriteableNumber(tWindow,
349 CHOICES_BREAK_WRITEABLE,
350 tOptionsTemp.iParagraphBreak);
351 } /* end of vSetParagraphBreak */
354 * vChangeParagraphBreak - change the paragraph break with the given number
356 static void
357 vChangeParagraphBreak(wimp_w tWindow, int iNumber)
359 int iTmp;
361 iTmp = tOptionsTemp.iParagraphBreak + iNumber;
362 if (iTmp < MIN_SCREEN_WIDTH || iTmp > MAX_SCREEN_WIDTH) {
363 return;
365 tOptionsTemp.iParagraphBreak = iTmp;
366 vUpdateWriteableNumber(tWindow,
367 CHOICES_BREAK_WRITEABLE,
368 tOptionsTemp.iParagraphBreak);
369 } /* end of vChangeParagraphBreak */
372 * vChangeAutofiletype - invert the permission to autofiletype
374 static void
375 vChangeAutofiletype(wimp_w tWindow)
377 tOptionsTemp.bAutofiletypeAllowed =
378 !tOptionsTemp.bAutofiletypeAllowed;
379 vUpdateRadioButton(tWindow,
380 CHOICES_AUTOFILETYPE_BUTTON,
381 tOptionsTemp.bAutofiletypeAllowed);
382 } /* end of vChangeAutofiletype */
385 * vChangeHiddenText - invert the hide/show hidden text
387 static void
388 vChangeHiddenText(wimp_w tWindow)
390 tOptionsTemp.bHideHiddenText = !tOptionsTemp.bHideHiddenText;
391 vUpdateRadioButton(tWindow,
392 CHOICES_HIDDEN_TEXT_BUTTON,
393 tOptionsTemp.bHideHiddenText);
394 } /* end of vChangeHiddenText */
397 * vUseOutlineFonts - use outline fonts
399 static void
400 vUseOutlineFonts(BOOL bUseOutlineFonts)
402 tOptionsTemp.bUseOutlineFonts = bUseOutlineFonts;
403 } /* end of vUseOutlineFonts */
406 * bChoicesMouseClick - handle a mouse click in the Choices window
408 void
409 vChoicesMouseClick(wimp_mousestr *m)
411 wimp_i tAction;
412 BOOL bCloseWindow, bLeft, bRight;
414 bLeft = (m->bbits & wimp_BLEFT) == wimp_BLEFT;
415 bRight = (m->bbits & wimp_BRIGHT) == wimp_BRIGHT;
416 if (!bLeft && !bRight) {
417 DBG_HEX(m->bbits);
418 return;
421 /* Which action should be taken */
422 if (bRight && m->i == CHOICES_BREAK_UP_BUTTON) {
423 tAction = CHOICES_BREAK_DOWN_BUTTON;
424 } else if (bRight && m->i == CHOICES_BREAK_DOWN_BUTTON) {
425 tAction = CHOICES_BREAK_UP_BUTTON;
426 } else {
427 tAction = m->i;
430 /* Actions */
431 bCloseWindow = FALSE;
432 switch (tAction) {
433 case CHOICES_DEFAULT_BUTTON:
434 vDefaultButtonAction(m->w);
435 break;
436 case CHOICES_SAVE_BUTTON:
437 vSaveButtonAction();
438 break;
439 case CHOICES_CANCEL_BUTTON:
440 bCloseWindow = TRUE;
441 break;
442 case CHOICES_APPLY_BUTTON:
443 vApplyButtonAction();
444 bCloseWindow = TRUE;
445 break;
446 case CHOICES_BREAK_BUTTON:
447 vSetParagraphBreak(m->w, DEFAULT_SCREEN_WIDTH);
448 break;
449 case CHOICES_BREAK_UP_BUTTON:
450 vChangeParagraphBreak(m->w, 1);
451 break;
452 case CHOICES_BREAK_DOWN_BUTTON:
453 vChangeParagraphBreak(m->w, -1);
454 break;
455 case CHOICES_NO_BREAK_BUTTON:
456 vSetParagraphBreak(m->w, 0);
457 break;
458 case CHOICES_AUTOFILETYPE_BUTTON:
459 vChangeAutofiletype(m->w);
460 break;
461 case CHOICES_HIDDEN_TEXT_BUTTON:
462 vChangeHiddenText(m->w);
463 break;
464 case CHOICES_OUTLINEFONT_BUTTON:
465 vUseOutlineFonts(TRUE);
466 break;
467 case CHOICES_SYSTEMFONT_BUTTON:
468 vUseOutlineFonts(FALSE);
469 break;
470 default:
471 DBG_DEC(m->i);
472 break;
474 if (bCloseWindow) {
475 wimpt_noerr(wimp_close_wind(m->w));
477 } /* end of vChoicesMouseClick */
479 void
480 vChoicesKeyPressed(wimp_caretstr *c)
482 wimp_icon tIcon;
483 char *pcChar;
484 int iNumber;
486 DBG_MSG("vChoicesKeyPressed");
488 fail(c == NULL);
490 wimpt_noerr(wimp_get_icon_info(c->w, c->i, &tIcon));
491 if ((tIcon.flags & (wimp_ITEXT|wimp_INDIRECT)) !=
492 (wimp_ITEXT|wimp_INDIRECT)) {
493 werr(1, "Icon %d must be indirected text", (int)c->i);
494 return;
496 iNumber = (int)strtol(tIcon.data.indirecttext.buffer, &pcChar, 10);
497 if (*pcChar != '\0') {
498 DBG_DEC(*pcChar);
499 iNumber = DEFAULT_SCREEN_WIDTH;
500 } else if (iNumber < MIN_SCREEN_WIDTH) {
501 iNumber = MIN_SCREEN_WIDTH;
502 } else if (iNumber > MAX_SCREEN_WIDTH) {
503 iNumber = MAX_SCREEN_WIDTH;
505 vSetParagraphBreak(c->w, iNumber);
506 } /* end of vChoicesKeyPressed */
507 #endif /* __riscos */