1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <sfx2/bindings.hxx>
21 #include <sfx2/dispatch.hxx>
22 #include <sfx2/viewfrm.hxx>
23 #include <svl/slstitm.hxx>
24 #include <svl/stritem.hxx>
25 #include <vcl/msgbox.hxx>
26 #include <vcl/svapp.hxx>
27 #include <vcl/settings.hxx>
29 #include "popmenu.hxx"
30 #include "scresid.hxx"
32 #include "globstr.hrc"
34 // class ScScenarioWindow ------------------------------------------------
36 ScScenarioListBox::ScScenarioListBox( ScScenarioWindow
& rParent
) :
37 ListBox( &rParent
, WB_BORDER
| WB_TABSTOP
),
40 vcl::Font
aFont( GetFont() );
41 aFont
.SetTransparent( true );
42 aFont
.SetWeight( WEIGHT_LIGHT
);
46 ScScenarioListBox::~ScScenarioListBox()
50 void ScScenarioListBox::UpdateEntries( const std::vector
<OUString
> &aNewEntryList
)
55 switch( aNewEntryList
.size() )
58 // no scenarios in current sheet
59 mrParent
.SetComment( EMPTY_OUSTRING
);
63 // sheet is a scenario container, comment only
64 mrParent
.SetComment( aNewEntryList
[0] );
69 // sheet contains scenarios
70 OSL_ENSURE( aNewEntryList
.size() % 3 == 0, "ScScenarioListBox::UpdateEntries - wrong list size" );
71 SetUpdateMode( false );
73 std::vector
<OUString
>::const_iterator iter
;
74 for (iter
= aNewEntryList
.begin(); iter
!= aNewEntryList
.end(); ++iter
)
78 // first entry of a triple is the scenario name
79 aEntry
.maName
= *iter
;
81 // second entry of a triple is the scenario comment
83 aEntry
.maComment
= *iter
;
85 // third entry of a triple is the protection ("0" = not protected, "1" = protected)
87 aEntry
.mbProtected
= !(*iter
).isEmpty() && (*iter
)[0] != '0';
89 maEntries
.push_back( aEntry
);
90 InsertEntry( aEntry
.maName
, LISTBOX_APPEND
);
92 SetUpdateMode( true );
94 mrParent
.SetComment( EMPTY_OUSTRING
);
99 void ScScenarioListBox::Select()
101 if( const ScenarioEntry
* pEntry
= GetSelectedEntry() )
102 mrParent
.SetComment( pEntry
->maComment
);
105 void ScScenarioListBox::DoubleClick()
110 bool ScScenarioListBox::Notify( NotifyEvent
& rNEvt
)
112 bool bHandled
= false;
114 if( rNEvt
.GetType() == MouseNotifyEvent::KEYINPUT
)
116 vcl::KeyCode aCode
= rNEvt
.GetKeyEvent()->GetKeyCode();
117 switch( aCode
.GetCode() )
124 DeleteScenario( true );
129 else if ( rNEvt
.GetType() == MouseNotifyEvent::COMMAND
&& GetSelectEntryCount() )
131 const CommandEvent
* pCEvt
= rNEvt
.GetCommandEvent();
132 if ( pCEvt
&& pCEvt
->GetCommand() == CommandEventId::ContextMenu
)
134 if( const ScenarioEntry
* pEntry
= GetSelectedEntry() )
136 if( !pEntry
->mbProtected
)
138 ScPopupMenu
aPopup( ScResId( RID_POPUP_NAVIPI_SCENARIO
) );
139 aPopup
.Execute( this, pCEvt
->GetMousePosPixel() );
142 switch( aPopup
.GetSelected() )
144 case RID_NAVIPI_SCENARIO_DELETE
:
145 DeleteScenario( true );
147 case RID_NAVIPI_SCENARIO_EDIT
:
158 return bHandled
|| ListBox::Notify( rNEvt
);
161 const ScScenarioListBox::ScenarioEntry
* ScScenarioListBox::GetSelectedEntry() const
163 size_t nPos
= GetSelectEntryPos();
164 return (nPos
< maEntries
.size()) ? &maEntries
[ nPos
] : 0;
167 void ScScenarioListBox::ExecuteScenarioSlot( sal_uInt16 nSlotId
)
169 if( SfxViewFrame
* pViewFrm
= SfxViewFrame::Current() )
171 SfxStringItem
aStringItem( nSlotId
, GetSelectEntry() );
172 pViewFrm
->GetDispatcher()->Execute( nSlotId
, SfxCallMode::SLOT
| SfxCallMode::RECORD
, &aStringItem
, 0L, 0L );
176 void ScScenarioListBox::SelectScenario()
178 if( GetSelectEntryCount() > 0 )
179 ExecuteScenarioSlot( SID_SELECT_SCENARIO
);
182 void ScScenarioListBox::EditScenario()
184 if( GetSelectEntryCount() > 0 )
185 ExecuteScenarioSlot( SID_EDIT_SCENARIO
);
188 void ScScenarioListBox::DeleteScenario( bool bQueryBox
)
190 if( GetSelectEntryCount() > 0 )
191 if( !bQueryBox
|| (ScopedVclPtr
<QueryBox
>::Create( nullptr, WinBits( WB_YES_NO
| WB_DEF_YES
), ScGlobal::GetRscString( STR_QUERY_DELSCENARIO
) )->Execute() == RET_YES
) )
192 ExecuteScenarioSlot( SID_DELETE_SCENARIO
);
195 // class ScScenarioWindow ------------------------------------------------
197 ScScenarioWindow::ScScenarioWindow( vcl::Window
* pParent
, const OUString
& aQH_List
,
198 const OUString
& aQH_Comment
)
199 : Window ( pParent
, WB_TABSTOP
| WB_DIALOGCONTROL
),
200 aLbScenario ( VclPtr
<ScScenarioListBox
>::Create(*this) ),
201 aEdComment ( VclPtr
<MultiLineEdit
>::Create(this, WB_BORDER
| WB_LEFT
| WB_READONLY
| WB_VSCROLL
| WB_TABSTOP
) )
203 vcl::Font
aFont( GetFont() );
204 aFont
.SetTransparent( true );
205 aFont
.SetWeight( WEIGHT_LIGHT
);
206 aEdComment
->SetFont( aFont
);
207 aEdComment
->SetMaxTextLen( 512 );
208 aLbScenario
->SetPosPixel( Point(0,0) );
209 aLbScenario
->SetHelpId(HID_SC_SCENWIN_TOP
);
210 aEdComment
->SetHelpId(HID_SC_SCENWIN_BOTTOM
);
214 aLbScenario
->SetQuickHelpText(aQH_List
);
215 aEdComment
->SetQuickHelpText(aQH_Comment
);
216 aEdComment
->SetBackground( Color( COL_LIGHTGRAY
) );
218 SfxViewFrame
* pViewFrm
= SfxViewFrame::Current();
221 SfxBindings
& rBindings
= pViewFrm
->GetBindings();
222 rBindings
.Invalidate( SID_SELECT_SCENARIO
);
223 rBindings
.Update( SID_SELECT_SCENARIO
);
227 ScScenarioWindow::~ScScenarioWindow()
232 void ScScenarioWindow::dispose()
234 aLbScenario
.disposeAndClear();
235 aEdComment
.disposeAndClear();
236 vcl::Window::dispose();
239 void ScScenarioWindow::Paint(vcl::RenderContext
& rRenderContext
, const Rectangle
& rRect
)
241 const StyleSettings
& rStyleSettings
= Application::GetSettings().GetStyleSettings();
242 Color aBgColor
= rStyleSettings
.GetFaceColor();
244 SetBackground( aBgColor
);
246 Window::Paint(rRenderContext
, rRect
);
249 void ScScenarioWindow::NotifyState( const SfxPoolItem
* pState
)
253 aLbScenario
->Enable();
255 if ( pState
->ISA(SfxStringItem
) )
257 OUString
aNewEntry( static_cast<const SfxStringItem
*>(pState
)->GetValue() );
259 if ( !aNewEntry
.isEmpty() )
260 aLbScenario
->SelectEntry( aNewEntry
);
262 aLbScenario
->SetNoSelection();
264 else if ( pState
->ISA(SfxStringListItem
) )
266 aLbScenario
->UpdateEntries( static_cast<const SfxStringListItem
*>(pState
)->GetList() );
271 aLbScenario
->Disable();
272 aLbScenario
->SetNoSelection();
276 void ScScenarioWindow::SetSizePixel( const Size
& rNewSize
)
278 Size
aSize( rNewSize
);
279 long nHeight
= aSize
.Height() / 2;
281 Window::SetSizePixel( aSize
);
283 aSize
.Height() = nHeight
;
284 aLbScenario
->SetSizePixel( aSize
);
287 aEdComment
->SetPosSizePixel( Point( 0, nHeight
+4 ), aSize
);
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */