[Sugar-devel] [PATCH Help] Migrated to Gtk3 and WebKit.WebView SL #3466

Manuel Kaufmann humitos at gmail.com
Tue Apr 24 21:26:19 EDT 2012


 - All the code was migrated to Gtk3 following this guide[1]
 - Toolbar View's icon was changed from "camera" to the right one: "toolbar-view"

[1] http://wiki.sugarlabs.org/go/Features/GTK3/Porting

Signed-off-by: Manuel Kaufmann <humitos at gmail.com>
---
 browser.py          |   45 ++------------
 helpactivity.py     |  165 ++++++++++++++++++++++-----------------------------
 mybutton.py         |   53 -----------------
 progresslistener.py |   27 +++++----
 setup.py            |    2 +-
 viewtoolbar.py      |   12 ++--
 6 files changed, 97 insertions(+), 207 deletions(-)
 delete mode 100644 mybutton.py

diff --git a/browser.py b/browser.py
index 4d89715..ab24d1d 100644
--- a/browser.py
+++ b/browser.py
@@ -12,53 +12,18 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
-import os
-import time
-import logging
-from gettext import gettext as _
-
-import gobject
-import gtk
-import hulahop
-import xpcom
-from xpcom.nsError import *
-from xpcom import components
-from xpcom.components import interfaces
-from hulahop.webview import WebView
-
-from sugar.datastore import datastore
-from sugar import profile
-from sugar import env
-from sugar.activity import activity
-from sugar.graphics import style
-
+from gi.repository import WebKit
 from progresslistener import ProgressListener
 
 _ZOOM_AMOUNT = 0.1
 
-class Browser(WebView):
+
+class Browser(WebKit.WebView):
     def __init__(self):
-        WebView.__init__(self)
+        WebKit.WebView.__init__(self)
 
         self.progress = ProgressListener()
 
     def do_setup(self):
-        WebView.do_setup(self)
+        WebKit.WebView.do_setup(self)
         self.progress.setup(self)
-
-    def zoom_in(self):
-        contentViewer = self.doc_shell.queryInterface( \
-                interfaces.nsIDocShell).contentViewer
-        if contentViewer is not None:
-            markupDocumentViewer = contentViewer.queryInterface( \
-                    interfaces.nsIMarkupDocumentViewer)
-            markupDocumentViewer.fullZoom += _ZOOM_AMOUNT
-
-    def zoom_out(self):
-        contentViewer = self.doc_shell.queryInterface( \
-                interfaces.nsIDocShell).contentViewer
-        if contentViewer is not None:
-            markupDocumentViewer = contentViewer.queryInterface( \
-                    interfaces.nsIMarkupDocumentViewer)
-            markupDocumentViewer.fullZoom -= _ZOOM_AMOUNT
-
diff --git a/helpactivity.py b/helpactivity.py
index 210c423..84e1688 100755
--- a/helpactivity.py
+++ b/helpactivity.py
@@ -15,25 +15,20 @@
 import os
 from gettext import gettext as _
 
-import gtk
-import gobject
+from gi.repository import Gtk
+from gi.repository import GObject
 
-from sugar.activity import activity
-from sugar.graphics.toolbutton import ToolButton
-from sugar.graphics.toolcombobox import ToolComboBox
+from sugar3.activity import activity
+from sugar3.graphics.toolbutton import ToolButton
+from sugar3.graphics.toolbarbox import ToolbarBox, ToolbarButton
+from sugar3.activity.widgets import StopButton
 
-import hulahop
-hulahop.startup(os.path.join(activity.get_activity_root(), 'data/gecko'))
-
-#from hulahop.webview import WebView
 from browser import Browser
-import xpcom
-from xpcom.components import interfaces
 from viewtoolbar import ViewToolbar
-gobject.threads_init()
 
-HOME = os.path.join(activity.get_bundle_path(), 'help/XO_Introduction.html')
-#HOME = "http://website.com/something.html"
+HOME = 'file://' + os.path.join(activity.get_bundle_path(),
+                                'help/XO_Introduction.html')
+
 
 class HelpActivity(activity.Activity):
     def __init__(self, handle):
@@ -41,91 +36,72 @@ class HelpActivity(activity.Activity):
 
         self.props.max_participants = 1
 
+        # we do not have collaboration features
+        # make the share option insensitive
+        self.max_participants = 1
+
         self._web_view = Browser()
+        self._scrolled_window = Gtk.ScrolledWindow()
+        self._scrolled_window.add(self._web_view)
+        self._scrolled_window.show()
+
+        toolbar_box = ToolbarBox()
+
+        viewtoolbar = ViewToolbar(self)
+        viewbutton = ToolbarButton(page=viewtoolbar,
+                                   icon_name='toolbar-view')
+        toolbar_box.toolbar.insert(viewbutton, -1)
+        viewbutton.show()
+
+        separator = Gtk.SeparatorToolItem()
+        #separator.props.draw = False
+        #separator.set_expand(True)
+        toolbar_box.toolbar.insert(separator, -1)
+        separator.show()
+
+        #lets reuse the code below
+        navtoolbar = Toolbar(self._web_view)
+
+        toolitem = Gtk.ToolItem()
+        navtoolbar._home.reparent(toolitem)
+        toolbar_box.toolbar.insert(toolitem, -1)
+        navtoolbar._home.show()
+        toolitem.show()
+
+        toolitem = Gtk.ToolItem()
+        navtoolbar._back.reparent(toolitem)
+        toolbar_box.toolbar.insert(toolitem, -1)
+        navtoolbar._back.show()
+        toolitem.show()
+
+        toolitem = Gtk.ToolItem()
+        navtoolbar._forward.reparent(toolitem)
+        toolbar_box.toolbar.insert(toolitem, -1)
+        navtoolbar._forward.show()
+        toolitem.show()
+
+        separator = Gtk.SeparatorToolItem()
+        separator.props.draw = False
+        separator.set_expand(True)
+        toolbar_box.toolbar.insert(separator, -1)
+        separator.show()
+
+        stop_button = StopButton(self)
+        toolbar_box.toolbar.insert(stop_button, -1)
+        stop_button.show()
+
+        self.set_toolbar_box(toolbar_box)
+        toolbar_box.show()
+
+        self.set_canvas(self._scrolled_window)
 
-        try:
-            from sugar.graphics.toolbarbox import ToolbarBox, ToolbarButton
-            from sugar.activity.widgets import ActivityToolbarButton, StopButton, \
-                                            ShareButton
-            from mybutton import MyActivityToolbarButton
-
-            toolbar_box = ToolbarBox()
-            activity_button = MyActivityToolbarButton(self)
-            toolbar_box.toolbar.insert(activity_button, 0)
-            activity_button.show()
-
-            viewtoolbar = ViewToolbar(self)
-            viewbutton = ToolbarButton(page=viewtoolbar, \
-                                        icon_name='camera')
-            toolbar_box.toolbar.insert(viewbutton, -1)
-            viewbutton.show()
-
-            separator = gtk.SeparatorToolItem()
-            #separator.props.draw = False
-            #separator.set_expand(True)
-            toolbar_box.toolbar.insert(separator, -1)
-            separator.show()
-
-            #lets reuse the code below
-            navtoolbar = Toolbar(self._web_view)
-
-            toolitem = gtk.ToolItem()
-            navtoolbar._home.reparent(toolitem)
-            toolbar_box.toolbar.insert(toolitem, -1)
-            navtoolbar._home.show()
-            toolitem.show()
-            
-            toolitem = gtk.ToolItem()
-            navtoolbar._back.reparent(toolitem)
-            toolbar_box.toolbar.insert(toolitem, -1)
-            navtoolbar._back.show()
-            toolitem.show()
-            
-            toolitem = gtk.ToolItem()
-            navtoolbar._forward.reparent(toolitem)
-            toolbar_box.toolbar.insert(toolitem, -1)
-            navtoolbar._forward.show()
-            toolitem.show()
-
-            # we do not have collaboration features
-            # make the share option insensitive
-            self.max_participants = 1
-        
-            separator = gtk.SeparatorToolItem()
-            separator.props.draw = False
-            separator.set_expand(True)
-            toolbar_box.toolbar.insert(separator, -1)
-            separator.show()
-
-            stop_button = StopButton(self)
-            toolbar_box.toolbar.insert(stop_button, -1)
-            stop_button.show()
-
-            self.set_toolbar_box(toolbar_box)
-            toolbar_box.show()
-            
-        except ImportError:
-            toolbox = activity.ActivityToolbox(self)
-            self.set_toolbox(toolbox)
-            toolbox.show()
-
-            toolbar = Toolbar(self._web_view)
-            toolbox.add_toolbar(_('Navigation'), toolbar)
-            toolbar.show()
-            viewtoolbar = ViewToolbar(self)
-            toolbox.add_toolbar(_('View'),viewtoolbar)
-            viewtoolbar.show()
-
-            toolbox.set_current_toolbar(1)
-        
-        self.set_canvas(self._web_view)
         self._web_view.show()
-
         self._web_view.load_uri(HOME)
 
-class Toolbar(gtk.Toolbar):
+
+class Toolbar(Gtk.Toolbar):
     def __init__(self, web_view):
-        gobject.GObject.__init__(self)
+        GObject.GObject.__init__(self)
 
         self._web_view = web_view
 
@@ -169,10 +145,9 @@ class Toolbar(gtk.Toolbar):
 
     def _go_back_cb(self, button):
         self._web_view.web_navigation.goBack()
-    
+
     def _go_forward_cb(self, button):
         self._web_view.web_navigation.goForward()
 
     def _go_home_cb(self, button):
         self._web_view.load_uri(HOME)
-
diff --git a/mybutton.py b/mybutton.py
deleted file mode 100644
index b1f1f84..0000000
--- a/mybutton.py
+++ /dev/null
@@ -1,53 +0,0 @@
-# mybutton.py  A version of ActivityToolbarButton that hides the "Keep"
-# button.
-
-# Copyright (C) 2010  James D. Simmons
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License along
-# with this program; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  US
-import gtk
-import gconf
-
-from sugar.graphics.toolbarbox import ToolbarButton
-from sugar.activity.widgets import ActivityToolbar
-from sugar.graphics.xocolor import XoColor
-from sugar.graphics.icon import Icon
-from sugar.bundle.activitybundle import ActivityBundle
-
-def _create_activity_icon(metadata):
-    if metadata.get('icon-color', ''):
-        color = XoColor(metadata['icon-color'])
-    else:
-        client = gconf.client_get_default()
-        color = XoColor(client.get_string('/desktop/sugar/user/color'))
-
-    from sugar.activity.activity import get_bundle_path
-    bundle = ActivityBundle(get_bundle_path())
-    icon = Icon(file=bundle.get_icon(), xo_color=color)
-
-    return icon
-
-class MyActivityToolbarButton(ToolbarButton):
-
-    def __init__(self, activity, **kwargs):
-        toolbar = ActivityToolbar(activity, orientation_left=True)
-        toolbar.stop.hide()
-        toolbar.keep.hide()
-        toolbar.title.unset_flags(gtk.CAN_FOCUS)
-
-        ToolbarButton.__init__(self, page=toolbar, **kwargs)
-
-        icon = _create_activity_icon(activity.metadata)
-        self.set_icon_widget(icon)
-        icon.show()
diff --git a/progresslistener.py b/progresslistener.py
index cf3cb43..c098843 100644
--- a/progresslistener.py
+++ b/progresslistener.py
@@ -12,26 +12,27 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
-import gobject
 import xpcom
 from xpcom.components import interfaces
+from gi.repository import GObject
 
-class ProgressListener(gobject.GObject):
+
+class ProgressListener(GObject.GObject):
     _com_interfaces_ = interfaces.nsIWebProgressListener
 
     __gsignals__ = {
-        'location-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
+        'location-changed': (GObject.SignalFlags.RUN_FIRST, None,
                              ([object])),
-        'loading-start':    (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
+        'loading-start':    (GObject.SignalFlags.RUN_FIRST, None,
                              ([])),
-        'loading-stop':     (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
+        'loading-stop':     (GObject.SignalFlags.RUN_FIRST, None,
                              ([])),
-        'loading-progress': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
+        'loading-progress': (GObject.SignalFlags.RUN_FIRST, None,
                              ([float]))
     }
 
     def __init__(self):
-        gobject.GObject.__init__(self)
+        GObject.GObject.__init__(self)
 
         self.total_requests = 0
         self.completed_requests = 0
@@ -48,21 +49,21 @@ class ProgressListener(gobject.GObject):
                interfaces.nsIWebProgress.NOTIFY_LOCATION
 
         browser.web_progress.addProgressListener(self._wrapped_self, mask)
-    
+
     def _reset_requests_count(self):
         self.total_requests = 0
         self.completed_requests = 0
-    
+
     def onLocationChange(self, webProgress, request, location):
         self.emit('location-changed', location)
-        
+
     def onProgressChange(self, webProgress, request, curSelfProgress,
                          maxSelfProgress, curTotalProgress, maxTotalProgress):
         pass
-    
+
     def onSecurityChange(self, webProgress, request, state):
         pass
-        
+
     def onStateChange(self, webProgress, request, stateFlags, status):
         if stateFlags & interfaces.nsIWebProgressListener.STATE_IS_REQUEST:
             if stateFlags & interfaces.nsIWebProgressListener.STATE_START:
@@ -73,7 +74,7 @@ class ProgressListener(gobject.GObject):
         if stateFlags & interfaces.nsIWebProgressListener.STATE_IS_NETWORK:
             if stateFlags & interfaces.nsIWebProgressListener.STATE_START:
                 self.emit('loading-start')
-                self._reset_requests_count()                
+                self._reset_requests_count()
             elif stateFlags & interfaces.nsIWebProgressListener.STATE_STOP:
                 self.emit('loading-stop')
 
diff --git a/setup.py b/setup.py
index 8c17051..653d383 100755
--- a/setup.py
+++ b/setup.py
@@ -14,6 +14,6 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
-from sugar.activity import bundlebuilder
+from sugar3.activity import bundlebuilder
 
 bundlebuilder.start()
diff --git a/viewtoolbar.py b/viewtoolbar.py
index c5dc990..13a1ce5 100644
--- a/viewtoolbar.py
+++ b/viewtoolbar.py
@@ -16,13 +16,15 @@
 
 from gettext import gettext as _
 
-import gtk
+from gi.repository import Gtk
+from gi.repository import GObject
 
-from sugar.graphics.toolbutton import ToolButton
+from sugar3.graphics.toolbutton import ToolButton
 
-class ViewToolbar(gtk.Toolbar):
+
+class ViewToolbar(Gtk.Toolbar):
     def __init__(self, activity):
-        gtk.Toolbar.__init__(self)
+        GObject.GObject.__init__(self)
 
         self._activity = activity
 
@@ -40,7 +42,7 @@ class ViewToolbar(gtk.Toolbar):
         self.insert(self.zoomin, -1)
         self.zoomin.show()
 
-        self.separator = gtk.SeparatorToolItem()
+        self.separator = Gtk.SeparatorToolItem()
         self.separator.set_draw(True)
         self.insert(self.separator, -1)
         self.separator.show()
-- 
1.7.10



More information about the Sugar-devel mailing list