OpenCPN Partial API docs
Loading...
Searching...
No Matches
tc_win.cpp
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2013 by David S. Register *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
16 **************************************************************************/
17
23
24// For compilers that support precompilation, includes "wx.h".
25#include <wx/wxprec.h>
26
27#include <wx/button.h>
28#include <wx/choice.h>
29#include <wx/font.h>
30#include <wx/panel.h>
31#include <wx/dcbuffer.h>
32#include <wx/listctrl.h>
33#include <wx/utils.h>
34
35#include "tc_win.h"
36
37#include "model/cutil.h"
38#include "model/config_vars.h"
39#include "model/gui_vars.h"
40
41#include "abstract_chart_canv.h"
42#include "chcanv.h"
43#include "dychart.h"
44#include "font_mgr.h"
45#include "ocpn_platform.h"
46#include "rollover_win.h"
47#include "navutil.h"
48#include "gui_lib.h"
49#include "navutil.h"
50#include "ocpn_platform.h"
51#include "rollover_win.h"
52#include "tc_data_factory.h"
53#include "tcmgr.h"
54#include "tide_time.h"
55#include "timers.h"
56#include "user_colors.h"
57
58extern ColorScheme global_color_scheme; // library dependence
59
60// Custom chart panel class definition
61class TCWin::TideChartPanel : public wxPanel {
62public:
63 TideChartPanel(TCWin *parent) : wxPanel(parent, wxID_ANY), m_tcWin(parent) {
64 SetMinSize(wxSize(400, 200));
65 Bind(wxEVT_PAINT, &TideChartPanel::OnPaint, this);
66 Bind(wxEVT_MOTION, &TideChartPanel::OnMouseMove, this);
67 SetBackgroundStyle(wxBG_STYLE_CUSTOM); // Prevent flicker
68
69 wxFont *qFont = GetOCPNScaledFont(_("Dialog"));
70 SetFont(*qFont);
71 wxScreenDC dc;
72 int text_height;
73 dc.SetFont(*qFont);
74 dc.GetTextExtent("W", NULL, &text_height);
75 m_refTCWTextHeight = text_height;
76 }
77
78private:
79 void OnPaint(wxPaintEvent &event) {
80 wxPaintDC dc(this);
81
82 // Clear the background
83 dc.SetBackground(wxBrush(GetBackgroundColour()));
84 dc.Clear();
85
86 // Calculate chart rectangle within this panel
87 wxSize panelSize = GetClientSize();
88 if (panelSize.GetWidth() <= 0 || panelSize.GetHeight() <= 0) {
89 return;
90 }
91
92 // Use larger left margin for Y-axis labels and units
93 int left_margin = 50; // Space for Y-axis numbers and units
94 int other_margins = 5; // Smaller margins for top, right, bottom
95 int chart_width = panelSize.GetWidth() - left_margin - other_margins;
96 int chart_height = panelSize.GetHeight() - (2 * other_margins);
97
98 // Reserve space at bottom for date/time text
99 int bottom_text_space = 6 * m_refTCWTextHeight;
100 chart_height -= bottom_text_space;
101 chart_width = wxMax(chart_width, 300);
102 chart_height = wxMax(chart_height, 150);
103 wxRect chartRect(left_margin, other_margins, chart_width, chart_height);
104
105 // Delegate chart painting to parent TCWin
106 m_tcWin->PaintChart(dc, chartRect);
107 }
108
109 void OnMouseMove(wxMouseEvent &event) {
110 wxPoint panelPos = event.GetPosition();
111 wxPoint mainWindowPos = panelPos + GetPosition();
112 m_tcWin->HandleChartMouseMove(mainWindowPos.x, mainWindowPos.y, panelPos);
113 event.Skip();
114 }
115
116 TCWin *m_tcWin;
117 int m_refTCWTextHeight;
118};
119
120enum { ID_TCWIN_NX, ID_TCWIN_PR };
121
122enum { TIDE_PLOT, CURRENT_PLOT };
123
124BEGIN_EVENT_TABLE(TCWin, wxWindow)
125EVT_PAINT(TCWin::OnPaint)
126EVT_SIZE(TCWin::OnSize)
127EVT_MOTION(TCWin::MouseEvent)
128EVT_BUTTON(wxID_OK, TCWin::OKEvent)
129EVT_BUTTON(ID_TCWIN_NX, TCWin::NXEvent)
130EVT_BUTTON(ID_TCWIN_PR, TCWin::PREvent)
131EVT_CLOSE(TCWin::OnCloseWindow)
132EVT_TIMER(TCWININF_TIMER, TCWin::OnTCWinPopupTimerEvent)
133EVT_TIMER(TCWIN_TIME_INDICATOR_TIMER, TCWin::OnTimeIndicatorTimer)
134END_EVENT_TABLE()
135
136// Define a constructor
137TCWin::TCWin(ChartCanvas *parent, int x, int y, void *pvIDX) {
138 m_created = false;
139 xSpot = 0;
140 ySpot = 0;
141
142 m_pTCRolloverWin = NULL;
143
144 long wstyle = wxCLIP_CHILDREN | wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER |
145 wxFRAME_FLOAT_ON_PARENT;
146
147 pParent = parent;
148 m_x = x;
149 m_y = y;
150
151 RecalculateSize();
152
153 // Read the config file to get the user specified time zone.
154 if (pConfig) {
155 pConfig->SetPath("/Settings/Others");
156 pConfig->Read("TCWindowTimeZone", &m_tzoneDisplay, 0);
157 }
158
159 wxFrame::Create(parent, wxID_ANY, wxString(""), m_position, m_tc_size,
160 wstyle);
161
162 m_created = true;
163 wxFont *qFont = GetOCPNScaledFont(_("Dialog"));
164 SetFont(*qFont);
165
166 pIDX = (IDX_entry *)pvIDX;
167
168 // Set up plot type
169 if (strchr("Tt", pIDX->IDX_type)) {
170 m_plot_type = TIDE_PLOT;
171 SetTitle(wxString(_("Tide")));
172
173 } else {
174 m_plot_type = CURRENT_PLOT;
175 SetTitle(wxString(_("Current")));
176 }
177
178 int sx, sy;
179 GetClientSize(&sx, &sy);
180
181 SetTimeFactors();
182
183 btc_valid = false;
184
185 // Establish a "reference" text hieght value, for layout assistance
186 wxScreenDC dc;
187 int text_height;
188 dc.SetFont(*qFont);
189 dc.GetTextExtent("W", NULL, &text_height);
190 m_refTextHeight = text_height;
191
192 CreateLayout();
193 Layout();
194 m_graph_rect = wxRect(0, 0, 400, 200);
195
196 // Measure the size of a generic button, with label
197 wxButton *test_button =
198 new wxButton(this, wxID_OK, _("OK"), wxPoint(-1, -1), wxDefaultSize);
199 test_button->GetSize(&m_tsx, &m_tsy);
200 delete test_button;
201
202 m_TCWinPopupTimer.SetOwner(this, TCWININF_TIMER);
203
204 // Timer for refreshing time indicators (red line moves with current time)
205 m_TimeIndicatorTimer.SetOwner(this, TCWIN_TIME_INDICATOR_TIMER);
206 m_TimeIndicatorTimer.Start(60000, false); // Refresh every 60 seconds
207
208 m_button_height = m_tsy;
209
210 // Build graphics tools
211
212 wxFont *dlg_font = FontMgr::Get().GetFont(_("Dialog"));
213 int dlg_font_size = dlg_font->GetPointSize();
214#if defined(__WXOSX__) || defined(__WXGTK3__)
215 // Support scaled HDPI displays.
216 dlg_font_size /= GetContentScaleFactor();
217#endif
218
219 pSFont = FontMgr::Get().FindOrCreateFont(
220 dlg_font_size - 2, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL,
221 wxFONTWEIGHT_NORMAL, FALSE, wxString("Arial"));
222 pSMFont = FontMgr::Get().FindOrCreateFont(
223 dlg_font_size - 1, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL,
224 wxFONTWEIGHT_NORMAL, FALSE, wxString("Arial"));
225 pMFont = FontMgr::Get().FindOrCreateFont(
226 dlg_font_size, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD,
227 FALSE, wxString("Arial"));
228 pLFont = FontMgr::Get().FindOrCreateFont(
229 dlg_font_size + 1, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL,
230 wxFONTWEIGHT_BOLD, FALSE, wxString("Arial"));
231
232 // Secondary grid
233 pblack_1 = wxThePenList->FindOrCreatePen(
234 this->GetForegroundColour(), wxMax(1, (int)(m_tcwin_scaler + 0.5)),
235 wxPENSTYLE_SOLID);
236 // Primary grid
237 pblack_2 = wxThePenList->FindOrCreatePen(
238 this->GetForegroundColour(), wxMax(2, (int)(2 * m_tcwin_scaler + 0.5)),
239 wxPENSTYLE_SOLID);
240 // Tide hours outline
241 pblack_3 = wxThePenList->FindOrCreatePen(
242 wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW),
243 wxMax(1, (int)(m_tcwin_scaler + 0.5)), wxPENSTYLE_SOLID);
244 // System time vertical line - solid red line showing current system time
245 // position on tide/current chart
246 pred_2 = wxThePenList->FindOrCreatePen(
247 wxColor(230, 54, 54), wxMax(4, (int)(4 * m_tcwin_scaler + 0.5)),
248 wxPENSTYLE_SOLID);
249 // Selected time vertical line - dotted blue line showing timeline widget or
250 // GRIB time selection on chart
251 pred_time = wxThePenList->FindOrCreatePen(
252 wxColour(0, 100, 255), wxMax(4, (int)(4 * m_tcwin_scaler + 0.5)),
253 wxPENSTYLE_DOT);
254 // Graph background
255 pltgray = wxTheBrushList->FindOrCreateBrush(this->GetBackgroundColour(),
256 wxBRUSHSTYLE_SOLID);
257 // Tide hours background
258 pltgray2 = wxTheBrushList->FindOrCreateBrush(this->GetBackgroundColour(),
259 wxBRUSHSTYLE_SOLID);
260 pgraph = wxThePenList->FindOrCreatePen(
261 wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT),
262 wxMax(1, (int)(m_tcwin_scaler + 0.5)), wxPENSTYLE_SOLID);
263
264 DimeControl(this);
265
266 // Initialize the station text now that fonts are available
267 InitializeStationText();
268}
269
270TCWin::TCWin(AbstractChartCanvas *parent, int x, int y, void *pvIDX)
271 : TCWin(dynamic_cast<ChartCanvas *>(parent), x, y, pvIDX) {}
272
273TCWin::~TCWin() {
274 m_TimeIndicatorTimer.Stop();
275 pParent->Refresh(false);
276}
277
278void TCWin::CreateLayout() {
279 // Create main sizer
280 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
281
282 // ROW 1: Top panel for station info and tide list (two cells)
283 m_topPanel = new wxPanel(this, wxID_ANY);
284 wxBoxSizer *topSizer = new wxBoxSizer(wxHORIZONTAL);
285 m_topPanel->SetSizer(topSizer);
286
287 // Left cell: Station info text control with minimum size
288 m_ptextctrl =
289 new wxTextCtrl(m_topPanel, -1, "", wxDefaultPosition, wxDefaultSize,
290 wxTE_MULTILINE | wxTE_READONLY | wxTE_DONTWRAP);
291 m_ptextctrl->SetMinSize(wxSize(0, 8 * m_refTextHeight));
292 topSizer->Add(m_ptextctrl, 60, wxALL, 5); // 2/3 of width
293
294 // Right cell: Tide list (LW/HW) with minimum size
295 m_tList = new wxListCtrl(m_topPanel, -1, wxDefaultPosition, wxDefaultSize,
296 wxLC_REPORT | wxLC_NO_HEADER);
297 m_tList->SetMinSize(
298 wxSize(0, 8 * m_refTextHeight)); // Minimum to show a few entries
299 topSizer->Add(m_tList, 40, wxALL, 5); // 1/3 of width
300
301 // Add first column to tide list
302 wxListItem col0;
303 col0.SetId(0);
304 col0.SetText("");
305 col0.SetAlign(wxLIST_FORMAT_LEFT);
306 col0.SetWidth(20 * m_refTextHeight);
307 m_tList->InsertColumn(0, col0);
308
309 // ROW 2: Chart panel (expandable - gets remaining space)
310 m_chartPanel = new TideChartPanel(this);
311
312 // ROW 3: Button panel (fixed height at bottom)
313 m_buttonPanel = new wxPanel(this, wxID_ANY);
314 wxBoxSizer *buttonSizer = new wxBoxSizer(wxHORIZONTAL);
315
316 // Create buttons
317 PR_button = new wxButton(m_buttonPanel, ID_TCWIN_PR, _("Prev"));
318 NX_button = new wxButton(m_buttonPanel, ID_TCWIN_NX, _("Next"));
319 OK_button = new wxButton(m_buttonPanel, wxID_OK, _("OK"));
320
321 // Create timezone choice
322 wxString choiceOptions[] = {_("LMT@Station"), _("UTC")};
323 int numChoices = sizeof(choiceOptions) / sizeof(wxString);
324 m_choiceTimezone = new wxChoice(m_buttonPanel, wxID_ANY, wxDefaultPosition,
325 wxDefaultSize, numChoices, choiceOptions);
326 m_choiceTimezone->SetSelection(m_tzoneDisplay);
327 m_choiceTimezone->SetToolTip(
328 _("Select whether tide times are shown in UTC or Local Mean Time (LMT) "
329 "at the station"));
330
331 // Layout buttons: Prev/Next on left, timezone/OK on right
332 buttonSizer->Add(PR_button, 0, wxALL, 5);
333 buttonSizer->Add(NX_button, 0, wxALL, 5);
334 buttonSizer->AddStretchSpacer(1); // Push timezone and OK to the right
335 buttonSizer->Add(m_choiceTimezone, 0, wxALL | wxALIGN_CENTER_VERTICAL, 5);
336 buttonSizer->AddSpacer(10); // Small space between timezone and OK
337 buttonSizer->Add(OK_button, 0, wxALL, 5);
338
339 m_buttonPanel->SetSizer(buttonSizer);
340
341 // Add all rows to main sizer with proper proportions
342 mainSizer->Add(m_topPanel, 0, wxEXPAND | wxLEFT | wxRIGHT | wxTOP,
343 5); // Row 1: Fixed height, no overlap
344 mainSizer->Add(m_chartPanel, 1, wxEXPAND | wxLEFT | wxRIGHT,
345 5); // Row 2: Expandable, gets remaining space
346 mainSizer->Add(m_buttonPanel, 0, wxEXPAND | wxALL,
347 5); // Row 3: Fixed height at bottom
348
349 // Set the main sizer
350 SetSizer(mainSizer);
351
352 // Connect timezone choice event
353 m_choiceTimezone->Connect(wxEVT_COMMAND_CHOICE_SELECTED,
354 wxCommandEventHandler(TCWin::TimezoneOnChoice),
355 NULL, this);
356}
357
358void TCWin::InitializeStationText() {
359 // Fill station information in text control
360 m_ptextctrl->Clear();
361
362 wxString locn(pIDX->IDX_station_name, wxConvUTF8);
363 wxString locna, locnb;
364 if (locn.Contains(wxString(","))) {
365 locna = locn.BeforeFirst(',');
366 locnb = locn.AfterFirst(',');
367 } else {
368 locna = locn;
369 locnb.Empty();
370 }
371
372 // write the first line
373 wxTextAttr style;
374 style.SetFont(*pLFont);
375 m_ptextctrl->SetDefaultStyle(style);
376
377 m_ptextctrl->AppendText(locna);
378 m_ptextctrl->AppendText("\n");
379
380 style.SetFont(*pSMFont);
381 m_ptextctrl->SetDefaultStyle(style);
382
383 if (!locnb.IsEmpty()) m_ptextctrl->AppendText(locnb);
384 m_ptextctrl->AppendText("\n");
385
386 // Reference to the master station
387 if (('t' == pIDX->IDX_type) || ('c' == pIDX->IDX_type)) {
388 wxString mref(pIDX->IDX_reference_name, wxConvUTF8);
389 mref.Prepend(" ");
390
391 m_ptextctrl->AppendText(_("Reference Station :"));
392 m_ptextctrl->AppendText("\n");
393
394 m_ptextctrl->AppendText(mref);
395 m_ptextctrl->AppendText("\n");
396
397 } else {
398 m_ptextctrl->AppendText("\n");
399 }
400
401 // Show the data source
402 wxString dsource(pIDX->source_ident, wxConvUTF8);
403 dsource.Prepend(" ");
404
405 m_ptextctrl->AppendText(_("Data Source :"));
406 m_ptextctrl->AppendText("\n");
407
408 m_ptextctrl->AppendText(dsource);
409
410 m_ptextctrl->SetInsertionPoint(0);
411 m_ptextctrl->ShowPosition(0);
412}
413
414void TCWin::PaintChart(wxDC &dc, const wxRect &chartRect) {
415 if (!IsShown()) {
416 return;
417 }
418
419 // Store the original graph rectangle and use the provided chartRect
420 wxRect originalGraphRect = m_graph_rect;
421 m_graph_rect = chartRect;
422
423 int i;
424 char sbuf[100];
425 int w;
426 float tcmax, tcmin;
427
428 if (m_graph_rect.x == 0) {
429 m_graph_rect = originalGraphRect;
430 return;
431 }
432
433 // Get client size for positioning date/timezone text below chart
434 int x, y;
435 GetClientSize(&x, &y);
436
437 // Adjust colors with current color scheme
438 pblack_1->SetColour(this->GetForegroundColour());
439 pblack_2->SetColour(this->GetForegroundColour());
440 pltgray->SetColour(this->GetBackgroundColour());
441 pltgray2->SetColour(this->GetBackgroundColour());
442 pred_2->SetColour(
443 GetGlobalColor("URED")); // System time indicator - universal red
444 pred_time->SetColour(
445 GetGlobalColor("UINFB")); // Selected time indicator - information blue
446
447 // Box the graph
448 dc.SetPen(*pblack_1);
449 dc.SetBrush(*pltgray);
450 dc.DrawRectangle(m_graph_rect.x, m_graph_rect.y, m_graph_rect.width,
451 m_graph_rect.height);
452
453 // On some platforms, we cannot draw rotated text.
454 // So, reduce the complexity of horizontal axis time labels
455#ifndef __WXMSW__
456 const int hour_delta = 4;
457#else
458 const int hour_delta = 1;
459#endif
460
461 int hour_start = 0;
462
463 // Horizontal axis
464 dc.SetFont(*pSFont);
465 for (i = 0; i < 25; i++) {
466 int xd = m_graph_rect.x + ((i)*m_graph_rect.width / 25);
467 if (hour_delta != 1) {
468 if (i % hour_delta == 0) {
469 dc.SetPen(*pblack_2);
470 dc.DrawLine(xd, m_graph_rect.y, xd,
471 m_graph_rect.y + m_graph_rect.height + 5);
472 char sbuf[16];
473 int hour_show = hour_start + i;
474 if (hour_show >= 24) hour_show -= 24;
475 sprintf(sbuf, "%02d", hour_show);
476 int x_shim = -20;
477 dc.DrawText(wxString(sbuf, wxConvUTF8),
478 xd + x_shim + (m_graph_rect.width / 25) / 2,
479 m_graph_rect.y + m_graph_rect.height + 8);
480 } else {
481 dc.SetPen(*pblack_1);
482 dc.DrawLine(xd, m_graph_rect.y, xd,
483 m_graph_rect.y + m_graph_rect.height + 5);
484 }
485 } else {
486 dc.SetPen(*pblack_1);
487 dc.DrawLine(xd, m_graph_rect.y, xd,
488 m_graph_rect.y + m_graph_rect.height + 5);
489 wxString sst;
490 sst.Printf("%02d", i);
491 dc.DrawRotatedText(sst, xd + (m_graph_rect.width / 25) / 2,
492 m_graph_rect.y + m_graph_rect.height + 8, 270.);
493 }
494 }
495
496 // Time indicators - system time and "selected" time (e.g. GRIB time)
497 wxDateTime system_now = wxDateTime::Now();
498 wxDateTime this_now = gTimeSource;
499 bool cur_time = !gTimeSource.IsValid();
500 if (cur_time) this_now = wxDateTime::Now();
501
502 // Always draw system time indicator (solid red line)
503 time_t t_system_now = system_now.GetTicks();
504 t_system_now -= m_diff_mins * 60;
505 if (m_tzoneDisplay == 0) // LMT @ Station
506 t_system_now += m_stationOffset_mins * 60;
507
508 float t_system_ratio =
509 m_graph_rect.width * (t_system_now - m_t_graphday_GMT) / (25 * 3600.0f);
510 int x_system = (t_system_ratio < 0 || t_system_ratio > m_graph_rect.width)
511 ? -1
512 : m_graph_rect.x + (int)t_system_ratio;
513
514 if (x_system >= 0) {
515 dc.SetPen(*pred_2); // solid red line for system time
516 dc.DrawLine(x_system, m_graph_rect.y, x_system,
517 m_graph_rect.y + m_graph_rect.height);
518 }
519
520 // Draw "selected time" indicator (from timeline widget) if different from
521 // system time.
522 if (gTimeSource.IsValid()) {
523 time_t t_selected_time = gTimeSource.GetTicks();
524 if (abs(t_selected_time - t_system_now) > 300) {
525 t_selected_time -= m_diff_mins * 60;
526 if (m_tzoneDisplay == 0) // LMT @ Station
527 t_selected_time += m_stationOffset_mins * 60;
528
529 float t_selected_time_ratio = m_graph_rect.width *
530 (t_selected_time - m_t_graphday_GMT) /
531 (25 * 3600.0f);
532 int x_selected_time = (t_selected_time_ratio < 0 ||
533 t_selected_time_ratio > m_graph_rect.width)
534 ? -1
535 : m_graph_rect.x + (int)t_selected_time_ratio;
536
537 if (x_selected_time >= 0) {
538 dc.SetPen(*pred_time);
539 dc.DrawLine(x_selected_time, m_graph_rect.y, x_selected_time,
540 m_graph_rect.y + m_graph_rect.height);
541 }
542 }
543 }
544 dc.SetPen(*pblack_1);
545
546 // Build the array of values, capturing max and min and HW/LW list
547 if (!btc_valid) {
548 float dir;
549 tcmax = -10;
550 tcmin = 10;
551 float val = -100;
552 m_tList->DeleteAllItems();
553 int list_index = 0;
554 bool wt = false;
555
556 wxBeginBusyCursor();
557
558 // The tide/current modules calculate values based on PC local time
559 // We want UTC, so adjust accordingly
560 int tt_localtz = m_t_graphday_GMT + (m_diff_mins * 60);
561 // then eventually we could need LMT at station
562 if (m_tzoneDisplay == 0)
563 tt_localtz -= m_stationOffset_mins * 60; // LMT at station
564
565 // get tide flow sens ( flood or ebb ? )
566 ptcmgr->GetTideFlowSens(tt_localtz, BACKWARD_TEN_MINUTES_STEP,
567 pIDX->IDX_rec_num, tcv[0], val, wt);
568
569 for (i = 0; i < 26; i++) {
570 int tt = tt_localtz + (i * FORWARD_ONE_HOUR_STEP);
571 ptcmgr->GetTideOrCurrent(tt, pIDX->IDX_rec_num, tcv[i], dir);
572 tt_tcv[i] = tt; // store the corresponding time_t value
573 float tcvalue_i = tcv[i]; // unconverted value
574
575 // Convert tide values from station units to user's height units
576 Station_Data *pmsd = pIDX->pref_sta_data;
577 if (pmsd) {
578 // Convert from station units to meters first
579 int unit_c = TCDataFactory::findunit(pmsd->unit);
580 if (unit_c >= 0) {
581 tcv[i] = tcv[i] * TCDataFactory::known_units[unit_c].conv_factor;
582 }
583 // Now convert from meters to preferred height units
584 if (CURRENT_PLOT == m_plot_type)
585 tcv[i] = toUsrSpeed(tcv[i]);
586 else
587 tcv[i] = toUsrHeight(tcv[i]);
588 }
589
590 if (tcv[i] > tcmax) tcmax = tcv[i];
591 if (tcv[i] < tcmin) tcmin = tcv[i];
592
593 if (TIDE_PLOT == m_plot_type) {
594 if (!((tcv[i] > val) == wt) && (i > 0)) { // if tide flow sense change
595 float tcvalue; // look backward for HW or LW
596 time_t tctime;
597 ptcmgr->GetHightOrLowTide(tt, BACKWARD_TEN_MINUTES_STEP,
598 BACKWARD_ONE_MINUTES_STEP, tcvalue_i, wt,
599 pIDX->IDX_rec_num, tcvalue, tctime);
600 if (tctime > tt_localtz) { // Only show events visible in graphic
601 // presently shown
602 wxDateTime tcd; // write date
603 wxString s, s1;
604 tcd.Set(tctime - (m_diff_mins * 60));
605 if (m_tzoneDisplay == 0) // LMT @ Station
606 tcd.Set(tctime + (m_stationOffset_mins - m_diff_mins) * 60);
607
608 s.Printf(tcd.Format("%H:%M "));
609
610 // Convert tcvalue to preferred height units (it comes from
611 // GetHightOrLowTide in station units)
612 double tcvalue_converted = tcvalue;
613 Station_Data *pmsd = pIDX->pref_sta_data;
614 if (pmsd) {
615 // Convert from station units to meters first
616 int unit_c = TCDataFactory::findunit(pmsd->unit);
617 if (unit_c >= 0) {
618 tcvalue_converted =
619 tcvalue_converted *
620 TCDataFactory::known_units[unit_c].conv_factor;
621 }
622 // Now convert from meters to preferred height units
623 if (CURRENT_PLOT == m_plot_type)
624 tcvalue_converted = toUsrSpeed(tcvalue_converted);
625 else
626 tcvalue_converted = toUsrHeight(tcvalue_converted);
627 }
628
629 s1.Printf("%05.2f ", tcvalue_converted); // write converted value
630 s.Append(s1);
631 s.Append(getUsrHeightUnit());
632 s.Append(" ");
633 (wt) ? s.Append(_("HW")) : s.Append(_("LW")); // write HW or LT
634
635 wxListItem li;
636 li.SetId(list_index);
637 li.SetAlign(wxLIST_FORMAT_LEFT);
638 li.SetText(s);
639 li.SetColumn(0);
640 m_tList->InsertItem(li);
641 list_index++;
642 }
643 wt = !wt; // change tide flow sens
644 }
645 val = tcv[i];
646 }
647 if (CURRENT_PLOT == m_plot_type) {
648 wxDateTime thx; // write date
649 wxString s, s1;
650 thx.Set((time_t)tt - (m_diff_mins * 60));
651 if (m_tzoneDisplay == 0) // LMT @ Station
652 thx.Set((time_t)tt + (m_stationOffset_mins - m_diff_mins) * 60);
653
654 s.Printf(thx.Format("%H:%M "));
655 s1.Printf("%05.2f ",
656 fabs(tcv[i])); // tcv[i] is already converted to height units
657 s.Append(s1);
658 s.Append(getUsrSpeedUnit());
659 s1.Printf(" %03.0f", dir); // write direction
660 s.Append(s1);
661
662 wxListItem li;
663 li.SetId(list_index);
664 li.SetAlign(wxLIST_FORMAT_LEFT);
665 li.SetText(s);
666 li.SetColumn(0);
667 m_tList->InsertItem(li);
668 list_index++;
669 }
670 }
671
672 wxEndBusyCursor();
673
674 // Set up the vertical parameters based on Tide or Current plot
675 if (CURRENT_PLOT == m_plot_type) {
676 it = std::max(abs((int)tcmin - 1), abs((int)tcmax + 1));
677 ib = -it;
678 im = 2 * it;
679 m_plot_y_offset = m_graph_rect.height / 2;
680 val_off = 0;
681 } else {
682 ib = (int)tcmin;
683 if (tcmin < 0) ib -= 1;
684 it = (int)tcmax + 1;
685 im = it - ib;
686 m_plot_y_offset = (m_graph_rect.height * (it - ib)) / im;
687 val_off = ib;
688 }
689
690 // Arrange to skip some lines and legends if there are too many for the
691 // vertical space we have
692 int height_stext;
693 dc.GetTextExtent("1", NULL, &height_stext);
694 float available_lines = (float)m_graph_rect.height / height_stext;
695 i_skip = (int)ceil(im / available_lines);
696
697 if (CURRENT_PLOT == m_plot_type && i_skip != 1) {
698 // Adjust steps so slack current "0" line is always drawn on graph
699 ib -= it % i_skip;
700 it = -ib;
701 im = 2 * it;
702 }
703
704 // Build spline list of points
705 for (auto it = m_sList.begin(); it != m_sList.end(); it++) delete (*it);
706 m_sList.clear();
707
708 for (i = 0; i < 26; i++) {
709 wxPoint *pp = new wxPoint;
710 pp->x = m_graph_rect.x + ((i)*m_graph_rect.width / 25);
711 pp->y = m_graph_rect.y + (m_plot_y_offset) -
712 (int)((tcv[i] - val_off) * m_graph_rect.height / im);
713 m_sList.push_back(pp);
714 }
715
716 btc_valid = true;
717 }
718
719 // Graph legend
720 dc.SetTextForeground(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
721
722 // Vertical Axis
723 i = ib;
724 while (i < it + 1) {
725 int yd = m_graph_rect.y + (m_plot_y_offset) -
726 ((i - val_off) * m_graph_rect.height / im);
727 if ((m_plot_y_offset + m_graph_rect.y) == yd)
728 dc.SetPen(*pblack_2);
729 else
730 dc.SetPen(*pblack_1);
731
732 dc.DrawLine(m_graph_rect.x, yd, m_graph_rect.x + m_graph_rect.width, yd);
733 if ((yd < m_graph_rect.height * 48 / 100) ||
734 (yd > m_graph_rect.height * 52 / 100)) {
735 snprintf(sbuf, 99, "%d", i);
736 dc.DrawText(wxString(sbuf, wxConvUTF8), m_graph_rect.x - 20, yd - 5);
737 }
738 i += i_skip;
739 }
740
741 // Draw the Value curve
742 wxPointList list;
743 for (auto &p : m_sList) list.Append(p);
744
745 dc.SetPen(*pgraph);
746#if wxUSE_SPLINES
747 dc.DrawSpline(&list);
748#else
749 dc.DrawLines(&list);
750#endif
751
752 // More Info - positioned below chart panel
753 if (m_tzoneDisplay == 0) {
754 int station_offset = ptcmgr->GetStationTimeOffset(pIDX);
755 int h = station_offset / 60;
756 int m = station_offset - (h * 60);
757 if (m_graphday.IsDST()) h += 1;
758 m_stz.Printf("UTC %+03d:%02d", h, m);
759
760 // Make the "nice" (for the US) station time-zone string, brutally by
761 // hand
762 double lat = ptcmgr->GetStationLat(pIDX);
763 if (lat > 20.0) {
764 wxString mtz;
765 switch (ptcmgr->GetStationTimeOffset(pIDX)) {
766 case -240:
767 mtz = "AST";
768 break;
769 case -300:
770 mtz = "EST";
771 break;
772 case -360:
773 mtz = "CST";
774 break;
775 }
776 if (mtz.Len()) {
777 if (m_graphday.IsDST()) mtz[1] = 'D';
778 m_stz = mtz;
779 }
780 }
781 } else {
782 m_stz = "UTC";
783 }
784
785 int h;
786 dc.SetFont(*pSFont);
787 dc.GetTextExtent(m_stz, &w, &h);
788 // Position timezone text below the chart, centered horizontally
789 dc.DrawText(m_stz, m_graph_rect.x + (m_graph_rect.width / 2) - (w / 2),
790 m_graph_rect.y + m_graph_rect.height + (2 * h));
791
792 wxString sdate;
793 if (g_locale == "en_US")
794 sdate = m_graphday.Format("%A %b %d, %Y");
795 else
796 sdate = m_graphday.Format("%A %d %b %Y");
797
798 dc.SetFont(*pMFont);
799 dc.GetTextExtent(sdate, &w, &h);
800 // Position date text below the chart, centered horizontally
801 dc.DrawText(sdate, m_graph_rect.x + (m_graph_rect.width / 2) - (w / 2),
802 m_graph_rect.y + m_graph_rect.height + (5 * h / 2));
803
804 Station_Data *pmsd = pIDX->pref_sta_data;
805 if (pmsd) {
806 if (CURRENT_PLOT == m_plot_type) {
807 // Use user's speed unit for Y-axis label instead of station units
808 wxString speed_unit = getUsrSpeedUnit();
809 dc.GetTextExtent(speed_unit, &w, &h);
810 dc.DrawRotatedText(speed_unit, 0,
811 m_graph_rect.y + m_graph_rect.height / 2 + w / 2, 90.);
812 } else {
813 // Use user's height unit for Y-axis label instead of station units
814 wxString height_unit = getUsrHeightUnit();
815 dc.GetTextExtent(height_unit, &w, &h);
816 dc.DrawRotatedText(height_unit, 0,
817 m_graph_rect.y + m_graph_rect.height / 2 + w / 2, 90.);
818 }
819 }
820
821 // Show flood and ebb directions
822 if ((strchr("c", pIDX->IDX_type)) || (strchr("C", pIDX->IDX_type))) {
823 dc.SetFont(*pSFont);
824 wxString fdir;
825 fdir.Printf("%03d", pIDX->IDX_flood_dir);
826 dc.DrawText(fdir, m_graph_rect.x + m_graph_rect.width + 4,
827 m_graph_rect.y + m_graph_rect.height * 1 / 4);
828
829 wxString edir;
830 edir.Printf("%03d", pIDX->IDX_ebb_dir);
831 dc.DrawText(edir, m_graph_rect.x + m_graph_rect.width + 4,
832 m_graph_rect.y + m_graph_rect.height * 3 / 4);
833 }
834
835 // Today or tomorrow
836 if ((m_button_height * 15) < x && cur_time) { // large enough horizontally?
837 wxString sday;
838 int day = m_graphday.GetDayOfYear();
839 if (m_graphday.GetYear() == this_now.GetYear()) {
840 if (day == this_now.GetDayOfYear())
841 sday.Append(_("Today"));
842 else if (day == this_now.GetDayOfYear() + 1)
843 sday.Append(_("Tomorrow"));
844 else
845 sday.Append(m_graphday.GetWeekDayName(m_graphday.GetWeekDay()));
846 } else if (m_graphday.GetYear() == this_now.GetYear() + 1 &&
847 day == this_now.Add(wxTimeSpan::Day()).GetDayOfYear())
848 sday.Append(_("Tomorrow"));
849
850 dc.SetFont(*pSFont);
851 dc.GetTextExtent(sday, &w, &h);
852 // Position day text at the left side of the chart, below it
853 dc.DrawText(sday, m_graph_rect.x,
854 m_graph_rect.y + m_graph_rect.height + (2 * h));
855 }
856
857 // Render "Spot of interest"
858 double spotDim = 4 * g_Platform->GetDisplayDPmm();
859 dc.SetBrush(*wxTheBrushList->FindOrCreateBrush(GetGlobalColor("YELO1"),
860 wxBRUSHSTYLE_SOLID));
861 dc.SetPen(wxPen(GetGlobalColor("URED"),
862 wxMax(2, 0.5 * g_Platform->GetDisplayDPmm())));
863 dc.DrawRoundedRectangle(xSpot - spotDim / 2, ySpot - spotDim / 2, spotDim,
864 spotDim, spotDim / 2);
865
866 dc.SetBrush(*wxTheBrushList->FindOrCreateBrush(GetGlobalColor("UBLCK"),
867 wxBRUSHSTYLE_SOLID));
868 dc.SetPen(wxPen(GetGlobalColor("UBLCK"), 1));
869 double ispotDim = spotDim / 5.;
870 dc.DrawRoundedRectangle(xSpot - ispotDim / 2, ySpot - ispotDim / 2, ispotDim,
871 ispotDim, ispotDim / 2);
872
873 // Restore original graph rectangle
874 m_graph_rect = originalGraphRect;
875}
876
877void TCWin::SetTimeFactors() {
878 // Figure out this computer timezone minute offset
879 wxDateTime this_now = gTimeSource;
880 bool cur_time = !gTimeSource.IsValid();
881
882 if (cur_time) {
883 this_now = wxDateTime::Now();
884 }
885 wxDateTime this_gmt = this_now.ToGMT();
886
887#if wxCHECK_VERSION(2, 6, 2)
888 wxTimeSpan diff = this_now.Subtract(this_gmt);
889#else
890 wxTimeSpan diff = this_gmt.Subtract(this_now);
891#endif
892
893 m_diff_mins = diff.GetMinutes();
894
895 // Correct a bug in wx3.0.2
896 // If the system TZ happens to be GMT, with DST active (e.g.summer in
897 // London), then wxDateTime returns incorrect results for toGMT() method
898#if wxCHECK_VERSION(3, 0, 2)
899 if (m_diff_mins == 0 && this_now.IsDST()) m_diff_mins += 60;
900#endif
901
902 int station_offset = ptcmgr->GetStationTimeOffset(pIDX);
903
904 m_stationOffset_mins = station_offset;
905 if (this_now.IsDST()) {
906 m_stationOffset_mins += 60;
907 }
908
909 // Correct a bug in wx3.0.2
910 // If the system TZ happens to be GMT, with DST active (e.g.summer in
911 // London), then wxDateTime returns incorrect results for toGMT() method
912#if wxCHECK_VERSION(3, 0, 2)
913// if( this_now.IsDST() )
914// m_corr_mins +=60;
915#endif
916
917 // Establish the inital drawing day as today, in the timezone of the
918 // station
919 m_graphday = this_gmt;
920
921 int day_gmt = this_gmt.GetDayOfYear();
922
923 time_t ttNow = this_now.GetTicks();
924 time_t tt_at_station =
925 ttNow - (m_diff_mins * 60) + (m_stationOffset_mins * 60);
926 wxDateTime atStation(tt_at_station);
927 int day_at_station = atStation.GetDayOfYear();
928
929 if (day_gmt > day_at_station) {
930 wxTimeSpan dt(24, 0, 0, 0);
931 m_graphday.Subtract(dt);
932 } else if (day_gmt < day_at_station) {
933 wxTimeSpan dt(24, 0, 0, 0);
934 m_graphday.Add(dt);
935 }
936
937 wxDateTime graphday_00 = m_graphday; // this_gmt;
938 graphday_00.ResetTime();
939 time_t t_graphday_00 = graphday_00.GetTicks();
940
941 // Correct a Bug in wxWidgets time support
942 // if( !graphday_00.IsDST() && m_graphday.IsDST() ) t_graphday_00 -= 3600;
943 // if( graphday_00.IsDST() && !m_graphday.IsDST() ) t_graphday_00 += 3600;
944
945 m_t_graphday_GMT = t_graphday_00;
946
947 btc_valid = false; // Force re-calculation
948}
949
950void TCWin::TimezoneOnChoice(wxCommandEvent &event) {
951 m_tzoneDisplay = m_choiceTimezone->GetSelection();
952 SetTimeFactors();
953 m_chartPanel->Refresh();
954 Refresh();
955}
956
957void TCWin::RecalculateSize() {
958 wxSize parent_size(2000, 2000);
959 if (pParent) parent_size = pParent->GetClientSize();
960
961 int unscaledheight = 600;
962 int unscaledwidth = 650;
963
964 // value of m_tcwin_scaler should be about unity on a 100 dpi display,
965 // when scale parameter g_tcwin_scale is 100
966 // parameter g_tcwin_scale is set in config file as value of
967 // TideCurrentWindowScale
968 g_tcwin_scale = wxMax(g_tcwin_scale, 10); // sanity check on g_tcwin_scale
969 m_tcwin_scaler = g_Platform->GetDisplayDPmm() * 0.254 * g_tcwin_scale / 100.0;
970
971 m_tc_size.x = (int)(unscaledwidth * m_tcwin_scaler + 0.5);
972 m_tc_size.y = (int)(unscaledheight * m_tcwin_scaler + 0.5);
973
974 m_tc_size.x = wxMin(m_tc_size.x, parent_size.x);
975 m_tc_size.y = wxMin(m_tc_size.y, parent_size.y);
976
977 int xc = m_x + 8;
978 int yc = m_y;
979
980 // Arrange for tcWindow to be always totally visible
981 // by shifting left and/or up
982 if ((m_x + 8 + m_tc_size.x) > parent_size.x) xc = xc - m_tc_size.x - 16;
983 if ((m_y + m_tc_size.y) > parent_size.y) yc = yc - m_tc_size.y;
984
985 // Don't let the window origin move out of client area
986 if (yc < 0) yc = 0;
987 if (xc < 0) xc = 0;
988
989 if (pParent) pParent->ClientToScreen(&xc, &yc);
990 m_position = wxPoint(xc, yc);
991
992 if (m_created) {
993 SetSize(m_tc_size);
994 Move(m_position);
995 }
996}
997
998void TCWin::OKEvent(wxCommandEvent &event) {
999 Hide();
1000
1001 // Ensure parent pointer is cleared before any potential deletion
1002 if (pParent && pParent->pCwin == this) {
1003 pParent->pCwin = NULL;
1004 }
1005
1006 // Clean up global tide window counter and associated resources
1007 --gpIDXn;
1008 delete m_pTCRolloverWin;
1009 m_pTCRolloverWin = NULL;
1010 delete m_tList;
1011 m_tList = NULL;
1012
1013 if (pParent) {
1014 pParent->Refresh(false);
1015 }
1016
1017 // Update the config file to set the user specified time zone.
1018 if (pConfig) {
1019 pConfig->SetPath("/Settings/Others");
1020 pConfig->Write("TCWindowTimeZone", m_tzoneDisplay);
1021 }
1022
1023 Destroy(); // that hurts
1024}
1025
1026void TCWin::OnCloseWindow(wxCloseEvent &event) {
1027 Hide();
1028
1029 // Ensure parent pointer is cleared before any potential deletion
1030 if (pParent && pParent->pCwin == this) {
1031 pParent->pCwin = NULL;
1032 }
1033
1034 // Clean up global tide window counter and associated resources
1035 --gpIDXn;
1036 delete m_pTCRolloverWin;
1037 m_pTCRolloverWin = NULL;
1038 delete m_tList;
1039 m_tList = NULL;
1040
1041 // Update the config file to set the user specified time zone.
1042 if (pConfig) {
1043 pConfig->SetPath("/Settings/Others");
1044 pConfig->Write("TCWindowTimeZone", m_tzoneDisplay);
1045 }
1046
1047 Destroy(); // that hurts
1048}
1049
1050void TCWin::NXEvent(wxCommandEvent &event) {
1051 wxTimeSpan dt(24, 0, 0, 0);
1052 m_graphday.Add(dt);
1053 wxDateTime dm = m_graphday;
1054
1055 wxDateTime graphday_00 = dm.ResetTime();
1056 time_t t_graphday_00 = graphday_00.GetTicks();
1057
1058 if (!graphday_00.IsDST() && m_graphday.IsDST()) t_graphday_00 -= 3600;
1059 if (graphday_00.IsDST() && !m_graphday.IsDST()) t_graphday_00 += 3600;
1060
1061 m_t_graphday_GMT = t_graphday_00;
1062
1063 btc_valid = false;
1064 m_chartPanel->Refresh();
1065 Refresh();
1066}
1067
1068void TCWin::PREvent(wxCommandEvent &event) {
1069 wxTimeSpan dt(-24, 0, 0, 0);
1070 m_graphday.Add(dt);
1071 wxDateTime dm = m_graphday;
1072
1073 wxDateTime graphday_00 = dm.ResetTime();
1074 time_t t_graphday_00 = graphday_00.GetTicks();
1075
1076 if (!graphday_00.IsDST() && m_graphday.IsDST()) t_graphday_00 -= 3600;
1077 if (graphday_00.IsDST() && !m_graphday.IsDST()) t_graphday_00 += 3600;
1078
1079 m_t_graphday_GMT = t_graphday_00;
1080
1081 btc_valid = false;
1082 m_chartPanel->Refresh();
1083 Refresh();
1084}
1085
1086void TCWin::RePosition() {
1087 // Position the window
1088 double lon = pIDX->IDX_lon;
1089 double lat = pIDX->IDX_lat;
1090
1091 wxPoint r;
1092 pParent->GetCanvasPointPix(lat, lon, &r);
1093 pParent->ClientToScreen(&r.x, &r.y);
1094 Move(r);
1095}
1096
1097void TCWin::OnPaint(wxPaintEvent &event) {
1098 if (!IsShown()) {
1099 return;
1100 }
1101
1102 // With the new sizer-based layout, the main OnPaint method is simplified.
1103 // Chart rendering is now handled by the TideChartPanel's OnPaint method,
1104 // which delegates to our PaintChart() method.
1105
1106 wxPaintDC dc(this);
1107
1108 // Clear the background
1109 dc.SetBrush(wxBrush(GetBackgroundColour()));
1110 dc.SetPen(wxPen(GetBackgroundColour()));
1111 wxSize size = GetClientSize();
1112 dc.DrawRectangle(0, 0, size.GetWidth(), size.GetHeight());
1113
1114 // Note: Chart painting is now handled by TideChartPanel::OnPaint()
1115 // which calls our PaintChart() method. This eliminates the need for
1116}
1117
1118void TCWin::OnSize(wxSizeEvent &event) {
1119 if (!m_created) return;
1120
1121 // With sizer-based layout, we don't need manual positioning.
1122 // The sizers automatically handle layout when the window is resized.
1123
1124 // Force chart panel to refresh with new size
1125 if (m_chartPanel) {
1126 m_chartPanel->Refresh();
1127 }
1128
1129 // Invalidate cached chart data to force recalculation
1130 btc_valid = false;
1131
1132 // Allow sizers to handle the layout
1133 event.Skip();
1134}
1135
1136void TCWin::MouseEvent(wxMouseEvent &event) {
1137 // This is now mainly for compatibility.
1138 // Chart mouse events are handled by HandleChartMouseMove
1139 event.GetPosition(&curs_x, &curs_y);
1140
1141 if (!m_TCWinPopupTimer.IsRunning())
1142 m_TCWinPopupTimer.Start(20, wxTIMER_ONE_SHOT);
1143}
1144
1145void TCWin::HandleChartMouseMove(int mainWindowX, int mainWindowY,
1146 const wxPoint &chartPanelPos) {
1147 // Store the main window coordinates for compatibility with existing rollover
1148 // code
1149 curs_x = mainWindowX;
1150 curs_y = mainWindowY;
1151
1152 // Also store the chart panel relative coordinates for calculations
1153 if (m_chartPanel) {
1154 // Calculate the chart rectangle within the chart panel
1155 wxSize panelSize = m_chartPanel->GetClientSize();
1156 int left_margin = 50; // Space for Y-axis numbers and units
1157 int other_margins = 5; // Smaller margins for top, right, bottom
1158 int chart_width = panelSize.GetWidth() - left_margin - other_margins;
1159 int chart_height = panelSize.GetHeight() - (2 * other_margins);
1160 int bottom_text_space = 50; // Increased space for date display
1161 chart_height -= bottom_text_space;
1162 chart_width = wxMax(chart_width, 300);
1163 chart_height = wxMax(chart_height, 150);
1164
1165 // Update the graph rectangle to match the current chart panel layout
1166 wxPoint chartPanelPos = m_chartPanel->GetPosition();
1167 m_graph_rect =
1168 wxRect(chartPanelPos.x + left_margin, chartPanelPos.y + other_margins,
1169 chart_width, chart_height);
1170 }
1171
1172 if (!m_TCWinPopupTimer.IsRunning())
1173 m_TCWinPopupTimer.Start(20, wxTIMER_ONE_SHOT);
1174}
1175
1176void TCWin::OnTCWinPopupTimerEvent(wxTimerEvent &event) {
1177 int x, y;
1178 bool ShowRollover;
1179
1180 GetClientSize(&x, &y);
1181 wxRegion cursorarea(m_graph_rect);
1182 if (cursorarea.Contains(curs_x, curs_y)) {
1183 ShowRollover = true;
1184 SetCursor(*pParent->pCursorCross);
1185 if (NULL == m_pTCRolloverWin) {
1186 m_pTCRolloverWin = new RolloverWin(this, -1, false);
1187 // doesn't really work, mouse positions are relative to rollover window
1188 // not this window.
1189 // effect: hide rollover window if mouse on rollover
1190 m_pTCRolloverWin->SetMousePropogation(1);
1191 m_pTCRolloverWin->Hide();
1192 }
1193 float t, d;
1194 wxString p, s;
1195
1196 // Calculate time based on actual chart rectangle position
1197 // t represents hours into the 25-hour display (0-25)
1198 float relativeX =
1199 (float)(curs_x - m_graph_rect.x) / (float)m_graph_rect.width;
1200 t = relativeX * 25.0f; // 25 hours displayed across the width
1201
1202 // Clamp to valid range
1203 t = wxMax(0.0f, wxMin(25.0f, t));
1204
1205 int tt = m_t_graphday_GMT + (int)(t * 3600);
1206 time_t ths = tt;
1207
1208 wxDateTime thd;
1209 thd.Set(ths);
1210 p.Printf(thd.Format("%Hh %Mmn"));
1211 p.Append("\n");
1212
1213 // The tide/current modules calculate values based on PC local time
1214 // We want UTC, so adjust accordingly
1215 int tt_localtz = m_t_graphday_GMT + (m_diff_mins * 60);
1216
1217 int ttv = tt_localtz + (int)(t * 3600);
1218 if (m_tzoneDisplay == 0) {
1219 ttv -= m_stationOffset_mins * 60; // LMT at station
1220 }
1221
1222 time_t tts = ttv;
1223
1224 // set tide level or current speed at that time
1225 ptcmgr->GetTideOrCurrent(tts, pIDX->IDX_rec_num, t, d);
1226
1227 // Convert tide/current value to preferred height units
1228 double t_converted = (t < 0 && CURRENT_PLOT == m_plot_type) ? -t : t;
1229 Station_Data *pmsd = pIDX->pref_sta_data;
1230 if (pmsd) {
1231 // Convert from station units to meters first
1232 int unit_c = TCDataFactory::findunit(pmsd->unit);
1233 if (unit_c >= 0) {
1234 t_converted =
1235 t_converted * TCDataFactory::known_units[unit_c].conv_factor;
1236 }
1237 // Now convert from meters to preferred height units
1238 if (CURRENT_PLOT == m_plot_type)
1239 t_converted = toUsrSpeed(t_converted);
1240 else
1241 t_converted = toUsrHeight(t_converted);
1242 }
1243
1244 s.Printf("%3.2f ", t_converted);
1245 p.Append(s);
1246
1247 // set unit - use preferred speed/height unit abbreviation
1248 if (CURRENT_PLOT == m_plot_type)
1249 p.Append(getUsrSpeedUnit());
1250 else
1251 p.Append(getUsrHeightUnit());
1252
1253 // set current direction
1254 if (CURRENT_PLOT == m_plot_type) {
1255 s.Printf("%3.0f%c", d, 0x00B0);
1256 p.Append("\n");
1257 p.Append(s);
1258 }
1259
1260 // set rollover area size
1261 wxSize win_size;
1262 win_size.Set(x * 90 / 100, y * 80 / 100);
1263
1264 m_pTCRolloverWin->SetString(p);
1265 m_pTCRolloverWin->SetBestPosition(curs_x, curs_y, 1, 1, TC_ROLLOVER,
1266 win_size);
1267 m_pTCRolloverWin->SetBitmap(TC_ROLLOVER);
1268 m_pTCRolloverWin->Refresh();
1269 m_pTCRolloverWin->Show();
1270
1271 // Mark the actual spot on the curve
1272 // x value is clear...
1273 // Find the point in the window that is used for the curve rendering,
1274 // rounding as necessary
1275
1276 int idx = 1; // in case m_graph_rect.width is weird ie ppx never > curs_x
1277 for (int i = 0; i < 26; i++) {
1278 float ppx = m_graph_rect.x + ((i)*m_graph_rect.width / 25.f);
1279 if (ppx > curs_x) {
1280 idx = i;
1281 break;
1282 }
1283 }
1284
1285 if (m_sList.size() > 0 && idx > 0 && idx < (int)m_sList.size()) {
1286 // Use iterator to access elements in std::list
1287 auto it_a = m_sList.begin();
1288 std::advance(it_a, idx - 1);
1289 auto it_b = m_sList.begin();
1290 std::advance(it_b, idx);
1291
1292 wxPoint *a = *it_a;
1293 wxPoint *b = *it_b;
1294
1295 float pct = (curs_x - a->x) / (float)((b->x - a->x));
1296 float dy = pct * (b->y - a->y);
1297
1298 ySpot = a->y + dy;
1299 xSpot = curs_x;
1300 } else {
1301 // Fallback if we can't find the curve point
1302 xSpot = curs_x;
1303 ySpot = m_graph_rect.y + m_graph_rect.height / 2;
1304 }
1305
1306 Refresh(true);
1307
1308 } else {
1309 SetCursor(*pParent->pCursorArrow);
1310 ShowRollover = false;
1311 }
1312
1313 if (m_pTCRolloverWin && m_pTCRolloverWin->IsShown() && !ShowRollover) {
1314 m_pTCRolloverWin->Hide();
1315 }
1316}
1317
1318void TCWin::OnTimeIndicatorTimer(wxTimerEvent &event) {
1319 // Refresh to update the red line (system time indicator)
1320 Refresh(false);
1321}
Minimal ChartCanvas interfaces.
Generic Chart canvas base.
Minimal ChartCAnvas interface with very little dependencies.
ChartCanvas - Main chart display and interaction component.
Definition chcanv.h:174
Represents an index entry for tidal and current data.
Definition idx_entry.h:48
Definition tc_win.h:46
Global variables stored in configuration file.
Extern C linked utilities.
Font list manager.
wxFont * GetOCPNScaledFont(wxString item, int default_size)
Retrieves a font from FontMgr, optionally scaled for physical readability.
Definition gui_lib.cpp:61
General purpose GUI support.
Miscellaneous globals primarely used by gui layer, not persisted in configuration file.
MyConfig * pConfig
Global instance.
Definition navutil.cpp:118
Utility functions.
wxString getUsrHeightUnit(int unit)
Get the abbreviation for the preferred height unit.
double toUsrHeight(double m_height, int unit)
Convert height from meters to preferred height units.
OCPNPlatform * g_Platform
Global instance.
OpenCPN Platform specific support utilities.
Tide and current data container.
Tide and currents window.
TCMgr * ptcmgr
Global instance.
Definition tcmgr.cpp:42
Tide and Current Manager @TODO Add original author copyright.
Timer identification constants.