update dev300-m58
[ooovba.git] / svtools / source / control / stdmenu.cxx
blob7da5f298eba81bee3d81bd01caea3371a159d588
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: stdmenu.cxx,v $
10 * $Revision: 1.11 $
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 #include <string.h>
36 #ifndef _APP_HXX
37 #include <vcl/svapp.hxx>
38 #endif
40 #include <vcl/i18nhelp.hxx>
42 #include <ctrltool.hxx>
43 #include <stdmenu.hxx>
45 // ========================================================================
47 FontNameMenu::FontNameMenu()
49 SetMenuFlags( GetMenuFlags() | MENU_FLAG_NOAUTOMNEMONICS );
52 // -----------------------------------------------------------------------
54 FontNameMenu::~FontNameMenu()
58 // -----------------------------------------------------------------------
60 void FontNameMenu::Select()
62 maCurName = GetItemText( GetCurItemId() );
63 maSelectHdl.Call( this );
66 // -----------------------------------------------------------------------
68 void FontNameMenu::Highlight()
70 XubString aTempName = maCurName;
71 maCurName = GetItemText( GetCurItemId() );
72 maHighlightHdl.Call( this );
73 maCurName = aTempName;
76 // -----------------------------------------------------------------------
78 void FontNameMenu::Fill( const FontList* pList )
80 // clear menu
81 Clear();
83 // add fonts
84 const vcl::I18nHelper& rI18nHelper = Application::GetSettings().GetUILocaleI18nHelper();
85 // more than 100 fonts reduces the speed of opening the menu.
86 // So only the first 100 fonts will be displayed.
87 USHORT nFontCount = ::std::min( pList->GetFontNameCount(), static_cast< USHORT >(100) );
88 for ( USHORT i = 0; i < nFontCount; i++ )
90 const XubString& rName = pList->GetFontName( i ).GetName();
92 // sort with the I18nHelper
93 USHORT j = GetItemCount();
94 while ( j )
96 XubString aText = GetItemText( GetItemId( j-1 ) );
97 if ( rI18nHelper.CompareString( rName, aText ) > 0 )
98 break;
99 j--;
101 InsertItem( i+1, rName, MIB_RADIOCHECK | MIB_AUTOCHECK, j );
104 SetCurName( maCurName );
107 // -----------------------------------------------------------------------
109 void FontNameMenu::SetCurName( const XubString& rName )
111 maCurName = rName;
113 // Menueintrag checken
114 USHORT nChecked = 0;
115 USHORT nItemCount = GetItemCount();
116 for( USHORT i = 0; i < nItemCount; i++ )
118 USHORT nItemId = GetItemId( i );
120 if ( IsItemChecked( nItemId ) )
121 nChecked = nItemId;
123 XubString aText = GetItemText( nItemId );
124 if ( aText == maCurName )
126 CheckItem( nItemId, TRUE );
127 return;
131 if ( nChecked )
132 CheckItem( nChecked, FALSE );
135 // ========================================================================
137 FontStyleMenu::FontStyleMenu()
139 SetMenuFlags( GetMenuFlags() | MENU_FLAG_NOAUTOMNEMONICS );
142 // -----------------------------------------------------------------------
144 FontStyleMenu::~FontStyleMenu()
148 // -----------------------------------------------------------------------
150 void FontStyleMenu::Select()
152 USHORT nCurId = GetCurItemId();
154 if ( (nCurId >= FONTSTYLEMENU_FIRSTID) && (nCurId <= FONTSTYLEMENU_LASTID) )
156 maCurStyle = GetItemText( nCurId );
157 maSelectHdl.Call( this );
159 else
160 PopupMenu::Select();
163 // -----------------------------------------------------------------------
165 void FontStyleMenu::Highlight()
167 USHORT nCurId = GetCurItemId();
169 if ( (nCurId >= FONTSTYLEMENU_FIRSTID) && (nCurId <= FONTSTYLEMENU_LASTID) )
171 XubString aTempName = maCurStyle;
172 maCurStyle = GetItemText( nCurId );
173 maHighlightHdl.Call( this );
174 maCurStyle = aTempName;
176 else
177 PopupMenu::Highlight();
180 // -----------------------------------------------------------------------
182 BOOL FontStyleMenu::ImplIsAlreadyInserted( const XubString& rStyleName, USHORT nCount )
184 for ( USHORT i = 0; i < nCount; i++ )
186 if ( GetItemText( i+FONTSTYLEMENU_FIRSTID ) == rStyleName )
187 return TRUE;
190 return FALSE;
193 // -----------------------------------------------------------------------
195 void FontStyleMenu::Fill( const XubString& rName, const FontList* pList )
197 USHORT nItemId = GetItemId( 0 );
198 while ( (nItemId >= FONTSTYLEMENU_FIRSTID) &&
199 (nItemId <= FONTSTYLEMENU_LASTID) )
201 RemoveItem( 0 );
202 nItemId = GetItemId( 0 );
205 // Existiert ein Font mit diesem Namen
206 sal_Handle hFontInfo = pList->GetFirstFontInfo( rName );
207 if ( hFontInfo )
209 XubString aStyleText;
210 USHORT nPos = 0;
211 USHORT nId = FONTSTYLEMENU_FIRSTID;
212 FontWeight eLastWeight = WEIGHT_DONTKNOW;
213 FontItalic eLastItalic = ITALIC_NONE;
214 FontWidth eLastWidth = WIDTH_DONTKNOW;
215 BOOL bNormal = FALSE;
216 BOOL bItalic = FALSE;
217 BOOL bBold = FALSE;
218 BOOL bBoldItalic = FALSE;
219 BOOL bInsert = FALSE;
220 FontInfo aInfo;
221 while ( hFontInfo )
223 aInfo = pList->GetFontInfo( hFontInfo );
225 FontWeight eWeight = aInfo.GetWeight();
226 FontItalic eItalic = aInfo.GetItalic();
227 FontWidth eWidth = aInfo.GetWidthType();
228 // Only if the attributes are different, we insert the
229 // Font to avoid double Entries in different languages
230 if ( (eWeight != eLastWeight) || (eItalic != eLastItalic) ||
231 (eWidth != eLastWidth) )
233 if ( bInsert )
235 InsertItem( nId, aStyleText,
236 MIB_RADIOCHECK | MIB_AUTOCHECK, nPos );
237 nPos++;
238 nId++;
241 if ( eWeight <= WEIGHT_NORMAL )
243 if ( eItalic != ITALIC_NONE )
244 bItalic = TRUE;
245 else
246 bNormal = TRUE;
248 else
250 if ( eItalic != ITALIC_NONE )
251 bBoldItalic = TRUE;
252 else
253 bBold = TRUE;
256 // For wrong StyleNames we replace this with the correct once
257 aStyleText = pList->GetStyleName( aInfo );
258 bInsert = !ImplIsAlreadyInserted( aStyleText, nPos );
259 if ( !bInsert )
261 aStyleText = pList->GetStyleName( eWeight, eItalic );
262 bInsert = !ImplIsAlreadyInserted( aStyleText, nPos );
265 eLastWeight = eWeight;
266 eLastItalic = eItalic;
267 eLastWidth = eWidth;
269 else
271 if ( bInsert )
273 // If we have two names for the same attributes
274 // we prefer the translated standard names
275 const XubString& rAttrStyleText = pList->GetStyleName( eWeight, eItalic );
276 if ( rAttrStyleText != aStyleText )
278 XubString aTempStyleText = pList->GetStyleName( aInfo );
279 if ( rAttrStyleText == aTempStyleText )
280 aStyleText = rAttrStyleText;
281 bInsert = !ImplIsAlreadyInserted( aStyleText, nPos );
286 if ( !bItalic && (aStyleText == pList->GetItalicStr()) )
287 bItalic = TRUE;
288 else if ( !bBold && (aStyleText == pList->GetBoldStr()) )
289 bBold = TRUE;
290 else if ( !bBoldItalic && (aStyleText == pList->GetBoldItalicStr()) )
291 bBoldItalic = TRUE;
293 hFontInfo = pList->GetNextFontInfo( hFontInfo );
296 if ( bInsert )
298 InsertItem( nId, aStyleText,
299 MIB_RADIOCHECK | MIB_AUTOCHECK, nPos );
300 nPos++;
301 nId++;
304 // Bestimmte Styles als Nachbildung
305 if ( bNormal )
307 if ( !bItalic )
309 InsertItem( nId, pList->GetItalicStr(),
310 MIB_RADIOCHECK | MIB_AUTOCHECK, nPos );
311 nPos++;
312 nId++;
314 if ( !bBold )
316 InsertItem( nId, pList->GetBoldStr(),
317 MIB_RADIOCHECK | MIB_AUTOCHECK, nPos );
318 nPos++;
319 nId++;
322 if ( !bBoldItalic )
324 if ( bNormal || bItalic || bBold )
326 InsertItem( nId, pList->GetBoldItalicStr(),
327 MIB_RADIOCHECK | MIB_AUTOCHECK, nPos );
328 nPos++;
329 nId++;
333 else
335 // Wenn Font nicht, dann Standard-Styles einfuegen
336 InsertItem( FONTSTYLEMENU_FIRSTID, pList->GetNormalStr(),
337 MIB_RADIOCHECK | MIB_AUTOCHECK, 0 );
338 InsertItem( FONTSTYLEMENU_FIRSTID+1, pList->GetItalicStr(),
339 MIB_RADIOCHECK | MIB_AUTOCHECK, 0 );
340 InsertItem( FONTSTYLEMENU_FIRSTID+2, pList->GetBoldStr(),
341 MIB_RADIOCHECK | MIB_AUTOCHECK, 0 );
342 InsertItem( FONTSTYLEMENU_FIRSTID+3, pList->GetBoldItalicStr(),
343 MIB_RADIOCHECK | MIB_AUTOCHECK, 0 );
346 SetCurStyle( maCurStyle );
349 // -----------------------------------------------------------------------
351 void FontStyleMenu::SetCurStyle( const XubString& rStyle )
353 maCurStyle = rStyle;
355 // Menueintrag checken
356 USHORT nChecked = 0;
357 USHORT nItemCount = GetItemCount();
358 for( USHORT i = 0; i < nItemCount; i++ )
360 USHORT nItemId = GetItemId( i );
362 if ( (nItemId < FONTSTYLEMENU_FIRSTID) ||
363 (nItemId > FONTSTYLEMENU_LASTID) )
364 break;
366 if ( IsItemChecked( nItemId ) )
367 nChecked = nItemId;
369 XubString aText = GetItemText( nItemId );
370 if ( aText == maCurStyle )
372 CheckItem( nItemId, TRUE );
373 return;
377 if ( nChecked )
378 CheckItem( nChecked, FALSE );
381 // ========================================================================
383 FontSizeMenu::FontSizeMenu()
384 : mpHeightAry( NULL )
385 , mnCurHeight( 100 )
387 SetMenuFlags( GetMenuFlags() | MENU_FLAG_NOAUTOMNEMONICS );
390 // -----------------------------------------------------------------------
392 FontSizeMenu::~FontSizeMenu()
394 if ( mpHeightAry )
395 delete[] mpHeightAry;
398 // -----------------------------------------------------------------------
400 void FontSizeMenu::Select()
402 const USHORT nCurItemId = GetCurItemId();
403 mnCurHeight = mpHeightAry[ nCurItemId - 1 ];
404 maSelectHdl.Call( this );
407 // -----------------------------------------------------------------------
409 void FontSizeMenu::Highlight()
411 const long nTempHeight = mnCurHeight;
412 const USHORT nCurItemId = GetCurItemId();
413 if ( !nCurItemId )
414 mnCurHeight = 0;
415 else
417 //sal_Int32 nValue = GetItemText( nCurItemId ).ToInt32();
418 mnCurHeight = mpHeightAry[ nCurItemId - 1 ];
420 maHighlightHdl.Call( this );
421 mnCurHeight = nTempHeight;
424 // -----------------------------------------------------------------------
426 void FontSizeMenu::Fill( const FontInfo& rInfo, const FontList* pList )
428 Clear();
430 // setup font size array
431 if ( mpHeightAry )
432 delete[] mpHeightAry;
434 const long* pTempAry;
435 const long* pAry = pList->GetSizeAry( rInfo );
436 USHORT nSizeCount = 0;
437 while ( pAry[nSizeCount] )
438 nSizeCount++;
440 USHORT nPos = 0;
442 // first insert font size names (for simplified/traditional chinese)
443 FontSizeNames aFontSizeNames( Application::GetSettings().GetUILanguage() );
444 mpHeightAry = new long[nSizeCount+aFontSizeNames.Count()];
445 if ( !aFontSizeNames.IsEmpty() )
447 if ( pAry == pList->GetStdSizeAry() )
449 // for scalable fonts all font size names
450 ULONG nCount = aFontSizeNames.Count();
451 for( ULONG i = 0; i < nCount; i++ )
453 String aSizeName = aFontSizeNames.GetIndexName( i );
454 long nSize = aFontSizeNames.GetIndexSize( i );
455 mpHeightAry[nPos] = nSize;
456 nPos++; // Id is nPos+1
457 InsertItem( nPos, aSizeName, MIB_RADIOCHECK | MIB_AUTOCHECK );
460 else
462 // for fixed size fonts only selectable font size names
463 pTempAry = pAry;
464 while ( *pTempAry )
466 String aSizeName = aFontSizeNames.Size2Name( *pTempAry );
467 if ( aSizeName.Len() )
469 mpHeightAry[nPos] = *pTempAry;
470 nPos++; // Id is nPos+1
471 InsertItem( nPos, aSizeName, MIB_RADIOCHECK | MIB_AUTOCHECK );
473 pTempAry++;
478 // then insert numerical font size values
479 const vcl::I18nHelper& rI18nHelper = Application::GetSettings().GetUILocaleI18nHelper();
480 pTempAry = pAry;
481 while ( *pTempAry )
483 mpHeightAry[nPos] = *pTempAry;
484 nPos++; // Id is nPos+1
485 InsertItem( nPos, rI18nHelper.GetNum( *pTempAry, 1, TRUE, FALSE ), MIB_RADIOCHECK | MIB_AUTOCHECK );
486 pTempAry++;
489 SetCurHeight( mnCurHeight );
492 // -----------------------------------------------------------------------
494 void FontSizeMenu::SetCurHeight( long nHeight )
496 mnCurHeight = nHeight;
498 // check menue item
499 XubString aHeight = Application::GetSettings().GetUILocaleI18nHelper().GetNum( nHeight, 1, TRUE, FALSE );
500 USHORT nChecked = 0;
501 USHORT nItemCount = GetItemCount();
502 for( USHORT i = 0; i < nItemCount; i++ )
504 USHORT nItemId = GetItemId( i );
506 if ( mpHeightAry[i] == nHeight )
508 CheckItem( nItemId, TRUE );
509 return;
512 if ( IsItemChecked( nItemId ) )
513 nChecked = nItemId;
516 if ( nChecked )
517 CheckItem( nChecked, FALSE );