merge the formfield patch from ooo-build
[ooovba.git] / svtools / source / control / taskbox.cxx
blob6d04fe1a4f70468809c1f768eb45529a5ab8bb35
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: taskbox.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
34 #define _TASKBAR_CXX
36 #ifndef _TOOLS_LIST_HXX
37 #include <tools/list.hxx>
38 #endif
39 #include <tools/debug.hxx>
40 #include <vcl/image.hxx>
41 #include <vcl/help.hxx>
43 #include <taskbar.hxx>
45 // =======================================================================
47 #define TASKBOX_TASKOFF 3
49 // =======================================================================
51 struct ImplTaskItem
53 Image maImage;
54 XubString maText;
57 DECLARE_LIST( ImplTaskItemList, ImplTaskItem* )
59 // =======================================================================
61 TaskToolBox::TaskToolBox( Window* pParent, WinBits nWinStyle ) :
62 ToolBox( pParent, nWinStyle | WB_SCROLL | WB_3DLOOK )
64 mpItemList = new ImplTaskItemList;
65 mnMaxTextWidth = 0;
66 mnActiveItemId = 0;
67 mnTaskItem = 0;
68 mnSmallItem = TOOLBOX_ITEM_NOTFOUND;
69 mbMinActivate = FALSE;
71 SetAlign( WINDOWALIGN_BOTTOM );
72 SetButtonType( BUTTON_SYMBOLTEXT );
75 // -----------------------------------------------------------------------
77 TaskToolBox::~TaskToolBox()
79 ImplTaskItem* pItem = mpItemList->First();
80 while ( pItem )
82 delete pItem;
83 pItem = mpItemList->Next();
86 delete mpItemList;
89 // -----------------------------------------------------------------------
91 void TaskToolBox::ActivateTaskItem( USHORT nItemId, BOOL bMinActivate )
93 if ( nItemId )
95 if ( nItemId != mnActiveItemId )
97 if ( mnActiveItemId )
98 CheckItem( mnActiveItemId, FALSE );
99 CheckItem( nItemId );
100 mnActiveItemId = nItemId;
102 else
104 if ( !bMinActivate )
105 return;
107 mbMinActivate = TRUE;
110 mnTaskItem = nItemId-1;
111 ActivateTask();
112 mnTaskItem = 0;
113 mbMinActivate = FALSE;
117 // -----------------------------------------------------------------------
119 void TaskToolBox::ActivateTask()
121 maActivateTaskHdl.Call( this );
124 // -----------------------------------------------------------------------
126 void TaskToolBox::ContextMenu()
128 maContextMenuHdl.Call( this );
131 // -----------------------------------------------------------------------
133 void TaskToolBox::MouseButtonDown( const MouseEvent& rMEvt )
135 if ( !rMEvt.IsRight() )
136 ToolBox::MouseButtonDown( rMEvt );
139 // -----------------------------------------------------------------------
141 void TaskToolBox::Resize()
143 mnOldItemCount = mpItemList->Count();
144 mnUpdatePos = (USHORT)mnOldItemCount;
145 mnUpdateNewPos = TOOLBOX_ITEM_NOTFOUND;
146 ImplFormatTaskToolBox();
147 ToolBox::Resize();
150 // -----------------------------------------------------------------------
152 void TaskToolBox::Command( const CommandEvent& rCEvt )
154 if ( rCEvt.GetCommand() == COMMAND_CONTEXTMENU )
156 USHORT nItemId = GetItemId( rCEvt.GetMousePosPixel() );
157 // Dies machen wir nicht mehr, da es von zu vielen als stoerend empfunden wurde
158 // ActivateTaskItem( nItemId );
159 mnTaskItem = nItemId-1;
161 maContextMenuPos = rCEvt.GetMousePosPixel();
162 ContextMenu();
163 maContextMenuPos = Point();
164 mnTaskItem = 0;
166 else
167 ToolBox::Command( rCEvt );
170 // -----------------------------------------------------------------------
172 void TaskToolBox::RequestHelp( const HelpEvent& rHEvt )
174 if ( rHEvt.GetMode() & (HELPMODE_BALLOON | HELPMODE_QUICK) )
176 USHORT nItemId = GetItemId( ScreenToOutputPixel( rHEvt.GetMousePosPixel() ) );
178 if ( nItemId )
180 ImplTaskItem* pItem = mpItemList->GetObject( nItemId-1 );
181 if ( pItem )
183 if ( pItem->maText != GetItemText( nItemId ) )
185 Rectangle aItemRect = GetItemRect( nItemId );
186 if ( rHEvt.GetMode() & HELPMODE_QUICK )
187 Help::ShowQuickHelp( this, aItemRect, pItem->maText );
188 else
189 Help::ShowBalloon( this, aItemRect.Center(), aItemRect, pItem->maText );
191 else
192 Help::ShowQuickHelp( this, Rectangle(), String() );
193 return;
198 ToolBox::RequestHelp( rHEvt );
201 // -----------------------------------------------------------------------
203 void TaskToolBox::Select()
205 USHORT nItemId = GetCurItemId();
206 ActivateTaskItem( nItemId, TRUE );
209 // -----------------------------------------------------------------------
211 void TaskToolBox::ImplFormatTaskToolBox()
213 if ( mnUpdateNewPos == TOOLBOX_ITEM_NOTFOUND )
215 // Eintraege aus der Liste entfernen
216 while ( mpItemList->Count() > mnUpdatePos )
217 delete mpItemList->Remove( (ULONG)mnUpdatePos );
218 mnUpdateNewPos = mnUpdatePos;
221 // Maximale Itemgroesse berechnen
222 long nOldMaxTextWidth = mnMaxTextWidth;
223 mnMaxTextWidth = 70;
224 if ( mpItemList->Count() )
226 long nWinSize = GetOutputSizePixel().Width()-8;
227 long nItemSize = mpItemList->GetObject(0)->maImage.GetSizePixel().Width()+7+TASKBOX_TASKOFF+2;
228 nWinSize -= mpItemList->Count()*nItemSize;
229 if ( nWinSize > 0 )
230 nWinSize /= mpItemList->Count();
231 else
232 nWinSize = 0;
233 if ( nWinSize < mnMaxTextWidth )
234 mnMaxTextWidth = nWinSize;
235 if ( (mnMaxTextWidth < nOldMaxTextWidth) ||
236 ((mnMaxTextWidth-nOldMaxTextWidth > 3) &&
237 (mnSmallItem != TOOLBOX_ITEM_NOTFOUND)) )
239 mnSmallItem = TOOLBOX_ITEM_NOTFOUND;
240 mnUpdateNewPos = 0;
244 // Eintraege aus der ToolBox entfernen, die ersetzt werden
245 USHORT nBtnPos = (mnUpdateNewPos*2);
246 while ( nBtnPos < GetItemCount() )
247 RemoveItem( nBtnPos );
248 if ( mnUpdateNewPos <= (mnActiveItemId-1) )
249 mnActiveItemId = 0;
251 // Neue Eintrage einfuegen
252 USHORT i = mnUpdateNewPos;
253 while ( i < mpItemList->Count() )
255 ImplTaskItem* pItem = mpItemList->GetObject( i );
257 // Textlaenge berechnen
258 XubString aText = pItem->maText;
259 if ( !aText.Len() )
260 aText = ' ';
261 long nTxtWidth = GetTextWidth( aText );
262 if ( nTxtWidth > mnMaxTextWidth )
264 if ( mnSmallItem == TOOLBOX_ITEM_NOTFOUND )
265 mnSmallItem = i;
266 // 3 == Len of "..."
267 aText.AppendAscii( "..." );
270 aText.Erase( aText.Len()-3-1, 1 );
271 nTxtWidth = GetTextWidth( aText );
273 while ( (nTxtWidth > mnMaxTextWidth) && (aText.Len() > 3) );
276 USHORT nItemId = i+1;
277 if ( aText.EqualsAscii( "..." ) )
278 InsertItem( nItemId, pItem->maImage, TIB_LEFT );
279 else
280 InsertItem( nItemId, pItem->maImage, aText, TIB_LEFT );
281 InsertSeparator( TOOLBOX_APPEND, TASKBOX_TASKOFF );
282 i++;
285 if ( mnUpdateNewPos != 0 )
286 mnMaxTextWidth = nOldMaxTextWidth;
288 if ( mnNewActivePos+1 != mnActiveItemId )
290 if ( mnActiveItemId )
291 CheckItem( mnActiveItemId, FALSE );
292 mnActiveItemId = mnNewActivePos+1;
293 CheckItem( mnActiveItemId );
297 // -----------------------------------------------------------------------
299 void TaskToolBox::StartUpdateTask()
301 mnOldItemCount = mpItemList->Count();
302 mnUpdatePos = 0;
303 mnUpdateNewPos = TOOLBOX_ITEM_NOTFOUND;
304 mnNewActivePos = 0xFFFE;
307 // -----------------------------------------------------------------------
309 void TaskToolBox::UpdateTask( const Image& rImage, const String& rText,
310 BOOL bActive )
312 ImplTaskItem* pItem = mpItemList->GetObject( mnUpdatePos );
313 if ( pItem )
315 if ( (pItem->maText != rText) || (pItem->maImage != rImage) )
317 // Eintraege aus der Liste entfernen
318 while ( mpItemList->Count() > mnUpdatePos )
319 delete mpItemList->Remove( (ULONG)mnUpdatePos );
320 pItem = NULL;
324 if ( !pItem )
326 if ( mnUpdatePos < mnUpdateNewPos )
327 mnUpdateNewPos = mnUpdatePos;
329 pItem = new ImplTaskItem;
330 pItem->maImage = rImage;
331 pItem->maText = rText;
332 mpItemList->Insert( pItem, LIST_APPEND );
335 if ( bActive )
336 mnNewActivePos = mnUpdatePos;
338 mnUpdatePos++;
341 // -----------------------------------------------------------------------
343 void TaskToolBox::EndUpdateTask()
345 if ( mnUpdateNewPos == TOOLBOX_ITEM_NOTFOUND )
347 // Eintraege aus der Liste entfernen
348 while ( mpItemList->Count() > mnUpdatePos )
349 delete mpItemList->Remove( (ULONG)mnUpdatePos );
350 mnUpdateNewPos = mnUpdatePos;
353 ImplFormatTaskToolBox();