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//---------------------------------------------------------------------------------------
53BEGIN_EVENT_TABLE(iENCToolbar, ocpnFloatingToolbarDialog)
54EVT_MENU(wxID_ANY, iENCToolbar::OnToolLeftClick)
55EVT_TIMER(STATE_TIMER, iENCToolbar::StateTimerEvent)
56EVT_MOUSE_EVENTS(iENCToolbar::MouseEvent)
57END_EVENT_TABLE()
58
59iENCToolbar::iENCToolbar(wxWindow *parent, wxPoint position, long orient,
60 float size_factor)
61 : ocpnFloatingToolbarDialog(parent, position, orient, size_factor) {
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, _T("Density"), m_bmMinimum, m_bmMinimum);
73 m_ptoolbar->AddTool(ID_RPLUS, _T("RangePlus"), m_bmRPlus, m_bmRPlus);
74 m_ptoolbar->AddTool(ID_RMINUS, _T("RangeMinus"), m_bmRMinus, m_bmRMinus);
75
76 SetCanToggleOrientation(false);
77 EnableRolloverBitmaps(false);
78 DisableTooltips();
79 SetGrabberEnable(false);
80
81 m_nDensity = 0;
82 SetDensityToolBitmap(m_nDensity);
83
84 // Realize() the toolbar
85 Realize();
86 Hide();
87
88 SetDefaultPosition();
89 Show(true);
90
91 m_state_timer.SetOwner(this, STATE_TIMER);
92 m_state_timer.Start(500, wxTIMER_CONTINUOUS);
93}
94
95iENCToolbar::~iENCToolbar() {}
96
97void iENCToolbar::MouseEvent(wxMouseEvent &event) {
98 if (event.IsButton()) gFrame->Raise();
99}
100
101void iENCToolbar::SetColorScheme(ColorScheme cs) {
102 m_range = 0; // Forcw a redraw of tools
103 m_nDensity = -1;
104
105 ocpnFloatingToolbarDialog::SetColorScheme(cs);
106}
107
108void iENCToolbar::LoadToolBitmaps() {
109 wxString svgDir = g_Platform->GetSharedDataDir() + _T("uidata") +
110 wxFileName::GetPathSeparator();
111
112 int w = 96;
113 int h = 32;
114
115 if (::wxFileExists(svgDir + _T("iENC_All.svg"))) {
116 m_bmAll = LoadSVG(svgDir + _T("iENC_All.svg"), w, h);
117 m_bmMinimum = LoadSVG(svgDir + _T("iENC_Minimum.svg"), w, h);
118 m_bmStandard = LoadSVG(svgDir + _T("iENC_Standard.svg"), w, h);
119 m_bmUStd = LoadSVG(svgDir + _T("iENC_UserStd.svg"), w, h);
120 m_bmRPlus = LoadSVG(svgDir + _T("iENC_RPlus.svg"), w, h);
121 m_bmRMinus = LoadSVG(svgDir + _T("iENC_RMinus.svg"), w, h);
122 } else {
123 wxLogMessage(_T("Cannot find iENC icons at: ") + svgDir);
124
125 m_bmMinimum = wxBitmap(96, 32);
126 m_bmStandard = wxBitmap(96, 32);
127 ;
128 m_bmAll = wxBitmap(96, 32);
129 m_bmUStd = wxBitmap(96, 32);
130
131 m_bmRPlus = wxBitmap(96, 32);
132 m_bmRMinus = wxBitmap(96, 32);
133 }
134}
135
136void iENCToolbar::OnToolLeftClick(wxCommandEvent &event) {
137 int itemId = event.GetId();
138
139 ChartCanvas *cc = gFrame->GetPrimaryCanvas();
140
141 enum _DisCat nset = STANDARD;
142 double range;
143
144 switch (itemId) {
145 case ID_DENSITY:
146
147 if (++m_nDensity > 3) m_nDensity = 0;
148
149 SetDensityToolBitmap(m_nDensity);
150 m_ptoolbar->Refresh();
151
152 switch (m_nDensity) {
153 case 0:
154 nset = DISPLAYBASE;
155 break;
156 case 1:
157 nset = STANDARD;
158 break;
159 case 2:
160 nset = OTHER;
161 break;
162 case 3:
163 nset = MARINERS_STANDARD;
164 break;
165 default:
166 nset = STANDARD;
167 break;
168 }
169
170 gFrame->SetENCDisplayCategory(cc, nset);
171
172 break;
173
174 case ID_RMINUS:
175 range = cc->GetCanvasRangeMeters();
176 range = wxRound(range * 10) / 10.;
177
178 if (range > 8000.) cc->SetCanvasRangeMeters(8000.);
179 if (range > 4000.)
180 cc->SetCanvasRangeMeters(4000.);
181 else if (range > 2000.)
182 cc->SetCanvasRangeMeters(2000.);
183 else if (range > 1600.)
184 cc->SetCanvasRangeMeters(1600.);
185 else if (range > 1200.)
186 cc->SetCanvasRangeMeters(1200.);
187 else if (range > 800.)
188 cc->SetCanvasRangeMeters(800.);
189 else if (range > 500.)
190 cc->SetCanvasRangeMeters(500.);
191 else if (range > 300.)
192 cc->SetCanvasRangeMeters(300.);
193
194 break;
195
196 case ID_RPLUS:
197 range = cc->GetCanvasRangeMeters();
198 range = wxRound(range * 10) / 10.;
199
200 if (range < 300.)
201 cc->SetCanvasRangeMeters(300.);
202 else if (range < 500.)
203 cc->SetCanvasRangeMeters(500.);
204 else if (range < 800.)
205 cc->SetCanvasRangeMeters(800.);
206 else if (range < 1200.)
207 cc->SetCanvasRangeMeters(1200.);
208 else if (range < 1600.)
209 cc->SetCanvasRangeMeters(1600.);
210 else if (range < 2000.)
211 cc->SetCanvasRangeMeters(2000.);
212 else if (range < 4000.)
213 cc->SetCanvasRangeMeters(4000.);
214 else if (range < 8000.)
215 cc->SetCanvasRangeMeters(8000.);
216
217 break;
218
219 default:
220 break;
221 }
222}
223
224void iENCToolbar::SetDensityToolBitmap(int nDensity) {
225 int toolID = m_toolDensity->GetId();
226
227 if (nDensity == 0)
228 m_ptoolbar->SetToolBitmaps(ID_DENSITY, &m_bmMinimum, &m_bmMinimum);
229 else if (nDensity == 1)
230 m_ptoolbar->SetToolBitmaps(ID_DENSITY, &m_bmStandard, &m_bmStandard);
231 else if (nDensity == 2)
232 m_ptoolbar->SetToolBitmaps(ID_DENSITY, &m_bmAll, &m_bmAll);
233 else if (nDensity == 3)
234 m_ptoolbar->SetToolBitmaps(ID_DENSITY, &m_bmUStd, &m_bmUStd);
235}
236
237void iENCToolbar::StateTimerEvent(wxTimerEvent &event) {
238 ChartCanvas *cc = gFrame->GetPrimaryCanvas();
239 if (!cc) return;
240
241 // Keep the Density tool in sync
242 if (ps52plib) {
243 int nset = 1;
244
245 switch (gFrame->GetPrimaryCanvas()->GetENCDisplayCategory()) {
246 case (DISPLAYBASE):
247 nset = 0;
248 break;
249 case (STANDARD):
250 nset = 1;
251 break;
252 case (OTHER):
253 nset = 2;
254 break;
255 case (MARINERS_STANDARD):
256 nset = 3;
257 break;
258 default:
259 nset = 1;
260 break;
261 }
262
263 if (nset != m_nDensity) {
264 if (nset < 3) {
265 m_nDensity = nset;
266 SetDensityToolBitmap(m_nDensity);
267
268 m_ptoolbar->Refresh();
269 }
270 }
271 }
272
273 // Keep the Range annunciator updated
274
275 if (cc) {
276 double range = cc->GetCanvasRangeMeters();
277
278 if (range != m_range) {
279 m_range = range;
280
281#if 0
282 // This DOES NOT WORK on Mac
283 // None of it....
284
285// wxImage image = m_bmRMinus.ConvertToImage();
286// wxBitmap bmTemplate(image);
287
288 // Get the template bitmap
289 wxBitmap bmTemplate = m_bmRMinus;
290
291 wxMask *mask = new wxMask(bmTemplate, wxColour(wxTRANSPARENT));
292 bmTemplate.SetMask(mask);
293
294 wxMemoryDC dct(bmTemplate);
295
296 // Make a deep copy by Blit
297 wxMemoryDC dc;
298 dc.SelectObject(*m_pbmScratch);
299 dc.SetBackground(wxBrush(GetGlobalColor(_T("GREY2"))));
300 dc.Clear();
301
302 dc.Blit(0, 0, m_pbmScratch->GetWidth(), m_pbmScratch->GetHeight(), &dct, 0, 0, wxCOPY, true);
303
304 dct.SelectObject(wxNullBitmap);
305#else
306 wxMemoryDC dc;
307 dc.SelectObject(*m_pbmScratch);
308 dc.SetBackground(wxBrush(GetGlobalColor(_T("GREY2"))));
309 dc.Clear();
310 dc.DrawBitmap(m_bmRMinus, wxPoint(0, 0));
311#endif
312 // Render the range as text onto the template
313 m_rangeFont = wxTheFontList->FindOrCreateFont(
314 12, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
315
316 wxString range_string;
317 range_string.Printf(_T("%4.0fm"), range);
318
319 dc.SetFont(*m_rangeFont);
320
321 // Select a font that will fit into the allow space.
322 bool good = false;
323 int target_points = 12;
324 while (!good && (target_points > 4)) {
325 int width, height;
326 dc.GetTextExtent(range_string, &width, &height);
327 if (width < 50) {
328 good = true;
329 break;
330 } else {
331 target_points--;
332 m_rangeFont = wxTheFontList->FindOrCreateFont(
333 target_points, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL,
334 wxFONTWEIGHT_NORMAL);
335 dc.SetFont(*m_rangeFont);
336 }
337 }
338
339 dc.SetTextForeground(*wxBLACK);
340 dc.SetBackgroundMode(wxTRANSPARENT);
341
342 dc.DrawText(range_string, 42, 8);
343
344 dc.SelectObject(wxNullBitmap);
345
346 m_ptoolbar->SetToolBitmaps(ID_RMINUS, m_pbmScratch, m_pbmScratch);
347
348 m_ptoolbar->Refresh();
349 }
350 }
351}