Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / svx / source / tbxctrls / lboxctrl.cxx
blob0a3706dc85fcd5ae8aa5a1023495b5d5d66b253c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #ifdef _TOOLS_DEBUG_HXX
31 #include <tools/debug.hxx>
32 #endif
33 #include <vcl/lstbox.hxx>
34 #include <vcl/toolbox.hxx>
35 #include <vcl/event.hxx>
36 #include <sfx2/app.hxx>
37 #include <sfx2/tbxctrl.hxx>
38 #include <sfx2/bindings.hxx>
39 #include <sfx2/dispatch.hxx>
40 #include <sfx2/viewsh.hxx>
41 #include <tools/gen.hxx>
42 #include <svl/intitem.hxx>
43 #include <svl/eitem.hxx>
44 #include <svtools/stdctrl.hxx>
45 #include <svl/slstitm.hxx>
46 #include <svl/stritem.hxx>
47 #include <svx/dialmgr.hxx>
48 #include <svx/lboxctrl.hxx>
49 #include <vcl/mnemonic.hxx>
50 #include <tools/urlobj.hxx>
52 #include <svx/svxids.hrc>
53 #include <svx/dialogs.hrc>
55 #include "lboxctrl.hrc"
58 using namespace ::com::sun::star::uno;
59 using namespace ::com::sun::star::beans;
60 using namespace ::com::sun::star::frame;
62 class SvxPopupWindowListBox;
64 /////////////////////////////////////////////////////////////////
66 class SvxPopupWindowListBox : public SfxPopupWindow
68 using FloatingWindow::StateChanged;
70 ListBox * pListBox;
71 ToolBox & rToolBox;
72 sal_Bool bUserSel;
73 sal_uInt16 nTbxId;
74 rtl::OUString maCommandURL;
75 // disallow copy-constructor and assignment-operator
77 SvxPopupWindowListBox(const int& );
78 SvxPopupWindowListBox & operator = (const int& );
80 // SvxPopupWindowListBox( sal_uInt16 nSlotId, ToolBox& rTbx, sal_uInt16 nTbxItemId );
82 public:
83 SvxPopupWindowListBox( sal_uInt16 nSlotId, const rtl::OUString& rCommandURL, sal_uInt16 nTbxId, ToolBox& rTbx );
84 virtual ~SvxPopupWindowListBox();
86 // SfxPopupWindow
87 virtual SfxPopupWindow * Clone() const;
88 virtual void PopupModeEnd();
89 virtual void StateChanged( sal_uInt16 nSID, SfxItemState eState,
90 const SfxPoolItem* pState );
92 inline ListBox & GetListBox() { return *pListBox; }
94 sal_Bool IsUserSelected() const { return bUserSel; }
95 void SetUserSelected( sal_Bool bVal ) { bUserSel = bVal; }
96 /*virtual*/Window* GetPreferredKeyInputWindow();
99 /////////////////////////////////////////////////////////////////
101 SvxPopupWindowListBox::SvxPopupWindowListBox( sal_uInt16 nSlotId, const rtl::OUString& rCommandURL, sal_uInt16 nId, ToolBox& rTbx ) :
102 SfxPopupWindow( nSlotId, Reference< XFrame >(), SVX_RES( RID_SVXTBX_UNDO_REDO_CTRL ) ),
103 rToolBox ( rTbx ),
104 bUserSel ( sal_False ),
105 nTbxId ( nId ),
106 maCommandURL( rCommandURL )
108 DBG_ASSERT( nSlotId == GetId(), "id mismatch" );
109 pListBox = new ListBox( this, SVX_RES( LB_SVXTBX_UNDO_REDO_CTRL ) );
110 FreeResource();
111 pListBox->EnableMultiSelection( sal_True, sal_True );
112 SetBackground( GetSettings().GetStyleSettings().GetDialogColor() );
113 AddStatusListener( rCommandURL );
117 SvxPopupWindowListBox::~SvxPopupWindowListBox()
119 delete pListBox;
123 SfxPopupWindow* SvxPopupWindowListBox::Clone() const
125 return new SvxPopupWindowListBox( GetId(), maCommandURL, nTbxId, rToolBox );
129 void SvxPopupWindowListBox::PopupModeEnd()
131 rToolBox.EndSelection();
132 SfxPopupWindow::PopupModeEnd();
133 //FloatingWindow::PopupModeEnd();
135 if( SfxViewShell::Current() )
137 Window* pShellWnd = SfxViewShell::Current()->GetWindow();
138 if (pShellWnd)
139 pShellWnd->GrabFocus();
144 void SvxPopupWindowListBox::StateChanged(
145 sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem* pState )
147 rToolBox.EnableItem( nTbxId, ( SfxToolBoxControl::GetItemState( pState ) != SFX_ITEM_DISABLED) );
148 SfxPopupWindow::StateChanged( nSID, eState, pState );
151 Window* SvxPopupWindowListBox::GetPreferredKeyInputWindow()
153 // allows forwarding key events in the correct window
154 // without setting the focus
155 return pListBox->GetPreferredKeyInputWindow();
158 /////////////////////////////////////////////////////////////////
160 SvxListBoxControl::SvxListBoxControl( sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rTbx )
161 :SfxToolBoxControl( nSlotId, nId, rTbx ),
162 pPopupWin ( 0 )
164 rTbx.SetItemBits( nId, TIB_DROPDOWN | rTbx.GetItemBits( nId ) );
165 rTbx.Invalidate();
169 SvxListBoxControl::~SvxListBoxControl()
174 SfxPopupWindow* SvxListBoxControl::CreatePopupWindow()
176 OSL_FAIL( "not implemented" );
177 return 0;
181 SfxPopupWindowType SvxListBoxControl::GetPopupWindowType() const
183 return SFX_POPUPWINDOW_ONTIMEOUT;
187 void SvxListBoxControl::StateChanged(
188 sal_uInt16, SfxItemState, const SfxPoolItem* pState )
190 GetToolBox().EnableItem( GetId(),
191 SFX_ITEM_DISABLED != GetItemState(pState) );
195 IMPL_LINK_NOARG(SvxListBoxControl, PopupModeEndHdl)
197 if( pPopupWin && 0 == pPopupWin->GetPopupModeFlags() &&
198 pPopupWin->IsUserSelected() )
200 sal_uInt16 nCount = pPopupWin->GetListBox().GetSelectEntryCount();
202 INetURLObject aObj( m_aCommandURL );
204 Sequence< PropertyValue > aArgs( 1 );
205 aArgs[0].Name = aObj.GetURLPath();
206 aArgs[0].Value = makeAny( sal_Int16( nCount ));
207 SfxToolBoxControl::Dispatch( m_aCommandURL, aArgs );
209 return 0;
213 void SvxListBoxControl::Impl_SetInfo( sal_uInt16 nCount )
215 DBG_ASSERT( pPopupWin, "NULL pointer, PopupWindow missing" );
217 // ListBox &rListBox = pPopupWin->GetListBox();
219 sal_uInt16 nId;
220 if (nCount == 1)
221 nId = SID_UNDO == GetSlotId() ? RID_SVXSTR_NUM_UNDO_ACTION : RID_SVXSTR_NUM_REDO_ACTION;
222 else
223 nId = SID_UNDO == GetSlotId() ? RID_SVXSTR_NUM_UNDO_ACTIONS : RID_SVXSTR_NUM_REDO_ACTIONS;
225 aActionStr = String(SVX_RES(nId));
227 String aText( aActionStr );
228 aText.SearchAndReplaceAllAscii( "$(ARG1)", String::CreateFromInt32( nCount ) );
229 pPopupWin->SetText( aText );
233 IMPL_LINK_NOARG(SvxListBoxControl, SelectHdl)
235 if (pPopupWin)
237 //pPopupWin->SetUserSelected( sal_False );
239 ListBox &rListBox = pPopupWin->GetListBox();
240 if (rListBox.IsTravelSelect())
241 Impl_SetInfo( rListBox.GetSelectEntryCount() );
242 else
244 pPopupWin->SetUserSelected( sal_True );
245 pPopupWin->EndPopupMode( 0 );
248 return 0;
251 /////////////////////////////////////////////////////////////////
253 SFX_IMPL_TOOLBOX_CONTROL( SvxUndoRedoControl, SfxStringItem );
255 SvxUndoRedoControl::SvxUndoRedoControl( sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rTbx )
256 : SvxListBoxControl( nSlotId, nId, rTbx )
258 rTbx.SetItemBits( nId, TIB_DROPDOWN | rTbx.GetItemBits( nId ) );
259 rTbx.Invalidate();
260 aDefaultText = MnemonicGenerator::EraseAllMnemonicChars( rTbx.GetItemText( nId ) );
263 SvxUndoRedoControl::~SvxUndoRedoControl()
267 void SvxUndoRedoControl::StateChanged(
268 sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem* pState )
270 if ( nSID == SID_UNDO || nSID == SID_REDO )
272 if ( eState == SFX_ITEM_DISABLED )
274 ToolBox& rBox = GetToolBox();
275 rBox.SetQuickHelpText( GetId(), aDefaultText );
277 else if ( pState && pState->ISA( SfxStringItem ) )
279 SfxStringItem& rItem = *(SfxStringItem *)pState;
280 ToolBox& rBox = GetToolBox();
281 String aQuickHelpText = MnemonicGenerator::EraseAllMnemonicChars( rItem.GetValue() );
282 rBox.SetQuickHelpText( GetId(), aQuickHelpText );
284 SvxListBoxControl::StateChanged( nSID, eState, pState );
286 else
288 aUndoRedoList.clear();
290 if ( pState && pState->ISA( SfxStringListItem ) )
292 SfxStringListItem &rItem = *(SfxStringListItem *)pState;
294 const std::vector<String> &aLst = rItem.GetList();
295 for( long nI = 0, nEnd = aLst.size(); nI < nEnd; ++nI )
296 aUndoRedoList.push_back( rtl::OUString( aLst[nI] ));
301 SfxPopupWindow* SvxUndoRedoControl::CreatePopupWindow()
303 DBG_ASSERT(( SID_UNDO == GetSlotId() || SID_REDO == GetSlotId() ), "mismatching ids" );
305 if ( m_aCommandURL == ".uno:Undo" )
306 updateStatus( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:GetUndoStrings" )));
307 else
308 updateStatus( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:GetRedoStrings" )));
310 ToolBox& rBox = GetToolBox();
312 pPopupWin = new SvxPopupWindowListBox( GetSlotId(), m_aCommandURL, GetId(), rBox );
313 pPopupWin->SetPopupModeEndHdl( LINK( this, SvxUndoRedoControl,
314 PopupModeEndHdl ) );
315 ListBox &rListBox = pPopupWin->GetListBox();
316 rListBox.SetSelectHdl( LINK( this, SvxUndoRedoControl, SelectHdl ) );
318 for( sal_uInt32 n = 0; n < aUndoRedoList.size(); n++ )
319 rListBox.InsertEntry( String( aUndoRedoList[n] ));
321 rListBox.SelectEntryPos( 0 );
322 aActionStr = String( SVX_RES( SID_UNDO == GetSlotId() ?
323 RID_SVXSTR_NUM_UNDO_ACTIONS : RID_SVXSTR_NUM_REDO_ACTIONS ) );
324 Impl_SetInfo( rListBox.GetSelectEntryCount() );
326 // move focus in floating window without
327 // closing it (GrabFocus() would close it!)
328 pPopupWin->StartPopupMode( &rBox, FLOATWIN_POPUPMODE_GRABFOCUS );
329 //pPopupWin->GetListBox().GrabFocus();
331 return pPopupWin;
334 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */