xref: /AOO41X/main/svtools/source/control/taskbox.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_svtools.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #define _TASKBAR_CXX
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <tools/list.hxx>
34*cdf0e10cSrcweir #include <tools/debug.hxx>
35*cdf0e10cSrcweir #include <vcl/image.hxx>
36*cdf0e10cSrcweir #include <vcl/help.hxx>
37*cdf0e10cSrcweir #include <svtools/taskbar.hxx>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir // =======================================================================
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #define TASKBOX_TASKOFF             3
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir // =======================================================================
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir struct ImplTaskItem
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir     Image               maImage;
48*cdf0e10cSrcweir     XubString           maText;
49*cdf0e10cSrcweir };
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir DECLARE_LIST( ImplTaskItemList, ImplTaskItem* )
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir // =======================================================================
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir TaskToolBox::TaskToolBox( Window* pParent, WinBits nWinStyle ) :
56*cdf0e10cSrcweir     ToolBox( pParent, nWinStyle | WB_SCROLL | WB_3DLOOK )
57*cdf0e10cSrcweir {
58*cdf0e10cSrcweir     mpItemList      = new ImplTaskItemList;
59*cdf0e10cSrcweir     mnMaxTextWidth  = 0;
60*cdf0e10cSrcweir     mnActiveItemId  = 0;
61*cdf0e10cSrcweir     mnTaskItem      = 0;
62*cdf0e10cSrcweir     mnSmallItem     = TOOLBOX_ITEM_NOTFOUND;
63*cdf0e10cSrcweir     mbMinActivate   = sal_False;
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir     SetAlign( WINDOWALIGN_BOTTOM );
66*cdf0e10cSrcweir     SetButtonType( BUTTON_SYMBOLTEXT );
67*cdf0e10cSrcweir }
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir // -----------------------------------------------------------------------
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir TaskToolBox::~TaskToolBox()
72*cdf0e10cSrcweir {
73*cdf0e10cSrcweir     ImplTaskItem* pItem = mpItemList->First();
74*cdf0e10cSrcweir     while ( pItem )
75*cdf0e10cSrcweir     {
76*cdf0e10cSrcweir         delete pItem;
77*cdf0e10cSrcweir         pItem = mpItemList->Next();
78*cdf0e10cSrcweir     }
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir     delete mpItemList;
81*cdf0e10cSrcweir }
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir // -----------------------------------------------------------------------
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir void TaskToolBox::ActivateTaskItem( sal_uInt16 nItemId, sal_Bool bMinActivate )
86*cdf0e10cSrcweir {
87*cdf0e10cSrcweir     if ( nItemId )
88*cdf0e10cSrcweir     {
89*cdf0e10cSrcweir         if ( nItemId != mnActiveItemId )
90*cdf0e10cSrcweir         {
91*cdf0e10cSrcweir             if ( mnActiveItemId )
92*cdf0e10cSrcweir                 CheckItem( mnActiveItemId, sal_False );
93*cdf0e10cSrcweir             CheckItem( nItemId );
94*cdf0e10cSrcweir             mnActiveItemId = nItemId;
95*cdf0e10cSrcweir         }
96*cdf0e10cSrcweir         else
97*cdf0e10cSrcweir         {
98*cdf0e10cSrcweir             if ( !bMinActivate )
99*cdf0e10cSrcweir                 return;
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir             mbMinActivate = sal_True;
102*cdf0e10cSrcweir         }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir         mnTaskItem = nItemId-1;
105*cdf0e10cSrcweir         ActivateTask();
106*cdf0e10cSrcweir         mnTaskItem = 0;
107*cdf0e10cSrcweir         mbMinActivate = sal_False;
108*cdf0e10cSrcweir     }
109*cdf0e10cSrcweir }
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir // -----------------------------------------------------------------------
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir void TaskToolBox::ActivateTask()
114*cdf0e10cSrcweir {
115*cdf0e10cSrcweir     maActivateTaskHdl.Call( this );
116*cdf0e10cSrcweir }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir // -----------------------------------------------------------------------
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir void TaskToolBox::ContextMenu()
121*cdf0e10cSrcweir {
122*cdf0e10cSrcweir     maContextMenuHdl.Call( this );
123*cdf0e10cSrcweir }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir // -----------------------------------------------------------------------
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir void TaskToolBox::MouseButtonDown( const MouseEvent& rMEvt )
128*cdf0e10cSrcweir {
129*cdf0e10cSrcweir     if ( !rMEvt.IsRight() )
130*cdf0e10cSrcweir         ToolBox::MouseButtonDown( rMEvt );
131*cdf0e10cSrcweir }
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir // -----------------------------------------------------------------------
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir void TaskToolBox::Resize()
136*cdf0e10cSrcweir {
137*cdf0e10cSrcweir     mnOldItemCount  = mpItemList->Count();
138*cdf0e10cSrcweir     mnUpdatePos     = (sal_uInt16)mnOldItemCount;
139*cdf0e10cSrcweir     mnUpdateNewPos  = TOOLBOX_ITEM_NOTFOUND;
140*cdf0e10cSrcweir     ImplFormatTaskToolBox();
141*cdf0e10cSrcweir     ToolBox::Resize();
142*cdf0e10cSrcweir }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir // -----------------------------------------------------------------------
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir void TaskToolBox::Command( const CommandEvent& rCEvt )
147*cdf0e10cSrcweir {
148*cdf0e10cSrcweir     if ( rCEvt.GetCommand() == COMMAND_CONTEXTMENU )
149*cdf0e10cSrcweir     {
150*cdf0e10cSrcweir         sal_uInt16 nItemId = GetItemId( rCEvt.GetMousePosPixel() );
151*cdf0e10cSrcweir // Dies machen wir nicht mehr, da es von zu vielen als stoerend empfunden wurde
152*cdf0e10cSrcweir //        ActivateTaskItem( nItemId );
153*cdf0e10cSrcweir         mnTaskItem = nItemId-1;
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir         maContextMenuPos = rCEvt.GetMousePosPixel();
156*cdf0e10cSrcweir         ContextMenu();
157*cdf0e10cSrcweir         maContextMenuPos = Point();
158*cdf0e10cSrcweir         mnTaskItem = 0;
159*cdf0e10cSrcweir     }
160*cdf0e10cSrcweir     else
161*cdf0e10cSrcweir         ToolBox::Command( rCEvt );
162*cdf0e10cSrcweir }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir // -----------------------------------------------------------------------
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir void TaskToolBox::RequestHelp( const HelpEvent& rHEvt )
167*cdf0e10cSrcweir {
168*cdf0e10cSrcweir     if ( rHEvt.GetMode() & (HELPMODE_BALLOON | HELPMODE_QUICK) )
169*cdf0e10cSrcweir     {
170*cdf0e10cSrcweir         sal_uInt16 nItemId = GetItemId( ScreenToOutputPixel( rHEvt.GetMousePosPixel() ) );
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir         if ( nItemId )
173*cdf0e10cSrcweir         {
174*cdf0e10cSrcweir             ImplTaskItem* pItem = mpItemList->GetObject( nItemId-1 );
175*cdf0e10cSrcweir             if ( pItem )
176*cdf0e10cSrcweir             {
177*cdf0e10cSrcweir                 if ( pItem->maText != GetItemText( nItemId ) )
178*cdf0e10cSrcweir                 {
179*cdf0e10cSrcweir                     Rectangle aItemRect = GetItemRect( nItemId );
180*cdf0e10cSrcweir                     if ( rHEvt.GetMode() & HELPMODE_QUICK )
181*cdf0e10cSrcweir                         Help::ShowQuickHelp( this, aItemRect, pItem->maText );
182*cdf0e10cSrcweir                     else
183*cdf0e10cSrcweir                         Help::ShowBalloon( this, aItemRect.Center(), aItemRect, pItem->maText );
184*cdf0e10cSrcweir                 }
185*cdf0e10cSrcweir                 else
186*cdf0e10cSrcweir                     Help::ShowQuickHelp( this, Rectangle(), String() );
187*cdf0e10cSrcweir                 return;
188*cdf0e10cSrcweir             }
189*cdf0e10cSrcweir         }
190*cdf0e10cSrcweir     }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir     ToolBox::RequestHelp( rHEvt );
193*cdf0e10cSrcweir }
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir // -----------------------------------------------------------------------
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir void TaskToolBox::Select()
198*cdf0e10cSrcweir {
199*cdf0e10cSrcweir     sal_uInt16 nItemId = GetCurItemId();
200*cdf0e10cSrcweir     ActivateTaskItem( nItemId, sal_True );
201*cdf0e10cSrcweir }
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir // -----------------------------------------------------------------------
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir void TaskToolBox::ImplFormatTaskToolBox()
206*cdf0e10cSrcweir {
207*cdf0e10cSrcweir     if ( mnUpdateNewPos == TOOLBOX_ITEM_NOTFOUND )
208*cdf0e10cSrcweir     {
209*cdf0e10cSrcweir         // Eintraege aus der Liste entfernen
210*cdf0e10cSrcweir         while ( mpItemList->Count() > mnUpdatePos )
211*cdf0e10cSrcweir             delete mpItemList->Remove( (sal_uLong)mnUpdatePos );
212*cdf0e10cSrcweir         mnUpdateNewPos = mnUpdatePos;
213*cdf0e10cSrcweir     }
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir     // Maximale Itemgroesse berechnen
216*cdf0e10cSrcweir     long nOldMaxTextWidth = mnMaxTextWidth;
217*cdf0e10cSrcweir     mnMaxTextWidth = 70;
218*cdf0e10cSrcweir     if ( mpItemList->Count() )
219*cdf0e10cSrcweir     {
220*cdf0e10cSrcweir         long nWinSize = GetOutputSizePixel().Width()-8;
221*cdf0e10cSrcweir         long nItemSize = mpItemList->GetObject(0)->maImage.GetSizePixel().Width()+7+TASKBOX_TASKOFF+2;
222*cdf0e10cSrcweir         nWinSize -= mpItemList->Count()*nItemSize;
223*cdf0e10cSrcweir         if ( nWinSize > 0 )
224*cdf0e10cSrcweir             nWinSize /= mpItemList->Count();
225*cdf0e10cSrcweir         else
226*cdf0e10cSrcweir             nWinSize = 0;
227*cdf0e10cSrcweir         if ( nWinSize < mnMaxTextWidth )
228*cdf0e10cSrcweir             mnMaxTextWidth = nWinSize;
229*cdf0e10cSrcweir         if ( (mnMaxTextWidth < nOldMaxTextWidth) ||
230*cdf0e10cSrcweir              ((mnMaxTextWidth-nOldMaxTextWidth > 3) &&
231*cdf0e10cSrcweir               (mnSmallItem != TOOLBOX_ITEM_NOTFOUND)) )
232*cdf0e10cSrcweir         {
233*cdf0e10cSrcweir             mnSmallItem = TOOLBOX_ITEM_NOTFOUND;
234*cdf0e10cSrcweir             mnUpdateNewPos = 0;
235*cdf0e10cSrcweir         }
236*cdf0e10cSrcweir     }
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir     // Eintraege aus der ToolBox entfernen, die ersetzt werden
239*cdf0e10cSrcweir     sal_uInt16 nBtnPos = (mnUpdateNewPos*2);
240*cdf0e10cSrcweir     while ( nBtnPos < GetItemCount() )
241*cdf0e10cSrcweir         RemoveItem( nBtnPos );
242*cdf0e10cSrcweir     if ( mnUpdateNewPos <= (mnActiveItemId-1) )
243*cdf0e10cSrcweir         mnActiveItemId = 0;
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir     // Neue Eintrage einfuegen
246*cdf0e10cSrcweir     sal_uInt16 i = mnUpdateNewPos;
247*cdf0e10cSrcweir     while ( i < mpItemList->Count() )
248*cdf0e10cSrcweir     {
249*cdf0e10cSrcweir         ImplTaskItem* pItem = mpItemList->GetObject( i );
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir         // Textlaenge berechnen
252*cdf0e10cSrcweir         XubString aText = pItem->maText;
253*cdf0e10cSrcweir         if ( !aText.Len() )
254*cdf0e10cSrcweir             aText = ' ';
255*cdf0e10cSrcweir         long nTxtWidth = GetTextWidth( aText );
256*cdf0e10cSrcweir         if ( nTxtWidth > mnMaxTextWidth )
257*cdf0e10cSrcweir         {
258*cdf0e10cSrcweir             if ( mnSmallItem == TOOLBOX_ITEM_NOTFOUND )
259*cdf0e10cSrcweir                 mnSmallItem = i;
260*cdf0e10cSrcweir             // 3 == Len of "..."
261*cdf0e10cSrcweir             aText.AppendAscii( "..." );
262*cdf0e10cSrcweir             do
263*cdf0e10cSrcweir             {
264*cdf0e10cSrcweir                 aText.Erase( aText.Len()-3-1, 1 );
265*cdf0e10cSrcweir                 nTxtWidth = GetTextWidth( aText );
266*cdf0e10cSrcweir             }
267*cdf0e10cSrcweir             while ( (nTxtWidth > mnMaxTextWidth) && (aText.Len() > 3) );
268*cdf0e10cSrcweir         }
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir         sal_uInt16 nItemId = i+1;
271*cdf0e10cSrcweir         if ( aText.EqualsAscii( "..." ) )
272*cdf0e10cSrcweir             InsertItem( nItemId, pItem->maImage, TIB_LEFT );
273*cdf0e10cSrcweir         else
274*cdf0e10cSrcweir             InsertItem( nItemId, pItem->maImage, aText, TIB_LEFT );
275*cdf0e10cSrcweir         InsertSeparator( TOOLBOX_APPEND, TASKBOX_TASKOFF );
276*cdf0e10cSrcweir         i++;
277*cdf0e10cSrcweir     }
278*cdf0e10cSrcweir 
279*cdf0e10cSrcweir     if ( mnUpdateNewPos != 0 )
280*cdf0e10cSrcweir         mnMaxTextWidth = nOldMaxTextWidth;
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir     if ( mnNewActivePos+1 != mnActiveItemId )
283*cdf0e10cSrcweir     {
284*cdf0e10cSrcweir         if ( mnActiveItemId )
285*cdf0e10cSrcweir             CheckItem( mnActiveItemId, sal_False );
286*cdf0e10cSrcweir         mnActiveItemId = mnNewActivePos+1;
287*cdf0e10cSrcweir         CheckItem( mnActiveItemId );
288*cdf0e10cSrcweir     }
289*cdf0e10cSrcweir }
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir // -----------------------------------------------------------------------
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir void TaskToolBox::StartUpdateTask()
294*cdf0e10cSrcweir {
295*cdf0e10cSrcweir     mnOldItemCount  = mpItemList->Count();
296*cdf0e10cSrcweir     mnUpdatePos     = 0;
297*cdf0e10cSrcweir     mnUpdateNewPos  = TOOLBOX_ITEM_NOTFOUND;
298*cdf0e10cSrcweir     mnNewActivePos  = 0xFFFE;
299*cdf0e10cSrcweir }
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir // -----------------------------------------------------------------------
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir void TaskToolBox::UpdateTask( const Image& rImage, const String& rText,
304*cdf0e10cSrcweir                               sal_Bool bActive )
305*cdf0e10cSrcweir {
306*cdf0e10cSrcweir     ImplTaskItem* pItem = mpItemList->GetObject( mnUpdatePos );
307*cdf0e10cSrcweir     if ( pItem )
308*cdf0e10cSrcweir     {
309*cdf0e10cSrcweir         if ( (pItem->maText != rText) || (pItem->maImage != rImage) )
310*cdf0e10cSrcweir         {
311*cdf0e10cSrcweir             // Eintraege aus der Liste entfernen
312*cdf0e10cSrcweir             while ( mpItemList->Count() > mnUpdatePos )
313*cdf0e10cSrcweir                 delete mpItemList->Remove( (sal_uLong)mnUpdatePos );
314*cdf0e10cSrcweir             pItem = NULL;
315*cdf0e10cSrcweir         }
316*cdf0e10cSrcweir     }
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir     if ( !pItem )
319*cdf0e10cSrcweir     {
320*cdf0e10cSrcweir         if ( mnUpdatePos < mnUpdateNewPos )
321*cdf0e10cSrcweir             mnUpdateNewPos = mnUpdatePos;
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir         pItem           = new ImplTaskItem;
324*cdf0e10cSrcweir         pItem->maImage  = rImage;
325*cdf0e10cSrcweir         pItem->maText   = rText;
326*cdf0e10cSrcweir         mpItemList->Insert( pItem, LIST_APPEND );
327*cdf0e10cSrcweir     }
328*cdf0e10cSrcweir 
329*cdf0e10cSrcweir     if ( bActive )
330*cdf0e10cSrcweir         mnNewActivePos = mnUpdatePos;
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir     mnUpdatePos++;
333*cdf0e10cSrcweir }
334*cdf0e10cSrcweir 
335*cdf0e10cSrcweir // -----------------------------------------------------------------------
336*cdf0e10cSrcweir 
337*cdf0e10cSrcweir void TaskToolBox::EndUpdateTask()
338*cdf0e10cSrcweir {
339*cdf0e10cSrcweir     if ( mnUpdateNewPos == TOOLBOX_ITEM_NOTFOUND )
340*cdf0e10cSrcweir     {
341*cdf0e10cSrcweir         // Eintraege aus der Liste entfernen
342*cdf0e10cSrcweir         while ( mpItemList->Count() > mnUpdatePos )
343*cdf0e10cSrcweir             delete mpItemList->Remove( (sal_uLong)mnUpdatePos );
344*cdf0e10cSrcweir         mnUpdateNewPos = mnUpdatePos;
345*cdf0e10cSrcweir     }
346*cdf0e10cSrcweir 
347*cdf0e10cSrcweir     ImplFormatTaskToolBox();
348*cdf0e10cSrcweir }
349*cdf0e10cSrcweir 
350