Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / sc / source / ui / navipi / scenwnd.cxx
bloba91d3f4ad2b35e88d4c53a31d1ed91b207dbf512
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 <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>
28 #include "navipi.hxx"
29 #include "popmenu.hxx"
30 #include "scresid.hxx"
31 #include "sc.hrc"
32 #include "globstr.hrc"
34 // class ScScenarioWindow ------------------------------------------------
36 ScScenarioListBox::ScScenarioListBox( ScScenarioWindow& rParent ) :
37 ListBox( &rParent, WB_BORDER | WB_TABSTOP ),
38 mrParent( rParent )
40 vcl::Font aFont( GetFont() );
41 aFont.SetTransparent( true );
42 aFont.SetWeight( WEIGHT_LIGHT );
43 SetFont( aFont );
46 ScScenarioListBox::~ScScenarioListBox()
50 void ScScenarioListBox::UpdateEntries( const std::vector<OUString> &aNewEntryList )
52 Clear();
53 maEntries.clear();
55 switch( aNewEntryList.size() )
57 case 0:
58 // no scenarios in current sheet
59 mrParent.SetComment( EMPTY_OUSTRING );
60 break;
62 case 1:
63 // sheet is a scenario container, comment only
64 mrParent.SetComment( aNewEntryList[0] );
65 break;
67 default:
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)
76 ScenarioEntry aEntry;
78 // first entry of a triple is the scenario name
79 aEntry.maName = *iter;
81 // second entry of a triple is the scenario comment
82 ++iter;
83 aEntry.maComment = *iter;
85 // third entry of a triple is the protection ("0" = not protected, "1" = protected)
86 ++iter;
87 aEntry.mbProtected = !(*iter).isEmpty() && (*iter)[0] != '0';
89 maEntries.push_back( aEntry );
90 InsertEntry( aEntry.maName );
92 SetUpdateMode( true );
93 SetNoSelection();
94 mrParent.SetComment( EMPTY_OUSTRING );
99 void ScScenarioListBox::Select()
101 if( const ScenarioEntry* pEntry = GetSelectedEntry() )
102 mrParent.SetComment( pEntry->maComment );
105 void ScScenarioListBox::DoubleClick()
107 SelectScenario();
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() )
119 case KEY_RETURN:
120 SelectScenario();
121 bHandled = true;
122 break;
123 case KEY_DELETE:
124 DeleteScenario();
125 bHandled = true;
126 break;
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() );
140 if (aPopup.WasHit())
142 switch( aPopup.GetSelected() )
144 case RID_NAVIPI_SCENARIO_DELETE:
145 DeleteScenario();
146 break;
147 case RID_NAVIPI_SCENARIO_EDIT:
148 EditScenario();
149 break;
154 bHandled = true;
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 ] : nullptr;
167 void ScScenarioListBox::ExecuteScenarioSlot( sal_uInt16 nSlotId )
169 if( SfxViewFrame* pViewFrm = SfxViewFrame::Current() )
171 SfxStringItem aStringItem( nSlotId, GetSelectEntry() );
172 pViewFrm->GetDispatcher()->ExecuteList(nSlotId,
173 SfxCallMode::SLOT | SfxCallMode::RECORD, { &aStringItem } );
177 void ScScenarioListBox::SelectScenario()
179 if( GetSelectEntryCount() > 0 )
180 ExecuteScenarioSlot( SID_SELECT_SCENARIO );
183 void ScScenarioListBox::EditScenario()
185 if( GetSelectEntryCount() > 0 )
186 ExecuteScenarioSlot( SID_EDIT_SCENARIO );
189 void ScScenarioListBox::DeleteScenario()
191 if( GetSelectEntryCount() > 0 )
192 if( ScopedVclPtrInstance<QueryBox>( nullptr, WinBits( WB_YES_NO | WB_DEF_YES ), ScGlobal::GetRscString( STR_QUERY_DELSCENARIO ) )->Execute() == RET_YES )
193 ExecuteScenarioSlot( SID_DELETE_SCENARIO );
196 // class ScScenarioWindow ------------------------------------------------
198 ScScenarioWindow::ScScenarioWindow( vcl::Window* pParent, const OUString& aQH_List,
199 const OUString& aQH_Comment)
200 : Window ( pParent, WB_TABSTOP | WB_DIALOGCONTROL ),
201 aLbScenario ( VclPtr<ScScenarioListBox>::Create(*this) ),
202 aEdComment ( VclPtr<MultiLineEdit>::Create(this, WB_BORDER | WB_LEFT | WB_READONLY | WB_VSCROLL | WB_TABSTOP) )
204 vcl::Font aFont( GetFont() );
205 aFont.SetTransparent( true );
206 aFont.SetWeight( WEIGHT_LIGHT );
207 aEdComment->SetFont( aFont );
208 aEdComment->SetMaxTextLen( 512 );
209 aLbScenario->SetPosPixel( Point(0,0) );
210 aLbScenario->SetHelpId(HID_SC_SCENWIN_TOP);
211 aEdComment->SetHelpId(HID_SC_SCENWIN_BOTTOM);
212 aLbScenario->Show();
213 aEdComment->Show();
215 aLbScenario->SetQuickHelpText(aQH_List);
216 aEdComment->SetQuickHelpText(aQH_Comment);
217 aEdComment->SetBackground( Color( COL_LIGHTGRAY ) );
219 SfxViewFrame* pViewFrm = SfxViewFrame::Current();
220 if (pViewFrm)
222 SfxBindings& rBindings = pViewFrm->GetBindings();
223 rBindings.Invalidate( SID_SELECT_SCENARIO );
224 rBindings.Update( SID_SELECT_SCENARIO );
228 ScScenarioWindow::~ScScenarioWindow()
230 disposeOnce();
233 void ScScenarioWindow::dispose()
235 aLbScenario.disposeAndClear();
236 aEdComment.disposeAndClear();
237 vcl::Window::dispose();
240 void ScScenarioWindow::Paint(vcl::RenderContext& rRenderContext, const Rectangle& rRect)
242 const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
243 Color aBgColor = rStyleSettings.GetFaceColor();
245 SetBackground( aBgColor );
247 Window::Paint(rRenderContext, rRect);
250 void ScScenarioWindow::NotifyState( const SfxPoolItem* pState )
252 if( pState )
254 aLbScenario->Enable();
256 if ( dynamic_cast<const SfxStringItem*>( pState) != nullptr )
258 OUString aNewEntry( static_cast<const SfxStringItem*>(pState)->GetValue() );
260 if ( !aNewEntry.isEmpty() )
261 aLbScenario->SelectEntry( aNewEntry );
262 else
263 aLbScenario->SetNoSelection();
265 else if ( dynamic_cast<const SfxStringListItem*>( pState) != nullptr )
267 aLbScenario->UpdateEntries( static_cast<const SfxStringListItem*>(pState)->GetList() );
270 else
272 aLbScenario->Disable();
273 aLbScenario->SetNoSelection();
277 void ScScenarioWindow::SetSizePixel( const Size& rNewSize )
279 Size aSize( rNewSize );
280 long nHeight = aSize.Height() / 2;
282 Window::SetSizePixel( aSize );
284 aSize.Height() = nHeight;
285 aLbScenario->SetSizePixel( aSize );
287 aSize.Height() -= 4;
288 aEdComment->SetPosSizePixel( Point( 0, nHeight+4 ), aSize );
291 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */