OpenCPN Partial API docs
Loading...
Searching...
No Matches
ienc_toolbar.cpp
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2017 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
24#include "config.h"
25
26#include <wx/wxprec.h>
27
28#ifndef WX_PRECOMP
29#include <wx/wx.h>
30#endif
31
32#include <wx/bitmap.h>
33#include <wx/brush.h>
34#include <wx/dcmemory.h>
35#include <wx/event.h>
36#include <wx/file.h>
37#include <wx/filename.h>
38#include <wx/log.h>
39
40#include "ienc_toolbar.h"
41
42#include "model/svg_utils.h"
43
44#include "chcanv.h"
45#include "ocpn_platform.h"
46#include "pluginmanager.h"
47#include "s52plib.h"
48#include "s52s57.h"
49#include "toolbar.h"
50#include "top_frame.h"
51#include "user_colors.h"
52
54
55//---------------------------------------------------------------------------------------
56// iENCToolbar Implementation
57//---------------------------------------------------------------------------------------
58
59iENCToolbar::iENCToolbar(wxWindow *parent, wxPoint pos, long orient,
60 float size_factor, ToolbarDlgCallbacks callbacks)
61 : ocpnFloatingToolbarDialog(parent, pos, orient, size_factor, callbacks) {
62 LoadToolBitmaps();
63
64 wxSize a = m_bmMinimum.GetSize();
65 m_ptoolbar->SetToolBitmapSize(a);
66
67 m_pbmScratch = new wxBitmap(a.x, a.y);
68
69 m_bmTemplate = m_bmRMinus;
70
71 m_toolDensity =
72 m_ptoolbar->AddTool(ID_DENSITY, "Density", m_bmMinimum, m_bmMinimum);
73 m_ptoolbar->AddTool(ID_RPLUS, "RangePlus", m_bmRPlus, m_bmRPlus);
74 m_ptoolbar->AddTool(ID_RMINUS, "RangeMinus", m_bmRMinus, m_bmRMinus);
75
76 SetCanToggleOrientation(false);
77 EnableRolloverBitmaps(false);
78 DisableTooltips();
79
80 m_nDensity = 0;
81 SetDensityToolBitmap(m_nDensity);
82 m_range = 0;
83
84 m_ptoolbar->SetBackgroundColour(GetGlobalColor("DILG0"));
85
86 // Realize() the toolbar
87 Realize();
88
89 m_state_timer.SetOwner(this, STATE_TIMER);
90 m_state_timer.Start(100, wxTIMER_CONTINUOUS);
91 this->Connect(wxEVT_TIMER, wxTimerEventHandler(iENCToolbar::StateTimerEvent),
92 NULL, this);
93}
94
95iENCToolbar::~iENCToolbar() {
96 m_state_timer.Stop();
97 delete m_pbmScratch;
98 this->Disconnect(wxEVT_TIMER,
99 wxTimerEventHandler(iENCToolbar::StateTimerEvent), NULL,
100 this);
101}
102
103void iENCToolbar::SetColorScheme(ColorScheme cs) {
104 m_nDensity = -1;
105 m_ptoolbar->SetBackgroundColour(GetGlobalColor("DILG0"));
106 SetRangeToolBitmap();
107 ocpnFloatingToolbarDialog::SetColorScheme(cs);
108 m_ptoolbar->InvalidateBitmaps();
109 RefreshToolbar();
110 m_range = 0;
111}
112
113void iENCToolbar::LoadToolBitmaps() {
114 wxString svgDir = g_Platform->GetSharedDataDir() + "uidata" +
115 wxFileName::GetPathSeparator();
116
117 int w = 96;
118 int h = 32;
119
120 if (::wxFileExists(svgDir + "iENC_All.svg")) {
121 m_bmAll = LoadSVG(svgDir + "iENC_All.svg", w, h);
122 m_bmMinimum = LoadSVG(svgDir + "iENC_Minimum.svg", w, h);
123 m_bmStandard = LoadSVG(svgDir + "iENC_Standard.svg", w, h);
124 m_bmUStd = LoadSVG(svgDir + "iENC_UserStd.svg", w, h);
125 m_bmRPlus = LoadSVG(svgDir + "iENC_RPlus.svg", w, h);
126 m_bmRMinus = LoadSVG(svgDir + "iENC_RMinus.svg", w, h);
127 } else {
128 wxLogMessage("Cannot find iENC icons at: " + svgDir);
129
130 m_bmMinimum = wxBitmap(96, 32);
131 m_bmStandard = wxBitmap(96, 32);
132 m_bmAll = wxBitmap(96, 32);
133 m_bmUStd = wxBitmap(96, 32);
134 m_bmRPlus = wxBitmap(96, 32);
135 m_bmRMinus = wxBitmap(96, 32);
136 }
137}
138
139void iENCToolbar::OnToolLeftClick(wxCommandEvent &event) {
140 int itemId = event.GetId();
141
142 AbstractChartCanvas *acc = top_frame::Get()->GetAbstractPrimaryCanvas();
143
144 enum _DisCat nset = STANDARD;
145 double range;
146
147 switch (itemId) {
148 case ID_DENSITY:
149
150 if (++m_nDensity > 3) m_nDensity = 0;
151
152 SetDensityToolBitmap(m_nDensity);
153
154 switch (m_nDensity) {
155 case 0:
156 nset = DISPLAYBASE;
157 break;
158 case 1:
159 nset = STANDARD;
160 break;
161 case 2:
162 nset = OTHER;
163 break;
164 case 3:
165 nset = MARINERS_STANDARD;
166 break;
167 default:
168 nset = STANDARD;
169 break;
170 }
171
172 top_frame::Get()->SetENCDisplayCategory(acc, nset);
173
174 break;
175
176 case ID_RMINUS:
177 range = acc->GetCanvasRangeMeters();
178 range = wxRound(range * 10) / 10.;
179
180 if (range > 8000.) acc->SetCanvasRangeMeters(8000.);
181 if (range > 4000.)
182 acc->SetCanvasRangeMeters(4000.);
183 else if (range > 2000.)
184 acc->SetCanvasRangeMeters(2000.);
185 else if (range > 1600.)
186 acc->SetCanvasRangeMeters(1600.);
187 else if (range > 1200.)
188 acc->SetCanvasRangeMeters(1200.);
189 else if (range > 800.)
190 acc->SetCanvasRangeMeters(800.);
191 else if (range > 500.)
192 acc->SetCanvasRangeMeters(500.);
193 else if (range > 300.)
194 acc->SetCanvasRangeMeters(300.);
195
196 break;
197
198 case ID_RPLUS:
199 range = acc->GetCanvasRangeMeters();
200 range = wxRound(range * 10) / 10.;
201
202 if (range < 300.)
203 acc->SetCanvasRangeMeters(300.);
204 else if (range < 500.)
205 acc->SetCanvasRangeMeters(500.);
206 else if (range < 800.)
207 acc->SetCanvasRangeMeters(800.);
208 else if (range < 1200.)
209 acc->SetCanvasRangeMeters(1200.);
210 else if (range < 1600.)
211 acc->SetCanvasRangeMeters(1600.);
212 else if (range < 2000.)
213 acc->SetCanvasRangeMeters(2000.);
214 else if (range < 4000.)
215 acc->SetCanvasRangeMeters(4000.);
216 else if (range < 8000.)
217 acc->SetCanvasRangeMeters(8000.);
218
219 break;
220
221 default:
222 break;
223 }
224 m_ptoolbar->InvalidateBitmaps();
225 RefreshToolbar();
226}
227
228void iENCToolbar::SetDensityToolBitmap(int nDensity) {
229 int toolID = m_toolDensity->GetId();
230
231 if (nDensity == 0)
232 m_ptoolbar->SetToolBitmaps(ID_DENSITY, &m_bmMinimum, &m_bmMinimum);
233 else if (nDensity == 1)
234 m_ptoolbar->SetToolBitmaps(ID_DENSITY, &m_bmStandard, &m_bmStandard);
235 else if (nDensity == 2)
236 m_ptoolbar->SetToolBitmaps(ID_DENSITY, &m_bmAll, &m_bmAll);
237 else if (nDensity == 3)
238 m_ptoolbar->SetToolBitmaps(ID_DENSITY, &m_bmUStd, &m_bmUStd);
239}
240
241void iENCToolbar::SetRangeToolBitmap() {
242 wxMemoryDC dc;
243 dc.SelectObject(*m_pbmScratch);
244 dc.SetBackground(wxBrush(GetGlobalColor("DILG0")));
245 dc.Clear();
246 dc.DrawBitmap(m_bmRMinus, wxPoint(0, 0));
247
248 // Render the range as text onto the template
249 m_rangeFont = wxTheFontList->FindOrCreateFont(
250 12, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
251
252 wxString range_string;
253 range_string.Printf("%4.0fm", m_range);
254
255 dc.SetFont(*m_rangeFont);
256
257 // Select a font that will fit into the allow space.
258 bool good = false;
259 int target_points = 12;
260 while (!good && (target_points > 4)) {
261 int width, height;
262 dc.GetTextExtent(range_string, &width, &height);
263 if (width < 50) {
264 good = true;
265 break;
266 } else {
267 target_points--;
268 m_rangeFont = wxTheFontList->FindOrCreateFont(
269 target_points, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL,
270 wxFONTWEIGHT_NORMAL);
271 dc.SetFont(*m_rangeFont);
272 }
273 }
274
275 dc.SetTextForeground(*wxBLACK);
276 dc.SetBackgroundMode(wxTRANSPARENT);
277
278 dc.DrawText(range_string, 42, 8);
279
280 dc.SelectObject(wxNullBitmap);
281
282 m_ptoolbar->SetToolBitmaps(ID_RMINUS, m_pbmScratch, m_pbmScratch);
283}
284
285void iENCToolbar::StateTimerEvent(wxTimerEvent &event) {
286 AbstractChartCanvas *acc = top_frame::Get()->GetAbstractPrimaryCanvas();
287 if (!acc) return;
288
289 bool bRefresh = false;
290 // Keep the Density tool in sync
291 if (ps52plib) {
292 int nset = 1;
293
294 switch (acc->GetENCDisplayCategory()) {
295 case (DISPLAYBASE):
296 nset = 0;
297 break;
298 case (STANDARD):
299 nset = 1;
300 break;
301 case (OTHER):
302 nset = 2;
303 break;
304 case (MARINERS_STANDARD):
305 nset = 3;
306 break;
307 default:
308 nset = 1;
309 break;
310 }
311
312 if (nset != m_nDensity) {
313 if (nset < 3) {
314 m_nDensity = nset;
315 SetDensityToolBitmap(m_nDensity);
316
317 bRefresh = true;
318 }
319 }
320 }
321
322 // Keep the Range annunciator updated
323
324 if (acc) {
325 double range = acc->GetCanvasRangeMeters();
326
327 if (range != m_range) {
328 m_range = range;
329 SetRangeToolBitmap();
330 bRefresh = true;
331 }
332 }
333 if (bRefresh) {
334 RebuildToolbar();
335 m_ptoolbar->InvalidateBitmaps();
336 RefreshToolbar();
337 }
338
339 // Restore runtime timer frequency after startup
340 m_state_timer.Start(500, wxTIMER_CONTINUOUS);
341}
Generic Chart canvas base.
Minimal ChartCAnvas interface with very little dependencies.
Floating toolbar for iENC (International Electronic Navigational Chart) functionality.
Floating toolbar dialog for OpenCPN.
Definition toolbar.h:395
iENCToolbar * g_iENCToolbar
Global instance.
iENC specific chart operations floating toolbar extension
OpenCPN Platform specific support utilities.
PlugInManager and helper classes – Mostly gui parts (dialogs) and plugin API stuff.
wxBitmap LoadSVG(const wxString filename, const unsigned int width, const unsigned int height, wxBitmap *default_bitmap, bool use_cache)
Load SVG file and return it's bitmap representation of requested size In case file can't be loaded an...
Definition svg_utils.cpp:59
SVG utilities.
OpenCPN Toolbar.
Abstract gFrame/MyFrame interface.