35#include "instrument.h"
37#ifdef __OCPN__ANDROID__
43extern PI_ColorScheme aktuellColorScheme;
45wxColour GetColourSchemeBackgroundColour(wxColour co) {
46 wxColour ret_val = co;
47#if wxCHECK_VERSION(3, 1, 6)
48 unsigned int red = co.GetRed();
49 unsigned int Green = co.GetGreen();
50 unsigned int Blue = co.GetBlue();
52 unsigned int red = co.Red();
53 unsigned int Green = co.Green();
54 unsigned int Blue = co.Blue();
56 switch (aktuellColorScheme) {
57 case PI_GLOBAL_COLOR_SCHEME_RGB:
59 case PI_GLOBAL_COLOR_SCHEME_DAY:
61 case PI_GLOBAL_COLOR_SCHEME_DUSK:
65 ret_val = wxColour(red, Green, Blue);
67 case PI_GLOBAL_COLOR_SCHEME_NIGHT:
71 ret_val = wxColour(red, Green, Blue);
79wxColour GetColourSchemeFont(wxColour co) {
80 wxColour ret_val = co;
81#if wxCHECK_VERSION(3, 1, 6)
82 unsigned int red = co.GetRed();
83 unsigned int Green = co.GetGreen();
84 unsigned int Blue = co.GetBlue();
86 unsigned int red = co.Red();
87 unsigned int Green = co.Green();
88 unsigned int Blue = co.Blue();
90 switch (aktuellColorScheme) {
91 case PI_GLOBAL_COLOR_SCHEME_RGB:
93 case PI_GLOBAL_COLOR_SCHEME_DAY:
95 case PI_GLOBAL_COLOR_SCHEME_DUSK:
102 ret_val = wxColour(red, Green, Blue);
104 case PI_GLOBAL_COLOR_SCHEME_NIGHT:
108 if (red + Green + Blue < 10) {
109 red = Green = Blue = 50;
111 ret_val = wxColour(red, Green, Blue);
125DashboardInstrument::DashboardInstrument(wxWindow* pparent, wxWindowID
id,
126 wxString title, DASH_CAP cap_flag,
128 : wxControl(pparent, id, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE) {
129 m_InstrumentSpacing = 0;
130 m_DataTextHeight = 0;
134 m_DataRightAlign =
false;
135 m_TitleRightAlign =
false;
137 m_Properties = Properties;
138 m_cap_flag.set(cap_flag);
139 m_popupWanted =
false;
141 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
142 SetDrawSoloInPane(
false);
144 Connect(wxEVT_ERASE_BACKGROUND,
145 wxEraseEventHandler(DashboardInstrument::OnEraseBackground));
146 Connect(wxEVT_PAINT, wxPaintEventHandler(DashboardInstrument::OnPaint));
155#if defined(__WXOSX__) || defined(__WXQT__)
156 Connect(wxEVT_RIGHT_DOWN,
157 wxMouseEventHandler(DashboardInstrument::MouseEvent), NULL,
this);
160#ifdef HAVE_WX_GESTURE_EVENTS
161 if (!EnableTouchEvents(wxTOUCH_PRESS_GESTURES)) {
162 wxLogError(
"Failed to enable touch events on dashboard Instrument");
165 Bind(wxEVT_LONG_PRESS, &DashboardInstrument::OnLongPress,
this);
166 Bind(wxEVT_LEFT_UP, &DashboardInstrument::OnLeftUp,
this);
170#ifdef HAVE_WX_GESTURE_EVENTS
171void DashboardInstrument::OnLongPress(wxLongPressEvent& event) {
175 m_popupWanted =
true;
179void DashboardInstrument::OnLeftUp(wxMouseEvent& event) {
180 wxPoint pos =
event.GetPosition();
182 if (!m_popupWanted) {
183 wxMouseEvent ev(wxEVT_LEFT_UP);
186 GetParent()->GetEventHandler()->AddPendingEvent(ev);
190 m_popupWanted =
false;
191 wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU, this->GetId(),
192 this->ClientToScreen(event.GetPosition()));
193 evtCtx.SetEventObject(
this);
194 GetParent()->GetEventHandler()->AddPendingEvent(evtCtx);
197void DashboardInstrument::MouseEvent(wxMouseEvent& event) {
198 if (event.GetEventType() == wxEVT_RIGHT_DOWN) {
199 wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU, this->GetId(),
200 this->ClientToScreen(event.GetPosition()));
201 evtCtx.SetEventObject(
this);
202 GetParent()->GetEventHandler()->AddPendingEvent(evtCtx);
206CapType DashboardInstrument::GetCapacity() {
return m_cap_flag; }
207void DashboardInstrument::SetDrawSoloInPane(
bool value) {
208 m_drawSoloInPane = value;
210void DashboardInstrument::OnEraseBackground(wxEraseEvent& WXUNUSED(evt)) {
214void DashboardInstrument::InitDataTextHeight(
const wxString& sampleText,
221 f = m_Properties->m_DataFont.GetChosenFont();
223 f = g_pFontData->GetChosenFont();
225 dc.GetTextExtent(sampleText, &sampleWidth, &m_DataTextHeight, 0, 0, &f);
228void DashboardInstrument::InitTitleSize() {
231 m_InstrumentSpacing = g_iInstrumentSpacing;
234 if (!m_Properties->m_Title.IsEmpty()) m_title = m_Properties->m_Title;
235 f = m_Properties->m_TitleFont.GetChosenFont();
236 if (m_Properties->m_InstrumentSpacing >= 0)
237 m_InstrumentSpacing = m_Properties->m_InstrumentSpacing;
239 f = g_pFontTitle->GetChosenFont();
241 dc.GetTextExtent(m_title, &m_TitleWidth, &m_TitleHeight, 0, 0, &f);
244void DashboardInstrument::InitTitleAndDataPosition(
int drawHeight) {
245 m_DataRightAlign = (g_DataAlignment & wxALIGN_RIGHT) != 0;
246 m_DataMargin = g_iDataMargin;
249 if (m_Properties->m_DataAlignment != wxALIGN_INVALID)
250 m_DataRightAlign = (m_Properties->m_DataAlignment & wxALIGN_RIGHT) != 0;
251 if (m_Properties->m_DataMargin >= 0)
252 m_DataMargin = m_Properties->m_DataMargin;
255 m_TitleRightAlign = (g_TitleAlignment & wxALIGN_RIGHT) != 0;
256 m_TitleTop = m_DataTextHeight * g_TitleVerticalOffset;
257 m_DataTop = m_TitleHeight;
258 if ((g_TitleAlignment & wxALIGN_BOTTOM) != 0) {
259 m_TitleTop = drawHeight + (m_DataTextHeight * g_TitleVerticalOffset);
264int DashboardInstrument::GetFullHeight(
int drawHeight) {
265 int h = m_TitleTop + m_TitleHeight + drawHeight + m_InstrumentSpacing;
266 if ((g_TitleAlignment & wxALIGN_BOTTOM) != 0) {
267 h = m_TitleTop + m_TitleHeight + m_InstrumentSpacing;
273int DashboardInstrument::GetDataBottom(
int clientHeight) {
274 if ((g_TitleAlignment & wxALIGN_BOTTOM) != 0) {
275 return clientHeight - m_TitleHeight - m_InstrumentSpacing - 3;
277 return clientHeight - m_InstrumentSpacing;
280void DashboardInstrument::SetDataFont(wxGCDC* dc) {
284 f = m_Properties->m_DataFont.GetChosenFont();
286 dc->SetTextForeground(
287 GetColourSchemeFont(m_Properties->m_DataFont.GetColour()));
289 f = g_pFontData->GetChosenFont();
291 dc->SetTextForeground(GetColourSchemeFont(g_pFontData->GetColour()));
295void DashboardInstrument::OnPaint(wxPaintEvent& WXUNUSED(event)) {
296 wxAutoBufferedPaintDC pdc(
this);
299 _T(
"DashboardInstrument::OnPaint() fatal: ")
300 _T(
"wxAutoBufferedPaintDC.IsOk() false."));
304 wxSize size = GetClientSize();
305 if (size.x == 0 || size.y == 0) {
306 wxLogMessage(_T(
"DashboardInstrument::OnPaint() fatal: Zero size DC."));
310#if wxUSE_GRAPHICS_CONTEXT
318 GetColourSchemeBackgroundColour(m_Properties->m_DataBackgroundColour));
320 GetGlobalColor(_T(
"DASHB"), &cl);
321 dc.SetBackground(cl);
325 dc.SetPen(*wxTRANSPARENT_PEN);
326 dc.DrawRectangle(0, 0, size.x, size.y);
332 if (!m_drawSoloInPane) {
334 pen.SetStyle(wxPENSTYLE_SOLID);
336 pen.SetColour(GetColourSchemeBackgroundColour(
337 m_Properties->m_TitleBackgroundColour));
339 dc.SetBrush(GetColourSchemeBackgroundColour(
340 m_Properties->m_TitleBackgroundColour));
342 GetGlobalColor(_T(
"DASHL"), &cl);
348 dc.DrawRoundedRectangle(0, m_TitleTop, size.x, m_TitleHeight, 3);
351 f = m_Properties->m_TitleFont.GetChosenFont();
353 dc.SetTextForeground(
354 GetColourSchemeFont(m_Properties->m_TitleFont.GetColour()));
355 dc.SetTextBackground(GetColourSchemeBackgroundColour(
356 m_Properties->m_TitleBackgroundColour));
358 f = g_pFontTitle->GetChosenFont();
360 dc.SetTextForeground(GetColourSchemeFont(g_pFontTitle->GetColour()));
361 GetGlobalColor(_T(
"DASHL"), &cl);
362 dc.SetTextBackground(cl);
366 if (m_TitleRightAlign) {
368 GetClientSize().GetWidth() - m_TitleWidth - g_iTitleMargin,
371 dc.DrawText(m_title, g_iTitleMargin, m_TitleTop);
382DashboardInstrument_Single::DashboardInstrument_Single(
383 wxWindow* pparent, wxWindowID
id, wxString title,
390wxSize DashboardInstrument_Single::GetSize(
int orient, wxSize hint) {
393 InitDataTextHeight(_T(
"000"), w);
395 int drawHeight = m_DataTextHeight * (1 + g_TitleVerticalOffset);
396 InitTitleAndDataPosition(drawHeight);
397 int h = GetFullHeight(drawHeight);
399 if (orient == wxHORIZONTAL) {
400 return wxSize(wxMax(w + m_DataMargin, DefaultWidth), wxMax(hint.y, h));
402 return wxSize(wxMax(hint.x, wxMax(w + m_DataMargin, DefaultWidth)), h);
406void DashboardInstrument_Single::Draw(wxGCDC* dc) {
410 if (m_DataMargin < 0)
411 m_DataMargin = m_TitleHeight;
414 if (m_DataRightAlign) {
416 dc->GetTextExtent(m_data, &w, &h, 0, 0);
417 x1 = GetClientSize().GetWidth() - w - m_DataMargin;
420 dc->DrawText(m_data, x1, m_DataTop);
423void DashboardInstrument_Single::SetData(DASH_CAP st,
double data,
425 if (m_cap_flag.test(st)) {
426 if (!std::isnan(data)) {
428 (m_Properties ? (m_Properties->m_ShowUnit == 1) : g_bShowUnit);
430 (m_Properties && m_Properties->m_Format !=
"" ? m_Properties->m_Format
433 m_data = wxString::Format(format, data) +
434 (showUnit ? DEGREE_SIGN + _T(
"C") :
"");
435 else if (
unit == _T(
"\u00B0"))
436 m_data = wxString::Format(format, data) + (showUnit ? DEGREE_SIGN :
"");
437 else if (
unit == _T(
"\u00B0T"))
438 m_data = wxString::Format(format, data) +
439 (showUnit ? DEGREE_SIGN + _(
" true") :
"");
440 else if (
unit == _T(
"\u00B0M"))
441 m_data = wxString::Format(format, data) +
442 (showUnit ? DEGREE_SIGN + _(
" mag") :
"");
443 else if (
unit == _T(
"\u00B0L"))
444 m_data = _T(
">") + wxString::Format(format, data) +
445 (showUnit ? DEGREE_SIGN :
"");
446 else if (
unit == _T(
"\u00B0R"))
447 m_data = wxString::Format(format, data) +
448 (showUnit ? DEGREE_SIGN + _T(
"<") :
"");
449 else if (
unit == _T(
"N"))
451 wxString::Format(format, data) + (showUnit ? _T(
" Kts") : _T(
""));
461 wxString::Format(format, data) + (showUnit ? _T(
" ") +
unit :
"");
475DashboardInstrument_Position::DashboardInstrument_Position(
476 wxWindow* pparent, wxWindowID
id, wxString title,
479 m_cap_flag.set(cap_flag2);
483 m_cap_flag1 = cap_flag1;
484 m_cap_flag2 = cap_flag2;
487wxSize DashboardInstrument_Position::GetSize(
int orient, wxSize hint) {
490 InitDataTextHeight(_T(
"000 00.0000 W"), w);
493 m_DataTextHeight * 2 + m_DataTextHeight * g_TitleVerticalOffset;
494 InitTitleAndDataPosition(drawHeight);
495 int h = GetFullHeight(drawHeight);
497 if (orient == wxHORIZONTAL) {
498 return wxSize(wxMax(w + m_DataMargin, DefaultWidth), wxMax(hint.y, h));
500 return wxSize(wxMax(hint.x, wxMax(w + m_DataMargin, DefaultWidth)), h);
504void DashboardInstrument_Position::Draw(wxGCDC* dc) {
508 x1 = x2 = m_DataMargin;
510 if (m_DataRightAlign) {
512 dc->GetTextExtent(m_data1, &w, &h, 0, 0);
513 x1 = GetClientSize().GetWidth() - w - m_DataMargin;
514 dc->GetTextExtent(m_data2, &w, &h, 0, 0);
515 x2 = GetClientSize().GetWidth() - w - m_DataMargin;
518 dc->DrawText(m_data1, x1, m_DataTop);
519 dc->DrawText(m_data2, x2, m_DataTop + m_DataTextHeight);
522void DashboardInstrument_Position::SetData(DASH_CAP st,
double data,
524 if (std::isnan(data))
return;
525 if (st == m_cap_flag1) {
526 m_data1 = toSDMM(1, data);
528 }
else if (st == m_cap_flag2) {
529 m_data2 = toSDMM(2, data);
539wxString toSDMM(
int NEflag,
double a) {
549 m = (long)((a - (
double)d) * 60000.0);
556 s.Printf(_T (
"%d %02ld.%03ld'" ), d, m / 1000, m % 1000);
566 s.Printf(_T (
"%03d %02ld.%03ld %c" ), d, m / 1000, (m % 1000), c);
567 }
else if (NEflag == 2) {
574 s.Printf(_T (
"%03d %02ld.%03ld %c" ), d, m / 1000, (m % 1000), c);