xref: /aoo41x/main/svtools/source/control/stdmenu.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 #include <string.h>
32 #include <vcl/svapp.hxx>
33 #include <vcl/i18nhelp.hxx>
34 #include <svtools/ctrltool.hxx>
35 #include <svtools/stdmenu.hxx>
36 
37 // ========================================================================
38 
39 FontNameMenu::FontNameMenu()
40 {
41 	SetMenuFlags( GetMenuFlags() | MENU_FLAG_NOAUTOMNEMONICS );
42 }
43 
44 // -----------------------------------------------------------------------
45 
46 FontNameMenu::~FontNameMenu()
47 {
48 }
49 
50 // -----------------------------------------------------------------------
51 
52 void FontNameMenu::Select()
53 {
54 	maCurName = GetItemText( GetCurItemId() );
55 	maSelectHdl.Call( this );
56 }
57 
58 // -----------------------------------------------------------------------
59 
60 void FontNameMenu::Highlight()
61 {
62 	XubString aTempName = maCurName;
63 	maCurName = GetItemText( GetCurItemId() );
64 	maHighlightHdl.Call( this );
65 	maCurName = aTempName;
66 }
67 
68 // -----------------------------------------------------------------------
69 
70 void FontNameMenu::Fill( const FontList* pList )
71 {
72     // clear menu
73 	Clear();
74 
75     // add fonts
76     const vcl::I18nHelper& rI18nHelper = Application::GetSettings().GetUILocaleI18nHelper();
77     // more than 100 fonts reduces the speed of opening the menu.
78     // So only the first 100 fonts will be displayed.
79     sal_uInt16 nFontCount = ::std::min( pList->GetFontNameCount(), static_cast< sal_uInt16 >(100) );
80 	for ( sal_uInt16 i = 0; i < nFontCount; i++ )
81 	{
82 		const XubString& rName = pList->GetFontName( i ).GetName();
83 
84         // sort with the I18nHelper
85 		sal_uInt16 j = GetItemCount();
86 		while ( j )
87 		{
88 			XubString aText = GetItemText( GetItemId( j-1 ) );
89 			if ( rI18nHelper.CompareString( rName, aText ) > 0 )
90 				break;
91 			j--;
92 		}
93 		InsertItem( i+1, rName, MIB_RADIOCHECK | MIB_AUTOCHECK, j );
94 	}
95 
96 	SetCurName( maCurName );
97 }
98 
99 // -----------------------------------------------------------------------
100 
101 void FontNameMenu::SetCurName( const XubString& rName )
102 {
103 	maCurName = rName;
104 
105 	// Menueintrag checken
106 	sal_uInt16 nChecked = 0;
107 	sal_uInt16 nItemCount = GetItemCount();
108 	for( sal_uInt16 i = 0; i < nItemCount; i++ )
109 	{
110 		sal_uInt16 nItemId = GetItemId( i );
111 
112 		if ( IsItemChecked( nItemId ) )
113 			nChecked = nItemId;
114 
115 		XubString aText = GetItemText( nItemId );
116 		if ( aText == maCurName )
117 		{
118 			CheckItem( nItemId, sal_True );
119 			return;
120 		}
121 	}
122 
123 	if ( nChecked )
124 		CheckItem( nChecked, sal_False );
125 }
126 
127 // ========================================================================
128 
129 FontStyleMenu::FontStyleMenu()
130 {
131 	SetMenuFlags( GetMenuFlags() | MENU_FLAG_NOAUTOMNEMONICS );
132 }
133 
134 // -----------------------------------------------------------------------
135 
136 FontStyleMenu::~FontStyleMenu()
137 {
138 }
139 
140 // -----------------------------------------------------------------------
141 
142 void FontStyleMenu::Select()
143 {
144 	sal_uInt16 nCurId = GetCurItemId();
145 
146 	if ( (nCurId >= FONTSTYLEMENU_FIRSTID) && (nCurId <= FONTSTYLEMENU_LASTID) )
147 	{
148 		maCurStyle = GetItemText( nCurId );
149 		maSelectHdl.Call( this );
150 	}
151 	else
152 		PopupMenu::Select();
153 }
154 
155 // -----------------------------------------------------------------------
156 
157 void FontStyleMenu::Highlight()
158 {
159 	sal_uInt16 nCurId = GetCurItemId();
160 
161 	if ( (nCurId >= FONTSTYLEMENU_FIRSTID) && (nCurId <= FONTSTYLEMENU_LASTID) )
162 	{
163 		XubString aTempName = maCurStyle;
164 		maCurStyle = GetItemText( nCurId );
165 		maHighlightHdl.Call( this );
166 		maCurStyle = aTempName;
167 	}
168 	else
169 		PopupMenu::Highlight();
170 }
171 
172 // -----------------------------------------------------------------------
173 
174 sal_Bool FontStyleMenu::ImplIsAlreadyInserted( const XubString& rStyleName, sal_uInt16 nCount )
175 {
176 	for ( sal_uInt16 i = 0; i < nCount; i++ )
177 	{
178 		if ( GetItemText( i+FONTSTYLEMENU_FIRSTID ) == rStyleName )
179 			return sal_True;
180 	}
181 
182 	return sal_False;
183 }
184 
185 // -----------------------------------------------------------------------
186 
187 void FontStyleMenu::Fill( const XubString& rName, const FontList* pList )
188 {
189 	sal_uInt16 nItemId = GetItemId( 0 );
190 	while ( (nItemId >= FONTSTYLEMENU_FIRSTID) &&
191 			(nItemId <= FONTSTYLEMENU_LASTID) )
192 	{
193 		RemoveItem( 0 );
194 		nItemId = GetItemId( 0 );
195 	}
196 
197 	// Existiert ein Font mit diesem Namen
198 	sal_Handle hFontInfo = pList->GetFirstFontInfo( rName );
199 	if ( hFontInfo )
200 	{
201 		XubString	aStyleText;
202 		sal_uInt16		nPos = 0;
203 		sal_uInt16		nId = FONTSTYLEMENU_FIRSTID;
204 		FontWeight	eLastWeight = WEIGHT_DONTKNOW;
205 		FontItalic	eLastItalic = ITALIC_NONE;
206 		FontWidth	eLastWidth = WIDTH_DONTKNOW;
207 		sal_Bool		bNormal = sal_False;
208 		sal_Bool		bItalic = sal_False;
209 		sal_Bool		bBold = sal_False;
210 		sal_Bool		bBoldItalic = sal_False;
211 		sal_Bool		bInsert = sal_False;
212 		FontInfo	aInfo;
213 		while ( hFontInfo )
214 		{
215 			aInfo = pList->GetFontInfo( hFontInfo );
216 
217 			FontWeight	eWeight = aInfo.GetWeight();
218 			FontItalic	eItalic = aInfo.GetItalic();
219 			FontWidth	eWidth = aInfo.GetWidthType();
220 			// Only if the attributes are different, we insert the
221 			// Font to avoid double Entries in different languages
222 			if ( (eWeight != eLastWeight) || (eItalic != eLastItalic) ||
223 				 (eWidth != eLastWidth) )
224 			{
225 				if ( bInsert )
226 				{
227 					InsertItem( nId, aStyleText,
228 								MIB_RADIOCHECK | MIB_AUTOCHECK, nPos );
229 					nPos++;
230 					nId++;
231 				}
232 
233 				if ( eWeight <= WEIGHT_NORMAL )
234 				{
235 					if ( eItalic != ITALIC_NONE )
236 						bItalic = sal_True;
237 					else
238 						bNormal = sal_True;
239 				}
240 				else
241 				{
242 					if ( eItalic != ITALIC_NONE )
243 						bBoldItalic = sal_True;
244 					else
245 						bBold = sal_True;
246 				}
247 
248 				// For wrong StyleNames we replace this with the correct once
249 				aStyleText = pList->GetStyleName( aInfo );
250 				bInsert = !ImplIsAlreadyInserted( aStyleText, nPos );
251 				if ( !bInsert )
252 				{
253 					aStyleText = pList->GetStyleName( eWeight, eItalic );
254 					bInsert = !ImplIsAlreadyInserted( aStyleText, nPos );
255 				}
256 
257 				eLastWeight = eWeight;
258 				eLastItalic = eItalic;
259 				eLastWidth = eWidth;
260 			}
261 			else
262 			{
263 				if ( bInsert )
264 				{
265 					// If we have two names for the same attributes
266 					// we prefer the translated standard names
267 					const XubString& rAttrStyleText = pList->GetStyleName( eWeight, eItalic );
268 					if ( rAttrStyleText != aStyleText )
269 					{
270 						XubString aTempStyleText = pList->GetStyleName( aInfo );
271 						if ( rAttrStyleText == aTempStyleText )
272 							aStyleText = rAttrStyleText;
273 						bInsert = !ImplIsAlreadyInserted( aStyleText, nPos );
274 					}
275 				}
276 			}
277 
278 			if ( !bItalic && (aStyleText == pList->GetItalicStr()) )
279 				bItalic = sal_True;
280 			else if ( !bBold && (aStyleText == pList->GetBoldStr()) )
281 				bBold = sal_True;
282 			else if ( !bBoldItalic && (aStyleText == pList->GetBoldItalicStr()) )
283 				bBoldItalic = sal_True;
284 
285 			hFontInfo = pList->GetNextFontInfo( hFontInfo );
286 		}
287 
288 		if ( bInsert )
289 		{
290 			InsertItem( nId, aStyleText,
291 						MIB_RADIOCHECK | MIB_AUTOCHECK, nPos );
292 			nPos++;
293 			nId++;
294 		}
295 
296 		// Bestimmte Styles als Nachbildung
297 		if ( bNormal )
298 		{
299 			if ( !bItalic )
300 			{
301 				InsertItem( nId, pList->GetItalicStr(),
302 							MIB_RADIOCHECK | MIB_AUTOCHECK, nPos );
303 				nPos++;
304 				nId++;
305 			}
306 			if ( !bBold )
307 			{
308 				InsertItem( nId, pList->GetBoldStr(),
309 							MIB_RADIOCHECK | MIB_AUTOCHECK, nPos );
310 				nPos++;
311 				nId++;
312 			}
313 		}
314 		if ( !bBoldItalic )
315 		{
316 			if ( bNormal || bItalic || bBold )
317 			{
318 				InsertItem( nId, pList->GetBoldItalicStr(),
319 							MIB_RADIOCHECK | MIB_AUTOCHECK, nPos );
320 				nPos++;
321 				nId++;
322 			}
323 		}
324 	}
325 	else
326 	{
327 		// Wenn Font nicht, dann Standard-Styles einfuegen
328 		InsertItem( FONTSTYLEMENU_FIRSTID,	 pList->GetNormalStr(),
329 					MIB_RADIOCHECK | MIB_AUTOCHECK, 0 );
330 		InsertItem( FONTSTYLEMENU_FIRSTID+1, pList->GetItalicStr(),
331 					MIB_RADIOCHECK | MIB_AUTOCHECK, 0 );
332 		InsertItem( FONTSTYLEMENU_FIRSTID+2, pList->GetBoldStr(),
333 					MIB_RADIOCHECK | MIB_AUTOCHECK, 0 );
334 		InsertItem( FONTSTYLEMENU_FIRSTID+3, pList->GetBoldItalicStr(),
335 					MIB_RADIOCHECK | MIB_AUTOCHECK, 0 );
336 	}
337 
338 	SetCurStyle( maCurStyle );
339 }
340 
341 // -----------------------------------------------------------------------
342 
343 void FontStyleMenu::SetCurStyle( const XubString& rStyle )
344 {
345 	maCurStyle = rStyle;
346 
347 	// Menueintrag checken
348 	sal_uInt16 nChecked = 0;
349 	sal_uInt16 nItemCount = GetItemCount();
350 	for( sal_uInt16 i = 0; i < nItemCount; i++ )
351 	{
352 		sal_uInt16 nItemId = GetItemId( i );
353 
354 		if ( (nItemId < FONTSTYLEMENU_FIRSTID) ||
355 			 (nItemId > FONTSTYLEMENU_LASTID) )
356 			break;
357 
358 		if ( IsItemChecked( nItemId ) )
359 			nChecked = nItemId;
360 
361 		XubString aText = GetItemText( nItemId );
362 		if ( aText == maCurStyle )
363 		{
364 			CheckItem( nItemId, sal_True );
365 			return;
366 		}
367 	}
368 
369 	if ( nChecked )
370 		CheckItem( nChecked, sal_False );
371 }
372 
373 // ========================================================================
374 
375 FontSizeMenu::FontSizeMenu()
376 :    mpHeightAry( NULL )
377 ,    mnCurHeight( 100 )
378 {
379 	SetMenuFlags( GetMenuFlags() | MENU_FLAG_NOAUTOMNEMONICS );
380 }
381 
382 // -----------------------------------------------------------------------
383 
384 FontSizeMenu::~FontSizeMenu()
385 {
386 	if ( mpHeightAry )
387 		delete[] mpHeightAry;
388 }
389 
390 // -----------------------------------------------------------------------
391 
392 void FontSizeMenu::Select()
393 {
394 	const sal_uInt16 nCurItemId = GetCurItemId();
395 	mnCurHeight = mpHeightAry[ nCurItemId - 1 ];
396 	maSelectHdl.Call( this );
397 }
398 
399 // -----------------------------------------------------------------------
400 
401 void FontSizeMenu::Highlight()
402 {
403 	const long nTempHeight = mnCurHeight;
404 	const sal_uInt16 nCurItemId = GetCurItemId();
405 	if ( !nCurItemId )
406 		mnCurHeight = 0;
407 	else
408 	{
409 		//sal_Int32 nValue = GetItemText( nCurItemId ).ToInt32();
410 		mnCurHeight = mpHeightAry[ nCurItemId - 1 ];
411 	}
412 	maHighlightHdl.Call( this );
413 	mnCurHeight = nTempHeight;
414 }
415 
416 // -----------------------------------------------------------------------
417 
418 void FontSizeMenu::Fill( const FontInfo& rInfo, const FontList* pList )
419 {
420 	Clear();
421 
422 	// setup font size array
423 	if ( mpHeightAry )
424 		delete[] mpHeightAry;
425 
426 	const long* pTempAry;
427 	const long* pAry = pList->GetSizeAry( rInfo );
428 	sal_uInt16 nSizeCount = 0;
429 	while ( pAry[nSizeCount] )
430 		nSizeCount++;
431 
432 	sal_uInt16 nPos = 0;
433 
434 	// first insert font size names (for simplified/traditional chinese)
435     FontSizeNames aFontSizeNames( Application::GetSettings().GetUILanguage() );
436 	mpHeightAry = new long[nSizeCount+aFontSizeNames.Count()];
437 	if ( !aFontSizeNames.IsEmpty() )
438 	{
439 		if ( pAry == pList->GetStdSizeAry() )
440 		{
441 			// for scalable fonts all font size names
442 			sal_uLong nCount = aFontSizeNames.Count();
443 			for( sal_uLong i = 0; i < nCount; i++ )
444 			{
445 				String	aSizeName = aFontSizeNames.GetIndexName( i );
446 				long	nSize = aFontSizeNames.GetIndexSize( i );
447 				mpHeightAry[nPos] = nSize;
448 				nPos++; // Id is nPos+1
449 				InsertItem( nPos, aSizeName, MIB_RADIOCHECK | MIB_AUTOCHECK );
450 			}
451 		}
452 		else
453 		{
454 			// for fixed size fonts only selectable font size names
455 			pTempAry = pAry;
456 			while ( *pTempAry )
457 			{
458 				String aSizeName = aFontSizeNames.Size2Name( *pTempAry );
459 				if ( aSizeName.Len() )
460 				{
461 					mpHeightAry[nPos] = *pTempAry;
462 					nPos++; // Id is nPos+1
463 					InsertItem( nPos, aSizeName, MIB_RADIOCHECK | MIB_AUTOCHECK );
464 				}
465 				pTempAry++;
466 			}
467 		}
468 	}
469 
470 	// then insert numerical font size values
471     const vcl::I18nHelper& rI18nHelper = Application::GetSettings().GetUILocaleI18nHelper();
472 	pTempAry = pAry;
473 	while ( *pTempAry )
474 	{
475 		mpHeightAry[nPos] = *pTempAry;
476 		nPos++; // Id is nPos+1
477 		InsertItem( nPos, rI18nHelper.GetNum( *pTempAry, 1, sal_True, sal_False ), MIB_RADIOCHECK | MIB_AUTOCHECK );
478 		pTempAry++;
479 	}
480 
481 	SetCurHeight( mnCurHeight );
482 }
483 
484 // -----------------------------------------------------------------------
485 
486 void FontSizeMenu::SetCurHeight( long nHeight )
487 {
488 	mnCurHeight = nHeight;
489 
490 	// check menue item
491 	XubString	aHeight = Application::GetSettings().GetUILocaleI18nHelper().GetNum( nHeight, 1, sal_True, sal_False  );
492 	sal_uInt16		nChecked = 0;
493 	sal_uInt16		nItemCount = GetItemCount();
494 	for( sal_uInt16 i = 0; i < nItemCount; i++ )
495 	{
496 		sal_uInt16 nItemId = GetItemId( i );
497 
498 		if ( mpHeightAry[i] == nHeight )
499 		{
500 			CheckItem( nItemId, sal_True );
501 			return;
502 		}
503 
504 		if ( IsItemChecked( nItemId ) )
505 			nChecked = nItemId;
506 	}
507 
508 	if ( nChecked )
509 		CheckItem( nChecked, sal_False );
510 }
511