LibreOffice Module sc (master) 1
scenwnd.cxx
Go to the documentation of this file.
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 <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/commandevent.hxx>
26#include <vcl/event.hxx>
27#include <vcl/svapp.hxx>
28#include <vcl/weld.hxx>
29#include <navipi.hxx>
30#include <sc.hrc>
31#include <globstr.hrc>
32#include <scresid.hxx>
33#include <helpids.h>
34
35// class ScScenarioWindow ------------------------------------------------
36
37void ScScenarioWindow::UpdateEntries( const std::vector<OUString> &rNewEntryList )
38{
39 m_xLbScenario->clear();
40 m_aEntries.clear();
41
42 switch( rNewEntryList.size() )
43 {
44 case 0:
45 // no scenarios in current sheet
46 SetComment( OUString() );
47 break;
48
49 case 1:
50 // sheet is a scenario container, comment only
51 SetComment( rNewEntryList[0] );
52 break;
53
54 default:
55 {
56 // sheet contains scenarios
57 assert(rNewEntryList.size() % 3 == 0 && "ScScenarioListBox::UpdateEntries - wrong list size");
58 m_xLbScenario->freeze();
59
60 std::vector<OUString>::const_iterator iter;
61 for (iter = rNewEntryList.begin(); iter != rNewEntryList.end(); ++iter)
62 {
63 ScenarioEntry aEntry;
64
65 // first entry of a triple is the scenario name
66 aEntry.maName = *iter;
67
68 // second entry of a triple is the scenario comment
69 ++iter;
70 aEntry.maComment = *iter;
71
72 // third entry of a triple is the protection ("0" = not protected, "1" = protected)
73 ++iter;
74 aEntry.mbProtected = !(*iter).isEmpty() && (*iter)[0] != '0';
75
76 m_aEntries.push_back( aEntry );
77 m_xLbScenario->append_text(aEntry.maName);
78 }
79 m_xLbScenario->thaw();
80 m_xLbScenario->unselect_all();
81 SetComment(OUString());
82 }
83 }
84}
85
87{
88 if (const ScenarioEntry* pEntry = GetSelectedScenarioEntry())
89 SetComment(pEntry->maComment);
90}
91
93{
94 SelectScenario();
95 return true;
96}
97
98IMPL_LINK(ScScenarioWindow, KeyInputHdl, const KeyEvent&, rKEvt, bool)
99{
100 bool bHandled = false;
101
102 vcl::KeyCode aCode = rKEvt.GetKeyCode();
103 switch( aCode.GetCode() )
104 {
105 case KEY_RETURN:
106 SelectScenario();
107 bHandled = true;
108 break;
109 case KEY_DELETE:
110 DeleteScenario();
111 bHandled = true;
112 break;
113 }
114
115 return bHandled;
116}
117
118IMPL_LINK(ScScenarioWindow, ContextMenuHdl, const CommandEvent&, rCEvt, bool)
119{
120 bool bHandled = false;
121
122 if (rCEvt.GetCommand() == CommandEventId::ContextMenu)
123 {
124 if (const ScenarioEntry* pEntry = GetSelectedScenarioEntry())
125 {
126 if (!pEntry->mbProtected)
127 {
128 std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(m_xLbScenario.get(), "modules/scalc/ui/scenariomenu.ui"));
129 std::unique_ptr<weld::Menu> xPopup(xBuilder->weld_menu("menu"));
130 OUString sIdent(xPopup->popup_at_rect(m_xLbScenario.get(), tools::Rectangle(rCEvt.GetMousePosPixel(), Size(1,1))));
131 if (sIdent == "delete")
132 DeleteScenario();
133 else if (sIdent == "edit")
134 EditScenario();
135 }
136 }
137 bHandled = true;
138 }
139
140 return bHandled;
141}
142
144{
145 size_t nPos = m_xLbScenario->get_selected_index();
146 return (nPos < m_aEntries.size()) ? &m_aEntries[ nPos ] : nullptr;
147}
148
150{
151 if( SfxViewFrame* pViewFrm = SfxViewFrame::Current() )
152 {
153 SfxStringItem aStringItem(nSlotId, m_xLbScenario->get_selected_text());
154 pViewFrm->GetDispatcher()->ExecuteList(nSlotId,
155 SfxCallMode::SLOT | SfxCallMode::RECORD, { &aStringItem } );
156 }
157}
158
160{
161 if (m_xLbScenario->get_selected_index() != -1)
162 ExecuteScenarioSlot(SID_SELECT_SCENARIO);
163}
164
166{
167 if (m_xLbScenario->get_selected_index() != -1)
168 ExecuteScenarioSlot(SID_EDIT_SCENARIO);
169}
170
172{
173 if (m_xLbScenario->get_selected_index() != -1)
174 {
175 std::unique_ptr<weld::MessageDialog> xQueryBox(Application::CreateMessageDialog(m_xLbScenario.get(),
176 VclMessageType::Question, VclButtonsType::YesNo,
177 ScResId(STR_QUERY_DELSCENARIO)));
178 xQueryBox->set_default_response(RET_YES);
179 if (xQueryBox->run() == RET_YES)
180 ExecuteScenarioSlot(SID_DELETE_SCENARIO);
181 }
182}
183
184// class ScScenarioWindow ------------------------------------------------
185
186ScScenarioWindow::ScScenarioWindow(weld::Builder& rBuilder, const OUString& aQH_List,
187 const OUString& aQH_Comment)
188 : m_xLbScenario(rBuilder.weld_tree_view("scenariolist"))
189 , m_xEdComment(rBuilder.weld_text_view("scenariotext"))
190{
191 m_xLbScenario->set_help_id(HID_SC_SCENWIN_TOP);
193
194 m_xLbScenario->set_tooltip_text(aQH_List);
195 m_xEdComment->set_tooltip_text(aQH_Comment);
196
197 m_xLbScenario->connect_changed(LINK(this, ScScenarioWindow, SelectHdl));
198 m_xLbScenario->connect_row_activated(LINK(this, ScScenarioWindow, DoubleClickHdl));
199 m_xLbScenario->connect_key_press(LINK(this, ScScenarioWindow, KeyInputHdl));
200 m_xLbScenario->connect_popup_menu(LINK(this, ScScenarioWindow, ContextMenuHdl));
201
203 if (pViewFrm)
204 {
205 SfxBindings& rBindings = pViewFrm->GetBindings();
206 rBindings.Invalidate( SID_SELECT_SCENARIO );
207 rBindings.Update( SID_SELECT_SCENARIO );
208 }
209}
210
212{
213}
214
216{
217 if( pState )
218 {
219 m_xLbScenario->set_sensitive(true);
220
221 if ( auto pStringItem = dynamic_cast<const SfxStringItem*>( pState) )
222 {
223 const OUString& aNewEntry( pStringItem->GetValue() );
224
225 if (!aNewEntry.isEmpty())
226 m_xLbScenario->select_text(aNewEntry);
227 else
228 m_xLbScenario->unselect_all();
229 }
230 else if ( auto pStringListItem = dynamic_cast<const SfxStringListItem*>( pState) )
231 {
232 UpdateEntries(pStringListItem->GetList());
233 }
234 }
235 else
236 {
237 m_xLbScenario->set_sensitive(false);
238 m_xLbScenario->unselect_all();
239 }
240}
241
242/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static std::unique_ptr< weld::Builder > CreateBuilder(weld::Widget *pParent, const OUString &rUIFile, bool bMobile=false, sal_uInt64 nLOKWindowId=0)
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, const ILibreOfficeKitNotifier *pNotifier=nullptr)
std::unique_ptr< weld::TreeView > m_xLbScenario
Definition: navipi.hxx:59
void EditScenario()
Definition: scenwnd.cxx:165
void ExecuteScenarioSlot(sal_uInt16 nSlotId)
Definition: scenwnd.cxx:149
void NotifyState(const SfxPoolItem *pState)
Definition: scenwnd.cxx:215
const ScenarioEntry * GetSelectedScenarioEntry() const
Definition: scenwnd.cxx:143
void SetComment(const OUString &rComment)
Definition: navipi.hxx:53
std::unique_ptr< weld::TextView > m_xEdComment
Definition: navipi.hxx:60
ScScenarioWindow(weld::Builder &rBuilder, const OUString &rQH_List, const OUString &rQH_Comment)
Definition: scenwnd.cxx:186
std::vector< ScenarioEntry > m_aEntries
Definition: navipi.hxx:71
void SelectScenario()
Definition: scenwnd.cxx:159
void UpdateEntries(const std::vector< OUString > &rNewEntryList)
Definition: scenwnd.cxx:37
void DeleteScenario()
Definition: scenwnd.cxx:171
void Update(sal_uInt16 nId)
void Invalidate(sal_uInt16 nId)
static SAL_WARN_UNUSED_RESULT SfxViewFrame * Current()
SfxBindings & GetBindings()
sal_uInt16 GetCode() const
constexpr OUStringLiteral HID_SC_SCENWIN_TOP
Definition: helpids.h:39
constexpr OUStringLiteral HID_SC_SCENWIN_BOTTOM
Definition: helpids.h:40
constexpr sal_uInt16 KEY_RETURN
constexpr sal_uInt16 KEY_DELETE
sal_uInt16 nPos
OUString ScResId(TranslateId aId)
Definition: scdll.cxx:90
IMPL_LINK(ScScenarioWindow, KeyInputHdl, const KeyEvent &, rKEvt, bool)
Definition: scenwnd.cxx:98
IMPL_LINK_NOARG(ScScenarioWindow, SelectHdl, weld::TreeView &, void)
Definition: scenwnd.cxx:86
RET_YES