Audacity  2.2.2
LinkingHtmlWindow.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  LinkingHtmlWindow.cpp
6 
7  Vaughan Johnson
8  Dominic Mazzoni
9 
10  utility fn and
11  descendant of HtmlWindow that opens links in the user's
12  default browser
13 
14 **********************************************************************/
15 
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include <wx/wxprec.h>
18 
19 #include "../Audacity.h"
20 
21 #include <wx/mimetype.h>
22 #include <wx/filename.h>
23 #include <wx/uri.h>
24 #include <wx/settings.h>
25 #include <wx/log.h>
26 
27 #include "LinkingHtmlWindow.h"
28 #include "../HelpText.h"
29 #include "../FileNames.h"
30 #include "../Prefs.h"
31 #include "ErrorDialog.h"
32 #include "HelpSystem.h"
33 
34 BEGIN_EVENT_TABLE(BrowserDialog, wxDialogWrapper)
35  EVT_BUTTON(wxID_FORWARD, BrowserDialog::OnForward)
36  EVT_BUTTON(wxID_BACKWARD, BrowserDialog::OnBackward)
37  EVT_BUTTON(wxID_CANCEL, BrowserDialog::OnClose)
38  EVT_KEY_DOWN(BrowserDialog::OnKeyDown)
40 
41 
42 BrowserDialog::BrowserDialog(wxWindow *pParent, const wxString &title)
43  : wxDialogWrapper{ pParent, ID, title, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER /*| wxMAXIMIZE_BOX */ }
44 {
45  int width, height;
46  const int minWidth = 400;
47  const int minHeight = 250;
48 
49  gPrefs->Read(wxT("/GUI/BrowserWidth"), &width, minWidth);
50  gPrefs->Read(wxT("/GUI/BrowserHeight"), &height, minHeight);
51 
52  if (width < minWidth || width > wxSystemSettings::GetMetric(wxSYS_SCREEN_X))
53  width = minWidth;
54  if (height < minHeight || height > wxSystemSettings::GetMetric(wxSYS_SCREEN_Y))
55  height = minHeight;
56 
57  SetMinSize(wxSize(minWidth, minHeight));
58  SetSize(wxDefaultPosition.x, wxDefaultPosition.y, width, height, wxSIZE_AUTO);
59 }
60 
61 void BrowserDialog::OnForward(wxCommandEvent & WXUNUSED(event))
62 {
63  mpHtml->HistoryForward();
64  UpdateButtons();
65 }
66 
67 void BrowserDialog::OnBackward(wxCommandEvent & WXUNUSED(event))
68 {
69  mpHtml->HistoryBack();
70  UpdateButtons();
71 }
72 
73 void BrowserDialog::OnClose(wxCommandEvent & WXUNUSED(event))
74 {
75  if (IsModal() && !mDismissed)
76  {
77  mDismissed = true;
78  EndModal(wxID_CANCEL);
79  }
80  auto parent = GetParent();
81 
82  gPrefs->Write(wxT("/GUI/BrowserWidth"), GetSize().GetX());
83  gPrefs->Write(wxT("/GUI/BrowserHeight"), GetSize().GetY());
84  gPrefs->Flush();
85 
86 #ifdef __WXMAC__
87  auto grandparent = GetParent()->GetParent();
88 #endif
89 
90  parent->Destroy();
91 
92 #ifdef __WXMAC__
93  if(grandparent && grandparent->IsShown()) {
94  grandparent->Raise();
95  }
96 #endif
97 }
98 
99 void BrowserDialog::OnKeyDown(wxKeyEvent & event)
100 {
101  bool bSkip = true;
102  if (event.GetKeyCode() == WXK_ESCAPE)
103  {
104  bSkip = false;
105  Close(false);
106  }
107  event.Skip(bSkip);
108 }
109 
110 
112 {
113  wxWindow * pWnd;
114  if( (pWnd = FindWindowById( wxID_BACKWARD, this )) != NULL )
115  {
116  pWnd->Enable(mpHtml->HistoryCanBack());
117  }
118  if( (pWnd = FindWindowById( wxID_FORWARD, this )) != NULL )
119  {
120  pWnd->Enable(mpHtml->HistoryCanForward());
121  }
122 }
123 
124 void OpenInDefaultBrowser(const wxHtmlLinkInfo& link)
125 {
126  wxURI uri(link.GetHref());
127  wxLaunchDefaultBrowser(uri.BuildURI());
128 }
129 
130 LinkingHtmlWindow::LinkingHtmlWindow(wxWindow *parent, wxWindowID id /*= -1*/,
131  const wxPoint& pos /*= wxDefaultPosition*/,
132  const wxSize& size /*= wxDefaultSize*/,
133  long style /*= wxHW_SCROLLBAR_AUTO*/) :
134  HtmlWindow(parent, id, pos, size, style)
135 {
136 }
137 
138 void LinkingHtmlWindow::OnLinkClicked(const wxHtmlLinkInfo& link)
139 {
140  wxString href = link.GetHref();
141  if( href.StartsWith(wxT("innerlink:")) )
142  {
143  wxString FileName =
144  wxFileName( FileNames::HtmlHelpDir(), href.Mid( 10 ) + wxT(".htm") ).GetFullPath();
145  if( wxFileExists( FileName ) )
146  {
147  HelpSystem::ShowHelp(this, FileName, wxEmptyString, false);
148  return;
149  }
150  else
151  {
152  SetPage( HelpText( href.Mid( 10 )));
153  wxGetTopLevelParent(this)->SetLabel( TitleText( href.Mid( 10 )));
154  }
155  }
156  else if( href.StartsWith(wxT("mailto:")) || href.StartsWith(wxT("file:")) )
157  {
158  OpenInDefaultBrowser( link );
159  return;
160  }
161  else if( !href.StartsWith( wxT("http:")) && !href.StartsWith( wxT("https:")) )
162  {
163  HtmlWindow::OnLinkClicked( link );
164  }
165  else
166  {
167  OpenInDefaultBrowser(link);
168  return;
169  }
170  BrowserDialog * pDlg = wxDynamicCast(
171  GetRelatedFrame()->FindWindow(BrowserDialog::ID), BrowserDialog );
172  if( pDlg )
173  {
174  pDlg->UpdateButtons();
175  };
176 }
void OnBackward(wxCommandEvent &event)
void OnForward(wxCommandEvent &event)
LinkingHtmlWindow(wxWindow *parent, wxWindowID id=-1, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxHW_SCROLLBAR_AUTO)
static wxString HtmlHelpDir()
Definition: FileNames.cpp:173
wxFileConfig * gPrefs
Definition: Prefs.cpp:72
void OnClose(const CommandContext &context)
wxString HelpText(const wxString &Key)
Definition: HelpText.cpp:297
HtmlWindow Class.
Definition: HtmlWindow.h:35
void OnLinkClicked(const wxHtmlLinkInfo &link) override
static void ShowHelp(wxWindow *parent, const wxString &localFileName, const wxString &remoteURL, bool bModal=false, bool alwaysDefaultBrowser=false)
Definition: HelpSystem.cpp:201
EVT_BUTTON(wxID_NO, DependencyDialog::OnNo) EVT_BUTTON(wxID_YES
END_EVENT_TABLE()
void OnKeyDown(wxKeyEvent &event)
HtmlWindow * mpHtml
void OpenInDefaultBrowser(const wxHtmlLinkInfo &link)
wxString TitleText(const wxString &Key)
Definition: HelpText.cpp:139
void OnClose(wxCommandEvent &event)