Line data Source code
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 :
21 : #include <svx/svdundo.hxx>
22 : #include <svx/svdocapt.hxx>
23 : #include <sfx2/bindings.hxx>
24 : #include <sfx2/dispatch.hxx>
25 : #include <vcl/msgbox.hxx>
26 : #include "svl/zforlist.hxx"
27 : #include "svl/zformat.hxx"
28 :
29 : #include "viewfunc.hxx"
30 : #include "detfunc.hxx"
31 : #include "detdata.hxx"
32 : #include "viewdata.hxx"
33 : #include "drwlayer.hxx"
34 : #include "docsh.hxx"
35 : #include "undocell.hxx"
36 : #include "futext.hxx"
37 : #include "docfunc.hxx"
38 : #include "globstr.hrc"
39 : #include "sc.hrc"
40 : #include "fusel.hxx"
41 : #include "reftokenhelper.hxx"
42 : #include "externalrefmgr.hxx"
43 : #include "formulacell.hxx"
44 : #include "markdata.hxx"
45 : #include "drawview.hxx"
46 : #include "globalnames.hxx"
47 :
48 : #include <vector>
49 :
50 : using ::std::vector;
51 :
52 0 : void ScViewFunc::DetectiveAddPred()
53 : {
54 0 : ScDocShell* pDocSh = GetViewData()->GetDocShell();
55 0 : pDocSh->GetDocFunc().DetectiveAddPred( GetViewData()->GetCurPos() );
56 0 : RecalcPPT(); //! use broadcast in DocFunc instead?
57 0 : }
58 :
59 0 : void ScViewFunc::DetectiveDelPred()
60 : {
61 0 : ScDocShell* pDocSh = GetViewData()->GetDocShell();
62 0 : pDocSh->GetDocFunc().DetectiveDelPred( GetViewData()->GetCurPos() );
63 0 : RecalcPPT();
64 0 : }
65 :
66 0 : void ScViewFunc::DetectiveAddSucc()
67 : {
68 0 : ScDocShell* pDocSh = GetViewData()->GetDocShell();
69 0 : pDocSh->GetDocFunc().DetectiveAddSucc( GetViewData()->GetCurPos() );
70 0 : RecalcPPT();
71 0 : }
72 :
73 0 : void ScViewFunc::DetectiveDelSucc()
74 : {
75 0 : ScDocShell* pDocSh = GetViewData()->GetDocShell();
76 0 : pDocSh->GetDocFunc().DetectiveDelSucc( GetViewData()->GetCurPos() );
77 0 : RecalcPPT();
78 0 : }
79 :
80 0 : void ScViewFunc::DetectiveAddError()
81 : {
82 0 : ScDocShell* pDocSh = GetViewData()->GetDocShell();
83 0 : pDocSh->GetDocFunc().DetectiveAddError( GetViewData()->GetCurPos() );
84 0 : RecalcPPT();
85 0 : }
86 :
87 0 : void ScViewFunc::DetectiveDelAll()
88 : {
89 0 : ScDocShell* pDocSh = GetViewData()->GetDocShell();
90 0 : pDocSh->GetDocFunc().DetectiveDelAll( GetViewData()->GetTabNo() );
91 0 : RecalcPPT();
92 0 : }
93 :
94 0 : void ScViewFunc::DetectiveMarkInvalid()
95 : {
96 0 : ScDocShell* pDocSh = GetViewData()->GetDocShell();
97 0 : pDocSh->GetDocFunc().DetectiveMarkInvalid( GetViewData()->GetTabNo() );
98 0 : RecalcPPT();
99 0 : }
100 :
101 0 : void ScViewFunc::DetectiveRefresh()
102 : {
103 0 : ScDocShell* pDocSh = GetViewData()->GetDocShell();
104 0 : pDocSh->GetDocFunc().DetectiveRefresh();
105 0 : RecalcPPT();
106 0 : }
107 :
108 0 : static void lcl_jumpToRange(const ScRange& rRange, ScViewData* pView, ScDocument* pDoc)
109 : {
110 0 : OUString aAddrText(rRange.Format(SCR_ABS_3D, pDoc));
111 0 : SfxStringItem aPosItem(SID_CURRENTCELL, aAddrText);
112 0 : SfxBoolItem aUnmarkItem(FN_PARAM_1, true); // remove existing selection
113 0 : pView->GetDispatcher().Execute(
114 : SID_CURRENTCELL, SFX_CALLMODE_SYNCHRON | SFX_CALLMODE_RECORD,
115 0 : &aPosItem, &aUnmarkItem, 0L);
116 0 : }
117 :
118 0 : void ScViewFunc::MarkAndJumpToRanges(const ScRangeList& rRanges)
119 : {
120 0 : ScViewData* pView = GetViewData();
121 0 : ScDocShell* pDocSh = pView->GetDocShell();
122 :
123 0 : ScRangeList aRanges(rRanges);
124 0 : ScRangeList aRangesToMark;
125 0 : ScAddress aCurPos = pView->GetCurPos();
126 0 : size_t ListSize = aRanges.size();
127 0 : for ( size_t i = 0; i < ListSize; ++i )
128 : {
129 0 : const ScRange* p = aRanges[i];
130 : // Collect only those ranges that are on the same sheet as the current
131 : // cursor.
132 0 : if (p->aStart.Tab() == aCurPos.Tab())
133 0 : aRangesToMark.Append(*p);
134 : }
135 :
136 0 : if (aRangesToMark.empty())
137 0 : return;
138 :
139 : // Jump to the first range of all precedent ranges.
140 0 : const ScRange* p = aRangesToMark.front();
141 0 : lcl_jumpToRange(*p, pView, pDocSh->GetDocument());
142 :
143 0 : ListSize = aRangesToMark.size();
144 0 : for ( size_t i = 0; i < ListSize; ++i )
145 : {
146 0 : p = aRangesToMark[i];
147 0 : MarkRange(*p, false, true);
148 0 : }
149 : }
150 :
151 0 : void ScViewFunc::DetectiveMarkPred()
152 : {
153 0 : ScViewData* pView = GetViewData();
154 0 : ScDocShell* pDocSh = pView->GetDocShell();
155 0 : ScDocument* pDoc = pDocSh->GetDocument();
156 0 : ScMarkData& rMarkData = pView->GetMarkData();
157 0 : ScAddress aCurPos = pView->GetCurPos();
158 0 : ScRangeList aRanges;
159 0 : if (rMarkData.IsMarked() || rMarkData.IsMultiMarked())
160 0 : rMarkData.FillRangeListWithMarks(&aRanges, false);
161 : else
162 0 : aRanges.Append(aCurPos);
163 :
164 0 : vector<ScTokenRef> aRefTokens;
165 0 : pDocSh->GetDocFunc().DetectiveCollectAllPreds(aRanges, aRefTokens);
166 :
167 0 : if (aRefTokens.empty())
168 : // No precedents found. Nothing to do.
169 0 : return;
170 :
171 0 : ScTokenRef p = aRefTokens.front();
172 0 : if (ScRefTokenHelper::isExternalRef(p))
173 : {
174 : // This is external. Open the external document if available, and
175 : // jump to the destination.
176 :
177 0 : sal_uInt16 nFileId = p->GetIndex();
178 0 : ScExternalRefManager* pRefMgr = pDoc->GetExternalRefManager();
179 0 : const OUString* pPath = pRefMgr->getExternalFileName(nFileId);
180 :
181 0 : ScRange aRange;
182 0 : if (pPath && ScRefTokenHelper::getRangeFromToken(aRange, p, aCurPos, true))
183 : {
184 0 : OUString aTabName = p->GetString().getString();
185 0 : OUStringBuffer aBuf;
186 0 : aBuf.append(*pPath);
187 0 : aBuf.append('#');
188 0 : aBuf.append(aTabName);
189 0 : aBuf.append('.');
190 :
191 0 : OUString aRangeStr(aRange.Format(SCA_VALID));
192 0 : aBuf.append(aRangeStr);
193 :
194 0 : ScGlobal::OpenURL(aBuf.makeStringAndClear(), OUString());
195 : }
196 0 : return;
197 : }
198 : else
199 : {
200 0 : ScRange aRange;
201 0 : ScRefTokenHelper::getRangeFromToken(aRange, p, aCurPos, false);
202 0 : if (aRange.aStart.Tab() != aCurPos.Tab())
203 : {
204 : // The first precedent range is on a different sheet. Jump to it
205 : // immediately and forget the rest.
206 0 : lcl_jumpToRange(aRange, pView, pDoc);
207 0 : return;
208 : }
209 : }
210 :
211 0 : ScRangeList aDestRanges;
212 0 : ScRefTokenHelper::getRangeListFromTokens(aDestRanges, aRefTokens, aCurPos);
213 0 : MarkAndJumpToRanges(aDestRanges);
214 : }
215 :
216 0 : void ScViewFunc::DetectiveMarkSucc()
217 : {
218 0 : ScViewData* pView = GetViewData();
219 0 : ScDocShell* pDocSh = pView->GetDocShell();
220 0 : ScMarkData& rMarkData = pView->GetMarkData();
221 0 : ScAddress aCurPos = pView->GetCurPos();
222 0 : ScRangeList aRanges;
223 0 : if (rMarkData.IsMarked() || rMarkData.IsMultiMarked())
224 0 : rMarkData.FillRangeListWithMarks(&aRanges, false);
225 : else
226 0 : aRanges.Append(aCurPos);
227 :
228 0 : vector<ScTokenRef> aRefTokens;
229 0 : pDocSh->GetDocFunc().DetectiveCollectAllSuccs(aRanges, aRefTokens);
230 :
231 0 : if (aRefTokens.empty())
232 : // No dependents found. Nothing to do.
233 0 : return;
234 :
235 0 : ScRangeList aDestRanges;
236 0 : ScRefTokenHelper::getRangeListFromTokens(aDestRanges, aRefTokens, aCurPos);
237 0 : MarkAndJumpToRanges(aDestRanges);
238 : }
239 :
240 0 : void ScViewFunc::InsertCurrentTime(short nCellFmt, const OUString& rUndoStr)
241 : {
242 0 : ScViewData* pViewData = GetViewData();
243 0 : ScAddress aCurPos = pViewData->GetCurPos();
244 0 : ScDocShell* pDocSh = pViewData->GetDocShell();
245 0 : ScDocument* pDoc = pDocSh->GetDocument();
246 0 : ::svl::IUndoManager* pUndoMgr = pDocSh->GetUndoManager();
247 0 : SvNumberFormatter* pFormatter = pDoc->GetFormatTable();
248 0 : Date aActDate( Date::SYSTEM );
249 0 : double fDate = aActDate - *pFormatter->GetNullDate();
250 0 : Time aActTime( Time::SYSTEM );
251 0 : double fTime = aActTime.GetHour() / static_cast<double>(::Time::hourPerDay) +
252 0 : aActTime.GetMin() / static_cast<double>(::Time::minutePerDay) +
253 0 : aActTime.GetSec() / static_cast<double>(::Time::secondPerDay) +
254 0 : aActTime.GetNanoSec() / static_cast<double>(::Time::nanoSecPerDay);
255 0 : pUndoMgr->EnterListAction(rUndoStr, rUndoStr);
256 0 : pDocSh->GetDocFunc().SetValueCell(aCurPos, fDate+fTime, true);
257 :
258 : // Set the new cell format only when it differs from the current cell
259 : // format type.
260 0 : sal_uInt32 nCurNumFormat = pDoc->GetNumberFormat(aCurPos);
261 0 : const SvNumberformat* pEntry = pFormatter->GetEntry(nCurNumFormat);
262 0 : if (!pEntry || !(pEntry->GetType() & nCellFmt))
263 0 : SetNumberFormat(nCellFmt);
264 0 : pUndoMgr->LeaveListAction();
265 0 : }
266 :
267 0 : void ScViewFunc::ShowNote( bool bShow )
268 : {
269 0 : if( bShow )
270 0 : HideNoteMarker();
271 0 : const ScViewData& rViewData = *GetViewData();
272 0 : ScAddress aPos( rViewData.GetCurX(), rViewData.GetCurY(), rViewData.GetTabNo() );
273 : // show note moved to ScDocFunc, to be able to use it in notesuno.cxx
274 0 : rViewData.GetDocShell()->GetDocFunc().ShowNote( aPos, bShow );
275 0 : }
276 :
277 0 : void ScViewFunc::EditNote()
278 : {
279 : // for editing display and activate
280 :
281 0 : ScDocShell* pDocSh = GetViewData()->GetDocShell();
282 0 : ScDocument* pDoc = pDocSh->GetDocument();
283 0 : SCCOL nCol = GetViewData()->GetCurX();
284 0 : SCROW nRow = GetViewData()->GetCurY();
285 0 : SCTAB nTab = GetViewData()->GetTabNo();
286 0 : ScAddress aPos( nCol, nRow, nTab );
287 :
288 : // start drawing undo to catch undo action for insertion of the caption object
289 0 : pDocSh->MakeDrawLayer();
290 0 : ScDrawLayer* pDrawLayer = pDoc->GetDrawLayer();
291 0 : pDrawLayer->BeginCalcUndo(true);
292 : // generated undo action is processed in FuText::StopEditMode
293 :
294 : // get existing note or create a new note (including caption drawing object)
295 0 : if( ScPostIt* pNote = pDoc->GetOrCreateNote( aPos ) )
296 : {
297 : // hide temporary note caption
298 0 : HideNoteMarker();
299 : // show caption object without changing internal visibility state
300 0 : pNote->ShowCaptionTemp( aPos );
301 :
302 : /* Drawing object has been created in ScDocument::GetOrCreateNote() or
303 : in ScPostIt::ShowCaptionTemp(), so ScPostIt::GetCaption() should
304 : return a caption object. */
305 0 : if( SdrCaptionObj* pCaption = pNote->GetCaption() )
306 : {
307 0 : if ( ScDrawView* pScDrawView = GetScDrawView() )
308 0 : pScDrawView->SyncForGrid( pCaption );
309 : // #i33764# enable the resize handles before starting edit mode
310 0 : if( FuPoor* pDraw = GetDrawFuncPtr() )
311 0 : static_cast< FuSelection* >( pDraw )->ActivateNoteHandles( pCaption );
312 :
313 : // activate object (as in FuSelection::TestComment)
314 0 : GetViewData()->GetDispatcher().Execute( SID_DRAW_NOTEEDIT, SFX_CALLMODE_SYNCHRON | SFX_CALLMODE_RECORD );
315 : // now get the created FuText and set into EditMode
316 0 : FuPoor* pPoor = GetDrawFuncPtr();
317 0 : if ( pPoor && (pPoor->GetSlotID() == SID_DRAW_NOTEEDIT) ) // has no RTTI
318 : {
319 0 : ScrollToObject( pCaption ); // make object fully visible
320 0 : static_cast< FuText* >( pPoor )->SetInEditMode( pCaption );
321 : }
322 : }
323 : }
324 0 : }
325 :
326 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|