LibreOffice Module sc (master) 1
namedefdlg.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
10#include <namedefdlg.hxx>
11
13#include <sfx2/app.hxx>
15
16#include <compiler.hxx>
17#include <document.hxx>
18#include <globstr.hrc>
19#include <scresid.hxx>
20#include <globalnames.hxx>
21#include <rangenam.hxx>
22#include <reffact.hxx>
23#include <undorangename.hxx>
24#include <tabvwsh.hxx>
25#include <tokenarray.hxx>
26
28 const ScViewData& rViewData, std::map<OUString, ScRangeName*>&& aRangeMap,
29 const ScAddress& aCursorPos, const bool bUndo )
30 : ScAnyRefDlgController( pB, pCW, pParent, "modules/scalc/ui/definename.ui", "DefineNameDialog")
31 , mbUndo( bUndo )
32 , mrDoc(rViewData.GetDocument())
33 , mpDocShell ( rViewData.GetDocShell() )
34 , maCursorPos( aCursorPos )
35 , maGlobalNameStr ( ScResId(STR_GLOBAL_SCOPE) )
36 , maErrInvalidNameStr( ScResId(STR_ERR_NAME_INVALID))
37 , maErrInvalidNameCellRefStr( ScResId(STR_ERR_NAME_INVALID_CELL_REF))
38 , maErrNameInUse ( ScResId(STR_ERR_NAME_EXISTS))
39 , maRangeMap( std::move(aRangeMap) )
40 , m_xEdName(m_xBuilder->weld_entry("edit"))
41 , m_xEdRange(new formula::RefEdit(m_xBuilder->weld_entry("range")))
42 , m_xRbRange(new formula::RefButton(m_xBuilder->weld_button("refbutton")))
43 , m_xLbScope(m_xBuilder->weld_combo_box("scope"))
44 , m_xBtnRowHeader(m_xBuilder->weld_check_button("rowheader"))
45 , m_xBtnColHeader(m_xBuilder->weld_check_button("colheader"))
46 , m_xBtnPrintArea(m_xBuilder->weld_check_button("printarea"))
47 , m_xBtnCriteria(m_xBuilder->weld_check_button("filter"))
48 , m_xBtnAdd(m_xBuilder->weld_button("add"))
49 , m_xBtnCancel(m_xBuilder->weld_button("cancel"))
50 , m_xFtInfo(m_xBuilder->weld_label("label"))
51 , m_xFtRange(m_xBuilder->weld_label("label3"))
52{
53 m_xEdRange->SetReferences(this, m_xFtRange.get());
54 m_xRbRange->SetReferences(this, m_xEdRange.get());
55 maStrInfoDefault = m_xFtInfo->get_label();
56
57 // Initialize scope list.
58 m_xLbScope->append_text(maGlobalNameStr);
59 m_xLbScope->set_active(0);
61 for (SCTAB i = 0; i < n; ++i)
62 {
63 OUString aTabName;
64 mrDoc.GetName(i, aTabName);
65 m_xLbScope->append_text(aTabName);
66 }
67
68 m_xBtnCancel->connect_clicked( LINK( this, ScNameDefDlg, CancelBtnHdl));
69 m_xBtnAdd->connect_clicked( LINK( this, ScNameDefDlg, AddBtnHdl ));
70 m_xEdName->connect_changed( LINK( this, ScNameDefDlg, NameModifyHdl ));
71 m_xEdRange->SetGetFocusHdl( LINK( this, ScNameDefDlg, AssignGetFocusHdl ) );
72
73 m_xBtnAdd->set_sensitive(false); // empty name is invalid
74
75 ScRange aRange;
76
77 rViewData.GetSimpleArea( aRange );
78 OUString aAreaStr(aRange.Format(mrDoc, ScRefFlags::RANGE_ABS_3D,
80
81 m_xEdRange->SetText( aAreaStr );
82
83 m_xEdName->grab_focus();
84 m_xEdName->select_region(0, -1);
85}
86
88{
89}
90
92{
93 if (mbUndo)
94 response(RET_CANCEL);
95 else
96 {
98 pViewSh->SwitchBetweenRefDialogs(this);
99 }
100}
101
103{
105 std::unique_ptr<ScTokenArray> pCode = aComp.CompileString(m_xEdRange->GetText());
106 if (pCode->GetCodeError() != FormulaError::NONE)
107 {
108 //TODO: info message
109 return false;
110 }
111 else
112 {
113 return true;
114 }
115}
116
118{
119 OUString aScope = m_xLbScope->get_active_text();
120 OUString aName = m_xEdName->get_text();
121
122 bool bIsNameValid = true;
123 OUString aHelpText = maStrInfoDefault;
124
125 ScRangeName* pRangeName = nullptr;
126 if(aScope == maGlobalNameStr)
127 {
128 pRangeName = maRangeMap.find(OUString(STR_GLOBAL_RANGE_NAME))->second;
129 }
130 else
131 {
132 pRangeName = maRangeMap.find(aScope)->second;
133 }
134
136 if ( aName.isEmpty() )
137 {
138 bIsNameValid = false;
139 }
142 {
144 {
145 aHelpText = maErrInvalidNameStr;
146 }
148 {
149 aHelpText = maErrInvalidNameCellRefStr;
150 }
151 bIsNameValid = false;
152 }
153 else if (pRangeName->findByUpperName(ScGlobal::getCharClass().uppercase(aName)))
154 {
155 aHelpText = maErrNameInUse;
156 bIsNameValid = false;
157 }
158
159 if (!IsFormulaValid())
160 {
161 bIsNameValid = false;
162 }
163
164 m_xEdName->set_tooltip_text(aHelpText);
165 m_xEdName->set_message_type(bIsNameValid || aName.isEmpty() ? weld::EntryMessageType::Normal
167 m_xBtnAdd->set_sensitive(bIsNameValid);
168 return bIsNameValid;
169}
170
172{
173 OUString aScope = m_xLbScope->get_active_text();
174 OUString aName = m_xEdName->get_text();
175 OUString aExpression = m_xEdRange->GetText();
176
177 if (aName.isEmpty())
178 {
179 return;
180 }
181 if (aScope.isEmpty())
182 {
183 return;
184 }
185
186 ScRangeName* pRangeName = nullptr;
187 if(aScope == maGlobalNameStr)
188 {
189 pRangeName = maRangeMap.find(OUString(STR_GLOBAL_RANGE_NAME))->second;
190 }
191 else
192 {
193 pRangeName = maRangeMap.find(aScope)->second;
194 }
195 if (!pRangeName)
196 return;
197
198 if (!IsNameValid()) //should not happen, but make sure we don't break anything
199 return;
200 else
201 {
203
204 ScRangeData* pNewEntry = new ScRangeData( mrDoc,
205 aName,
206 aExpression,
208 nType );
209
210 if ( m_xBtnRowHeader->get_active() ) nType |= ScRangeData::Type::RowHeader;
211 if ( m_xBtnColHeader->get_active() ) nType |= ScRangeData::Type::ColHeader;
212 if ( m_xBtnPrintArea->get_active() ) nType |= ScRangeData::Type::PrintArea;
213 if ( m_xBtnCriteria->get_active() ) nType |= ScRangeData::Type::Criteria;
214
215 pNewEntry->AddType(nType);
216
217 // aExpression valid?
218 if ( FormulaError::NONE == pNewEntry->GetErrCode() )
219 {
220 if ( !pRangeName->insert( pNewEntry, false /*bReuseFreeIndex*/ ) )
221 pNewEntry = nullptr;
222
223 if (mbUndo)
224 {
225 // this means we called directly through the menu
226
227 SCTAB nTab;
228 // if no table with that name is found, assume global range name
229 if (!mrDoc.GetTable(aScope, nTab))
230 nTab = -1;
231
232 assert( pNewEntry); // undo of no insertion smells fishy
233 if (pNewEntry)
235 std::make_unique<ScUndoAddRangeData>( mpDocShell, pNewEntry, nTab) );
236
237 // set table stream invalid, otherwise RangeName won't be saved if no other
238 // call invalidates the stream
239 if (nTab != -1)
240 mrDoc.SetStreamValid(nTab, false);
241 SfxGetpApp()->Broadcast( SfxHint( SfxHintId::ScAreasChanged ) );
243 Close();
244 }
245 else
246 {
247 maName = aName;
248 maScope = aScope;
250 pViewSh->SwitchBetweenRefDialogs(this);
251 }
252 }
253 else
254 {
255 delete pNewEntry;
256 m_xEdRange->GrabFocus();
257 m_xEdRange->SelectAll();
258 }
259 }
260}
261
262void ScNameDefDlg::GetNewData(OUString& rName, OUString& rScope)
263{
264 rName = maName;
265 rScope = maScope;
266}
267
269{
270 return m_xEdRange->GetWidget()->get_sensitive();
271}
272
273void ScNameDefDlg::RefInputDone( bool bForced)
274{
276 IsNameValid();
277}
278
280{
281 if (m_xEdRange->GetWidget()->get_sensitive())
282 {
283 if ( rRef.aStart != rRef.aEnd )
285 OUString aRefStr(rRef.Format(rDocP, ScRefFlags::RANGE_ABS_3D,
287 m_xEdRange->SetRefString( aRefStr );
288 }
289}
290
292{
293 DoClose( ScNameDefDlgWrapper::GetChildWindowId() );
294}
295
297{
298 m_xEdRange->GrabFocus();
299 RefInputDone();
300}
301
303{
304 CancelPushed();
305}
306
308{
309 AddPushed();
310};
311
313{
314 IsNameValid();
315}
316
318{
319 IsNameValid();
320}
321
322/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SfxApplication * SfxGetpApp()
OUString uppercase(const OUString &rStr, sal_Int32 nPos, sal_Int32 nCount) const
std::unique_ptr< ScTokenArray > CompileString(const OUString &rFormula)
Tokenize formula expression string into an array of tokens.
Definition: compiler.cxx:4692
void SetDocumentModified()
Definition: docsh.cxx:2959
virtual SfxUndoManager * GetUndoManager() override
Definition: docsh.cxx:2945
SC_DLLPUBLIC bool GetTable(const OUString &rName, SCTAB &rTab) const
Definition: document.cxx:264
SC_DLLPUBLIC formula::FormulaGrammar::AddressConvention GetAddressConvention() const
Definition: documen3.cxx:500
void SetStreamValid(SCTAB nTab, bool bSet, bool bIgnoreLock=false)
Definition: document.cxx:937
SC_DLLPUBLIC formula::FormulaGrammar::Grammar GetGrammar() const
Definition: document.hxx:1008
SC_DLLPUBLIC bool GetName(SCTAB nTab, OUString &rName) const
Definition: document.cxx:217
SC_DLLPUBLIC SCTAB GetTableCount() const
Definition: document.cxx:317
static SC_DLLPUBLIC const CharClass & getCharClass()
Definition: global.cxx:1062
OUString maStrInfoDefault
Definition: namedefdlg.hxx:29
ScNameDefDlg(SfxBindings *pB, SfxChildWindow *pCW, weld::Window *pParent, const ScViewData &rViewData, std::map< OUString, ScRangeName * > &&aRangeMap, const ScAddress &aCursorPos, const bool bUndo)
Definition: namedefdlg.cxx:27
std::unique_ptr< weld::Button > m_xBtnCancel
Definition: namedefdlg.hxx:54
const OUString maErrNameInUse
Definition: namedefdlg.hxx:33
OUString maName
Definition: namedefdlg.hxx:36
virtual void SetReference(const ScRange &rRef, ScDocument &rDoc) override
Definition: namedefdlg.cxx:279
ScDocShell * mpDocShell
Definition: namedefdlg.hxx:26
bool IsFormulaValid()
Definition: namedefdlg.cxx:102
const OUString maErrInvalidNameCellRefStr
Definition: namedefdlg.hxx:32
ScDocument & mrDoc
Definition: namedefdlg.hxx:25
std::unique_ptr< formula::RefEdit > m_xEdRange
Definition: namedefdlg.hxx:43
std::unique_ptr< weld::CheckButton > m_xBtnRowHeader
Definition: namedefdlg.hxx:48
std::unique_ptr< weld::ComboBox > m_xLbScope
Definition: namedefdlg.hxx:46
bool IsNameValid()
Definition: namedefdlg.cxx:117
OUString maScope
Definition: namedefdlg.hxx:37
std::unique_ptr< weld::CheckButton > m_xBtnColHeader
Definition: namedefdlg.hxx:49
virtual void Close() override
Definition: namedefdlg.cxx:291
virtual ~ScNameDefDlg() override
Definition: namedefdlg.cxx:87
const OUString maGlobalNameStr
Definition: namedefdlg.hxx:30
ScAddress maCursorPos
Definition: namedefdlg.hxx:28
virtual bool IsRefInputMode() const override
Definition: namedefdlg.cxx:268
virtual void SetActive() override
Definition: namedefdlg.cxx:296
std::unique_ptr< weld::Label > m_xFtRange
Definition: namedefdlg.hxx:56
std::unique_ptr< formula::RefButton > m_xRbRange
Definition: namedefdlg.hxx:44
std::unique_ptr< weld::CheckButton > m_xBtnPrintArea
Definition: namedefdlg.hxx:50
virtual void RefInputDone(bool bForced=false) override
Definition: namedefdlg.cxx:273
std::map< OUString, ScRangeName * > maRangeMap
Definition: namedefdlg.hxx:39
std::unique_ptr< weld::Button > m_xBtnAdd
Definition: namedefdlg.hxx:53
void GetNewData(OUString &rName, OUString &rScope)
Definition: namedefdlg.cxx:262
void AddPushed()
Definition: namedefdlg.cxx:171
const OUString maErrInvalidNameStr
Definition: namedefdlg.hxx:31
std::unique_ptr< weld::Entry > m_xEdName
Definition: namedefdlg.hxx:41
std::unique_ptr< weld::CheckButton > m_xBtnCriteria
Definition: namedefdlg.hxx:51
void CancelPushed()
Definition: namedefdlg.cxx:91
std::unique_ptr< weld::Label > m_xFtInfo
Definition: namedefdlg.hxx:55
SC_DLLPUBLIC FormulaError GetErrCode() const
Definition: rangenam.cxx:515
void AddType(Type nType)
Definition: rangenam.hxx:173
static SC_DLLPUBLIC IsNameValidType IsNameValid(const OUString &rName, const ScDocument &rDoc)
Definition: rangenam.cxx:468
SC_DLLPUBLIC ScRangeData * findByUpperName(const OUString &rName)
Definition: rangenam.cxx:704
SC_DLLPUBLIC bool insert(ScRangeData *p, bool bReuseFreeIndex=true)
Insert object into set.
Definition: rangenam.cxx:802
OUString Format(const ScDocument &rDocument, ScRefFlags nFlags=ScRefFlags::ZERO, const ScAddress::Details &rDetails=ScAddress::detailsOOOa1, bool bFullAddressNotation=false) const
Returns string with formatted cell range from aStart to aEnd, according to provided address conventio...
Definition: address.cxx:2170
ScAddress aEnd
Definition: address.hxx:498
ScAddress aStart
Definition: address.hxx:497
virtual void RefInputStart(formula::RefEdit *pEdit, formula::RefButton *pButton=nullptr) override
Definition: anyrefdg.cxx:749
virtual void RefInputDone(bool bForced=false) override
Definition: anyrefdg.cxx:779
bool DoClose(sal_uInt16 nId)
Definition: anyrefdg.cxx:718
void SwitchBetweenRefDialogs(SfxModelessDialogController *pDialog)
Definition: tabvwshc.cxx:87
static ScTabViewShell * GetActiveViewShell()
Definition: tabvwsh4.cxx:1076
ScMarkType GetSimpleArea(SCCOL &rStartCol, SCROW &rStartRow, SCTAB &rStartTab, SCCOL &rEndCol, SCROW &rEndRow, SCTAB &rEndTab) const
Definition: viewdata.cxx:1181
virtual void AddUndoAction(std::unique_ptr< SfxUndoAction > pAction, bool bTryMerg=false)
DocumentType eType
constexpr OUStringLiteral STR_GLOBAL_RANGE_NAME
Definition: globalnames.hxx:17
OUString aName
sal_Int64 n
IMPL_LINK_NOARG(ScNameDefDlg, CancelBtnHdl, weld::Button &, void)
Definition: namedefdlg.cxx:302
int i
QPRO_FUNC_TYPE nType
Definition: qproform.cxx:398
OUString ScResId(TranslateId aId)
Definition: scdll.cxx:90
sal_Int16 SCTAB
Definition: types.hxx:22
RET_CANCEL