Tutorial Writer

From Apache OpenOffice Wiki
Revision as of 12:33, 28 November 2006 by Kr (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Hack Writer! Tutorial_Help

Now let's launch a bigger window than just a tiny little InfoBox. So we want to click on a graphic in Writer and have the option of saving the graphic/ picture to disk direct!

Cool! So what we would have to start out with is defining a identifier we will use throughout and we stick it in where all the other identifiers are located:

--- sw/inc/cmdid.h      2004-12-07 18:57:10.000000000 +0530
+++ sw/inc/cmdid.h      2004-12-21 19:08:39.000000000 +0530
@@ -159,6 +159,7 @@ Achtung: Ab sofort sind in diesem File k
 
 #define FN_GET_DOCSTAT                 (FN_FILE + 33)    /* Dokumentstatistik einzeln auslesen */
 
+#define FN_SAVE_GRAPHIC       (FN_FILE + 34)    /* Save embedded graphic as */
 
 #define FN_SAVE_SELECTION       (FN_FILE + 35)    /* Selektion speichern */

So that's going to be our identifier, next we qualify our identifier for what method to call when it is invoked, through whichever channel:

--- sw/sdi/_grfsh.sdi   2004-10-15 16:13:21.000000000 +0530
+++ sw/sdi/_grfsh.sdi   2004-12-21 20:15:52.000000000 +0530
@@ -85,6 +85,13 @@ interface BaseTextGraphic : Selection
                DisableFlags="SW_DISABLE_ON_PROTECTED_CURSOR";
        ]
 
+       FN_SAVE_GRAPHIC
+       [
+               ExecMethod = Execute ;
+               StateMethod = NoState ;
+               DisableFlags="SW_DISABLE_ON_PROTECTED_CURSOR";
+       ]
+
        SID_INSERT_GRAPHIC // zeigt auf FN_FORMAT_GRAFIC_DLG
        [
                ExecMethod = Execute ;

There are also a whole lot of attributes that need to be defined for any slot that is used to hold a menu item or a toolbar item and these attributes are defined as Sfx...Items:

--- sw/sdi/swriter.sdi  2004-11-29 14:28:57.000000000 +0530
+++ sw/sdi/swriter.sdi  2004-12-21 19:13:45.000000000 +0530
@@ -3466,6 +3466,31 @@ SfxVoidItem GraphicDialog FN_FORMAT_GRAF
 ]
 
 //--------------------------------------------------------------------------
+SfxVoidItem SaveGraphic FN_SAVE_GRAPHIC
+()
+[
+       /* flags: */
+       AutoUpdate = FALSE,
+       Cachable = Cachable,
+       FastCall = FALSE,
+       HasCoreId = FALSE,
+       HasDialog = TRUE,
+       ReadOnlyDoc = FALSE,
+       Toggle = FALSE,
+       Container = FALSE,
+       RecordAbsolute = FALSE,
+       RecordPerItem;
+       Asynchron;
+
+       /* config: */
+       AccelConfig = TRUE,
+       MenuConfig = TRUE,
+       StatusBarConfig = FALSE,
+       ToolBoxConfig = TRUE,
+       GroupId = GID_GRAPHIC;
+]
+
+//--------------------------------------------------------------------------
 SfxVoidItem Grow FN_GROW_FONT_SIZE
 ()
 [

With that, we are ready to use our brand new sfx item and we'll use it by adding it into the popup menu that is specified as MN_GRF_POPUPMENU in mn.src:

--- sw/source/ui/app/mn.src     2004-11-29 14:29:10.000000000 +0530
+++ sw/source/ui/app/mn.src     2004-12-21 20:28:27.000000000 +0530
@@ -945,6 +945,13 @@ Menu MN_GRF_POPUPMENU
                SEPARATOR ;
         MenuItem
         {
+            Identifier = FN_SAVE_GRAPHIC ; 
+            HelpID = FN_SAVE_GRAPHIC ; 
+            Text [ de ] = "~Grafik speichern..." ; 
+            Text [ en-US ] = "Save ~Graphic..." ; 
+        };
+        MenuItem
+        {
             Identifier = FN_FORMAT_GRAFIC_DLG ; 
             HelpID = FN_FORMAT_GRAFIC_DLG ; 
             Text [ de ] = "~Grafik..." ; 

That adds in the item to the menu and we still need to define what happens when it is clicked as we have specified in our handler that it invokes the Execute method, so we'll fill out the handler for this item in that method:

--- sw/source/ui/shells/grfsh.cxx       2004-12-07 18:57:35.000000000 +0530
+++ sw/source/ui/shells/grfsh.cxx       2004-12-21 22:19:53.684296352 +0530
@@ -217,6 +227,11 @@ void SwGrfShell::Execute(SfxRequest &rRe
        USHORT nSlot = rReq.GetSlot();
        switch(nSlot)
        {
+               case FN_SAVE_GRAPHIC:
+                   SaveGraphic();
+                   rReq.Ignore();
+                   break;
+
                case SID_INSERT_GRAPHIC:
                case FN_FORMAT_GRAFIC_DLG:
                {

So we define a method called SaveGraphic that will be invoked when this item is clicked, and then the definition of the method:

--- sw/source/ui/shells/grfsh.cxx       2004-12-07 18:57:35.000000000 +0530
+++ sw/source/ui/shells/grfsh.cxx       2004-12-21 22:19:53.684296352 +0530
@@ -739,6 +754,20 @@ void SwGrfShell::GetAttrState(SfxItemSet
 }
 
 
+void SwGrfShell::SaveGraphic()
+{
+       using namespace ::sfx2;
+       using namespace com::sun::star::ui::dialogs;
+       using namespace ::com::sun::star::uno;
+
+       FileDialogHelper aDlgHelper( TemplateDescription::FILESAVE_SIMPLE, 0 );
+       Reference  < XFilePicker > xFP = aDlgHelper.GetFilePicker();
+       aDlgHelper.Execute();
+
+       InfoBox( NULL, xFP->getFiles()[0] ).Execute();
+}
+
+
 SwGrfShell::SwGrfShell(SwView &rView) :
        SwBaseShell(rView)

Can you predict what the function does ? :-) As promised, it launches a dialog box that is definitely bigger than an InfoBox and then follows up with an InfoBox anyway :-D

For all these objects we have used, we need to declare them as well:

--- sw/source/ui/shells/grfsh.cxx       2004-12-07 18:57:35.000000000 +0530
+++ sw/source/ui/shells/grfsh.cxx       2004-12-21 22:19:53.684296352 +0530
@@ -137,6 +137,16 @@<br>
 #include <svx/tbxcolor.hxx><br>

 #endif
 
+#ifndef _COM_SUN_STAR_UI_DIALOGS_XFILEPICKER_HPP_
+#include <com/sun/star/ui/dialogs/XFilePicker.hpp>
+#endif
+#ifndef  _COM_SUN_STAR_UI_DIALOGS_TEMPLATEDESCRIPTION_HPP_
+#include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
+#endif
+#ifndef _FILEDLGHELPER_HXX
+#include <sfx2/filedlghelper.hxx>
+#endif
+
 #ifndef _FMTURL_HXX //autogen
 #include <fmturl.hxx>
 #endif

And we also have to declare the new method that we have added to the class in the class definition:

--- sw/source/ui/inc/grfsh.hxx  2000-10-05 17:04:19.000000000 +0530
+++ sw/source/ui/inc/grfsh.hxx  2004-12-21 19:24:14.000000000 +0530
@@ -71,6 +71,7 @@ public:
        void    Execute(SfxRequest &);
        void    ExecAttr(SfxRequest &);
        void    GetAttrState(SfxItemSet &);
+       void    SaveGraphic();
 
                        SwGrfShell(SwView &rView);
 };

So there stands the porting of the wonderful sd-save-image-context-menu as we plan to apply to Writer. We still need to extract the selected graphic out of Writer and save it to disk. If that is done, the patch would be complete :-)

Any takers ? :-)

But after understanding this, we can understand another hack on which this is based - the hack in Impress that allows the same capability, the difference being that it is complete! :-)


sw-save-image-context-menu.diff Next: Tutorial_Impress
Personal tools