OpenCPN Partial API docs
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 
34 extern ColorScheme global_color_scheme;
35 extern S57QueryDialog* g_pObjectQueryDialog;
36 extern int g_S57_dialog_sx;
37 extern int g_S57_dialog_sy;
38 extern int g_S57_extradialog_sx;
39 extern int g_S57_extradialog_sy;
40 extern bool g_bresponsive;
41 
42 // Private class implementations
43 class MessageHardBreakWrapper : public wxTextWrapper {
44 public:
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 
53 protected:
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 
63 private:
64  wxString m_wrapped;
65  int m_lineCount;
66  wxArrayString m_array;
67 };
68 
69 IMPLEMENT_CLASS(S57QueryDialog, wxFrame)
70 // S57QueryDialog event table definition
71 BEGIN_EVENT_TABLE(S57QueryDialog, wxFrame) // ws wxDialog
72 EVT_SIZE(S57QueryDialog::OnSize)
73 EVT_CLOSE(S57QueryDialog::OnClose)
74 EVT_HTML_LINK_CLICKED(wxID_ANY, S57QueryDialog::OnHtmlLinkClicked)
75 EVT_CHAR_HOOK(S57QueryDialog::OnKey)
76 END_EVENT_TABLE()
77 
78 S57QueryDialog::S57QueryDialog() { Init(); }
79 
80 S57QueryDialog::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 
87 S57QueryDialog::~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 
95 void S57QueryDialog::Init() {}
96 
97 bool 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 
127 void 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 
148 void 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 
172 void 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 
191 void S57QueryDialog::OnKey(wxKeyEvent& ke) {
192  if (ke.GetKeyCode() == WXK_ESCAPE)
193  Close(true);
194  else
195  ke.Skip();
196 }
197 
198 void S57QueryDialog::SetHTMLPage(wxString& page) {
199  m_phtml->SetPage(page);
200  SetColorScheme();
201 }
202 
203 void S57QueryDialog::OnSize(wxSizeEvent& event) {
204  g_S57_dialog_sx = GetSize().x;
205  g_S57_dialog_sy = GetSize().y;
206  wxFrame::OnSize(event);
207 }
208 
209 void 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 
216 void 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  wxString str;
233  str = txf.GetFirstLine();
234  do {
235  MessageHardBreakWrapper wrapper(ExtraObjInfoDlg->m_phtml, str,
236  m_phtml->GetSize().x * 9 / 10);
237  contents += wrapper.GetWrapped();
238  contents += "<br>";
239 
240  str = txf.GetNextLine();
241  } while (!txf.Eof());
242 
243  ExtraObjInfoDlg->m_phtml->SetPage(contents);
244  }
245  }
246 
247  ExtraObjInfoDlg->SetColorScheme();
248 
249 #ifdef __OCPN__ANDROID__
250  ExtraObjInfoDlg->SetSize(GetSize().x - 40, GetSize().y - 40);
251 #endif
252 
253  ExtraObjInfoDlg->Show(true);
254 }
255 
257 
258 IMPLEMENT_CLASS(S57ExtraQueryInfoDlg, wxFrame)
259 // S57QueryDialog event table definition
260 BEGIN_EVENT_TABLE(S57ExtraQueryInfoDlg, wxFrame) // ws wxDialog
261 EVT_SIZE(S57ExtraQueryInfoDlg::OnSize)
262 EVT_CLOSE(S57ExtraQueryInfoDlg::OnClose)
263 END_EVENT_TABLE()
264 
266 
267 S57ExtraQueryInfoDlg::S57ExtraQueryInfoDlg(wxWindow* parent, wxWindowID id,
268  const wxString& caption,
269  const wxPoint& pos,
270  const wxSize& size, long style) {
271  Init();
272  Create(parent, id, caption, pos, size, style);
273 }
274 bool S57ExtraQueryInfoDlg::Create(wxWindow* parent, wxWindowID id,
275  const wxString& caption, const wxPoint& pos,
276  const wxSize& size, long style) {
277  long wstyle = wxDEFAULT_FRAME_STYLE | wxFRAME_FLOAT_ON_PARENT;
278 
279  if (!wxFrame::Create(parent, id, caption, pos, size, wstyle)) return false;
280 
281  wxFont* dFont = GetOCPNScaledFont(_("ObjectQuery"));
282 
283  SetFont(*dFont);
284  CreateControls();
285 
286  DimeControl(this);
287  return true;
288 }
289 S57ExtraQueryInfoDlg::~S57ExtraQueryInfoDlg() {
290  g_S57_extradialog_sx = GetSize().x;
291  g_S57_extradialog_sy = GetSize().y;
292 }
293 
294 void S57ExtraQueryInfoDlg::OnSize(wxSizeEvent& event) {
295  g_S57_extradialog_sx = GetSize().x;
296  g_S57_extradialog_sy = GetSize().y;
297  wxFrame::OnSize(event);
298 }
299 
300 void S57ExtraQueryInfoDlg::OnClose(wxCloseEvent& event) {
301  g_S57_extradialog_sx = GetSize().x;
302  g_S57_extradialog_sy = GetSize().y;
303  Destroy();
304 }
S57ExtraQueryInfoDlg()
Constructors.
S57QueryDialog()
Constructors.
General purpose GUI support.