27#include <wx/html/htmlwin.h>
29#include "AISTargetQueryDialog.h"
34#include "ais_target_data.h"
35#include "ais_decoder.h"
37#include "routemanagerdialog.h"
38#include "OCPNPlatform.h"
40#include "route_point.h"
41#include "ocpn_frame.h"
44extern int g_ais_query_dialog_x;
45extern int g_ais_query_dialog_y;
46extern ColorScheme global_color_scheme;
48extern wxString g_default_wp_icon;
52extern std::vector<Track*> g_TrackList;
57#define xID_WPT_CREATE 10010
58#define xID_TRK_CREATE 10011
62EVT_BUTTON(xID_OK, AISTargetQueryDialog::OnIdOKClick)
63EVT_BUTTON(xID_WPT_CREATE, AISTargetQueryDialog::OnIdWptCreateClick)
64EVT_BUTTON(xID_TRK_CREATE, AISTargetQueryDialog::OnIdTrkCreateClick)
65EVT_CLOSE(AISTargetQueryDialog::OnClose)
66EVT_MOVE(AISTargetQueryDialog::OnMove)
67EVT_SIZE(AISTargetQueryDialog::OnSize)
68EVT_CHAR_HOOK(AISTargetQueryDialog::OnKey)
74 const wxString &caption,
76 const wxSize &size,
long style) {
78 Create(parent,
id, caption, pos, size, style);
81AISTargetQueryDialog::~AISTargetQueryDialog() {
delete m_pQueryTextCtl; }
85 m_pQueryTextCtl = NULL;
87 m_colorscheme = (ColorScheme)(-1);
90 m_bautoCentre =
false;
93void AISTargetQueryDialog::OnClose(wxCloseEvent &event) {
95 g_pais_query_dialog_active = NULL;
98void AISTargetQueryDialog::OnIdOKClick(wxCommandEvent &event) { Close(); }
100void AISTargetQueryDialog::OnKey(wxKeyEvent &ke) {
101 if (ke.GetKeyCode() == WXK_ESCAPE)
107void AISTargetQueryDialog::OnIdWptCreateClick(wxCommandEvent &event) {
109 auto td = g_pAIS->Get_Target_Data_From_MMSI(m_MMSI);
111 wxString n0 = wxString::Format(wxT(
"%s"), td->ShipName);
112 n0.Replace(_T(
"@"), _T(
" "));
114 wxString mmsi = wxString::Format(wxT(
"%i "), td->MMSI);
115 wxString n = _T(
"\"") + n0 + _T(
"\" ") + mmsi;
116 n.append(wxDateTime::Now().Format(wxT(
"%H:%M")));
120 new RoutePoint(td->Lat, td->Lon, g_default_wp_icon, n, wxEmptyString);
121 pWP->m_bIsolatedMark =
true;
122 pSelect->AddSelectableRoutePoint(td->Lat, td->Lon, pWP);
123 pConfig->AddNewWayPoint(pWP, -1);
125 if (pRouteManagerDialog && pRouteManagerDialog->IsShown())
126 pRouteManagerDialog->UpdateWptListCtrl();
127 gFrame->GetPrimaryCanvas()->undo->BeforeUndoableAction(
128 Undo_CreateWaypoint, pWP, Undo_HasParent, NULL);
129 gFrame->GetPrimaryCanvas()->undo->AfterUndoableAction(NULL);
135void AISTargetQueryDialog::OnIdTrkCreateClick(wxCommandEvent &event) {
137 auto td = g_pAIS->Get_Target_Data_From_MMSI(m_MMSI);
139 if (td->b_PersistTrack)
142 td->b_PersistTrack =
false;
143 g_pAIS->m_persistent_tracks.erase(td->MMSI);
144 m_createTrkBtn->SetLabel(_(
"Record Track"));
151 t->SetName(wxString::Format(_T(
"AIS %s (%u) %s %s"),
152 td->GetFullName().c_str(), td->MMSI,
153 wxDateTime::Now().FormatISODate().c_str(),
154 wxDateTime::Now().FormatISOTime().c_str()));
156 vector2D point(ptrack_point.m_lon, ptrack_point.m_lat);
157 tp1 = t->AddNewPoint(point, wxDateTime(ptrack_point.m_time).ToUTC());
159 pSelect->AddSelectableTrackSegment(tp->m_lat, tp->m_lon, tp1->m_lat,
160 tp1->m_lon, tp, tp1, t);
165 g_TrackList.push_back(t);
166 pConfig->AddNewTrack(t);
170 if (pRouteManagerDialog && pRouteManagerDialog->IsShown())
171 pRouteManagerDialog->UpdateTrkListCtrl();
177 _(
"The recently captured track of this target has been "
178 "recorded.\nDo you want to continue recording until the end "
179 "of the current OpenCPN session?"),
180 _(
"OpenCPN Info"), wxYES_NO | wxCENTER, 60)) {
181 td->b_PersistTrack =
true;
182 g_pAIS->m_persistent_tracks[td->MMSI] = t;
190 const wxString &caption,
const wxPoint &pos,
191 const wxSize &size,
long style) {
198 long wstyle = AIS_TARGET_QUERY_STYLE;
199 if ((global_color_scheme != GLOBAL_COLOR_SCHEME_DAY) &&
200 (global_color_scheme != GLOBAL_COLOR_SCHEME_RGB))
201 wstyle |= (wxNO_BORDER);
203 if (!wxFrame::Create(parent,
id, caption, pos, size, wstyle))
return false;
207 wxFont *dFont = FontMgr::Get().GetFont(_(
"AISTargetQuery"));
208 int font_size = wxMax(8, dFont->GetPointSize());
209 wxString face = dFont->GetFaceName();
211 face = _T(
"Monospace");
213 m_basefont = FontMgr::Get().FindOrCreateFont(font_size, wxFONTFAMILY_MODERN,
215 dFont->GetWeight(),
false, face);
217 SetFont(*m_basefont);
218 m_adjustedFontSize = dFont->GetPointSize();
219 m_control_font_size = dFont->GetPointSize();
223 SetColorScheme(global_color_scheme);
226 wxSize sz = g_Platform->getDisplaySize();
227 SetSizeHints(50, 50, sz.x - 20, sz.y - 40);
232 SetSize(-1, m_adjustedFontSize * 30);
238void AISTargetQueryDialog::SetMMSI(
int mmsi) {
241 auto td = g_pAIS->Get_Target_Data_From_MMSI(m_MMSI);
242 AdjustBestSize(td.get());
245void AISTargetQueryDialog::RecalculateSize() {
246 auto td = g_pAIS->Get_Target_Data_From_MMSI(m_MMSI);
247 AdjustBestSize(td.get());
251void AISTargetQueryDialog::SetColorScheme(ColorScheme cs) {
253 wxColor bg = GetBackgroundColour();
254 m_pQueryTextCtl->SetBackgroundColour(bg);
261 wxBitmap tbm(GetSize().x, GetSize().y, -1);
264 tdc.SetBackground(bg);
266 m_pQueryTextCtl->SetBackgroundImage(tbm);
269 if (cs != m_colorscheme) {
275void AISTargetQueryDialog::CreateControls() {
276 wxBoxSizer *topSizer =
new wxBoxSizer(wxVERTICAL);
280 new wxHtmlWindow(
this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
281 wxHW_SCROLLBAR_AUTO | wxHW_NO_SELECTION);
282 m_pQueryTextCtl->SetBorders(1);
283 topSizer->Add(m_pQueryTextCtl, 1, wxEXPAND, 5);
285 wxSizer *opt =
new wxBoxSizer(wxHORIZONTAL);
286 m_createWptBtn =
new wxButton(
this, xID_WPT_CREATE, _(
"Create Waypoint"),
287 wxDefaultPosition, wxDefaultSize, 0);
288 opt->Add(m_createWptBtn, 0, wxALL | wxEXPAND, 5);
290 m_createTrkBtn =
new wxButton(
this, xID_TRK_CREATE, _(
"Record Track"),
291 wxDefaultPosition, wxDefaultSize, 0);
292 opt->Add(m_createTrkBtn, 0, wxALL | wxEXPAND, 5);
293 topSizer->Add(opt, 0, wxALIGN_CENTER_HORIZONTAL | wxBOTTOM, 5);
295 topSizer->Add(
new wxButton(
this, xID_OK, _(
"OK")), 0,
296 wxALIGN_CENTER_HORIZONTAL | wxBOTTOM, 5);
301void AISTargetQueryDialog::UpdateText() {
304 if (!m_pQueryTextCtl)
return;
306 int scroll_x, scroll_y;
307 m_pQueryTextCtl->GetViewStart(&scroll_x, &scroll_y);
309 auto td = g_pAIS->Get_Target_Data_From_MMSI(m_MMSI);
313 wxColor bg = GetBackgroundColour();
314 m_pQueryTextCtl->SetBackgroundColour(bg);
315 SetBackgroundColour(bg);
318 if (td->b_PersistTrack)
319 m_createTrkBtn->SetLabel(_(
"Stop Tracking"));
321 m_createTrkBtn->SetLabel(_(
"Record Track"));
323 m_createWptBtn->Enable(td->b_positionOnceValid);
324 m_createTrkBtn->Enable(td->b_show_track);
326 RenderHTMLQuery(td.get());
330 SetColorScheme(m_colorscheme);
333 m_pQueryTextCtl->Scroll(scroll_x, scroll_y);
336void AISTargetQueryDialog::OnMove(wxMoveEvent &event) {
338 wxPoint p =
event.GetPosition();
339 g_ais_query_dialog_x = p.x;
340 g_ais_query_dialog_y = p.y;
345void AISTargetQueryDialog::OnSize(wxSizeEvent &event) {
event.Skip(); }
347void AISTargetQueryDialog::AdjustBestSize(
AisTargetData *td) {
350 wxSize origSize = GetSize();
367 wxSize szv = m_pQueryTextCtl->GetVirtualSize();
368 if (szv.x > m_pQueryTextCtl->GetSize().x) {
369 while ((szv.x > m_pQueryTextCtl->GetSize().x) &&
370 (m_adjustedFontSize > 8)) {
371 m_adjustedFontSize--;
374 m_pQueryTextCtl->Refresh();
375 m_pQueryTextCtl->Update();
377 szv = m_pQueryTextCtl->GetVirtualSize();
380 m_adjustedFontSize--;
383 wxSize szv = m_pQueryTextCtl->GetVirtualSize();
384 int csz = g_Platform->getDisplaySize().x * 8 / 10;
386 if (szv.x > m_pQueryTextCtl->GetSize().x) target_x = szv.x;
394 if (m_createWptBtn && m_createTrkBtn) {
395 wxSize psz = g_Platform->getDisplaySize();
398 wxFont *tFont = FontMgr::Get().FindOrCreateFont(
399 m_control_font_size, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL,
400 m_basefont->GetWeight(),
false, m_basefont->GetFaceName());
403 wxSize tsz = dc.GetTextExtent(m_createWptBtn->GetLabel() +
404 m_createTrkBtn->GetLabel());
406 float totalButtonWidth = tsz.x;
408 if (totalButtonWidth * 1.5 > psz.x) {
409 float delta = (float)totalButtonWidth * 2. / psz.x;
411 float font_size = m_control_font_size / delta;
413 wxFont *fp_font = FontMgr::Get().FindOrCreateFont(
414 font_size, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL,
415 m_basefont->GetWeight(),
false, m_basefont->GetFaceName());
417 m_createWptBtn->SetFont(*fp_font);
418 m_createTrkBtn->SetFont(*fp_font);
420 m_control_font_size = font_size;
429 if (m_createWptBtn) yb = m_createWptBtn->GetSize().y * 4;
431 wxSize szyv = m_pQueryTextCtl->GetVirtualSize();
432 int csz = g_Platform->getDisplaySize().y * 85 / 100;
433 if ((szyv.y + yb) < csz) {
434 if (szyv.y > m_pQueryTextCtl->GetSize().y)
435 target_y = (szyv.y * 11 / 10) + yb;
439 target_x = szyv.x * 11 / 10;
442 SetSize(target_x, target_y);
444 wxSize nowSize = GetSize();
446 if (nowSize != origSize) {
447 if (m_bautoCentre) Centre();
451void AISTargetQueryDialog::RenderHTMLQuery(
AisTargetData *td) {
452 int font_size = m_adjustedFontSize;
453 wxFont *fp_font = FontMgr::Get().FindOrCreateFont(
454 font_size, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL,
455 m_basefont->GetWeight(),
false, m_basefont->GetFaceName());
460 for (
int i = -2; i < 5; i++) {
461 sizes[i + 2] = fp_font->GetPointSize() + i + (i > 0 ? i : 0);
465 wxColor bg = GetBackgroundColour();
466 wxColor fg = GetForegroundColour();
469 _T(
"<html><body bgcolor=#%02x%02x%02x><font ")
470 _T(
"color=#%02x%02x%02x><center>"),
471 bg.Red(), bg.Green(), bg.Blue(), fg.Red(), fg.Green(), fg.Blue());
473 html << td->BuildQueryResult();
475 html << _T(
"</center></font></body></html>");
477 m_pQueryTextCtl->SetFonts(fp_font->GetFaceName(), fp_font->GetFaceName(),
480 wxCharBuffer buf = html.ToUTF8();
482 m_pQueryTextCtl->SetPage(html);
AISTargetQueryDialog()
Constructors.
bool Create(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &caption=_("Object Query"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=AIS_TARGET_QUERY_STYLE)
Creation.
void Init()
Initialise our variables.