1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include "namedefdlg.hxx"
12 #include <vcl/msgbox.hxx>
13 #include <vcl/settings.hxx>
15 #include <sfx2/app.hxx>
17 #include "document.hxx"
18 #include "globstr.hrc"
19 #include "globalnames.hxx"
20 #include "rangenam.hxx"
21 #include "reffact.hxx"
22 #include "undorangename.hxx"
23 #include "tabvwsh.hxx"
24 #include "tokenarray.hxx"
27 // defines -------------------------------------------------------------------
29 #define ABS_SREF SCA_VALID \
30 | SCA_COL_ABSOLUTE | SCA_ROW_ABSOLUTE | SCA_TAB_ABSOLUTE
31 #define ABS_DREF ABS_SREF \
32 | SCA_COL2_ABSOLUTE | SCA_ROW2_ABSOLUTE | SCA_TAB2_ABSOLUTE
33 #define ABS_DREF3D ABS_DREF | SCA_TAB_3D
35 ScNameDefDlg::ScNameDefDlg( SfxBindings
* pB
, SfxChildWindow
* pCW
, vcl::Window
* pParent
,
36 ScViewData
* pViewData
, const std::map
<OUString
, ScRangeName
*>& aRangeMap
,
37 const ScAddress
& aCursorPos
, const bool bUndo
)
38 : ScAnyRefDlg( pB
, pCW
, pParent
, "DefineNameDialog", "modules/scalc/ui/definename.ui" )
41 mpDoc( pViewData
->GetDocument() ),
42 mpDocShell ( pViewData
->GetDocShell() ),
43 maCursorPos( aCursorPos
),
45 maGlobalNameStr ( ScGlobal::GetRscString(STR_GLOBAL_SCOPE
) ),
46 maErrInvalidNameStr( ScGlobal::GetRscString(STR_ERR_NAME_INVALID
)),
47 maErrNameInUse ( ScGlobal::GetRscString(STR_ERR_NAME_EXISTS
)),
48 maRangeMap( aRangeMap
)
50 get(m_pEdName
, "edit");
51 get(m_pEdRange
, "range");
52 m_pEdRange
->SetReferences(this, m_pEdName
);
53 get(m_pRbRange
, "refbutton");
54 m_pRbRange
->SetReferences(this, m_pEdRange
);
55 get(m_pLbScope
, "scope");
56 get(m_pBtnRowHeader
, "rowheader");
57 get(m_pBtnColHeader
, "colheader");
58 get(m_pBtnPrintArea
, "printarea");
59 get(m_pBtnCriteria
, "filter");
60 get(m_pBtnAdd
, "add");
61 get(m_pBtnCancel
, "cancel");
62 get(m_pFtInfo
, "label");
63 maStrInfoDefault
= m_pFtInfo
->GetText();
65 // Initialize scope list.
66 m_pLbScope
->InsertEntry(maGlobalNameStr
);
67 m_pLbScope
->SelectEntryPos(0);
68 SCTAB n
= mpDoc
->GetTableCount();
69 for (SCTAB i
= 0; i
< n
; ++i
)
72 mpDoc
->GetName(i
, aTabName
);
73 m_pLbScope
->InsertEntry(aTabName
);
76 m_pBtnCancel
->SetClickHdl( LINK( this, ScNameDefDlg
, CancelBtnHdl
));
77 m_pBtnAdd
->SetClickHdl( LINK( this, ScNameDefDlg
, AddBtnHdl
));
78 m_pEdName
->SetModifyHdl( LINK( this, ScNameDefDlg
, NameModifyHdl
));
79 m_pEdRange
->SetGetFocusHdl( LINK( this, ScNameDefDlg
, AssignGetFocusHdl
) );
81 m_pBtnAdd
->Disable(); // empty name is invalid
85 pViewData
->GetSimpleArea( aRange
);
86 OUString
aAreaStr(aRange
.Format(ABS_DREF3D
, mpDoc
,
87 ScAddress::Details(mpDoc
->GetAddressConvention(), 0, 0)));
89 m_pEdRange
->SetText( aAreaStr
);
91 Selection aCurSel
= Selection( 0, SELECTION_MAX
);
92 m_pEdName
->GrabFocus();
93 m_pEdName
->SetSelection( aCurSel
);
96 ScNameDefDlg::~ScNameDefDlg()
101 void ScNameDefDlg::dispose()
107 m_pBtnRowHeader
.clear();
108 m_pBtnColHeader
.clear();
109 m_pBtnPrintArea
.clear();
110 m_pBtnCriteria
.clear();
112 m_pBtnCancel
.clear();
114 ScAnyRefDlg::dispose();
117 void ScNameDefDlg::CancelPushed()
123 ScTabViewShell
* pViewSh
= ScTabViewShell::GetActiveViewShell();
124 pViewSh
->SwitchBetweenRefDialogs(this);
128 bool ScNameDefDlg::IsFormulaValid()
130 ScCompiler
aComp( mpDoc
, maCursorPos
);
131 aComp
.SetGrammar( mpDoc
->GetGrammar() );
132 ScTokenArray
* pCode
= aComp
.CompileString(m_pEdRange
->GetText());
133 if (pCode
->GetCodeError())
146 bool ScNameDefDlg::IsNameValid()
148 OUString aScope
= m_pLbScope
->GetSelectEntry();
149 OUString aName
= m_pEdName
->GetText();
151 ScRangeName
* pRangeName
= NULL
;
152 if(aScope
== maGlobalNameStr
)
154 pRangeName
= maRangeMap
.find(OUString(STR_GLOBAL_RANGE_NAME
))->second
;
158 pRangeName
= maRangeMap
.find(aScope
)->second
;
161 m_pFtInfo
->SetControlBackground(GetSettings().GetStyleSettings().GetDialogColor());
162 if ( aName
.isEmpty() )
164 m_pBtnAdd
->Disable();
165 m_pFtInfo
->SetText(maStrInfoDefault
);
168 else if (!ScRangeData::IsNameValid( aName
, mpDoc
))
170 m_pFtInfo
->SetControlBackground(GetSettings().GetStyleSettings().GetHighlightColor());
171 m_pFtInfo
->SetText(maErrInvalidNameStr
);
172 m_pBtnAdd
->Disable();
175 else if (pRangeName
->findByUpperName(ScGlobal::pCharClass
->uppercase(aName
)))
177 m_pFtInfo
->SetControlBackground(GetSettings().GetStyleSettings().GetHighlightColor());
178 m_pFtInfo
->SetText(maErrNameInUse
);
179 m_pBtnAdd
->Disable();
183 if (!IsFormulaValid())
185 m_pFtInfo
->SetControlBackground(GetSettings().GetStyleSettings().GetHighlightColor());
186 m_pBtnAdd
->Disable();
190 m_pFtInfo
->SetText(maStrInfoDefault
);
195 void ScNameDefDlg::AddPushed()
197 OUString aScope
= m_pLbScope
->GetSelectEntry();
198 OUString aName
= m_pEdName
->GetText();
199 OUString aExpression
= m_pEdRange
->GetText();
205 if (aScope
.isEmpty())
210 ScRangeName
* pRangeName
= NULL
;
211 if(aScope
== maGlobalNameStr
)
213 pRangeName
= maRangeMap
.find(OUString(STR_GLOBAL_RANGE_NAME
))->second
;
217 pRangeName
= maRangeMap
.find(aScope
)->second
;
222 if (!IsNameValid()) //should not happen, but make sure we don't break anything
228 RangeType nType
= RT_NAME
;
230 ScRangeData
* pNewEntry
= new ScRangeData( mpDoc
,
237 | (m_pBtnRowHeader
->IsChecked() ? RT_ROWHEADER
: RangeType(0))
238 | (m_pBtnColHeader
->IsChecked() ? RT_COLHEADER
: RangeType(0))
239 | (m_pBtnPrintArea
->IsChecked() ? RT_PRINTAREA
: RangeType(0))
240 | (m_pBtnCriteria
->IsChecked() ? RT_CRITERIA
: RangeType(0));
241 pNewEntry
->AddType(nType
);
243 // aExpression valid?
244 if ( 0 == pNewEntry
->GetErrCode() )
246 if ( !pRangeName
->insert( pNewEntry
) )
251 // this means we called directly through the menu
254 // if no table with that name is found, assume global range name
255 if (!mpDoc
->GetTable(aScope
, nTab
))
258 assert( pNewEntry
); // undo of no insertion smells fishy
260 mpDocShell
->GetUndoManager()->AddUndoAction(
261 new ScUndoAddRangeData( mpDocShell
, pNewEntry
, nTab
) );
263 // set table stream invalid, otherwise RangeName won't be saved if no other
264 // call invalidates the stream
266 mpDoc
->SetStreamValid(nTab
, false);
267 SfxGetpApp()->Broadcast( SfxSimpleHint( SC_HINT_AREAS_CHANGED
) );
268 mpDocShell
->SetDocumentModified();
275 ScTabViewShell
* pViewSh
= ScTabViewShell::GetActiveViewShell();
276 pViewSh
->SwitchBetweenRefDialogs(this);
282 Selection aCurSel
= Selection( 0, SELECTION_MAX
);
283 m_pEdRange
->GrabFocus();
284 m_pEdRange
->SetSelection( aCurSel
);
290 void ScNameDefDlg::GetNewData(OUString
& rName
, OUString
& rScope
)
296 bool ScNameDefDlg::IsRefInputMode() const
298 return m_pEdRange
->IsEnabled();
301 void ScNameDefDlg::RefInputDone( bool bForced
)
303 ScAnyRefDlg::RefInputDone(bForced
);
307 void ScNameDefDlg::SetReference( const ScRange
& rRef
, ScDocument
* pDocP
)
309 if ( m_pEdRange
->IsEnabled() )
311 if ( rRef
.aStart
!= rRef
.aEnd
)
312 RefInputStart(m_pEdRange
);
313 OUString
aRefStr(rRef
.Format(ABS_DREF3D
, pDocP
,
314 ScAddress::Details(pDocP
->GetAddressConvention(), 0, 0)));
315 m_pEdRange
->SetRefString( aRefStr
);
319 bool ScNameDefDlg::Close()
321 return DoClose( ScNameDefDlgWrapper::GetChildWindowId() );
324 void ScNameDefDlg::SetActive()
326 m_pEdRange
->GrabFocus();
330 IMPL_LINK_NOARG(ScNameDefDlg
, CancelBtnHdl
)
336 IMPL_LINK_NOARG(ScNameDefDlg
, AddBtnHdl
)
342 IMPL_LINK_NOARG(ScNameDefDlg
, NameModifyHdl
)
348 IMPL_LINK_NOARG(ScNameDefDlg
, AssignGetFocusHdl
)
354 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */