bump product version to 5.0.4.1
[LibreOffice.git] / svx / source / tbxctrls / lboxctrl.cxx
blobb4f09219c24f7786edab9300a3afcb24f698e239
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <boost/noncopyable.hpp>
23 #include <tools/debug.hxx>
24 #include <sal/types.h>
25 #include <vcl/lstbox.hxx>
26 #include <vcl/toolbox.hxx>
27 #include <vcl/event.hxx>
28 #include <sfx2/app.hxx>
29 #include <sfx2/tbxctrl.hxx>
30 #include <sfx2/bindings.hxx>
31 #include <sfx2/dispatch.hxx>
32 #include <sfx2/viewsh.hxx>
33 #include <svl/intitem.hxx>
34 #include <svl/eitem.hxx>
35 #include <svtools/stdctrl.hxx>
36 #include <svl/slstitm.hxx>
37 #include <svl/stritem.hxx>
38 #include <svx/dialmgr.hxx>
39 #include <svx/lboxctrl.hxx>
40 #include <vcl/mnemonic.hxx>
41 #include <vcl/settings.hxx>
42 #include <tools/urlobj.hxx>
44 #include <svx/svxids.hrc>
45 #include <svx/dialogs.hrc>
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::beans;
49 using namespace ::com::sun::star::frame;
51 class SvxPopupWindowListBox;
53 class SvxPopupWindowListBox: public SfxPopupWindow
55 using FloatingWindow::StateChanged;
57 VclPtr<ListBox> m_pListBox;
58 ToolBox & rToolBox;
59 bool bUserSel;
60 sal_uInt16 nTbxId;
61 OUString maCommandURL;
63 public:
64 SvxPopupWindowListBox( sal_uInt16 nSlotId, const OUString& rCommandURL, sal_uInt16 nTbxId, ToolBox& rTbx );
65 virtual ~SvxPopupWindowListBox();
66 virtual void dispose() SAL_OVERRIDE;
68 // SfxPopupWindow
69 virtual VclPtr<SfxPopupWindow> Clone() const SAL_OVERRIDE;
70 virtual void PopupModeEnd() SAL_OVERRIDE;
71 virtual void StateChanged( sal_uInt16 nSID, SfxItemState eState,
72 const SfxPoolItem* pState ) SAL_OVERRIDE;
74 inline ListBox & GetListBox() { return *m_pListBox; }
76 bool IsUserSelected() const { return bUserSel; }
77 void SetUserSelected( bool bVal ) { bUserSel = bVal; }
78 virtual vcl::Window* GetPreferredKeyInputWindow() SAL_OVERRIDE;
81 SvxPopupWindowListBox::SvxPopupWindowListBox(sal_uInt16 nSlotId, const OUString& rCommandURL, sal_uInt16 nId, ToolBox& rTbx)
82 : SfxPopupWindow(nSlotId, "FloatingUndoRedo", "svx/ui/floatingundoredo.ui")
83 , rToolBox(rTbx)
84 , bUserSel(false)
85 , nTbxId(nId)
86 , maCommandURL(rCommandURL)
88 DBG_ASSERT( nSlotId == GetId(), "id mismatch" );
89 get(m_pListBox, "treeview");
90 WinBits nBits(m_pListBox->GetStyle());
91 nBits &= ~(WB_SIMPLEMODE);
92 m_pListBox->SetStyle(nBits);
93 Size aSize(LogicToPixel(Size(100, 85), MAP_APPFONT));
94 m_pListBox->set_width_request(aSize.Width());
95 m_pListBox->set_height_request(aSize.Height());
96 m_pListBox->EnableMultiSelection( true, true );
97 SetBackground( GetSettings().GetStyleSettings().GetDialogColor() );
98 AddStatusListener( rCommandURL );
101 SvxPopupWindowListBox::~SvxPopupWindowListBox()
103 disposeOnce();
106 void SvxPopupWindowListBox::dispose()
108 m_pListBox.clear();
109 SfxPopupWindow::dispose();
112 VclPtr<SfxPopupWindow> SvxPopupWindowListBox::Clone() const
114 return VclPtr<SvxPopupWindowListBox>::Create( GetId(), maCommandURL, nTbxId, rToolBox );
117 void SvxPopupWindowListBox::PopupModeEnd()
119 rToolBox.EndSelection();
120 SfxPopupWindow::PopupModeEnd();
121 //FloatingWindow::PopupModeEnd();
123 if( SfxViewShell::Current() )
125 vcl::Window* pShellWnd = SfxViewShell::Current()->GetWindow();
126 if (pShellWnd)
127 pShellWnd->GrabFocus();
132 void SvxPopupWindowListBox::StateChanged(
133 sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem* pState )
135 rToolBox.EnableItem( nTbxId, ( SfxToolBoxControl::GetItemState( pState ) != SfxItemState::DISABLED) );
136 SfxPopupWindow::StateChanged( nSID, eState, pState );
139 vcl::Window* SvxPopupWindowListBox::GetPreferredKeyInputWindow()
141 // allows forwarding key events in the correct window
142 // without setting the focus
143 return m_pListBox->GetPreferredKeyInputWindow();
146 SvxListBoxControl::SvxListBoxControl( sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rTbx )
147 :SfxToolBoxControl( nSlotId, nId, rTbx ),
148 pPopupWin ( 0 )
150 rTbx.SetItemBits( nId, ToolBoxItemBits::DROPDOWN | rTbx.GetItemBits( nId ) );
151 rTbx.Invalidate();
155 SvxListBoxControl::~SvxListBoxControl()
158 VclPtr<SfxPopupWindow> SvxListBoxControl::CreatePopupWindow()
160 OSL_FAIL( "not implemented" );
161 return 0;
165 SfxPopupWindowType SvxListBoxControl::GetPopupWindowType() const
167 return SfxPopupWindowType::ONTIMEOUT;
171 void SvxListBoxControl::StateChanged(
172 sal_uInt16, SfxItemState, const SfxPoolItem* pState )
174 GetToolBox().EnableItem( GetId(),
175 SfxItemState::DISABLED != GetItemState(pState) );
179 IMPL_LINK_NOARG(SvxListBoxControl, PopupModeEndHdl)
181 if( pPopupWin && FloatWinPopupFlags::NONE == pPopupWin->GetPopupModeFlags() &&
182 pPopupWin->IsUserSelected() )
184 sal_uInt16 nCount = pPopupWin->GetListBox().GetSelectEntryCount();
186 INetURLObject aObj( m_aCommandURL );
188 Sequence< PropertyValue > aArgs( 1 );
189 aArgs[0].Name = aObj.GetURLPath();
190 aArgs[0].Value = makeAny( sal_Int16( nCount ));
191 SfxToolBoxControl::Dispatch( m_aCommandURL, aArgs );
193 return 0;
197 void SvxListBoxControl::Impl_SetInfo( sal_uInt16 nCount )
199 DBG_ASSERT( pPopupWin, "NULL pointer, PopupWindow missing" );
201 // ListBox &rListBox = pPopupWin->GetListBox();
203 sal_uInt16 nId;
204 if (nCount == 1)
205 nId = SID_UNDO == GetSlotId() ? RID_SVXSTR_NUM_UNDO_ACTION : RID_SVXSTR_NUM_REDO_ACTION;
206 else
207 nId = SID_UNDO == GetSlotId() ? RID_SVXSTR_NUM_UNDO_ACTIONS : RID_SVXSTR_NUM_REDO_ACTIONS;
209 aActionStr = SVX_RESSTR(nId);
211 OUString aText = aActionStr.replaceAll("$(ARG1)", OUString::number(nCount));
212 pPopupWin->SetText(aText);
216 IMPL_LINK_NOARG(SvxListBoxControl, SelectHdl)
218 if (pPopupWin)
220 //pPopupWin->SetUserSelected( false );
222 ListBox &rListBox = pPopupWin->GetListBox();
223 if (rListBox.IsTravelSelect())
224 Impl_SetInfo( rListBox.GetSelectEntryCount() );
225 else
227 pPopupWin->SetUserSelected( true );
228 pPopupWin->EndPopupMode();
231 return 0;
236 SFX_IMPL_TOOLBOX_CONTROL( SvxUndoRedoControl, SfxStringItem );
238 SvxUndoRedoControl::SvxUndoRedoControl( sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rTbx )
239 : SvxListBoxControl( nSlotId, nId, rTbx )
241 rTbx.SetItemBits( nId, ToolBoxItemBits::DROPDOWN | rTbx.GetItemBits( nId ) );
242 rTbx.Invalidate();
243 aDefaultText = MnemonicGenerator::EraseAllMnemonicChars( rTbx.GetItemText( nId ) );
246 SvxUndoRedoControl::~SvxUndoRedoControl()
250 void SvxUndoRedoControl::StateChanged(
251 sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem* pState )
253 if ( nSID == SID_UNDO || nSID == SID_REDO )
255 if ( eState == SfxItemState::DISABLED )
257 ToolBox& rBox = GetToolBox();
258 rBox.SetQuickHelpText( GetId(), aDefaultText );
260 else if ( pState && pState->ISA( SfxStringItem ) )
262 const SfxStringItem& rItem = *static_cast<const SfxStringItem *>(pState);
263 ToolBox& rBox = GetToolBox();
264 OUString aQuickHelpText = MnemonicGenerator::EraseAllMnemonicChars( rItem.GetValue() );
265 rBox.SetQuickHelpText( GetId(), aQuickHelpText );
267 SvxListBoxControl::StateChanged( nSID, eState, pState );
269 else
271 aUndoRedoList.clear();
273 if ( pState && pState->ISA( SfxStringListItem ) )
275 const SfxStringListItem &rItem = *static_cast<const SfxStringListItem *>(pState);
277 const std::vector<OUString> &aLst = rItem.GetList();
278 for( long nI = 0, nEnd = aLst.size(); nI < nEnd; ++nI )
279 aUndoRedoList.push_back( aLst[nI] );
284 VclPtr<SfxPopupWindow> SvxUndoRedoControl::CreatePopupWindow()
286 DBG_ASSERT(( SID_UNDO == GetSlotId() || SID_REDO == GetSlotId() ), "mismatching ids" );
288 if ( m_aCommandURL == ".uno:Undo" )
289 updateStatus( OUString( ".uno:GetUndoStrings" ));
290 else
291 updateStatus( OUString( ".uno:GetRedoStrings" ));
293 ToolBox& rBox = GetToolBox();
295 pPopupWin = VclPtr<SvxPopupWindowListBox>::Create( GetSlotId(), m_aCommandURL, GetId(), rBox );
296 pPopupWin->SetPopupModeEndHdl( LINK( this, SvxUndoRedoControl,
297 PopupModeEndHdl ) );
298 ListBox &rListBox = pPopupWin->GetListBox();
299 rListBox.SetSelectHdl( LINK( this, SvxUndoRedoControl, SelectHdl ) );
301 for( sal_uInt32 n = 0; n < aUndoRedoList.size(); n++ )
302 rListBox.InsertEntry( aUndoRedoList[n] );
304 rListBox.SelectEntryPos( 0 );
305 aActionStr = SVX_RESSTR(SID_UNDO == GetSlotId() ?
306 RID_SVXSTR_NUM_UNDO_ACTIONS : RID_SVXSTR_NUM_REDO_ACTIONS);
307 Impl_SetInfo( rListBox.GetSelectEntryCount() );
309 // move focus in floating window without
310 // closing it (GrabFocus() would close it!)
311 pPopupWin->StartPopupMode( &rBox, FloatWinPopupFlags::GrabFocus );
312 //pPopupWin->GetListBox().GrabFocus();
314 return pPopupWin;
317 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */