[Libreoffice-commits] core.git: Branch 'feature/gtkbmptiledviewer2' - 2 commits - include/LibreOfficeKit libreofficekit/qa libreofficekit/source

Andrzej Hunt andrzej.hunt at collabora.com
Mon Jun 23 12:29:35 PDT 2014


Rebased ref, commits from common ancestor:
commit a0ae063311509168c39d9f6a773e63a80ce59cde
Author: Andrzej Hunt <andrzej.hunt at collabora.com>
Date:   Mon Jun 23 15:13:40 2014 +0100

    Add zoom controls to gtktiledviewer.
    
    Change-Id: I33ae83a97be254a3d3716bd9ae05f089845fd536

diff --git a/libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx b/libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx
index cfa836c..bd06b04 100644
--- a/libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx
+++ b/libreofficekit/qa/gtktiledviewer/gtktiledviewer.cxx
@@ -21,6 +21,52 @@ static int help()
     return 1;
 }
 
+static GtkWidget* pDocView;
+
+const float fZooms[] = { 0.25, 0.5, 0.75, 1.0, 1.5, 2.0, 3.0, 5.0 };
+
+void changeZoom( GtkWidget* pButton, gpointer /* pItem */ )
+{
+    const char *sName = gtk_tool_button_get_stock_id( GTK_TOOL_BUTTON(pButton) );
+
+    float fZoom = 0;
+    const float fCurrentZoom = lok_docview_get_zoom( LOK_DOCVIEW(pDocView) );
+    if ( strcmp(sName, "gtk-zoom-in") == 0)
+    {
+        for ( unsigned int i = 0; i < sizeof( fZooms ) / sizeof( fZooms[0] ); i++ )
+        {
+            if ( fCurrentZoom < fZooms[i] )
+            {
+                fZoom = fZooms[i];
+                break;
+            }
+        }
+    }
+    else if ( strcmp(sName, "gtk-zoom-100") == 0)
+    {
+        fZoom = 1;
+    }
+    else if ( strcmp(sName, "gtk-zoom-fit") == 0)
+    {
+        // TODO -- will need access to lokdocview internals?
+    }
+    else if ( strcmp(sName, "gtk-zoom-out") == 0)
+    {
+        for ( unsigned int i = 0; i < sizeof( fZooms ) / sizeof( fZooms[0] ); i++ )
+        {
+            if ( fCurrentZoom > fZooms[i] )
+            {
+                fZoom = fZooms[i];
+            }
+        }
+    }
+
+    if ( fZoom != 0 )
+    {
+        lok_docview_set_zoom( LOK_DOCVIEW(pDocView), fZoom );
+    }
+}
+
 int main( int argc, char* argv[] )
 {
     if( argc < 2 ||
@@ -54,14 +100,38 @@ int main( int argc, char* argv[] )
     gtk_window_set_default_size(GTK_WINDOW(pWindow), 800, 600);
     g_signal_connect( pWindow, "destroy", G_CALLBACK(gtk_main_quit), NULL );
 
+    GtkWidget* pVBox = gtk_vbox_new( FALSE, 0 );
+    gtk_container_add( GTK_CONTAINER(pWindow), pVBox );
 
-    GtkWidget* pDocView = lok_docview_new( pOffice );
-    gtk_container_add( GTK_CONTAINER(pWindow), pDocView );
+    // Toolbar
+    GtkWidget* pToolbar = gtk_toolbar_new();
+    gtk_toolbar_set_style( GTK_TOOLBAR(pToolbar), GTK_TOOLBAR_ICONS );
 
-    lok_docview_open_document( LOK_DOCVIEW(pDocView), argv[2] );
+    GtkToolItem* pZoomIn = gtk_tool_button_new_from_stock( GTK_STOCK_ZOOM_IN );
+    gtk_toolbar_insert( GTK_TOOLBAR(pToolbar), pZoomIn, 0);
+    g_signal_connect( G_OBJECT(pZoomIn), "clicked", G_CALLBACK(changeZoom), NULL );
+
+    GtkToolItem* pZoom1 = gtk_tool_button_new_from_stock( GTK_STOCK_ZOOM_100 );
+    gtk_toolbar_insert( GTK_TOOLBAR(pToolbar), pZoom1, -1);
+    g_signal_connect( G_OBJECT(pZoom1), "clicked", G_CALLBACK(changeZoom), NULL );
 
-    gtk_widget_show( pDocView );
-    gtk_widget_show( pWindow );
+    GtkToolItem* pZoomFit = gtk_tool_button_new_from_stock( GTK_STOCK_ZOOM_FIT );
+    gtk_toolbar_insert( GTK_TOOLBAR(pToolbar), pZoomFit, -1);
+    g_signal_connect( G_OBJECT(pZoomFit), "clicked", G_CALLBACK(changeZoom), NULL );
+
+    GtkToolItem* pZoomOut = gtk_tool_button_new_from_stock( GTK_STOCK_ZOOM_OUT );
+    gtk_toolbar_insert( GTK_TOOLBAR(pToolbar), pZoomOut, -1);
+    g_signal_connect( G_OBJECT(pZoomOut), "clicked", G_CALLBACK(changeZoom), NULL );
+
+    gtk_box_pack_start( GTK_BOX(pVBox), pToolbar, FALSE, FALSE, 0 ); // Adds to top.
+
+    // Docview
+    pDocView = lok_docview_new( pOffice );
+    gtk_container_add( GTK_CONTAINER(pVBox), pDocView );
+
+    gtk_widget_show_all( pWindow );
+
+    lok_docview_open_document( LOK_DOCVIEW(pDocView), argv[2] );
 
     gtk_main();
 
commit e73e1263d5cf7553c2d53dacbf41a7559b9c0d5b
Author: Andrzej Hunt <andrzej.hunt at collabora.com>
Date:   Mon Jun 23 15:13:25 2014 +0100

    LOK Docview: add set_zoom
    
    Change-Id: I902f3a134b4a7dcc721eff3f67376014a4276885

diff --git a/include/LibreOfficeKit/LibreOfficeKitGtk.h b/include/LibreOfficeKit/LibreOfficeKitGtk.h
index 2435fc2..f160925 100644
--- a/include/LibreOfficeKit/LibreOfficeKitGtk.h
+++ b/include/LibreOfficeKit/LibreOfficeKitGtk.h
@@ -37,6 +37,8 @@ struct _LOKDocView
     GtkWidget* pCanvas;
     GdkPixbuf* pPixBuf;
 
+    float fZoom;
+
     LibreOfficeKit* pOffice;
     LibreOfficeKitDocument* pDocument;
 };
@@ -52,6 +54,9 @@ guint           lok_docview_get_type        (void);
 GtkWidget*      lok_docview_new             ( LibreOfficeKit* pOffice );
 gboolean        lok_docview_open_document   (LOKDocView* pDocView,
                                              char* pPath);
+void            lok_docview_set_zoom        (LOKDocView* pDocView,
+                                             float fZoom);
+float           lok_docview_get_zoom        (LOKDocView* pDocView);
 
 #ifdef __cplusplus
 }
diff --git a/libreofficekit/source/gtk/lokdocview.c b/libreofficekit/source/gtk/lokdocview.c
index 4a16db0..99f2b15 100644
--- a/libreofficekit/source/gtk/lokdocview.c
+++ b/libreofficekit/source/gtk/lokdocview.c
@@ -68,6 +68,8 @@ static void lok_docview_init( LOKDocView* pDocView )
     // TODO: figure out a clever view of getting paths set up.
     pDocView->pOffice = 0;
     pDocView->pDocument = 0;
+
+    pDocView->fZoom = 1;
 }
 
 SAL_DLLPUBLIC_EXPORT GtkWidget* lok_docview_new( LibreOfficeKit* pOffice )
@@ -77,16 +79,10 @@ SAL_DLLPUBLIC_EXPORT GtkWidget* lok_docview_new( LibreOfficeKit* pOffice )
     return GTK_WIDGET( pDocView );
 }
 
-SAL_DLLPUBLIC_EXPORT gboolean lok_docview_open_document( LOKDocView* pDocView, char* pPath )
+void renderDocument( LOKDocView* pDocView )
 {
-    if ( pDocView->pDocument )
-    {
-        pDocView->pDocument->pClass->destroy( pDocView->pDocument );
-        pDocView->pDocument = 0;
-    }
+    g_assert( pDocView->pDocument );
 
-    pDocView->pDocument = pDocView->pOffice->pClass->documentLoad( pDocView->pOffice,
-                                                           pPath );
     if ( pDocView->pPixBuf )
     {
         g_object_unref( G_OBJECT( pDocView->pPixBuf ) );
@@ -96,15 +92,17 @@ SAL_DLLPUBLIC_EXPORT gboolean lok_docview_open_document( LOKDocView* pDocView, c
     pDocView->pDocument->pClass->getDocumentSize( pDocView->pDocument, &nWidth, &nHeight );
 
     // Draw the whole document at once (for now)
-    int nRenderWidth = nWidth / 10;
-    int nRenderHeight = nHeight / 10;
+
+    // TODO: we really should scale by screen DPI here -- 10 seems to be a vaguely
+    // correct factor for my screen at least.
+    int nRenderWidth = nWidth * pDocView->fZoom / 10;
+    int nRenderHeight = nHeight * pDocView->fZoom / 10;
 
     pDocView->pPixBuf = gdk_pixbuf_new( GDK_COLORSPACE_RGB,
                                         TRUE, 8,
                                         nRenderWidth, nRenderHeight);
 
 
-    // TODO: move the rendering into it's own function etc.
     unsigned char* pBuffer = gdk_pixbuf_get_pixels( pDocView->pPixBuf );
     int nRowStride;
     pDocView->pDocument->pClass->paintTile( pDocView->pDocument,
@@ -119,8 +117,34 @@ SAL_DLLPUBLIC_EXPORT gboolean lok_docview_open_document( LOKDocView* pDocView, c
     (void) nRowStride;
 
     gtk_image_set_from_pixbuf( GTK_IMAGE( pDocView->pCanvas ), pDocView->pPixBuf );
+}
+
+SAL_DLLPUBLIC_EXPORT gboolean lok_docview_open_document( LOKDocView* pDocView, char* pPath )
+{
+    if ( pDocView->pDocument )
+    {
+        pDocView->pDocument->pClass->destroy( pDocView->pDocument );
+        pDocView->pDocument = 0;
+    }
+
+    pDocView->pDocument = pDocView->pOffice->pClass->documentLoad( pDocView->pOffice,
+                                                           pPath );
+
+    renderDocument( pDocView );
 
     return FALSE;
 }
 
+SAL_DLLPUBLIC_EXPORT void lok_docview_set_zoom ( LOKDocView* pDocView, float fZoom )
+{
+    pDocView->fZoom = fZoom;
+    renderDocument( pDocView );
+    // TODO: maybe remember and reset positiong?
+}
+
+SAL_DLLPUBLIC_EXPORT float lok_docview_get_zoom ( LOKDocView* pDocView )
+{
+    return pDocView->fZoom;
+}
+
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */


More information about the Libreoffice-commits mailing list