File: xxTextMode.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 (513 lines) | stat: -rw-r--r-- 15,203 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
// $Id: xxTextMode.cc,v 1.4 2003/01/17 18:33:00 flaterco Exp $
/*  xxTextMode  Plain old text mode, in a 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"

void
xxTextMode::dismiss () {
  delete this; // Is this safe?
}

void
xxTextMode::help() {
  Dstr helpstring;

  if (doneness == 2)
    helpstring = "\
XTide Text Mode Window\n\
\n\
Use the forward and backward buttons to scroll the tide predictions\n\
forward or backward in time.  You can resize the window vertically\n\
to see more predictions at once.\n\
\n\
The options menu supplies the following commands:\n\
\n\
  Save:  Use this to write the tide predictions to a text file.\n\
\n\
  Set Mark:  This is used to set the 'mark level' for tide predictions.\n\
  The prediction window will show the times when the tide level crosses\n\
  the mark.  Some subordinate stations don't have this option.\n\
\n\
  Convert ft<->m:  Convert feet to meters or vice-versa.\n\
\n\
  Set Time:  Go to a different point in time.  Major adjustments that\n\
  cannot be made with the forward and backward buttons can be made with\n\
  Set Time.\n\
\n\
  New Graph Window:  Open a window with a tide graph for the current\n\
  location and time.\n\
\n\
  New Text Window:  Duplicates the existing text window.\n\
\n\
  New Raw Mode Window:  Open a window with a text listing of unprocessed\n\
  numerical time stamps and tide levels for the current location and time.\n\
\n\
  New Medium Rare Mode Window:  Open a window with a text listing of\n\
  processed timestamps and unprocessed numerical tide levels for the\n\
  current location and time.\n\
\n\
  New Clock Window:  Open a window with a tide clock for the current\n\
  location.\n\
\n\
  New Location Chooser:  Open new globe and location list windows to allow\n\
  selecting a new location.";
  else {

  if (doneness)
    helpstring = "XTide Medium Rare Mode Window\n";
  else
    helpstring = "XTide Raw Mode Window\n";
  helpstring += "\n\
Use the forward and backward buttons to scroll the tide predictions\n\
forward or backward in time.  You can resize the window vertically\n\
to see more predictions at once.\n\
\n\
The options menu supplies the following commands:\n\
\n\
  Save:  Use this to send output to a text file.  In this mode you are\n\
  given the opportunity to generate output for a different time interval\n\
  than is displayed on the screen.\n\
\n\
  Convert ft<->m:  Convert feet to meters or vice-versa.\n\
\n\
  Set Time:  Go to a different point in time.  Major adjustments that\n\
  cannot be made with the forward and backward buttons can be made with\n\
  Set Time.\n\
\n\
  Set Step:  Change the time increment of the output.\n\
\n\
  New Graph Window:  Open a window with a tide graph for the current\n\
  location and time.\n\
\n\
  New Text Window:  Open a window with a text listing of tide predictions\n\
  for the current location and time.\n\
\n";

  if (doneness)
    helpstring += "\
  New Raw Mode Window:  Open a window with a text listing of unprocessed\n\
  numerical time stamps and tide levels for the current location and time.\n\
\n\
  New Medium Rare Mode Window:  Duplicates the existing medium rare mode\n\
  window.\n";
  else
    helpstring += "\
  New Raw Mode Window:  Duplicates the existing raw mode window.\n\
\n\
  New Medium Rare Mode Window:  Open a window with a text listing of\n\
  processed timestamps and unprocessed numerical tide levels for the\n\
  current location and time.\n";

  helpstring += "\n\
  New Clock Window:  Open a window with a tide clock for the current\n\
  location.\n\
\n\
  New Location Chooser:  Open new globe and location list windows to allow\n\
  selecting a new location.";
  }
  xtidecontext->root->newHelpBox (helpstring);
}

void xxTextMode::predictexact (Station::Direction dir, struct tideline *tl) {
  xtidecontext->predictText (station, dir, tl->tm, tl->line, tl->etype);
}

void
xxTextModeforwardCallback (Widget w, XtPointer client_data,
XtPointer call_data) {
  xxTextMode *txt = (xxTextMode *)client_data;
  Timestamp tm = txt->lastline->tm;
  Station::EventType avoid_etype = txt->lastline->etype;

  // Move the first one to the end
  struct xxTextMode::tideline *tl = txt->firstline;
  txt->firstline = tl->next;
  txt->lastline->next = tl;
  txt->lastline = tl;
  tl->next = NULL;

  // Change its contents

  if (txt->doneness == 2) {
    tl->tm = tm;
    txt->predictexact (Station::forward, tl);
    // Avoid repetition
    if (tl->etype == avoid_etype)
      txt->predictexact (Station::forward, tl);
  } else {
    tm += txt->station->step;
    tl->tm = tm;
    txt->xtidecontext->rareModes (txt->station, tm, tm, txt->station->step, tl->line, txt->doneness);
    tl->line -= tl->line.length() - 1;
  }

  txt->draw();
}

void
xxTextModebackwardCallback (Widget w, XtPointer client_data,
XtPointer call_data) {
  xxTextMode *txt = (xxTextMode *)client_data;
  Timestamp tm = txt->firstline->tm;
  Station::EventType avoid_etype = txt->firstline->etype;

  // Move the last one to the start
  assert (txt->firstline != txt->lastline);
  struct xxTextMode::tideline *tl = txt->lastline;
  struct xxTextMode::tideline *temp2 = txt->firstline;
  while (temp2->next != tl)
    temp2 = temp2->next;
  temp2->next = NULL;
  txt->lastline = temp2;
  tl->next = txt->firstline;
  txt->firstline = tl;

  // Change its contents

  if (txt->doneness == 2) {
    tl->tm = tm;
    txt->predictexact (Station::backward, tl);
    // Avoid repetition
    if (tl->etype == avoid_etype)
      txt->predictexact (Station::backward, tl);
  } else {
    tm -= txt->station->step;
    tl->tm = tm;
    txt->xtidecontext->rareModes (txt->station, tm, tm, txt->station->step, tl->line, txt->doneness);
    tl->line -= tl->line.length() - 1;
  }

  txt->draw();
}

void
xxTextModeResizeHandler (Widget w, XtPointer client_data,
    XEvent *event, Boolean *continue_dispatch) {
  xxTextMode *txt = (xxTextMode *)client_data;
  switch (event->type) {
  case ConfigureNotify:
    {
      XConfigureEvent *ev = (XConfigureEvent *)event;
      Dimension newheight = ev->height;
      if (newheight != txt->height) {
	txt->height = newheight;
	// Extremely kludgy guesstimate of how many lines we should have now.
	int t = (int)((double)(newheight - (txt->origheight -
	  txt->origlabelheight)) * (double)origtextmodelines /
	  (double)(txt->origlabelheight));
	if (t < 1)
	  txt->lines = 1;
	else
	  txt->lines = t;
	txt->redraw();
      }
    }
    break;
  default:
    ;
  }
}

void
xxRareModeSaveCallback (Dstr &filename, Timestamp start_tm, Timestamp end_tm,
void *in_ptr) {
  xxTextMode *txt = (xxTextMode *)in_ptr;
  FILE *fp;
  if ((fp = fopen (filename.aschar(), "w"))) {
    Dstr text;
    txt->xtidecontext->rareModes (txt->station,
      start_tm, end_tm, txt->station->step, text, txt->doneness);
    fprintf (fp, "%s", text.aschar());
    fclose (fp);
  } else {
    Dstr details (filename);
    details += ": ";
    details += strerror (errno);
    details += ".";
    barf (CANT_OPEN_FILE, details, 0);
  }
}

void
xxTextModeSaveCallback (Dstr &filename, void *in_ptr) {
  xxTextMode *txt = (xxTextMode *)in_ptr;
  FILE *fp;
  if ((fp = fopen (filename.aschar(), "w"))) {
    Dstr text;
    txt->xtidecontext->textMode (txt->station,
      txt->t, txt->lastline->tm, text);
    fprintf (fp, "%s", text.aschar());
    fclose (fp);
  } else {
    Dstr details (filename);
    details += ": ";
    details += strerror (errno);
    details += ".";
    barf (CANT_OPEN_FILE, details, 0);
  }
}

void
xxTextMode::save () {
  if (doneness == 2)
    (void) new xxFilename (xtidecontext, mypopup, xxTextModeSaveCallback, this, "tides.txt");
  else
    (void) new xxRareModeSavePrompts (xtidecontext, mypopup, xxRareModeSaveCallback, this, "tides.txt", firstline->tm, lastline->tm, station->timeZone);
}

xxTextMode::xxTextMode (xxTideContext *in_tidecontext,
xxContext *in_xxcontext, Station *in_station, int doneness_in): xxDrawable (in_tidecontext,
in_xxcontext, in_station, 2) {
  doneness = doneness_in;
  t = Timestamp((time_t)(time(NULL)));
  construct();
}

xxTextMode::xxTextMode (xxTideContext *in_tidecontext,
xxContext *in_xxcontext, Station *in_station, int doneness_in, Timestamp t_in):
xxDrawable (in_tidecontext, in_xxcontext, in_station, 2) {
  doneness = doneness_in;
  t = t_in;
  construct();
}

void xxTextMode::construct () {
  lines = origtextmodelines;
  Dstr title (station->name);
  switch (doneness) {
  case 0:
    title += " (Raw)";
    break;
  case 1:
    title += " (Medium Rare)";
    break;
  case 2:
    title += " (Text)";
    break;
  default:
    assert (0);
  }
  mypopup->setTitle (title);
  XtAddEventHandler (mypopup->manager, StructureNotifyMask, False,
		     xxTextModeResizeHandler, (XtPointer)this);

  {
    Arg labelargs[6] =  {
      {XtNbackground, (XtArgVal)mypopup->pixels[Colors::background]},
      {XtNforeground, (XtArgVal)mypopup->pixels[Colors::foreground]},
      {XtNleft, (XtArgVal)XawChainLeft},
      {XtNright, (XtArgVal)XawChainLeft},
      {XtNtop, (XtArgVal)XawChainTop},
      {XtNbottom, (XtArgVal)XawChainTop}
    };
    Widget labelwidget = XtCreateManagedWidget (station->name.aschar(),
      labelWidgetClass, container->manager, labelargs, 6);
    namelabel = new xxContext (mypopup, labelwidget);
  }
  {
    Arg labelargs[7] =  {
      {XtNbackground, (XtArgVal)mypopup->pixels[Colors::background]},
      {XtNforeground, (XtArgVal)mypopup->pixels[Colors::foreground]},
      {XtNfromVert, (XtArgVal)namelabel->manager},
      {XtNleft, (XtArgVal)XawChainLeft},
      {XtNright, (XtArgVal)XawChainRight},
      {XtNtop, (XtArgVal)XawChainTop},
      {XtNbottom, (XtArgVal)XawChainBottom}
    };
    Widget labelwidget = XtCreateManagedWidget ("",
      labelWidgetClass, container->manager, labelargs, 7);
    label = new xxContext (mypopup, labelwidget);
  }
  {
    Arg buttonargs[7] =  {
      {XtNbackground, (XtArgVal)mypopup->pixels[Colors::button]},
      {XtNforeground, (XtArgVal)mypopup->pixels[Colors::foreground]},
      {XtNfromVert, (XtArgVal)label->manager},
      {XtNleft, (XtArgVal)XawChainLeft},
      {XtNright, (XtArgVal)XawChainLeft},
      {XtNtop, (XtArgVal)XawChainBottom},
      {XtNbottom, (XtArgVal)XawChainBottom}
    };
    Widget buttonwidget = XtCreateManagedWidget ("Backward", repeaterWidgetClass,
      container->manager, buttonargs, 7);
    XtAddCallback (buttonwidget, XtNcallback, xxTextModebackwardCallback,
     (XtPointer)this);
    backwardbutton = new xxContext (mypopup, buttonwidget);
  }
  {
    Arg buttonargs[8] =  {
      {XtNbackground, (XtArgVal)mypopup->pixels[Colors::button]},
      {XtNforeground, (XtArgVal)mypopup->pixels[Colors::foreground]},
      {XtNfromVert, (XtArgVal)label->manager},
      {XtNfromHoriz, (XtArgVal)backwardbutton->manager},
      {XtNleft, (XtArgVal)XawChainLeft},
      {XtNright, (XtArgVal)XawChainLeft},
      {XtNtop, (XtArgVal)XawChainBottom},
      {XtNbottom, (XtArgVal)XawChainBottom}
    };
    Widget buttonwidget = XtCreateManagedWidget ("Forward", repeaterWidgetClass,
      container->manager, buttonargs, 8);
    XtAddCallback (buttonwidget, XtNcallback, xxTextModeforwardCallback,
     (XtPointer)this);
    forwardbutton = new xxContext (mypopup, buttonwidget);
  }

  addNormalButtons (label->manager, forwardbutton->manager);

  firstline = lastline = NULL;
  redraw();
  mypopup->realize();
  mypopup->widthNoSmaller();

  {
    Arg args[1] = {
      {XtNheight, (XtArgVal)(&origheight)}
    };
    XtGetValues (container->manager, args, 1);
#ifdef SUPER_ULTRA_VERBOSE_DEBUGGING
    cerr << "Text window original height " << origheight << endl;
#endif
    height = origheight;
  }
  {
    Dimension internalheight;
    Arg args[2] = {
      {XtNheight, (XtArgVal)(&origlabelheight)},
      {XtNinternalHeight, (XtArgVal)(&internalheight)}
    };
    XtGetValues (label->manager, args, 2);
    origlabelheight -= internalheight * 2;
#ifdef SUPER_ULTRA_VERBOSE_DEBUGGING
    cerr << "Text label original height " << origlabelheight << endl;
#endif
  }
}

void xxTextMode::redraw() {
  Timestamp tm = t;

  clearbuf();

  unsigned a;
  for (a=0; a<lines; a++) {
    struct tideline *tl = new tideline;
    tl->tm = tm;

    if (doneness == 2) {
      predictexact (Station::forward, tl);
      tm = tl->tm;
    } else {
      xtidecontext->rareModes (station, tm, tm, station->step, tl->line, doneness);
      tl->line -= tl->line.length() - 1;
      tm += station->step;
    }

    tl->next = NULL;
    if (!a) {
      firstline = lastline = tl;
    } else {
      lastline->next = tl;
      lastline = tl;
    }
  }

  // Was having major trouble getting the label widget to resize itself
  // reliably.  This seems to help.
  {
    int heightguess = height - (origheight - origlabelheight);
    if (heightguess < 1)
      heightguess = 1;
    Arg labelargs[1] =  {
      {XtNheight, (XtArgVal)heightguess}
    };
    XtSetValues (label->manager, labelargs, 1);
  }

  draw();
}

void xxTextMode::draw () {
  Dstr tidetext;
  struct tideline *tl = firstline;
  while (tl) {
    tidetext += tl->line;
    tl = tl->next;
    if (tl)
      tidetext += "\n";
  }
  {
    Arg labelargs[1] =  {
      {XtNlabel, (XtArgVal)tidetext.aschar()}
    };
    XtSetValues (label->manager, labelargs, 1);
  }

  // Keep start time updated.
  if (doneness == 2)
    t = firstline->tm - Interval(60);
  else
    t = firstline->tm;
}

void
xxTextMode::clearbuf() {
  while (firstline) {
    struct tideline *tl = firstline->next;
    delete firstline;
    firstline = tl;
  }
  firstline = lastline = NULL;
}

xxTextMode::~xxTextMode() {
  mypopup->unrealize();
  delete label;
  delete namelabel;
  delete backwardbutton;
  delete forwardbutton;
  clearbuf();
}

void xxTextMode::global_redraw() {
  xxDrawable::global_redraw();
  redraw();
  Arg buttonargs[2] =  {
    {XtNbackground, (XtArgVal)mypopup->pixels[Colors::button]},
    {XtNforeground, (XtArgVal)mypopup->pixels[Colors::foreground]}
  };
  if (backwardbutton)
    XtSetValues (backwardbutton->manager, buttonargs, 2);
  if (forwardbutton)
    XtSetValues (forwardbutton->manager, buttonargs, 2);
  Arg args[2] =  {
    {XtNbackground, (XtArgVal)mypopup->pixels[Colors::background]},
    {XtNforeground, (XtArgVal)mypopup->pixels[Colors::foreground]}
  };
  if (label)
    XtSetValues (label->manager, args, 2);
  if (namelabel)
    XtSetValues (namelabel->manager, args, 2);
}

int xxTextMode::is_rare() {
  if (doneness < 2)
    return 1;
  return 0;
}