OpenCPN Partial API docs
Loading...
Searching...
No Matches
S57QueryDialog.cpp
1/******************************************************************************
2 *
3 * Project: OpenCPN
4 *
5 ***************************************************************************
6 * Copyright (C) 2013 by David S. Register *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
22 ***************************************************************************
23 */
24
25#include <wx/wxprec.h>
26#include <wx/wxhtml.h>
27
28#include "S57QueryDialog.h"
29#include "navutil.h"
30#include "gui_lib.h"
31#include <wx/textwrapper.h>
32#include "color_types.h"
33
34extern ColorScheme global_color_scheme;
35extern S57QueryDialog* g_pObjectQueryDialog;
36extern int g_S57_dialog_sx;
37extern int g_S57_dialog_sy;
38extern int g_S57_extradialog_sx;
39extern int g_S57_extradialog_sy;
40extern bool g_bresponsive;
41extern bool g_btouch;
42
43// Private class implementations
44class MessageHardBreakWrapper : public wxTextWrapper {
45public:
46 MessageHardBreakWrapper(wxWindow* win, const wxString& text, int widthMax) {
47 m_lineCount = 0;
48 Wrap(win, text, widthMax);
49 }
50 wxString const& GetWrapped() const { return m_wrapped; }
51 int const GetLineCount() const { return m_lineCount; }
52 wxArrayString GetLineArray() { return m_array; }
53
54protected:
55 virtual void OnOutputLine(const wxString& line) {
56 m_wrapped += line;
57 m_array.Add(line);
58 }
59 virtual void OnNewLine() {
60 m_wrapped += '\n';
61 m_lineCount++;
62 }
63
64private:
65 wxString m_wrapped;
66 int m_lineCount;
67 wxArrayString m_array;
68};
69
70IMPLEMENT_CLASS(S57QueryDialog, wxFrame)
71// S57QueryDialog event table definition
72BEGIN_EVENT_TABLE(S57QueryDialog, wxFrame) // ws wxDialog
73EVT_SIZE(S57QueryDialog::OnSize)
74EVT_CLOSE(S57QueryDialog::OnClose)
75EVT_HTML_LINK_CLICKED(wxID_ANY, S57QueryDialog::OnHtmlLinkClicked)
76EVT_CHAR_HOOK(S57QueryDialog::OnKey)
77END_EVENT_TABLE()
78
80
81S57QueryDialog::S57QueryDialog(wxWindow* parent, wxWindowID id,
82 const wxString& caption, const wxPoint& pos,
83 const wxSize& size, long style) {
84 Init();
85 Create(parent, id, caption, pos, size, style);
86}
87
88S57QueryDialog::~S57QueryDialog() {
89 g_S57_dialog_sx = GetSize().x;
90 g_S57_dialog_sy = GetSize().y;
91 m_btnOK->Disconnect(wxEVT_COMMAND_BUTTON_CLICKED,
92 wxCommandEventHandler(S57QueryDialog::OnOKClick), NULL,
93 this);
94}
95
96void S57QueryDialog::Init() {}
97
98bool S57QueryDialog::Create(wxWindow* parent, wxWindowID id,
99 const wxString& caption, const wxPoint& pos,
100 const wxSize& size, long style) {
101 // As a display optimization....
102 // if current color scheme is other than DAY,
103 // Then create the dialog ..WITHOUT.. borders and title bar.
104 // This way, any window decorations set by external themes, etc
105 // will not detract from night-vision
106
107 long wstyle = wxDEFAULT_FRAME_STYLE | wxFRAME_FLOAT_ON_PARENT;
108
109 if ((global_color_scheme != GLOBAL_COLOR_SCHEME_DAY) &&
110 (global_color_scheme != GLOBAL_COLOR_SCHEME_RGB))
111 wstyle |= (wxNO_BORDER);
112
113 if (!wxFrame::Create(parent, id, caption, pos, size, wstyle)) return false;
114
115 wxFont* dFont = GetOCPNScaledFont(_("ObjectQuery"));
116
117 SetFont(*dFont);
118 CreateControls();
119
120 m_createsize = size;
121 /*
122 // This ensures that the dialog cannot be sized smaller
123 // than the minimum size
124 GetSizer()->SetSizeHints( this );
125
126 // Explicitely set the size
127 SetSize( size );
128
129 // Centre the dialog on the parent or (if none) screen
130 Centre();
131 */
132 RecalculateSize();
133
134 DimeControl(this);
135 return true;
136}
137
138void S57QueryDialog::RecalculateSize(void) {
139 // Make an estimate of the dialog size, without scrollbars showing
140
141 wxSize esize = m_createsize;
142 if (g_bresponsive) {
143 esize = GetParent()->GetClientSize();
144 }
145
146 wxSize dsize = GetParent()->GetClientSize();
147 esize.y = wxMin(esize.y, dsize.y - (1 * GetCharHeight()));
148 esize.x = wxMin(esize.x, dsize.x - (1 * GetCharHeight()));
149 SetSize(esize);
150
151 wxSize fsize = GetSize();
152 fsize.y = wxMin(fsize.y, dsize.y - (2 * GetCharHeight()));
153 fsize.x = wxMin(fsize.x, dsize.x - (2 * GetCharHeight()));
154 SetSize(fsize);
155
156 Centre();
157}
158
159void S57QueryDialog::CreateControls() {
160 wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
161 SetSizer(topSizer);
162
163 long style = wxHW_SCROLLBAR_AUTO;
164 if (g_btouch) style |= wxHW_NO_SELECTION;
165
166 m_phtml =
167 new wxHtmlWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, style);
168
169 m_phtml->SetBorders(5);
170
171 m_phtml->SetMinSize(wxSize(100, 100)); // this will constrain the dialog, too
172 topSizer->Add(m_phtml, 1, wxBOTTOM | wxEXPAND, 10);
173
174 topSizer->FitInside(this);
175
176 m_btnOK = new wxButton(this, wxID_OK);
177 m_btnOK->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
178 wxCommandEventHandler(S57QueryDialog::OnOKClick), NULL,
179 this);
180 topSizer->Add(m_btnOK, 0, wxALIGN_CENTER_HORIZONTAL | wxBOTTOM, 5);
181}
182
183void S57QueryDialog::SetColorScheme(void) {
184 DimeControl(this);
185 wxColor bg = GetBackgroundColour();
186 m_phtml->SetBackgroundColour(bg);
187 SetBackgroundColour(
188 bg); // This looks like non-sense, but is needed for __WXGTK__
189 // to get colours to propagate down the control's family tree.
190
191#ifdef __WXQT__
192 // wxQT has some trouble clearing the background of HTML window...
193 wxBitmap tbm(GetSize().x, GetSize().y, -1);
194 wxMemoryDC tdc(tbm);
195 // wxColour cback = GetGlobalColor( _T("YELO1") );
196 tdc.SetBackground(bg);
197 tdc.Clear();
198 m_phtml->SetBackgroundImage(tbm);
199#endif
200}
201
202void S57QueryDialog::OnKey(wxKeyEvent& ke) {
203 if (ke.GetKeyCode() == WXK_ESCAPE)
204 Close(true);
205 else
206 ke.Skip();
207}
208
209void S57QueryDialog::SetHTMLPage(wxString& page) {
210 m_phtml->SetPage(page);
211 SetColorScheme();
212}
213
214void S57QueryDialog::OnSize(wxSizeEvent& event) {
215 g_S57_dialog_sx = GetSize().x;
216 g_S57_dialog_sy = GetSize().y;
217 wxFrame::OnSize(event);
218}
219
220void S57QueryDialog::OnClose(wxCloseEvent& event) {
221 g_S57_dialog_sx = GetSize().x;
222 g_S57_dialog_sy = GetSize().y;
223 Destroy();
224 g_pObjectQueryDialog = NULL;
225}
226
227void S57QueryDialog::OnHtmlLinkClicked(wxHtmlLinkEvent& event) {
228 S57ExtraQueryInfoDlg* ExtraObjInfoDlg = new S57ExtraQueryInfoDlg(
229 GetParent(), wxID_ANY, _("Extra Object Info"),
230 wxPoint(GetPosition().x + 20, GetPosition().y + 20),
231 wxSize(g_S57_extradialog_sx, g_S57_extradialog_sy));
232
233 // Check te kind of file, load text files serial and pictures direct
234 wxFileName filen(event.GetLinkInfo().GetHref());
235 wxString Extensions = wxString("txt,html,rtf");
236
237 if (Extensions.Find(filen.GetExt().Lower()) == wxNOT_FOUND)
238 ExtraObjInfoDlg->m_phtml->LoadPage(event.GetLinkInfo().GetHref());
239 else {
240 wxTextFile txf(filen.GetFullPath());
241 if (txf.Open()) {
242 wxString contents;
243 wxString str;
244 str = txf.GetFirstLine();
245 do {
246 MessageHardBreakWrapper wrapper(ExtraObjInfoDlg->m_phtml, str,
247 m_phtml->GetSize().x * 9 / 10);
248 contents += wrapper.GetWrapped();
249 contents += "<br>";
250
251 str = txf.GetNextLine();
252 } while (!txf.Eof());
253
254 ExtraObjInfoDlg->m_phtml->SetPage(contents);
255 }
256 }
257
258 ExtraObjInfoDlg->SetColorScheme();
259
260#ifdef __OCPN__ANDROID__
261 ExtraObjInfoDlg->SetSize(GetSize().x - 40, GetSize().y - 40);
262#endif
263
264 ExtraObjInfoDlg->Show(true);
265}
266
268
269IMPLEMENT_CLASS(S57ExtraQueryInfoDlg, wxFrame)
270// S57QueryDialog event table definition
271BEGIN_EVENT_TABLE(S57ExtraQueryInfoDlg, wxFrame) // ws wxDialog
272EVT_SIZE(S57ExtraQueryInfoDlg::OnSize)
273EVT_CLOSE(S57ExtraQueryInfoDlg::OnClose)
274END_EVENT_TABLE()
275
277
278S57ExtraQueryInfoDlg::S57ExtraQueryInfoDlg(wxWindow* parent, wxWindowID id,
279 const wxString& caption,
280 const wxPoint& pos,
281 const wxSize& size, long style) {
282 Init();
283 Create(parent, id, caption, pos, size, style);
284}
285bool S57ExtraQueryInfoDlg::Create(wxWindow* parent, wxWindowID id,
286 const wxString& caption, const wxPoint& pos,
287 const wxSize& size, long style) {
288 // As a display optimization....
289 // if current color scheme is other than DAY,
290 // Then create the dialog ..WITHOUT.. borders and title bar.
291 // This way, any window decorations set by external themes, etc
292 // will not detract from night-vision
293
294 long wstyle = wxDEFAULT_FRAME_STYLE | wxFRAME_FLOAT_ON_PARENT;
295
296 if ((global_color_scheme != GLOBAL_COLOR_SCHEME_DAY) &&
297 (global_color_scheme != GLOBAL_COLOR_SCHEME_RGB))
298 wstyle |= (wxNO_BORDER);
299
300 if (!wxFrame::Create(parent, id, caption, pos, size, wstyle)) return false;
301
302 wxFont* dFont = GetOCPNScaledFont(_("ObjectQuery"));
303
304 SetFont(*dFont);
305 CreateControls();
306
307 DimeControl(this);
308 return true;
309}
310S57ExtraQueryInfoDlg::~S57ExtraQueryInfoDlg() {
311 g_S57_extradialog_sx = GetSize().x;
312 g_S57_extradialog_sy = GetSize().y;
313}
314
315void S57ExtraQueryInfoDlg::OnSize(wxSizeEvent& event) {
316 g_S57_extradialog_sx = GetSize().x;
317 g_S57_extradialog_sy = GetSize().y;
318 wxFrame::OnSize(event);
319}
320
321void S57ExtraQueryInfoDlg::OnClose(wxCloseEvent& event) {
322 g_S57_extradialog_sx = GetSize().x;
323 g_S57_extradialog_sy = GetSize().y;
324 Destroy();
325}
S57ExtraQueryInfoDlg()
Constructors.
S57QueryDialog()
Constructors.