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