Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <sal/config.h>
21 :
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>
43 :
44 : #include <svx/svxids.hrc>
45 : #include <svx/dialogs.hrc>
46 :
47 : #include "lboxctrl.hrc"
48 :
49 :
50 : using namespace ::com::sun::star::uno;
51 : using namespace ::com::sun::star::beans;
52 : using namespace ::com::sun::star::frame;
53 :
54 : class SvxPopupWindowListBox;
55 :
56 :
57 :
58 : class SvxPopupWindowListBox: public SfxPopupWindow, private boost::noncopyable
59 : {
60 : using FloatingWindow::StateChanged;
61 :
62 : ListBox * pListBox;
63 : ToolBox & rToolBox;
64 : bool bUserSel;
65 : sal_uInt16 nTbxId;
66 : OUString maCommandURL;
67 : // disallow copy-constructor and assignment-operator
68 :
69 : // SvxPopupWindowListBox( sal_uInt16 nSlotId, ToolBox& rTbx, sal_uInt16 nTbxItemId );
70 :
71 : public:
72 : SvxPopupWindowListBox( sal_uInt16 nSlotId, const OUString& rCommandURL, sal_uInt16 nTbxId, ToolBox& rTbx );
73 : virtual ~SvxPopupWindowListBox();
74 :
75 : // SfxPopupWindow
76 : virtual SfxPopupWindow * Clone() const SAL_OVERRIDE;
77 : virtual void PopupModeEnd() SAL_OVERRIDE;
78 : virtual void StateChanged( sal_uInt16 nSID, SfxItemState eState,
79 : const SfxPoolItem* pState ) SAL_OVERRIDE;
80 :
81 0 : inline ListBox & GetListBox() { return *pListBox; }
82 :
83 0 : bool IsUserSelected() const { return bUserSel; }
84 0 : void SetUserSelected( bool bVal ) { bUserSel = bVal; }
85 : /*virtual*/Window* GetPreferredKeyInputWindow() SAL_OVERRIDE;
86 : };
87 :
88 :
89 :
90 0 : SvxPopupWindowListBox::SvxPopupWindowListBox( sal_uInt16 nSlotId, const OUString& rCommandURL, sal_uInt16 nId, ToolBox& rTbx ) :
91 0 : SfxPopupWindow( nSlotId, Reference< XFrame >(), SVX_RES( RID_SVXTBX_UNDO_REDO_CTRL ) ),
92 : rToolBox ( rTbx ),
93 : bUserSel ( false ),
94 : nTbxId ( nId ),
95 0 : maCommandURL( rCommandURL )
96 : {
97 : DBG_ASSERT( nSlotId == GetId(), "id mismatch" );
98 0 : pListBox = new ListBox( this, SVX_RES( LB_SVXTBX_UNDO_REDO_CTRL ) );
99 0 : FreeResource();
100 0 : pListBox->EnableMultiSelection( true, true );
101 0 : SetBackground( GetSettings().GetStyleSettings().GetDialogColor() );
102 0 : AddStatusListener( rCommandURL );
103 0 : }
104 :
105 :
106 0 : SvxPopupWindowListBox::~SvxPopupWindowListBox()
107 : {
108 0 : delete pListBox;
109 0 : }
110 :
111 :
112 0 : SfxPopupWindow* SvxPopupWindowListBox::Clone() const
113 : {
114 0 : return new SvxPopupWindowListBox( GetId(), maCommandURL, nTbxId, rToolBox );
115 : }
116 :
117 :
118 0 : void SvxPopupWindowListBox::PopupModeEnd()
119 : {
120 0 : rToolBox.EndSelection();
121 0 : SfxPopupWindow::PopupModeEnd();
122 : //FloatingWindow::PopupModeEnd();
123 :
124 0 : if( SfxViewShell::Current() )
125 : {
126 0 : Window* pShellWnd = SfxViewShell::Current()->GetWindow();
127 0 : if (pShellWnd)
128 0 : pShellWnd->GrabFocus();
129 : }
130 0 : }
131 :
132 :
133 0 : void SvxPopupWindowListBox::StateChanged(
134 : sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem* pState )
135 : {
136 0 : rToolBox.EnableItem( nTbxId, ( SfxToolBoxControl::GetItemState( pState ) != SFX_ITEM_DISABLED) );
137 0 : SfxPopupWindow::StateChanged( nSID, eState, pState );
138 0 : }
139 :
140 0 : Window* SvxPopupWindowListBox::GetPreferredKeyInputWindow()
141 : {
142 : // allows forwarding key events in the correct window
143 : // without setting the focus
144 0 : return pListBox->GetPreferredKeyInputWindow();
145 : }
146 :
147 :
148 :
149 0 : SvxListBoxControl::SvxListBoxControl( sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rTbx )
150 : :SfxToolBoxControl( nSlotId, nId, rTbx ),
151 0 : pPopupWin ( 0 )
152 : {
153 0 : rTbx.SetItemBits( nId, TIB_DROPDOWN | rTbx.GetItemBits( nId ) );
154 0 : rTbx.Invalidate();
155 0 : }
156 :
157 :
158 0 : SvxListBoxControl::~SvxListBoxControl()
159 : {
160 0 : }
161 :
162 :
163 0 : SfxPopupWindow* SvxListBoxControl::CreatePopupWindow()
164 : {
165 : OSL_FAIL( "not implemented" );
166 0 : return 0;
167 : }
168 :
169 :
170 0 : SfxPopupWindowType SvxListBoxControl::GetPopupWindowType() const
171 : {
172 0 : return SFX_POPUPWINDOW_ONTIMEOUT;
173 : }
174 :
175 :
176 0 : void SvxListBoxControl::StateChanged(
177 : sal_uInt16, SfxItemState, const SfxPoolItem* pState )
178 : {
179 0 : GetToolBox().EnableItem( GetId(),
180 0 : SFX_ITEM_DISABLED != GetItemState(pState) );
181 0 : }
182 :
183 :
184 0 : IMPL_LINK_NOARG(SvxListBoxControl, PopupModeEndHdl)
185 : {
186 0 : if( pPopupWin && 0 == pPopupWin->GetPopupModeFlags() &&
187 0 : pPopupWin->IsUserSelected() )
188 : {
189 0 : sal_uInt16 nCount = pPopupWin->GetListBox().GetSelectEntryCount();
190 :
191 0 : INetURLObject aObj( m_aCommandURL );
192 :
193 0 : Sequence< PropertyValue > aArgs( 1 );
194 0 : aArgs[0].Name = aObj.GetURLPath();
195 0 : aArgs[0].Value = makeAny( sal_Int16( nCount ));
196 0 : SfxToolBoxControl::Dispatch( m_aCommandURL, aArgs );
197 : }
198 0 : return 0;
199 : }
200 :
201 :
202 0 : void SvxListBoxControl::Impl_SetInfo( sal_uInt16 nCount )
203 : {
204 : DBG_ASSERT( pPopupWin, "NULL pointer, PopupWindow missing" );
205 :
206 : // ListBox &rListBox = pPopupWin->GetListBox();
207 :
208 : sal_uInt16 nId;
209 0 : if (nCount == 1)
210 0 : nId = SID_UNDO == GetSlotId() ? RID_SVXSTR_NUM_UNDO_ACTION : RID_SVXSTR_NUM_REDO_ACTION;
211 : else
212 0 : nId = SID_UNDO == GetSlotId() ? RID_SVXSTR_NUM_UNDO_ACTIONS : RID_SVXSTR_NUM_REDO_ACTIONS;
213 :
214 0 : aActionStr = SVX_RESSTR(nId);
215 :
216 0 : OUString aText = aActionStr.replaceAll("$(ARG1)", OUString::number(nCount));
217 0 : pPopupWin->SetText(aText);
218 0 : }
219 :
220 :
221 0 : IMPL_LINK_NOARG(SvxListBoxControl, SelectHdl)
222 : {
223 0 : if (pPopupWin)
224 : {
225 : //pPopupWin->SetUserSelected( false );
226 :
227 0 : ListBox &rListBox = pPopupWin->GetListBox();
228 0 : if (rListBox.IsTravelSelect())
229 0 : Impl_SetInfo( rListBox.GetSelectEntryCount() );
230 : else
231 : {
232 0 : pPopupWin->SetUserSelected( true );
233 0 : pPopupWin->EndPopupMode( 0 );
234 : }
235 : }
236 0 : return 0;
237 : }
238 :
239 :
240 :
241 0 : SFX_IMPL_TOOLBOX_CONTROL( SvxUndoRedoControl, SfxStringItem );
242 :
243 0 : SvxUndoRedoControl::SvxUndoRedoControl( sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rTbx )
244 0 : : SvxListBoxControl( nSlotId, nId, rTbx )
245 : {
246 0 : rTbx.SetItemBits( nId, TIB_DROPDOWN | rTbx.GetItemBits( nId ) );
247 0 : rTbx.Invalidate();
248 0 : aDefaultText = MnemonicGenerator::EraseAllMnemonicChars( rTbx.GetItemText( nId ) );
249 0 : }
250 :
251 0 : SvxUndoRedoControl::~SvxUndoRedoControl()
252 : {
253 0 : }
254 :
255 0 : void SvxUndoRedoControl::StateChanged(
256 : sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem* pState )
257 : {
258 0 : if ( nSID == SID_UNDO || nSID == SID_REDO )
259 : {
260 0 : if ( eState == SFX_ITEM_DISABLED )
261 : {
262 0 : ToolBox& rBox = GetToolBox();
263 0 : rBox.SetQuickHelpText( GetId(), aDefaultText );
264 : }
265 0 : else if ( pState && pState->ISA( SfxStringItem ) )
266 : {
267 0 : SfxStringItem& rItem = *(SfxStringItem *)pState;
268 0 : ToolBox& rBox = GetToolBox();
269 0 : OUString aQuickHelpText = MnemonicGenerator::EraseAllMnemonicChars( rItem.GetValue() );
270 0 : rBox.SetQuickHelpText( GetId(), aQuickHelpText );
271 : }
272 0 : SvxListBoxControl::StateChanged( nSID, eState, pState );
273 : }
274 : else
275 : {
276 0 : aUndoRedoList.clear();
277 :
278 0 : if ( pState && pState->ISA( SfxStringListItem ) )
279 : {
280 0 : SfxStringListItem &rItem = *(SfxStringListItem *)pState;
281 :
282 0 : const std::vector<OUString> &aLst = rItem.GetList();
283 0 : for( long nI = 0, nEnd = aLst.size(); nI < nEnd; ++nI )
284 0 : aUndoRedoList.push_back( aLst[nI] );
285 : }
286 : }
287 0 : }
288 :
289 0 : SfxPopupWindow* SvxUndoRedoControl::CreatePopupWindow()
290 : {
291 : DBG_ASSERT(( SID_UNDO == GetSlotId() || SID_REDO == GetSlotId() ), "mismatching ids" );
292 :
293 0 : if ( m_aCommandURL == ".uno:Undo" )
294 0 : updateStatus( OUString( ".uno:GetUndoStrings" ));
295 : else
296 0 : updateStatus( OUString( ".uno:GetRedoStrings" ));
297 :
298 0 : ToolBox& rBox = GetToolBox();
299 :
300 0 : pPopupWin = new SvxPopupWindowListBox( GetSlotId(), m_aCommandURL, GetId(), rBox );
301 : pPopupWin->SetPopupModeEndHdl( LINK( this, SvxUndoRedoControl,
302 0 : PopupModeEndHdl ) );
303 0 : ListBox &rListBox = pPopupWin->GetListBox();
304 0 : rListBox.SetSelectHdl( LINK( this, SvxUndoRedoControl, SelectHdl ) );
305 :
306 0 : for( sal_uInt32 n = 0; n < aUndoRedoList.size(); n++ )
307 0 : rListBox.InsertEntry( aUndoRedoList[n] );
308 :
309 0 : rListBox.SelectEntryPos( 0 );
310 0 : aActionStr = SVX_RESSTR(SID_UNDO == GetSlotId() ?
311 0 : RID_SVXSTR_NUM_UNDO_ACTIONS : RID_SVXSTR_NUM_REDO_ACTIONS);
312 0 : Impl_SetInfo( rListBox.GetSelectEntryCount() );
313 :
314 : // move focus in floating window without
315 : // closing it (GrabFocus() would close it!)
316 0 : pPopupWin->StartPopupMode( &rBox, FLOATWIN_POPUPMODE_GRABFOCUS );
317 : //pPopupWin->GetListBox().GrabFocus();
318 :
319 0 : return pPopupWin;
320 : }
321 :
322 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|