From f1f5170c062c4a96dd986116631a6d8e1fb1d87f Mon Sep 17 00:00:00 2001
From: Zeno Albisser <zeno.albisser@digia.com>
Date: Tue, 18 Jun 2013 17:15:00 +0200
Subject: [PATCH] Disable the back/forward buttons when appropriate.

---
 examples/qtquick/quickwindow.qml  |  2 ++
 examples/widgets/widgetwindow.cpp |  6 ++++--
 examples/widgets/widgetwindow.h   |  2 ++
 lib/qwebcontentsview.cpp          | 10 ++++++++++
 lib/qwebcontentsview.h            |  2 ++
 5 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/examples/qtquick/quickwindow.qml b/examples/qtquick/quickwindow.qml
index 7962be141..afaff7c8d 100644
--- a/examples/qtquick/quickwindow.qml
+++ b/examples/qtquick/quickwindow.qml
@@ -20,12 +20,14 @@ ApplicationWindow {
                 iconName: "go-previous"
                 iconSource: ":/icons/go-previous.png"
                 onClicked: webContentsView.goBack()
+                enabled: webContentsView.canGoBack
             }
             ToolButton {
                 id: forwardButton
                 iconName: "go-next"
                 iconSource: ":/icons/go-next.png"
                 onClicked: webContentsView.goForward()
+                enabled: webContentsView.canGoForward
             }
             ToolButton {
                 id: reloadButton
diff --git a/examples/widgets/widgetwindow.cpp b/examples/widgets/widgetwindow.cpp
index 589ef13c3..a28a6f130 100644
--- a/examples/widgets/widgetwindow.cpp
+++ b/examples/widgets/widgetwindow.cpp
@@ -63,11 +63,11 @@ WidgetWindow::WidgetWindow()
     QHBoxLayout* addressBar = new QHBoxLayout;
     addressBar->setSpacing(margin); // Bigger buttons, less space between them
 
-    QToolButton* backButton = new QToolButton;
+    backButton = new QToolButton;
     backButton->setIcon(QIcon::fromTheme("go-previous"));
     addressBar->addWidget(backButton);
 
-    QToolButton* forwardButton = new QToolButton;
+    forwardButton = new QToolButton;
     forwardButton->setIcon(QIcon::fromTheme("go-next"));
     addressBar->addWidget(forwardButton);
 
@@ -117,5 +117,7 @@ void WidgetWindow::loadStarted()
 void WidgetWindow::loadFinished(bool success)
 {
     Q_UNUSED(success);
+    forwardButton->setEnabled(m_webView->canGoForward());
+    backButton->setEnabled(m_webView->canGoBack());
     reloadButton->setIcon(QIcon::fromTheme("view-refresh"));
 }
diff --git a/examples/widgets/widgetwindow.h b/examples/widgets/widgetwindow.h
index 5273d4255..863e8a73f 100644
--- a/examples/widgets/widgetwindow.h
+++ b/examples/widgets/widgetwindow.h
@@ -62,6 +62,8 @@ private Q_SLOTS:
 private:
     QScopedPointer<QWebContentsView> m_webView;
     QLineEdit* addressLineEdit;
+    QToolButton* forwardButton;
+    QToolButton* backButton;
     QToolButton* reloadButton;
 };
 
diff --git a/lib/qwebcontentsview.cpp b/lib/qwebcontentsview.cpp
index 323bb3b60..07aa034d5 100644
--- a/lib/qwebcontentsview.cpp
+++ b/lib/qwebcontentsview.cpp
@@ -95,6 +95,16 @@ void QWebContentsView::load(const QUrl& url)
     d->webContentsDelegate->web_contents()->GetView()->Focus();
 }
 
+bool QWebContentsView::canGoBack() const
+{
+    return d->webContentsDelegate->web_contents()->GetController().CanGoBack();
+}
+
+bool QWebContentsView::canGoForward() const
+{
+    return d->webContentsDelegate->web_contents()->GetController().CanGoForward();
+}
+
 void QWebContentsView::back()
 {
     d->webContentsDelegate->web_contents()->GetController().GoToOffset(-1);
diff --git a/lib/qwebcontentsview.h b/lib/qwebcontentsview.h
index 6eef056c7..83eefd59e 100644
--- a/lib/qwebcontentsview.h
+++ b/lib/qwebcontentsview.h
@@ -54,6 +54,8 @@ public:
     ~QWebContentsView();
 
     void load(const QUrl& url);
+    bool canGoBack() const;
+    bool canGoForward() const;
 
 public Q_SLOTS:
     void back();
-- 
GitLab