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 "svg_utils.h"
44#include "ocpn_frame.h"
45
46extern s52plib *ps52plib;
47extern MyFrame *gFrame;
48extern OCPNPlatform *g_Platform;
49
50//---------------------------------------------------------------------------------------
51// iENCToolbar Implementation
52//---------------------------------------------------------------------------------------
53
54iENCToolbar::iENCToolbar(wxWindow *parent, wxPoint position, long orient,
55 float size_factor)
56 : ocpnFloatingToolbarDialog(parent, position, orient, size_factor) {
57 LoadToolBitmaps();
58
59 wxSize a = m_bmMinimum.GetSize();
60 m_ptoolbar->SetToolBitmapSize(a);
61
62 m_pbmScratch = new wxBitmap(a.x, a.y);
63
64 m_bmTemplate = m_bmRMinus;
65
66 m_toolDensity =
67 m_ptoolbar->AddTool(ID_DENSITY, _T("Density"), m_bmMinimum, m_bmMinimum);
68 m_ptoolbar->AddTool(ID_RPLUS, _T("RangePlus"), m_bmRPlus, m_bmRPlus);
69 m_ptoolbar->AddTool(ID_RMINUS, _T("RangeMinus"), m_bmRMinus, m_bmRMinus);
70
71 SetCanToggleOrientation(false);
72 EnableRolloverBitmaps(false);
73 DisableTooltips();
74
75 m_nDensity = 0;
76 SetDensityToolBitmap(m_nDensity);
77 m_range = 0;
78
79 m_ptoolbar->SetBackgroundColour(GetGlobalColor("DILG0"));
80
81 // Realize() the toolbar
82 Realize();
83 SetDefaultPosition();
84
85 m_state_timer.SetOwner(this, STATE_TIMER);
86 m_state_timer.Start(100, wxTIMER_CONTINUOUS);
87 this->Connect(wxEVT_TIMER, wxTimerEventHandler(iENCToolbar::StateTimerEvent),
88 NULL, this);
89}
90
91iENCToolbar::~iENCToolbar() {}
92
93void iENCToolbar::SetColorScheme(ColorScheme cs) {
94 m_nDensity = -1;
95 m_ptoolbar->SetBackgroundColour(GetGlobalColor("DILG0"));
96 SetRangeToolBitmap();
97 ocpnFloatingToolbarDialog::SetColorScheme(cs);
98 m_ptoolbar->InvalidateBitmaps();
99 RefreshToolbar();
100 m_range = 0;
101}
102
103void iENCToolbar::LoadToolBitmaps() {
104 wxString svgDir = g_Platform->GetSharedDataDir() + _T("uidata") +
105 wxFileName::GetPathSeparator();
106
107 int w = 96;
108 int h = 32;
109
110 if (::wxFileExists(svgDir + _T("iENC_All.svg"))) {
111 m_bmAll = LoadSVG(svgDir + _T("iENC_All.svg"), w, h);
112 m_bmMinimum = LoadSVG(svgDir + _T("iENC_Minimum.svg"), w, h);
113 m_bmStandard = LoadSVG(svgDir + _T("iENC_Standard.svg"), w, h);
114 m_bmUStd = LoadSVG(svgDir + _T("iENC_UserStd.svg"), w, h);
115 m_bmRPlus = LoadSVG(svgDir + _T("iENC_RPlus.svg"), w, h);
116 m_bmRMinus = LoadSVG(svgDir + _T("iENC_RMinus.svg"), w, h);
117 } else {
118 wxLogMessage(_T("Cannot find iENC icons at: ") + svgDir);
119
120 m_bmMinimum = wxBitmap(96, 32);
121 m_bmStandard = wxBitmap(96, 32);
122 m_bmAll = wxBitmap(96, 32);
123 m_bmUStd = wxBitmap(96, 32);
124 m_bmRPlus = wxBitmap(96, 32);
125 m_bmRMinus = wxBitmap(96, 32);
126 }
127}
128
129void iENCToolbar::OnToolLeftClick(wxCommandEvent &event) {
130 int itemId = event.GetId();
131
132 ChartCanvas *cc = gFrame->GetPrimaryCanvas();
133
134 enum _DisCat nset = STANDARD;
135 double range;
136
137 switch (itemId) {
138 case ID_DENSITY:
139
140 if (++m_nDensity > 3) m_nDensity = 0;
141
142 SetDensityToolBitmap(m_nDensity);
143
144 switch (m_nDensity) {
145 case 0:
146 nset = DISPLAYBASE;
147 break;
148 case 1:
149 nset = STANDARD;
150 break;
151 case 2:
152 nset = OTHER;
153 break;
154 case 3:
155 nset = MARINERS_STANDARD;
156 break;
157 default:
158 nset = STANDARD;
159 break;
160 }
161
162 gFrame->SetENCDisplayCategory(cc, nset);
163
164 break;
165
166 case ID_RMINUS:
167 range = cc->GetCanvasRangeMeters();
168 range = wxRound(range * 10) / 10.;
169
170 if (range > 8000.) cc->SetCanvasRangeMeters(8000.);
171 if (range > 4000.)
172 cc->SetCanvasRangeMeters(4000.);
173 else if (range > 2000.)
174 cc->SetCanvasRangeMeters(2000.);
175 else if (range > 1600.)
176 cc->SetCanvasRangeMeters(1600.);
177 else if (range > 1200.)
178 cc->SetCanvasRangeMeters(1200.);
179 else if (range > 800.)
180 cc->SetCanvasRangeMeters(800.);
181 else if (range > 500.)
182 cc->SetCanvasRangeMeters(500.);
183 else if (range > 300.)
184 cc->SetCanvasRangeMeters(300.);
185
186 break;
187
188 case ID_RPLUS:
189 range = cc->GetCanvasRangeMeters();
190 range = wxRound(range * 10) / 10.;
191
192 if (range < 300.)
193 cc->SetCanvasRangeMeters(300.);
194 else if (range < 500.)
195 cc->SetCanvasRangeMeters(500.);
196 else if (range < 800.)
197 cc->SetCanvasRangeMeters(800.);
198 else if (range < 1200.)
199 cc->SetCanvasRangeMeters(1200.);
200 else if (range < 1600.)
201 cc->SetCanvasRangeMeters(1600.);
202 else if (range < 2000.)
203 cc->SetCanvasRangeMeters(2000.);
204 else if (range < 4000.)
205 cc->SetCanvasRangeMeters(4000.);
206 else if (range < 8000.)
207 cc->SetCanvasRangeMeters(8000.);
208
209 break;
210
211 default:
212 break;
213 }
214 m_ptoolbar->InvalidateBitmaps();
215 RefreshToolbar();
216}
217
218void iENCToolbar::SetDensityToolBitmap(int nDensity) {
219 int toolID = m_toolDensity->GetId();
220
221 if (nDensity == 0)
222 m_ptoolbar->SetToolBitmaps(ID_DENSITY, &m_bmMinimum, &m_bmMinimum);
223 else if (nDensity == 1)
224 m_ptoolbar->SetToolBitmaps(ID_DENSITY, &m_bmStandard, &m_bmStandard);
225 else if (nDensity == 2)
226 m_ptoolbar->SetToolBitmaps(ID_DENSITY, &m_bmAll, &m_bmAll);
227 else if (nDensity == 3)
228 m_ptoolbar->SetToolBitmaps(ID_DENSITY, &m_bmUStd, &m_bmUStd);
229}
230
231void iENCToolbar::SetRangeToolBitmap() {
232 wxMemoryDC dc;
233 dc.SelectObject(*m_pbmScratch);
234 dc.SetBackground(wxBrush(GetGlobalColor("DILG0")));
235 dc.Clear();
236 dc.DrawBitmap(m_bmRMinus, wxPoint(0, 0));
237
238 // Render the range as text onto the template
239 m_rangeFont = wxTheFontList->FindOrCreateFont(
240 12, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
241
242 wxString range_string;
243 range_string.Printf(_T("%4.0fm"), m_range);
244
245 dc.SetFont(*m_rangeFont);
246
247 // Select a font that will fit into the allow space.
248 bool good = false;
249 int target_points = 12;
250 while (!good && (target_points > 4)) {
251 int width, height;
252 dc.GetTextExtent(range_string, &width, &height);
253 if (width < 50) {
254 good = true;
255 break;
256 } else {
257 target_points--;
258 m_rangeFont = wxTheFontList->FindOrCreateFont(
259 target_points, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL,
260 wxFONTWEIGHT_NORMAL);
261 dc.SetFont(*m_rangeFont);
262 }
263 }
264
265 dc.SetTextForeground(*wxBLACK);
266 dc.SetBackgroundMode(wxTRANSPARENT);
267
268 dc.DrawText(range_string, 42, 8);
269
270 dc.SelectObject(wxNullBitmap);
271
272 m_ptoolbar->SetToolBitmaps(ID_RMINUS, m_pbmScratch, m_pbmScratch);
273}
274
275void iENCToolbar::StateTimerEvent(wxTimerEvent &event) {
276 ChartCanvas *cc = gFrame->GetPrimaryCanvas();
277 if (!cc) return;
278
279 bool bRefresh = false;
280 // Keep the Density tool in sync
281 if (ps52plib) {
282 int nset = 1;
283
284 switch (gFrame->GetPrimaryCanvas()->GetENCDisplayCategory()) {
285 case (DISPLAYBASE):
286 nset = 0;
287 break;
288 case (STANDARD):
289 nset = 1;
290 break;
291 case (OTHER):
292 nset = 2;
293 break;
294 case (MARINERS_STANDARD):
295 nset = 3;
296 break;
297 default:
298 nset = 1;
299 break;
300 }
301
302 if (nset != m_nDensity) {
303 if (nset < 3) {
304 m_nDensity = nset;
305 SetDensityToolBitmap(m_nDensity);
306
307 bRefresh = true;
308 }
309 }
310 }
311
312 // Keep the Range annunciator updated
313
314 if (cc) {
315 double range = cc->GetCanvasRangeMeters();
316
317 if (range != m_range) {
318 m_range = range;
319 SetRangeToolBitmap();
320 bRefresh = true;
321 }
322 }
323 if (bRefresh) {
324 RebuildToolbar();
325 m_ptoolbar->InvalidateBitmaps();
326 RefreshToolbar();
327 }
328
329 // Restore runtime timer frequency after startup
330 m_state_timer.Start(500, wxTIMER_CONTINUOUS);
331}
ChartCanvas - Main chart display and interaction component.
Definition chcanv.h:148
Main application frame.
Definition ocpn_frame.h:135
Provides platform-specific support utilities for OpenCPN.
Floating toolbar dialog for OpenCPN.
Definition toolbar.h:386