1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <rtl/ustrbuf.hxx>
#include <utility>
#include <vcl/menubarupdateicon.hxx>
#include <vcl/lineinfo.hxx>
#include <vcl/settings.hxx>
#include <vcl/svapp.hxx>
#include <unotools/resmgr.hxx>
#include <bubblewindow.hxx>
#include <bitmaps.hlst>
#define TIP_HEIGHT 15
#define TIP_WIDTH 7
#define TIP_RIGHT_OFFSET 18
#define BUBBLE_BORDER 10
#define TEXT_MAX_WIDTH 300
#define TEXT_MAX_HEIGHT 200
BubbleWindow::BubbleWindow( vcl::Window* pParent, OUString aTitle,
OUString aText, Image aImage )
: FloatingWindow( pParent, WB_SYSTEMWINDOW
| WB_OWNERDRAWDECORATION
| WB_NOBORDER
)
, maBubbleTitle(std::move( aTitle ))
, maBubbleText(std::move( aText ))
, maBubbleImage(std::move( aImage ))
, maMaxTextSize( TEXT_MAX_WIDTH, TEXT_MAX_HEIGHT )
, mnTipOffset( 0 )
{
SetBackground( Wallpaper( GetSettings().GetStyleSettings().GetHelpColor() ) );
}
void BubbleWindow::Resize()
{
FloatingWindow::Resize();
Size aSize = GetSizePixel();
if ( ( aSize.Height() < 20 ) || ( aSize.Width() < 60 ) )
return;
tools::Rectangle aRect( 0, TIP_HEIGHT, aSize.Width(), aSize.Height() - TIP_HEIGHT );
maRectPoly = tools::Polygon( aRect, 6, 6 );
vcl::Region aRegion( maRectPoly );
tools::Long nTipOffset = aSize.Width() - TIP_RIGHT_OFFSET + mnTipOffset;
Point aPointArr[4];
aPointArr[0] = Point( nTipOffset, TIP_HEIGHT );
aPointArr[1] = Point( nTipOffset, 0 );
aPointArr[2] = Point( nTipOffset + TIP_WIDTH , TIP_HEIGHT );
aPointArr[3] = Point( nTipOffset, TIP_HEIGHT );
maTriPoly = tools::Polygon( 4, aPointArr );
vcl::Region aTriRegion( maTriPoly );
aRegion.Union( aTriRegion);
maBounds = aRegion;
SetWindowRegionPixel( maBounds );
}
void BubbleWindow::SetTitleAndText( const OUString& rTitle,
const OUString& rText,
const Image& rImage )
{
maBubbleTitle = rTitle;
maBubbleText = rText;
maBubbleImage = rImage;
Resize();
}
void BubbleWindow::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& /*rRect*/)
{
LineInfo aThickLine( LineStyle::Solid, 2 );
rRenderContext.DrawPolyLine( maRectPoly, aThickLine );
rRenderContext.DrawPolyLine( maTriPoly );
Color aOldLine = rRenderContext.GetLineColor();
Size aSize = GetSizePixel();
tools::Long nTipOffset = aSize.Width() - TIP_RIGHT_OFFSET + mnTipOffset;
rRenderContext.SetLineColor( GetSettings().GetStyleSettings().GetHelpColor() );
rRenderContext.DrawLine( Point( nTipOffset+2, TIP_HEIGHT ),
Point( nTipOffset + TIP_WIDTH -1 , TIP_HEIGHT ),
aThickLine );
rRenderContext.SetLineColor( aOldLine );
Size aImgSize = maBubbleImage.GetSizePixel();
rRenderContext.DrawImage( Point( BUBBLE_BORDER, BUBBLE_BORDER + TIP_HEIGHT ), maBubbleImage );
vcl::Font aOldFont = GetFont();
vcl::Font aBoldFont = aOldFont;
aBoldFont.SetWeight( WEIGHT_BOLD );
SetFont( aBoldFont );
tools::Rectangle aTitleRect = maTitleRect;
aTitleRect.Move( aImgSize.Width(), 0 );
rRenderContext.DrawText( aTitleRect, maBubbleTitle, DrawTextFlags::MultiLine | DrawTextFlags::WordBreak );
SetFont( aOldFont );
tools::Rectangle aTextRect = maTextRect;
aTextRect.Move( aImgSize.Width(), 0 );
rRenderContext.DrawText( aTextRect, maBubbleText, DrawTextFlags::MultiLine | DrawTextFlags::WordBreak );
}
void BubbleWindow::MouseButtonDown( const MouseEvent& )
{
Show( false );
}
void BubbleWindow::Show( bool bVisible )
{
if ( !bVisible )
{
FloatingWindow::Show( bVisible );
return;
}
// don't show bubbles without a text
if ( ( maBubbleTitle.isEmpty() ) && ( maBubbleText.isEmpty() ) )
return;
Size aWindowSize = GetSizePixel();
Size aImgSize = maBubbleImage.GetSizePixel();
RecalcTextRects();
aWindowSize.setHeight( maTitleRect.GetHeight() * 7 / 4+ maTextRect.GetHeight() +
3 * BUBBLE_BORDER + TIP_HEIGHT );
if ( maTitleRect.GetWidth() > maTextRect.GetWidth() )
aWindowSize.setWidth( maTitleRect.GetWidth() );
else
aWindowSize.setWidth( maTextRect.GetWidth() );
aWindowSize.setWidth( aWindowSize.Width() + 3 * BUBBLE_BORDER + aImgSize.Width() );
if ( aWindowSize.Height() < aImgSize.Height() + TIP_HEIGHT + 2 * BUBBLE_BORDER )
aWindowSize.setHeight( aImgSize.Height() + TIP_HEIGHT + 2 * BUBBLE_BORDER );
Point aPos;
aPos.setX( maTipPos.X() - aWindowSize.Width() + TIP_RIGHT_OFFSET );
aPos.setY( maTipPos.Y() );
AbsoluteScreenPixelPoint aScreenPos = GetParent()->OutputToAbsoluteScreenPixel( aPos );
if ( aScreenPos.X() < 0 )
{
mnTipOffset = aScreenPos.X();
aPos.AdjustX( -mnTipOffset );
}
SetPosSizePixel( aPos, aWindowSize );
FloatingWindow::Show( bVisible, ShowFlags::NoActivate );
}
void BubbleWindow::RecalcTextRects()
{
Size aTotalSize;
bool bFinished = false;
vcl::Font aOldFont = GetFont();
vcl::Font aBoldFont = aOldFont;
aBoldFont.SetWeight( WEIGHT_BOLD );
while ( !bFinished )
{
SetFont( aBoldFont );
maTitleRect = GetTextRect( tools::Rectangle( Point( 0, 0 ), maMaxTextSize ),
maBubbleTitle,
DrawTextFlags::MultiLine | DrawTextFlags::WordBreak );
SetFont( aOldFont );
maTextRect = GetTextRect( tools::Rectangle( Point( 0, 0 ), maMaxTextSize ),
maBubbleText,
DrawTextFlags::MultiLine | DrawTextFlags::WordBreak );
if ( maTextRect.GetHeight() < 10 )
maTextRect.setHeight( 10 );
aTotalSize.setHeight( maTitleRect.GetHeight() +
aBoldFont.GetFontHeight() * 3 / 4 +
maTextRect.GetHeight() +
3 * BUBBLE_BORDER + TIP_HEIGHT );
if ( aTotalSize.Height() > maMaxTextSize.Height() )
{
maMaxTextSize.setWidth( maMaxTextSize.Width() * 3 / 2 );
maMaxTextSize.setHeight( maMaxTextSize.Height() * 3 / 2 );
}
else
bFinished = true;
}
maTitleRect.Move( 2*BUBBLE_BORDER, BUBBLE_BORDER + TIP_HEIGHT );
maTextRect.Move( 2*BUBBLE_BORDER, BUBBLE_BORDER + TIP_HEIGHT + maTitleRect.GetHeight() + aBoldFont.GetFontHeight() * 3 / 4 );
}
MenuBarUpdateIconManager::MenuBarUpdateIconManager()
: maTimeoutTimer("MenuBarUpdateIconManager")
, maWaitIdle("vcl MenuBarUpdateIconManager maWaitIdle")
, mbShowMenuIcon(false)
, mbShowBubble(false)
, mbBubbleChanged( false )
{
maTimeoutTimer.SetTimeout( 10000 );
maTimeoutTimer.SetInvokeHandler(LINK(this, MenuBarUpdateIconManager, TimeOutHdl));
maWaitIdle.SetPriority( TaskPriority::LOWEST );
maWaitIdle.SetInvokeHandler(LINK(this, MenuBarUpdateIconManager, WaitTimeOutHdl));
maApplicationEventHdl = LINK(this, MenuBarUpdateIconManager, ApplicationEventHdl);
Application::AddEventListener( maApplicationEventHdl );
maWindowEventHdl = LINK(this, MenuBarUpdateIconManager, WindowEventHdl);
}
sal_uInt16 MenuBarUpdateIconManager::GetIconID(MenuBar* pMenuBar) const
{
auto aI = std::find(maIconMBars.begin(), maIconMBars.end(), pMenuBar);
if (aI == maIconMBars.end())
return 0;
return maIconIDs[std::distance(maIconMBars.begin(), aI)];
}
VclPtr<BubbleWindow> MenuBarUpdateIconManager::GetBubbleWindow()
{
if (!mpActiveSysWin)
return nullptr;
tools::Rectangle aIconRect = mpActiveMBar->GetMenuBarButtonRectPixel(GetIconID(mpActiveMBar));
if( aIconRect.IsEmpty() )
return nullptr;
auto pBubbleWin = mpBubbleWin;
if ( !pBubbleWin ) {
pBubbleWin = VclPtr<BubbleWindow>::Create( mpActiveSysWin, maBubbleTitle,
maBubbleText, maBubbleImage );
mbBubbleChanged = false;
}
else if ( mbBubbleChanged ) {
pBubbleWin->SetTitleAndText( maBubbleTitle, maBubbleText,
maBubbleImage );
mbBubbleChanged = false;
}
Point aWinPos = aIconRect.BottomCenter();
pBubbleWin->SetTipPosPixel( aWinPos );
return pBubbleWin;
}
IMPL_LINK_NOARG(MenuBarUpdateIconManager, TimeOutHdl, Timer *, void)
{
RemoveBubbleWindow();
}
IMPL_LINK(MenuBarUpdateIconManager, WindowEventHdl, VclWindowEvent&, rEvent, void)
{
VclEventId nEventID = rEvent.GetId();
if ( VclEventId::ObjectDying == nEventID )
{
if (mpActiveSysWin == rEvent.GetWindow())
{
RemoveBubbleWindow();
mpActiveSysWin = nullptr;
mpActiveMBar = nullptr;
}
}
else if ( VclEventId::WindowMenubarAdded == nEventID )
{
vcl::Window *pWindow = rEvent.GetWindow();
if ( pWindow )
{
SystemWindow *pSysWin = pWindow->GetSystemWindow();
if (pSysWin)
AddMenuBarIcon(*pSysWin, false);
}
}
else if ( VclEventId::WindowMenubarRemoved == nEventID )
{
MenuBar *pMBar = static_cast<MenuBar*>(rEvent.GetData());
if (pMBar)
{
if (pMBar == mpActiveMBar)
{
RemoveBubbleWindow();
mpActiveMBar = nullptr;
}
RemoveMenuBarIcon(pMBar);
}
}
else if ( ( nEventID == VclEventId::WindowMove ) ||
( nEventID == VclEventId::WindowResize ) )
{
if ( mpActiveSysWin == rEvent.GetWindow() &&
mpBubbleWin && mpActiveMBar )
{
tools::Rectangle aIconRect = mpActiveMBar->GetMenuBarButtonRectPixel(GetIconID(mpActiveMBar));
Point aWinPos = aIconRect.BottomCenter();
mpBubbleWin->SetTipPosPixel( aWinPos );
if ( mpBubbleWin->IsVisible() )
mpBubbleWin->Show(); // This will recalc the screen position of the bubble
}
}
}
IMPL_LINK(MenuBarUpdateIconManager, ApplicationEventHdl, VclSimpleEvent&, rEvent, void)
{
switch (rEvent.GetId())
{
case VclEventId::WindowShow:
case VclEventId::WindowActivate:
case VclEventId::WindowGetFocus: {
vcl::Window *pWindow = static_cast< VclWindowEvent * >(&rEvent)->GetWindow();
if ( pWindow && pWindow->IsTopWindow() )
{
SystemWindow *pSysWin = pWindow->GetSystemWindow();
MenuBar *pMBar = pSysWin ? pSysWin->GetMenuBar() : nullptr;
if (pMBar)
AddMenuBarIcon(*pSysWin, true);
}
break;
}
default: break;
}
}
IMPL_LINK_NOARG(MenuBarUpdateIconManager, UserEventHdl, void*, void)
{
vcl::Window *pTopWin = Application::GetFirstTopLevelWindow();
vcl::Window *pActiveWin = Application::GetActiveTopWindow();
SystemWindow *pActiveSysWin = nullptr;
vcl::Window *pBubbleWin = nullptr;
if ( mpBubbleWin )
pBubbleWin = mpBubbleWin;
if ( pActiveWin && ( pActiveWin != pBubbleWin ) && pActiveWin->IsTopWindow() )
pActiveSysWin = pActiveWin->GetSystemWindow();
if ( pActiveWin == pBubbleWin )
pActiveSysWin = nullptr;
while ( !pActiveSysWin && pTopWin )
{
if ( ( pTopWin != pBubbleWin ) && pTopWin->IsTopWindow() )
pActiveSysWin = pTopWin->GetSystemWindow();
if ( !pActiveSysWin )
pTopWin = Application::GetNextTopLevelWindow( pTopWin );
}
if ( pActiveSysWin )
AddMenuBarIcon(*pActiveSysWin, true);
}
IMPL_LINK_NOARG(MenuBarUpdateIconManager, ClickHdl, MenuBarButtonCallbackArg&, bool)
{
maWaitIdle.Stop();
if ( mpBubbleWin )
mpBubbleWin->Show( false );
maClickHdl.Call(nullptr);
return false;
}
IMPL_LINK(MenuBarUpdateIconManager, HighlightHdl, MenuBarButtonCallbackArg&, rData, bool)
{
if ( rData.bHighlight )
maWaitIdle.Start();
else
RemoveBubbleWindow();
return false;
}
IMPL_LINK_NOARG(MenuBarUpdateIconManager, WaitTimeOutHdl, Timer *, void)
{
mpBubbleWin = GetBubbleWindow();
if ( mpBubbleWin )
{
mpBubbleWin->Show();
}
}
MenuBarUpdateIconManager::~MenuBarUpdateIconManager()
{
Application::RemoveEventListener( maApplicationEventHdl );
RemoveBubbleWindow();
RemoveMenuBarIcons();
}
void MenuBarUpdateIconManager::RemoveMenuBarIcons()
{
while (!maIconMBars.empty())
RemoveMenuBarIcon(maIconMBars[0]);
}
void MenuBarUpdateIconManager::SetShowMenuIcon(bool bShowMenuIcon)
{
if ( bShowMenuIcon != mbShowMenuIcon )
{
mbShowMenuIcon = bShowMenuIcon;
if ( bShowMenuIcon )
Application::PostUserEvent(LINK(this, MenuBarUpdateIconManager, UserEventHdl));
else
{
RemoveBubbleWindow();
RemoveMenuBarIcons();
}
}
}
void MenuBarUpdateIconManager::SetShowBubble(bool bShowBubble)
{
mbShowBubble = bShowBubble;
if ( mbShowBubble )
Application::PostUserEvent(LINK(this, MenuBarUpdateIconManager, UserEventHdl));
else if ( mpBubbleWin )
mpBubbleWin->Show( false );
}
void MenuBarUpdateIconManager::SetBubbleChanged()
{
mbBubbleChanged = true;
if (mbBubbleChanged && mpBubbleWin)
mpBubbleWin->Show( false );
}
void MenuBarUpdateIconManager::SetBubbleImage(const Image& rImage)
{
maBubbleImage = rImage;
SetBubbleChanged();
}
void MenuBarUpdateIconManager::SetBubbleTitle(const OUString& rTitle)
{
if (rTitle != maBubbleTitle)
{
maBubbleTitle = rTitle;
SetBubbleChanged();
}
}
void MenuBarUpdateIconManager::SetBubbleText(const OUString& rText)
{
if (rText != maBubbleText)
{
maBubbleText = rText;
SetBubbleChanged();
}
}
namespace {
Image GetMenuBarIcon( MenuBar const * pMBar )
{
OUString sResID;
vcl::Window *pMBarWin = pMBar->GetWindow();
sal_uInt32 nMBarHeight = 20;
if ( pMBarWin )
nMBarHeight = pMBarWin->GetOutputSizePixel().getHeight();
if (nMBarHeight >= 35)
sResID = RID_UPDATE_AVAILABLE_26;
else
sResID = RID_UPDATE_AVAILABLE_16;
return Image(StockImage::Yes, sResID);
}
}
void MenuBarUpdateIconManager::AddMenuBarIcon(SystemWindow& rSysWin, bool bAddEventHdl)
{
if (!mbShowMenuIcon)
return;
MenuBar *pActiveMBar = rSysWin.GetMenuBar();
if (&rSysWin != mpActiveSysWin || pActiveMBar != mpActiveMBar)
RemoveBubbleWindow();
auto aI = std::find(maIconMBars.begin(), maIconMBars.end(), pActiveMBar);
if (aI == maIconMBars.end())
{
if (pActiveMBar)
{
OUStringBuffer aBuf;
if( !maBubbleTitle.isEmpty() )
aBuf.append( maBubbleTitle );
if( !maBubbleText.isEmpty() )
{
if( !maBubbleTitle.isEmpty() )
aBuf.append( "\n\n" );
aBuf.append( maBubbleText );
}
Image aImage = GetMenuBarIcon( pActiveMBar );
sal_uInt16 nIconID = pActiveMBar->AddMenuBarButton( aImage,
LINK( this, MenuBarUpdateIconManager, ClickHdl ),
aBuf.makeStringAndClear() );
maIconMBars.push_back(pActiveMBar);
maIconIDs.push_back(nIconID);
}
if (bAddEventHdl)
rSysWin.AddEventListener( maWindowEventHdl );
}
if (mpActiveMBar != pActiveMBar)
{
if (mpActiveMBar)
{
mpActiveMBar->SetMenuBarButtonHighlightHdl(GetIconID(mpActiveMBar),
Link<MenuBarButtonCallbackArg&,bool>());
}
mpActiveMBar = pActiveMBar;
if (mpActiveMBar)
{
mpActiveMBar->SetMenuBarButtonHighlightHdl(GetIconID(mpActiveMBar),
LINK(this, MenuBarUpdateIconManager, HighlightHdl));
}
}
mpActiveSysWin = &rSysWin;
if (mbShowBubble && pActiveMBar)
{
mpBubbleWin = GetBubbleWindow();
if ( mpBubbleWin )
{
mpBubbleWin->Show();
maTimeoutTimer.Start();
}
mbShowBubble = false;
}
}
void MenuBarUpdateIconManager::RemoveMenuBarIcon(MenuBar* pMenuBar)
{
auto aI = std::find(maIconMBars.begin(), maIconMBars.end(), pMenuBar);
if (aI == maIconMBars.end())
return;
auto aIconI = maIconIDs.begin() + std::distance(maIconMBars.begin(), aI);
try
{
pMenuBar->RemoveMenuBarButton(*aIconI);
}
catch (...)
{
}
maIconMBars.erase(aI);
maIconIDs.erase(aIconI);
}
void MenuBarUpdateIconManager::RemoveBubbleWindow()
{
maWaitIdle.Stop();
maTimeoutTimer.Stop();
mpBubbleWin.disposeAndClear();
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|