File: xxClock.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 (381 lines) | stat: -rw-r--r-- 12,491 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
// $Id: xxClock.cc,v 1.3 2003/01/17 17:31:00 flaterco Exp $
/*  xxClock  Tide clock.

    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
xxClock::dismiss () {
  delete this; // Is this safe?
}

void
xxClock::help() {
  Dstr helpstring ("\
XTide Tide Clock Window\n\
\n\
The tide clock shows the current time and tide level.  It updates once per\n\
minute.  You can resize the window to see more or less detail.\n\
\n\
To remove the buttons and just have a tide clock, click on the graph.  To\n\
bring back the buttons, click on the graph again.\n\
\n\
The options menu supplies the following commands:\n\
\n\
  Save:  Use this to write the tide predictions to a PNG image file.\n\
\n\
  Set Mark:  This is used to set the 'mark level' for tide predictions.\n\
  In clock mode, this only causes a line to be drawn at the mark level.\n\
  To see the times of mark transitions you must switch to graph or text\n\
  mode.  Some subordinate stations don't have this option.\n\
\n\
  Convert ft<->m:  Convert feet to meters or vice-versa.\n\
\n\
  Set Aspect:  This is used to change the aspect ratio for tide graphing.\n\
  The aspect ratio is a measure of how scrunched up or stretched out\n\
  the graph is.  If tide events are too close together, increase the\n\
  aspect; if you want to fit more information onto one graph, decrease\n\
  the aspect.\n\
\n\
  New Graph Window:  Open a graph window for the current 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\
  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:  Duplicates the existing clock window.\n\
\n\
  New Location Chooser:  Open new globe and location list windows to allow\n\
  selecting a new location.");
  xtidecontext->root->newHelpBox (helpstring);
}

void
xxClockResizeHandler (Widget w, XtPointer client_data,
    XEvent *event, Boolean *continue_dispatch) {
  xxClock *g = (xxClock *)client_data;
  switch (event->type) {
  case ConfigureNotify:
    {
      XConfigureEvent *ev = (XConfigureEvent *)event;
      Dimension newheight = ev->height;
      Dimension newwidth = ev->width;
      if (newheight != g->curwindowheight ||
	  newwidth != g->curwindowwidth) {
	g->curwindowheight = newheight;
	g->curwindowwidth = newwidth;
	g->curgraphheight = newheight - (g->origwindowheight - g->origgraphheight);
	g->curgraphwidth = newwidth - (g->origwindowwidth - g->origgraphwidth);
	g->redraw();
      }
    }
    break;
  default:
    ;
  }
}

void
xxClockSaveCallback (Dstr &filename, void *in_ptr) {
  xxClock *gm = (xxClock *)in_ptr;
  // See externC.cc
  if ((png_file_ptr = fopen (filename.aschar(), "w"))) {
    RGBGraph g (gm->curgraphwidth, gm->curgraphheight, gm->xtidecontext->colors);
    g.clockmode = 1;
    g.drawTides (gm->station, gm->t);
    g.writeAsPNG (file_write_data_fn);
    fclose (png_file_ptr);
  } else {
    Dstr details (filename);
    details += ": ";
    details += strerror (errno);
    details += ".";
    barf (CANT_OPEN_FILE, details, 0);
  }
}

void
xxClock::save () {
  (void) new xxFilename (xtidecontext, mypopup, xxClockSaveCallback, this, "tides.png");
}

void
xxClockButtonPressEventHandler (Widget w, XtPointer client_data,
			 XEvent *event, Boolean *continue_dispatch) {
  xxClock *c = (xxClock *)client_data;
  // This is rather crude, but it's hard to do better.
  if (c->station)
    c->xtidecontext->root->newClock (c->station, !(c->nobuttons));
  c->station = NULL;
  delete c;
}

xxClock::xxClock (xxTideContext *in_tidecontext,
xxContext *in_xxcontext, Station *in_station, int nobuttonsflag):
xxDrawable (in_tidecontext, in_xxcontext, in_station, !nobuttonsflag)
{
  label = NULL;
  t = Timestamp((time_t)(time(NULL)));
  nobuttons = nobuttonsflag;
  Dstr title (station->name);
  title += " (Clock)";
  mypopup->setTitle (title);
  XtAddEventHandler (mypopup->manager, StructureNotifyMask, False,
		     xxClockResizeHandler, (XtPointer)this);

  Dimension minxcwidth = minwidthfudge * 3 + mypopup->stringWidth
    (mypopup->defaultfontstruct, "OptionsDismiss?");

  origgraphheight = curgraphheight = xtidecontext->settings->gh;
  if (nobuttons)
    origgraphwidth = curgraphwidth = xtidecontext->settings->cw;
  else
    origgraphwidth = curgraphwidth = max (minxcwidth, xtidecontext->settings->cw);
  graph = new xxPixmapGraph (origgraphwidth, origgraphheight, mypopup);
  graph->clockmode = 1;
  graph->drawTides (station, t, &analogangle);

  if (nobuttons) {
    Arg args[3] = {
      {XtNbackgroundPixmap, (XtArgVal)graph->pixmap},
      {XtNwidth, (XtArgVal)curgraphwidth},
      {XtNheight, (XtArgVal)curgraphheight}
    };
    XtSetValues (mypopup->manager, args, 3);
    XtAddEventHandler (mypopup->manager, ButtonPressMask, False,
      xxClockButtonPressEventHandler, (XtPointer)this);
  } else {
    Arg labelargs[5] =  {
      {XtNbitmap, (XtArgVal)graph->pixmap},
      {XtNbackground, (XtArgVal)mypopup->pixels[Colors::background]},
      {XtNforeground, (XtArgVal)mypopup->pixels[Colors::foreground]},
      {XtNinternalHeight, (XtArgVal)0},
      {XtNinternalWidth, (XtArgVal)0}
    };
    {
      Widget labelwidget = XtCreateManagedWidget ("",
	labelWidgetClass, container->manager, labelargs, 5);
      label = new xxContext (mypopup, labelwidget);
    }
    addNormalButtons();
    XtAddEventHandler (label->manager, ButtonPressMask, False,
      xxClockButtonPressEventHandler, (XtPointer)this);
  }

  // No call to redraw this time.
  mypopup->realize();

  {
    Arg args[2] = {
      {XtNwidth, (XtArgVal)(&origwindowwidth)},
      {XtNheight, (XtArgVal)(&origwindowheight)}
    };
    if (nobuttons)
      XtGetValues (mypopup->manager, args, 2);
    else
      XtGetValues (container->manager, args, 2);
#ifdef SUPER_ULTRA_VERBOSE_DEBUGGING
    cerr << "Clock window original height " << origwindowheight << endl;
    cerr << "Clock window original width " << origwindowwidth << endl;
#endif
  }
  curwindowheight = origwindowheight;
  curwindowwidth = origwindowwidth;

  if (nobuttons)
    mypopup->setMinSize(mingwidth + (origwindowwidth - origgraphwidth),
                      mingheight + (origwindowheight - origgraphheight));
  else
    mypopup->setMinSize(minxcwidth + (origwindowwidth - origgraphwidth),
                      mingheight + (origwindowheight - origgraphheight));

  clockicon = mypopup->makePixmap (mypopup->icon_size, mypopup->icon_size, mypopup->ddepth);
  XResizeWindow (mypopup->display, mypopup->icon_window, mypopup->icon_size, mypopup->icon_size);

  // Need two icon windows to minimize "flashing" on updates
  int screen = DefaultScreen(mypopup->display);
  iconwindow2 = XCreateWindow (mypopup->display,
    RootWindow (mypopup->display, screen), 0, 0, mypopup->icon_size,
     mypopup->icon_size, 0, DefaultDepth (mypopup->display, screen),
    InputOutput, DefaultVisual (mypopup->display, screen), 0, NULL);
  toggle_icon_window = 1;
  redrawIcon();

  timer = XtAppAddTimeOut (mypopup->context, 1000*(60-(time(NULL)%60)),
    xxClockTimerCallback, this);
}

#ifdef clocktest
int ctserial = 0;
#endif
void
xxClock::redrawIcon() {
  XSetForeground (mypopup->display, mypopup->icon_gc, mypopup->iconpixels[0]);
  XFillRectangle (mypopup->display, clockicon, mypopup->icon_gc, 0, 0,
    mypopup->icon_size, mypopup->icon_size);

  XSetForeground (mypopup->display, mypopup->icon_gc, mypopup->iconpixels[1]);

  char *hicap, *locap;
  if (station->isCurrent) {
    hicap = "Flood"; locap = "Ebb";
  } else {
    hicap = "High"; locap = "Low";
  }
  int c = mypopup->icon_size / 2; // Center
  int d = mypopup->icon_size - 17; // Diameter w/ fixed size "Hi" and "Lo"
  int hl = d / 2 - 4; // Hand length
  XDrawArc (mypopup->display, clockicon, mypopup->icon_gc,
    8, 8, d, d, 0, 360*64);
  XDrawArc (mypopup->display, clockicon, mypopup->icon_gc,
    c-1, c-1, 2, 2, 0, 360*64);
  XDrawString (mypopup->display, clockicon, mypopup->icon_gc,
    c-(mypopup->stringWidth(mypopup->tinyfontstruct, hicap)/2), 7,
    hicap, strlen(hicap));
  XDrawString (mypopup->display, clockicon, mypopup->icon_gc,
    c-(mypopup->stringWidth(mypopup->tinyfontstruct, locap)/2),
    mypopup->icon_size, locap, strlen(locap));
  int handx = (int)(c + hl * sin (analogangle) + 0.5);
  int handy = (int)(c - hl * cos (analogangle) + 0.5);
  XDrawLine (mypopup->display, clockicon, mypopup->icon_gc,
    c, c, handx, handy);

#ifdef clocktest
  char temp[80];
  sprintf (temp, "%d", ctserial++);
  XDrawString (mypopup->display, clockicon, mypopup->icon_gc,
    0, 7, temp, strlen(temp));
#endif

  // Use two windows to minimize "flashing" (can't get rid of it totally)
  Window window;
  if (toggle_icon_window == 1) {
    window = mypopup->icon_window;
    toggle_icon_window = 2;
  } else {
    window = iconwindow2;
    toggle_icon_window = 1;
  }

  // Lower level version of the same old song and dance to force it to
  // update its pixmap.
  XSetWindowAttributes xswa;
  xswa.background_pixmap = None;
  // xswa.event_mask = ExposureMask;
  XChangeWindowAttributes (mypopup->display, window, CWBackPixmap, &xswa);
  xswa.background_pixmap = clockicon;
  XChangeWindowAttributes (mypopup->display, window, CWBackPixmap, &xswa);
  // mypopup->refreshIcon();
#if 0
  Arg args[1] = {
    {XtNiconWindow, (XtArgVal)0}
  };
  XtSetValues (mypopup->manager, args, 1);
#endif
  Arg args2[1] = {
    {XtNiconWindow, (XtArgVal)(window)}
  };
  XtSetValues (mypopup->manager, args2, 1);
}

void
xxClockTimerCallback (XtPointer client_data, XtIntervalId *timerid) {
  xxClock *g = (xxClock *)client_data;
  g->t = Timestamp((time_t)(time(NULL)));
  // Lightweight redraw
  g->graph->drawTides (g->station, g->t, &(g->analogangle));
  g->draw();
  g->redrawIcon();
  g->timer = XtAppAddTimeOut (g->mypopup->context, 1000*(60-(time(NULL)%60)),
    xxClockTimerCallback, g);
}

void xxClock::redraw() {
  // Save this until the window is updated to avoid a BadDrawable error
  xxPixmapGraph *oldgraph = graph;
  graph = new xxPixmapGraph (curgraphwidth, curgraphheight, mypopup);
  graph->clockmode = 1;
  graph->drawTides (station, t, &analogangle);

  // Don't seem to have problems resizing this time.  See xxTextMode
  // for the kludge if needed.

  draw();
  delete oldgraph;
}

// You have to call drawTides on the graph before calling this.
void xxClock::draw () {
  if (nobuttons) {
    // (Kludge from xxTitleScreen)
    // You really have to pound on it to get it to refresh.
    Arg args1[1] = {
      {XtNbackgroundPixmap, (XtArgVal)0}
    };
    XtSetValues (mypopup->manager, args1, 1);
    Arg args2[1] = {
      {XtNbackgroundPixmap, (XtArgVal)graph->pixmap}
    };
    XtSetValues (mypopup->manager, args2, 1);
    mypopup->refresh();
  } else {
    Arg args[1] = {
      {"bitmap", (XtArgVal)graph->pixmap}
    };
    XtSetValues (label->manager, args, 1);
    label->refresh();
  }
}

xxClock::~xxClock() {
  XtRemoveTimeOut (timer);
  mypopup->unrealize();
  delete graph;
  if (!nobuttons)
    delete label;
  XDestroyWindow(mypopup->display, iconwindow2);
  XFreePixmap (mypopup->display, clockicon);
}

int
xxClock::is_graph() {
  return 1;
}

int
xxClock::is_clock() {
  return 1;
}

void xxClock::global_redraw() {
  xxDrawable::global_redraw();
  Arg args[2] =  {
    {XtNbackground, (XtArgVal)mypopup->pixels[Colors::background]},
    {XtNforeground, (XtArgVal)mypopup->pixels[Colors::foreground]}
  };
  if (label)
    XtSetValues (label->manager, args, 2);
  redrawIcon();
}