LCOV - code coverage report
Current view: top level - libreofficekit/qa/gtktiledviewer - gtktiledviewer.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 0 144 0.0 %
Date: 2014-11-03 Functions: 0 8 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  */
       9             : 
      10             : #include <assert.h>
      11             : #include <stdio.h>
      12             : #include <string.h>
      13             : 
      14             : #include <gdk/gdk.h>
      15             : #include <gtk/gtk.h>
      16             : 
      17             : #include <LibreOfficeKit/LibreOfficeKitGtk.h>
      18             : #include <LibreOfficeKit/LibreOfficeKitInit.h>
      19             : #include "../lokdocview_quad/lokdocview_quad.h"
      20             : 
      21           0 : static int help()
      22             : {
      23           0 :     fprintf( stderr, "Usage: gtktiledviewer <absolute-path-to-libreoffice-install> [path to document]\n" );
      24           0 :     return 1;
      25             : }
      26             : 
      27             : static GtkWidget* pDocView;
      28             : static GtkWidget* pDocViewQuad;
      29             : static GtkWidget* pVBox;
      30             : // GtkComboBox requires gtk 2.24 or later
      31             : #if ( GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION >= 24 ) || GTK_MAJOR_VERSION > 2
      32             : static GtkComboBoxText* pPartSelector;
      33             : #endif
      34             : 
      35             : static LibreOfficeKit* pOffice;
      36             : static char* pFileName;
      37             : 
      38             : const float fZooms[] = { 0.25, 0.5, 0.75, 1.0, 1.5, 2.0, 3.0, 5.0 };
      39             : 
      40           0 : void changeZoom( GtkWidget* pButton, gpointer /* pItem */ )
      41             : {
      42           0 :     const char *sName = gtk_tool_button_get_stock_id( GTK_TOOL_BUTTON(pButton) );
      43             : 
      44           0 :     float fZoom = 0;
      45           0 :     float fCurrentZoom = 0;
      46             : 
      47           0 :     if ( pDocView )
      48             :     {
      49           0 :         fCurrentZoom = lok_docview_get_zoom( LOK_DOCVIEW(pDocView) );
      50             :     }
      51           0 :     else if ( pDocViewQuad )
      52             :     {
      53           0 :         fCurrentZoom = lok_docview_quad_get_zoom( LOK_DOCVIEW_QUAD(pDocViewQuad) );
      54             :     }
      55             : 
      56           0 :     if ( strcmp(sName, "gtk-zoom-in") == 0)
      57             :     {
      58           0 :         for ( unsigned int i = 0; i < sizeof( fZooms ) / sizeof( fZooms[0] ); i++ )
      59             :         {
      60           0 :             if ( fCurrentZoom < fZooms[i] )
      61             :             {
      62           0 :                 fZoom = fZooms[i];
      63           0 :                 break;
      64             :             }
      65             :         }
      66             :     }
      67           0 :     else if ( strcmp(sName, "gtk-zoom-100") == 0)
      68             :     {
      69           0 :         fZoom = 1;
      70             :     }
      71           0 :     else if ( strcmp(sName, "gtk-zoom-fit") == 0)
      72             :     {
      73             :         // TODO -- will need access to lokdocview internals?
      74             :     }
      75           0 :     else if ( strcmp(sName, "gtk-zoom-out") == 0)
      76             :     {
      77           0 :         for ( unsigned int i = 0; i < sizeof( fZooms ) / sizeof( fZooms[0] ); i++ )
      78             :         {
      79           0 :             if ( fCurrentZoom > fZooms[i] )
      80             :             {
      81           0 :                 fZoom = fZooms[i];
      82             :             }
      83             :         }
      84             :     }
      85             : 
      86           0 :     if ( fZoom != 0 )
      87             :     {
      88           0 :         if ( pDocView )
      89             :         {
      90           0 :             lok_docview_set_zoom( LOK_DOCVIEW(pDocView), fZoom );
      91             :         }
      92           0 :         else if ( pDocViewQuad )
      93             :         {
      94           0 :             lok_docview_quad_set_zoom( LOK_DOCVIEW_QUAD(pDocViewQuad), fZoom );
      95             :         }
      96             :     }
      97           0 : }
      98             : 
      99           0 : void changeQuadView( GtkWidget* /*pButton*/, gpointer /* pItem */ )
     100             : {
     101           0 :     if ( pDocView )
     102             :     {
     103           0 :         const float fCurrentZoom = lok_docview_get_zoom( LOK_DOCVIEW(pDocView) );
     104           0 :         gtk_widget_destroy( pDocView );
     105           0 :         pDocView = 0;
     106           0 :         pDocViewQuad = lok_docview_quad_new( pOffice );
     107           0 :         gtk_container_add( GTK_CONTAINER(pVBox), pDocViewQuad );
     108           0 :         gtk_widget_show( pDocViewQuad );
     109             : 
     110           0 :         lok_docview_quad_set_zoom( LOK_DOCVIEW_QUAD(pDocViewQuad), fCurrentZoom );
     111           0 :         lok_docview_quad_open_document( LOK_DOCVIEW_QUAD(pDocViewQuad), pFileName );
     112             :     }
     113           0 :     else if ( pDocViewQuad )
     114             :     {
     115           0 :         const float fCurrentZoom = lok_docview_quad_get_zoom( LOK_DOCVIEW_QUAD(pDocViewQuad) );
     116           0 :         gtk_widget_destroy( pDocViewQuad );
     117           0 :         pDocViewQuad = 0;
     118           0 :         pDocView = lok_docview_new( pOffice );
     119           0 :         gtk_container_add( GTK_CONTAINER(pVBox), pDocView );
     120           0 :         gtk_widget_show( pDocView );
     121             : 
     122           0 :         lok_docview_set_zoom( LOK_DOCVIEW(pDocView), fCurrentZoom );
     123           0 :         lok_docview_open_document( LOK_DOCVIEW(pDocView), pFileName );
     124             :     }
     125           0 : }
     126             : 
     127             : // GtkComboBox requires gtk 2.24 or later
     128             : #if ( GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION >= 24 ) || GTK_MAJOR_VERSION > 2
     129           0 : void populatePartSelector()
     130             : {
     131           0 :     gtk_list_store_clear( GTK_LIST_STORE(
     132             :                               gtk_combo_box_get_model(
     133           0 :                                   GTK_COMBO_BOX(pPartSelector) )) );
     134             : 
     135           0 :     if ( !pDocView )
     136             :     {
     137           0 :         return;
     138             :     }
     139             : 
     140           0 :     const int nMaxLength = 50;
     141             :     char sText[nMaxLength];
     142             : 
     143           0 :     int nParts = lok_docview_get_parts( LOK_DOCVIEW(pDocView) );
     144           0 :     for ( int i = 0; i < nParts; i++ )
     145             :     {
     146           0 :         char* pName = lok_docview_get_part_name( LOK_DOCVIEW(pDocView), i );
     147             :         assert( pName );
     148           0 :         snprintf( sText, nMaxLength, "%i (%s)", i+1, pName );
     149           0 :         free( pName );
     150             : 
     151           0 :         gtk_combo_box_text_append_text( pPartSelector, sText );
     152             :     }
     153           0 :     gtk_combo_box_set_active( GTK_COMBO_BOX(pPartSelector),
     154           0 :                               lok_docview_get_part( LOK_DOCVIEW(pDocView) ) );
     155             : }
     156             : 
     157           0 : void changePart( GtkWidget* pSelector, gpointer /* pItem */ )
     158             : {
     159           0 :     int nPart = gtk_combo_box_get_active( GTK_COMBO_BOX(pSelector) );
     160             : 
     161             :     // We don't really care about the quad view for now -- it's only purpose
     162             :     // is to check that the edges of tiles aren't messed up, and no real
     163             :     // reason to maintain it to be able to show other document parts etc.
     164           0 :     if ( pDocView )
     165             :     {
     166           0 :         lok_docview_set_part( LOK_DOCVIEW(pDocView), nPart );
     167             :     }
     168           0 : }
     169             : 
     170           0 : void populatePartModeSelector( GtkComboBoxText* pSelector )
     171             : {
     172           0 :     gtk_combo_box_text_append_text( pSelector, "Default" );
     173           0 :     gtk_combo_box_text_append_text( pSelector, "Slide" );
     174           0 :     gtk_combo_box_text_append_text( pSelector, "Notes" );
     175           0 :     gtk_combo_box_text_append_text( pSelector, "Combined (SlideNotes)" );
     176           0 :     gtk_combo_box_text_append_text( pSelector, "Embedded Objects" );
     177           0 :     gtk_combo_box_set_active( GTK_COMBO_BOX(pSelector), 0 );
     178           0 : }
     179             : 
     180           0 : void changePartMode( GtkWidget* pSelector, gpointer /* pItem */ )
     181             : {
     182             :     // Just convert directly back to the LibreOfficeKitPartMode enum.
     183             :     // I.e. the ordering above should match the enum member ordering.
     184             :     LibreOfficeKitPartMode ePartMode =
     185           0 :         LibreOfficeKitPartMode( gtk_combo_box_get_active( GTK_COMBO_BOX(pSelector) ) );
     186             : 
     187           0 :     if ( pDocView )
     188             :     {
     189           0 :         lok_docview_set_partmode( LOK_DOCVIEW(pDocView), ePartMode );
     190             :     }
     191             : 
     192             :     // The number of items could change e.g. if we change from slide
     193             :     // to embeddede obj mode -- hence we should update the part list.
     194           0 :     populatePartSelector();
     195           0 : }
     196             : #endif
     197             : 
     198           0 : int main( int argc, char* argv[] )
     199             : {
     200           0 :     if( argc < 2 ||
     201           0 :         ( argc > 1 && ( !strcmp( argv[1], "--help" ) || !strcmp( argv[1], "-h" ) ) ) )
     202           0 :         return help();
     203             : 
     204           0 :     if ( argv[1][0] != '/' )
     205             :     {
     206           0 :         fprintf(stderr, "Absolute path required to libreoffice install\n");
     207           0 :         return 1;
     208             :     }
     209             : 
     210           0 :     pOffice = lok_init( argv[1] );
     211             : 
     212           0 :     gtk_init( &argc, &argv );
     213             : 
     214           0 :     GtkWidget *pWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
     215           0 :     gtk_window_set_title( GTK_WINDOW(pWindow), "LibreOffice GTK Tiled Viewer" );
     216           0 :     gtk_window_set_default_size(GTK_WINDOW(pWindow), 800, 600);
     217           0 :     g_signal_connect( pWindow, "destroy", G_CALLBACK(gtk_main_quit), NULL );
     218             : 
     219           0 :     pVBox = gtk_vbox_new( FALSE, 0 );
     220           0 :     gtk_container_add( GTK_CONTAINER(pWindow), pVBox );
     221             : 
     222             :     // Toolbar
     223           0 :     GtkWidget* pToolbar = gtk_toolbar_new();
     224           0 :     gtk_toolbar_set_style( GTK_TOOLBAR(pToolbar), GTK_TOOLBAR_ICONS );
     225             : 
     226           0 :     GtkToolItem* pZoomIn = gtk_tool_button_new_from_stock( GTK_STOCK_ZOOM_IN );
     227           0 :     gtk_toolbar_insert( GTK_TOOLBAR(pToolbar), pZoomIn, 0);
     228           0 :     g_signal_connect( G_OBJECT(pZoomIn), "clicked", G_CALLBACK(changeZoom), NULL );
     229             : 
     230           0 :     GtkToolItem* pZoom1 = gtk_tool_button_new_from_stock( GTK_STOCK_ZOOM_100 );
     231           0 :     gtk_toolbar_insert( GTK_TOOLBAR(pToolbar), pZoom1, -1);
     232           0 :     g_signal_connect( G_OBJECT(pZoom1), "clicked", G_CALLBACK(changeZoom), NULL );
     233             : 
     234           0 :     GtkToolItem* pZoomFit = gtk_tool_button_new_from_stock( GTK_STOCK_ZOOM_FIT );
     235           0 :     gtk_toolbar_insert( GTK_TOOLBAR(pToolbar), pZoomFit, -1);
     236           0 :     g_signal_connect( G_OBJECT(pZoomFit), "clicked", G_CALLBACK(changeZoom), NULL );
     237             : 
     238           0 :     GtkToolItem* pZoomOut = gtk_tool_button_new_from_stock( GTK_STOCK_ZOOM_OUT );
     239           0 :     gtk_toolbar_insert( GTK_TOOLBAR(pToolbar), pZoomOut, -1);
     240           0 :     g_signal_connect( G_OBJECT(pZoomOut), "clicked", G_CALLBACK(changeZoom), NULL );
     241             : 
     242           0 :     GtkToolItem* pSeparator1 = gtk_separator_tool_item_new();
     243           0 :     gtk_toolbar_insert( GTK_TOOLBAR(pToolbar), pSeparator1, -1);
     244             : 
     245             : // GtkComboBox requires gtk 2.24 or later
     246             : #if ( GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION >= 24 ) || GTK_MAJOR_VERSION > 2
     247           0 :     GtkToolItem* pPartSelectorToolItem = gtk_tool_item_new();
     248           0 :     GtkWidget* pComboBox = gtk_combo_box_text_new();
     249           0 :     gtk_container_add( GTK_CONTAINER(pPartSelectorToolItem), pComboBox );
     250           0 :     gtk_toolbar_insert( GTK_TOOLBAR(pToolbar), pPartSelectorToolItem, -1 );
     251           0 :     g_signal_connect( G_OBJECT(pComboBox), "changed", G_CALLBACK(changePart), NULL );
     252             : 
     253           0 :     pPartSelector = GTK_COMBO_BOX_TEXT(pComboBox);
     254             : 
     255           0 :     GtkToolItem* pSeparator2 = gtk_separator_tool_item_new();
     256           0 :     gtk_toolbar_insert( GTK_TOOLBAR(pToolbar), pSeparator2, -1);
     257             : 
     258           0 :     GtkToolItem* pPartModeSelectorToolItem = gtk_tool_item_new();
     259           0 :     GtkWidget* pPartModeComboBox = gtk_combo_box_text_new();
     260           0 :     gtk_container_add( GTK_CONTAINER(pPartModeSelectorToolItem), pPartModeComboBox );
     261           0 :     gtk_toolbar_insert( GTK_TOOLBAR(pToolbar), pPartModeSelectorToolItem, -1 );
     262           0 :     g_signal_connect( G_OBJECT(pPartModeComboBox), "changed", G_CALLBACK(changePartMode), NULL );
     263             : #endif
     264             : 
     265           0 :     GtkToolItem* pSeparator3 = gtk_separator_tool_item_new();
     266           0 :     gtk_toolbar_insert( GTK_TOOLBAR(pToolbar), pSeparator3, -1);
     267             : 
     268           0 :     GtkToolItem* pEnableQuadView = gtk_toggle_tool_button_new();
     269           0 :     gtk_tool_button_set_label( GTK_TOOL_BUTTON(pEnableQuadView), "Use Quad View" );
     270           0 :     gtk_toolbar_insert( GTK_TOOLBAR(pToolbar), pEnableQuadView, -1 );
     271           0 :     g_signal_connect( G_OBJECT(pEnableQuadView), "toggled", G_CALLBACK(changeQuadView), NULL );
     272             : 
     273           0 :     gtk_box_pack_start( GTK_BOX(pVBox), pToolbar, FALSE, FALSE, 0 ); // Adds to top.
     274             : 
     275             :     // Docview
     276           0 :     pDocView = lok_docview_new( pOffice );
     277           0 :     pDocViewQuad = 0;
     278           0 :     gtk_container_add( GTK_CONTAINER(pVBox), pDocView );
     279             : 
     280           0 :     gtk_widget_show_all( pWindow );
     281             : 
     282           0 :     pFileName = argv[2];
     283           0 :     int bOpened = lok_docview_open_document( LOK_DOCVIEW(pDocView), argv[2] );
     284             :     assert( bOpened ); (void)bOpened;
     285             :     assert( LOK_DOCVIEW(pDocView)->pDocument );
     286             : 
     287             :     // GtkComboBox requires gtk 2.24 or later
     288             : #if ( GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION >= 24 ) || GTK_MAJOR_VERSION > 2
     289           0 :     populatePartSelector();
     290           0 :     populatePartModeSelector( GTK_COMBO_BOX_TEXT(pPartModeComboBox) );
     291             : #endif
     292             : 
     293           0 :     gtk_main();
     294             : 
     295           0 :     pOffice->pClass->destroy( pOffice );
     296             : 
     297           0 :     return 0;
     298             : }
     299             : 
     300             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10