Add wxWebView::SetZoomFactor(float) and GetZoomFactor()

The new method allows to set the zoom level more precisely than the
existing SetZoom(wxWebViewZoom).

Also improve the webview sample by using radio menu items instead of
check items and manually resetting them.

Closes https://github.com/wxWidgets/wxWidgets/pull/1894

Closes #18769.
This commit is contained in:
Hertatijanto Hartono
2020-06-13 22:50:55 +07:00
committed by Vadim Zeitlin
parent dc9040cc89
commit 895424ecc0
12 changed files with 227 additions and 59 deletions

View File

@@ -35,6 +35,7 @@
#endif
#include "wx/webviewarchivehandler.h"
#include "wx/webviewfshandler.h"
#include "wx/numdlg.h"
#include "wx/infobar.h"
#include "wx/filesys.h"
#include "wx/fs_arc.h"
@@ -132,6 +133,7 @@ public:
void OnRedo(wxCommandEvent& evt);
void OnMode(wxCommandEvent& evt);
void OnZoomLayout(wxCommandEvent& evt);
void OnZoomCustom(wxCommandEvent& evt);
void OnHistory(wxCommandEvent& evt);
void OnScrollLineUp(wxCommandEvent&) { m_browser->LineUp(); }
void OnScrollLineDown(wxCommandEvent&) { m_browser->LineDown(); }
@@ -194,6 +196,7 @@ private:
wxMenuItem* m_tools_medium;
wxMenuItem* m_tools_large;
wxMenuItem* m_tools_largest;
wxMenuItem* m_tools_custom;
wxMenuItem* m_tools_handle_navigation;
wxMenuItem* m_tools_handle_new_window;
wxMenuItem* m_tools_enable_history;
@@ -237,6 +240,7 @@ private:
wxMenuHistoryMap m_histMenuItems;
wxString m_findText;
int m_findFlags, m_findCount;
long m_zoomFactor;
// Last executed JavaScript snippet, for convenience.
wxString m_javascript;
@@ -402,12 +406,13 @@ WebFrame::WebFrame(const wxString& url) :
wxMenuItem* viewSource = m_tools_menu->Append(wxID_ANY , _("View Source"));
wxMenuItem* viewText = m_tools_menu->Append(wxID_ANY, _("View Text"));
m_tools_menu->AppendSeparator();
m_tools_layout = m_tools_menu->AppendCheckItem(wxID_ANY, _("Use Layout Zoom"));
m_tools_tiny = m_tools_menu->AppendCheckItem(wxID_ANY, _("Tiny"));
m_tools_small = m_tools_menu->AppendCheckItem(wxID_ANY, _("Small"));
m_tools_medium = m_tools_menu->AppendCheckItem(wxID_ANY, _("Medium"));
m_tools_large = m_tools_menu->AppendCheckItem(wxID_ANY, _("Large"));
m_tools_largest = m_tools_menu->AppendCheckItem(wxID_ANY, _("Largest"));
m_tools_layout = m_tools_menu->AppendRadioItem(wxID_ANY, _("Use Layout Zoom"));
m_tools_tiny = m_tools_menu->AppendRadioItem(wxID_ANY, _("Tiny"));
m_tools_small = m_tools_menu->AppendRadioItem(wxID_ANY, _("Small"));
m_tools_medium = m_tools_menu->AppendRadioItem(wxID_ANY, _("Medium"));
m_tools_large = m_tools_menu->AppendRadioItem(wxID_ANY, _("Large"));
m_tools_largest = m_tools_menu->AppendRadioItem(wxID_ANY, _("Largest"));
m_tools_custom = m_tools_menu->AppendRadioItem(wxID_ANY, _("Custom Size"));
m_tools_menu->AppendSeparator();
m_tools_handle_navigation = m_tools_menu->AppendCheckItem(wxID_ANY, _("Handle Navigation"));
m_tools_handle_new_window = m_tools_menu->AppendCheckItem(wxID_ANY, _("Handle New Windows"));
@@ -486,10 +491,14 @@ WebFrame::WebFrame(const wxString& url) :
m_tools_handle_navigation->Check();
m_tools_handle_new_window->Check();
m_tools_enable_history->Check();
//Zoom
m_zoomFactor = 100;
m_tools_medium->Check();
if(!m_browser->CanSetZoomType(wxWEBVIEW_ZOOM_TYPE_LAYOUT))
m_tools_layout->Enable(false);
// Connect the toolbar events
Bind(wxEVT_TOOL, &WebFrame::OnBack, this, m_toolbar_back->GetId());
Bind(wxEVT_TOOL, &WebFrame::OnForward, this, m_toolbar_forward->GetId());
@@ -527,6 +536,7 @@ WebFrame::WebFrame(const wxString& url) :
Bind(wxEVT_MENU, &WebFrame::OnSetZoom, this, m_tools_medium->GetId());
Bind(wxEVT_MENU, &WebFrame::OnSetZoom, this, m_tools_large->GetId());
Bind(wxEVT_MENU, &WebFrame::OnSetZoom, this, m_tools_largest->GetId());
Bind(wxEVT_MENU, &WebFrame::OnSetZoom, this, m_tools_custom->GetId());
Bind(wxEVT_MENU, &WebFrame::OnClearHistory, this, clearhist->GetId());
Bind(wxEVT_MENU, &WebFrame::OnEnableHistory, this, m_tools_enable_history->GetId());
Bind(wxEVT_MENU, &WebFrame::OnCut, this, m_edit_cut->GetId());
@@ -928,32 +938,6 @@ void WebFrame::OnToolsClicked(wxCommandEvent& WXUNUSED(evt))
if(m_browser->GetCurrentURL() == "")
return;
m_tools_tiny->Check(false);
m_tools_small->Check(false);
m_tools_medium->Check(false);
m_tools_large->Check(false);
m_tools_largest->Check(false);
wxWebViewZoom zoom = m_browser->GetZoom();
switch (zoom)
{
case wxWEBVIEW_ZOOM_TINY:
m_tools_tiny->Check();
break;
case wxWEBVIEW_ZOOM_SMALL:
m_tools_small->Check();
break;
case wxWEBVIEW_ZOOM_MEDIUM:
m_tools_medium->Check();
break;
case wxWEBVIEW_ZOOM_LARGE:
m_tools_large->Check();
break;
case wxWEBVIEW_ZOOM_LARGEST:
m_tools_largest->Check();
break;
}
m_edit_cut->Enable(m_browser->CanCut());
m_edit_copy->Enable(m_browser->CanCopy());
m_edit_paste->Enable(m_browser->CanPaste());
@@ -1033,6 +1017,10 @@ void WebFrame::OnSetZoom(wxCommandEvent& evt)
{
m_browser->SetZoom(wxWEBVIEW_ZOOM_LARGEST);
}
else if (evt.GetId() == m_tools_custom->GetId())
{
OnZoomCustom(evt);
}
else
{
wxFAIL;
@@ -1047,6 +1035,24 @@ void WebFrame::OnZoomLayout(wxCommandEvent& WXUNUSED(evt))
m_browser->SetZoomType(wxWEBVIEW_ZOOM_TYPE_TEXT);
}
void WebFrame::OnZoomCustom(wxCommandEvent& WXUNUSED(evt))
{
wxNumberEntryDialog dialog
(
this,
"Enter zoom factor as a percentage (10-10000)%",
"Zoom Factor:",
"Change Zoom Factor",
m_zoomFactor,
10, 10000
);
if( dialog.ShowModal() != wxID_OK )
return;
m_zoomFactor = dialog.GetValue();
m_browser->SetZoomFactor((float)m_zoomFactor/100);
}
void WebFrame::OnHistory(wxCommandEvent& evt)
{
m_browser->LoadHistoryItem(m_histMenuItems[evt.GetId()]);