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 : #include <svx/svdundo.hxx>
21 : #include <svx/svdocapt.hxx>
22 : #include <sfx2/bindings.hxx>
23 : #include <sfx2/dispatch.hxx>
24 : #include <vcl/msgbox.hxx>
25 : #include <svl/zforlist.hxx>
26 : #include <svl/zformat.hxx>
27 : #include <editeng/editview.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 : #include "inputhdl.hxx"
48 :
49 : #include <vector>
50 :
51 : using ::std::vector;
52 :
53 0 : void ScViewFunc::DetectiveAddPred()
54 : {
55 0 : ScDocShell* pDocSh = GetViewData().GetDocShell();
56 0 : pDocSh->GetDocFunc().DetectiveAddPred( GetViewData().GetCurPos() );
57 0 : RecalcPPT(); //! use broadcast in DocFunc instead?
58 0 : }
59 :
60 0 : void ScViewFunc::DetectiveDelPred()
61 : {
62 0 : ScDocShell* pDocSh = GetViewData().GetDocShell();
63 0 : pDocSh->GetDocFunc().DetectiveDelPred( GetViewData().GetCurPos() );
64 0 : RecalcPPT();
65 0 : }
66 :
67 0 : void ScViewFunc::DetectiveAddSucc()
68 : {
69 0 : ScDocShell* pDocSh = GetViewData().GetDocShell();
70 0 : pDocSh->GetDocFunc().DetectiveAddSucc( GetViewData().GetCurPos() );
71 0 : RecalcPPT();
72 0 : }
73 :
74 0 : void ScViewFunc::DetectiveDelSucc()
75 : {
76 0 : ScDocShell* pDocSh = GetViewData().GetDocShell();
77 0 : pDocSh->GetDocFunc().DetectiveDelSucc( GetViewData().GetCurPos() );
78 0 : RecalcPPT();
79 0 : }
80 :
81 0 : void ScViewFunc::DetectiveAddError()
82 : {
83 0 : ScDocShell* pDocSh = GetViewData().GetDocShell();
84 0 : pDocSh->GetDocFunc().DetectiveAddError( GetViewData().GetCurPos() );
85 0 : RecalcPPT();
86 0 : }
87 :
88 0 : void ScViewFunc::DetectiveDelAll()
89 : {
90 0 : ScDocShell* pDocSh = GetViewData().GetDocShell();
91 0 : pDocSh->GetDocFunc().DetectiveDelAll( GetViewData().GetTabNo() );
92 0 : RecalcPPT();
93 0 : }
94 :
95 0 : void ScViewFunc::DetectiveMarkInvalid()
96 : {
97 0 : ScDocShell* pDocSh = GetViewData().GetDocShell();
98 0 : pDocSh->GetDocFunc().DetectiveMarkInvalid( GetViewData().GetTabNo() );
99 0 : RecalcPPT();
100 0 : }
101 :
102 0 : void ScViewFunc::DetectiveRefresh()
103 : {
104 0 : ScDocShell* pDocSh = GetViewData().GetDocShell();
105 0 : pDocSh->GetDocFunc().DetectiveRefresh();
106 0 : RecalcPPT();
107 0 : }
108 :
109 0 : static void lcl_jumpToRange(const ScRange& rRange, ScViewData* pView, ScDocument* pDoc)
110 : {
111 0 : OUString aAddrText(rRange.Format(SCR_ABS_3D, pDoc));
112 0 : SfxStringItem aPosItem(SID_CURRENTCELL, aAddrText);
113 0 : SfxBoolItem aUnmarkItem(FN_PARAM_1, true); // remove existing selection
114 0 : pView->GetDispatcher().Execute(
115 : SID_CURRENTCELL, SfxCallMode::SYNCHRON | SfxCallMode::RECORD,
116 0 : &aPosItem, &aUnmarkItem, 0L);
117 0 : }
118 :
119 0 : void ScViewFunc::MarkAndJumpToRanges(const ScRangeList& rRanges)
120 : {
121 0 : ScViewData& rView = GetViewData();
122 0 : ScDocShell* pDocSh = rView.GetDocShell();
123 :
124 0 : ScRangeList aRanges(rRanges);
125 0 : ScRangeList aRangesToMark;
126 0 : ScAddress aCurPos = rView.GetCurPos();
127 0 : size_t ListSize = aRanges.size();
128 0 : for ( size_t i = 0; i < ListSize; ++i )
129 : {
130 0 : const ScRange* p = aRanges[i];
131 : // Collect only those ranges that are on the same sheet as the current
132 : // cursor.
133 0 : if (p->aStart.Tab() == aCurPos.Tab())
134 0 : aRangesToMark.Append(*p);
135 : }
136 :
137 0 : if (aRangesToMark.empty())
138 0 : return;
139 :
140 : // Jump to the first range of all precedent ranges.
141 0 : const ScRange* p = aRangesToMark.front();
142 0 : lcl_jumpToRange(*p, &rView, &pDocSh->GetDocument());
143 :
144 0 : ListSize = aRangesToMark.size();
145 0 : for ( size_t i = 0; i < ListSize; ++i )
146 : {
147 0 : p = aRangesToMark[i];
148 0 : MarkRange(*p, false, true);
149 0 : }
150 : }
151 :
152 0 : void ScViewFunc::DetectiveMarkPred()
153 : {
154 0 : ScViewData& rView = GetViewData();
155 0 : ScDocShell* pDocSh = rView.GetDocShell();
156 0 : ScDocument& rDoc = pDocSh->GetDocument();
157 0 : ScMarkData& rMarkData = rView.GetMarkData();
158 0 : ScAddress aCurPos = rView.GetCurPos();
159 0 : ScRangeList aRanges;
160 0 : if (rMarkData.IsMarked() || rMarkData.IsMultiMarked())
161 0 : rMarkData.FillRangeListWithMarks(&aRanges, false);
162 : else
163 0 : aRanges.Append(aCurPos);
164 :
165 0 : vector<ScTokenRef> aRefTokens;
166 0 : pDocSh->GetDocFunc().DetectiveCollectAllPreds(aRanges, aRefTokens);
167 :
168 0 : if (aRefTokens.empty())
169 : // No precedents found. Nothing to do.
170 0 : return;
171 :
172 0 : ScTokenRef p = aRefTokens.front();
173 0 : if (ScRefTokenHelper::isExternalRef(p))
174 : {
175 : // This is external. Open the external document if available, and
176 : // jump to the destination.
177 :
178 0 : sal_uInt16 nFileId = p->GetIndex();
179 0 : ScExternalRefManager* pRefMgr = rDoc.GetExternalRefManager();
180 0 : const OUString* pPath = pRefMgr->getExternalFileName(nFileId);
181 :
182 0 : ScRange aRange;
183 0 : if (pPath && ScRefTokenHelper::getRangeFromToken(aRange, p, aCurPos, true))
184 : {
185 0 : OUString aTabName = p->GetString().getString();
186 0 : OUStringBuffer aBuf;
187 0 : aBuf.append(*pPath);
188 0 : aBuf.append('#');
189 0 : aBuf.append(aTabName);
190 0 : aBuf.append('.');
191 :
192 0 : OUString aRangeStr(aRange.Format(SCA_VALID));
193 0 : aBuf.append(aRangeStr);
194 :
195 0 : ScGlobal::OpenURL(aBuf.makeStringAndClear(), OUString());
196 : }
197 0 : return;
198 : }
199 : else
200 : {
201 0 : ScRange aRange;
202 0 : ScRefTokenHelper::getRangeFromToken(aRange, p, aCurPos, false);
203 0 : if (aRange.aStart.Tab() != aCurPos.Tab())
204 : {
205 : // The first precedent range is on a different sheet. Jump to it
206 : // immediately and forget the rest.
207 0 : lcl_jumpToRange(aRange, &rView, &rDoc);
208 0 : return;
209 : }
210 : }
211 :
212 0 : ScRangeList aDestRanges;
213 0 : ScRefTokenHelper::getRangeListFromTokens(aDestRanges, aRefTokens, aCurPos);
214 0 : MarkAndJumpToRanges(aDestRanges);
215 : }
216 :
217 0 : void ScViewFunc::DetectiveMarkSucc()
218 : {
219 0 : ScViewData& rView = GetViewData();
220 0 : ScDocShell* pDocSh = rView.GetDocShell();
221 0 : ScMarkData& rMarkData = rView.GetMarkData();
222 0 : ScAddress aCurPos = rView.GetCurPos();
223 0 : ScRangeList aRanges;
224 0 : if (rMarkData.IsMarked() || rMarkData.IsMultiMarked())
225 0 : rMarkData.FillRangeListWithMarks(&aRanges, false);
226 : else
227 0 : aRanges.Append(aCurPos);
228 :
229 0 : vector<ScTokenRef> aRefTokens;
230 0 : pDocSh->GetDocFunc().DetectiveCollectAllSuccs(aRanges, aRefTokens);
231 :
232 0 : if (aRefTokens.empty())
233 : // No dependents found. Nothing to do.
234 0 : return;
235 :
236 0 : ScRangeList aDestRanges;
237 0 : ScRefTokenHelper::getRangeListFromTokens(aDestRanges, aRefTokens, aCurPos);
238 0 : MarkAndJumpToRanges(aDestRanges);
239 : }
240 :
241 : /** Insert date or time into current cell.
242 :
243 : If cell is in input or edit mode, insert date/time at cursor position, else
244 : create a date or time or date+time cell as follows:
245 :
246 : - key date on time cell => current date + time of cell => date+time formatted cell
247 : - unless time cell was empty or 00:00 time => current date => date formatted cell
248 : - key date on date+time cell => current date + 00:00 time => date+time formatted cell
249 : - unless date was current date => current date => date formatted cell
250 : - key date on other cell => current date => date formatted cell
251 : - key time on date cell => date of cell + current time => date+time formatted cell
252 : - unless date cell was empty => current time => time formatted cell
253 : - key time on date+time cell => current time => time formatted cell
254 : - unless cell was empty => current date+time => date+time formatted cell
255 : - key time on other cell => current time => time formatted cell
256 : */
257 0 : void ScViewFunc::InsertCurrentTime(short nReqFmt, const OUString& rUndoStr)
258 : {
259 0 : ScViewData& rViewData = GetViewData();
260 :
261 0 : ScInputHandler* pInputHdl = SC_MOD()->GetInputHdl( rViewData.GetViewShell());
262 0 : bool bInputMode = (pInputHdl && pInputHdl->IsInputMode());
263 :
264 0 : ScDocShell* pDocSh = rViewData.GetDocShell();
265 0 : ScDocument& rDoc = pDocSh->GetDocument();
266 0 : ScAddress aCurPos = rViewData.GetCurPos();
267 0 : const sal_uInt32 nCurNumFormat = rDoc.GetNumberFormat(aCurPos);
268 0 : SvNumberFormatter* pFormatter = rDoc.GetFormatTable();
269 0 : const SvNumberformat* pCurNumFormatEntry = pFormatter->GetEntry(nCurNumFormat);
270 : const short nCurNumFormatType = (pCurNumFormatEntry ?
271 0 : (pCurNumFormatEntry->GetType() & ~NUMBERFORMAT_DEFINED) : NUMBERFORMAT_UNDEFINED);
272 :
273 0 : if (bInputMode)
274 : {
275 0 : double fVal = 0.0;
276 0 : sal_uInt32 nFormat = 0;
277 0 : switch (nReqFmt)
278 : {
279 : case NUMBERFORMAT_DATE:
280 : {
281 0 : Date aActDate( Date::SYSTEM );
282 0 : fVal = aActDate - *pFormatter->GetNullDate();
283 0 : if (nCurNumFormatType == NUMBERFORMAT_DATE)
284 0 : nFormat = nCurNumFormat;
285 : }
286 0 : break;
287 : case NUMBERFORMAT_TIME:
288 : {
289 0 : tools::Time aActTime( tools::Time::SYSTEM );
290 0 : fVal = aActTime.GetTimeInDays();
291 0 : if (nCurNumFormatType == NUMBERFORMAT_TIME)
292 0 : nFormat = nCurNumFormat;
293 : }
294 0 : break;
295 : default:
296 : assert(!"unhandled current date/time request");
297 0 : nReqFmt = NUMBERFORMAT_DATETIME;
298 : // fallthru
299 : case NUMBERFORMAT_DATETIME:
300 : {
301 0 : DateTime aActDateTime( DateTime::SYSTEM );
302 0 : fVal = aActDateTime - DateTime( *pFormatter->GetNullDate());
303 0 : if (nCurNumFormatType == NUMBERFORMAT_DATETIME)
304 0 : nFormat = nCurNumFormat;
305 : }
306 0 : break;
307 : }
308 :
309 0 : if (!nFormat)
310 : {
311 0 : const LanguageType nLang = (pCurNumFormatEntry ? pCurNumFormatEntry->GetLanguage() : ScGlobal::eLnge);
312 0 : nFormat = pFormatter->GetStandardFormat( nReqFmt, nLang);
313 : // This would return a more precise format with seconds and 100th
314 : // seconds for a time request.
315 : //nFormat = pFormatter->GetStandardFormat( fVal, nFormat, nReqFmt, nLang);
316 : }
317 0 : OUString aString;
318 : Color* pColor;
319 0 : pFormatter->GetOutputString( fVal, nFormat, aString, &pColor);
320 :
321 0 : pInputHdl->DataChanging();
322 0 : EditView* pTopView = pInputHdl->GetTopView();
323 0 : if (pTopView)
324 0 : pTopView->InsertText( aString);
325 0 : EditView* pTableView = pInputHdl->GetTableView();
326 0 : if (pTableView)
327 0 : pTableView->InsertText( aString);
328 0 : pInputHdl->DataChanged( false, true);
329 : }
330 : else
331 : {
332 0 : bool bForceReqFmt = false;
333 0 : const double fCell = rDoc.GetValue( aCurPos);
334 : // Combine requested date/time stamp with existing cell time/date, if any.
335 0 : switch (nReqFmt)
336 : {
337 : case NUMBERFORMAT_DATE:
338 0 : switch (nCurNumFormatType)
339 : {
340 : case NUMBERFORMAT_TIME:
341 : // An empty cell formatted as time (or 00:00 time) shall
342 : // not result in the current date with 00:00 time, but only
343 : // in current date.
344 0 : if (fCell != 0.0)
345 0 : nReqFmt = NUMBERFORMAT_DATETIME;
346 0 : break;
347 : case NUMBERFORMAT_DATETIME:
348 : {
349 : // Force to only date if the existing date+time is the
350 : // current date. This way inserting current date twice
351 : // on an existing date+time cell can be used to force
352 : // date, which otherwise would only be possible by
353 : // applying a date format.
354 0 : double fDate = rtl::math::approxFloor( fCell);
355 0 : if (fDate == (Date( Date::SYSTEM) - *pFormatter->GetNullDate()))
356 0 : bForceReqFmt = true;
357 : }
358 0 : break;
359 : }
360 0 : break;
361 : case NUMBERFORMAT_TIME:
362 0 : switch (nCurNumFormatType)
363 : {
364 : case NUMBERFORMAT_DATE:
365 : // An empty cell formatted as date shall not result in the
366 : // null date and current time, but only in current time.
367 0 : if (fCell != 0.0)
368 0 : nReqFmt = NUMBERFORMAT_DATETIME;
369 0 : break;
370 : case NUMBERFORMAT_DATETIME:
371 : // Requesting current time on an empty date+time cell
372 : // inserts both current date+time.
373 0 : if (fCell == 0.0)
374 0 : nReqFmt = NUMBERFORMAT_DATETIME;
375 : else
376 : {
377 : // Add current time to an existing date+time where time is
378 : // zero and date is current date, else force time only.
379 0 : double fDate = rtl::math::approxFloor( fCell);
380 0 : double fTime = fCell - fDate;
381 0 : if (fTime == 0.0 && fDate == (Date( Date::SYSTEM) - *pFormatter->GetNullDate()))
382 0 : nReqFmt = NUMBERFORMAT_DATETIME;
383 : else
384 0 : bForceReqFmt = true;
385 : }
386 0 : break;
387 : }
388 0 : break;
389 : default:
390 : assert(!"unhandled current date/time request");
391 0 : nReqFmt = NUMBERFORMAT_DATETIME;
392 : // fallthru
393 : case NUMBERFORMAT_DATETIME:
394 0 : break;
395 : }
396 0 : double fVal = 0.0;
397 0 : switch (nReqFmt)
398 : {
399 : case NUMBERFORMAT_DATE:
400 : {
401 0 : Date aActDate( Date::SYSTEM );
402 0 : fVal = aActDate - *pFormatter->GetNullDate();
403 : }
404 0 : break;
405 : case NUMBERFORMAT_TIME:
406 : {
407 0 : tools::Time aActTime( tools::Time::SYSTEM );
408 0 : fVal = aActTime.GetTimeInDays();
409 : }
410 0 : break;
411 : case NUMBERFORMAT_DATETIME:
412 0 : switch (nCurNumFormatType)
413 : {
414 : case NUMBERFORMAT_DATE:
415 : {
416 0 : double fDate = rtl::math::approxFloor( fCell);
417 0 : tools::Time aActTime( tools::Time::SYSTEM );
418 0 : fVal = fDate + aActTime.GetTimeInDays();
419 : }
420 0 : break;
421 : case NUMBERFORMAT_TIME:
422 : {
423 0 : double fTime = fCell - rtl::math::approxFloor( fCell);
424 0 : Date aActDate( Date::SYSTEM );
425 0 : fVal = (aActDate - *pFormatter->GetNullDate()) + fTime;
426 : }
427 0 : break;
428 : default:
429 : {
430 0 : DateTime aActDateTime( DateTime::SYSTEM );
431 : // Converting the null date to DateTime forces the
432 : // correct operator-() to be used, resulting in a
433 : // fractional date+time instead of only date value.
434 0 : fVal = aActDateTime - DateTime( *pFormatter->GetNullDate());
435 : }
436 : }
437 0 : break;
438 : }
439 :
440 0 : ::svl::IUndoManager* pUndoMgr = pDocSh->GetUndoManager();
441 0 : pUndoMgr->EnterListAction(rUndoStr, rUndoStr);
442 :
443 0 : pDocSh->GetDocFunc().SetValueCell(aCurPos, fVal, true);
444 :
445 : // Set the new cell format only when it differs from the current cell
446 : // format type. Preserve a date+time format unless we force a format
447 : // through.
448 0 : if (bForceReqFmt || (nReqFmt != nCurNumFormatType && nCurNumFormatType != NUMBERFORMAT_DATETIME))
449 0 : SetNumberFormat(nReqFmt);
450 : else
451 0 : rViewData.UpdateInputHandler(); // update input bar with new value
452 :
453 0 : pUndoMgr->LeaveListAction();
454 : }
455 0 : }
456 :
457 0 : void ScViewFunc::ShowNote( bool bShow )
458 : {
459 0 : if( bShow )
460 0 : HideNoteMarker();
461 0 : const ScViewData& rViewData = GetViewData();
462 0 : ScAddress aPos( rViewData.GetCurX(), rViewData.GetCurY(), rViewData.GetTabNo() );
463 : // show note moved to ScDocFunc, to be able to use it in notesuno.cxx
464 0 : rViewData.GetDocShell()->GetDocFunc().ShowNote( aPos, bShow );
465 0 : }
466 :
467 0 : void ScViewFunc::EditNote()
468 : {
469 : // for editing display and activate
470 :
471 0 : ScDocShell* pDocSh = GetViewData().GetDocShell();
472 0 : ScDocument& rDoc = pDocSh->GetDocument();
473 0 : SCCOL nCol = GetViewData().GetCurX();
474 0 : SCROW nRow = GetViewData().GetCurY();
475 0 : SCTAB nTab = GetViewData().GetTabNo();
476 0 : ScAddress aPos( nCol, nRow, nTab );
477 :
478 : // start drawing undo to catch undo action for insertion of the caption object
479 0 : pDocSh->MakeDrawLayer();
480 0 : ScDrawLayer* pDrawLayer = rDoc.GetDrawLayer();
481 0 : pDrawLayer->BeginCalcUndo(true);
482 : // generated undo action is processed in FuText::StopEditMode
483 :
484 : // get existing note or create a new note (including caption drawing object)
485 0 : if( ScPostIt* pNote = rDoc.GetOrCreateNote( aPos ) )
486 : {
487 : // hide temporary note caption
488 0 : HideNoteMarker();
489 : // show caption object without changing internal visibility state
490 0 : pNote->ShowCaptionTemp( aPos );
491 :
492 : /* Drawing object has been created in ScDocument::GetOrCreateNote() or
493 : in ScPostIt::ShowCaptionTemp(), so ScPostIt::GetCaption() should
494 : return a caption object. */
495 0 : if( SdrCaptionObj* pCaption = pNote->GetCaption() )
496 : {
497 0 : if ( ScDrawView* pScDrawView = GetScDrawView() )
498 0 : pScDrawView->SyncForGrid( pCaption );
499 : // #i33764# enable the resize handles before starting edit mode
500 0 : if( FuPoor* pDraw = GetDrawFuncPtr() )
501 0 : static_cast< FuSelection* >( pDraw )->ActivateNoteHandles( pCaption );
502 :
503 : // activate object (as in FuSelection::TestComment)
504 0 : GetViewData().GetDispatcher().Execute( SID_DRAW_NOTEEDIT, SfxCallMode::SYNCHRON | SfxCallMode::RECORD );
505 : // now get the created FuText and set into EditMode
506 0 : FuPoor* pPoor = GetDrawFuncPtr();
507 0 : if ( pPoor && (pPoor->GetSlotID() == SID_DRAW_NOTEEDIT) ) // has no RTTI
508 : {
509 0 : ScrollToObject( pCaption ); // make object fully visible
510 0 : static_cast< FuText* >( pPoor )->SetInEditMode( pCaption );
511 : }
512 : }
513 : }
514 228 : }
515 :
516 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|