LCOV - code coverage report
Current view: top level - include/LibreOfficeKit - LibreOfficeKit.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 28 42 66.7 %
Date: 2015-06-13 12:38:46 Functions: 8 13 61.5 %
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             : #ifndef INCLUDED_LIBREOFFICEKIT_LIBREOFFICEKIT_HXX
      11             : #define INCLUDED_LIBREOFFICEKIT_LIBREOFFICEKIT_HXX
      12             : 
      13             : #include "LibreOfficeKit.h"
      14             : 
      15             : /*
      16             :  * The reasons this C++ code is not as pretty as it could be are:
      17             :  *  a) provide a pure C API - that's useful for some people
      18             :  *  b) allow ABI stability - C++ vtables are not good for that.
      19             :  *  c) avoid C++ types as part of the API.
      20             :  */
      21             : namespace lok
      22             : {
      23             : 
      24             : /// The lok::Document class represents one loaded document instance.
      25             : class Document
      26             : {
      27             : private:
      28             :     LibreOfficeKitDocument* mpDoc;
      29             : 
      30             : public:
      31             :     /// A lok::Document is typically created by the lok::Office::documentLoad() method.
      32           2 :     inline Document(LibreOfficeKitDocument* pDoc) :
      33           2 :         mpDoc(pDoc)
      34           2 :     {}
      35             : 
      36           2 :     inline ~Document()
      37             :     {
      38           2 :         mpDoc->pClass->destroy(mpDoc);
      39           2 :     }
      40             : 
      41             :     /**
      42             :      * Stores the document's persistent data to a URL and
      43             :      * continues to be a representation of the old URL.
      44             :      *
      45             :      * @param pUrl the location where to store the document
      46             :      * @param pFormat the format to use while exporting, when omitted, then deducted from pURL's extension
      47             :      * @param pFilterOptions options for the export filter, e.g. SkipImages.
      48             :      */
      49             :     inline bool saveAs(const char* pUrl, const char* pFormat = NULL, const char* pFilterOptions = NULL)
      50             :     {
      51             :         return mpDoc->pClass->saveAs(mpDoc, pUrl, pFormat, pFilterOptions) != 0;
      52             :     }
      53             : 
      54             :     /// Gives access to the underlying C pointer.
      55             :     inline LibreOfficeKitDocument *get() { return mpDoc; }
      56             : 
      57             : #ifdef LOK_USE_UNSTABLE_API
      58             :     /**
      59             :      * Get document type.
      60             :      *
      61             :      * @return an element of the LibreOfficeKitDocumentType enum.
      62             :      */
      63           2 :     inline int getDocumentType()
      64             :     {
      65           2 :         return mpDoc->pClass->getDocumentType(mpDoc);
      66             :     }
      67             : 
      68             :     /**
      69             :      * Get number of part that the document contains.
      70             :      *
      71             :      * Part refers to either indivual sheets in a Calc, or slides in Impress,
      72             :      * and has no relevance for Writer.
      73             :      */
      74           0 :     inline int getParts()
      75             :     {
      76           0 :         return mpDoc->pClass->getParts(mpDoc);
      77             :     }
      78             : 
      79             :     /// Get the current part of the document.
      80             :     inline int getPart()
      81             :     {
      82             :         return mpDoc->pClass->getPart(mpDoc);
      83             :     }
      84             : 
      85             :     /// Set the current part of the document.
      86           0 :     inline void setPart(int nPart)
      87             :     {
      88           0 :         mpDoc->pClass->setPart(mpDoc, nPart);
      89           0 :     }
      90             : 
      91             :     /// Get the current part's name.
      92           0 :     inline char* getPartName(int nPart)
      93             :     {
      94           0 :         return mpDoc->pClass->getPartName(mpDoc, nPart);
      95             :     }
      96             : 
      97             :     /**
      98             :      * Renders a subset of the document to a pre-allocated buffer.
      99             :      *
     100             :      * Note that the buffer size and the tile size implicitly supports
     101             :      * rendering at different zoom levels, as the number of rendered pixels and
     102             :      * the rendered rectangle of the document are independent.
     103             :      *
     104             :      * @param pBuffer pointer to the buffer, its size is determined by nCanvasWidth and nCanvasHeight.
     105             :      * @param nCanvasWidth number of pixels in a row of pBuffer.
     106             :      * @param nCanvasHeight number of pixels in a column of pBuffer.
     107             :      * @param nTilePosX logical X position of the top left corner of the rendered rectangle, in TWIPs.
     108             :      * @param nTilePosY logical Y position of the top left corner of the rendered rectangle, in TWIPs.
     109             :      * @param nTileWidth logical width of the rendered rectangle, in TWIPs.
     110             :      * @param nTileHeight logical height of the rendered rectangle, in TWIPs.
     111             :      */
     112           0 :     inline void paintTile(
     113             :                           unsigned char* pBuffer,
     114             :                           const int nCanvasWidth,
     115             :                           const int nCanvasHeight,
     116             :                           const int nTilePosX,
     117             :                           const int nTilePosY,
     118             :                           const int nTileWidth,
     119             :                           const int nTileHeight)
     120             :     {
     121             :         return mpDoc->pClass->paintTile(mpDoc, pBuffer, nCanvasWidth, nCanvasHeight,
     122           0 :                                 nTilePosX, nTilePosY, nTileWidth, nTileHeight);
     123             :     }
     124             : 
     125             :     /// Get the document sizes in TWIPs.
     126           0 :     inline void getDocumentSize(long* pWidth, long* pHeight)
     127             :     {
     128           0 :         mpDoc->pClass->getDocumentSize(mpDoc, pWidth, pHeight);
     129           0 :     }
     130             : 
     131             :     /**
     132             :      * Initialize document for rendering.
     133             :      *
     134             :      * Sets the rendering and document parameters to default values that are
     135             :      * needed to render the document correctly using tiled rendering. This
     136             :      * method has to be called right after documentLoad() in case any of the
     137             :      * tiled rendering methods are to be used later.
     138             :      */
     139             :     inline void initializeForRendering()
     140             :     {
     141             :         mpDoc->pClass->initializeForRendering(mpDoc);
     142             :     }
     143             : 
     144             :     /**
     145             :      * Registers a callback. LOK will invoke this function when it wants to
     146             :      * inform the client about events.
     147             :      *
     148             :      * @param pCallback the callback to invoke
     149             :      * @param pData the user data, will be passed to the callback on invocation
     150             :      */
     151             :     inline void registerCallback(LibreOfficeKitCallback pCallback, void* pData)
     152             :     {
     153             :         mpDoc->pClass->registerCallback(mpDoc, pCallback, pData);
     154             :     }
     155             : 
     156             :     /**
     157             :      * Posts a keyboard event to the focused frame.
     158             :      *
     159             :      * @param nType Event type, like press or release.
     160             :      * @param nCharCode contains the Unicode character generated by this event or 0
     161             :      * @param nKeyCode contains the integer code representing the key of the event (non-zero for control keys)
     162             :      */
     163             :     inline void postKeyEvent(int nType, int nCharCode, int nKeyCode)
     164             :     {
     165             :         mpDoc->pClass->postKeyEvent(mpDoc, nType, nCharCode, nKeyCode);
     166             :     }
     167             : 
     168             :     /**
     169             :      * Posts a mouse event to the document.
     170             :      *
     171             :      * @param nType Event type, like down, move or up.
     172             :      * @param nX horizontal position in document coordinates
     173             :      * @param nY vertical position in document coordinates
     174             :      * @param nCount number of clicks: 1 for single click, 2 for double click
     175             :      */
     176             :     inline void postMouseEvent(int nType, int nX, int nY, int nCount)
     177             :     {
     178             :         mpDoc->pClass->postMouseEvent(mpDoc, nType, nX, nY, nCount);
     179             :     }
     180             : 
     181             :     /**
     182             :      * Posts an UNO command to the document.
     183             :      *
     184             :      * Example argument string:
     185             :      *
     186             :      * {
     187             :      *     "SearchItem.SearchString":
     188             :      *     {
     189             :      *         "type": "string",
     190             :      *         "value": "foobar"
     191             :      *     },
     192             :      *     "SearchItem.Backward":
     193             :      *     {
     194             :      *         "type": "boolean",
     195             :      *         "value": "false"
     196             :      *     }
     197             :      * }
     198             :      *
     199             :      * @param pCommand uno command to be posted to the document, like ".uno:Bold"
     200             :      * @param pArguments arguments of the uno command.
     201             :      */
     202           1 :     inline void postUnoCommand(const char* pCommand, const char* pArguments = 0)
     203             :     {
     204           1 :         mpDoc->pClass->postUnoCommand(mpDoc, pCommand, pArguments);
     205           1 :     }
     206             : 
     207             :     /**
     208             :      * Sets the start or end of a text selection.
     209             :      *
     210             :      * @param nType @see LibreOfficeKitSetTextSelectionType
     211             :      * @param nX horizontal position in document coordinates
     212             :      * @param nY vertical position in document coordinates
     213             :      */
     214             :     inline void setTextSelection(int nType, int nX, int nY)
     215             :     {
     216             :         mpDoc->pClass->setTextSelection(mpDoc, nType, nX, nY);
     217             :     }
     218             : 
     219             :     /**
     220             :      * Adjusts the graphic selection.
     221             :      *
     222             :      * @param nType @see LibreOfficeKitSetGraphicSelectionType
     223             :      * @param nX horizontal position in document coordinates
     224             :      * @param nY vertical position in document coordinates
     225             :      */
     226             :     inline void setGraphicSelection(int nType, int nX, int nY)
     227             :     {
     228             :         mpDoc->pClass->setGraphicSelection(mpDoc, nType, nX, nY);
     229             :     }
     230             : 
     231             :     /**
     232             :      * Gets rid of any text or graphic selection.
     233             :      */
     234             :     inline void resetSelection()
     235             :     {
     236             :         mpDoc->pClass->resetSelection(mpDoc);
     237             :     }
     238             : #endif // LOK_USE_UNSTABLE_API
     239             : };
     240             : 
     241             : /// The lok::Office class represents one started LibreOfficeKit instance.
     242             : class Office
     243             : {
     244             : private:
     245             :     LibreOfficeKit* mpThis;
     246             : 
     247             : public:
     248             :     /// A lok::Office is typically created by the lok_cpp_init() function.
     249           1 :     inline Office(LibreOfficeKit* pThis) :
     250           1 :         mpThis(pThis)
     251           1 :     {}
     252             : 
     253           1 :     inline ~Office()
     254             :     {
     255           1 :         mpThis->pClass->destroy(mpThis);
     256           1 :     }
     257             : 
     258             :     /**
     259             :      * Loads a document from an URL.
     260             :      *
     261             :      * @param pUrl the URL of the document to load
     262             :      * @param pFilterOptions options for the import filter, e.g. SkipImages.
     263             :      */
     264           3 :     inline Document* documentLoad(const char* pUrl, const char* pFilterOptions = NULL)
     265             :     {
     266           3 :         LibreOfficeKitDocument* pDoc = NULL;
     267             : 
     268           3 :         if (LIBREOFFICEKIT_HAS(mpThis, documentLoadWithOptions))
     269           3 :             pDoc = mpThis->pClass->documentLoadWithOptions(mpThis, pUrl, pFilterOptions);
     270             :         else
     271           0 :             pDoc = mpThis->pClass->documentLoad(mpThis, pUrl);
     272             : 
     273           3 :         if (pDoc == NULL)
     274           1 :             return NULL;
     275             : 
     276           2 :         return new Document(pDoc);
     277             :     }
     278             : 
     279             :     /// Returns the last error as a string, the returned pointer has to be freed by the caller.
     280             :     inline char* getError()
     281             :     {
     282             :         return mpThis->pClass->getError(mpThis);
     283             :     }
     284             : };
     285             : 
     286             : /// Factory method to create a lok::Office instance.
     287           1 : inline Office* lok_cpp_init(const char* pInstallPath, const char* pUserProfilePath = NULL)
     288             : {
     289           1 :     LibreOfficeKit* pThis = lok_init_2(pInstallPath, pUserProfilePath);
     290           1 :     if (pThis == NULL || pThis->pClass->nSize == 0)
     291           0 :         return NULL;
     292           1 :     return new ::lok::Office(pThis);
     293             : }
     294             : 
     295             : }
     296             : 
     297             : #endif // INCLUDED_LIBREOFFICEKIT_LIBREOFFICEKIT_HXX
     298             : 
     299             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11