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;
41
42// Private class implementations
43class MessageHardBreakWrapper : public wxTextWrapper {
44public:
45 MessageHardBreakWrapper(wxWindow* win, const wxString& text, int widthMax) {
46 m_lineCount = 0;
47 Wrap(win, text, widthMax);
48 }
49 wxString const& GetWrapped() const { return m_wrapped; }
50 int const GetLineCount() const { return m_lineCount; }
51 wxArrayString GetLineArray() { return m_array; }
52
53protected:
54 virtual void OnOutputLine(const wxString& line) {
55 m_wrapped += line;
56 m_array.Add(line);
57 }
58 virtual void OnNewLine() {
59 m_wrapped += '\n';
60 m_lineCount++;
61 }
62
63private:
64 wxString m_wrapped;
65 int m_lineCount;
66 wxArrayString m_array;
67};
68
69IMPLEMENT_CLASS(S57QueryDialog, wxFrame)
70// S57QueryDialog event table definition
71BEGIN_EVENT_TABLE(S57QueryDialog, wxFrame) // ws wxDialog
72EVT_SIZE(S57QueryDialog::OnSize)
73EVT_CLOSE(S57QueryDialog::OnClose)
74EVT_HTML_LINK_CLICKED(wxID_ANY, S57QueryDialog::OnHtmlLinkClicked)
75EVT_CHAR_HOOK(S57QueryDialog::OnKey)
76END_EVENT_TABLE()
77
79
80S57QueryDialog::S57QueryDialog(wxWindow* parent, wxWindowID id,
81 const wxString& caption, const wxPoint& pos,
82 const wxSize& size, long style) {
83 Init();
84 Create(parent, id, caption, pos, size, style);
85}
86
87S57QueryDialog::~S57QueryDialog() {
88 g_S57_dialog_sx = GetSize().x;
89 g_S57_dialog_sy = GetSize().y;
90 m_btnOK->Disconnect(wxEVT_COMMAND_BUTTON_CLICKED,
91 wxCommandEventHandler(S57QueryDialog::OnOKClick), NULL,
92 this);
93}
94
95void S57QueryDialog::Init() {}
96
97bool S57QueryDialog::Create(wxWindow* parent, wxWindowID id,
98 const wxString& caption, const wxPoint& pos,
99 const wxSize& size, long style) {
100 long wstyle = wxDEFAULT_FRAME_STYLE | wxFRAME_FLOAT_ON_PARENT;
101
102 if (!wxFrame::Create(parent, id, caption, pos, size, wstyle)) return false;
103
104 wxFont* dFont = GetOCPNScaledFont(_("ObjectQuery"));
105
106 SetFont(*dFont);
107 CreateControls();
108
109 m_createsize = size;
110 /*
111 // This ensures that the dialog cannot be sized smaller
112 // than the minimum size
113 GetSizer()->SetSizeHints( this );
114
115 // Explicitely set the size
116 SetSize( size );
117
118 // Centre the dialog on the parent or (if none) screen
119 Centre();
120 */
121 RecalculateSize();
122
123 DimeControl(this);
124 return true;
125}
126
127void S57QueryDialog::RecalculateSize(void) {
128 // Make an estimate of the dialog size, without scrollbars showing
129
130 wxSize esize = m_createsize;
131 if (g_bresponsive) {
132 esize = GetParent()->GetClientSize();
133 }
134
135 wxSize dsize = GetParent()->GetClientSize();
136 esize.y = wxMin(esize.y, dsize.y - (1 * GetCharHeight()));
137 esize.x = wxMin(esize.x, dsize.x - (1 * GetCharHeight()));
138 SetSize(esize);
139
140 wxSize fsize = GetSize();
141 fsize.y = wxMin(fsize.y, dsize.y - (2 * GetCharHeight()));
142 fsize.x = wxMin(fsize.x, dsize.x - (2 * GetCharHeight()));
143 SetSize(fsize);
144
145 Centre();
146}
147
148void S57QueryDialog::CreateControls() {
149 wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
150 SetSizer(topSizer);
151
152 long style = wxHW_SCROLLBAR_AUTO;
153 if (g_btouch) style |= wxHW_NO_SELECTION;
154
155 m_phtml =
156 new wxHtmlWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, style);
157
158 m_phtml->SetBorders(5);
159
160 m_phtml->SetMinSize(wxSize(100, 100)); // this will constrain the dialog, too
161 topSizer->Add(m_phtml, 1, wxBOTTOM | wxEXPAND, 10);
162
163 topSizer->FitInside(this);
164
165 m_btnOK = new wxButton(this, wxID_OK);
166 m_btnOK->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
167 wxCommandEventHandler(S57QueryDialog::OnOKClick), NULL,
168 this);
169 topSizer->Add(m_btnOK, 0, wxALIGN_CENTER_HORIZONTAL | wxBOTTOM, 5);
170}
171
172void S57QueryDialog::SetColorScheme(void) {
173 DimeControl(this);
174 wxColor bg = GetBackgroundColour();
175 m_phtml->SetBackgroundColour(bg);
176 SetBackgroundColour(
177 bg); // This looks like non-sense, but is needed for __WXGTK__
178 // to get colours to propagate down the control's family tree.
179
180#ifdef __WXQT__
181 // wxQT has some trouble clearing the background of HTML window...
182 wxBitmap tbm(GetSize().x, GetSize().y, -1);
183 wxMemoryDC tdc(tbm);
184 // wxColour cback = GetGlobalColor( _T("YELO1") );
185 tdc.SetBackground(bg);
186 tdc.Clear();
187 m_phtml->SetBackgroundImage(tbm);
188#endif
189}
190
191void S57QueryDialog::OnKey(wxKeyEvent& ke) {
192 if (ke.GetKeyCode() == WXK_ESCAPE)
193 Close(true);
194 else
195 ke.Skip();
196}
197
198void S57QueryDialog::SetHTMLPage(wxString& page) {
199 m_phtml->SetPage(page);
200 SetColorScheme();
201}
202
203void S57QueryDialog::OnSize(wxSizeEvent& event) {
204 g_S57_dialog_sx = GetSize().x;
205 g_S57_dialog_sy = GetSize().y;
206 wxFrame::OnSize(event);
207}
208
209void S57QueryDialog::OnClose(wxCloseEvent& event) {
210 g_S57_dialog_sx = GetSize().x;
211 g_S57_dialog_sy = GetSize().y;
212 Destroy();
213 g_pObjectQueryDialog = NULL;
214}
215
216void S57QueryDialog::OnHtmlLinkClicked(wxHtmlLinkEvent& event) {
217 S57ExtraQueryInfoDlg* ExtraObjInfoDlg = new S57ExtraQueryInfoDlg(
218 GetParent(), wxID_ANY, _("Extra Object Info"),
219 wxPoint(GetPosition().x + 20, GetPosition().y + 20),
220 wxSize(g_S57_extradialog_sx, g_S57_extradialog_sy));
221
222 // Check te kind of file, load text files serial and pictures direct
223 wxFileName filen(event.GetLinkInfo().GetHref());
224 wxString Extensions = wxString("txt,html,rtf");
225
226 if (Extensions.Find(filen.GetExt().Lower()) == wxNOT_FOUND)
227 ExtraObjInfoDlg->m_phtml->LoadPage(event.GetLinkInfo().GetHref());
228 else {
229 wxTextFile txf(filen.GetFullPath());
230 if (txf.Open()) {
231 wxString contents;
232 if (filen.GetExt().Lower() == "txt") {
233 contents +=
234 "<font color="
235 "" +
236 GetForegroundColour().GetAsString(wxC2S_HTML_SYNTAX) +
237 ""
238 ">";
239 }
240 wxString str;
241 str = txf.GetFirstLine();
242 do {
243 MessageHardBreakWrapper wrapper(ExtraObjInfoDlg->m_phtml, str,
244 m_phtml->GetSize().x * 9 / 10);
245 contents += wrapper.GetWrapped();
246 contents += "<br>";
247
248 str = txf.GetNextLine();
249 } while (!txf.Eof());
250 if (filen.GetExt().Lower() == "txt") {
251 contents += "</font>";
252 }
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 long wstyle = wxDEFAULT_FRAME_STYLE | wxFRAME_FLOAT_ON_PARENT;
289
290 if (!wxFrame::Create(parent, id, caption, pos, size, wstyle)) return false;
291
292 wxFont* dFont = GetOCPNScaledFont(_("ObjectQuery"));
293
294 SetFont(*dFont);
295 CreateControls();
296
297 DimeControl(this);
298 return true;
299}
300S57ExtraQueryInfoDlg::~S57ExtraQueryInfoDlg() {
301 g_S57_extradialog_sx = GetSize().x;
302 g_S57_extradialog_sy = GetSize().y;
303}
304
305void S57ExtraQueryInfoDlg::OnSize(wxSizeEvent& event) {
306 g_S57_extradialog_sx = GetSize().x;
307 g_S57_extradialog_sy = GetSize().y;
308 wxFrame::OnSize(event);
309}
310
311void S57ExtraQueryInfoDlg::OnClose(wxCloseEvent& event) {
312 g_S57_extradialog_sx = GetSize().x;
313 g_S57_extradialog_sy = GetSize().y;
314 Destroy();
315}
Dialog for displaying extra query information for S57 objects.
S57ExtraQueryInfoDlg()
Constructors.
Dialog for displaying query results of S57 objects.
S57QueryDialog()
Constructors.
wxFont * GetOCPNScaledFont(wxString item, int default_size)
Retrieves a font from FontMgr, optionally scaled for physical readability.
Definition gui_lib.cpp:54
General purpose GUI support.