fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / ui / namedlg / namedefdlg.cxx
blob0f80368c53770d98a23a09aed94f2575fcc79af5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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"
25 #include "sc.hrc"
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" )
40 mbUndo( bUndo ),
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)
71 OUString aTabName;
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
83 ScRange aRange;
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()
98 disposeOnce();
101 void ScNameDefDlg::dispose()
103 m_pEdName.clear();
104 m_pEdRange.clear();
105 m_pRbRange.clear();
106 m_pLbScope.clear();
107 m_pBtnRowHeader.clear();
108 m_pBtnColHeader.clear();
109 m_pBtnPrintArea.clear();
110 m_pBtnCriteria.clear();
111 m_pBtnAdd.clear();
112 m_pBtnCancel.clear();
113 m_pFtInfo.clear();
114 ScAnyRefDlg::dispose();
117 void ScNameDefDlg::CancelPushed()
119 if (mbUndo)
120 Close();
121 else
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())
135 //TODO: info message
136 delete pCode;
137 return false;
139 else
141 delete pCode;
142 return true;
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;
156 else
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);
166 return false;
168 else if (!ScRangeData::IsNameValid( aName, mpDoc ))
170 m_pFtInfo->SetControlBackground(GetSettings().GetStyleSettings().GetHighlightColor());
171 m_pFtInfo->SetText(maErrInvalidNameStr);
172 m_pBtnAdd->Disable();
173 return false;
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();
180 return false;
183 if (!IsFormulaValid())
185 m_pFtInfo->SetControlBackground(GetSettings().GetStyleSettings().GetHighlightColor());
186 m_pBtnAdd->Disable();
187 return false;
190 m_pFtInfo->SetText(maStrInfoDefault);
191 m_pBtnAdd->Enable();
192 return true;
195 void ScNameDefDlg::AddPushed()
197 OUString aScope = m_pLbScope->GetSelectEntry();
198 OUString aName = m_pEdName->GetText();
199 OUString aExpression = m_pEdRange->GetText();
201 if (aName.isEmpty())
203 return;
205 if (aScope.isEmpty())
207 return;
210 ScRangeName* pRangeName = NULL;
211 if(aScope == maGlobalNameStr)
213 pRangeName = maRangeMap.find(OUString(STR_GLOBAL_RANGE_NAME))->second;
215 else
217 pRangeName = maRangeMap.find(aScope)->second;
219 if (!pRangeName)
220 return;
222 if (!IsNameValid()) //should not happen, but make sure we don't break anything
223 return;
224 else
226 if ( mpDoc )
228 RangeType nType = RT_NAME;
230 ScRangeData* pNewEntry = new ScRangeData( mpDoc,
231 aName,
232 aExpression,
233 maCursorPos,
234 nType );
236 nType = nType
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 ) )
247 pNewEntry = NULL;
249 if (mbUndo)
251 // this means we called directly through the menu
253 SCTAB nTab;
254 // if no table with that name is found, assume global range name
255 if (!mpDoc->GetTable(aScope, nTab))
256 nTab = -1;
258 assert( pNewEntry); // undo of no insertion smells fishy
259 if (pNewEntry)
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
265 if (nTab != -1)
266 mpDoc->SetStreamValid(nTab, false);
267 SfxGetpApp()->Broadcast( SfxSimpleHint( SC_HINT_AREAS_CHANGED ) );
268 mpDocShell->SetDocumentModified();
269 Close();
271 else
273 maName = aName;
274 maScope = aScope;
275 ScTabViewShell* pViewSh = ScTabViewShell::GetActiveViewShell();
276 pViewSh->SwitchBetweenRefDialogs(this);
279 else
281 delete pNewEntry;
282 Selection aCurSel = Selection( 0, SELECTION_MAX );
283 m_pEdRange->GrabFocus();
284 m_pEdRange->SetSelection( aCurSel );
290 void ScNameDefDlg::GetNewData(OUString& rName, OUString& rScope)
292 rName = maName;
293 rScope = maScope;
296 bool ScNameDefDlg::IsRefInputMode() const
298 return m_pEdRange->IsEnabled();
301 void ScNameDefDlg::RefInputDone( bool bForced)
303 ScAnyRefDlg::RefInputDone(bForced);
304 IsNameValid();
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();
327 RefInputDone();
330 IMPL_LINK_NOARG(ScNameDefDlg, CancelBtnHdl)
332 CancelPushed();
333 return 0;
336 IMPL_LINK_NOARG(ScNameDefDlg, AddBtnHdl)
338 AddPushed();
339 return 0;
342 IMPL_LINK_NOARG(ScNameDefDlg, NameModifyHdl)
344 IsNameValid();
345 return 0;
348 IMPL_LINK_NOARG(ScNameDefDlg, AssignGetFocusHdl)
350 IsNameValid();
351 return 0;
354 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */