Reset parser in grace_set_project().
[grace.git] / src / project_ui.c
blobe038b5dcec3ab36e83b50fc0bb85d2abc0c1011e
1 /*
2 * Grace - GRaphing, Advanced Computation and Exploration of data
3 *
4 * Home page: http://plasma-gate.weizmann.ac.il/Grace/
5 *
6 * Copyright (c) 1996-2004 Grace Development Team
7 *
8 * Maintained by Evgeny Stambulchik
9 *
11 * All Rights Reserved
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 /* Project UI */
30 #include <stdlib.h>
32 #include <Xm/Text.h>
34 #include "explorer.h"
35 #include "utils.h"
36 #include "protos.h"
38 static void wrap_year_cb(Widget but, int onoff, void *data)
40 Widget wrap_year = (Widget) data;
42 SetSensitive(wrap_year, onoff);
45 #define PAGE_UNITS_PP 0
46 #define PAGE_UNITS_IN 1
47 #define PAGE_UNITS_CM 2
49 static void do_format_toggle(OptionStructure *opt, int value, void *data)
51 ProjectUI *ui = (ProjectUI *) data;
52 int orientation;
53 double px, py;
54 int page_units;
55 char buf[32];
57 if (value == PAGE_FORMAT_CUSTOM) {
58 SetSensitive(ui->page_x, TRUE);
59 SetSensitive(ui->page_y, TRUE);
60 SetSensitive(ui->page_orient->menu, FALSE);
61 return;
62 } else {
63 SetSensitive(ui->page_x, FALSE);
64 SetSensitive(ui->page_y, FALSE);
65 SetSensitive(ui->page_orient->menu, TRUE);
68 switch (value) {
69 case PAGE_FORMAT_USLETTER:
70 px = 612.0;
71 py = 792.0;
72 break;
73 case PAGE_FORMAT_A4:
74 px = 595.0;
75 py = 842.0;
76 break;
77 default:
78 return;
82 page_units = GetOptionChoice(ui->page_size_unit);
83 orientation = GetOptionChoice(ui->page_orient);
85 switch (page_units) {
86 case PAGE_UNITS_IN:
87 px /= 72.0;
88 py /= 72.0;
89 break;
90 case PAGE_UNITS_CM:
91 px /= 72.0/CM_PER_INCH;
92 py /= 72.0/CM_PER_INCH;
93 break;
96 if ((orientation == PAGE_ORIENT_LANDSCAPE && px > py) ||
97 (orientation == PAGE_ORIENT_PORTRAIT && px < py) ) {
98 sprintf (buf, "%.2f", px);
99 xv_setstr(ui->page_x, buf);
100 sprintf (buf, "%.2f", py);
101 xv_setstr(ui->page_y, buf);
102 } else {
103 sprintf (buf, "%.2f", py);
104 xv_setstr(ui->page_x, buf);
105 sprintf (buf, "%.2f", px);
106 xv_setstr(ui->page_y, buf);
110 static void do_orient_toggle(OptionStructure *opt, int value, void *data)
112 ProjectUI *ui = (ProjectUI *) data;
113 double px, py;
114 char buf[32];
115 int orientation = value;
117 if (xv_evalexpr(ui->page_x, &px) != RETURN_SUCCESS ||
118 xv_evalexpr(ui->page_y, &py) != RETURN_SUCCESS ) {
119 errmsg("Invalid page dimension(s)");
120 return;
123 if ((orientation == PAGE_ORIENT_LANDSCAPE && px < py) ||
124 (orientation == PAGE_ORIENT_PORTRAIT && px > py) ) {
125 sprintf (buf, "%.2f", py);
126 xv_setstr(ui->page_x, buf);
127 sprintf (buf, "%.2f", px);
128 xv_setstr(ui->page_y, buf);
132 static void do_units_toggle(OptionStructure *opt, int value, void *data)
134 ProjectUI *ui = (ProjectUI *) data;
135 char buf[32];
136 double page_x, page_y;
137 int page_units = value;
139 if (xv_evalexpr(ui->page_x, &page_x) != RETURN_SUCCESS ||
140 xv_evalexpr(ui->page_y, &page_y) != RETURN_SUCCESS ) {
141 errmsg("Invalid page dimension(s)");
142 return;
145 if (ui->current_page_units == page_units) {
146 return;
149 switch (ui->current_page_units) {
150 case PAGE_UNITS_IN:
151 page_x *= 72.0;
152 page_y *= 72.0;
153 break;
154 case PAGE_UNITS_CM:
155 page_x *= 72.0/CM_PER_INCH;
156 page_y *= 72.0/CM_PER_INCH;
157 break;
160 switch (page_units) {
161 case PAGE_UNITS_IN:
162 page_x /= 72.0;
163 page_y /= 72.0;
164 break;
165 case PAGE_UNITS_CM:
166 page_x /= 72.0/CM_PER_INCH;
167 page_y /= 72.0/CM_PER_INCH;
168 break;
171 ui->current_page_units = page_units;
173 sprintf (buf, "%.2f", page_x);
174 xv_setstr(ui->page_x, buf);
175 sprintf (buf, "%.2f", page_y);
176 xv_setstr(ui->page_y, buf);
179 ProjectUI *create_project_ui(ExplorerUI *eui)
181 ProjectUI *ui;
182 Widget form, fr, rc, rc1;
184 form = CreateVContainer(eui->scrolled_window);
185 AddHelpCB(form, "doc/UsersGuide.html#project-properties");
187 ui = xmalloc(sizeof(ProjectUI));
188 ui->current_page_units = PAGE_UNITS_PP;
190 fr = CreateFrame(form, NULL);
191 rc = CreateVContainer(fr);
192 ui->description = CreateScrollTextItem2(rc, 5, "Project description:");
193 AddTextItemCB(ui->description, titem_explorer_cb, eui);
194 ui->sformat = CreateTextItem2(rc, 15, "Data format:");
195 AddTextItemCB(ui->sformat, titem_explorer_cb, eui);
197 fr = CreateFrame(form, "Page dimensions");
198 rc1 = CreateVContainer(fr);
200 rc = CreateHContainer(rc1);
201 ui->page_orient = CreatePaperOrientationChoice(rc, "Orientation:");
202 AddOptionChoiceCB(ui->page_orient, do_orient_toggle, ui);
203 AddOptionChoiceCB(ui->page_orient, oc_explorer_cb, eui);
205 ui->page_format = CreatePaperFormatChoice(rc, "Size:");
206 AddOptionChoiceCB(ui->page_format, do_format_toggle, ui);
207 AddOptionChoiceCB(ui->page_format, oc_explorer_cb, eui);
209 rc = CreateHContainer(rc1);
210 ui->page_x = CreateTextItem2(rc, 7, "Dimensions:");
211 AddTextItemCB(ui->page_x, titem_explorer_cb, eui);
212 ui->page_y = CreateTextItem2(rc, 7, "x ");
213 AddTextItemCB(ui->page_y, titem_explorer_cb, eui);
214 ui->page_size_unit = CreateOptionChoiceVA(rc, " ",
215 "pp", PAGE_UNITS_PP,
216 "in", PAGE_UNITS_IN,
217 "cm", PAGE_UNITS_CM,
218 NULL);
219 SetOptionChoice(ui->page_size_unit, ui->current_page_units);
220 AddOptionChoiceCB(ui->page_size_unit, do_units_toggle, ui);
223 fr = CreateFrame(form, "Page background");
224 AddDialogFormChild(form, fr);
225 rc = CreateHContainer(fr);
226 ui->bg_color = CreateColorChoice(rc, "Color:");
227 AddOptionChoiceCB(ui->bg_color, oc_explorer_cb, eui);
228 ui->bg_fill = CreateToggleButton(rc, "Fill");
229 AddToggleButtonCB(ui->bg_fill, tb_explorer_cb, eui);
231 fr = CreateFrame(form, "Scaling factors");
232 AddDialogFormChild(form, fr);
233 rc = CreateVContainer(fr);
234 ui->fsize_scale = CreateSpinChoice(rc, "Font size:", 5,
235 SPIN_TYPE_FLOAT, 0.0, 1.0, 0.005);
236 AddSpinChoiceCB(ui->fsize_scale, sp_explorer_cb, eui);
237 ui->lwidth_scale = CreateSpinChoice(rc, "Line width:", 6,
238 SPIN_TYPE_FLOAT, 0.0, 1.0, 0.0005);
239 AddSpinChoiceCB(ui->lwidth_scale, sp_explorer_cb, eui);
241 fr = CreateFrame(form, "Dates");
242 rc1 = CreateVContainer(fr);
243 ui->refdate = CreateTextItem2(rc1, 20, "Reference date:");
244 AddTextItemCB(ui->refdate, titem_explorer_cb, eui);
245 rc = CreateHContainer(rc1);
246 ui->two_digits_years = CreateToggleButton(rc, "Two-digit year span");
247 AddToggleButtonCB(ui->two_digits_years, tb_explorer_cb, eui);
248 ui->wrap_year = CreateTextItem2(rc, 4, "Wrap year:");
249 AddTextItemCB(ui->wrap_year, titem_explorer_cb, eui);
250 AddToggleButtonCB(ui->two_digits_years, wrap_year_cb, ui->wrap_year);
252 ui->top = form;
254 return ui;
257 void update_project_ui(ProjectUI *ui, Quark *q)
259 Project *pr = project_get_data(q);
260 if (pr) {
261 int y, m, d, h, mm, sec;
262 char date_string[64], wrap_year_string[64], buf[32];
263 double factor;
264 int format;
266 xv_setstr(ui->sformat, project_get_sformat(q));
267 xv_setstr(ui->description, project_get_description(q));
269 switch (GetOptionChoice(ui->page_size_unit)) {
270 case PAGE_UNITS_IN:
271 factor = 1.0/72.0;
272 break;
273 case PAGE_UNITS_CM:
274 factor = CM_PER_INCH/72.0;
275 break;
276 default:
277 factor = 1.0;
280 sprintf (buf, "%.2f", factor*pr->page_wpp);
281 xv_setstr(ui->page_x, buf);
282 sprintf (buf, "%.2f", factor*pr->page_hpp);
283 xv_setstr(ui->page_y, buf);
285 if ((pr->page_wpp == 612 && pr->page_hpp == 792) ||
286 (pr->page_hpp == 612 && pr->page_wpp == 792)) {
287 format = PAGE_FORMAT_USLETTER;
288 } else
289 if ((pr->page_wpp == 595 && pr->page_hpp == 842) ||
290 (pr->page_hpp == 595 && pr->page_wpp == 842)) {
291 format = PAGE_FORMAT_A4;
292 } else {
293 format = PAGE_FORMAT_CUSTOM;
295 if (format == PAGE_FORMAT_CUSTOM) {
296 SetSensitive(ui->page_x, TRUE);
297 SetSensitive(ui->page_y, TRUE);
298 SetSensitive(ui->page_orient->menu, FALSE);
299 return;
300 } else {
301 SetSensitive(ui->page_x, FALSE);
302 SetSensitive(ui->page_y, FALSE);
303 SetSensitive(ui->page_orient->menu, TRUE);
305 SetOptionChoice(ui->page_format, format);
307 if (pr->page_wpp > pr->page_hpp) {
308 SetOptionChoice(ui->page_orient, PAGE_ORIENT_LANDSCAPE);
309 } else {
310 SetOptionChoice(ui->page_orient, PAGE_ORIENT_PORTRAIT);
313 SetOptionChoice(ui->bg_color, pr->bgcolor);
314 SetToggleButtonState(ui->bg_fill, pr->bgfill);
316 SetSpinChoice(ui->fsize_scale, pr->fscale);
317 SetSpinChoice(ui->lwidth_scale, pr->lscale);
319 jul_to_cal_and_time(q, 0.0, ROUND_SECOND, &y, &m, &d, &h, &mm, &sec);
320 sprintf(date_string, "%d-%02d-%02d %02d:%02d:%02d",
321 y, m, d, h, mm, sec);
322 xv_setstr(ui->refdate, date_string);
323 SetToggleButtonState(ui->two_digits_years, pr->two_digits_years);
324 sprintf(wrap_year_string, "%04d", pr->wrap_year);
325 xv_setstr(ui->wrap_year, wrap_year_string);
326 SetSensitive(ui->wrap_year, pr->two_digits_years ? TRUE:FALSE);
330 int set_project_data(ProjectUI *ui, Quark *q, void *caller)
332 Project *pr = project_get_data(q);
333 int retval = RETURN_SUCCESS;
335 if (ui && pr) {
336 double jul;
338 if (!caller || caller == ui->sformat) {
339 project_set_sformat(q, xv_getstr(ui->sformat));
341 if (!caller || caller == ui->description) {
342 char *s = XmTextGetString(ui->description);
343 project_set_description(q, s);
344 XtFree(s);
347 if (caller == ui->page_orient) {
348 int wpp, hpp;
349 int orientation = GetOptionChoice(ui->page_orient);
350 project_get_page_dimensions(q, &wpp, &hpp);
351 if ((orientation == PAGE_ORIENT_LANDSCAPE && wpp < hpp) ||
352 (orientation == PAGE_ORIENT_PORTRAIT && wpp > hpp)) {
353 Grace *grace = grace_from_quark(q);
354 set_page_dimensions(grace, hpp, wpp, TRUE);
357 if (caller == ui->page_format) {
358 int wpp, hpp;
359 int orientation = GetOptionChoice(ui->page_orient);
360 int format = GetOptionChoice(ui->page_format);
361 Grace *grace = grace_from_quark(q);
363 switch (format) {
364 case PAGE_FORMAT_USLETTER:
365 wpp = 792.0;
366 hpp = 612.0;
367 break;
368 case PAGE_FORMAT_A4:
369 wpp = 842.0;
370 hpp = 595.0;
371 break;
372 default:
373 return RETURN_SUCCESS;
376 if (orientation == PAGE_ORIENT_PORTRAIT) {
377 iswap(&wpp, &hpp);
380 set_page_dimensions(grace, wpp, hpp, TRUE);
383 if (!caller || caller == ui->page_x || caller == ui->page_y) {
384 int page_units = GetOptionChoice(ui->page_size_unit);
385 double factor, page_x, page_y;
386 Grace *grace = grace_from_quark(q);
388 if (xv_evalexpr(ui->page_x, &page_x) != RETURN_SUCCESS ||
389 xv_evalexpr(ui->page_y, &page_y) != RETURN_SUCCESS) {
390 errmsg("Invalid page dimension(s)");
391 return RETURN_FAILURE;
394 switch (page_units) {
395 case PAGE_UNITS_IN:
396 factor = 72.0;
397 break;
398 case PAGE_UNITS_CM:
399 factor = 72.0/CM_PER_INCH;
400 break;
401 default:
402 factor = 1.0;
403 break;
406 page_x *= factor;
407 page_y *= factor;
409 set_page_dimensions(grace, (int) rint(page_x), (int) rint(page_y),
410 TRUE);
413 if (!caller || caller == ui->bg_color) {
414 pr->bgcolor = GetOptionChoice(ui->bg_color);
416 if (!caller || caller == ui->bg_fill) {
417 pr->bgfill = GetToggleButtonState(ui->bg_fill);
420 if (!caller || caller == ui->fsize_scale) {
421 pr->fscale = GetSpinChoice(ui->fsize_scale);
423 if (!caller || caller == ui->lwidth_scale) {
424 pr->lscale = GetSpinChoice(ui->lwidth_scale);
427 if (!caller || caller == ui->refdate) {
428 if (parse_date_or_number(q, xv_getstr(ui->refdate), TRUE, &jul) ==
429 RETURN_SUCCESS) {
430 pr->ref_date = jul;
431 } else {
432 errmsg("Invalid date");
433 retval = RETURN_FAILURE;
436 if (!caller || caller == ui->two_digits_years) {
437 pr->two_digits_years = GetToggleButtonState(ui->two_digits_years);
439 if (!caller || caller == ui->wrap_year) {
440 pr->wrap_year = atoi(xv_getstr(ui->wrap_year));
443 quark_dirtystate_set(q, TRUE);
446 return retval;