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