Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <stdio.h>
21 :
22 : #include <tools/shl.hxx>
23 : #include <vcl/svapp.hxx>
24 : #include <svtools/colorcfg.hxx>
25 : #include <svl/eitem.hxx>
26 : #include <svl/intitem.hxx>
27 : #include <svl/itempool.hxx>
28 :
29 : #include <rtl/textenc.h>
30 : #include <svx/ucsubset.hxx>
31 : #include <sfx2/objsh.hxx>
32 : #include <vcl/msgbox.hxx>
33 : #include <svl/stritem.hxx>
34 :
35 : #include <cuires.hrc>
36 : #include <dialmgr.hxx>
37 : #include "cuicharmap.hxx"
38 : #include <sfx2/request.hxx>
39 : #include <sfx2/sfxsids.hrc>
40 : #include <sfx2/app.hxx>
41 : #include <editeng/fontitem.hxx>
42 : #include "macroass.hxx"
43 :
44 : // class SvxCharacterMap =================================================
45 :
46 0 : SvxCharacterMap::SvxCharacterMap( Window* pParent, sal_Bool bOne_, const SfxItemSet* pSet )
47 : : SfxModalDialog(pParent, "SpecialCharactersDialog", "cui/ui/specialcharacters.ui")
48 : , bOne( bOne_ )
49 0 : , pSubsetMap( NULL )
50 : {
51 0 : get(m_pShowSet, "showcharset");
52 0 : get(m_pShowChar, "showchar");
53 0 : m_pShowChar->SetCentered(true);
54 0 : get(m_pShowText, "showtext");
55 0 : get(m_pOKBtn, "ok");
56 0 : get(m_pDeleteBtn, "delete");
57 0 : get(m_pFontText, "fontft");
58 0 : get(m_pFontLB, "fontlb");
59 0 : m_pFontLB->SetStyle(m_pFontLB->GetStyle() | WB_SORT);
60 0 : get(m_pSubsetText, "subsetft");
61 0 : get(m_pSubsetLB, "subsetlb");
62 0 : get(m_pCharCodeText, "charcodeft");
63 0 : get(m_pSymbolText, "symboltext");
64 :
65 0 : SFX_ITEMSET_ARG( pSet, pItem, SfxBoolItem, FN_PARAM_1, sal_False );
66 0 : if ( pItem )
67 0 : bOne = pItem->GetValue();
68 :
69 0 : init();
70 :
71 0 : SFX_ITEMSET_ARG( pSet, pCharItem, SfxInt32Item, SID_ATTR_CHAR, sal_False );
72 0 : if ( pCharItem )
73 0 : SetChar( pCharItem->GetValue() );
74 :
75 0 : SFX_ITEMSET_ARG( pSet, pDisableItem, SfxBoolItem, FN_PARAM_2, sal_False );
76 0 : if ( pDisableItem && pDisableItem->GetValue() )
77 0 : DisableFontSelection();
78 :
79 0 : SFX_ITEMSET_ARG( pSet, pFontItem, SvxFontItem, SID_ATTR_CHAR_FONT, sal_False );
80 0 : SFX_ITEMSET_ARG( pSet, pFontNameItem, SfxStringItem, SID_FONT_NAME, sal_False );
81 0 : if ( pFontItem )
82 : {
83 0 : Font aTmpFont( pFontItem->GetFamilyName(), pFontItem->GetStyleName(), GetCharFont().GetSize() );
84 0 : aTmpFont.SetCharSet( pFontItem->GetCharSet() );
85 0 : aTmpFont.SetPitch( pFontItem->GetPitch() );
86 0 : SetCharFont( aTmpFont );
87 : }
88 0 : else if ( pFontNameItem )
89 : {
90 0 : Font aTmpFont( GetCharFont() );
91 0 : aTmpFont.SetName( pFontNameItem->GetValue() );
92 0 : SetCharFont( aTmpFont );
93 : }
94 :
95 0 : CreateOutputItemSet( pSet ? *pSet->GetPool() : SFX_APP()->GetPool() );
96 0 : }
97 :
98 : // -----------------------------------------------------------------------
99 :
100 0 : SvxCharacterMap::~SvxCharacterMap()
101 : {
102 0 : }
103 :
104 : // -----------------------------------------------------------------------
105 :
106 0 : const Font& SvxCharacterMap::GetCharFont() const
107 : {
108 0 : return aFont;
109 : }
110 :
111 : // -----------------------------------------------------------------------
112 :
113 0 : void SvxCharacterMap::SetChar( sal_UCS4 c )
114 : {
115 0 : m_pShowSet->SelectCharacter( c );
116 0 : }
117 :
118 : // -----------------------------------------------------------------------
119 :
120 0 : sal_UCS4 SvxCharacterMap::GetChar() const
121 : {
122 0 : return m_pShowSet->GetSelectCharacter();
123 : }
124 :
125 : // -----------------------------------------------------------------------
126 :
127 0 : String SvxCharacterMap::GetCharacters() const
128 : {
129 0 : return m_pShowText->GetText();
130 : }
131 :
132 :
133 : // -----------------------------------------------------------------------
134 :
135 0 : void SvxCharacterMap::DisableFontSelection()
136 : {
137 0 : m_pFontText->Disable();
138 0 : m_pFontLB->Disable();
139 0 : }
140 :
141 0 : short SvxCharacterMap::Execute()
142 : {
143 0 : short nResult = SfxModalDialog::Execute();
144 0 : if ( nResult == RET_OK )
145 : {
146 0 : SfxItemSet* pSet = GetItemSet();
147 0 : if ( pSet )
148 : {
149 0 : const SfxItemPool* pPool = pSet->GetPool();
150 0 : const Font& rFont( GetCharFont() );
151 0 : pSet->Put( SfxStringItem( pPool->GetWhich(SID_CHARMAP), GetCharacters() ) );
152 0 : pSet->Put( SvxFontItem( rFont.GetFamily(), rFont.GetName(),
153 0 : rFont.GetStyleName(), rFont.GetPitch(), rFont.GetCharSet(), pPool->GetWhich(SID_ATTR_CHAR_FONT) ) );
154 0 : pSet->Put( SfxStringItem( pPool->GetWhich(SID_FONT_NAME), rFont.GetName() ) );
155 0 : pSet->Put( SfxInt32Item( pPool->GetWhich(SID_ATTR_CHAR), GetChar() ) );
156 : }
157 : }
158 :
159 0 : return nResult;
160 : }
161 :
162 :
163 : // class SvxShowText =====================================================
164 :
165 0 : SvxShowText::SvxShowText(Window* pParent, sal_Bool bCenter)
166 : : Control( pParent ),
167 0 : mbCenter( bCenter)
168 0 : {}
169 :
170 0 : extern "C" SAL_DLLPUBLIC_EXPORT Window* SAL_CALL makeSvxShowText(Window *pParent, VclBuilder::stringmap &)
171 : {
172 0 : return new SvxShowText(pParent);
173 : }
174 :
175 : // -----------------------------------------------------------------------
176 :
177 0 : void SvxShowText::Paint( const Rectangle& )
178 : {
179 0 : Color aTextCol = GetTextColor();
180 :
181 0 : const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
182 0 : const Color aWindowTextColor( rStyleSettings.GetDialogTextColor() );
183 0 : SetTextColor( aWindowTextColor );
184 :
185 0 : const String aText = GetText();
186 0 : const Size aSize = GetOutputSizePixel();
187 0 : Point aPoint( 2, mnY );
188 :
189 : // adjust position using ink boundary if possible
190 0 : Rectangle aBoundRect;
191 0 : if( !GetTextBoundRect( aBoundRect, aText ) || aBoundRect.IsEmpty() )
192 0 : aPoint.X() = (aSize.Width() - GetTextWidth( aText )) / 2;
193 : else
194 : {
195 : // adjust position before it gets out of bounds
196 0 : aBoundRect += aPoint;
197 :
198 : // shift back vertically if needed
199 0 : int nYLDelta = aBoundRect.Top();
200 0 : int nYHDelta = aSize.Height() - aBoundRect.Bottom();
201 0 : if( nYLDelta <= 0 )
202 0 : aPoint.Y() -= nYLDelta - 1;
203 0 : else if( nYHDelta <= 0 )
204 0 : aPoint.Y() += nYHDelta - 1;
205 :
206 0 : if( mbCenter )
207 : {
208 : // move glyph to middle of cell
209 0 : aPoint.X() = -aBoundRect.Left()
210 0 : + (aSize.Width() - aBoundRect.GetWidth()) / 2;
211 : }
212 : else
213 : {
214 : // shift back horizontally if needed
215 0 : int nXLDelta = aBoundRect.Left();
216 0 : int nXHDelta = aSize.Width() - aBoundRect.Right();
217 0 : if( nXLDelta <= 0 )
218 0 : aPoint.X() -= nXLDelta - 1;
219 0 : else if( nXHDelta <= 0 )
220 0 : aPoint.X() += nXHDelta - 1;
221 : }
222 : }
223 :
224 0 : DrawText( aPoint, aText );
225 0 : SetTextColor( aTextCol );
226 0 : }
227 :
228 : // -----------------------------------------------------------------------
229 :
230 0 : void SvxShowText::SetFont( const Font& rFont )
231 : {
232 0 : long nWinHeight = GetOutputSizePixel().Height();
233 0 : Font aFont = rFont;
234 0 : aFont.SetWeight( WEIGHT_NORMAL );
235 0 : aFont.SetAlign( ALIGN_TOP );
236 0 : aFont.SetSize( PixelToLogic( Size( 0, nWinHeight/2 ) ) );
237 0 : aFont.SetTransparent( sal_True );
238 0 : Control::SetFont( aFont );
239 0 : mnY = ( nWinHeight - GetTextHeight() ) / 2;
240 :
241 0 : Invalidate();
242 0 : }
243 :
244 0 : void SvxShowText::Resize()
245 : {
246 0 : Control::Resize();
247 0 : SetFont(GetFont()); //force recalculation of size
248 0 : }
249 :
250 : // -----------------------------------------------------------------------
251 :
252 0 : void SvxShowText::SetText( const String& rText )
253 : {
254 0 : Control::SetText( rText );
255 0 : Invalidate();
256 0 : }
257 :
258 : // -----------------------------------------------------------------------
259 :
260 0 : SvxShowText::~SvxShowText()
261 0 : {}
262 :
263 : // class SvxCharacterMap =================================================
264 :
265 0 : void SvxCharacterMap::init()
266 : {
267 0 : aFont = GetFont();
268 0 : aFont.SetTransparent( sal_True );
269 0 : aFont.SetFamily( FAMILY_DONTKNOW );
270 0 : aFont.SetPitch( PITCH_DONTKNOW );
271 0 : aFont.SetCharSet( RTL_TEXTENCODING_DONTKNOW );
272 :
273 0 : if (bOne)
274 : {
275 0 : m_pSymbolText->Hide();
276 0 : m_pShowText->Hide();
277 0 : m_pDeleteBtn->Hide();
278 : }
279 :
280 0 : rtl::OUString aDefStr( aFont.GetName() );
281 0 : String aLastName;
282 0 : int nCount = GetDevFontCount();
283 0 : for ( int i = 0; i < nCount; i++ )
284 : {
285 0 : String aFontName( GetDevFont( i ).GetName() );
286 0 : if ( aFontName != aLastName )
287 : {
288 0 : aLastName = aFontName;
289 0 : sal_uInt16 nPos = m_pFontLB->InsertEntry( aFontName );
290 0 : m_pFontLB->SetEntryData( nPos, (void*)(sal_uLong)i );
291 : }
292 0 : }
293 : // the font may not be in the list =>
294 : // try to find a font name token in list and select found font,
295 : // else select topmost entry
296 0 : bool bFound = (m_pFontLB->GetEntryPos( aDefStr ) == LISTBOX_ENTRY_NOTFOUND );
297 0 : if( !bFound )
298 : {
299 0 : sal_Int32 nIndex = 0;
300 0 : do
301 : {
302 0 : rtl::OUString aToken = aDefStr.getToken(0, ';', nIndex);
303 0 : if ( m_pFontLB->GetEntryPos( aToken ) != LISTBOX_ENTRY_NOTFOUND )
304 : {
305 0 : aDefStr = aToken;
306 0 : bFound = sal_True;
307 : break;
308 0 : }
309 : }
310 : while ( nIndex >= 0 );
311 : }
312 :
313 0 : if ( bFound )
314 0 : m_pFontLB->SelectEntry( aDefStr );
315 0 : else if ( m_pFontLB->GetEntryCount() )
316 0 : m_pFontLB->SelectEntryPos(0);
317 0 : FontSelectHdl(m_pFontLB);
318 :
319 0 : m_pOKBtn->SetClickHdl( LINK( this, SvxCharacterMap, OKHdl ) );
320 0 : m_pFontLB->SetSelectHdl( LINK( this, SvxCharacterMap, FontSelectHdl ) );
321 0 : m_pSubsetLB->SetSelectHdl( LINK( this, SvxCharacterMap, SubsetSelectHdl ) );
322 0 : m_pShowSet->SetDoubleClickHdl( LINK( this, SvxCharacterMap, CharDoubleClickHdl ) );
323 0 : m_pShowSet->SetSelectHdl( LINK( this, SvxCharacterMap, CharSelectHdl ) );
324 0 : m_pShowSet->SetHighlightHdl( LINK( this, SvxCharacterMap, CharHighlightHdl ) );
325 0 : m_pShowSet->SetPreSelectHdl( LINK( this, SvxCharacterMap, CharPreSelectHdl ) );
326 0 : m_pDeleteBtn->SetClickHdl( LINK( this, SvxCharacterMap, DeleteHdl ) );
327 :
328 0 : if( SvxShowCharSet::getSelectedChar() == ' ')
329 0 : m_pOKBtn->Disable();
330 : else
331 0 : m_pOKBtn->Enable();
332 0 : }
333 :
334 : // -----------------------------------------------------------------------
335 :
336 0 : void SvxCharacterMap::SetCharFont( const Font& rFont )
337 : {
338 : // first get the underlying info in order to get font names
339 : // like "Times New Roman;Times" resolved
340 0 : Font aTmp( GetFontMetric( rFont ) );
341 :
342 0 : if ( m_pFontLB->GetEntryPos( aTmp.GetName() ) == LISTBOX_ENTRY_NOTFOUND )
343 0 : return;
344 :
345 0 : m_pFontLB->SelectEntry( aTmp.GetName() );
346 0 : aFont = aTmp;
347 0 : FontSelectHdl(m_pFontLB);
348 :
349 : // for compatibility reasons
350 0 : ModalDialog::SetFont( aFont );
351 : }
352 :
353 : // -----------------------------------------------------------------------
354 :
355 0 : IMPL_LINK_NOARG(SvxCharacterMap, OKHdl)
356 : {
357 0 : String aStr = m_pShowText->GetText();
358 :
359 0 : if ( !aStr.Len() )
360 : {
361 0 : sal_UCS4 cChar = m_pShowSet->GetSelectCharacter();
362 : // using the new UCS4 constructor
363 0 : rtl::OUString aOUStr( &cChar, 1 );
364 0 : m_pShowText->SetText( aOUStr );
365 : }
366 0 : EndDialog( sal_True );
367 0 : return 0;
368 : }
369 :
370 : // -----------------------------------------------------------------------
371 :
372 0 : IMPL_LINK_NOARG(SvxCharacterMap, FontSelectHdl)
373 : {
374 0 : sal_uInt16 nPos = m_pFontLB->GetSelectEntryPos(),
375 0 : nFont = (sal_uInt16)(sal_uLong)m_pFontLB->GetEntryData( nPos );
376 0 : aFont = GetDevFont( nFont );
377 0 : aFont.SetWeight( WEIGHT_DONTKNOW );
378 0 : aFont.SetItalic( ITALIC_NONE );
379 0 : aFont.SetWidthType( WIDTH_DONTKNOW );
380 0 : aFont.SetPitch( PITCH_DONTKNOW );
381 0 : aFont.SetFamily( FAMILY_DONTKNOW );
382 :
383 : // notify children using this font
384 0 : m_pShowSet->SetFont( aFont );
385 0 : m_pShowChar->SetFont( aFont );
386 0 : m_pShowText->SetFont( aFont );
387 :
388 : // setup unicode subset listbar with font specific subsets,
389 : // hide unicode subset listbar for symbol fonts
390 : // TODO: get info from the Font once it provides it
391 0 : delete pSubsetMap;
392 0 : pSubsetMap = NULL;
393 :
394 0 : sal_Bool bNeedSubset = (aFont.GetCharSet() != RTL_TEXTENCODING_SYMBOL);
395 0 : if( bNeedSubset )
396 : {
397 0 : FontCharMap aFontCharMap;
398 0 : m_pShowSet->GetFontCharMap( aFontCharMap );
399 0 : pSubsetMap = new SubsetMap( &aFontCharMap );
400 :
401 : // update subset listbox for new font's unicode subsets
402 0 : m_pSubsetLB->Clear();
403 : // TODO: is it worth to improve the stupid linear search?
404 0 : bool bFirst = true;
405 : const Subset* s;
406 0 : while( NULL != (s = pSubsetMap->GetNextSubset( bFirst )) )
407 : {
408 0 : sal_uInt16 nPos_ = m_pSubsetLB->InsertEntry( s->GetName() );
409 0 : m_pSubsetLB->SetEntryData( nPos_, (void*)s );
410 : // NOTE: subset must live at least as long as the selected font
411 0 : if( bFirst )
412 0 : m_pSubsetLB->SelectEntryPos( nPos_ );
413 0 : bFirst = false;
414 : }
415 0 : if( m_pSubsetLB->GetEntryCount() <= 1 )
416 0 : bNeedSubset = sal_False;
417 : }
418 :
419 0 : m_pSubsetText->Show( bNeedSubset);
420 0 : m_pSubsetLB->Show( bNeedSubset);
421 :
422 0 : return 0;
423 : }
424 :
425 : // -----------------------------------------------------------------------
426 :
427 0 : IMPL_LINK_NOARG(SvxCharacterMap, SubsetSelectHdl)
428 : {
429 0 : sal_uInt16 nPos = m_pSubsetLB->GetSelectEntryPos();
430 0 : const Subset* pSubset = reinterpret_cast<const Subset*> (m_pSubsetLB->GetEntryData(nPos));
431 0 : if( pSubset )
432 : {
433 0 : sal_UCS4 cFirst = pSubset->GetRangeMin();
434 0 : m_pShowSet->SelectCharacter( cFirst );
435 : }
436 0 : m_pSubsetLB->SelectEntryPos( nPos );
437 0 : return 0;
438 : }
439 :
440 : // -----------------------------------------------------------------------
441 :
442 0 : IMPL_LINK_NOARG(SvxCharacterMap, CharDoubleClickHdl)
443 : {
444 0 : EndDialog( sal_True );
445 0 : return 0;
446 : }
447 :
448 : // -----------------------------------------------------------------------
449 :
450 0 : IMPL_LINK_NOARG(SvxCharacterMap, CharSelectHdl)
451 : {
452 0 : if ( !bOne )
453 : {
454 0 : String aText = m_pShowText->GetText();
455 :
456 0 : if ( aText.Len() != CHARMAP_MAXLEN )
457 : {
458 0 : sal_UCS4 cChar = m_pShowSet->GetSelectCharacter();
459 : // using the new UCS4 constructor
460 0 : rtl::OUString aOUStr( &cChar, 1 );
461 0 : m_pShowText->SetText( aText + aOUStr );
462 0 : }
463 :
464 : }
465 0 : m_pOKBtn->Enable();
466 0 : return 0;
467 : }
468 :
469 : // -----------------------------------------------------------------------
470 :
471 0 : IMPL_LINK_NOARG(SvxCharacterMap, CharHighlightHdl)
472 : {
473 0 : String aText;
474 0 : sal_UCS4 cChar = m_pShowSet->GetSelectCharacter();
475 0 : sal_Bool bSelect = (cChar > 0);
476 :
477 : // show char sample
478 0 : if ( bSelect )
479 : {
480 : // using the new UCS4 constructor
481 0 : aText = rtl::OUString( &cChar, 1 );
482 :
483 0 : const Subset* pSubset = NULL;
484 0 : if( pSubsetMap )
485 0 : pSubset = pSubsetMap->GetSubsetByUnicode( cChar );
486 0 : if( pSubset )
487 0 : m_pSubsetLB->SelectEntry( pSubset->GetName() );
488 : else
489 0 : m_pSubsetLB->SetNoSelection();
490 : }
491 0 : m_pShowChar->SetText( aText );
492 0 : m_pShowChar->Update();
493 :
494 : // show char code
495 0 : if ( bSelect )
496 : {
497 : char aBuf[32];
498 0 : snprintf( aBuf, sizeof(aBuf), "U+%04X", static_cast<unsigned>(cChar) );
499 0 : if( cChar < 0x0100 )
500 0 : snprintf( aBuf+6, sizeof(aBuf)-6, " (%u)", static_cast<unsigned>(cChar) );
501 0 : aText = rtl::OUString::createFromAscii(aBuf);
502 : }
503 0 : m_pCharCodeText->SetText( aText );
504 :
505 0 : return 0;
506 : }
507 :
508 : // -----------------------------------------------------------------------
509 :
510 0 : IMPL_LINK_NOARG(SvxCharacterMap, CharPreSelectHdl)
511 : {
512 : // adjust subset selection
513 0 : if( pSubsetMap )
514 : {
515 0 : sal_UCS4 cChar = m_pShowSet->GetSelectCharacter();
516 0 : const Subset* pSubset = pSubsetMap->GetSubsetByUnicode( cChar );
517 0 : if( pSubset )
518 0 : m_pSubsetLB->SelectEntry( pSubset->GetName() );
519 : }
520 :
521 0 : m_pOKBtn->Enable();
522 0 : return 0;
523 : }
524 :
525 : // -----------------------------------------------------------------------
526 :
527 0 : IMPL_LINK_NOARG(SvxCharacterMap, DeleteHdl)
528 : {
529 0 : m_pShowText->SetText( String() );
530 0 : m_pOKBtn->Disable();
531 0 : return 0;
532 3 : }
533 :
534 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|