File: xxXTideRoot.cc

package info (click to toggle)
xtide 2.6.4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,996 kB
  • ctags: 2,617
  • sloc: cpp: 26,266; ansic: 8,105; makefile: 152; yacc: 113; sh: 54; lex: 54
file content (963 lines) | stat: -rw-r--r-- 26,721 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
// $Id: xxXTideRoot.cc,v 1.7 2003/02/04 21:26:48 flaterco Exp $
/*  xxXTideRoot  XTide "root" window

    Copyright (C) 1998  David Flater.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "xtide.hh"

static void
dismissCallback (Widget w, XtPointer client_data, XtPointer call_data) {
  ((xxXTideRoot*)client_data)->hide();
}

static void
applyCallback (Widget w, XtPointer client_data, XtPointer call_data) {
  ((xxXTideRoot*)client_data)->apply();
}

// This code is pretty much duplicated from xxAspect
int
xxXTideRoot::sancheckdouble (char *d) {
  Dstr num (d);
  if (num.length() > 0) {
    if (num.strchr ('\n') != -1 ||
           num.strchr ('\r') != -1 ||
           num.strchr (' ') != -1) {
      Dstr details ("Numbers aren't supposed to contain whitespace.  You entered '");
      details += num;
      details += "'.";
      barf (NOT_A_NUMBER, details, 0);
      return 0;
    } else {
      double temp;
      if (sscanf (num.aschar(), "%lf", &temp) != 1) {
	Dstr details ("The offending input was '");
	details += num;
	details += "'.";
	barf (NOT_A_NUMBER, details, 0);
        return 0;
      } else {
        if (temp <= 0.0) {
  	  Dstr details ("The offending input was '");
	  details += num;
	  details += "'.";
	  barf (NUMBER_RANGE_ERROR, details, 0);
          return 0;
        } else
  	  return 1;
      }
    }
  }
  return 1;
}

void
xxXTideRoot::apply (int saveflag) {

  // First sanity check, then apply if OK.

  // Sanity check all colors
  int a;
  for (a=0; a<numcolors; a++) {
    unsigned char r, g, b;
    Dstr color (colordiags[a]->val());
    if (color.length()) {
      if (!(myTideContext->colors->parseColor (color, r, g, b, 0)))
        return;
    }
  }
  // Sanity check date/time formats -- prevent nasty XML characters
#define datetimebarf { \
    Dstr details ("Please don't use nasty characters like \", >, and < \
in your date/time formats."); \
    barf (XMLPARSE, details, 0); \
    return; \
  }
  if (strchr (dfdiag->val(), '"') ||
      strchr (dfdiag->val(), '>') ||
      strchr (dfdiag->val(), '<'))
    datetimebarf;
  if (strchr (hfdiag->val(), '"') ||
      strchr (hfdiag->val(), '>') ||
      strchr (hfdiag->val(), '<'))
    datetimebarf;
  if (strchr (tfdiag->val(), '"') ||
      strchr (tfdiag->val(), '>') ||
      strchr (tfdiag->val(), '<'))
    datetimebarf;
  // Sanity check doubles.
  if (!(sancheckdouble (gadiag->val())))
    return;
  if (!(sancheckdouble (lwdiag->val())))
    return;

  // Apply changes.
  Settings newset;
  // Colors
  for (a=0; a<numcolors; a++) {
    Dstr color (colordiags[a]->val());
    if (color.length())
      newset.colors[a] = color;
  }
  // Toggles
  switch (elchoice->choice()) {
  case 0:
    newset.el = "y";
    break;
  case 1:
    newset.el = "n";
    break;
  case 2:
    break;
  default:
    assert (0);
  }
  switch (fechoice->choice()) {
  case 0:
    newset.fe = "y";
    break;
  case 1:
    newset.fe = "n";
    break;
  case 2:
    break;
  default:
    assert (0);
  }
  switch (nfchoice->choice()) {
  case 0:
    newset.nf = "y";
    break;
  case 1:
    newset.nf = "n";
    break;
  case 2:
    break;
  default:
    assert (0);
  }
  switch (nschoice->choice()) {
  case 0:
    newset.ns = "y";
    break;
  case 1:
    newset.ns = "n";
    break;
  case 2:
    break;
  default:
    assert (0);
  }
  switch (inchoice->choice()) {
  case 0:
    newset.in = "y";
    break;
  case 1:
    newset.in = "n";
    break;
  case 2:
    break;
  default:
    assert (0);
  }
  switch (tlchoice->choice()) {
  case 0:
    newset.tl = "y";
    break;
  case 1:
    newset.tl = "n";
    break;
  case 2:
    break;
  default:
    assert (0);
  }
  switch (zchoice->choice()) {
  case 0:
    newset.z = "y";
    break;
  case 1:
    newset.z = "n";
    break;
  case 2:
    break;
  default:
    assert (0);
  }

  switch (uchoice->choice()) {
  case 0:
    newset.u = "ft";
    break;
  case 1:
    newset.u = "m";
    break;
  case 2:
    newset.u = "x";
    break;
  case 3:
    break;
  default:
    assert (0);
  }

  switch (glchoice->choice()) {
  case 12:
    newset.gl_isnull = 0;
    newset.gl = 360.0;
    break;
  case 13:
    break;
  default:
    newset.gl_isnull = 0;
    newset.gl = (double)(glchoice->choice()) * 30.0 - 180.0;
    break;
  }

  // Date/time formats.
  {
    Dstr fmt (dfdiag->val());
    if (fmt.length())
      newset.df = fmt;
  }
  {
    Dstr fmt (hfdiag->val());
    if (fmt.length())
      newset.hf = fmt;
  }
  {
    Dstr fmt (tfdiag->val());
    if (fmt.length())
      newset.tf = fmt;
  }

  // Unsigned numbers.
  if (gwchoice->choice() != 1000000) {
    newset.gw_isnull = 0;
    newset.gw = gwchoice->choice();
  }
  if (ghchoice->choice() != 1000000) {
    newset.gh_isnull = 0;
    newset.gh = ghchoice->choice();
  }
  if (twchoice->choice() != 1000000) {
    newset.tw_isnull = 0;
    newset.tw = twchoice->choice();
  }
  if (thchoice->choice() != 1000000) {
    newset.th_isnull = 0;
    newset.th = thchoice->choice();
  }
  if (cwchoice->choice() != 1000000) {
    newset.cw_isnull = 0;
    newset.cw = cwchoice->choice();
  }

  // Doubles.
  {
    Dstr num (gadiag->val());
    if (num.length() > 0) {
      double d;
      assert (sscanf (num.aschar(), "%lf", &d) == 1);
      newset.ga_isnull = 0;
      newset.ga = d;
    }
  }
  {
    Dstr num (lwdiag->val());
    if (num.length() > 0) {
      double d;
      assert (sscanf (num.aschar(), "%lf", &d) == 1);
      newset.lw_isnull = 0;
      newset.lw = d;
    }
  }

  mycontext->change_settings (myTideContext, newset);
  global_redraw();

  if (saveflag)
    newset.save();
}

static void
saveCallback (Widget w, XtPointer client_data, XtPointer call_data) {
  xxXTideRoot *f = (xxXTideRoot *)client_data;
  f->apply (1);
}

static void
helpCallback (Widget w, XtPointer client_data, XtPointer call_data) {
  Dstr helpstring ("\
XTide Control Panel\n\
\n\
The Control Panel is used to change global XTide settings.  These settings\n\
take precedence over compiled-in defaults and X application defaults, but\n\
they are overridden by settings made on the command line.  Therefore, any\n\
settings that you make in the Control Panel will not have any visible effect\n\
if you have also made these settings on the command line.\n\
\n\
Some settings have dialog boxes for you to type in; others have pull-down\n\
menus or '+' and '-' controls.  In each case, there is a way to leave the\n\
field blank if you want to keep the inherited settings.  Either delete all\n\
text in the dialog box, or choose the \"(blank)\" option on the pull-down\n\
menu, or lay on the '-' button until the field reads \"(blank)\".\n\
\n\
The settings identified as \"default\" settings (widths, heights, and\n\
aspect ratio) will not affect existing windows.  Widths and heights will\n\
affect all new windows; aspect ratio will only affect new windows that\n\
are created from the location chooser.  (This is because the aspect is\n\
preserved from one tide window to any new windows that it spawns.)\n\
\n\
Use the Apply button to apply the new settings to the current XTide session\n\
without making them permanent.  Use Save to make them permanent.\n\
\n\
If necessary, resize the Control Panel to keep all of the settings visible.\n\
\n\
About colors:  When entering colors, use either standard X11 color names\n\
or 24-bit hex specs of the form rgb:hh/hh/hh.  Do not use more or less bits;\n\
it will not work.\n\
\n\
About date/time formats:  Please refer to the man page for strftime to see\n\
how these formats work.  Here is how to get XTide to use 24-hour time instead\n\
of AM/PM:\n\
   Hour format  %H\n\
   Time format  %H:%M %Z");
  xxXTideRoot *f = (xxXTideRoot *)client_data;
  f->newHelpBox (helpstring);
}

void RootCloseHandler (Widget w, XtPointer client_data,
			   XEvent *event, Boolean *continue_dispatch) {
  xxXTideRoot *f = (xxXTideRoot *)client_data;
  switch (event->type) {
  case ClientMessage:
    {
      XClientMessageEvent *ev = (XClientMessageEvent *) event;
      // Window manager close.
      if (ev->message_type == f->mycontext->protocol_atom &&
	  ev->data.l[0] == f->mycontext->kill_atom)
        f->hide();
    }
    break;
  default:
    ;
  }
}

// This is called by the constructor and also by hide()
void
xxXTideRoot::addTwoButtons() {
  {
    Arg buttonargs[10] =  {
      {XtNvisual, (XtArgVal)mycontext->visual},
      {XtNcolormap, (XtArgVal)mycontext->colormap},
      {XtNbackground, (XtArgVal)mycontext->pixels[Colors::button]},
      {XtNforeground, (XtArgVal)mycontext->pixels[Colors::foreground]},
      {XtNfromVert, (XtArgVal)viewport->manager},
      {XtNfromHoriz, (XtArgVal)savebutton->manager},
      {XtNleft, (XtArgVal)XawChainLeft},
      {XtNright, (XtArgVal)XawChainLeft},
      {XtNtop, (XtArgVal)XawChainBottom},
      {XtNbottom, (XtArgVal)XawChainBottom}
    };
    Widget buttonwidget = XtCreateManagedWidget ("Dismiss", commandWidgetClass,
      form->manager, buttonargs, 10);
    XtAddCallback (buttonwidget, XtNcallback, dismissCallback,
     (XtPointer)this);
    dismissbutton = new xxContext (form, buttonwidget);
  }
  {
    Arg buttonargs[10] =  {
      {XtNvisual, (XtArgVal)mycontext->visual},
      {XtNcolormap, (XtArgVal)mycontext->colormap},
      {XtNbackground, (XtArgVal)mycontext->pixels[Colors::button]},
      {XtNforeground, (XtArgVal)mycontext->pixels[Colors::foreground]},
      {XtNfromVert, (XtArgVal)viewport->manager},
      {XtNfromHoriz, (XtArgVal)dismissbutton->manager},
      {XtNleft, (XtArgVal)XawChainLeft},
      {XtNright, (XtArgVal)XawChainLeft},
      {XtNtop, (XtArgVal)XawChainBottom},
      {XtNbottom, (XtArgVal)XawChainBottom}
    };
    Widget buttonwidget = XtCreateManagedWidget ("?", commandWidgetClass,
      form->manager, buttonargs, 10);
    XtAddCallback (buttonwidget, XtNcallback, helpCallback,
      (XtPointer)this);
    helpbutton = new xxContext (form, buttonwidget);
  }
}

xxXTideRoot::xxXTideRoot (int in_argc, char **in_argv) {
  numpopups = 0;
  children = NULL;
  Colors *colors;
  Settings *settings;
  mycontext = new xxContext (in_argc, in_argv, colors, settings);
  mycontext->setTitle ("Control Panel");
  myTideContext = new xxTideContext (in_argc, in_argv, mycontext, this,
    colors, settings);
  set_error_context (mycontext, myTideContext);

  Arg args[2] =  {
    {XtNbackground, (XtArgVal)mycontext->pixels[Colors::background]},
    {XtNforeground, (XtArgVal)mycontext->pixels[Colors::foreground]}
  };
  XtSetValues (mycontext->manager, args, 2);
  XtAddEventHandler (mycontext->manager, NoEventMask, True,
    RootCloseHandler, (XtPointer)this);

  {
    Arg formargs[3] =  {
      {XtNbackground, (XtArgVal)mycontext->pixels[Colors::background]},
      {XtNforeground, (XtArgVal)mycontext->pixels[Colors::foreground]},
      {XtNvSpace, (XtArgVal)0}
    };
    Widget formwidget = XtCreateManagedWidget ("", formWidgetClass,
      mycontext->manager, formargs, 3);
    form = new xxContext (mycontext, formwidget);
  }

  {
    Arg labelargs[6] =  {
      {XtNbackground, (XtArgVal)mycontext->pixels[Colors::background]},
      {XtNforeground, (XtArgVal)mycontext->pixels[Colors::foreground]},
      {XtNleft, (XtArgVal)XawChainLeft},
      {XtNright, (XtArgVal)XawChainLeft},
      {XtNtop, (XtArgVal)XawChainTop},
      {XtNbottom, (XtArgVal)XawChainTop}
    };
    Widget labelwidget = XtCreateManagedWidget (
      "-------------- XTide Control Panel --------------",
      labelWidgetClass, form->manager, labelargs, 6);
    label = new xxContext (form, labelwidget);
  }

  Arg vpargs[9] =  {
    {XtNbackground, (XtArgVal)mycontext->pixels[Colors::background]},
    {XtNforeground, (XtArgVal)mycontext->pixels[Colors::foreground]},
    {XtNallowVert, (XtArgVal)1},
    {XtNforceBars, (XtArgVal)1},
    {XtNfromVert, (XtArgVal)label->manager},
    {XtNleft, (XtArgVal)XawChainLeft},
    {XtNright, (XtArgVal)XawChainRight},
    {XtNtop, (XtArgVal)XawChainTop},
    {XtNbottom, (XtArgVal)XawChainBottom}
  };

  Widget viewportwidget = XtCreateManagedWidget ("", viewportWidgetClass,
    form->manager, vpargs, 9);
  viewport = new xxContext (form, viewportwidget);

  Widget scrollbarwidget;
  assert (scrollbarwidget = XtNameToWidget (viewportwidget, "vertical"));
  Arg sbargs[2] = {
    {XtNbackground, (XtArgVal)mycontext->pixels[Colors::background]},
    {XtNforeground, (XtArgVal)mycontext->pixels[Colors::foreground]}
  };
  XtSetValues (scrollbarwidget, sbargs, 2);

  {
    Arg boxargs[4] =  {
      {XtNbackground, (XtArgVal)mycontext->pixels[Colors::background]},
      {XtNforeground, (XtArgVal)mycontext->pixels[Colors::foreground]},
      {XtNvSpace, (XtArgVal)0},
      {XtNorientation, (XtArgVal)XtorientVertical}
    };
    Widget boxwidget = XtCreateManagedWidget ("", boxWidgetClass,
      viewport->manager, boxargs, 4);
    viewbox = new xxContext (viewport, boxwidget);
  }

  // Get current settings.  (Another duplication...)
  ud = new UserDefaults();

  // Add dialogs for all of the colors
  int a;
  for (a=0; a<numcolors; a++)
    colordiags[a] = new xxHorizDialog (viewbox, colordesc[a],
      ud->colors[a].aschar());

  // Add toggles
  static char *togglechoices[] = {"Yes", "No", "(blank)", NULL};
  {
    Dstr t = ud->el;
    unsigned firstchoice;
    if (t.isNull())
      firstchoice = 2;
    else if (t == "n")
      firstchoice = 1;
    else
      firstchoice = 0;
    elchoice = new xxMultiChoice (viewbox,
      "Draw datum and Mean Tide Level lines in tide graphs?", togglechoices, firstchoice);
  }
  {
    Dstr t = ud->fe;
    unsigned firstchoice;
    if (t.isNull())
      firstchoice = 2;
    else if (t == "n")
      firstchoice = 1;
    else
      firstchoice = 0;
    fechoice = new xxMultiChoice (viewbox,
      "Prefer flat map to round globe location chooser?", togglechoices, firstchoice);
  }
  {
    Dstr t = ud->nf;
    unsigned firstchoice;
    if (t.isNull())
      firstchoice = 2;
    else if (t == "n")
      firstchoice = 1;
    else
      firstchoice = 0;
    nfchoice = new xxMultiChoice (viewbox,
      "Draw tide graphs as line graphs?", togglechoices, firstchoice);
  }
  {
    Dstr t = ud->ns;
    unsigned firstchoice;
    if (t.isNull())
      firstchoice = 2;
    else if (t == "n")
      firstchoice = 1;
    else
      firstchoice = 0;
    nschoice = new xxMultiChoice (viewbox,
      "Suppress sun and moon events in output?", togglechoices, firstchoice);
  }
  {
    Dstr t = ud->in;
    unsigned firstchoice;
    if (t.isNull())
      firstchoice = 2;
    else if (t == "n")
      firstchoice = 1;
    else
      firstchoice = 0;
    inchoice = new xxMultiChoice (viewbox,
      "Use inferred constituents (expert only)?", togglechoices, firstchoice);
  }
  {
    Dstr t = ud->tl;
    unsigned firstchoice;
    if (t.isNull())
      firstchoice = 2;
    else if (t == "n")
      firstchoice = 1;
    else
      firstchoice = 0;
    tlchoice = new xxMultiChoice (viewbox,
      "Draw depth lines on top of tide graph?", togglechoices, firstchoice);
  }
  {
    Dstr t = ud->z;
    unsigned firstchoice;
    if (t.isNull())
      firstchoice = 2;
    else if (t == "n")
      firstchoice = 1;
    else
      firstchoice = 0;
    zchoice = new xxMultiChoice (viewbox,
      "Coerce all time zones to UTC?", togglechoices, firstchoice);
  }

  static char *unitschoices[] = {"Feet", "Meters", "No preference",
    "(blank)", NULL};
  {
    Dstr t = ud->u;
    unsigned firstchoice;
    if (t.isNull())
      firstchoice = 3;
    else if (t == "ft")
      firstchoice = 0;
    else if (t == "m")
      firstchoice = 1;
    else
      firstchoice = 2;
    uchoice = new xxMultiChoice (viewbox,
      "Preferred units of length:", unitschoices, firstchoice);
  }

  static char *glchoices[] = {"180", "150 W", "120 W", "90 W", "60 W",
    "30 W", "0", "30 E", "60 E", "90 E", "120 E", "150 E",
    "Max stations", "(blank)", NULL};
  {
    unsigned firstchoice = 13;
    if (!(ud->gl_isnull)) {
      if (ud->gl == 360.0)
        firstchoice = 12;
      else
        firstchoice = (unsigned) ((ud->gl + 180.0) / 30.0);
    }
    glchoice = new xxMultiChoice (viewbox,
      "Default center longitude for globe:", glchoices, firstchoice);
  }

  // Date/time format dialogs
  dfdiag = new xxHorizDialog (viewbox,
    "Strftime style format string for printing dates.", ud->df.aschar());
  hfdiag = new xxHorizDialog (viewbox,
    "Strftime style format string for printing hour labels on time axis.",
    ud->hf.aschar());
  tfdiag = new xxHorizDialog (viewbox,
    "Strftime style format string for printing times.", ud->tf.aschar());

  // Unsigned numbers.
  gwchoice = new xxUnsignedChooser (viewbox,
    "Default width for tide graphs (pixels):", ud->gw, ud->gw_isnull, mingwidth);
  ghchoice = new xxUnsignedChooser (viewbox,
    "Default height for tide graphs and clocks (pixels):", ud->gh, ud->gh_isnull,
    mingheight);
  cwchoice = new xxUnsignedChooser (viewbox,
    "Default width for tide clocks (pixels):", ud->cw, ud->cw_isnull, mingwidth);
  twchoice = new xxUnsignedChooser (viewbox,
    "Default width of ASCII graphs and banners (characters):", ud->tw,
     ud->tw_isnull, minttywidth);
  thchoice = new xxUnsignedChooser (viewbox,
    "Default height of ASCII graphs (characters):", ud->th, ud->th_isnull,
    minttyheight);

  // Doubles.
  {
    char temp[80];
    if (ud->ga_isnull)
      strcpy (temp, "");
    else
      sprintf (temp, "%f", ud->ga);
    gadiag = new xxHorizDialog (viewbox,
      "Default aspect for tide graphs.", temp);
  }
  {
    char temp[80];
    if (ud->lw_isnull)
      strcpy (temp, "");
    else
      sprintf (temp, "%f", ud->lw);
    lwdiag = new xxHorizDialog (viewbox,
      "Width for lines in line graphs (pixels, pos. real number).", temp);
  }

  {
    Arg applyargs[9] =  {
      {XtNvisual, (XtArgVal)mycontext->visual},
      {XtNcolormap, (XtArgVal)mycontext->colormap},
      {XtNbackground, (XtArgVal)mycontext->pixels[Colors::button]},
      {XtNforeground, (XtArgVal)mycontext->pixels[Colors::foreground]},
      {XtNfromVert, (XtArgVal)viewport->manager},
      {XtNleft, (XtArgVal)XawChainLeft},
      {XtNright, (XtArgVal)XawChainLeft},
      {XtNtop, (XtArgVal)XawChainBottom},
      {XtNbottom, (XtArgVal)XawChainBottom}
    };
    Widget buttonwidget = XtCreateManagedWidget ("Apply", commandWidgetClass,
      form->manager, applyargs, 9);
    XtAddCallback (buttonwidget, XtNcallback, applyCallback,
     (XtPointer)this);
    applybutton = new xxContext (form, buttonwidget);
  }
  {
    Arg saveargs[10] =  {
      {XtNvisual, (XtArgVal)mycontext->visual},
      {XtNcolormap, (XtArgVal)mycontext->colormap},
      {XtNbackground, (XtArgVal)mycontext->pixels[Colors::button]},
      {XtNforeground, (XtArgVal)mycontext->pixels[Colors::foreground]},
      {XtNfromVert, (XtArgVal)viewport->manager},
      {XtNfromHoriz, (XtArgVal)applybutton->manager},
      {XtNleft, (XtArgVal)XawChainLeft},
      {XtNright, (XtArgVal)XawChainLeft},
      {XtNtop, (XtArgVal)XawChainBottom},
      {XtNbottom, (XtArgVal)XawChainBottom}
    };
    Widget buttonwidget = XtCreateManagedWidget ("Save", commandWidgetClass,
      form->manager, saveargs, 10);
    XtAddCallback (buttonwidget, XtNcallback, saveCallback,
     (XtPointer)this);
    savebutton = new xxContext (form, buttonwidget);
  }

  addTwoButtons();
}

void
xxXTideRoot::dup() {
  numpopups++;
}

void
xxXTideRoot::dup(xxWindow *child) {
  dup();
  struct cnode *tempc = new cnode;
  tempc->child = child;
  tempc->next = children;
  children = tempc;
}

void xxXTideRoot::release(int is_title_screen) {
  assert (numpopups > 0);
  numpopups--;
  if ((!numpopups) && (!is_title_screen))
    exit (0);
}

void xxXTideRoot::release(xxContext *child, int is_title_screen) {
  XtUnmanageChild (child->manager);
  release(is_title_screen);
}

void xxXTideRoot::release(xxWindow *child) {
  release (child->mypopup, child->is_title_screen);
  struct cnode *tempc = NULL;
  while (children) {
    struct cnode *t2c = children;
    children = children->next;
    if (t2c->child != child) {
      t2c->next = tempc;
      tempc = t2c;
    } else {
      delete t2c;
    }
  }
  children = tempc;
}

void xxXTideRoot::global_redraw() {
  struct cnode *tempc = children;
  while (tempc) {
    tempc->child->global_redraw();
    tempc = tempc->next;
  }

  int a;
  for (a=0; a<numcolors; a++)
    colordiags[a]->global_redraw();

  elchoice->global_redraw();
  fechoice->global_redraw();
  nfchoice->global_redraw();
  nschoice->global_redraw();
  inchoice->global_redraw();
  tlchoice->global_redraw();
  zchoice->global_redraw();
  uchoice->global_redraw();

  dfdiag->global_redraw();
  hfdiag->global_redraw();
  tfdiag->global_redraw();
  gwchoice->global_redraw();
  glchoice->global_redraw();
  ghchoice->global_redraw();
  cwchoice->global_redraw();
  twchoice->global_redraw();
  thchoice->global_redraw();

  gadiag->global_redraw();
  lwdiag->global_redraw();

  Arg buttonargs[2] =  {
    {XtNbackground, (XtArgVal)mycontext->pixels[Colors::button]},
    {XtNforeground, (XtArgVal)mycontext->pixels[Colors::foreground]}
  };
  if (applybutton)
    XtSetValues (applybutton->manager, buttonargs, 2);
  if (savebutton)
    XtSetValues (savebutton->manager, buttonargs, 2);
  if (helpbutton)
    XtSetValues (helpbutton->manager, buttonargs, 2);
  if (dismissbutton)
    XtSetValues (dismissbutton->manager, buttonargs, 2);

  Arg args[2] =  {
    {XtNbackground, (XtArgVal)mycontext->pixels[Colors::background]},
    {XtNforeground, (XtArgVal)mycontext->pixels[Colors::foreground]}
  };
  if (label)
    XtSetValues (label->manager, args, 2);
  if (viewbox)
    XtSetValues (viewbox->manager, args, 2);
  if (viewport)
    XtSetValues (viewport->manager, args, 2);
  if (form)
    XtSetValues (form->manager, args, 2);
  if (mycontext)
    XtSetValues (mycontext->manager, args, 2);
}

xxXTideRoot::~xxXTideRoot () {
  mycontext->unrealize();
  delete applybutton;
  delete savebutton;
  delete helpbutton;
  delete dismissbutton;
  delete label;
  int a;
  for (a=0; a<numcolors; a++)
    delete colordiags[a];
  delete dfdiag;
  delete hfdiag;
  delete tfdiag;
  delete gadiag;
  delete lwdiag;
  delete gwchoice;
  delete ghchoice;
  delete glchoice;
  delete twchoice;
  delete thchoice;
  delete cwchoice;
  delete elchoice;
  delete fechoice;
  delete nfchoice;
  delete nschoice;
  delete inchoice;
  delete tlchoice;
  delete zchoice;
  delete uchoice;
  delete viewbox;
  delete viewport;
  delete form;
  delete mycontext;
  delete myTideContext;
  delete ud;
}

void xxXTideRoot::mainloop() {
  struct stat buf;
  if (stat (myTideContext->disabledisclaimerfile.aschar(), &buf)) {
    (void) new xxDisclaimer (mycontext, myTideContext);
    xxMainLoop (mycontext);
  }

  if (!(myTideContext->cls->l))
    newChooser (XtGrabNone); // Let the disclaimer scroll.
  else
    newWindows (myTideContext->cls);

  xxMainLoopForever (mycontext);
}

void xxXTideRoot::newHelpBox (const Dstr &help) {
  (void) new xxHelpBox (myTideContext, mycontext, help);
}

void xxXTideRoot::newGraph (Station *s, Timestamp t) {
  (void) new xxGraphMode (myTideContext, mycontext, s, t);
}

void xxXTideRoot::newGraph (StationRef *sr) {
  Timestamp t ((time_t)(time(NULL)));
  newGraph (sr->load (myTideContext), t);
}

void xxXTideRoot::newText (Station *s, Timestamp t) {
  (void) new xxTextMode (myTideContext, mycontext, s, 2, t);
}

void xxXTideRoot::newRaw (Station *s, Timestamp t) {
  (void) new xxTextMode (myTideContext, mycontext, s, 0, t);
}

void xxXTideRoot::newMediumRare (Station *s, Timestamp t) {
  (void) new xxTextMode (myTideContext, mycontext, s, 1, t);
}

void xxXTideRoot::newClock (Station *s, int nobuttonsflag) {
  (void) new xxClock (myTideContext, mycontext, s, nobuttonsflag);
}

void xxXTideRoot::newWindows (CommandLineSettings *cls) {
  Timestamp t ((time_t)(time(NULL)));
  assert (cls);
  struct CommandLineSettings::lnode *l = cls->l;
  while (l) {
    StationRef *sr;
    sr = (*(myTideContext->stationIndex()))[l->name];
    if (sr) {
      if (!(cls->b.isNull())) {
        t = Timestamp (cls->b, sr->timezone, myTideContext->settings);
	if (t.isNull()) {
	  Dstr details ("The offending input was ");
	  details += cls->b;
	  details += "\nin the time zone ";
	  if (myTideContext->settings->z == "n")
	    details += sr->timezone;
	  else
	    details += "UTC0";
	  barf (MKTIME_FAILED, details);
	}
      }
      Station *s = sr->load (myTideContext);
      if (cls->m == "g")
        newGraph (s, t);
      else if (cls->m == "p")
        newText (s, t);
      else
        newClock (s, 1);
    } else {
      Dstr details ("Could not find: ");
      details += l->name;
      barf (STATION_NOT_FOUND, details);
    }
    l = l->next;
  }
}

void xxXTideRoot::newChooser (XtGrabKind in_grabkind) {
  if (myTideContext->settings->fe == "n")
    (void) new xxGlobe (mycontext, *(myTideContext->stationIndex(in_grabkind)),
      myTideContext);
  else
    (void) new xxMap (mycontext, *(myTideContext->stationIndex(in_grabkind)),
      myTideContext);
}

void xxXTideRoot::newMap (XtGrabKind in_grabkind) {
  (void) new xxMap (mycontext, *(myTideContext->stationIndex(in_grabkind)),
    myTideContext);
}

void xxXTideRoot::newGlobe (XtGrabKind in_grabkind) {
  (void) new xxGlobe (mycontext, *(myTideContext->stationIndex(in_grabkind)),
    myTideContext);
}

void xxXTideRoot::show() {
  if (!(mycontext->is_realized)) {
    mycontext->realize();
    dup();
  }
}

void xxXTideRoot::hide() {
  if (mycontext->is_realized) {
    mycontext->unrealize();
    release(0);

    // Since the window vanishes, the Dismiss button misses the LeaveWindow
    // event.  The next time the window is realized, Dismiss is still stuck
    // on.  I tried to synthesize a LeaveWindow event, but it didn't work.
    // I don't know a good way to reset the state, so extreme measures are
    // required.  To keep the buttons in order, the help button must also
    // be deleted.
    delete dismissbutton;
    delete helpbutton;
    addTwoButtons();
  }
}