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 <vcl/commandevent.hxx> | |||
21 | #include <vcl/event.hxx> | |||
22 | #include <vcl/decoview.hxx> | |||
23 | #include <vcl/toolkit/spinfld.hxx> | |||
24 | #include <vcl/settings.hxx> | |||
25 | #include <vcl/uitest/uiobject.hxx> | |||
26 | #include <sal/log.hxx> | |||
27 | ||||
28 | #include <controldata.hxx> | |||
29 | #include <spin.hxx> | |||
30 | #include <svdata.hxx> | |||
31 | ||||
32 | namespace { | |||
33 | ||||
34 | void ImplGetSpinbuttonValue(vcl::Window* pWin, | |||
35 | const tools::Rectangle& rUpperRect, const tools::Rectangle& rLowerRect, | |||
36 | bool bUpperIn, bool bLowerIn, bool bUpperEnabled, bool bLowerEnabled, | |||
37 | bool bHorz, SpinbuttonValue& rValue ) | |||
38 | { | |||
39 | // convert spinbutton data to a SpinbuttonValue structure for native painting | |||
40 | ||||
41 | rValue.maUpperRect = rUpperRect; | |||
42 | rValue.maLowerRect = rLowerRect; | |||
43 | ||||
44 | Point aPointerPos = pWin->GetPointerPosPixel(); | |||
45 | ||||
46 | ControlState nState = ControlState::ENABLED; | |||
47 | if (bUpperIn) | |||
48 | nState |= ControlState::PRESSED; | |||
49 | if (!pWin->IsEnabled() || !bUpperEnabled) | |||
50 | nState &= ~ControlState::ENABLED; | |||
51 | if (pWin->HasFocus()) | |||
52 | nState |= ControlState::FOCUSED; | |||
53 | if (pWin->IsMouseOver() && rUpperRect.IsInside(aPointerPos)) | |||
54 | nState |= ControlState::ROLLOVER; | |||
55 | rValue.mnUpperState = nState; | |||
56 | ||||
57 | nState = ControlState::ENABLED; | |||
58 | if (bLowerIn) | |||
59 | nState |= ControlState::PRESSED; | |||
60 | if (!pWin->IsEnabled() || !bLowerEnabled) | |||
61 | nState &= ~ControlState::ENABLED; | |||
62 | if (pWin->HasFocus()) | |||
63 | nState |= ControlState::FOCUSED; | |||
64 | // for overlapping spins: highlight only one | |||
65 | if (pWin->IsMouseOver() && rLowerRect.IsInside(aPointerPos) && !rUpperRect.IsInside(aPointerPos)) | |||
66 | nState |= ControlState::ROLLOVER; | |||
67 | rValue.mnLowerState = nState; | |||
68 | ||||
69 | rValue.mnUpperPart = bHorz ? ControlPart::ButtonLeft : ControlPart::ButtonUp; | |||
70 | rValue.mnLowerPart = bHorz ? ControlPart::ButtonRight : ControlPart::ButtonDown; | |||
71 | } | |||
72 | ||||
73 | bool ImplDrawNativeSpinfield(vcl::RenderContext& rRenderContext, vcl::Window const * pWin, const SpinbuttonValue& rSpinbuttonValue) | |||
74 | { | |||
75 | bool bNativeOK = false; | |||
76 | ||||
77 | if (rRenderContext.IsNativeControlSupported(ControlType::Spinbox, ControlPart::Entire) && | |||
78 | // there is just no useful native support for spinfields with dropdown | |||
79 | !(pWin->GetStyle() & WB_DROPDOWN)) | |||
80 | { | |||
81 | if (rRenderContext.IsNativeControlSupported(ControlType::Spinbox, rSpinbuttonValue.mnUpperPart) && | |||
82 | rRenderContext.IsNativeControlSupported(ControlType::Spinbox, rSpinbuttonValue.mnLowerPart)) | |||
83 | { | |||
84 | // only paint the embedded spin buttons, all buttons are painted at once | |||
85 | tools::Rectangle aUpperAndLowerButtons( rSpinbuttonValue.maUpperRect.GetUnion( rSpinbuttonValue.maLowerRect ) ); | |||
86 | bNativeOK = rRenderContext.DrawNativeControl(ControlType::Spinbox, ControlPart::AllButtons, aUpperAndLowerButtons, | |||
87 | ControlState::ENABLED, rSpinbuttonValue, OUString()); | |||
88 | } | |||
89 | else | |||
90 | { | |||
91 | // paint the spinbox as a whole, use borderwindow to have proper clipping | |||
92 | vcl::Window* pBorder = pWin->GetWindow(GetWindowType::Border); | |||
93 | ||||
94 | // to not overwrite everything, set the button region as clipregion to the border window | |||
95 | tools::Rectangle aClipRect(rSpinbuttonValue.maLowerRect); | |||
96 | aClipRect.Union(rSpinbuttonValue.maUpperRect); | |||
97 | ||||
98 | vcl::RenderContext* pContext = &rRenderContext; | |||
99 | vcl::Region oldRgn; | |||
100 | Point aPt; | |||
101 | Size aSize(pBorder->GetOutputSizePixel()); // the size of the border window, i.e., the whole control | |||
102 | tools::Rectangle aNatRgn(aPt, aSize); | |||
103 | ||||
104 | if (!pWin->SupportsDoubleBuffering()) | |||
105 | { | |||
106 | // convert from screen space to borderwin space | |||
107 | aClipRect.SetPos(pBorder->ScreenToOutputPixel(pWin->OutputToScreenPixel(aClipRect.TopLeft()))); | |||
108 | ||||
109 | oldRgn = pBorder->GetClipRegion(); | |||
110 | pBorder->SetClipRegion(vcl::Region(aClipRect)); | |||
111 | ||||
112 | pContext = pBorder; | |||
113 | } | |||
114 | ||||
115 | tools::Rectangle aBound, aContent; | |||
116 | if (!ImplGetSVData()->maNWFData.mbCanDrawWidgetAnySize && | |||
117 | pContext->GetNativeControlRegion(ControlType::Spinbox, ControlPart::Entire, | |||
118 | aNatRgn, ControlState::NONE, rSpinbuttonValue, | |||
119 | aBound, aContent)) | |||
120 | { | |||
121 | aSize = aContent.GetSize(); | |||
122 | } | |||
123 | ||||
124 | tools::Rectangle aRgn(aPt, aSize); | |||
125 | if (pWin->SupportsDoubleBuffering()) | |||
126 | { | |||
127 | // convert from borderwin space, to the pWin's space | |||
128 | aRgn.SetPos(pWin->ScreenToOutputPixel(pBorder->OutputToScreenPixel(aRgn.TopLeft()))); | |||
129 | } | |||
130 | ||||
131 | bNativeOK = pContext->DrawNativeControl(ControlType::Spinbox, ControlPart::Entire, aRgn, | |||
132 | ControlState::ENABLED, rSpinbuttonValue, OUString()); | |||
133 | ||||
134 | if (!pWin->SupportsDoubleBuffering()) | |||
135 | pBorder->SetClipRegion(oldRgn); | |||
136 | } | |||
137 | } | |||
138 | return bNativeOK; | |||
139 | } | |||
140 | ||||
141 | bool ImplDrawNativeSpinbuttons(vcl::RenderContext& rRenderContext, const SpinbuttonValue& rSpinbuttonValue) | |||
142 | { | |||
143 | bool bNativeOK = false; | |||
144 | ||||
145 | if (rRenderContext.IsNativeControlSupported(ControlType::SpinButtons, ControlPart::Entire)) | |||
146 | { | |||
147 | tools::Rectangle aArea = rSpinbuttonValue.maUpperRect.GetUnion(rSpinbuttonValue.maLowerRect); | |||
148 | // only paint the standalone spin buttons, all buttons are painted at once | |||
149 | bNativeOK = rRenderContext.DrawNativeControl(ControlType::SpinButtons, ControlPart::AllButtons, aArea, | |||
150 | ControlState::ENABLED, rSpinbuttonValue, OUString()); | |||
151 | } | |||
152 | return bNativeOK; | |||
153 | } | |||
154 | ||||
155 | } | |||
156 | ||||
157 | void ImplDrawSpinButton(vcl::RenderContext& rRenderContext, vcl::Window* pWindow, | |||
158 | const tools::Rectangle& rUpperRect, const tools::Rectangle& rLowerRect, | |||
159 | bool bUpperIn, bool bLowerIn, bool bUpperEnabled, bool bLowerEnabled, | |||
160 | bool bHorz, bool bMirrorHorz) | |||
161 | { | |||
162 | bool bNativeOK = false; | |||
163 | ||||
164 | if (pWindow) | |||
165 | { | |||
166 | // are we drawing standalone spin buttons or members of a spinfield ? | |||
167 | ControlType aControl = ControlType::SpinButtons; | |||
168 | switch (pWindow->GetType()) | |||
169 | { | |||
170 | case WindowType::EDIT: | |||
171 | case WindowType::MULTILINEEDIT: | |||
172 | case WindowType::PATTERNFIELD: | |||
173 | case WindowType::METRICFIELD: | |||
174 | case WindowType::CURRENCYFIELD: | |||
175 | case WindowType::DATEFIELD: | |||
176 | case WindowType::TIMEFIELD: | |||
177 | case WindowType::SPINFIELD: | |||
178 | case WindowType::FORMATTEDFIELD: | |||
179 | aControl = ControlType::Spinbox; | |||
180 | break; | |||
181 | default: | |||
182 | aControl = ControlType::SpinButtons; | |||
183 | break; | |||
184 | } | |||
185 | ||||
186 | SpinbuttonValue aValue; | |||
187 | ImplGetSpinbuttonValue(pWindow, rUpperRect, rLowerRect, | |||
188 | bUpperIn, bLowerIn, bUpperEnabled, bLowerEnabled, | |||
189 | bHorz, aValue); | |||
190 | ||||
191 | if( aControl == ControlType::Spinbox ) | |||
192 | bNativeOK = ImplDrawNativeSpinfield(rRenderContext, pWindow, aValue); | |||
193 | else if( aControl == ControlType::SpinButtons ) | |||
194 | bNativeOK = ImplDrawNativeSpinbuttons(rRenderContext, aValue); | |||
195 | } | |||
196 | ||||
197 | if (bNativeOK) | |||
198 | return; | |||
199 | ||||
200 | ImplDrawUpDownButtons(rRenderContext, | |||
201 | rUpperRect, rLowerRect, | |||
202 | bUpperIn, bLowerIn, bUpperEnabled, bLowerEnabled, | |||
203 | bHorz, bMirrorHorz); | |||
204 | } | |||
205 | ||||
206 | void ImplDrawUpDownButtons(vcl::RenderContext& rRenderContext, | |||
207 | const tools::Rectangle& rUpperRect, const tools::Rectangle& rLowerRect, | |||
208 | bool bUpperIn, bool bLowerIn, bool bUpperEnabled, bool bLowerEnabled, | |||
209 | bool bHorz, bool bMirrorHorz) | |||
210 | { | |||
211 | DecorationView aDecoView(&rRenderContext); | |||
212 | ||||
213 | SymbolType eType1, eType2; | |||
214 | ||||
215 | if ( bHorz ) | |||
216 | { | |||
217 | eType1 = bMirrorHorz ? SymbolType::SPIN_RIGHT : SymbolType::SPIN_LEFT; | |||
218 | eType2 = bMirrorHorz ? SymbolType::SPIN_LEFT : SymbolType::SPIN_RIGHT; | |||
219 | } | |||
220 | else | |||
221 | { | |||
222 | eType1 = SymbolType::SPIN_UP; | |||
223 | eType2 = SymbolType::SPIN_DOWN; | |||
224 | } | |||
225 | ||||
226 | DrawButtonFlags nStyle = DrawButtonFlags::NoLeftLightBorder; | |||
227 | // draw upper/left Button | |||
228 | if (bUpperIn) | |||
229 | nStyle |= DrawButtonFlags::Pressed; | |||
230 | ||||
231 | tools::Rectangle aUpRect = aDecoView.DrawButton(rUpperRect, nStyle); | |||
232 | ||||
233 | nStyle = DrawButtonFlags::NoLeftLightBorder; | |||
234 | // draw lower/right Button | |||
235 | if (bLowerIn) | |||
236 | nStyle |= DrawButtonFlags::Pressed; | |||
237 | ||||
238 | tools::Rectangle aLowRect = aDecoView.DrawButton(rLowerRect, nStyle); | |||
239 | ||||
240 | // make use of additional default edge | |||
241 | aUpRect.AdjustLeft( -1 ); | |||
242 | aUpRect.AdjustTop( -1 ); | |||
243 | aUpRect.AdjustRight( 1 ); | |||
244 | aUpRect.AdjustBottom( 1 ); | |||
245 | aLowRect.AdjustLeft( -1 ); | |||
246 | aLowRect.AdjustTop( -1 ); | |||
247 | aLowRect.AdjustRight( 1 ); | |||
248 | aLowRect.AdjustBottom( 1 ); | |||
249 | ||||
250 | // draw into the edge, so that something is visible if the rectangle is too small | |||
251 | if (aUpRect.GetHeight() < 4) | |||
252 | { | |||
253 | aUpRect.AdjustRight( 1 ); | |||
254 | aUpRect.AdjustBottom( 1 ); | |||
255 | aLowRect.AdjustRight( 1 ); | |||
256 | aLowRect.AdjustBottom( 1 ); | |||
257 | } | |||
258 | ||||
259 | // calculate Symbol size | |||
260 | long nTempSize1 = aUpRect.GetWidth(); | |||
261 | long nTempSize2 = aLowRect.GetWidth(); | |||
262 | if (std::abs( nTempSize1-nTempSize2 ) == 1) | |||
263 | { | |||
264 | if (nTempSize1 > nTempSize2) | |||
265 | aUpRect.AdjustLeft( 1 ); | |||
266 | else | |||
267 | aLowRect.AdjustLeft( 1 ); | |||
268 | } | |||
269 | nTempSize1 = aUpRect.GetHeight(); | |||
270 | nTempSize2 = aLowRect.GetHeight(); | |||
271 | if (std::abs(nTempSize1 - nTempSize2) == 1) | |||
272 | { | |||
273 | if (nTempSize1 > nTempSize2) | |||
274 | aUpRect.AdjustTop( 1 ); | |||
275 | else | |||
276 | aLowRect.AdjustTop( 1 ); | |||
277 | } | |||
278 | ||||
279 | const StyleSettings& rStyleSettings = rRenderContext.GetSettings().GetStyleSettings(); | |||
280 | ||||
281 | DrawSymbolFlags nSymStyle = DrawSymbolFlags::NONE; | |||
282 | if (!bUpperEnabled) | |||
283 | nSymStyle |= DrawSymbolFlags::Disable; | |||
284 | aDecoView.DrawSymbol(aUpRect, eType1, rStyleSettings.GetButtonTextColor(), nSymStyle); | |||
285 | ||||
286 | nSymStyle = DrawSymbolFlags::NONE; | |||
287 | if (!bLowerEnabled) | |||
288 | nSymStyle |= DrawSymbolFlags::Disable; | |||
289 | aDecoView.DrawSymbol(aLowRect, eType2, rStyleSettings.GetButtonTextColor(), nSymStyle); | |||
290 | } | |||
291 | ||||
292 | void SpinField::ImplInitSpinFieldData() | |||
293 | { | |||
294 | mpEdit.disposeAndClear(); | |||
295 | mbSpin = false; | |||
296 | mbRepeat = false; | |||
297 | mbUpperIn = false; | |||
298 | mbLowerIn = false; | |||
299 | mbInitialUp = false; | |||
300 | mbInitialDown = false; | |||
301 | mbInDropDown = false; | |||
302 | } | |||
303 | ||||
304 | void SpinField::ImplInit(vcl::Window* pParent, WinBits nWinStyle) | |||
305 | { | |||
306 | Edit::ImplInit( pParent, nWinStyle ); | |||
307 | ||||
308 | if (!(nWinStyle & (WB_SPIN | WB_DROPDOWN))) | |||
309 | return; | |||
310 | ||||
311 | mbSpin = true; | |||
312 | ||||
313 | // Some themes want external spin buttons, therefore the main | |||
314 | // spinfield should not overdraw the border between its encapsulated | |||
315 | // edit field and the spin buttons | |||
316 | if ((nWinStyle & WB_SPIN) && ImplUseNativeBorder(*this, nWinStyle)) | |||
317 | { | |||
318 | SetBackground(); | |||
319 | mpEdit.set(VclPtr<Edit>::Create(this, WB_NOBORDER)); | |||
320 | mpEdit->SetBackground(); | |||
321 | } | |||
322 | else | |||
323 | mpEdit.set(VclPtr<Edit>::Create(this, WB_NOBORDER)); | |||
324 | ||||
325 | mpEdit->EnableRTL(false); | |||
326 | mpEdit->SetPosPixel(Point()); | |||
327 | mpEdit->Show(); | |||
328 | ||||
329 | SetSubEdit(mpEdit); | |||
330 | ||||
331 | maRepeatTimer.SetInvokeHandler(LINK( this, SpinField, ImplTimeout)::tools::detail::makeLink( ::tools::detail::castTo<SpinField *>(this), &SpinField::LinkStubImplTimeout)); | |||
332 | maRepeatTimer.SetTimeout(MouseSettings::GetButtonStartRepeat()); | |||
333 | if (nWinStyle & WB_REPEAT) | |||
334 | mbRepeat = true; | |||
335 | ||||
336 | SetCompoundControl(true); | |||
337 | } | |||
338 | ||||
339 | SpinField::SpinField(vcl::Window* pParent, WinBits nWinStyle, WindowType nType) : | |||
340 | Edit(nType) | |||
341 | { | |||
342 | ImplInitSpinFieldData(); | |||
343 | ImplInit(pParent, nWinStyle); | |||
344 | } | |||
345 | ||||
346 | SpinField::~SpinField() | |||
347 | { | |||
348 | disposeOnce(); | |||
349 | } | |||
350 | ||||
351 | void SpinField::dispose() | |||
352 | { | |||
353 | mpEdit.disposeAndClear(); | |||
| ||||
354 | ||||
355 | Edit::dispose(); | |||
356 | } | |||
357 | ||||
358 | void SpinField::Up() | |||
359 | { | |||
360 | ImplCallEventListenersAndHandler( VclEventId::SpinfieldUp, [this] () { maUpHdlLink.Call(*this); } ); | |||
361 | } | |||
362 | ||||
363 | void SpinField::Down() | |||
364 | { | |||
365 | ImplCallEventListenersAndHandler( VclEventId::SpinfieldDown, [this] () { maDownHdlLink.Call(*this); } ); | |||
366 | } | |||
367 | ||||
368 | void SpinField::First() | |||
369 | { | |||
370 | ImplCallEventListenersAndHandler(VclEventId::SpinfieldFirst, nullptr); | |||
371 | } | |||
372 | ||||
373 | void SpinField::Last() | |||
374 | { | |||
375 | ImplCallEventListenersAndHandler(VclEventId::SpinfieldLast, nullptr); | |||
376 | } | |||
377 | ||||
378 | void SpinField::MouseButtonDown( const MouseEvent& rMEvt ) | |||
379 | { | |||
380 | if (!HasFocus() && (!mpEdit || !mpEdit->HasFocus())) | |||
381 | { | |||
382 | GrabFocus(); | |||
383 | } | |||
384 | ||||
385 | if (!IsReadOnly()) | |||
386 | { | |||
387 | if (maUpperRect.IsInside(rMEvt.GetPosPixel())) | |||
388 | { | |||
389 | mbUpperIn = true; | |||
390 | mbInitialUp = true; | |||
391 | Invalidate(maUpperRect); | |||
392 | } | |||
393 | else if (maLowerRect.IsInside(rMEvt.GetPosPixel())) | |||
394 | { | |||
395 | mbLowerIn = true; | |||
396 | mbInitialDown = true; | |||
397 | Invalidate(maLowerRect); | |||
398 | } | |||
399 | else if (maDropDownRect.IsInside(rMEvt.GetPosPixel())) | |||
400 | { | |||
401 | // put DropDownButton to the right | |||
402 | mbInDropDown = ShowDropDown( !mbInDropDown ); | |||
403 | Invalidate(tools::Rectangle(Point(), GetOutputSizePixel())); | |||
404 | } | |||
405 | ||||
406 | if (mbUpperIn || mbLowerIn) | |||
407 | { | |||
408 | CaptureMouse(); | |||
409 | if (mbRepeat) | |||
410 | maRepeatTimer.Start(); | |||
411 | return; | |||
412 | } | |||
413 | } | |||
414 | ||||
415 | Edit::MouseButtonDown(rMEvt); | |||
416 | } | |||
417 | ||||
418 | void SpinField::MouseButtonUp(const MouseEvent& rMEvt) | |||
419 | { | |||
420 | ReleaseMouse(); | |||
421 | mbInitialUp = mbInitialDown = false; | |||
422 | maRepeatTimer.Stop(); | |||
423 | maRepeatTimer.SetTimeout(MouseSettings::GetButtonStartRepeat()); | |||
424 | ||||
425 | if (mbUpperIn) | |||
426 | { | |||
427 | mbUpperIn = false; | |||
428 | Invalidate(maUpperRect); | |||
429 | Up(); | |||
430 | } | |||
431 | else if (mbLowerIn) | |||
432 | { | |||
433 | mbLowerIn = false; | |||
434 | Invalidate(maLowerRect); | |||
435 | Down(); | |||
436 | } | |||
437 | ||||
438 | Edit::MouseButtonUp(rMEvt); | |||
439 | } | |||
440 | ||||
441 | void SpinField::MouseMove(const MouseEvent& rMEvt) | |||
442 | { | |||
443 | if (rMEvt.IsLeft()) | |||
444 | { | |||
445 | if (mbInitialUp) | |||
446 | { | |||
447 | bool bNewUpperIn = maUpperRect.IsInside(rMEvt.GetPosPixel()); | |||
448 | if (bNewUpperIn != mbUpperIn) | |||
449 | { | |||
450 | if (bNewUpperIn) | |||
451 | { | |||
452 | if (mbRepeat) | |||
453 | maRepeatTimer.Start(); | |||
454 | } | |||
455 | else | |||
456 | maRepeatTimer.Stop(); | |||
457 | ||||
458 | mbUpperIn = bNewUpperIn; | |||
459 | Invalidate(maUpperRect); | |||
460 | } | |||
461 | } | |||
462 | else if (mbInitialDown) | |||
463 | { | |||
464 | bool bNewLowerIn = maLowerRect.IsInside(rMEvt.GetPosPixel()); | |||
465 | if (bNewLowerIn != mbLowerIn) | |||
466 | { | |||
467 | if (bNewLowerIn) | |||
468 | { | |||
469 | if (mbRepeat) | |||
470 | maRepeatTimer.Start(); | |||
471 | } | |||
472 | else | |||
473 | maRepeatTimer.Stop(); | |||
474 | ||||
475 | mbLowerIn = bNewLowerIn; | |||
476 | Invalidate(maLowerRect); | |||
477 | } | |||
478 | } | |||
479 | } | |||
480 | ||||
481 | Edit::MouseMove(rMEvt); | |||
482 | } | |||
483 | ||||
484 | bool SpinField::EventNotify(NotifyEvent& rNEvt) | |||
485 | { | |||
486 | bool bDone = false; | |||
487 | if (rNEvt.GetType() == MouseNotifyEvent::KEYINPUT) | |||
488 | { | |||
489 | const KeyEvent& rKEvt = *rNEvt.GetKeyEvent(); | |||
490 | if (!IsReadOnly()) | |||
491 | { | |||
492 | sal_uInt16 nMod = rKEvt.GetKeyCode().GetModifier(); | |||
493 | switch (rKEvt.GetKeyCode().GetCode()) | |||
494 | { | |||
495 | case KEY_UP: | |||
496 | { | |||
497 | if (!nMod) | |||
498 | { | |||
499 | Up(); | |||
500 | bDone = true; | |||
501 | } | |||
502 | } | |||
503 | break; | |||
504 | case KEY_DOWN: | |||
505 | { | |||
506 | if (!nMod) | |||
507 | { | |||
508 | Down(); | |||
509 | bDone = true; | |||
510 | } | |||
511 | else if ((nMod == KEY_MOD2) && !mbInDropDown && (GetStyle() & WB_DROPDOWN)) | |||
512 | { | |||
513 | mbInDropDown = ShowDropDown(true); | |||
514 | Invalidate(tools::Rectangle(Point(), GetOutputSizePixel())); | |||
515 | bDone = true; | |||
516 | } | |||
517 | } | |||
518 | break; | |||
519 | case KEY_PAGEUP: | |||
520 | { | |||
521 | if (!nMod) | |||
522 | { | |||
523 | Last(); | |||
524 | bDone = true; | |||
525 | } | |||
526 | } | |||
527 | break; | |||
528 | case KEY_PAGEDOWN: | |||
529 | { | |||
530 | if (!nMod) | |||
531 | { | |||
532 | First(); | |||
533 | bDone = true; | |||
534 | } | |||
535 | } | |||
536 | break; | |||
537 | } | |||
538 | } | |||
539 | } | |||
540 | ||||
541 | if (rNEvt.GetType() == MouseNotifyEvent::COMMAND) | |||
542 | { | |||
543 | if ((rNEvt.GetCommandEvent()->GetCommand() == CommandEventId::Wheel) && !IsReadOnly()) | |||
544 | { | |||
545 | MouseWheelBehaviour nWheelBehavior(GetSettings().GetMouseSettings().GetWheelBehavior()); | |||
546 | if (nWheelBehavior == MouseWheelBehaviour::ALWAYS | |||
547 | || (nWheelBehavior == MouseWheelBehaviour::FocusOnly && HasChildPathFocus())) | |||
548 | { | |||
549 | const CommandWheelData* pData = rNEvt.GetCommandEvent()->GetWheelData(); | |||
550 | if (pData->GetMode() == CommandWheelMode::SCROLL) | |||
551 | { | |||
552 | if (pData->GetDelta() < 0) | |||
553 | Down(); | |||
554 | else | |||
555 | Up(); | |||
556 | bDone = true; | |||
557 | ||||
558 | if (!HasChildPathFocus()) | |||
559 | GrabFocus(); | |||
560 | } | |||
561 | } | |||
562 | else | |||
563 | bDone = false; // don't eat this event, let the default handling happen (i.e. scroll the context) | |||
564 | } | |||
565 | } | |||
566 | ||||
567 | return bDone || Edit::EventNotify(rNEvt); | |||
568 | } | |||
569 | ||||
570 | void SpinField::FillLayoutData() const | |||
571 | { | |||
572 | if (mbSpin) | |||
573 | { | |||
574 | mpControlData->mpLayoutData.reset( new vcl::ControlLayoutData ); | |||
575 | AppendLayoutData(*GetSubEdit()); | |||
576 | GetSubEdit()->SetLayoutDataParent(this); | |||
577 | } | |||
578 | else | |||
579 | Edit::FillLayoutData(); | |||
580 | } | |||
581 | ||||
582 | void SpinField::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect) | |||
583 | { | |||
584 | if (mbSpin) | |||
585 | { | |||
586 | bool bEnable = IsEnabled(); | |||
587 | ImplDrawSpinButton(rRenderContext, this, maUpperRect, maLowerRect, | |||
588 | mbUpperIn, mbLowerIn, bEnable, bEnable); | |||
589 | } | |||
590 | ||||
591 | if (GetStyle() & WB_DROPDOWN) | |||
592 | { | |||
593 | DecorationView aView(&rRenderContext); | |||
594 | ||||
595 | DrawButtonFlags nStyle = DrawButtonFlags::NoLightBorder; | |||
596 | if (mbInDropDown) | |||
597 | nStyle |= DrawButtonFlags::Pressed; | |||
598 | tools::Rectangle aInnerRect = aView.DrawButton(maDropDownRect, nStyle); | |||
599 | ||||
600 | DrawSymbolFlags nSymbolStyle = IsEnabled() ? DrawSymbolFlags::NONE : DrawSymbolFlags::Disable; | |||
601 | aView.DrawSymbol(aInnerRect, SymbolType::SPIN_DOWN, rRenderContext.GetSettings().GetStyleSettings().GetButtonTextColor(), nSymbolStyle); | |||
602 | } | |||
603 | ||||
604 | Edit::Paint(rRenderContext, rRect); | |||
605 | } | |||
606 | ||||
607 | void SpinField::ImplCalcButtonAreas(OutputDevice* pDev, const Size& rOutSz, tools::Rectangle& rDDArea, | |||
608 | tools::Rectangle& rSpinUpArea, tools::Rectangle& rSpinDownArea) | |||
609 | { | |||
610 | const StyleSettings& rStyleSettings = pDev->GetSettings().GetStyleSettings(); | |||
611 | ||||
612 | Size aSize = rOutSz; | |||
613 | Size aDropDownSize; | |||
614 | ||||
615 | if (GetStyle() & WB_DROPDOWN) | |||
616 | { | |||
617 | long nW = rStyleSettings.GetScrollBarSize(); | |||
618 | nW = GetDrawPixel( pDev, nW ); | |||
619 | aDropDownSize = Size( CalcZoom( nW ), aSize.Height() ); | |||
620 | aSize.AdjustWidth( -(aDropDownSize.Width()) ); | |||
621 | rDDArea = tools::Rectangle( Point( aSize.Width(), 0 ), aDropDownSize ); | |||
622 | rDDArea.AdjustTop( -1 ); | |||
623 | } | |||
624 | else | |||
625 | rDDArea.SetEmpty(); | |||
626 | ||||
627 | // calculate sizes according to the height | |||
628 | if (GetStyle() & WB_SPIN) | |||
629 | { | |||
630 | long nBottom1 = aSize.Height()/2; | |||
631 | long nBottom2 = aSize.Height()-1; | |||
632 | long nTop2 = nBottom1; | |||
633 | if ( !(aSize.Height() & 0x01) ) | |||
634 | nBottom1--; | |||
635 | ||||
636 | bool bNativeRegionOK = false; | |||
637 | tools::Rectangle aContentUp, aContentDown; | |||
638 | ||||
639 | if ((pDev->GetOutDevType() == OUTDEV_WINDOW) && | |||
640 | // there is just no useful native support for spinfields with dropdown | |||
641 | ! (GetStyle() & WB_DROPDOWN) && | |||
642 | IsNativeControlSupported(ControlType::Spinbox, ControlPart::Entire)) | |||
643 | { | |||
644 | vcl::Window *pWin = static_cast<vcl::Window*>(pDev); | |||
645 | vcl::Window *pBorder = pWin->GetWindow( GetWindowType::Border ); | |||
646 | ||||
647 | // get the system's spin button size | |||
648 | ImplControlValue aControlValue; | |||
649 | tools::Rectangle aBound; | |||
650 | Point aPoint; | |||
651 | ||||
652 | // use the full extent of the control | |||
653 | tools::Rectangle aArea( aPoint, pBorder->GetOutputSizePixel() ); | |||
654 | ||||
655 | bNativeRegionOK = | |||
656 | pWin->GetNativeControlRegion(ControlType::Spinbox, ControlPart::ButtonUp, | |||
657 | aArea, ControlState::NONE, aControlValue, aBound, aContentUp) && | |||
658 | pWin->GetNativeControlRegion(ControlType::Spinbox, ControlPart::ButtonDown, | |||
659 | aArea, ControlState::NONE, aControlValue, aBound, aContentDown); | |||
660 | ||||
661 | if (bNativeRegionOK) | |||
662 | { | |||
663 | // convert back from border space to local coordinates | |||
664 | aPoint = pBorder->ScreenToOutputPixel( pWin->OutputToScreenPixel( aPoint ) ); | |||
665 | aContentUp.Move(-aPoint.X(), -aPoint.Y()); | |||
666 | aContentDown.Move(-aPoint.X(), -aPoint.Y()); | |||
667 | } | |||
668 | } | |||
669 | ||||
670 | if (bNativeRegionOK) | |||
671 | { | |||
672 | rSpinUpArea = aContentUp; | |||
673 | rSpinDownArea = aContentDown; | |||
674 | } | |||
675 | else | |||
676 | { | |||
677 | aSize.AdjustWidth( -(CalcZoom( GetDrawPixel( pDev, rStyleSettings.GetSpinSize() ) )) ); | |||
678 | ||||
679 | rSpinUpArea = tools::Rectangle( aSize.Width(), 0, rOutSz.Width()-aDropDownSize.Width()-1, nBottom1 ); | |||
680 | rSpinDownArea = tools::Rectangle( rSpinUpArea.Left(), nTop2, rSpinUpArea.Right(), nBottom2 ); | |||
681 | } | |||
682 | } | |||
683 | else | |||
684 | { | |||
685 | rSpinUpArea.SetEmpty(); | |||
686 | rSpinDownArea.SetEmpty(); | |||
687 | } | |||
688 | } | |||
689 | ||||
690 | void SpinField::Resize() | |||
691 | { | |||
692 | if (!mbSpin) | |||
693 | return; | |||
694 | ||||
695 | Control::Resize(); | |||
696 | Size aSize = GetOutputSizePixel(); | |||
697 | bool bSubEditPositioned = false; | |||
698 | ||||
699 | if (GetStyle() & (WB_SPIN | WB_DROPDOWN)) | |||
700 | { | |||
701 | ImplCalcButtonAreas( this, aSize, maDropDownRect, maUpperRect, maLowerRect ); | |||
702 | ||||
703 | ImplControlValue aControlValue; | |||
704 | Point aPoint; | |||
705 | tools::Rectangle aContent, aBound; | |||
706 | ||||
707 | // use the full extent of the control | |||
708 | vcl::Window *pBorder = GetWindow( GetWindowType::Border ); | |||
709 | tools::Rectangle aArea( aPoint, pBorder->GetOutputSizePixel() ); | |||
710 | ||||
711 | // adjust position and size of the edit field | |||
712 | if (GetNativeControlRegion(ControlType::Spinbox, ControlPart::SubEdit, aArea, ControlState::NONE, | |||
713 | aControlValue, aBound, aContent) && | |||
714 | // there is just no useful native support for spinfields with dropdown | |||
715 | !(GetStyle() & WB_DROPDOWN)) | |||
716 | { | |||
717 | // convert back from border space to local coordinates | |||
718 | aPoint = pBorder->ScreenToOutputPixel(OutputToScreenPixel(aPoint)); | |||
719 | aContent.Move(-aPoint.X(), -aPoint.Y()); | |||
720 | ||||
721 | // use the themes drop down size | |||
722 | mpEdit->SetPosPixel( aContent.TopLeft() ); | |||
723 | bSubEditPositioned = true; | |||
724 | aSize = aContent.GetSize(); | |||
725 | } | |||
726 | else | |||
727 | { | |||
728 | if (maUpperRect.IsEmpty()) | |||
729 | { | |||
730 | SAL_WARN_IF( maDropDownRect.IsEmpty(), "vcl", "SpinField::Resize: SPIN && DROPDOWN, but all empty rects?" )do { if (true && (maDropDownRect.IsEmpty())) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_WARN, "vcl")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case SAL_DETAIL_LOG_ACTION_LOG : if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart () << "SpinField::Resize: SPIN && DROPDOWN, but all empty rects?" ) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("vcl" ), ("/home/maarten/src/libreoffice/core/vcl/source/control/spinfld.cxx" ":" "730" ": "), ::sal::detail::unwrapStream( ::sal::detail:: StreamStart() << "SpinField::Resize: SPIN && DROPDOWN, but all empty rects?" ), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream << "SpinField::Resize: SPIN && DROPDOWN, but all empty rects?" ; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("vcl"), ("/home/maarten/src/libreoffice/core/vcl/source/control/spinfld.cxx" ":" "730" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL : if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart () << "SpinField::Resize: SPIN && DROPDOWN, but all empty rects?" ) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("vcl" ), ("/home/maarten/src/libreoffice/core/vcl/source/control/spinfld.cxx" ":" "730" ": "), ::sal::detail::unwrapStream( ::sal::detail:: StreamStart() << "SpinField::Resize: SPIN && DROPDOWN, but all empty rects?" ), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream << "SpinField::Resize: SPIN && DROPDOWN, but all empty rects?" ; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("vcl"), ("/home/maarten/src/libreoffice/core/vcl/source/control/spinfld.cxx" ":" "730" ": "), sal_detail_stream, 0); }; std::abort(); break ; } } } while (false); | |||
731 | aSize.setWidth( maDropDownRect.Left() ); | |||
732 | } | |||
733 | else | |||
734 | aSize.setWidth( maUpperRect.Left() ); | |||
735 | } | |||
736 | } | |||
737 | ||||
738 | if (!bSubEditPositioned) | |||
739 | { | |||
740 | // this moves our sub edit if RTL gets switched | |||
741 | mpEdit->SetPosPixel(Point()); | |||
742 | } | |||
743 | mpEdit->SetSizePixel(aSize); | |||
744 | ||||
745 | if (GetStyle() & WB_SPIN) | |||
746 | Invalidate(tools::Rectangle(maUpperRect.TopLeft(), maLowerRect.BottomRight())); | |||
747 | if (GetStyle() & WB_DROPDOWN) | |||
748 | Invalidate(maDropDownRect); | |||
749 | } | |||
750 | ||||
751 | void SpinField::StateChanged(StateChangedType nType) | |||
752 | { | |||
753 | Edit::StateChanged(nType); | |||
754 | ||||
755 | if (nType == StateChangedType::Enable) | |||
756 | { | |||
757 | if (mbSpin || (GetStyle() & WB_DROPDOWN)) | |||
758 | { | |||
759 | mpEdit->Enable(IsEnabled()); | |||
760 | ||||
761 | if (mbSpin) | |||
762 | { | |||
763 | Invalidate(maLowerRect); | |||
764 | Invalidate(maUpperRect); | |||
765 | } | |||
766 | if (GetStyle() & WB_DROPDOWN) | |||
767 | Invalidate(maDropDownRect); | |||
768 | } | |||
769 | } | |||
770 | else if (nType == StateChangedType::Style) | |||
771 | { | |||
772 | if (GetStyle() & WB_REPEAT) | |||
773 | mbRepeat = true; | |||
774 | else | |||
775 | mbRepeat = false; | |||
776 | } | |||
777 | else if (nType == StateChangedType::Zoom) | |||
778 | { | |||
779 | Resize(); | |||
780 | if (mpEdit) | |||
781 | mpEdit->SetZoom(GetZoom()); | |||
782 | Invalidate(); | |||
783 | } | |||
784 | else if (nType == StateChangedType::ControlFont) | |||
785 | { | |||
786 | if (mpEdit) | |||
787 | mpEdit->SetControlFont(GetControlFont()); | |||
788 | Invalidate(); | |||
789 | } | |||
790 | else if (nType == StateChangedType::ControlForeground) | |||
791 | { | |||
792 | if (mpEdit) | |||
793 | mpEdit->SetControlForeground(GetControlForeground()); | |||
794 | Invalidate(); | |||
795 | } | |||
796 | else if (nType == StateChangedType::ControlBackground) | |||
797 | { | |||
798 | if (mpEdit) | |||
799 | mpEdit->SetControlBackground(GetControlBackground()); | |||
800 | Invalidate(); | |||
801 | } | |||
802 | else if( nType == StateChangedType::Mirroring ) | |||
803 | { | |||
804 | if (mpEdit) | |||
805 | mpEdit->CompatStateChanged(StateChangedType::Mirroring); | |||
806 | Resize(); | |||
807 | } | |||
808 | } | |||
809 | ||||
810 | void SpinField::DataChanged( const DataChangedEvent& rDCEvt ) | |||
811 | { | |||
812 | Edit::DataChanged(rDCEvt); | |||
813 | ||||
814 | if ((rDCEvt.GetType() == DataChangedEventType::SETTINGS) && | |||
815 | (rDCEvt.GetFlags() & AllSettingsFlags::STYLE)) | |||
816 | { | |||
817 | Resize(); | |||
818 | Invalidate(); | |||
819 | } | |||
820 | } | |||
821 | ||||
822 | tools::Rectangle* SpinField::ImplFindPartRect(const Point& rPt) | |||
823 | { | |||
824 | if (maUpperRect.IsInside(rPt)) | |||
825 | return &maUpperRect; | |||
826 | else if (maLowerRect.IsInside(rPt)) | |||
827 | return &maLowerRect; | |||
828 | else | |||
829 | return nullptr; | |||
830 | } | |||
831 | ||||
832 | bool SpinField::PreNotify(NotifyEvent& rNEvt) | |||
833 | { | |||
834 | if (rNEvt.GetType() == MouseNotifyEvent::MOUSEMOVE) | |||
835 | { | |||
836 | const MouseEvent* pMouseEvt = rNEvt.GetMouseEvent(); | |||
837 | if (pMouseEvt && !pMouseEvt->GetButtons() && !pMouseEvt->IsSynthetic() && !pMouseEvt->IsModifierChanged()) | |||
838 | { | |||
839 | // trigger redraw if mouse over state has changed | |||
840 | if( IsNativeControlSupported(ControlType::Spinbox, ControlPart::Entire) || | |||
841 | IsNativeControlSupported(ControlType::Spinbox, ControlPart::AllButtons) ) | |||
842 | { | |||
843 | tools::Rectangle* pRect = ImplFindPartRect( GetPointerPosPixel() ); | |||
844 | tools::Rectangle* pLastRect = ImplFindPartRect( GetLastPointerPosPixel() ); | |||
845 | if( pRect != pLastRect || (pMouseEvt->IsLeaveWindow() || pMouseEvt->IsEnterWindow()) ) | |||
846 | { | |||
847 | // FIXME: this is currently only on macOS | |||
848 | // check for other platforms that need similar handling | |||
849 | if (ImplGetSVData()->maNWFData.mbNoFocusRects && IsNativeWidgetEnabled() && | |||
850 | IsNativeControlSupported(ControlType::Editbox, ControlPart::Entire)) | |||
851 | { | |||
852 | ImplInvalidateOutermostBorder(this); | |||
853 | } | |||
854 | else | |||
855 | { | |||
856 | // paint directly | |||
857 | vcl::Region aRgn( GetActiveClipRegion() ); | |||
858 | if (pLastRect) | |||
859 | { | |||
860 | SetClipRegion(vcl::Region(*pLastRect)); | |||
861 | Invalidate(*pLastRect); | |||
862 | SetClipRegion( aRgn ); | |||
863 | } | |||
864 | if (pRect) | |||
865 | { | |||
866 | SetClipRegion(vcl::Region(*pRect)); | |||
867 | Invalidate(*pRect); | |||
868 | SetClipRegion( aRgn ); | |||
869 | } | |||
870 | } | |||
871 | } | |||
872 | } | |||
873 | } | |||
874 | } | |||
875 | ||||
876 | return Edit::PreNotify(rNEvt); | |||
877 | } | |||
878 | ||||
879 | void SpinField::EndDropDown() | |||
880 | { | |||
881 | mbInDropDown = false; | |||
882 | Invalidate(tools::Rectangle(Point(), GetOutputSizePixel())); | |||
883 | } | |||
884 | ||||
885 | bool SpinField::ShowDropDown( bool ) | |||
886 | { | |||
887 | return false; | |||
888 | } | |||
889 | ||||
890 | Size SpinField::CalcMinimumSizeForText(const OUString &rString) const | |||
891 | { | |||
892 | Size aSz = Edit::CalcMinimumSizeForText(rString); | |||
893 | ||||
894 | if ( GetStyle() & WB_DROPDOWN ) | |||
895 | aSz.AdjustWidth(GetSettings().GetStyleSettings().GetScrollBarSize() ); | |||
896 | if ( GetStyle() & WB_SPIN ) | |||
897 | { | |||
898 | ImplControlValue aControlValue; | |||
899 | tools::Rectangle aArea( Point(), Size(100, aSz.Height())); | |||
900 | tools::Rectangle aEntireBound, aEntireContent, aEditBound, aEditContent; | |||
901 | if ( | |||
902 | GetNativeControlRegion(ControlType::Spinbox, ControlPart::Entire, | |||
903 | aArea, ControlState::NONE, aControlValue, aEntireBound, aEntireContent) && | |||
904 | GetNativeControlRegion(ControlType::Spinbox, ControlPart::SubEdit, | |||
905 | aArea, ControlState::NONE, aControlValue, aEditBound, aEditContent) | |||
906 | ) | |||
907 | { | |||
908 | aSz.AdjustWidth(aEntireContent.GetWidth() - aEditContent.GetWidth()); | |||
909 | } | |||
910 | else | |||
911 | { | |||
912 | aSz.AdjustWidth(maUpperRect.GetWidth() ); | |||
913 | } | |||
914 | } | |||
915 | ||||
916 | return aSz; | |||
917 | } | |||
918 | ||||
919 | Size SpinField::CalcMinimumSize() const | |||
920 | { | |||
921 | return CalcMinimumSizeForText(GetText()); | |||
922 | } | |||
923 | ||||
924 | Size SpinField::GetOptimalSize() const | |||
925 | { | |||
926 | return CalcMinimumSize(); | |||
927 | } | |||
928 | ||||
929 | Size SpinField::CalcSize(sal_Int32 nChars) const | |||
930 | { | |||
931 | Size aSz = Edit::CalcSize( nChars ); | |||
932 | ||||
933 | if ( GetStyle() & WB_DROPDOWN ) | |||
934 | aSz.AdjustWidth(GetSettings().GetStyleSettings().GetScrollBarSize() ); | |||
935 | if ( GetStyle() & WB_SPIN ) | |||
936 | aSz.AdjustWidth(GetSettings().GetStyleSettings().GetSpinSize() ); | |||
937 | ||||
938 | return aSz; | |||
939 | } | |||
940 | ||||
941 | IMPL_LINK( SpinField, ImplTimeout, Timer*, pTimer, void )void SpinField::LinkStubImplTimeout(void * instance, Timer* data ) { return static_cast<SpinField *>(instance)->ImplTimeout (data); } void SpinField::ImplTimeout(Timer* pTimer) | |||
942 | { | |||
943 | if ( pTimer->GetTimeout() == MouseSettings::GetButtonStartRepeat() ) | |||
944 | { | |||
945 | pTimer->SetTimeout( GetSettings().GetMouseSettings().GetButtonRepeat() ); | |||
946 | pTimer->Start(); | |||
947 | } | |||
948 | else | |||
949 | { | |||
950 | if ( mbInitialUp ) | |||
951 | Up(); | |||
952 | else | |||
953 | Down(); | |||
954 | } | |||
955 | } | |||
956 | ||||
957 | void SpinField::Draw(OutputDevice* pDev, const Point& rPos, DrawFlags nFlags) | |||
958 | { | |||
959 | Edit::Draw(pDev, rPos, nFlags); | |||
960 | ||||
961 | WinBits nFieldStyle = GetStyle(); | |||
962 | if ( (nFlags & DrawFlags::NoControls ) || !( nFieldStyle & (WB_SPIN|WB_DROPDOWN) ) ) | |||
963 | return; | |||
964 | ||||
965 | Point aPos = pDev->LogicToPixel( rPos ); | |||
966 | Size aSize = GetSizePixel(); | |||
967 | AllSettings aOldSettings = pDev->GetSettings(); | |||
968 | ||||
969 | pDev->Push(); | |||
970 | pDev->SetMapMode(); | |||
971 | ||||
972 | tools::Rectangle aDD, aUp, aDown; | |||
973 | ImplCalcButtonAreas(pDev, aSize, aDD, aUp, aDown); | |||
974 | aDD.Move(aPos.X(), aPos.Y()); | |||
975 | aUp.Move(aPos.X(), aPos.Y()); | |||
976 | aUp.AdjustTop( 1 ); | |||
977 | aDown.Move(aPos.X(), aPos.Y()); | |||
978 | ||||
979 | Color aButtonTextColor; | |||
980 | if (nFlags & DrawFlags::Mono) | |||
981 | aButtonTextColor = COL_BLACK; | |||
982 | else | |||
983 | aButtonTextColor = GetSettings().GetStyleSettings().GetButtonTextColor(); | |||
984 | ||||
985 | if (GetStyle() & WB_DROPDOWN) | |||
986 | { | |||
987 | DecorationView aView( pDev ); | |||
988 | tools::Rectangle aInnerRect = aView.DrawButton( aDD, DrawButtonFlags::NoLightBorder ); | |||
989 | DrawSymbolFlags nSymbolStyle = IsEnabled() ? DrawSymbolFlags::NONE : DrawSymbolFlags::Disable; | |||
990 | aView.DrawSymbol(aInnerRect, SymbolType::SPIN_DOWN, aButtonTextColor, nSymbolStyle); | |||
991 | } | |||
992 | ||||
993 | if (GetStyle() & WB_SPIN) | |||
994 | { | |||
995 | ImplDrawSpinButton(*pDev, this, aUp, aDown, false, false); | |||
996 | } | |||
997 | ||||
998 | pDev->Pop(); | |||
999 | pDev->SetSettings(aOldSettings); | |||
1000 | ||||
1001 | } | |||
1002 | ||||
1003 | FactoryFunction SpinField::GetUITestFactory() const | |||
1004 | { | |||
1005 | return SpinFieldUIObject::create; | |||
1006 | } | |||
1007 | ||||
1008 | /* 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 |