为LibreOffice的工具栏添加一个新功能流程

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! :-)

以上只是核心的步骤,当然还有如何增加界面图标,以及界面如何和功能关联增加不在此描述。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值