xref: /aoo41x/main/svtools/source/control/taskbar.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svtools.hxx"
30 
31 #define _TASKBAR_CXX
32 
33 #include <tools/list.hxx>
34 #include <tools/debug.hxx>
35 #include <vcl/floatwin.hxx>
36 
37 #include <svtools/taskbar.hxx>
38 
39 // =======================================================================
40 
41 class ImplTaskBarFloat : public FloatingWindow
42 {
43 public:
44 	TaskBar*			mpTaskBar;
45 
46 public:
47 						ImplTaskBarFloat( TaskBar* pTaskBar );
48 };
49 
50 // -----------------------------------------------------------------------
51 
52 ImplTaskBarFloat::ImplTaskBarFloat( TaskBar* pTaskBar ) :
53 	FloatingWindow( pTaskBar, 0 )
54 {
55 	mpTaskBar	= pTaskBar;
56 }
57 
58 // =======================================================================
59 
60 #define TASKBAR_BORDER				2
61 #define TASKBAR_OFFSIZE 			3
62 #define TASKBAR_OFFX				2
63 #define TASKBAR_OFFY				1
64 #define TASKBAR_BUTTONOFF			5
65 #define TASKBAR_AUTOHIDE_HEIGHT 	2
66 
67 // =======================================================================
68 
69 TaskBar::TaskBar( Window* pParent, WinBits nWinStyle ) :
70 	Window( pParent, WB_3DLOOK )
71 {
72 	mpButtonBar 		= NULL;
73 	mpTaskToolBox		= NULL;
74 	mpStatusBar 		= NULL;
75 	mnStatusWidth		= 0;
76 	mnOldStatusWidth	= 0;
77 	mnLines 			= 1;
78 	mnWinBits			= nWinStyle;
79 	mbStatusText		= sal_False;
80 	mbShowItems 		= sal_False;
81 	mbAutoHide			= sal_False;
82 
83 	ImplInitSettings();
84 }
85 
86 // -----------------------------------------------------------------------
87 
88 TaskBar::~TaskBar()
89 {
90 	if ( mpButtonBar )
91 		delete mpButtonBar;
92 	if ( mpTaskToolBox )
93 		delete mpTaskToolBox;
94 	if ( mpStatusBar )
95 		delete mpStatusBar;
96 }
97 
98 // -----------------------------------------------------------------------
99 
100 void TaskBar::ImplInitSettings()
101 {
102 	const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
103 
104 	Color aColor;
105 	if ( IsControlBackground() )
106 		aColor = GetControlBackground();
107 	else if ( Window::GetStyle() & WB_3DLOOK )
108 		aColor = rStyleSettings.GetFaceColor();
109 	else
110 		aColor = rStyleSettings.GetWindowColor();
111 	SetBackground( aColor );
112 }
113 
114 // -----------------------------------------------------------------------
115 
116 void TaskBar::ImplNewHeight( long nNewHeight )
117 {
118 	long nOldHeight = GetSizePixel().Height();
119 	if ( nNewHeight != nOldHeight )
120 	{
121 		long nY = GetPosPixel().Y()-(nNewHeight-nOldHeight);
122 		SetPosSizePixel( 0, nY, 0, nNewHeight,
123 						 WINDOW_POSSIZE_Y | WINDOW_POSSIZE_HEIGHT );
124 		TaskResize();
125 	}
126 }
127 
128 // -----------------------------------------------------------------------
129 
130 void TaskBar::TaskResize()
131 {
132 	maTaskResizeHdl.Call( this );
133 }
134 
135 // -----------------------------------------------------------------------
136 
137 TaskButtonBar* TaskBar::CreateButtonBar()
138 {
139 	return new TaskButtonBar( this );
140 }
141 
142 // -----------------------------------------------------------------------
143 
144 TaskToolBox* TaskBar::CreateTaskToolBox()
145 {
146 	return new TaskToolBox( this );
147 }
148 
149 // -----------------------------------------------------------------------
150 
151 TaskStatusBar* TaskBar::CreateTaskStatusBar()
152 {
153 	return new TaskStatusBar( this );
154 }
155 
156 // -----------------------------------------------------------------------
157 
158 void TaskBar::MouseMove( const MouseEvent& rMEvt )
159 {
160 	if ( mnWinBits & WB_SIZEABLE )
161 	{
162 		TaskToolBox*	pTempTaskToolBox = GetTaskToolBox();
163 		TaskStatusBar*	pTempStatusBar = GetStatusBar();
164 
165 		if ( pTempTaskToolBox && pTempStatusBar )
166 		{
167 			long			nStatusX = pTempStatusBar->GetPosPixel().X()-TASKBAR_OFFSIZE-2;
168 			long			nMouseX = rMEvt.GetPosPixel().X();
169 			PointerStyle	ePtrStyle;
170 			if ( (nMouseX >= nStatusX-1) && (nMouseX <= nStatusX+3) )
171 				ePtrStyle = POINTER_HSIZEBAR;
172 			else
173 				ePtrStyle = POINTER_ARROW;
174 			Pointer aPtr( ePtrStyle );
175 			SetPointer( aPtr );
176 		}
177 	}
178 }
179 
180 // -----------------------------------------------------------------------
181 
182 void TaskBar::MouseButtonDown( const MouseEvent& rMEvt )
183 {
184 	if ( rMEvt.IsLeft() && (mnWinBits & WB_SIZEABLE) )
185 	{
186 		TaskToolBox*	pTempTaskToolBox = GetTaskToolBox();
187 		TaskStatusBar*	pTempStatusBar = GetStatusBar();
188 
189 		if ( pTempTaskToolBox && pTempStatusBar )
190 		{
191 			long	nStatusX = pTempStatusBar->GetPosPixel().X()-TASKBAR_OFFSIZE-2;
192 			long	nMouseX = rMEvt.GetPosPixel().X();
193 			if ( (nMouseX >= nStatusX-1) && (nMouseX <= nStatusX+3) )
194 			{
195 				if ( rMEvt.GetClicks() == 2 )
196 				{
197 					if ( mnStatusWidth )
198 					{
199 						mnStatusWidth = 0;
200 						Resize();
201 					}
202 				}
203 				else
204 				{
205 					StartTracking();
206 					mnOldStatusWidth = mnStatusWidth;
207 					mnMouseOff = nMouseX-nStatusX;
208 				}
209 			}
210 		}
211 	}
212 }
213 
214 // -----------------------------------------------------------------------
215 
216 void TaskBar::Tracking( const TrackingEvent& rTEvt )
217 {
218 	if ( rTEvt.IsTrackingEnded() )
219 	{
220 		if ( rTEvt.IsTrackingCanceled() )
221 		{
222 			mnStatusWidth = mnOldStatusWidth;
223 			Resize();
224 			Update();
225 		}
226 	}
227 	else
228 	{
229 		Size aSize = GetOutputSizePixel();
230 
231 		long nMouseX = rTEvt.GetMouseEvent().GetPosPixel().X()-mnMouseOff;
232 		if ( nMouseX < 0 )
233 			nMouseX = 0;
234 		long nMaxX = aSize.Width()-TASKBAR_OFFX-TASKBAR_OFFSIZE-1;
235 		if ( nMouseX > nMaxX )
236 			nMouseX = nMaxX;
237 		mnStatusWidth = aSize.Width()-nMouseX-TASKBAR_OFFX-TASKBAR_OFFSIZE;
238 		Resize();
239 		Update();
240 	}
241 }
242 
243 // -----------------------------------------------------------------------
244 
245 void TaskBar::Paint( const Rectangle& rRect )
246 {
247 	if ( mnWinBits & (WB_BORDER | WB_SIZEABLE) )
248 	{
249 		const StyleSettings&	rStyleSettings = GetSettings().GetStyleSettings();
250 		Size					aSize = GetOutputSizePixel();
251 		long	nY = 0;
252 
253 		if ( mnWinBits & WB_BORDER )
254 		{
255 			SetLineColor( rStyleSettings.GetShadowColor() );
256 			DrawLine( Point( 0, 0 ), Point( aSize.Width()-1, 0 ) );
257 			SetLineColor( rStyleSettings.GetLightColor() );
258 			DrawLine( Point( 0, 1 ), Point( aSize.Width()-1, 1 ) );
259 			nY += 2;
260 		}
261 
262 		if ( (mnWinBits & WB_SIZEABLE) )
263 		{
264 			//TaskButtonBar*	pTempButtonBar = GetButtonBar();
265 			TaskToolBox*	pTempTaskToolBox = GetTaskToolBox();
266 			TaskStatusBar*	pTempStatusBar = GetStatusBar();
267 
268 			if ( pTempTaskToolBox && pTempStatusBar )
269 			{
270 				long nStatusX = pTempStatusBar->GetPosPixel().X()-TASKBAR_OFFSIZE-2;
271 				if ( nStatusX > 0 )
272 				{
273 					SetLineColor( rStyleSettings.GetShadowColor() );
274 					DrawLine( Point( nStatusX, nY ), Point( nStatusX, aSize.Height()-1 ) );
275 					nStatusX++;
276 					SetLineColor( rStyleSettings.GetLightColor() );
277 					DrawLine( Point( nStatusX, nY ), Point( nStatusX, aSize.Height()-1 ) );
278 				}
279 			}
280 		}
281 	}
282 
283 	Window::Paint( rRect );
284 }
285 
286 // -----------------------------------------------------------------------
287 
288 void TaskBar::Resize()
289 {
290 	if ( !IsReallyShown() )
291 		return;
292 
293 	TaskButtonBar*	pTempButtonBar = GetButtonBar();
294 	TaskToolBox*	pTempTaskToolBox = GetTaskToolBox();
295 	TaskStatusBar*	pTempStatusBar = GetStatusBar();
296 	Point			aToolPos( TASKBAR_OFFX, 0 );
297 	Size			aSize = GetOutputSizePixel();
298 	Size			aStatusSize;
299 	Size			aToolSize( aSize.Width()-(TASKBAR_OFFX*2), 0 );
300 	long			nOldStatusX = -1;
301 	long			nNewStatusX = -1;
302 	long			nTaskHeight = aSize.Height() - (TASKBAR_OFFY*2);
303 
304 	if ( mnWinBits & WB_BORDER )
305 	{
306 		nTaskHeight -= TASKBAR_BORDER;
307 		aToolPos.Y() += TASKBAR_BORDER;
308 	}
309 
310 	if ( pTempButtonBar )
311 	{
312 		sal_uInt16	i = 0;
313 		sal_Bool	bVisibleItems = sal_False;
314 		while ( i < pTempButtonBar->GetItemCount() )
315 		{
316 			if ( pTempButtonBar->IsItemVisible( pTempButtonBar->GetItemId( i ) ) )
317 			{
318 				bVisibleItems = sal_True;
319 				break;
320 			}
321 			i++;
322 		}
323 		if ( mbStatusText || !bVisibleItems )
324 			pTempButtonBar->Hide();
325 		else
326 		{
327 			Size aButtonBarSize = pTempButtonBar->CalcWindowSizePixel();
328 			if ( pTempButtonBar->GetItemCount() )
329 				nTaskHeight = aButtonBarSize.Height();
330 			else
331 				aButtonBarSize.Height() = nTaskHeight;
332 			Point aTempPos = aToolPos;
333 			aTempPos.Y() += (aSize.Height()-aButtonBarSize.Height()-aTempPos.Y())/2;
334 			pTempButtonBar->SetPosSizePixel( aTempPos, aButtonBarSize );
335 			pTempButtonBar->Show();
336 			aToolPos.X() += aButtonBarSize.Width()+TASKBAR_BUTTONOFF;
337 		}
338 	}
339 
340 	if ( pTempStatusBar )
341 	{
342 		aStatusSize = pTempStatusBar->CalcWindowSizePixel();
343 		if ( mnStatusWidth )
344 			aStatusSize.Width() = mnStatusWidth;
345 		if ( !pTempTaskToolBox || mbStatusText )
346 			aStatusSize.Width() = aSize.Width();
347 		long nMaxHeight = aSize.Height()-(TASKBAR_OFFY*2);
348 		if ( mnWinBits & WB_BORDER )
349 			nMaxHeight -= TASKBAR_BORDER;
350 		if ( nMaxHeight+2 > aStatusSize.Height() )
351 			aStatusSize.Height() = nMaxHeight;
352 		Point aPos( aSize.Width()-aStatusSize.Width(), 0 );
353 		if ( pTempTaskToolBox && (mnWinBits & WB_SIZEABLE) && !mbStatusText )
354 		{
355 			long nMinToolWidth = aToolPos.X()+50;
356 			if ( aPos.X() < nMinToolWidth )
357 			{
358 				aStatusSize.Width() -= nMinToolWidth-aPos.X();
359 				aPos.X() = nMinToolWidth;
360 			}
361 		}
362 		if ( aPos.X() < 0 )
363 		{
364 			aStatusSize.Width() = aSize.Width();
365 			aPos.X() = 0;
366 		}
367 		if ( mnWinBits & WB_BORDER )
368 			aPos.Y() += TASKBAR_BORDER;
369 		aPos.Y() += (aSize.Height()-aStatusSize.Height()-aPos.Y())/2;
370 		if ( mnWinBits & WB_SIZEABLE )
371 		{
372 			if ( pTempTaskToolBox )
373 			{
374 				nOldStatusX = pTempStatusBar->GetPosPixel().X()-TASKBAR_OFFSIZE-2;
375 				nNewStatusX = aPos.X()-TASKBAR_OFFSIZE-2;
376 			}
377 		}
378 		pTempStatusBar->SetPosSizePixel( aPos, aStatusSize );
379 		pTempStatusBar->Show();
380 		aToolSize.Width() = aPos.X()-aToolPos.X()-TASKBAR_OFFX;
381 		if ( mnWinBits & WB_SIZEABLE )
382 			aToolSize.Width() -= (TASKBAR_OFFSIZE*2)-2;
383 	}
384 
385 	if ( pTempTaskToolBox )
386 	{
387 		if ( aToolSize.Width() <= 24 )
388 			pTempTaskToolBox->Hide();
389 		else
390 		{
391 			aToolSize.Height() = pTempTaskToolBox->CalcWindowSizePixel().Height();
392 			if ( pTempTaskToolBox->GetItemCount() )
393 				nTaskHeight = aToolSize.Height();
394 			else
395 				aToolSize.Height() = nTaskHeight;
396 			aToolPos.Y() += (aSize.Height()-aToolSize.Height()-aToolPos.Y())/2;
397 			pTempTaskToolBox->SetPosSizePixel( aToolPos, aToolSize );
398 			pTempTaskToolBox->Show();
399 		}
400 	}
401 
402 	if ( nOldStatusX != nNewStatusX )
403 	{
404 		if ( nOldStatusX > 0 )
405 		{
406 			Rectangle aRect( nOldStatusX, 0, nOldStatusX+2, aSize.Height()-1 );
407 			Invalidate( aRect );
408 		}
409 		if ( nNewStatusX > 0 )
410 		{
411 			Rectangle aRect( nNewStatusX, 0, nNewStatusX+2, aSize.Height()-1 );
412 			Invalidate( aRect );
413 		}
414 	}
415 }
416 
417 // -----------------------------------------------------------------------
418 
419 void TaskBar::StateChanged( StateChangedType nType )
420 {
421 	Window::StateChanged( nType );
422 
423 	if ( nType == STATE_CHANGE_INITSHOW )
424 		Format();
425 	else if ( nType == STATE_CHANGE_CONTROLBACKGROUND )
426 	{
427 		ImplInitSettings();
428 		Invalidate();
429 	}
430 	else if ( nType == STATE_CHANGE_FORMAT )
431 	{
432 		ImplInitSettings();
433 		ImplNewHeight( CalcWindowSizePixel().Height() );
434 		Format();
435 		Invalidate();
436 	}
437 }
438 
439 // -----------------------------------------------------------------------
440 
441 void TaskBar::DataChanged( const DataChangedEvent& rDCEvt )
442 {
443 	Window::DataChanged( rDCEvt );
444 
445 	if ( (rDCEvt.GetType() == DATACHANGED_FONTS) ||
446 		 (rDCEvt.GetType() == DATACHANGED_FONTSUBSTITUTION) ||
447 		 ((rDCEvt.GetType() == DATACHANGED_SETTINGS) &&
448 		  (rDCEvt.GetFlags() & SETTINGS_STYLE)) )
449 	{
450 		// Asyncronen StateChanged ausloesen, damit sich die
451 		// TaskBar an die neuen Groessen der Child-Fenster
452 		// orientieren kann
453 		PostStateChanged( STATE_CHANGE_FORMAT );
454 	}
455 }
456 
457 // -----------------------------------------------------------------------
458 
459 void TaskBar::Format()
460 {
461 	ImplNewHeight( CalcWindowSizePixel().Height() );
462 	Resize();
463 }
464 
465 // -----------------------------------------------------------------------
466 
467 void TaskBar::SetLines( sal_uInt16 nLines )
468 {
469 	mnLines = nLines;
470 }
471 
472 // -----------------------------------------------------------------------
473 
474 void TaskBar::EnableAutoHide( sal_Bool bAutoHide )
475 {
476 	mbAutoHide = bAutoHide;
477 
478 	if ( mbAutoHide )
479 	{
480 		ImplNewHeight( TASKBAR_AUTOHIDE_HEIGHT );
481 	}
482 	else
483 	{
484 		ImplNewHeight( CalcWindowSizePixel().Height() );
485 	}
486 }
487 
488 // -----------------------------------------------------------------------
489 
490 void TaskBar::ShowStatusText( const String& rText )
491 {
492 	if ( mpStatusBar )
493 	{
494 		if ( !mbStatusText )
495 		{
496 			mbStatusText = sal_True;
497 			if ( mpStatusBar->AreItemsVisible() )
498 			{
499 				mbShowItems = sal_True;
500 				mpStatusBar->HideItems();
501 			}
502 			else
503 				mbShowItems = sal_True;
504 			maOldText = mpStatusBar->GetText();
505 			Resize();
506 			mpStatusBar->SetText( rText );
507 			Update();
508 			mpStatusBar->Update();
509 		}
510 		else
511 			mpStatusBar->SetText( rText );
512 	}
513 }
514 
515 // -----------------------------------------------------------------------
516 
517 void TaskBar::HideStatusText()
518 {
519 	if ( mbStatusText && mpStatusBar )
520 	{
521 		mbStatusText = sal_False;
522 		mpStatusBar->SetText( maOldText );
523 		Resize();
524 		if ( mbShowItems )
525 			mpStatusBar->ShowItems();
526 	}
527 }
528 
529 // -----------------------------------------------------------------------
530 
531 Size TaskBar::CalcWindowSizePixel() const
532 {
533 	TaskButtonBar*	pTempButtonBar = GetButtonBar();
534 	TaskToolBox*	pTempTaskToolBox = GetTaskToolBox();
535 	TaskStatusBar*	pTempStatusBar = GetStatusBar();
536 	Size			aSize;
537 	long			nTempHeight;
538 
539 	if ( pTempButtonBar && pTempButtonBar->GetItemCount() )
540 		aSize.Height() = pTempButtonBar->CalcWindowSizePixel().Height()+(TASKBAR_OFFY*2);
541 	if ( pTempTaskToolBox && pTempTaskToolBox->GetItemCount() )
542 	{
543 		nTempHeight = pTempTaskToolBox->CalcWindowSizePixel().Height()+(TASKBAR_OFFY*2);
544 		if ( nTempHeight > aSize.Height() )
545 			 aSize.Height() = nTempHeight;
546 	}
547 	if ( pTempStatusBar )
548 	{
549 		nTempHeight = pTempStatusBar->GetSizePixel().Height();
550 		if ( nTempHeight > aSize.Height() )
551 			 aSize.Height() = nTempHeight;
552 	}
553 
554 	if ( mnWinBits & WB_BORDER )
555 		aSize.Height() += TASKBAR_BORDER;
556 
557 	return aSize;
558 }
559 
560 // -----------------------------------------------------------------------
561 
562 TaskButtonBar* TaskBar::GetButtonBar() const
563 {
564 	if ( !mpButtonBar )
565 		((TaskBar*)this)->mpButtonBar = ((TaskBar*)this)->CreateButtonBar();
566 	return mpButtonBar;
567 }
568 
569 // -----------------------------------------------------------------------
570 
571 TaskToolBox* TaskBar::GetTaskToolBox() const
572 {
573 	if ( !mpTaskToolBox )
574 		((TaskBar*)this)->mpTaskToolBox = ((TaskBar*)this)->CreateTaskToolBox();
575 	return mpTaskToolBox;
576 }
577 
578 // -----------------------------------------------------------------------
579 
580 TaskStatusBar* TaskBar::GetStatusBar() const
581 {
582 	if ( !mpStatusBar )
583 	{
584 		((TaskBar*)this)->mpStatusBar = ((TaskBar*)this)->CreateTaskStatusBar();
585 		if ( mpStatusBar )
586 			mpStatusBar->mpNotifyTaskBar = (TaskBar*)this;
587 	}
588 	return mpStatusBar;
589 }
590