xref: /aoo42x/main/sw/source/ui/utlui/bookctrl.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sw.hxx"
30 
31 #include "hintids.hxx"
32 
33 #ifndef _SVSTDARR_HXX
34 #define _SVSTDARR_USHORTS
35 #include <svl/svstdarr.hxx>
36 #endif
37 #include <svl/intitem.hxx>
38 #include <svl/stritem.hxx>
39 #include <sfx2/dispatch.hxx>
40 #ifndef _EVENT_HXX //autogen
41 #include <vcl/event.hxx>
42 #endif
43 #ifndef _STATUS_HXX //autogen
44 #include <vcl/status.hxx>
45 #endif
46 #ifndef _MENU_HXX //autogen
47 #include <vcl/menu.hxx>
48 #endif
49 #include "cmdid.h"
50 #include "errhdl.hxx"
51 #include "swmodule.hxx"
52 #include "wrtsh.hxx"
53 #include "IMark.hxx"
54 #include "bookctrl.hxx"
55 #include <map>
56 
57 SFX_IMPL_STATUSBAR_CONTROL( SwBookmarkControl, SfxStringItem );
58 
59 // class BookmarkPopup_Impl --------------------------------------------------
60 
61 class BookmarkPopup_Impl : public PopupMenu
62 {
63 public:
64 	BookmarkPopup_Impl();
65 
66 	sal_uInt16			GetCurId() const { return nCurId; }
67 
68 private:
69 	sal_uInt16			nCurId;
70 
71 	virtual void    Select();
72 };
73 
74 // -----------------------------------------------------------------------
75 
76 BookmarkPopup_Impl::BookmarkPopup_Impl() :
77 	PopupMenu(),
78 	nCurId(USHRT_MAX)
79 {
80 }
81 
82 // -----------------------------------------------------------------------
83 
84 void BookmarkPopup_Impl::Select()
85 {
86 	nCurId = GetCurItemId();
87 }
88 
89 // class SvxZoomStatusBarControl ------------------------------------------
90 
91 SwBookmarkControl::SwBookmarkControl( sal_uInt16 _nSlotId,
92                                       sal_uInt16 _nId,
93 	                                  StatusBar& rStb ) :
94     SfxStatusBarControl( _nSlotId, _nId, rStb )
95 {
96 }
97 
98 // -----------------------------------------------------------------------
99 
100 SwBookmarkControl::~SwBookmarkControl()
101 {
102 }
103 
104 // -----------------------------------------------------------------------
105 
106 void SwBookmarkControl::StateChanged(
107     sal_uInt16 /*nSID*/, SfxItemState eState, const SfxPoolItem* pState )
108 {
109 	if( eState != SFX_ITEM_AVAILABLE || pState->ISA( SfxVoidItem ) )
110 		GetStatusBar().SetItemText( GetId(), String() );
111 	else if ( pState->ISA( SfxStringItem ) )
112 	{
113 		sPageNumber = ((SfxStringItem*)pState)->GetValue();
114 		GetStatusBar().SetItemText( GetId(), sPageNumber );
115 	}
116 }
117 
118 // -----------------------------------------------------------------------
119 
120 void SwBookmarkControl::Paint( const UserDrawEvent&  )
121 {
122 	GetStatusBar().SetItemText( GetId(), sPageNumber );
123 }
124 
125 // -----------------------------------------------------------------------
126 
127 void SwBookmarkControl::Command( const CommandEvent& rCEvt )
128 {
129     if ( rCEvt.GetCommand() == COMMAND_CONTEXTMENU &&
130             GetStatusBar().GetItemText( GetId() ).Len() )
131     {
132         CaptureMouse();
133         BookmarkPopup_Impl aPop;
134         SwWrtShell* pWrtShell = ::GetActiveWrtShell();
135         if( pWrtShell && pWrtShell->getIDocumentMarkAccess()->getMarksCount() > 0 )
136         {
137             IDocumentMarkAccess* const pMarkAccess = pWrtShell->getIDocumentMarkAccess();
138             IDocumentMarkAccess::const_iterator_t ppBookmarkStart = pMarkAccess->getBookmarksBegin();
139             sal_uInt16 nPopupId = 1;
140             ::std::map<sal_Int32, sal_uInt16> aBookmarkIdx;
141             for(IDocumentMarkAccess::const_iterator_t ppBookmark = ppBookmarkStart;
142                 ppBookmark != pMarkAccess->getBookmarksEnd();
143                 ppBookmark++)
144             {
145                 if(IDocumentMarkAccess::BOOKMARK == IDocumentMarkAccess::GetType(**ppBookmark))
146                 {
147                     aPop.InsertItem( nPopupId, ppBookmark->get()->GetName() );
148                     aBookmarkIdx[nPopupId] = static_cast<sal_uInt16>(ppBookmark - ppBookmarkStart);
149                     nPopupId++;
150                 }
151             }
152             aPop.Execute( &GetStatusBar(), rCEvt.GetMousePosPixel());
153             sal_uInt16 nCurrId = aPop.GetCurId();
154             if( nCurrId != USHRT_MAX)
155             {
156                 SfxUInt16Item aBookmark( FN_STAT_BOOKMARK, aBookmarkIdx[nCurrId] );
157                 SfxViewFrame::Current()->GetDispatcher()->Execute( FN_STAT_BOOKMARK,
158                     SFX_CALLMODE_ASYNCHRON|SFX_CALLMODE_RECORD,
159                                         &aBookmark, 0L );
160             }
161         }
162         ReleaseMouse();
163     }
164 }
165