File: | home/maarten/src/libreoffice/core/include/rtl/ref.hxx |
Warning: | line 192, column 9 Use of memory after it is freed |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
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 <scitems.hxx> | |||
21 | #include <vcl/settings.hxx> | |||
22 | #include <comphelper/lok.hxx> | |||
23 | ||||
24 | #include <gridwin.hxx> | |||
25 | #include <tabvwsh.hxx> | |||
26 | #include <docsh.hxx> | |||
27 | #include <viewdata.hxx> | |||
28 | #include <pivot.hxx> | |||
29 | #include <uiitems.hxx> | |||
30 | #include <scresid.hxx> | |||
31 | #include <globstr.hrc> | |||
32 | #include <strings.hrc> | |||
33 | #include <pagedata.hxx> | |||
34 | #include <dpobject.hxx> | |||
35 | #include <dpsave.hxx> | |||
36 | #include <dpshttab.hxx> | |||
37 | #include <dbdocfun.hxx> | |||
38 | #include <checklistmenu.hxx> | |||
39 | #include <dpcontrol.hxx> | |||
40 | #include <userlist.hxx> | |||
41 | #include <scabstdlg.hxx> | |||
42 | ||||
43 | #include <com/sun/star/sheet/DataPilotFieldOrientation.hpp> | |||
44 | ||||
45 | #include <unordered_map> | |||
46 | #include <memory> | |||
47 | #include <vector> | |||
48 | ||||
49 | using namespace css; | |||
50 | using namespace css::sheet; | |||
51 | using css::sheet::DataPilotFieldOrientation; | |||
52 | using std::vector; | |||
53 | ||||
54 | DataPilotFieldOrientation ScGridWindow::GetDPFieldOrientation( SCCOL nCol, SCROW nRow ) const | |||
55 | { | |||
56 | ScDocument& rDoc = mrViewData.GetDocument(); | |||
57 | SCTAB nTab = mrViewData.GetTabNo(); | |||
58 | ScDPObject* pDPObj = rDoc.GetDPAtCursor(nCol, nRow, nTab); | |||
59 | if (!pDPObj) | |||
60 | return DataPilotFieldOrientation_HIDDEN; | |||
61 | ||||
62 | DataPilotFieldOrientation nOrient = DataPilotFieldOrientation_HIDDEN; | |||
63 | ||||
64 | // Check for page field first. | |||
65 | if (nCol > 0) | |||
66 | { | |||
67 | // look for the dimension header left of the drop-down arrow | |||
68 | long nField = pDPObj->GetHeaderDim( ScAddress( nCol-1, nRow, nTab ), nOrient ); | |||
69 | if ( nField >= 0 && nOrient == DataPilotFieldOrientation_PAGE ) | |||
70 | { | |||
71 | bool bIsDataLayout = false; | |||
72 | OUString aFieldName = pDPObj->GetDimName( nField, bIsDataLayout ); | |||
73 | if ( !aFieldName.isEmpty() && !bIsDataLayout ) | |||
74 | return DataPilotFieldOrientation_PAGE; | |||
75 | } | |||
76 | } | |||
77 | ||||
78 | nOrient = DataPilotFieldOrientation_HIDDEN; | |||
79 | ||||
80 | // Now, check for row/column field. | |||
81 | long nField = pDPObj->GetHeaderDim(ScAddress(nCol, nRow, nTab), nOrient); | |||
82 | if (nField >= 0 && (nOrient == DataPilotFieldOrientation_COLUMN || nOrient == DataPilotFieldOrientation_ROW) ) | |||
83 | { | |||
84 | bool bIsDataLayout = false; | |||
85 | OUString aFieldName = pDPObj->GetDimName(nField, bIsDataLayout); | |||
86 | if (!aFieldName.isEmpty() && !bIsDataLayout) | |||
87 | return nOrient; | |||
88 | } | |||
89 | ||||
90 | return DataPilotFieldOrientation_HIDDEN; | |||
91 | } | |||
92 | ||||
93 | // private method for mouse button handling | |||
94 | bool ScGridWindow::DoPageFieldSelection( SCCOL nCol, SCROW nRow ) | |||
95 | { | |||
96 | if (GetDPFieldOrientation( nCol, nRow ) == DataPilotFieldOrientation_PAGE) | |||
97 | { | |||
98 | LaunchPageFieldMenu( nCol, nRow ); | |||
99 | return true; | |||
100 | } | |||
101 | return false; | |||
102 | } | |||
103 | ||||
104 | bool ScGridWindow::DoAutoFilterButton( SCCOL nCol, SCROW nRow, const MouseEvent& rMEvt ) | |||
105 | { | |||
106 | ScDocument& rDoc = mrViewData.GetDocument(); | |||
107 | SCTAB nTab = mrViewData.GetTabNo(); | |||
108 | Point aScrPos = mrViewData.GetScrPos(nCol, nRow, eWhich); | |||
109 | Point aDiffPix = rMEvt.GetPosPixel(); | |||
110 | ||||
111 | aDiffPix -= aScrPos; | |||
112 | bool bLayoutRTL = rDoc.IsLayoutRTL( nTab ); | |||
113 | if ( bLayoutRTL ) | |||
114 | aDiffPix.setX( -aDiffPix.X() ); | |||
115 | ||||
116 | long nSizeX, nSizeY; | |||
117 | mrViewData.GetMergeSizePixel( nCol, nRow, nSizeX, nSizeY ); | |||
118 | // The button height should not use the merged cell height, should still use single row height | |||
119 | nSizeY = ScViewData::ToPixel(rDoc.GetRowHeight(nRow, nTab), mrViewData.GetPPTY()); | |||
120 | Size aScrSize(nSizeX-1, nSizeY-1); | |||
121 | ||||
122 | // Check if the mouse cursor is clicking on the popup arrow box. | |||
123 | mpFilterButton.reset(new ScDPFieldButton(this, &GetSettings().GetStyleSettings(), &mrViewData.GetZoomY(), &rDoc)); | |||
124 | mpFilterButton->setBoundingBox(aScrPos, aScrSize, bLayoutRTL); | |||
125 | mpFilterButton->setPopupLeft(bLayoutRTL); // #i114944# AutoFilter button is left-aligned in RTL | |||
126 | Point aPopupPos; | |||
127 | Size aPopupSize; | |||
128 | mpFilterButton->getPopupBoundingBox(aPopupPos, aPopupSize); | |||
129 | tools::Rectangle aRect(aPopupPos, aPopupSize); | |||
130 | if (aRect.IsInside(rMEvt.GetPosPixel())) | |||
131 | { | |||
132 | if ( DoPageFieldSelection( nCol, nRow ) ) | |||
133 | return true; | |||
134 | ||||
135 | bool bFilterActive = IsAutoFilterActive(nCol, nRow, nTab); | |||
136 | mpFilterButton->setHasHiddenMember(bFilterActive); | |||
137 | mpFilterButton->setDrawBaseButton(false); | |||
138 | mpFilterButton->setDrawPopupButton(true); | |||
139 | mpFilterButton->setPopupPressed(true); | |||
140 | mpFilterButton->draw(); | |||
141 | LaunchAutoFilterMenu(nCol, nRow); | |||
142 | return true; | |||
143 | } | |||
144 | ||||
145 | return false; | |||
146 | } | |||
147 | ||||
148 | void ScGridWindow::DoPushPivotButton( SCCOL nCol, SCROW nRow, const MouseEvent& rMEvt, bool bButton, bool bPopup ) | |||
149 | { | |||
150 | ScDocument& rDoc = mrViewData.GetDocument(); | |||
151 | SCTAB nTab = mrViewData.GetTabNo(); | |||
152 | ||||
153 | ScDPObject* pDPObj = rDoc.GetDPAtCursor(nCol, nRow, nTab); | |||
154 | ||||
155 | if (pDPObj) | |||
| ||||
156 | { | |||
157 | DataPilotFieldOrientation nOrient = DataPilotFieldOrientation_HIDDEN; | |||
158 | ScAddress aPos( nCol, nRow, nTab ); | |||
159 | ScAddress aDimPos = aPos; | |||
160 | if (!bButton && bPopup && aDimPos.Col() > 0) | |||
161 | // For page field selection cell, the real field position is to the left. | |||
162 | aDimPos.IncCol(-1); | |||
163 | ||||
164 | long nField = pDPObj->GetHeaderDim(aDimPos, nOrient); | |||
165 | if ( nField >= 0 ) | |||
166 | { | |||
167 | bDPMouse = false; | |||
168 | nDPField = nField; | |||
169 | pDragDPObj = pDPObj; | |||
170 | if (bPopup && DPTestFieldPopupArrow(rMEvt, aPos, aDimPos, pDPObj)) | |||
171 | { | |||
172 | // field name pop up menu has been launched. Don't activate | |||
173 | // field move. | |||
174 | return; | |||
175 | } | |||
176 | ||||
177 | if (bButton) | |||
178 | { | |||
179 | bDPMouse = true; | |||
180 | DPTestMouse( rMEvt, true ); | |||
181 | StartTracking(); | |||
182 | } | |||
183 | } | |||
184 | else if ( pDPObj->IsFilterButton(aPos) ) | |||
185 | { | |||
186 | ReleaseMouse(); // may have been captured in ButtonDown | |||
187 | ||||
188 | ScQueryParam aQueryParam; | |||
189 | SCTAB nSrcTab = 0; | |||
190 | const ScSheetSourceDesc* pDesc = pDPObj->GetSheetDesc(); | |||
191 | OSL_ENSURE(pDesc, "no sheet source for filter button")do { if (true && (!(pDesc))) { sal_detail_logFormat(( SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/sc/source/ui/view/gridwin2.cxx" ":" "191" ": "), "%s", "no sheet source for filter button"); } } while (false); | |||
192 | if (pDesc) | |||
193 | { | |||
194 | aQueryParam = pDesc->GetQueryParam(); | |||
195 | nSrcTab = pDesc->GetSourceRange().aStart.Tab(); | |||
196 | } | |||
197 | ||||
198 | SfxItemSet aArgSet( mrViewData.GetViewShell()->GetPool(), | |||
199 | svl::Items<SCITEM_QUERYDATA1103, SCITEM_QUERYDATA1103>{} ); | |||
200 | aArgSet.Put( ScQueryItem( SCITEM_QUERYDATA1103, &mrViewData, &aQueryParam ) ); | |||
201 | ||||
202 | ScAbstractDialogFactory* pFact = ScAbstractDialogFactory::Create(); | |||
203 | ||||
204 | ScopedVclPtr<AbstractScPivotFilterDlg> pDlg( | |||
205 | pFact->CreateScPivotFilterDlg( | |||
206 | mrViewData.GetViewShell()->GetFrameWeld(), aArgSet, nSrcTab)); | |||
207 | if ( pDlg->Execute() == RET_OK ) | |||
208 | { | |||
209 | ScSheetSourceDesc aNewDesc(&rDoc); | |||
210 | if (pDesc) | |||
211 | aNewDesc = *pDesc; | |||
212 | ||||
213 | const ScQueryItem& rQueryItem = pDlg->GetOutputItem(); | |||
214 | aNewDesc.SetQueryParam(rQueryItem.GetQueryData()); | |||
215 | ||||
216 | ScDPObject aNewObj( *pDPObj ); | |||
217 | aNewObj.SetSheetDesc( aNewDesc ); | |||
218 | ScDBDocFunc aFunc( *mrViewData.GetDocShell() ); | |||
219 | aFunc.DataPilotUpdate( pDPObj, &aNewObj, true, false ); | |||
220 | mrViewData.GetView()->CursorPosChanged(); // shells may be switched | |||
221 | } | |||
222 | } | |||
223 | } | |||
224 | else | |||
225 | { | |||
226 | OSL_FAIL("Nothing here")do { if (true && (((sal_Bool)1))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/sc/source/ui/view/gridwin2.cxx" ":" "226" ": "), "%s", "Nothing here"); } } while (false); | |||
227 | } | |||
228 | } | |||
229 | ||||
230 | // Data Pilot interaction | |||
231 | ||||
232 | void ScGridWindow::DPTestMouse( const MouseEvent& rMEvt, bool bMove ) | |||
233 | { | |||
234 | OSL_ENSURE(pDragDPObj, "pDragDPObj missing")do { if (true && (!(pDragDPObj))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/sc/source/ui/view/gridwin2.cxx" ":" "234" ": "), "%s", "pDragDPObj missing"); } } while (false ); | |||
235 | ||||
236 | // scroll window if at edges | |||
237 | //! move this to separate method | |||
238 | ||||
239 | bool bTimer = false; | |||
240 | Point aPixel = rMEvt.GetPosPixel(); | |||
241 | ||||
242 | SCCOL nDx = 0; | |||
243 | SCROW nDy = 0; | |||
244 | if ( aPixel.X() < 0 ) | |||
245 | nDx = -1; | |||
246 | if ( aPixel.Y() < 0 ) | |||
247 | nDy = -1; | |||
248 | Size aSize = GetOutputSizePixel(); | |||
249 | if ( aPixel.X() >= aSize.Width() ) | |||
250 | nDx = 1; | |||
251 | if ( aPixel.Y() >= aSize.Height() ) | |||
252 | nDy = 1; | |||
253 | if ( nDx != 0 || nDy != 0 ) | |||
254 | { | |||
255 | UpdateDragRect( false, tools::Rectangle() ); | |||
256 | ||||
257 | if ( nDx != 0) | |||
258 | mrViewData.GetView()->ScrollX( nDx, WhichH(eWhich) ); | |||
259 | if ( nDy != 0 ) | |||
260 | mrViewData.GetView()->ScrollY( nDy, WhichV(eWhich) ); | |||
261 | ||||
262 | bTimer = true; | |||
263 | } | |||
264 | ||||
265 | SCCOL nPosX; | |||
266 | SCROW nPosY; | |||
267 | mrViewData.GetPosFromPixel( aPixel.X(), aPixel.Y(), eWhich, nPosX, nPosY ); | |||
268 | bool bMouseLeft; | |||
269 | bool bMouseTop; | |||
270 | mrViewData.GetMouseQuadrant( aPixel, eWhich, nPosX, nPosY, bMouseLeft, bMouseTop ); | |||
271 | ||||
272 | ScAddress aPos( nPosX, nPosY, mrViewData.GetTabNo() ); | |||
273 | ||||
274 | tools::Rectangle aPosRect; | |||
275 | DataPilotFieldOrientation nOrient; | |||
276 | long nDimPos; | |||
277 | bool bHasRange = pDragDPObj->GetHeaderDrag( aPos, bMouseLeft, bMouseTop, nDPField, | |||
278 | aPosRect, nOrient, nDimPos ); | |||
279 | UpdateDragRect( bHasRange && bMove, aPosRect ); | |||
280 | ||||
281 | bool bIsDataLayout; | |||
282 | sal_Int32 nDimFlags = 0; | |||
283 | OUString aDimName = pDragDPObj->GetDimName( nDPField, bIsDataLayout, &nDimFlags ); | |||
284 | bool bAllowed = !bHasRange || ScDPObject::IsOrientationAllowed( nOrient, nDimFlags ); | |||
285 | ||||
286 | if (bMove) // set mouse pointer | |||
287 | { | |||
288 | PointerStyle ePointer = PointerStyle::PivotDelete; | |||
289 | if ( !bAllowed ) | |||
290 | ePointer = PointerStyle::NotAllowed; | |||
291 | else if ( bHasRange ) | |||
292 | switch (nOrient) | |||
293 | { | |||
294 | case DataPilotFieldOrientation_COLUMN: ePointer = PointerStyle::PivotCol; break; | |||
295 | case DataPilotFieldOrientation_ROW: ePointer = PointerStyle::PivotRow; break; | |||
296 | case DataPilotFieldOrientation_PAGE: | |||
297 | case DataPilotFieldOrientation_DATA: ePointer = PointerStyle::PivotField; break; | |||
298 | default: break; | |||
299 | } | |||
300 | SetPointer( ePointer ); | |||
301 | } | |||
302 | else // execute change | |||
303 | { | |||
304 | if (!bHasRange) | |||
305 | nOrient = DataPilotFieldOrientation_HIDDEN; | |||
306 | ||||
307 | if ( bIsDataLayout && ( nOrient != DataPilotFieldOrientation_COLUMN && | |||
308 | nOrient != DataPilotFieldOrientation_ROW ) ) | |||
309 | { | |||
310 | // removing data layout is not allowed | |||
311 | mrViewData.GetView()->ErrorMessage(STR_PIVOT_MOVENOTALLOWEDreinterpret_cast<char const *>("STR_PIVOT_MOVENOTALLOWED" "\004" u8"The data range can not be deleted.")); | |||
312 | } | |||
313 | else if ( bAllowed ) | |||
314 | { | |||
315 | ScDPSaveData aSaveData( *pDragDPObj->GetSaveData() ); | |||
316 | ||||
317 | ScDPSaveDimension* pDim; | |||
318 | if ( bIsDataLayout ) | |||
319 | pDim = aSaveData.GetDataLayoutDimension(); | |||
320 | else | |||
321 | pDim = aSaveData.GetDimensionByName(aDimName); | |||
322 | pDim->SetOrientation( nOrient ); | |||
323 | aSaveData.SetPosition( pDim, nDimPos ); | |||
324 | ||||
325 | //! docfunc method with ScDPSaveData as argument? | |||
326 | ||||
327 | ScDPObject aNewObj( *pDragDPObj ); | |||
328 | aNewObj.SetSaveData( aSaveData ); | |||
329 | ScDBDocFunc aFunc( *mrViewData.GetDocShell() ); | |||
330 | // when dragging fields, allow re-positioning (bAllowMove) | |||
331 | aFunc.DataPilotUpdate( pDragDPObj, &aNewObj, true, false, true ); | |||
332 | mrViewData.GetView()->CursorPosChanged(); // shells may be switched | |||
333 | } | |||
334 | } | |||
335 | ||||
336 | if (bTimer && bMove) | |||
337 | mrViewData.GetView()->SetTimer( this, rMEvt ); // repeat event | |||
338 | else | |||
339 | mrViewData.GetView()->ResetTimer(); | |||
340 | } | |||
341 | ||||
342 | bool ScGridWindow::DPTestFieldPopupArrow( | |||
343 | const MouseEvent& rMEvt, const ScAddress& rPos, const ScAddress& rDimPos, ScDPObject* pDPObj) | |||
344 | { | |||
345 | bool bLayoutRTL = mrViewData.GetDocument().IsLayoutRTL( mrViewData.GetTabNo() ); | |||
346 | ||||
347 | // Get the geometry of the cell. | |||
348 | Point aScrPos = mrViewData.GetScrPos(rPos.Col(), rPos.Row(), eWhich); | |||
349 | long nSizeX, nSizeY; | |||
350 | mrViewData.GetMergeSizePixel(rPos.Col(), rPos.Row(), nSizeX, nSizeY); | |||
351 | Size aScrSize(nSizeX-1, nSizeY-1); | |||
352 | ||||
353 | // Check if the mouse cursor is clicking on the popup arrow box. | |||
354 | ScDPFieldButton aBtn(this, &GetSettings().GetStyleSettings()); | |||
355 | aBtn.setBoundingBox(aScrPos, aScrSize, bLayoutRTL); | |||
356 | aBtn.setPopupLeft(false); // DataPilot popup is always right-aligned for now | |||
357 | Point aPopupPos; | |||
358 | Size aPopupSize; | |||
359 | aBtn.getPopupBoundingBox(aPopupPos, aPopupSize); | |||
360 | tools::Rectangle aRect(aPopupPos, aPopupSize); | |||
361 | if (aRect.IsInside(rMEvt.GetPosPixel())) | |||
362 | { | |||
363 | // Mouse cursor inside the popup arrow box. Launch the field menu. | |||
364 | DPLaunchFieldPopupMenu(OutputToScreenPixel(aScrPos), aScrSize, rDimPos, pDPObj); | |||
365 | return true; | |||
366 | } | |||
367 | ||||
368 | return false; | |||
369 | } | |||
370 | ||||
371 | namespace { | |||
372 | ||||
373 | struct DPFieldPopupData : public ScCheckListMenuControl::ExtendedData | |||
374 | { | |||
375 | ScDPLabelData maLabels; | |||
376 | ScDPObject* mpDPObj; | |||
377 | long mnDim; | |||
378 | }; | |||
379 | ||||
380 | class DPFieldPopupOKAction : public ScCheckListMenuControl::Action | |||
381 | { | |||
382 | public: | |||
383 | explicit DPFieldPopupOKAction(ScGridWindow* p) : | |||
384 | mpGridWindow(p) {} | |||
385 | ||||
386 | virtual void execute() override | |||
387 | { | |||
388 | mpGridWindow->UpdateDPFromFieldPopupMenu(); | |||
389 | } | |||
390 | private: | |||
391 | VclPtr<ScGridWindow> mpGridWindow; | |||
392 | }; | |||
393 | ||||
394 | class PopupSortAction : public ScCheckListMenuControl::Action | |||
395 | { | |||
396 | public: | |||
397 | enum SortType { ASCENDING, DESCENDING, CUSTOM }; | |||
398 | ||||
399 | explicit PopupSortAction(ScDPObject* pDPObject, long nDimIndex, SortType eType, | |||
400 | sal_uInt16 nUserListIndex, ScTabViewShell* pViewShell) | |||
401 | : mpDPObject(pDPObject) | |||
402 | , mnDimIndex(nDimIndex) | |||
403 | , meType(eType) | |||
404 | , mnUserListIndex(nUserListIndex) | |||
405 | , mpViewShell(pViewShell) | |||
406 | {} | |||
407 | ||||
408 | virtual void execute() override | |||
409 | { | |||
410 | switch (meType) | |||
411 | { | |||
412 | case ASCENDING: | |||
413 | mpViewShell->DataPilotSort(mpDPObject, mnDimIndex, true); | |||
414 | break; | |||
415 | case DESCENDING: | |||
416 | mpViewShell->DataPilotSort(mpDPObject, mnDimIndex, false); | |||
417 | break; | |||
418 | case CUSTOM: | |||
419 | mpViewShell->DataPilotSort(mpDPObject, mnDimIndex, true, &mnUserListIndex); | |||
420 | break; | |||
421 | default: | |||
422 | ; | |||
423 | } | |||
424 | } | |||
425 | ||||
426 | private: | |||
427 | ScDPObject* mpDPObject; | |||
428 | long mnDimIndex; | |||
429 | SortType meType; | |||
430 | sal_uInt16 mnUserListIndex; | |||
431 | ScTabViewShell* mpViewShell; | |||
432 | }; | |||
433 | ||||
434 | } | |||
435 | ||||
436 | void ScGridWindow::DPLaunchFieldPopupMenu(const Point& rScreenPosition, const Size& rScreenSize, | |||
437 | const ScAddress& rAddress, ScDPObject* pDPObject) | |||
438 | { | |||
439 | DataPilotFieldOrientation nOrient; | |||
440 | long nDimIndex = pDPObject->GetHeaderDim(rAddress, nOrient); | |||
441 | ||||
442 | DPLaunchFieldPopupMenu(rScreenPosition, rScreenSize, nDimIndex, pDPObject); | |||
443 | } | |||
444 | ||||
445 | void ScGridWindow::DPLaunchFieldPopupMenu(const Point& rScrPos, const Size& rScrSize, | |||
446 | long nDimIndex, ScDPObject* pDPObj) | |||
447 | { | |||
448 | std::unique_ptr<DPFieldPopupData> pDPData(new DPFieldPopupData); | |||
449 | pDPData->mnDim = nDimIndex; | |||
450 | pDPObj->GetSource(); | |||
451 | ||||
452 | bool bIsDataLayout; | |||
453 | OUString aDimName = pDPObj->GetDimName(pDPData->mnDim, bIsDataLayout); | |||
454 | pDPObj->BuildAllDimensionMembers(); | |||
455 | const ScDPSaveData* pSaveData = pDPObj->GetSaveData(); | |||
456 | const ScDPSaveDimension* pDim = pSaveData->GetExistingDimensionByName(aDimName); | |||
457 | if (!pDim) | |||
458 | // This should never happen. | |||
459 | return; | |||
460 | ||||
461 | bool bDimOrientNotPage = pDim->GetOrientation() != DataPilotFieldOrientation_PAGE; | |||
462 | ||||
463 | // We need to get the list of field members. | |||
464 | pDPObj->FillLabelData(pDPData->mnDim, pDPData->maLabels); | |||
465 | pDPData->mpDPObj = pDPObj; | |||
466 | ||||
467 | const ScDPLabelData& rLabelData = pDPData->maLabels; | |||
468 | ||||
469 | mpDPFieldPopup.disposeAndClear(); | |||
470 | mpDPFieldPopup.reset(VclPtr<ScCheckListMenuWindow>::Create(this, &mrViewData.GetDocument(), | |||
471 | bDimOrientNotPage, false)); | |||
472 | ||||
473 | ScCheckListMenuControl& rControl = mpDPFieldPopup->get_widget(); | |||
474 | rControl.setExtendedData(std::move(pDPData)); | |||
475 | rControl.setOKAction(new DPFieldPopupOKAction(this)); | |||
476 | { | |||
477 | // Populate field members. | |||
478 | size_t n = rLabelData.maMembers.size(); | |||
479 | rControl.setMemberSize(n); | |||
480 | for (size_t i = 0; i < n; ++i) | |||
481 | { | |||
482 | const ScDPLabelData::Member& rMem = rLabelData.maMembers[i]; | |||
483 | OUString aName = rMem.getDisplayName(); | |||
484 | if (aName.isEmpty()) | |||
485 | // Use special string for an empty name. | |||
486 | rControl.addMember(ScResId(STR_EMPTYDATAreinterpret_cast<char const *>("STR_EMPTYDATA" "\004" u8"(empty)" )), rMem.mbVisible); | |||
487 | else | |||
488 | rControl.addMember(rMem.getDisplayName(), rMem.mbVisible); | |||
489 | } | |||
490 | } | |||
491 | ||||
492 | if (bDimOrientNotPage) | |||
493 | { | |||
494 | vector<OUString> aUserSortNames; | |||
495 | ScUserList* pUserList = ScGlobal::GetUserList(); | |||
496 | if (pUserList) | |||
497 | { | |||
498 | size_t n = pUserList->size(); | |||
499 | aUserSortNames.reserve(n); | |||
500 | for (size_t i = 0; i < n; ++i) | |||
501 | { | |||
502 | const ScUserListData& rData = (*pUserList)[i]; | |||
503 | aUserSortNames.push_back(rData.GetString()); | |||
504 | } | |||
505 | } | |||
506 | ||||
507 | // Populate the menus. | |||
508 | ScTabViewShell* pViewShell = mrViewData.GetViewShell(); | |||
509 | rControl.addMenuItem( | |||
510 | ScResId(STR_MENU_SORT_ASCreinterpret_cast<char const *>("STR_MENU_SORT_ASC" "\004" u8"Sort Ascending")), | |||
511 | new PopupSortAction(pDPObj, nDimIndex, PopupSortAction::ASCENDING, 0, pViewShell)); | |||
512 | rControl.addMenuItem( | |||
513 | ScResId(STR_MENU_SORT_DESCreinterpret_cast<char const *>("STR_MENU_SORT_DESC" "\004" u8"Sort Descending")), | |||
514 | new PopupSortAction(pDPObj, nDimIndex, PopupSortAction::DESCENDING, 0, pViewShell)); | |||
515 | ||||
516 | ScCheckListMenuWindow* pSubMenu = rControl.addSubMenuItem(ScResId(STR_MENU_SORT_CUSTOMreinterpret_cast<char const *>("STR_MENU_SORT_CUSTOM" "\004" u8"Custom Sort")), !aUserSortNames.empty()); | |||
517 | if (pSubMenu) | |||
518 | { | |||
519 | ScCheckListMenuControl& rSubMenu = pSubMenu->get_widget(); | |||
520 | size_t n = aUserSortNames.size(); | |||
521 | for (size_t i = 0; i < n; ++i) | |||
522 | { | |||
523 | rSubMenu.addMenuItem(aUserSortNames[i], | |||
524 | new PopupSortAction(pDPObj, nDimIndex, PopupSortAction::CUSTOM, sal_uInt16(i), pViewShell)); | |||
525 | } | |||
526 | rSubMenu.resizeToFitMenuItems(); | |||
527 | } | |||
528 | } | |||
529 | ||||
530 | rControl.initMembers(); | |||
531 | ||||
532 | tools::Rectangle aCellRect(rScrPos, rScrSize); | |||
533 | ||||
534 | ScCheckListMenuControl::Config aConfig; | |||
535 | aConfig.mbAllowEmptySet = false; | |||
536 | aConfig.mbRTL = mrViewData.GetDocument().IsLayoutRTL(mrViewData.GetTabNo()); | |||
537 | rControl.setConfig(aConfig); | |||
538 | if (IsMouseCaptured()) | |||
539 | ReleaseMouse(); | |||
540 | rControl.launch(aCellRect); | |||
541 | } | |||
542 | ||||
543 | void ScGridWindow::UpdateDPFromFieldPopupMenu() | |||
544 | { | |||
545 | typedef std::unordered_map<OUString, OUString> MemNameMapType; | |||
546 | ||||
547 | if (!mpDPFieldPopup) | |||
548 | return; | |||
549 | ||||
550 | ScCheckListMenuControl& rControl = mpDPFieldPopup->get_widget(); | |||
551 | ||||
552 | DPFieldPopupData* pDPData = static_cast<DPFieldPopupData*>(rControl.getExtendedData()); | |||
553 | if (!pDPData) | |||
554 | return; | |||
555 | ||||
556 | ScDPObject* pDPObj = pDPData->mpDPObj; | |||
557 | ScDPSaveData* pSaveData = pDPObj->GetSaveData(); | |||
558 | ||||
559 | bool bIsDataLayout; | |||
560 | OUString aDimName = pDPObj->GetDimName(pDPData->mnDim, bIsDataLayout); | |||
561 | ScDPSaveDimension* pDim = pSaveData->GetDimensionByName(aDimName); | |||
562 | if (!pDim) | |||
563 | return; | |||
564 | ||||
565 | // Build a map of layout names to original names. | |||
566 | const ScDPLabelData& rLabelData = pDPData->maLabels; | |||
567 | MemNameMapType aMemNameMap; | |||
568 | for (const auto& rMember : rLabelData.maMembers) | |||
569 | aMemNameMap.emplace(rMember.maLayoutName, rMember.maName); | |||
570 | ||||
571 | // The raw result may contain a mixture of layout names and original names. | |||
572 | ScCheckListMenuControl::ResultType aRawResult; | |||
573 | rControl.getResult(aRawResult); | |||
574 | ||||
575 | std::unordered_map<OUString, bool> aResult; | |||
576 | for (const auto& rItem : aRawResult) | |||
577 | { | |||
578 | MemNameMapType::const_iterator itrNameMap = aMemNameMap.find(rItem.aName); | |||
579 | if (itrNameMap == aMemNameMap.end()) | |||
580 | { | |||
581 | // This is an original member name. Use it as-is. | |||
582 | OUString aName = rItem.aName; | |||
583 | if (aName == ScResId(STR_EMPTYDATAreinterpret_cast<char const *>("STR_EMPTYDATA" "\004" u8"(empty)" ))) | |||
584 | // Translate the special empty name into an empty string. | |||
585 | aName.clear(); | |||
586 | ||||
587 | aResult.emplace(aName, rItem.bValid); | |||
588 | } | |||
589 | else | |||
590 | { | |||
591 | // This is a layout name. Get the original member name and use it. | |||
592 | aResult.emplace(itrNameMap->second, rItem.bValid); | |||
593 | } | |||
594 | } | |||
595 | pDim->UpdateMemberVisibility(aResult); | |||
596 | ||||
597 | ScDBDocFunc aFunc(*mrViewData.GetDocShell()); | |||
598 | aFunc.UpdatePivotTable(*pDPObj, true, false); | |||
599 | } | |||
600 | ||||
601 | namespace { | |||
602 | ||||
603 | template <typename T> | |||
604 | inline | |||
605 | T lcl_getValidValue(T value, T defvalue) | |||
606 | { | |||
607 | return (value <0) ? defvalue : value; | |||
608 | } | |||
609 | ||||
610 | } // anonymous namespace | |||
611 | ||||
612 | bool ScGridWindow::UpdateVisibleRange() | |||
613 | { | |||
614 | ScDocument const& rDoc = mrViewData.GetDocument(); | |||
615 | SCCOL nPosX = 0; | |||
616 | SCROW nPosY = 0; | |||
617 | SCCOL nXRight = rDoc.MaxCol(); | |||
618 | SCROW nYBottom = rDoc.MaxRow(); | |||
619 | ||||
620 | if (comphelper::LibreOfficeKit::isActive()) | |||
621 | { | |||
622 | ScTabViewShell* pViewShell = mrViewData.GetViewShell(); | |||
623 | nPosX = lcl_getValidValue(pViewShell->GetLOKStartHeaderCol(), nPosX); | |||
624 | nPosY = lcl_getValidValue(pViewShell->GetLOKStartHeaderRow(), nPosY); | |||
625 | nXRight = lcl_getValidValue(pViewShell->GetLOKEndHeaderCol(), nXRight); | |||
626 | nYBottom = lcl_getValidValue(pViewShell->GetLOKEndHeaderRow(), nYBottom); | |||
627 | } | |||
628 | else | |||
629 | { | |||
630 | nPosX = mrViewData.GetPosX(eHWhich); | |||
631 | nPosY = mrViewData.GetPosY(eVWhich); | |||
632 | nXRight = nPosX + mrViewData.VisibleCellsX(eHWhich); | |||
633 | if (nXRight > rDoc.MaxCol()) | |||
634 | nXRight = rDoc.MaxCol(); | |||
635 | nYBottom = nPosY + mrViewData.VisibleCellsY(eVWhich); | |||
636 | if (nYBottom > rDoc.MaxRow()) | |||
637 | nYBottom = rDoc.MaxRow(); | |||
638 | } | |||
639 | ||||
640 | // Store the current visible range. | |||
641 | bool bChanged = maVisibleRange.set(nPosX, nPosY, nXRight, nYBottom); | |||
642 | if (bChanged) | |||
643 | ResetAutoSpell(); | |||
644 | ||||
645 | return bChanged; | |||
646 | } | |||
647 | ||||
648 | void ScGridWindow::DPMouseMove( const MouseEvent& rMEvt ) | |||
649 | { | |||
650 | DPTestMouse( rMEvt, true ); | |||
651 | } | |||
652 | ||||
653 | void ScGridWindow::DPMouseButtonUp( const MouseEvent& rMEvt ) | |||
654 | { | |||
655 | bDPMouse = false; | |||
656 | ReleaseMouse(); | |||
657 | ||||
658 | DPTestMouse( rMEvt, false ); | |||
659 | SetPointer( PointerStyle::Arrow ); | |||
660 | } | |||
661 | ||||
662 | void ScGridWindow::UpdateDragRect( bool bShowRange, const tools::Rectangle& rPosRect ) | |||
663 | { | |||
664 | SCCOL nStartX = ( rPosRect.Left() >= 0 ) ? static_cast<SCCOL>(rPosRect.Left()) : SCCOL_MAX; | |||
665 | SCROW nStartY = ( rPosRect.Top() >= 0 ) ? static_cast<SCROW>(rPosRect.Top()) : SCROW_MAX; | |||
666 | SCCOL nEndX = ( rPosRect.Right() >= 0 ) ? static_cast<SCCOL>(rPosRect.Right()) : SCCOL_MAX; | |||
667 | SCROW nEndY = ( rPosRect.Bottom() >= 0 ) ? static_cast<SCROW>(rPosRect.Bottom()) : SCROW_MAX; | |||
668 | ||||
669 | if ( bShowRange == bDragRect && nDragStartX == nStartX && nDragEndX == nEndX && | |||
670 | nDragStartY == nStartY && nDragEndY == nEndY ) | |||
671 | { | |||
672 | return; // everything unchanged | |||
673 | } | |||
674 | ||||
675 | if ( bShowRange ) | |||
676 | { | |||
677 | nDragStartX = nStartX; | |||
678 | nDragStartY = nStartY; | |||
679 | nDragEndX = nEndX; | |||
680 | nDragEndY = nEndY; | |||
681 | bDragRect = true; | |||
682 | } | |||
683 | else | |||
684 | bDragRect = false; | |||
685 | ||||
686 | UpdateDragRectOverlay(); | |||
687 | } | |||
688 | ||||
689 | // Page-Break Mode | |||
690 | ||||
691 | sal_uInt16 ScGridWindow::HitPageBreak( const Point& rMouse, ScRange* pSource, | |||
692 | SCCOLROW* pBreak, SCCOLROW* pPrev ) | |||
693 | { | |||
694 | sal_uInt16 nFound = SC_PD_NONE0; // 0 | |||
695 | ScRange aSource; | |||
696 | SCCOLROW nBreak = 0; | |||
697 | SCCOLROW nPrev = 0; | |||
698 | ||||
699 | ScPageBreakData* pPageData = mrViewData.GetView()->GetPageBreakData(); | |||
700 | if ( pPageData ) | |||
701 | { | |||
702 | bool bHori = false; | |||
703 | bool bVert = false; | |||
704 | SCCOL nHitX = 0; | |||
705 | SCROW nHitY = 0; | |||
706 | ||||
707 | long nMouseX = rMouse.X(); | |||
708 | long nMouseY = rMouse.Y(); | |||
709 | SCCOL nPosX; | |||
710 | SCROW nPosY; | |||
711 | mrViewData.GetPosFromPixel( nMouseX, nMouseY, eWhich, nPosX, nPosY ); | |||
712 | Point aTL = mrViewData.GetScrPos( nPosX, nPosY, eWhich ); | |||
713 | Point aBR = mrViewData.GetScrPos( nPosX+1, nPosY+1, eWhich ); | |||
714 | ||||
715 | // Horizontal more tolerances as for vertical, because there is more space | |||
716 | if ( nMouseX <= aTL.X() + 4 ) | |||
717 | { | |||
718 | bHori = true; | |||
719 | nHitX = nPosX; | |||
720 | } | |||
721 | else if ( nMouseX >= aBR.X() - 6 ) | |||
722 | { | |||
723 | bHori = true; | |||
724 | nHitX = nPosX+1; // left edge of the next cell | |||
725 | } | |||
726 | if ( nMouseY <= aTL.Y() + 2 ) | |||
727 | { | |||
728 | bVert = true; | |||
729 | nHitY = nPosY; | |||
730 | } | |||
731 | else if ( nMouseY >= aBR.Y() - 4 ) | |||
732 | { | |||
733 | bVert = true; | |||
734 | nHitY = nPosY+1; // upper edge of the next cell | |||
735 | } | |||
736 | ||||
737 | if ( bHori || bVert ) | |||
738 | { | |||
739 | sal_uInt16 nCount = sal::static_int_cast<sal_uInt16>( pPageData->GetCount() ); | |||
740 | for (sal_uInt16 nPos=0; nPos<nCount && !nFound; nPos++) | |||
741 | { | |||
742 | ScPrintRangeData& rData = pPageData->GetData(nPos); | |||
743 | ScRange aRange = rData.GetPrintRange(); | |||
744 | bool bLHit = ( bHori && nHitX == aRange.aStart.Col() ); | |||
745 | bool bRHit = ( bHori && nHitX == aRange.aEnd.Col() + 1 ); | |||
746 | bool bTHit = ( bVert && nHitY == aRange.aStart.Row() ); | |||
747 | bool bBHit = ( bVert && nHitY == aRange.aEnd.Row() + 1 ); | |||
748 | bool bInsideH = ( nPosX >= aRange.aStart.Col() && nPosX <= aRange.aEnd.Col() ); | |||
749 | bool bInsideV = ( nPosY >= aRange.aStart.Row() && nPosY <= aRange.aEnd.Row() ); | |||
750 | ||||
751 | if ( bLHit ) | |||
752 | { | |||
753 | if ( bTHit ) | |||
754 | nFound = SC_PD_RANGE_TL(4|1); | |||
755 | else if ( bBHit ) | |||
756 | nFound = SC_PD_RANGE_BL(8|1); | |||
757 | else if ( bInsideV ) | |||
758 | nFound = SC_PD_RANGE_L1; | |||
759 | } | |||
760 | else if ( bRHit ) | |||
761 | { | |||
762 | if ( bTHit ) | |||
763 | nFound = SC_PD_RANGE_TR(4|2); | |||
764 | else if ( bBHit ) | |||
765 | nFound = SC_PD_RANGE_BR(8|2); | |||
766 | else if ( bInsideV ) | |||
767 | nFound = SC_PD_RANGE_R2; | |||
768 | } | |||
769 | else if ( bTHit && bInsideH ) | |||
770 | nFound = SC_PD_RANGE_T4; | |||
771 | else if ( bBHit && bInsideH ) | |||
772 | nFound = SC_PD_RANGE_B8; | |||
773 | if (nFound) | |||
774 | aSource = aRange; | |||
775 | ||||
776 | // breaks | |||
777 | ||||
778 | if ( bVert && bInsideH && !nFound ) | |||
779 | { | |||
780 | size_t nRowCount = rData.GetPagesY(); | |||
781 | const SCROW* pRowEnd = rData.GetPageEndY(); | |||
782 | for (size_t nRowPos=0; nRowPos+1<nRowCount; nRowPos++) | |||
783 | if ( pRowEnd[nRowPos]+1 == nHitY ) | |||
784 | { | |||
785 | nFound = SC_PD_BREAK_V32; | |||
786 | aSource = aRange; | |||
787 | nBreak = nHitY; | |||
788 | if ( nRowPos ) | |||
789 | nPrev = pRowEnd[nRowPos-1]+1; | |||
790 | else | |||
791 | nPrev = aRange.aStart.Row(); | |||
792 | } | |||
793 | } | |||
794 | if ( bHori && bInsideV && !nFound ) | |||
795 | { | |||
796 | size_t nColCount = rData.GetPagesX(); | |||
797 | const SCCOL* pColEnd = rData.GetPageEndX(); | |||
798 | for (size_t nColPos=0; nColPos+1<nColCount; nColPos++) | |||
799 | if ( pColEnd[nColPos]+1 == nHitX ) | |||
800 | { | |||
801 | nFound = SC_PD_BREAK_H16; | |||
802 | aSource = aRange; | |||
803 | nBreak = nHitX; | |||
804 | if ( nColPos ) | |||
805 | nPrev = pColEnd[nColPos-1]+1; | |||
806 | else | |||
807 | nPrev = aRange.aStart.Col(); | |||
808 | } | |||
809 | } | |||
810 | } | |||
811 | } | |||
812 | } | |||
813 | ||||
814 | if (pSource) | |||
815 | *pSource = aSource; // print break | |||
816 | if (pBreak) | |||
817 | *pBreak = nBreak; // X/Y position of the moved page break | |||
818 | if (pPrev) | |||
819 | *pPrev = nPrev; // X/Y beginning of the page, which is above the break | |||
820 | return nFound; | |||
821 | } | |||
822 | ||||
823 | void ScGridWindow::PagebreakMove( const MouseEvent& rMEvt, bool bUp ) | |||
824 | { | |||
825 | //! Combine scrolling and switching with RFMouseMove ! | |||
826 | //! (Inverting before scrolling is different) | |||
827 | ||||
828 | // Scrolling | |||
829 | ||||
830 | bool bTimer = false; | |||
831 | Point aPos = rMEvt.GetPosPixel(); | |||
832 | SCCOL nDx = 0; | |||
833 | SCROW nDy = 0; | |||
834 | if ( aPos.X() < 0 ) nDx = -1; | |||
835 | if ( aPos.Y() < 0 ) nDy = -1; | |||
836 | Size aSize = GetOutputSizePixel(); | |||
837 | if ( aPos.X() >= aSize.Width() ) | |||
838 | nDx = 1; | |||
839 | if ( aPos.Y() >= aSize.Height() ) | |||
840 | nDy = 1; | |||
841 | if ( nDx != 0 || nDy != 0 ) | |||
842 | { | |||
843 | if ( bPagebreakDrawn ) // invert | |||
844 | { | |||
845 | bPagebreakDrawn = false; | |||
846 | UpdateDragRectOverlay(); | |||
847 | } | |||
848 | ||||
849 | if ( nDx != 0 ) mrViewData.GetView()->ScrollX( nDx, WhichH(eWhich) ); | |||
850 | if ( nDy != 0 ) mrViewData.GetView()->ScrollY( nDy, WhichV(eWhich) ); | |||
851 | bTimer = true; | |||
852 | } | |||
853 | ||||
854 | // Switching when fixating (so Scrolling works) | |||
855 | ||||
856 | if ( eWhich == mrViewData.GetActivePart() ) //?? | |||
857 | { | |||
858 | if ( mrViewData.GetHSplitMode() == SC_SPLIT_FIX ) | |||
859 | if ( nDx > 0 ) | |||
860 | { | |||
861 | if ( eWhich == SC_SPLIT_TOPLEFT ) | |||
862 | mrViewData.GetView()->ActivatePart( SC_SPLIT_TOPRIGHT ); | |||
863 | else if ( eWhich == SC_SPLIT_BOTTOMLEFT ) | |||
864 | mrViewData.GetView()->ActivatePart( SC_SPLIT_BOTTOMRIGHT ); | |||
865 | } | |||
866 | ||||
867 | if ( mrViewData.GetVSplitMode() == SC_SPLIT_FIX ) | |||
868 | if ( nDy > 0 ) | |||
869 | { | |||
870 | if ( eWhich == SC_SPLIT_TOPLEFT ) | |||
871 | mrViewData.GetView()->ActivatePart( SC_SPLIT_BOTTOMLEFT ); | |||
872 | else if ( eWhich == SC_SPLIT_TOPRIGHT ) | |||
873 | mrViewData.GetView()->ActivatePart( SC_SPLIT_BOTTOMRIGHT ); | |||
874 | } | |||
875 | } | |||
876 | ||||
877 | // from here new | |||
878 | ||||
879 | // Searching for a position between the cells (before nPosX / nPosY) | |||
880 | SCCOL nPosX; | |||
881 | SCROW nPosY; | |||
882 | mrViewData.GetPosFromPixel( aPos.X(), aPos.Y(), eWhich, nPosX, nPosY ); | |||
883 | bool bLeft, bTop; | |||
884 | mrViewData.GetMouseQuadrant( aPos, eWhich, nPosX, nPosY, bLeft, bTop ); | |||
885 | if ( !bLeft ) ++nPosX; | |||
886 | if ( !bTop ) ++nPosY; | |||
887 | ||||
888 | bool bBreak = ( nPagebreakMouse == SC_PD_BREAK_H16 || nPagebreakMouse == SC_PD_BREAK_V32 ); | |||
889 | bool bHide = false; | |||
890 | bool bToEnd = false; | |||
891 | ScRange aDrawRange = aPagebreakSource; | |||
892 | if ( bBreak ) | |||
893 | { | |||
894 | if ( nPagebreakMouse == SC_PD_BREAK_H16 ) | |||
895 | { | |||
896 | if ( nPosX > aPagebreakSource.aStart.Col() && | |||
897 | nPosX <= aPagebreakSource.aEnd.Col() + 1 ) // to the end is also allowed | |||
898 | { | |||
899 | bToEnd = ( nPosX == aPagebreakSource.aEnd.Col() + 1 ); | |||
900 | aDrawRange.aStart.SetCol( nPosX ); | |||
901 | aDrawRange.aEnd.SetCol( nPosX - 1 ); | |||
902 | } | |||
903 | else | |||
904 | bHide = true; | |||
905 | } | |||
906 | else | |||
907 | { | |||
908 | if ( nPosY > aPagebreakSource.aStart.Row() && | |||
909 | nPosY <= aPagebreakSource.aEnd.Row() + 1 ) // to the end is also allowed | |||
910 | { | |||
911 | bToEnd = ( nPosY == aPagebreakSource.aEnd.Row() + 1 ); | |||
912 | aDrawRange.aStart.SetRow( nPosY ); | |||
913 | aDrawRange.aEnd.SetRow( nPosY - 1 ); | |||
914 | } | |||
915 | else | |||
916 | bHide = true; | |||
917 | } | |||
918 | } | |||
919 | else | |||
920 | { | |||
921 | if ( nPagebreakMouse & SC_PD_RANGE_L1 ) | |||
922 | aDrawRange.aStart.SetCol( nPosX ); | |||
923 | if ( nPagebreakMouse & SC_PD_RANGE_T4 ) | |||
924 | aDrawRange.aStart.SetRow( nPosY ); | |||
925 | if ( nPagebreakMouse & SC_PD_RANGE_R2 ) | |||
926 | { | |||
927 | if ( nPosX > 0 ) | |||
928 | aDrawRange.aEnd.SetCol( nPosX-1 ); | |||
929 | else | |||
930 | bHide = true; | |||
931 | } | |||
932 | if ( nPagebreakMouse & SC_PD_RANGE_B8 ) | |||
933 | { | |||
934 | if ( nPosY > 0 ) | |||
935 | aDrawRange.aEnd.SetRow( nPosY-1 ); | |||
936 | else | |||
937 | bHide = true; | |||
938 | } | |||
939 | if ( aDrawRange.aStart.Col() > aDrawRange.aEnd.Col() || | |||
940 | aDrawRange.aStart.Row() > aDrawRange.aEnd.Row() ) | |||
941 | bHide = true; | |||
942 | } | |||
943 | ||||
944 | if ( !bPagebreakDrawn || bUp || aDrawRange != aPagebreakDrag ) | |||
945 | { | |||
946 | // draw... | |||
947 | ||||
948 | if ( bPagebreakDrawn ) | |||
949 | { | |||
950 | // invert | |||
951 | bPagebreakDrawn = false; | |||
952 | } | |||
953 | aPagebreakDrag = aDrawRange; | |||
954 | if ( !bUp && !bHide ) | |||
955 | { | |||
956 | // revert | |||
957 | bPagebreakDrawn = true; | |||
958 | } | |||
959 | UpdateDragRectOverlay(); | |||
960 | } | |||
961 | ||||
962 | // when ButtonUp execute the changes | |||
963 | ||||
964 | if ( bUp ) | |||
965 | { | |||
966 | ScViewFunc* pViewFunc = mrViewData.GetView(); | |||
967 | ScDocShell* pDocSh = mrViewData.GetDocShell(); | |||
968 | ScDocument& rDoc = pDocSh->GetDocument(); | |||
969 | SCTAB nTab = mrViewData.GetTabNo(); | |||
970 | bool bUndo (rDoc.IsUndoEnabled()); | |||
971 | ||||
972 | if ( bBreak ) | |||
973 | { | |||
974 | bool bColumn = ( nPagebreakMouse == SC_PD_BREAK_H16 ); | |||
975 | SCCOLROW nNew = bColumn ? static_cast<SCCOLROW>(nPosX) : static_cast<SCCOLROW>(nPosY); | |||
976 | if ( nNew != nPagebreakBreak ) | |||
977 | { | |||
978 | if (bUndo) | |||
979 | { | |||
980 | OUString aUndo = ScResId( STR_UNDO_DRAG_BREAKreinterpret_cast<char const *>("STR_UNDO_DRAG_BREAK" "\004" u8"Move Page Break") ); | |||
981 | pDocSh->GetUndoManager()->EnterListAction( aUndo, aUndo, 0, mrViewData.GetViewShell()->GetViewShellId() ); | |||
982 | } | |||
983 | ||||
984 | bool bGrow = !bHide && nNew > nPagebreakBreak; | |||
985 | if ( bColumn ) | |||
986 | { | |||
987 | if (rDoc.HasColBreak(static_cast<SCCOL>(nPagebreakBreak), nTab) & ScBreakType::Manual) | |||
988 | { | |||
989 | ScAddress aOldAddr( static_cast<SCCOL>(nPagebreakBreak), nPosY, nTab ); | |||
990 | pViewFunc->DeletePageBreak( true, true, &aOldAddr, false ); | |||
991 | } | |||
992 | if ( !bHide && !bToEnd ) // not at the end | |||
993 | { | |||
994 | ScAddress aNewAddr( static_cast<SCCOL>(nNew), nPosY, nTab ); | |||
995 | pViewFunc->InsertPageBreak( true, true, &aNewAddr, false ); | |||
996 | } | |||
997 | if ( bGrow ) | |||
998 | { | |||
999 | // change last break to hard, and change scaling | |||
1000 | bool bManualBreak(rDoc.HasColBreak(static_cast<SCCOL>(nPagebreakPrev), nTab) & ScBreakType::Manual); | |||
1001 | if ( static_cast<SCCOL>(nPagebreakPrev) > aPagebreakSource.aStart.Col() && !bManualBreak ) | |||
1002 | { | |||
1003 | ScAddress aPrev( static_cast<SCCOL>(nPagebreakPrev), nPosY, nTab ); | |||
1004 | pViewFunc->InsertPageBreak( true, true, &aPrev, false ); | |||
1005 | } | |||
1006 | ||||
1007 | if (!pDocSh->AdjustPrintZoom( ScRange( | |||
1008 | static_cast<SCCOL>(nPagebreakPrev),0,nTab, static_cast<SCCOL>(nNew-1),0,nTab ) )) | |||
1009 | bGrow = false; | |||
1010 | } | |||
1011 | } | |||
1012 | else | |||
1013 | { | |||
1014 | if (rDoc.HasRowBreak(nPagebreakBreak, nTab) & ScBreakType::Manual) | |||
1015 | { | |||
1016 | ScAddress aOldAddr( nPosX, nPagebreakBreak, nTab ); | |||
1017 | pViewFunc->DeletePageBreak( false, true, &aOldAddr, false ); | |||
1018 | } | |||
1019 | if ( !bHide && !bToEnd ) // not at the end | |||
1020 | { | |||
1021 | ScAddress aNewAddr( nPosX, nNew, nTab ); | |||
1022 | pViewFunc->InsertPageBreak( false, true, &aNewAddr, false ); | |||
1023 | } | |||
1024 | if ( bGrow ) | |||
1025 | { | |||
1026 | // change last break to hard, and change scaling | |||
1027 | bool bManualBreak(rDoc.HasRowBreak(nPagebreakPrev, nTab) & ScBreakType::Manual); | |||
1028 | if ( nPagebreakPrev > aPagebreakSource.aStart.Row() && !bManualBreak ) | |||
1029 | { | |||
1030 | ScAddress aPrev( nPosX, nPagebreakPrev, nTab ); | |||
1031 | pViewFunc->InsertPageBreak( false, true, &aPrev, false ); | |||
1032 | } | |||
1033 | ||||
1034 | if (!pDocSh->AdjustPrintZoom( ScRange( | |||
1035 | 0,nPagebreakPrev,nTab, 0,nNew-1,nTab ) )) | |||
1036 | bGrow = false; | |||
1037 | } | |||
1038 | } | |||
1039 | ||||
1040 | if (bUndo) | |||
1041 | { | |||
1042 | pDocSh->GetUndoManager()->LeaveListAction(); | |||
1043 | } | |||
1044 | ||||
1045 | if (!bGrow) // otherwise has already happened in AdjustPrintZoom | |||
1046 | { | |||
1047 | pViewFunc->UpdatePageBreakData( true ); | |||
1048 | pDocSh->SetDocumentModified(); | |||
1049 | } | |||
1050 | } | |||
1051 | } | |||
1052 | else if ( bHide || aPagebreakDrag != aPagebreakSource ) | |||
1053 | { | |||
1054 | // set print range | |||
1055 | ||||
1056 | OUString aNewRanges; | |||
1057 | sal_uInt16 nOldCount = rDoc.GetPrintRangeCount( nTab ); | |||
1058 | if ( nOldCount ) | |||
1059 | { | |||
1060 | for (sal_uInt16 nPos=0; nPos<nOldCount; nPos++) | |||
1061 | { | |||
1062 | const ScRange* pOld = rDoc.GetPrintRange( nTab, nPos ); | |||
1063 | if ( pOld ) | |||
1064 | { | |||
1065 | OUString aTemp; | |||
1066 | if ( *pOld != aPagebreakSource ) | |||
1067 | aTemp = pOld->Format(rDoc, ScRefFlags::VALID); | |||
1068 | else if ( !bHide ) | |||
1069 | aTemp = aPagebreakDrag.Format(rDoc, ScRefFlags::VALID); | |||
1070 | if (!aTemp.isEmpty()) | |||
1071 | { | |||
1072 | if ( !aNewRanges.isEmpty() ) | |||
1073 | aNewRanges += ";"; | |||
1074 | aNewRanges += aTemp; | |||
1075 | } | |||
1076 | } | |||
1077 | } | |||
1078 | } | |||
1079 | else if (!bHide) | |||
1080 | aNewRanges = aPagebreakDrag.Format(rDoc, ScRefFlags::VALID); | |||
1081 | ||||
1082 | pViewFunc->SetPrintRanges( rDoc.IsPrintEntireSheet( nTab ), &aNewRanges, nullptr, nullptr, false ); | |||
1083 | } | |||
1084 | } | |||
1085 | ||||
1086 | // Timer for Scrolling | |||
1087 | ||||
1088 | if (bTimer && !bUp) | |||
1089 | mrViewData.GetView()->SetTimer( this, rMEvt ); // repeat event | |||
1090 | else | |||
1091 | mrViewData.GetView()->ResetTimer(); | |||
1092 | } | |||
1093 | ||||
1094 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |
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 | #ifndef INCLUDED_VCL_PTR_HXX |
21 | #define INCLUDED_VCL_PTR_HXX |
22 | |
23 | #include <sal/config.h> |
24 | |
25 | #include <rtl/ref.hxx> |
26 | |
27 | #include <utility> |
28 | #include <type_traits> |
29 | |
30 | #ifdef DBG_UTIL |
31 | #ifndef _WIN32 |
32 | #include <vcl/vclmain.hxx> |
33 | #endif |
34 | #endif |
35 | |
36 | class VclReferenceBase; |
37 | |
38 | namespace vcl::detail { |
39 | |
40 | template<typename> |
41 | constexpr bool isIncompleteOrDerivedFromVclReferenceBase(...) { return true; } |
42 | |
43 | template<typename T> constexpr bool isIncompleteOrDerivedFromVclReferenceBase( |
44 | int (*)[sizeof(T)]) |
45 | { return std::is_base_of<VclReferenceBase, T>::value; } |
46 | |
47 | } // namespace vcl::detail |
48 | |
49 | /** |
50 | * A thin wrapper around rtl::Reference to implement the acquire and dispose semantics we want for references to vcl::Window subclasses. |
51 | * |
52 | * For more details on the design please see vcl/README.lifecycle |
53 | * |
54 | * @param reference_type must be a subclass of vcl::Window |
55 | */ |
56 | template <class reference_type> |
57 | class VclPtr |
58 | { |
59 | static_assert( |
60 | vcl::detail::isIncompleteOrDerivedFromVclReferenceBase<reference_type>( |
61 | nullptr), |
62 | "template argument type must be derived from VclReferenceBase"); |
63 | |
64 | ::rtl::Reference<reference_type> m_rInnerRef; |
65 | |
66 | public: |
67 | /** Constructor... |
68 | */ |
69 | VclPtr() |
70 | : m_rInnerRef() |
71 | {} |
72 | |
73 | /** Constructor... |
74 | */ |
75 | VclPtr (reference_type * pBody) |
76 | : m_rInnerRef(pBody) |
77 | {} |
78 | |
79 | /** Constructor... that doesn't take a ref. |
80 | */ |
81 | VclPtr (reference_type * pBody, __sal_NoAcquire) |
82 | : m_rInnerRef(pBody, SAL_NO_ACQUIRE) |
83 | {} |
84 | |
85 | /** Up-casting conversion constructor: Copies interface reference. |
86 | |
87 | Does not work for up-casts to ambiguous bases. For the special case of |
88 | up-casting to Reference< XInterface >, see the corresponding conversion |
89 | operator. |
90 | |
91 | @param rRef another reference |
92 | */ |
93 | template< class derived_type > |
94 | VclPtr( |
95 | const VclPtr< derived_type > & rRef, |
96 | typename std::enable_if< |
97 | std::is_base_of<reference_type, derived_type>::value, int>::type |
98 | = 0 ) |
99 | : m_rInnerRef( static_cast<reference_type*>(rRef) ) |
100 | { |
101 | } |
102 | |
103 | #if defined(DBG_UTIL) && !defined(_WIN32) |
104 | virtual ~VclPtr() |
105 | { |
106 | assert(m_rInnerRef.get() == nullptr || vclmain::isAlive())(static_cast <bool> (m_rInnerRef.get() == nullptr || vclmain ::isAlive()) ? void (0) : __assert_fail ("m_rInnerRef.get() == nullptr || vclmain::isAlive()" , "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx" , 106, __extension__ __PRETTY_FUNCTION__)); |
107 | // We can be one of the intermediate counts, but if we are the last |
108 | // VclPtr keeping this object alive, then something forgot to call dispose(). |
109 | assert((!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1)(static_cast <bool> ((!m_rInnerRef.get() || m_rInnerRef ->isDisposed() || m_rInnerRef->getRefCount() > 1) && "someone forgot to call dispose()") ? void (0) : __assert_fail ("(!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1) && \"someone forgot to call dispose()\"" , "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx" , 110, __extension__ __PRETTY_FUNCTION__)) |
110 | && "someone forgot to call dispose()")(static_cast <bool> ((!m_rInnerRef.get() || m_rInnerRef ->isDisposed() || m_rInnerRef->getRefCount() > 1) && "someone forgot to call dispose()") ? void (0) : __assert_fail ("(!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1) && \"someone forgot to call dispose()\"" , "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx" , 110, __extension__ __PRETTY_FUNCTION__)); |
111 | } |
112 | VclPtr(VclPtr const &) = default; |
113 | VclPtr(VclPtr &&) = default; |
114 | VclPtr & operator =(VclPtr const &) = default; |
115 | VclPtr & operator =(VclPtr &&) = default; |
116 | #endif |
117 | |
118 | /** |
119 | * A construction helper for VclPtr. Since VclPtr types are created |
120 | * with a reference-count of one - to help fit into the existing |
121 | * code-flow; this helps us to construct them easily. |
122 | * |
123 | * For more details on the design please see vcl/README.lifecycle |
124 | * |
125 | * @tparam reference_type must be a subclass of vcl::Window |
126 | */ |
127 | template<typename... Arg> [[nodiscard]] static VclPtr< reference_type > Create(Arg &&... arg) |
128 | { |
129 | return VclPtr< reference_type >( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE ); |
130 | } |
131 | |
132 | /** Probably most common used: handle->someBodyOp(). |
133 | */ |
134 | reference_type * operator->() const |
135 | { |
136 | return m_rInnerRef.get(); |
137 | } |
138 | |
139 | /** Get the body. Can be used instead of operator->(). |
140 | I.e. handle->someBodyOp() and handle.get()->someBodyOp() |
141 | are the same. |
142 | */ |
143 | reference_type * get() const |
144 | { |
145 | return m_rInnerRef.get(); |
146 | } |
147 | |
148 | void set(reference_type *pBody) |
149 | { |
150 | m_rInnerRef.set(pBody); |
151 | } |
152 | |
153 | void reset(reference_type *pBody) |
154 | { |
155 | m_rInnerRef.set(pBody); |
156 | } |
157 | |
158 | /** Up-casting copy assignment operator. |
159 | |
160 | Does not work for up-casts to ambiguous bases. |
161 | |
162 | @param rRef another reference |
163 | */ |
164 | template<typename derived_type> |
165 | typename std::enable_if< |
166 | std::is_base_of<reference_type, derived_type>::value, |
167 | VclPtr &>::type |
168 | operator =(VclPtr<derived_type> const & rRef) |
169 | { |
170 | m_rInnerRef.set(rRef.get()); |
171 | return *this; |
172 | } |
173 | |
174 | VclPtr & operator =(reference_type * pBody) |
175 | { |
176 | m_rInnerRef.set(pBody); |
177 | return *this; |
178 | } |
179 | |
180 | operator reference_type * () const |
181 | { |
182 | return m_rInnerRef.get(); |
183 | } |
184 | |
185 | explicit operator bool () const |
186 | { |
187 | return m_rInnerRef.get() != nullptr; |
188 | } |
189 | |
190 | void clear() |
191 | { |
192 | m_rInnerRef.clear(); |
193 | } |
194 | |
195 | void reset() |
196 | { |
197 | m_rInnerRef.clear(); |
198 | } |
199 | |
200 | void disposeAndClear() |
201 | { |
202 | // hold it alive for the lifetime of this method |
203 | ::rtl::Reference<reference_type> aTmp(m_rInnerRef); |
204 | m_rInnerRef.clear(); // we should use some 'swap' method ideally ;-) |
205 | if (aTmp.get()) { |
206 | aTmp->disposeOnce(); |
207 | } |
208 | } |
209 | |
210 | /** Needed to place VclPtr's into STL collection. |
211 | */ |
212 | bool operator< (const VclPtr<reference_type> & handle) const |
213 | { |
214 | return (m_rInnerRef < handle.m_rInnerRef); |
215 | } |
216 | }; // class VclPtr |
217 | |
218 | template<typename T1, typename T2> |
219 | inline bool operator ==(VclPtr<T1> const & p1, VclPtr<T2> const & p2) { |
220 | return p1.get() == p2.get(); |
221 | } |
222 | |
223 | template<typename T> inline bool operator ==(VclPtr<T> const & p1, T const * p2) |
224 | { |
225 | return p1.get() == p2; |
226 | } |
227 | |
228 | template<typename T> inline bool operator ==(VclPtr<T> const & p1, T * p2) { |
229 | return p1.get() == p2; |
230 | } |
231 | |
232 | template<typename T> inline bool operator ==(T const * p1, VclPtr<T> const & p2) |
233 | { |
234 | return p1 == p2.get(); |
235 | } |
236 | |
237 | template<typename T> inline bool operator ==(T * p1, VclPtr<T> const & p2) { |
238 | return p1 == p2.get(); |
239 | } |
240 | |
241 | template<typename T1, typename T2> |
242 | inline bool operator !=(VclPtr<T1> const & p1, VclPtr<T2> const & p2) { |
243 | return !(p1 == p2); |
244 | } |
245 | |
246 | template<typename T> inline bool operator !=(VclPtr<T> const & p1, T const * p2) |
247 | { |
248 | return !(p1 == p2); |
249 | } |
250 | |
251 | template<typename T> inline bool operator !=(VclPtr<T> const & p1, T * p2) { |
252 | return !(p1 == p2); |
253 | } |
254 | |
255 | template<typename T> inline bool operator !=(T const * p1, VclPtr<T> const & p2) |
256 | { |
257 | return !(p1 == p2); |
258 | } |
259 | |
260 | template<typename T> inline bool operator !=(T * p1, VclPtr<T> const & p2) { |
261 | return !(p1 == p2); |
262 | } |
263 | |
264 | /** |
265 | * A construction helper for a temporary VclPtr. Since VclPtr types |
266 | * are created with a reference-count of one - to help fit into |
267 | * the existing code-flow; this helps us to construct them easily. |
268 | * see also VclPtr::Create and ScopedVclPtr |
269 | * |
270 | * For more details on the design please see vcl/README.lifecycle |
271 | * |
272 | * @param reference_type must be a subclass of vcl::Window |
273 | */ |
274 | template <class reference_type> |
275 | class SAL_WARN_UNUSED__attribute__((warn_unused)) VclPtrInstance final : public VclPtr<reference_type> |
276 | { |
277 | public: |
278 | template<typename... Arg> VclPtrInstance(Arg &&... arg) |
279 | : VclPtr<reference_type>( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE ) |
280 | { |
281 | } |
282 | |
283 | /** |
284 | * Override and disallow this, to prevent people accidentally calling it and actually |
285 | * getting VclPtr::Create and getting a naked VclPtr<> instance |
286 | */ |
287 | template<typename... Arg> static VclPtrInstance< reference_type > Create(Arg &&... ) = delete; |
288 | }; |
289 | |
290 | template <class reference_type> |
291 | class ScopedVclPtr : public VclPtr<reference_type> |
292 | { |
293 | public: |
294 | /** Constructor... |
295 | */ |
296 | ScopedVclPtr() |
297 | : VclPtr<reference_type>() |
298 | {} |
299 | |
300 | /** Constructor |
301 | */ |
302 | ScopedVclPtr (reference_type * pBody) |
303 | : VclPtr<reference_type>(pBody) |
304 | {} |
305 | |
306 | /** Copy constructor... |
307 | */ |
308 | ScopedVclPtr (const VclPtr<reference_type> & handle) |
309 | : VclPtr<reference_type>(handle) |
310 | {} |
311 | |
312 | /** |
313 | Assignment that releases the last reference. |
314 | */ |
315 | void disposeAndReset(reference_type *pBody) |
316 | { |
317 | if (pBody != this->get()) { |
318 | VclPtr<reference_type>::disposeAndClear(); |
319 | VclPtr<reference_type>::set(pBody); |
320 | } |
321 | } |
322 | |
323 | /** |
324 | Assignment that releases the last reference. |
325 | */ |
326 | ScopedVclPtr<reference_type>& operator = (reference_type * pBody) |
327 | { |
328 | disposeAndReset(pBody); |
329 | return *this; |
330 | } |
331 | |
332 | /** Up-casting conversion constructor: Copies interface reference. |
333 | |
334 | Does not work for up-casts to ambiguous bases. For the special case of |
335 | up-casting to Reference< XInterface >, see the corresponding conversion |
336 | operator. |
337 | |
338 | @param rRef another reference |
339 | */ |
340 | template< class derived_type > |
341 | ScopedVclPtr( |
342 | const VclPtr< derived_type > & rRef, |
343 | typename std::enable_if< |
344 | std::is_base_of<reference_type, derived_type>::value, int>::type |
345 | = 0 ) |
346 | : VclPtr<reference_type>( rRef ) |
347 | { |
348 | } |
349 | |
350 | /** Up-casting assignment operator. |
351 | |
352 | Does not work for up-casts to ambiguous bases. |
353 | |
354 | @param rRef another VclPtr |
355 | */ |
356 | template<typename derived_type> |
357 | typename std::enable_if< |
358 | std::is_base_of<reference_type, derived_type>::value, |
359 | ScopedVclPtr &>::type |
360 | operator =(VclPtr<derived_type> const & rRef) |
361 | { |
362 | disposeAndReset(rRef.get()); |
363 | return *this; |
364 | } |
365 | |
366 | /** |
367 | * Override and disallow this, to prevent people accidentally calling it and actually |
368 | * getting VclPtr::Create and getting a naked VclPtr<> instance |
369 | */ |
370 | template<typename... Arg> static ScopedVclPtr< reference_type > Create(Arg &&... ) = delete; |
371 | |
372 | ~ScopedVclPtr() |
373 | { |
374 | VclPtr<reference_type>::disposeAndClear(); |
375 | assert(VclPtr<reference_type>::get() == nullptr)(static_cast <bool> (VclPtr<reference_type>::get( ) == nullptr) ? void (0) : __assert_fail ("VclPtr<reference_type>::get() == nullptr" , "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx" , 375, __extension__ __PRETTY_FUNCTION__)); // make sure there are no lingering references |
376 | } |
377 | |
378 | private: |
379 | // Most likely we don't want this default copy-constructor. |
380 | ScopedVclPtr (const ScopedVclPtr<reference_type> &) = delete; |
381 | // And certainly we don't want a default assignment operator. |
382 | ScopedVclPtr<reference_type>& operator = (const ScopedVclPtr<reference_type> &) = delete; |
383 | // And disallow reset as that doesn't call disposeAndClear on the original reference |
384 | void reset() = delete; |
385 | void reset(reference_type *pBody) = delete; |
386 | |
387 | protected: |
388 | ScopedVclPtr (reference_type * pBody, __sal_NoAcquire) |
389 | : VclPtr<reference_type>(pBody, SAL_NO_ACQUIRE) |
390 | {} |
391 | }; |
392 | |
393 | /** |
394 | * A construction helper for ScopedVclPtr. Since VclPtr types are created |
395 | * with a reference-count of one - to help fit into the existing |
396 | * code-flow; this helps us to construct them easily. |
397 | * |
398 | * For more details on the design please see vcl/README.lifecycle |
399 | * |
400 | * @param reference_type must be a subclass of vcl::Window |
401 | */ |
402 | #if defined _MSC_VER |
403 | #pragma warning(push) |
404 | #pragma warning(disable: 4521) // " multiple copy constructors specified" |
405 | #endif |
406 | template <class reference_type> |
407 | class SAL_WARN_UNUSED__attribute__((warn_unused)) ScopedVclPtrInstance final : public ScopedVclPtr<reference_type> |
408 | { |
409 | public: |
410 | template<typename... Arg> ScopedVclPtrInstance(Arg &&... arg) |
411 | : ScopedVclPtr<reference_type>( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE ) |
412 | { |
413 | } |
414 | |
415 | /** |
416 | * Override and disallow this, to prevent people accidentally calling it and actually |
417 | * getting VclPtr::Create and getting a naked VclPtr<> instance |
418 | */ |
419 | template<typename... Arg> static ScopedVclPtrInstance< reference_type > Create(Arg &&...) = delete; |
420 | |
421 | private: |
422 | // Prevent the above perfect forwarding ctor from hijacking (accidental) |
423 | // attempts at ScopedVclPtrInstance copy construction (where the hijacking |
424 | // would typically lead to somewhat obscure error messages); both non-const |
425 | // and const variants are needed here, as the ScopedVclPtr base class has a |
426 | // const--variant copy ctor, so the implicitly declared copy ctor for |
427 | // ScopedVclPtrInstance would also be the const variant, so non-const copy |
428 | // construction attempts would be hijacked by the perfect forwarding ctor; |
429 | // but if we only declared a non-const variant here, the const variant would |
430 | // no longer be implicitly declared (as there would already be an explicitly |
431 | // declared copy ctor), so const copy construction attempts would then be |
432 | // hijacked by the perfect forwarding ctor: |
433 | ScopedVclPtrInstance(ScopedVclPtrInstance &) = delete; |
434 | ScopedVclPtrInstance(ScopedVclPtrInstance const &) = delete; |
435 | }; |
436 | #if defined _MSC_VER |
437 | #pragma warning(pop) |
438 | #endif |
439 | |
440 | #endif // INCLUDED_VCL_PTR_HXX |
441 | |
442 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |
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 | #ifndef INCLUDED_RTL_REF_HXX | ||||||||
21 | #define INCLUDED_RTL_REF_HXX | ||||||||
22 | |||||||||
23 | #include "sal/config.h" | ||||||||
24 | |||||||||
25 | #include <cassert> | ||||||||
26 | #include <cstddef> | ||||||||
27 | #include <functional> | ||||||||
28 | #ifdef LIBO_INTERNAL_ONLY1 | ||||||||
29 | #include <type_traits> | ||||||||
30 | #endif | ||||||||
31 | |||||||||
32 | #include "sal/types.h" | ||||||||
33 | |||||||||
34 | namespace rtl | ||||||||
35 | { | ||||||||
36 | |||||||||
37 | /** Template reference class for reference type. | ||||||||
38 | */ | ||||||||
39 | template <class reference_type> | ||||||||
40 | class Reference | ||||||||
41 | { | ||||||||
42 | /** The <b>reference_type</b> body pointer. | ||||||||
43 | */ | ||||||||
44 | reference_type * m_pBody; | ||||||||
45 | |||||||||
46 | |||||||||
47 | public: | ||||||||
48 | /** Constructor... | ||||||||
49 | */ | ||||||||
50 | Reference() | ||||||||
51 | : m_pBody (NULL__null) | ||||||||
52 | {} | ||||||||
53 | |||||||||
54 | |||||||||
55 | /** Constructor... | ||||||||
56 | */ | ||||||||
57 | Reference (reference_type * pBody, __sal_NoAcquire) | ||||||||
58 | : m_pBody (pBody) | ||||||||
59 | { | ||||||||
60 | } | ||||||||
61 | |||||||||
62 | /** Constructor... | ||||||||
63 | */ | ||||||||
64 | Reference (reference_type * pBody) | ||||||||
65 | : m_pBody (pBody) | ||||||||
66 | { | ||||||||
67 | if (m_pBody) | ||||||||
68 | m_pBody->acquire(); | ||||||||
69 | } | ||||||||
70 | |||||||||
71 | /** Copy constructor... | ||||||||
72 | */ | ||||||||
73 | Reference (const Reference<reference_type> & handle) | ||||||||
74 | : m_pBody (handle.m_pBody) | ||||||||
75 | { | ||||||||
76 | if (m_pBody) | ||||||||
77 | m_pBody->acquire(); | ||||||||
78 | } | ||||||||
79 | |||||||||
80 | #ifdef LIBO_INTERNAL_ONLY1 | ||||||||
81 | /** Move constructor... | ||||||||
82 | */ | ||||||||
83 | Reference (Reference<reference_type> && handle) noexcept | ||||||||
84 | : m_pBody (handle.m_pBody) | ||||||||
85 | { | ||||||||
86 | handle.m_pBody = nullptr; | ||||||||
87 | } | ||||||||
88 | #endif | ||||||||
89 | |||||||||
90 | #if defined LIBO_INTERNAL_ONLY1 | ||||||||
91 | /** Up-casting conversion constructor: Copies interface reference. | ||||||||
92 | |||||||||
93 | Does not work for up-casts to ambiguous bases. | ||||||||
94 | |||||||||
95 | @param rRef another reference | ||||||||
96 | */ | ||||||||
97 | template< class derived_type > | ||||||||
98 | inline Reference( | ||||||||
99 | const Reference< derived_type > & rRef, | ||||||||
100 | std::enable_if_t<std::is_base_of_v<reference_type, derived_type>, int> = 0 ) | ||||||||
101 | : m_pBody (rRef.get()) | ||||||||
102 | { | ||||||||
103 | if (m_pBody) | ||||||||
104 | m_pBody->acquire(); | ||||||||
105 | } | ||||||||
106 | #endif | ||||||||
107 | |||||||||
108 | /** Destructor... | ||||||||
109 | */ | ||||||||
110 | ~Reference() COVERITY_NOEXCEPT_FALSE | ||||||||
111 | { | ||||||||
112 | if (m_pBody
| ||||||||
113 | m_pBody->release(); | ||||||||
114 | } | ||||||||
115 | |||||||||
116 | /** Set... | ||||||||
117 | Similar to assignment. | ||||||||
118 | */ | ||||||||
119 | Reference<reference_type> & | ||||||||
120 | SAL_CALL set (reference_type * pBody) | ||||||||
121 | { | ||||||||
122 | if (pBody) | ||||||||
123 | pBody->acquire(); | ||||||||
124 | reference_type * const pOld = m_pBody; | ||||||||
125 | m_pBody = pBody; | ||||||||
126 | if (pOld) | ||||||||
127 | pOld->release(); | ||||||||
128 | return *this; | ||||||||
129 | } | ||||||||
130 | |||||||||
131 | /** Assignment. | ||||||||
132 | Unbinds this instance from its body (if bound) and | ||||||||
133 | bind it to the body represented by the handle. | ||||||||
134 | */ | ||||||||
135 | Reference<reference_type> & | ||||||||
136 | SAL_CALL operator= (const Reference<reference_type> & handle) | ||||||||
137 | { | ||||||||
138 | return set( handle.m_pBody ); | ||||||||
139 | } | ||||||||
140 | |||||||||
141 | #ifdef LIBO_INTERNAL_ONLY1 | ||||||||
142 | /** Assignment. | ||||||||
143 | * Unbinds this instance from its body (if bound), | ||||||||
144 | * bind it to the body represented by the handle, and | ||||||||
145 | * set the body represented by the handle to nullptr. | ||||||||
146 | */ | ||||||||
147 | Reference<reference_type> & | ||||||||
148 | operator= (Reference<reference_type> && handle) | ||||||||
149 | { | ||||||||
150 | // self-movement guts ourself | ||||||||
151 | if (m_pBody) | ||||||||
152 | m_pBody->release(); | ||||||||
153 | m_pBody = handle.m_pBody; | ||||||||
154 | handle.m_pBody = nullptr; | ||||||||
155 | return *this; | ||||||||
156 | } | ||||||||
157 | #endif | ||||||||
158 | |||||||||
159 | /** Assignment... | ||||||||
160 | */ | ||||||||
161 | Reference<reference_type> & | ||||||||
162 | SAL_CALL operator= (reference_type * pBody) | ||||||||
163 | { | ||||||||
164 | return set( pBody ); | ||||||||
165 | } | ||||||||
166 | |||||||||
167 | /** Unbind the body from this handle. | ||||||||
168 | Note that for a handle representing a large body, | ||||||||
169 | "handle.clear().set(new body());" _might_ | ||||||||
170 | perform a little bit better than "handle.set(new body());", | ||||||||
171 | since in the second case two large objects exist in memory | ||||||||
172 | (the old body and the new body). | ||||||||
173 | */ | ||||||||
174 | Reference<reference_type> & SAL_CALL clear() | ||||||||
175 | { | ||||||||
176 | if (m_pBody) | ||||||||
177 | { | ||||||||
178 | reference_type * const pOld = m_pBody; | ||||||||
179 | m_pBody = NULL__null; | ||||||||
180 | pOld->release(); | ||||||||
181 | } | ||||||||
182 | return *this; | ||||||||
183 | } | ||||||||
184 | |||||||||
185 | |||||||||
186 | /** Get the body. Can be used instead of operator->(). | ||||||||
187 | I.e. handle->someBodyOp() and handle.get()->someBodyOp() | ||||||||
188 | are the same. | ||||||||
189 | */ | ||||||||
190 | reference_type * SAL_CALL get() const | ||||||||
191 | { | ||||||||
192 | return m_pBody; | ||||||||
| |||||||||
193 | } | ||||||||
194 | |||||||||
195 | |||||||||
196 | /** Probably most common used: handle->someBodyOp(). | ||||||||
197 | */ | ||||||||
198 | reference_type * SAL_CALL operator->() const | ||||||||
199 | { | ||||||||
200 | assert(m_pBody != NULL)(static_cast <bool> (m_pBody != __null) ? void (0) : __assert_fail ("m_pBody != NULL", "/home/maarten/src/libreoffice/core/include/rtl/ref.hxx" , 200, __extension__ __PRETTY_FUNCTION__)); | ||||||||
201 | return m_pBody; | ||||||||
202 | } | ||||||||
203 | |||||||||
204 | |||||||||
205 | /** Allows (*handle).someBodyOp(). | ||||||||
206 | */ | ||||||||
207 | reference_type & SAL_CALL operator*() const | ||||||||
208 | { | ||||||||
209 | assert(m_pBody != NULL)(static_cast <bool> (m_pBody != __null) ? void (0) : __assert_fail ("m_pBody != NULL", "/home/maarten/src/libreoffice/core/include/rtl/ref.hxx" , 209, __extension__ __PRETTY_FUNCTION__)); | ||||||||
210 | return *m_pBody; | ||||||||
211 | } | ||||||||
212 | |||||||||
213 | |||||||||
214 | /** Returns True if the handle does point to a valid body. | ||||||||
215 | */ | ||||||||
216 | bool SAL_CALL is() const | ||||||||
217 | { | ||||||||
218 | return (m_pBody != NULL__null); | ||||||||
219 | } | ||||||||
220 | |||||||||
221 | #if defined LIBO_INTERNAL_ONLY1 | ||||||||
222 | /** Returns True if the handle does point to a valid body. | ||||||||
223 | */ | ||||||||
224 | explicit operator bool() const | ||||||||
225 | { | ||||||||
226 | return is(); | ||||||||
227 | } | ||||||||
228 | #endif | ||||||||
229 | |||||||||
230 | /** Returns True if this points to pBody. | ||||||||
231 | */ | ||||||||
232 | bool SAL_CALL operator== (const reference_type * pBody) const | ||||||||
233 | { | ||||||||
234 | return (m_pBody == pBody); | ||||||||
235 | } | ||||||||
236 | |||||||||
237 | |||||||||
238 | /** Returns True if handle points to the same body. | ||||||||
239 | */ | ||||||||
240 | bool | ||||||||
241 | SAL_CALL operator== (const Reference<reference_type> & handle) const | ||||||||
242 | { | ||||||||
243 | return (m_pBody == handle.m_pBody); | ||||||||
244 | } | ||||||||
245 | |||||||||
246 | |||||||||
247 | /** Needed to place References into STL collection. | ||||||||
248 | */ | ||||||||
249 | bool | ||||||||
250 | SAL_CALL operator!= (const Reference<reference_type> & handle) const | ||||||||
251 | { | ||||||||
252 | return (m_pBody != handle.m_pBody); | ||||||||
253 | } | ||||||||
254 | |||||||||
255 | |||||||||
256 | /** Needed to place References into STL collection. | ||||||||
257 | */ | ||||||||
258 | bool | ||||||||
259 | SAL_CALL operator< (const Reference<reference_type> & handle) const | ||||||||
260 | { | ||||||||
261 | return (m_pBody < handle.m_pBody); | ||||||||
262 | } | ||||||||
263 | |||||||||
264 | |||||||||
265 | /** Needed to place References into STL collection. | ||||||||
266 | */ | ||||||||
267 | bool | ||||||||
268 | SAL_CALL operator> (const Reference<reference_type> & handle) const | ||||||||
269 | { | ||||||||
270 | return (m_pBody > handle.m_pBody); | ||||||||
271 | } | ||||||||
272 | }; | ||||||||
273 | |||||||||
274 | } // namespace rtl | ||||||||
275 | |||||||||
276 | #if defined LIBO_INTERNAL_ONLY1 | ||||||||
277 | namespace std | ||||||||
278 | { | ||||||||
279 | |||||||||
280 | /// @cond INTERNAL | ||||||||
281 | /** | ||||||||
282 | Make rtl::Reference hashable by default for use in STL containers. | ||||||||
283 | |||||||||
284 | @since LibreOffice 6.3 | ||||||||
285 | */ | ||||||||
286 | template<typename T> | ||||||||
287 | struct hash<::rtl::Reference<T>> | ||||||||
288 | { | ||||||||
289 | std::size_t operator()(::rtl::Reference<T> const & s) const | ||||||||
290 | { return std::size_t(s.get()); } | ||||||||
291 | }; | ||||||||
292 | /// @endcond | ||||||||
293 | |||||||||
294 | } | ||||||||
295 | |||||||||
296 | #endif | ||||||||
297 | |||||||||
298 | #endif /* ! INCLUDED_RTL_REF_HXX */ | ||||||||
299 | |||||||||
300 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |
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 | #ifndef INCLUDED_VCL_Reference_HXX |
20 | #define INCLUDED_VCL_Reference_HXX |
21 | |
22 | #include <vcl/dllapi.h> |
23 | #include <osl/interlck.h> |
24 | |
25 | class VCL_DLLPUBLIC__attribute__ ((visibility("default"))) VclReferenceBase |
26 | { |
27 | mutable oslInterlockedCount mnRefCnt; |
28 | |
29 | template<typename T> friend class VclPtr; |
30 | |
31 | public: |
32 | void acquire() const |
33 | { |
34 | osl_atomic_increment(&mnRefCnt)__sync_add_and_fetch((&mnRefCnt), 1); |
35 | } |
36 | |
37 | void release() const |
38 | { |
39 | if (osl_atomic_decrement(&mnRefCnt)__sync_sub_and_fetch((&mnRefCnt), 1) == 0) |
40 | delete this; |
41 | } |
42 | #ifdef DBG_UTIL |
43 | #ifndef _WIN32 |
44 | sal_Int32 getRefCount() const { return mnRefCnt; } |
45 | #endif |
46 | #endif |
47 | |
48 | |
49 | private: |
50 | VclReferenceBase(const VclReferenceBase&) = delete; |
51 | VclReferenceBase& operator=(const VclReferenceBase&) = delete; |
52 | |
53 | bool mbDisposed : 1; |
54 | |
55 | protected: |
56 | VclReferenceBase(); |
57 | protected: |
58 | virtual ~VclReferenceBase(); |
59 | |
60 | protected: |
61 | virtual void dispose(); |
62 | |
63 | public: |
64 | void disposeOnce(); |
65 | bool isDisposed() const { return mbDisposed; } |
66 | |
67 | }; |
68 | #endif |