OpenCPN Partial API docs
Loading...
Searching...
No Matches
iENCToolbar.cpp
1/***************************************************************************
2 *
3 * Project: OpenCPN
4 * Purpose: OpenCPN iENCToolbar
5 * Author: David Register
6 *
7 ***************************************************************************
8 * Copyright (C) 2017 by David S. Register *
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., *
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
24 **************************************************************************/
25
26#include "config.h"
27
28#include <wx/wxprec.h>
29
30#ifndef WX_PRECOMP
31#include <wx/wx.h>
32#endif
33
34#include "toolbar.h"
35#include "iENCToolbar.h"
36
37#include "chcanv.h"
38#include "s52s57.h"
39#include "s52plib.h"
40#include "pluginmanager.h"
41#include "OCPNPlatform.h"
42#include "chcanv.h"
43#include "model/svg_utils.h"
44#include "ocpn_frame.h"
45
46extern s52plib *ps52plib;
47extern MyFrame *gFrame;
48extern OCPNPlatform *g_Platform;
49
50iENCToolbar *g_iENCToolbar;
51//---------------------------------------------------------------------------------------
52// iENCToolbar Implementation
53//---------------------------------------------------------------------------------------
54
55iENCToolbar::iENCToolbar(wxWindow *parent, wxPoint position, long orient,
56 float size_factor)
57 : ocpnFloatingToolbarDialog(parent, position, orient, size_factor) {
58 LoadToolBitmaps();
59
60 wxSize a = m_bmMinimum.GetSize();
61 m_ptoolbar->SetToolBitmapSize(a);
62
63 m_pbmScratch = new wxBitmap(a.x, a.y);
64
65 m_bmTemplate = m_bmRMinus;
66
67 m_toolDensity =
68 m_ptoolbar->AddTool(ID_DENSITY, "Density", m_bmMinimum, m_bmMinimum);
69 m_ptoolbar->AddTool(ID_RPLUS, "RangePlus", m_bmRPlus, m_bmRPlus);
70 m_ptoolbar->AddTool(ID_RMINUS, "RangeMinus", m_bmRMinus, m_bmRMinus);
71
72 SetCanToggleOrientation(false);
73 EnableRolloverBitmaps(false);
74 DisableTooltips();
75
76 m_nDensity = 0;
77 SetDensityToolBitmap(m_nDensity);
78 m_range = 0;
79
80 m_ptoolbar->SetBackgroundColour(GetGlobalColor("DILG0"));
81
82 // Realize() the toolbar
83 Realize();
84 SetDefaultPosition();
85
86 m_state_timer.SetOwner(this, STATE_TIMER);
87 m_state_timer.Start(100, wxTIMER_CONTINUOUS);
88 this->Connect(wxEVT_TIMER, wxTimerEventHandler(iENCToolbar::StateTimerEvent),
89 NULL, this);
90}
91
92iENCToolbar::~iENCToolbar() {}
93
94void iENCToolbar::SetColorScheme(ColorScheme cs) {
95 m_nDensity = -1;
96 m_ptoolbar->SetBackgroundColour(GetGlobalColor("DILG0"));
97 SetRangeToolBitmap();
98 ocpnFloatingToolbarDialog::SetColorScheme(cs);
99 m_ptoolbar->InvalidateBitmaps();
100 RefreshToolbar();
101 m_range = 0;
102}
103
104void iENCToolbar::LoadToolBitmaps() {
105 wxString svgDir = g_Platform->GetSharedDataDir() + "uidata" +
106 wxFileName::GetPathSeparator();
107
108 int w = 96;
109 int h = 32;
110
111 if (::wxFileExists(svgDir + "iENC_All.svg")) {
112 m_bmAll = LoadSVG(svgDir + "iENC_All.svg", w, h);
113 m_bmMinimum = LoadSVG(svgDir + "iENC_Minimum.svg", w, h);
114 m_bmStandard = LoadSVG(svgDir + "iENC_Standard.svg", w, h);
115 m_bmUStd = LoadSVG(svgDir + "iENC_UserStd.svg", w, h);
116 m_bmRPlus = LoadSVG(svgDir + "iENC_RPlus.svg", w, h);
117 m_bmRMinus = LoadSVG(svgDir + "iENC_RMinus.svg", w, h);
118 } else {
119 wxLogMessage("Cannot find iENC icons at: " + svgDir);
120
121 m_bmMinimum = wxBitmap(96, 32);
122 m_bmStandard = wxBitmap(96, 32);
123 m_bmAll = wxBitmap(96, 32);
124 m_bmUStd = wxBitmap(96, 32);
125 m_bmRPlus = wxBitmap(96, 32);
126 m_bmRMinus = wxBitmap(96, 32);
127 }
128}
129
130void iENCToolbar::OnToolLeftClick(wxCommandEvent &event) {
131 int itemId = event.GetId();
132
133 ChartCanvas *cc = gFrame->GetPrimaryCanvas();
134
135 enum _DisCat nset = STANDARD;
136 double range;
137
138 switch (itemId) {
139 case ID_DENSITY:
140
141 if (++m_nDensity > 3) m_nDensity = 0;
142
143 SetDensityToolBitmap(m_nDensity);
144
145 switch (m_nDensity) {
146 case 0:
147 nset = DISPLAYBASE;
148 break;
149 case 1:
150 nset = STANDARD;
151 break;
152 case 2:
153 nset = OTHER;
154 break;
155 case 3:
156 nset = MARINERS_STANDARD;
157 break;
158 default:
159 nset = STANDARD;
160 break;
161 }
162
163 gFrame->SetENCDisplayCategory(cc, nset);
164
165 break;
166
167 case ID_RMINUS:
168 range = cc->GetCanvasRangeMeters();
169 range = wxRound(range * 10) / 10.;
170
171 if (range > 8000.) cc->SetCanvasRangeMeters(8000.);
172 if (range > 4000.)
173 cc->SetCanvasRangeMeters(4000.);
174 else if (range > 2000.)
175 cc->SetCanvasRangeMeters(2000.);
176 else if (range > 1600.)
177 cc->SetCanvasRangeMeters(1600.);
178 else if (range > 1200.)
179 cc->SetCanvasRangeMeters(1200.);
180 else if (range > 800.)
181 cc->SetCanvasRangeMeters(800.);
182 else if (range > 500.)
183 cc->SetCanvasRangeMeters(500.);
184 else if (range > 300.)
185 cc->SetCanvasRangeMeters(300.);
186
187 break;
188
189 case ID_RPLUS:
190 range = cc->GetCanvasRangeMeters();
191 range = wxRound(range * 10) / 10.;
192
193 if (range < 300.)
194 cc->SetCanvasRangeMeters(300.);
195 else if (range < 500.)
196 cc->SetCanvasRangeMeters(500.);
197 else if (range < 800.)
198 cc->SetCanvasRangeMeters(800.);
199 else if (range < 1200.)
200 cc->SetCanvasRangeMeters(1200.);
201 else if (range < 1600.)
202 cc->SetCanvasRangeMeters(1600.);
203 else if (range < 2000.)
204 cc->SetCanvasRangeMeters(2000.);
205 else if (range < 4000.)
206 cc->SetCanvasRangeMeters(4000.);
207 else if (range < 8000.)
208 cc->SetCanvasRangeMeters(8000.);
209
210 break;
211
212 default:
213 break;
214 }
215 m_ptoolbar->InvalidateBitmaps();
216 RefreshToolbar();
217}
218
219void iENCToolbar::SetDensityToolBitmap(int nDensity) {
220 int toolID = m_toolDensity->GetId();
221
222 if (nDensity == 0)
223 m_ptoolbar->SetToolBitmaps(ID_DENSITY, &m_bmMinimum, &m_bmMinimum);
224 else if (nDensity == 1)
225 m_ptoolbar->SetToolBitmaps(ID_DENSITY, &m_bmStandard, &m_bmStandard);
226 else if (nDensity == 2)
227 m_ptoolbar->SetToolBitmaps(ID_DENSITY, &m_bmAll, &m_bmAll);
228 else if (nDensity == 3)
229 m_ptoolbar->SetToolBitmaps(ID_DENSITY, &m_bmUStd, &m_bmUStd);
230}
231
232void iENCToolbar::SetRangeToolBitmap() {
233 wxMemoryDC dc;
234 dc.SelectObject(*m_pbmScratch);
235 dc.SetBackground(wxBrush(GetGlobalColor("DILG0")));
236 dc.Clear();
237 dc.DrawBitmap(m_bmRMinus, wxPoint(0, 0));
238
239 // Render the range as text onto the template
240 m_rangeFont = wxTheFontList->FindOrCreateFont(
241 12, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
242
243 wxString range_string;
244 range_string.Printf("%4.0fm", m_range);
245
246 dc.SetFont(*m_rangeFont);
247
248 // Select a font that will fit into the allow space.
249 bool good = false;
250 int target_points = 12;
251 while (!good && (target_points > 4)) {
252 int width, height;
253 dc.GetTextExtent(range_string, &width, &height);
254 if (width < 50) {
255 good = true;
256 break;
257 } else {
258 target_points--;
259 m_rangeFont = wxTheFontList->FindOrCreateFont(
260 target_points, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL,
261 wxFONTWEIGHT_NORMAL);
262 dc.SetFont(*m_rangeFont);
263 }
264 }
265
266 dc.SetTextForeground(*wxBLACK);
267 dc.SetBackgroundMode(wxTRANSPARENT);
268
269 dc.DrawText(range_string, 42, 8);
270
271 dc.SelectObject(wxNullBitmap);
272
273 m_ptoolbar->SetToolBitmaps(ID_RMINUS, m_pbmScratch, m_pbmScratch);
274}
275
276void iENCToolbar::StateTimerEvent(wxTimerEvent &event) {
277 ChartCanvas *cc = gFrame->GetPrimaryCanvas();
278 if (!cc) return;
279
280 bool bRefresh = false;
281 // Keep the Density tool in sync
282 if (ps52plib) {
283 int nset = 1;
284
285 switch (gFrame->GetPrimaryCanvas()->GetENCDisplayCategory()) {
286 case (DISPLAYBASE):
287 nset = 0;
288 break;
289 case (STANDARD):
290 nset = 1;
291 break;
292 case (OTHER):
293 nset = 2;
294 break;
295 case (MARINERS_STANDARD):
296 nset = 3;
297 break;
298 default:
299 nset = 1;
300 break;
301 }
302
303 if (nset != m_nDensity) {
304 if (nset < 3) {
305 m_nDensity = nset;
306 SetDensityToolBitmap(m_nDensity);
307
308 bRefresh = true;
309 }
310 }
311 }
312
313 // Keep the Range annunciator updated
314
315 if (cc) {
316 double range = cc->GetCanvasRangeMeters();
317
318 if (range != m_range) {
319 m_range = range;
320 SetRangeToolBitmap();
321 bRefresh = true;
322 }
323 }
324 if (bRefresh) {
325 RebuildToolbar();
326 m_ptoolbar->InvalidateBitmaps();
327 RefreshToolbar();
328 }
329
330 // Restore runtime timer frequency after startup
331 m_state_timer.Start(500, wxTIMER_CONTINUOUS);
332}
Generic Chart canvas base.
ChartCanvas - Main chart display and interaction component.
Definition chcanv.h:151
Main application frame.
Definition ocpn_frame.h:138
Provides platform-specific support utilities for OpenCPN.
Floating toolbar for iENC (International Electronic Navigational Chart) functionality.
Definition iENCToolbar.h:45
Floating toolbar dialog for OpenCPN.
Definition toolbar.h:386
PlugInManager and helper classes – Mostly gui parts (dialogs) and plugin API stuff.
SVG utilities.