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