xref: /aoo41x/main/svx/source/dialog/charmap.cxx (revision 9b8096d0)
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_svx.hxx"
26 
27 // include ---------------------------------------------------------------
28 
29 #include <stdio.h>
30 
31 #define _SVX_CHARMAP_CXX_
32 #include <vcl/svapp.hxx>
33 #include <svtools/colorcfg.hxx>
34 
35 #include <rtl/textenc.h>
36 #include <svx/ucsubset.hxx>
37 
38 #include <svx/dialogs.hrc>
39 
40 #include <svx/charmap.hxx>
41 #include <svx/dialmgr.hxx>
42 #include <svx/svxdlg.hxx>
43 
44 #include "charmapacc.hxx"
45 #include <com/sun/star/accessibility/AccessibleEventObject.hpp>
46 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
47 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
48 #include <comphelper/types.hxx>
49 #include <svl/itemset.hxx>
50 
51 #include "rtl/ustrbuf.hxx"
52 
53 using namespace ::com::sun::star::accessibility;
54 using namespace ::com::sun::star::uno;
55 using namespace ::com::sun::star;
56 
57 // -----------------------------------------------------------------------
getSelectedChar()58 sal_uInt32& SvxShowCharSet::getSelectedChar()
59 {
60     static sal_uInt32 cSelectedChar = ' '; // keeps selected character over app livetime
61     return cSelectedChar;
62 }
63 
64 // class SvxShowCharSet ==================================================
65 
66 #define SBWIDTH 16
67 
SvxShowCharSet(Window * pParent,const ResId & rResId)68 SvxShowCharSet::SvxShowCharSet( Window* pParent, const ResId& rResId ) :
69     Control( pParent, rResId )
70 	,m_pAccessible(NULL)
71     ,aVscrollSB( this, WB_VERT)
72 {
73     nSelectedIndex = -1;    // TODO: move into init list when it is no longer static
74 
75     aOrigSize = GetOutputSizePixel();
76     aOrigPos = GetPosPixel();
77 
78     SetStyle( GetStyle() | WB_CLIPCHILDREN );
79     aVscrollSB.SetScrollHdl( LINK( this, SvxShowCharSet, VscrollHdl ) );
80     aVscrollSB.EnableDrag( sal_True );
81     // other settings like aVscroll depend on selected font => see SetFont
82 
83     bDrag = sal_False;
84     InitSettings( sal_True, sal_True );
85 }
86 
87 // -----------------------------------------------------------------------
88 
GetFocus()89 void SvxShowCharSet::GetFocus()
90 {
91     Control::GetFocus();
92     SelectIndex( nSelectedIndex, sal_True );
93 }
94 
95 // -----------------------------------------------------------------------
96 
LoseFocus()97 void SvxShowCharSet::LoseFocus()
98 {
99     Control::LoseFocus();
100     SelectIndex( nSelectedIndex, sal_False );
101 }
102 
103 // -----------------------------------------------------------------------
104 
StateChanged(StateChangedType nType)105 void SvxShowCharSet::StateChanged( StateChangedType nType )
106 {
107     if ( nType == STATE_CHANGE_CONTROLFOREGROUND )
108         InitSettings( sal_True, sal_False );
109     else if ( nType == STATE_CHANGE_CONTROLBACKGROUND )
110         InitSettings( sal_False, sal_True );
111 
112     Control::StateChanged( nType );
113 }
114 
115 // -----------------------------------------------------------------------
116 
DataChanged(const DataChangedEvent & rDCEvt)117 void SvxShowCharSet::DataChanged( const DataChangedEvent& rDCEvt )
118 {
119     if ( ( rDCEvt.GetType() == DATACHANGED_SETTINGS )
120       && ( rDCEvt.GetFlags() & SETTINGS_STYLE ) )
121         InitSettings( sal_True, sal_True );
122     else
123         Control::DataChanged( rDCEvt );
124 }
125 
126 // -----------------------------------------------------------------------
127 
MouseButtonDown(const MouseEvent & rMEvt)128 void SvxShowCharSet::MouseButtonDown( const MouseEvent& rMEvt )
129 {
130     if ( rMEvt.IsLeft() )
131     {
132         if ( rMEvt.GetClicks() == 1 )
133         {
134             GrabFocus();
135             bDrag = sal_True;
136             CaptureMouse();
137 
138             int nIndex = PixelToMapIndex( rMEvt.GetPosPixel() );
139 	    // Fire the focus event
140             SelectIndex( nIndex , sal_True);
141         }
142 
143         if ( !(rMEvt.GetClicks() % 2) )
144             aDoubleClkHdl.Call( this );
145     }
146 }
147 
148 // -----------------------------------------------------------------------
149 
MouseButtonUp(const MouseEvent & rMEvt)150 void SvxShowCharSet::MouseButtonUp( const MouseEvent& rMEvt )
151 {
152     if ( bDrag && rMEvt.IsLeft() )
153     {
154         // released mouse over character map
155         if ( Rectangle(Point(), GetOutputSize()).IsInside(rMEvt.GetPosPixel()))
156             aSelectHdl.Call( this );
157         ReleaseMouse();
158         bDrag = sal_False;
159     }
160 }
161 
162 // -----------------------------------------------------------------------
163 
MouseMove(const MouseEvent & rMEvt)164 void SvxShowCharSet::MouseMove( const MouseEvent& rMEvt )
165 {
166     if ( rMEvt.IsLeft() && bDrag )
167     {
168         Point aPos  = rMEvt.GetPosPixel();
169         Size  aSize = GetSizePixel();
170 
171         if ( aPos.X() < 0 )
172             aPos.X() = 0;
173         else if ( aPos.X() > aSize.Width()-5 )
174             aPos.X() = aSize.Width()-5;
175         if ( aPos.Y() < 0 )
176             aPos.Y() = 0;
177         else if ( aPos.Y() > aSize.Height()-5 )
178             aPos.Y() = aSize.Height()-5;
179 
180         int nIndex = PixelToMapIndex( aPos );
181 	// Fire the focus event.
182         SelectIndex( nIndex , sal_True );
183     }
184 }
185 
186 // -----------------------------------------------------------------------
187 
Command(const CommandEvent & rCEvt)188 void SvxShowCharSet::Command( const CommandEvent& rCEvt )
189 {
190     if( !HandleScrollCommand( rCEvt, 0, &aVscrollSB ) )
191         Control::Command( rCEvt );
192 }
193 
194 // -----------------------------------------------------------------------------
195 
GetRowPos(sal_uInt16 _nPos) const196 sal_uInt16 SvxShowCharSet::GetRowPos(sal_uInt16 _nPos) const
197 {
198     return _nPos / COLUMN_COUNT ;
199 }
200 
201 // -----------------------------------------------------------------------------
202 
GetColumnPos(sal_uInt16 _nPos) const203 sal_uInt16 SvxShowCharSet::GetColumnPos(sal_uInt16 _nPos) const
204 {
205     return _nPos % COLUMN_COUNT ;
206 }
207 
208 // -----------------------------------------------------------------------
209 
FirstInView(void) const210 int SvxShowCharSet::FirstInView( void ) const
211 {
212     int nIndex = 0;
213     if( aVscrollSB.IsVisible() )
214         nIndex += aVscrollSB.GetThumbPos() * COLUMN_COUNT;
215     return nIndex;
216 }
217 
218 // -----------------------------------------------------------------------
219 
LastInView(void) const220 int SvxShowCharSet::LastInView( void ) const
221 {
222     sal_uIntPtr nIndex = FirstInView();
223     nIndex += ROW_COUNT * COLUMN_COUNT - 1;
224 	sal_uIntPtr nCompare = sal::static_int_cast<sal_uIntPtr>( maFontCharMap.GetCharCount() - 1 );
225     if( nIndex > nCompare )
226         nIndex = nCompare;
227     return nIndex;
228 }
229 
230 // -----------------------------------------------------------------------
231 
MapIndexToPixel(int nIndex) const232 inline Point SvxShowCharSet::MapIndexToPixel( int nIndex ) const
233 {
234     const int nBase = FirstInView();
235     int x = ((nIndex - nBase) % COLUMN_COUNT) * nX;
236     int y = ((nIndex - nBase) / COLUMN_COUNT) * nY;
237     return Point( x, y );
238 }
239 // -----------------------------------------------------------------------------
240 
PixelToMapIndex(const Point & point) const241 int SvxShowCharSet::PixelToMapIndex( const Point& point) const
242 {
243     int nBase = FirstInView();
244     return (nBase + (point.X()/nX) + (point.Y()/nY) * COLUMN_COUNT);
245 }
246 
247 // -----------------------------------------------------------------------
248 
KeyInput(const KeyEvent & rKEvt)249 void SvxShowCharSet::KeyInput( const KeyEvent& rKEvt )
250 {
251     KeyCode aCode = rKEvt.GetKeyCode();
252 
253     if( aCode.GetModifier() )
254     {
255         Control::KeyInput( rKEvt );
256         return;
257     }
258 
259     int tmpSelected = nSelectedIndex;
260 
261     switch ( aCode.GetCode() )
262     {
263         case KEY_SPACE:
264             aSelectHdl.Call( this );
265             break;
266         case KEY_LEFT:
267             --tmpSelected;
268             break;
269         case KEY_RIGHT:
270             ++tmpSelected;
271             break;
272         case KEY_UP:
273             tmpSelected -= COLUMN_COUNT;
274             break;
275         case KEY_DOWN:
276             tmpSelected += COLUMN_COUNT;
277             break;
278         case KEY_PAGEUP:
279             tmpSelected -= ROW_COUNT * COLUMN_COUNT;
280             break;
281         case KEY_PAGEDOWN:
282             tmpSelected += ROW_COUNT * COLUMN_COUNT;
283             break;
284         case KEY_HOME:
285             tmpSelected = 0;
286             break;
287         case KEY_END:
288             tmpSelected = maFontCharMap.GetCharCount() - 1;
289             break;
290         case KEY_TAB:   // some fonts have a character at these unicode control codes
291         case KEY_ESCAPE:
292         case KEY_RETURN:
293             Control::KeyInput( rKEvt );
294             tmpSelected = - 1;  // mark as invalid
295             break;
296         default:
297             {
298                 sal_UCS4 cChar = rKEvt.GetCharCode();
299                 sal_UCS4 cNext = maFontCharMap.GetNextChar( cChar - 1 );
300                 tmpSelected = maFontCharMap.GetIndexFromChar( cNext );
301                 if( tmpSelected < 0 || (cChar != cNext) )
302                 {
303                     Control::KeyInput( rKEvt );
304                     tmpSelected = - 1;  // mark as invalid
305                 }
306             }
307     }
308 
309     if ( tmpSelected >= 0 )
310     {
311         SelectIndex( tmpSelected, sal_True );
312         aPreSelectHdl.Call( this );
313     }
314 }
315 
316 // -----------------------------------------------------------------------
317 
Paint(const Rectangle &)318 void SvxShowCharSet::Paint( const Rectangle& )
319 {
320     DrawChars_Impl( FirstInView(), LastInView() );
321 }
322 // -----------------------------------------------------------------------------
DeSelect()323 void SvxShowCharSet::DeSelect()
324 {
325 	DrawChars_Impl(nSelectedIndex,nSelectedIndex);
326 }
327 // -----------------------------------------------------------------------
328 
DrawChars_Impl(int n1,int n2)329 void SvxShowCharSet::DrawChars_Impl( int n1, int n2 )
330 {
331     if( n1 > LastInView() || n2 < FirstInView() )
332         return;
333 
334     Size aOutputSize = GetOutputSizePixel();
335     if( aVscrollSB.IsVisible() )
336         aOutputSize.setWidth( aOutputSize.Width() - SBWIDTH );
337 
338     int i;
339     for ( i = 1; i < COLUMN_COUNT; ++i )
340         DrawLine( Point( nX * i, 0 ), Point( nX * i, aOutputSize.Height() ) );
341     for ( i = 1; i < ROW_COUNT; ++i )
342         DrawLine( Point( 0, nY * i ), Point( aOutputSize.Width(), nY * i ) );
343 
344     const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
345 	const Color aWindowTextColor( rStyleSettings.GetFieldTextColor() );
346 	Color aHighlightColor( rStyleSettings.GetHighlightColor() );
347 	Color aHighlightTextColor( rStyleSettings.GetHighlightTextColor() );
348 	Color aFaceColor( rStyleSettings.GetFaceColor() );
349 	Color aLightColor( rStyleSettings.GetLightColor() );
350 	Color aShadowColor( rStyleSettings.GetShadowColor() );
351 
352     int nTextHeight = GetTextHeight();
353     Rectangle aBoundRect;
354     for( i = n1; i <= n2; ++i )
355     {
356         Point pix = MapIndexToPixel( i );
357         int x = pix.X();
358         int y = pix.Y();
359 
360         rtl::OUStringBuffer buf;
361         buf.appendUtf32( maFontCharMap.GetCharFromIndex( i ) );
362         String aCharStr(buf.makeStringAndClear());
363         int nTextWidth = GetTextWidth(aCharStr);
364         int tx = x + (nX - nTextWidth + 1) / 2;
365         int ty = y + (nY - nTextHeight + 1) / 2;
366         Point aPointTxTy( tx, ty );
367 
368         // adjust position before it gets out of bounds
369         if( GetTextBoundRect( aBoundRect, aCharStr ) && !aBoundRect.IsEmpty() )
370         {
371             // zero advance width => use ink width to center glyph
372             if( !nTextWidth )
373             {
374                 aPointTxTy.X() = x - aBoundRect.Left()
375                                + (nX - aBoundRect.GetWidth() + 1) / 2;
376             }
377 
378             aBoundRect += aPointTxTy;
379 
380             // shift back vertically if needed
381             int nYLDelta = aBoundRect.Top() - y;
382             int nYHDelta = (y + nY) - aBoundRect.Bottom();
383             if( nYLDelta <= 0 )
384                 aPointTxTy.Y() -= nYLDelta - 1;
385             else if( nYHDelta <= 0 )
386                 aPointTxTy.Y() += nYHDelta - 1;
387 
388             // shift back horizontally if needed
389             int nXLDelta = aBoundRect.Left() - x;
390             int nXHDelta = (x + nX) - aBoundRect.Right();
391             if( nXLDelta <= 0 )
392                 aPointTxTy.X() -= nXLDelta - 1;
393             else if( nXHDelta <= 0 )
394                 aPointTxTy.X() += nXHDelta - 1;
395         }
396 
397         Color aTextCol = GetTextColor();
398         if ( i != nSelectedIndex )
399         {
400             SetTextColor( aWindowTextColor );
401             DrawText( aPointTxTy, aCharStr );
402         }
403         else
404         {
405             Color aLineCol = GetLineColor();
406             Color aFillCol = GetFillColor();
407             SetLineColor();
408             Point aPointUL( x + 1, y + 1 );
409             if( HasFocus() )
410             {
411                 SetFillColor( aHighlightColor );
412                 DrawRect( Rectangle( aPointUL, Size(nX-1,nY-1) ) );
413 
414                 SetTextColor( aHighlightTextColor );
415                 DrawText( aPointTxTy, aCharStr );
416             }
417             else
418             {
419                 SetFillColor( aFaceColor );
420                 DrawRect( Rectangle( aPointUL, Size( nX-1, nY-1) ) );
421 
422                 SetLineColor( aLightColor );
423                 DrawLine( aPointUL, Point( x+nX-1, y+1) );
424                 DrawLine( aPointUL, Point( x+1, y+nY-1) );
425 
426                 SetLineColor( aShadowColor );
427                 DrawLine( Point( x+1, y+nY-1), Point( x+nX-1, y+nY-1) );
428                 DrawLine( Point( x+nX-1, y+nY-1), Point( x+nX-1, y+1) );
429 
430                 DrawText( aPointTxTy, aCharStr );
431             }
432             SetLineColor( aLineCol );
433             SetFillColor( aFillCol );
434         }
435         SetTextColor( aTextCol );
436     }
437 }
438 
439 // -----------------------------------------------------------------------
440 
InitSettings(sal_Bool bForeground,sal_Bool bBackground)441 void SvxShowCharSet::InitSettings( sal_Bool bForeground, sal_Bool bBackground )
442 {
443     const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
444 
445     if ( bForeground )
446     {
447         Color aTextColor( rStyleSettings.GetDialogTextColor() );
448 
449         if ( IsControlForeground() )
450             aTextColor = GetControlForeground();
451         SetTextColor( aTextColor );
452     }
453 
454     if ( bBackground )
455     {
456         if ( IsControlBackground() )
457             SetBackground( GetControlBackground() );
458         else
459             SetBackground( rStyleSettings.GetWindowColor() );
460     }
461 
462     Invalidate();
463 }
464 
465 // -----------------------------------------------------------------------
466 
GetSelectCharacter() const467 sal_UCS4 SvxShowCharSet::GetSelectCharacter() const
468 {
469     if( nSelectedIndex >= 0 )
470         getSelectedChar() = maFontCharMap.GetCharFromIndex( nSelectedIndex );
471     return getSelectedChar();
472 }
473 
474 // -----------------------------------------------------------------------
475 
SetFont(const Font & rFont)476 void SvxShowCharSet::SetFont( const Font& rFont )
477 {
478     // save last selected unicode
479     if( nSelectedIndex >= 0 )
480         getSelectedChar() = maFontCharMap.GetCharFromIndex( nSelectedIndex );
481 
482     Font aFont = rFont;
483     aFont.SetWeight( WEIGHT_LIGHT );
484     aFont.SetAlign( ALIGN_TOP );
485     int nFontHeight = (aOrigSize.Height() - 5) * 2 / (3 * ROW_COUNT);
486     aFont.SetSize( PixelToLogic( Size( 0, nFontHeight ) ) );
487     aFont.SetTransparent( sal_True );
488     Control::SetFont( aFont );
489     GetFontCharMap( maFontCharMap );
490 
491     // hide scrollbar when there is nothing to scroll
492     sal_Bool bNeedVscroll = (maFontCharMap.GetCharCount() > ROW_COUNT*COLUMN_COUNT);
493 
494     nX = (aOrigSize.Width() - (bNeedVscroll ? SBWIDTH : 0)) / COLUMN_COUNT;
495     nY = aOrigSize.Height() / ROW_COUNT;
496 
497     if( bNeedVscroll)
498     {
499         aVscrollSB.SetPosSizePixel( nX * COLUMN_COUNT, 0, SBWIDTH, nY * ROW_COUNT );
500         aVscrollSB.SetRangeMin( 0 );
501         int nLastRow = (maFontCharMap.GetCharCount() - 1 + COLUMN_COUNT) / COLUMN_COUNT;
502         aVscrollSB.SetRangeMax( nLastRow );
503         aVscrollSB.SetPageSize( ROW_COUNT-1 );
504         aVscrollSB.SetVisibleSize( ROW_COUNT );
505     }
506 
507     // restore last selected unicode
508     int nMapIndex = maFontCharMap.GetIndexFromChar( getSelectedChar() );
509     SelectIndex( nMapIndex );
510 
511     // rearrange CharSet element in sync with nX- and nY-multiples
512     Size aNewSize( nX * COLUMN_COUNT + (bNeedVscroll ? SBWIDTH : 0), nY * ROW_COUNT );
513     Point aNewPos = aOrigPos + Point( (aOrigSize.Width() - aNewSize.Width()) / 2, 0 );
514     SetPosPixel( aNewPos );
515     SetOutputSizePixel( aNewSize );
516 
517     aVscrollSB.Show( bNeedVscroll );
518     Invalidate();
519 }
520 
521 // -----------------------------------------------------------------------
522 
SelectIndex(int nNewIndex,sal_Bool bFocus)523 void SvxShowCharSet::SelectIndex( int nNewIndex, sal_Bool bFocus )
524 {
525     if( nNewIndex < 0 )
526     {
527         // need to scroll see closest unicode
528         sal_uInt32 cPrev = maFontCharMap.GetPrevChar( getSelectedChar() );
529         int nMapIndex = maFontCharMap.GetIndexFromChar( cPrev );
530         int nNewPos = nMapIndex / COLUMN_COUNT;
531         aVscrollSB.SetThumbPos( nNewPos );
532         nSelectedIndex = bFocus ? nMapIndex+1 : -1;
533         Invalidate();
534         Update();
535     }
536     else if( nNewIndex < FirstInView() )
537     {
538         // need to scroll up to see selected item
539         int nOldPos = aVscrollSB.GetThumbPos();
540         int nDelta = (FirstInView() - nNewIndex + COLUMN_COUNT-1) / COLUMN_COUNT;
541         aVscrollSB.SetThumbPos( nOldPos - nDelta );
542         nSelectedIndex = nNewIndex;
543         Invalidate();
544         if( nDelta )
545             Update();
546     }
547     else if( nNewIndex > LastInView() )
548     {
549         // need to scroll down to see selected item
550         int nOldPos = aVscrollSB.GetThumbPos();
551         int nDelta = (nNewIndex - LastInView() + COLUMN_COUNT) / COLUMN_COUNT;
552         aVscrollSB.SetThumbPos( nOldPos + nDelta );
553         if( nNewIndex < maFontCharMap.GetCharCount() )
554         {
555             nSelectedIndex = nNewIndex;
556             Invalidate();
557         }
558         if( nOldPos != aVscrollSB.GetThumbPos() )
559         {
560             Invalidate();
561             Update();
562         }
563     }
564     else
565     {
566         // remove highlighted view
567         Color aLineCol = GetLineColor();
568         Color aFillCol = GetFillColor();
569         SetLineColor();
570         SetFillColor( GetBackground().GetColor() );
571 
572         Point aOldPixel = MapIndexToPixel( nSelectedIndex );
573         aOldPixel.Move( +1, +1);
574         DrawRect( Rectangle( aOldPixel, Size( nX-1, nY-1 ) ) );
575         SetLineColor( aLineCol );
576         SetFillColor( aFillCol );
577 
578         int nOldIndex = nSelectedIndex;
579         nSelectedIndex = nNewIndex;
580         DrawChars_Impl( nOldIndex, nOldIndex );
581         DrawChars_Impl( nNewIndex, nNewIndex );
582     }
583 
584     if( nSelectedIndex >= 0 )
585 	{
586 	    getSelectedChar() = maFontCharMap.GetCharFromIndex( nSelectedIndex );
587 		if( m_pAccessible )
588 		{
589 			::svx::SvxShowCharSetItem* pItem = ImplGetItem(nSelectedIndex);
590 			// Don't fire the focus event.
591 			if ( bFocus )
592 				m_pAccessible->fireEvent( AccessibleEventId::ACTIVE_DESCENDANT_CHANGED, Any(), makeAny(pItem->GetAccessible()) ); // this call asures that m_pItem is set
593 			else
594 				m_pAccessible->fireEvent( AccessibleEventId::ACTIVE_DESCENDANT_CHANGED_NOFOCUS, Any(), makeAny(pItem->GetAccessible()) ); // this call asures that m_pItem is set
595 
596 			OSL_ENSURE(pItem->m_pItem,"No accessible created!");
597 			Any aOldAny, aNewAny;
598 			aNewAny <<= AccessibleStateType::FOCUSED;
599 			// Don't fire the focus event.
600 			if ( bFocus )
601 				pItem->m_pItem->fireEvent( AccessibleEventId::STATE_CHANGED, aOldAny, aNewAny );
602 
603 			aNewAny <<= AccessibleStateType::SELECTED;
604 			pItem->m_pItem->fireEvent( AccessibleEventId::STATE_CHANGED, aOldAny, aNewAny );
605 		}
606 	}
607 
608 
609     aHighHdl.Call( this );
610 }
611 
612 // -----------------------------------------------------------------------
613 
SelectCharacter(sal_UCS4 cNew,sal_Bool bFocus)614 void SvxShowCharSet::SelectCharacter( sal_UCS4 cNew, sal_Bool bFocus )
615 {
616     // get next available char of current font
617     sal_UCS4 cNext = maFontCharMap.GetNextChar( cNew - 1 );
618 
619     int nMapIndex = maFontCharMap.GetIndexFromChar( cNext );
620     SelectIndex( nMapIndex, bFocus );
621     if( !bFocus )
622     {
623         // move selected item to top row if not in focus
624         aVscrollSB.SetThumbPos( nMapIndex / COLUMN_COUNT );
625         Invalidate();
626     }
627 }
628 
629 // -----------------------------------------------------------------------
630 
IMPL_LINK(SvxShowCharSet,VscrollHdl,ScrollBar *,EMPTYARG)631 IMPL_LINK( SvxShowCharSet, VscrollHdl, ScrollBar *, EMPTYARG )
632 {
633     if( nSelectedIndex < FirstInView() )
634 	{
635         SelectIndex( FirstInView() + (nSelectedIndex % COLUMN_COUNT) );
636 	}
637     else if( nSelectedIndex > LastInView() )
638 	{
639 		if( m_pAccessible )
640 		{
641 			::com::sun::star::uno::Any aOldAny, aNewAny;
642 			int nLast = LastInView();
643 			for ( ; nLast != nSelectedIndex; ++nLast)
644 			{
645 				aOldAny <<= ImplGetItem(nLast)->GetAccessible();
646 				m_pAccessible ->fireEvent( AccessibleEventId::CHILD, aOldAny, aNewAny );
647 			}
648 		}
649         SelectIndex( (LastInView() - COLUMN_COUNT + 1) + (nSelectedIndex % COLUMN_COUNT) );
650 	}
651 
652     Invalidate();
653     return 0;
654 }
655 
656 // -----------------------------------------------------------------------
657 
~SvxShowCharSet()658 SvxShowCharSet::~SvxShowCharSet()
659 {
660 	if ( m_pAccessible )
661 		ReleaseAccessible();
662 }
663 // -----------------------------------------------------------------------------
ReleaseAccessible()664 void SvxShowCharSet::ReleaseAccessible()
665 {
666 	m_aItems.clear();
667 	m_pAccessible = NULL;
668 	m_xAccessible = NULL;
669 }
670 // -----------------------------------------------------------------------------
CreateAccessible()671 ::com::sun::star::uno::Reference< XAccessible > SvxShowCharSet::CreateAccessible()
672 {
673 	OSL_ENSURE(!m_pAccessible,"Accessible already created!");
674 	m_pAccessible = new ::svx::SvxShowCharSetVirtualAcc(this);
675 	m_xAccessible = m_pAccessible;
676 	return m_xAccessible;
677 }
678 // -----------------------------------------------------------------------------
ImplGetItem(int _nPos)679 ::svx::SvxShowCharSetItem* SvxShowCharSet::ImplGetItem( int _nPos )
680 {
681 	ItemsMap::iterator aFind = m_aItems.find(_nPos);
682 	if ( aFind == m_aItems.end() )
683 	{
684 		OSL_ENSURE(m_pAccessible,"Who wants to create a child of my table without a parent?");
685 		aFind = m_aItems.insert(ItemsMap::value_type(_nPos,new ::svx::SvxShowCharSetItem(*this,m_pAccessible->getTable(),sal::static_int_cast< sal_uInt16 >(_nPos)))).first;
686         rtl::OUStringBuffer buf;
687         buf.appendUtf32( maFontCharMap.GetCharFromIndex( _nPos ) );
688 		aFind->second->maText = buf.makeStringAndClear();
689 		Point pix = MapIndexToPixel( _nPos );
690 		aFind->second->maRect = Rectangle( Point( pix.X() + 1, pix.Y() + 1 ), Size(nX-1,nY-1) );
691 	}
692 
693 	return aFind->second;
694 }
695 // -----------------------------------------------------------------------------
ImplFireAccessibleEvent(short nEventId,const::com::sun::star::uno::Any & rOldValue,const::com::sun::star::uno::Any & rNewValue)696 void SvxShowCharSet::ImplFireAccessibleEvent( short nEventId, const ::com::sun::star::uno::Any& rOldValue, const ::com::sun::star::uno::Any& rNewValue )
697 {
698     if( m_pAccessible )
699         m_pAccessible->fireEvent( nEventId, rOldValue, rNewValue );
700 }
701 // -----------------------------------------------------------------------------
getScrollBar()702 ScrollBar* SvxShowCharSet::getScrollBar()
703 {
704 	return &aVscrollSB;
705 }
706 // -----------------------------------------------------------------------
getMaxCharCount() const707 sal_Int32 SvxShowCharSet::getMaxCharCount() const
708 {
709     return maFontCharMap.GetCharCount();
710 }
711 
712 
713 // class SubsetMap =======================================================
714 // TODO: should be moved into Font Attributes stuff
715 // we let it mature here though because it is currently the only use
716 
SubsetMap(const FontCharMap * pFontCharMap)717 SubsetMap::SubsetMap( const FontCharMap* pFontCharMap )
718 :   Resource( SVX_RES(RID_SUBSETMAP) )
719 {
720     InitList();
721     ApplyCharMap( pFontCharMap );
722     FreeResource();
723 }
724 
GetNextSubset(bool bFirst) const725 const Subset* SubsetMap::GetNextSubset( bool bFirst ) const
726 {
727     if( bFirst )
728         maSubsetIterator = maSubsets.begin();
729     if( maSubsetIterator == maSubsets.end() )
730         return NULL;
731     const Subset* s = &*(maSubsetIterator++);
732     return s;
733 }
734 
GetSubsetByUnicode(sal_UCS4 cChar) const735 const Subset* SubsetMap::GetSubsetByUnicode( sal_UCS4 cChar ) const
736 {
737     // TODO: is it worth to avoid a linear search?
738     for( const Subset* s = GetNextSubset( true ); s; s = GetNextSubset( false ) )
739         if( (s->GetRangeMin() <= cChar) && (cChar <= s->GetRangeMax()) )
740             return s;
741     return NULL;
742 }
743 
Subset(sal_UCS4 nMin,sal_UCS4 nMax,int resId)744 inline Subset::Subset( sal_UCS4 nMin, sal_UCS4 nMax, int resId)
745 :   mnRangeMin(nMin), mnRangeMax(nMax), maRangeName( SVX_RES(resId) )
746 {}
747 
InitList()748 void SubsetMap::InitList()
749 {
750     static SubsetList aAllSubsets;
751     static bool bInit = true;
752     if( bInit )
753     {
754         bInit = false;
755 
756         // TODO: eventually merge or split unicode subranges
757         //       a "native writer" should decide for his subsets
758         aAllSubsets.push_back( Subset( 0x0020, 0x007F, RID_SUBSETSTR_BASIC_LATIN ) );
759         aAllSubsets.push_back( Subset( 0x0080, 0x00FF, RID_SUBSETSTR_LATIN_1 ) );
760         aAllSubsets.push_back( Subset( 0x0100, 0x017F, RID_SUBSETSTR_LATIN_EXTENDED_A ) );
761         aAllSubsets.push_back( Subset( 0x0180, 0x024F, RID_SUBSETSTR_LATIN_EXTENDED_B ) );
762         aAllSubsets.push_back( Subset( 0x0250, 0x02AF, RID_SUBSETSTR_IPA_EXTENSIONS ) );
763         aAllSubsets.push_back( Subset( 0x02B0, 0x02FF, RID_SUBSETSTR_SPACING_MODIFIERS ) );
764         aAllSubsets.push_back( Subset( 0x0300, 0x036F, RID_SUBSETSTR_COMB_DIACRITICAL ) );
765         aAllSubsets.push_back( Subset( 0x0370, 0x03FF, RID_SUBSETSTR_BASIC_GREEK ) );
766     //  aAllSubsets.push_back( Subset( 0x03D0, 0x03F3, RID_SUBSETSTR_GREEK_SYMS_COPTIC ) );
767         aAllSubsets.push_back( Subset( 0x0400, 0x04FF, RID_SUBSETSTR_CYRILLIC ) );
768         aAllSubsets.push_back( Subset( 0x0530, 0x058F, RID_SUBSETSTR_ARMENIAN ) );
769         aAllSubsets.push_back( Subset( 0x0590, 0x05FF, RID_SUBSETSTR_BASIC_HEBREW ) );
770     //  aAllSubsets.push_back( Subset( 0x0591, 0x05C4, RID_SUBSETSTR_HEBREW_EXTENDED ) );
771         aAllSubsets.push_back( Subset( 0x0600, 0x065F, RID_SUBSETSTR_BASIC_ARABIC ) );
772         aAllSubsets.push_back( Subset( 0x0660, 0x06FF, RID_SUBSETSTR_ARABIC_EXTENDED ) );
773         aAllSubsets.push_back( Subset( 0x0700, 0x074F, RID_SUBSETSTR_SYRIAC ) );
774         aAllSubsets.push_back( Subset( 0x0780, 0x07BF, RID_SUBSETSTR_THAANA ) );
775         aAllSubsets.push_back( Subset( 0x0900, 0x097F, RID_SUBSETSTR_DEVANAGARI ) );
776         aAllSubsets.push_back( Subset( 0x0980, 0x09FF, RID_SUBSETSTR_BENGALI ) );
777         aAllSubsets.push_back( Subset( 0x0A00, 0x0A7F, RID_SUBSETSTR_GURMUKHI ) );
778         aAllSubsets.push_back( Subset( 0x0A80, 0x0AFF, RID_SUBSETSTR_GUJARATI ) );
779         aAllSubsets.push_back( Subset( 0x0B00, 0x0B7F, RID_SUBSETSTR_ORIYA ) );
780         aAllSubsets.push_back( Subset( 0x0B80, 0x0BFF, RID_SUBSETSTR_TAMIL ) );
781         aAllSubsets.push_back( Subset( 0x0C00, 0x0C7F, RID_SUBSETSTR_TELUGU ) );
782         aAllSubsets.push_back( Subset( 0x0C80, 0x0CFF, RID_SUBSETSTR_KANNADA ) );
783         aAllSubsets.push_back( Subset( 0x0D00, 0x0D7F, RID_SUBSETSTR_MALAYALAM ) );
784         aAllSubsets.push_back( Subset( 0x0D80, 0x0DFF, RID_SUBSETSTR_SINHALA ) );
785         aAllSubsets.push_back( Subset( 0x0E00, 0x0E7F, RID_SUBSETSTR_THAI ) );
786         aAllSubsets.push_back( Subset( 0x0E80, 0x0EFF, RID_SUBSETSTR_LAO ) );
787         aAllSubsets.push_back( Subset( 0x0F00, 0x0FBF, RID_SUBSETSTR_TIBETAN ) );
788         aAllSubsets.push_back( Subset( 0x1000, 0x109F, RID_SUBSETSTR_MYANMAR ) );
789         aAllSubsets.push_back( Subset( 0x10A0, 0x10FF, RID_SUBSETSTR_BASIC_GEORGIAN ) );
790     //  aAllSubsets.push_back( Subset( 0x10A0, 0x10C5, RID_SUBSETSTR_GEORGIAN_EXTENDED ) );
791         aAllSubsets.push_back( Subset( 0x1100, 0x11FF, RID_SUBSETSTR_HANGUL_JAMO ) );
792         aAllSubsets.push_back( Subset( 0x1200, 0x137F, RID_SUBSETSTR_ETHIOPIC ) );
793         aAllSubsets.push_back( Subset( 0x13A0, 0x13FF, RID_SUBSETSTR_CHEROKEE ) );
794         aAllSubsets.push_back( Subset( 0x1400, 0x167F, RID_SUBSETSTR_CANADIAN_ABORIGINAL ) );
795         aAllSubsets.push_back( Subset( 0x1680, 0x169F, RID_SUBSETSTR_OGHAM ) );
796         aAllSubsets.push_back( Subset( 0x16A0, 0x16F0, RID_SUBSETSTR_RUNIC ) );
797         aAllSubsets.push_back( Subset( 0x1700, 0x171F, RID_SUBSETSTR_TAGALOG ) );
798         aAllSubsets.push_back( Subset( 0x1720, 0x173F, RID_SUBSETSTR_HANUNOO ) );
799         aAllSubsets.push_back( Subset( 0x1740, 0x175F, RID_SUBSETSTR_BUHID ) );
800         aAllSubsets.push_back( Subset( 0x1760, 0x177F, RID_SUBSETSTR_TAGBANWA ) );
801         aAllSubsets.push_back( Subset( 0x1780, 0x17FF, RID_SUBSETSTR_KHMER ) );
802         aAllSubsets.push_back( Subset( 0x1800, 0x18AF, RID_SUBSETSTR_MONGOLIAN ) );
803         aAllSubsets.push_back( Subset( 0x1E00, 0x1EFF, RID_SUBSETSTR_LATIN_EXTENDED_ADDS ) );
804         aAllSubsets.push_back( Subset( 0x1F00, 0x1FFF, RID_SUBSETSTR_GREEK_EXTENDED ) );
805 
806         aAllSubsets.push_back( Subset( 0x2000, 0x206F, RID_SUBSETSTR_GENERAL_PUNCTUATION ) );
807         aAllSubsets.push_back( Subset( 0x2070, 0x209F, RID_SUBSETSTR_SUB_SUPER_SCRIPTS ) );
808         aAllSubsets.push_back( Subset( 0x20A0, 0x20CF, RID_SUBSETSTR_CURRENCY_SYMBOLS ) );
809         aAllSubsets.push_back( Subset( 0x20D0, 0x20FF, RID_SUBSETSTR_COMB_DIACRITIC_SYMS ) );
810         aAllSubsets.push_back( Subset( 0x2100, 0x214F, RID_SUBSETSTR_LETTERLIKE_SYMBOLS ) );
811         aAllSubsets.push_back( Subset( 0x2150, 0x218F, RID_SUBSETSTR_NUMBER_FORMS ) );
812         aAllSubsets.push_back( Subset( 0x2190, 0x21FF, RID_SUBSETSTR_ARROWS ) );
813         aAllSubsets.push_back( Subset( 0x2200, 0x22FF, RID_SUBSETSTR_MATH_OPERATORS ) );
814         aAllSubsets.push_back( Subset( 0x2300, 0x23FF, RID_SUBSETSTR_MISC_TECHNICAL ) );
815         aAllSubsets.push_back( Subset( 0x2400, 0x243F, RID_SUBSETSTR_CONTROL_PICTURES ) );
816         aAllSubsets.push_back( Subset( 0x2440, 0x245F, RID_SUBSETSTR_OPTICAL_CHAR_REC ) );
817         aAllSubsets.push_back( Subset( 0x2460, 0x24FF, RID_SUBSETSTR_ENCLOSED_ALPHANUM ) );
818         aAllSubsets.push_back( Subset( 0x2500, 0x257F, RID_SUBSETSTR_BOX_DRAWING ) );
819         aAllSubsets.push_back( Subset( 0x2580, 0x259F, RID_SUBSETSTR_BLOCK_ELEMENTS ) );
820         aAllSubsets.push_back( Subset( 0x25A0, 0x25FF, RID_SUBSETSTR_GEOMETRIC_SHAPES ) );
821         aAllSubsets.push_back( Subset( 0x2600, 0x26FF, RID_SUBSETSTR_MISC_DINGBATS ) );
822         aAllSubsets.push_back( Subset( 0x2700, 0x27BF, RID_SUBSETSTR_DINGBATS ) );
823 
824         aAllSubsets.push_back( Subset( 0x27C0, 0x27FF, RID_SUBSETSTR_MISC_MATH_SYMS_A ) );
825         aAllSubsets.push_back( Subset( 0x27F0, 0x27FF, RID_SUBSETSTR_SUPPL_ARROWS_A ) );
826         aAllSubsets.push_back( Subset( 0x2800, 0x28FF, RID_SUBSETSTR_BRAILLE_PATTERNS ) );
827         aAllSubsets.push_back( Subset( 0x2900, 0x297F, RID_SUBSETSTR_SUPPL_ARROWS_B ) );
828         aAllSubsets.push_back( Subset( 0x2980, 0x29FF, RID_SUBSETSTR_MISC_MATH_SYMS_B ) );
829         aAllSubsets.push_back( Subset( 0x2E80, 0x2EFF, RID_SUBSETSTR_CJK_RADICAL_SUPPL ) );
830         aAllSubsets.push_back( Subset( 0x2F00, 0x2FDF, RID_SUBSETSTR_KANXI_RADICALS ) );
831         aAllSubsets.push_back( Subset( 0x2FF0, 0x2FFF, RID_SUBSETSTR_IDEO_DESC_CHARS ) );
832 
833         aAllSubsets.push_back( Subset( 0x3000, 0x303F, RID_SUBSETSTR_CJK_SYMS_PUNCTUATION ) );
834         aAllSubsets.push_back( Subset( 0x3040, 0x309F, RID_SUBSETSTR_HIRAGANA ) );
835         aAllSubsets.push_back( Subset( 0x30A0, 0x30FF, RID_SUBSETSTR_KATAKANA ) );
836         aAllSubsets.push_back( Subset( 0x3100, 0x312F, RID_SUBSETSTR_BOPOMOFO ) );
837         aAllSubsets.push_back( Subset( 0x3130, 0x318F, RID_SUBSETSTR_HANGUL_COMPAT_JAMO ) );
838         aAllSubsets.push_back( Subset( 0x3190, 0x319F, RID_SUBSETSTR_KANBUN ) );
839         aAllSubsets.push_back( Subset( 0x31A0, 0x31BF, RID_SUBSETSTR_BOPOMOFO_EXTENDED ) );
840         aAllSubsets.push_back( Subset( 0x31C0, 0x31FF, RID_SUBSETSTR_KATAKANA_PHONETIC ) );
841         aAllSubsets.push_back( Subset( 0x3200, 0x32FF, RID_SUBSETSTR_ENCLOSED_CJK_LETTERS ) );
842         aAllSubsets.push_back( Subset( 0x3300, 0x33FF, RID_SUBSETSTR_CJK_COMPATIBILITY ) );
843 
844         aAllSubsets.push_back( Subset( 0x3400, 0x4DFF, RID_SUBSETSTR_CJK_EXT_A_UNIFIED_IDGRAPH ) );
845         aAllSubsets.push_back( Subset( 0x4E00, 0x9FA5, RID_SUBSETSTR_CJK_UNIFIED_IDGRAPH ) );
846         aAllSubsets.push_back( Subset( 0xA000, 0xA4CF, RID_SUBSETSTR_YI ) );
847         aAllSubsets.push_back( Subset( 0xAC00, 0xB097, RID_SUBSETSTR_HANGUL_GA ) );
848         aAllSubsets.push_back( Subset( 0xB098, 0xB2E3, RID_SUBSETSTR_HANGUL_NA ) );
849         aAllSubsets.push_back( Subset( 0xB2E4, 0xB77B, RID_SUBSETSTR_HANGUL_DA ) );
850         aAllSubsets.push_back( Subset( 0xB77C, 0xB9C7, RID_SUBSETSTR_HANGUL_RA ) );
851         aAllSubsets.push_back( Subset( 0xB9C8, 0xBC13, RID_SUBSETSTR_HANGUL_MA ) );
852         aAllSubsets.push_back( Subset( 0xBC14, 0xC0AB, RID_SUBSETSTR_HANGUL_BA ) );
853         aAllSubsets.push_back( Subset( 0xC0AC, 0xC543, RID_SUBSETSTR_HANGUL_SA ) );
854         aAllSubsets.push_back( Subset( 0xC544, 0xC78F, RID_SUBSETSTR_HANGUL_AH ) );
855         aAllSubsets.push_back( Subset( 0xC790, 0xCC27, RID_SUBSETSTR_HANGUL_JA ) );
856         aAllSubsets.push_back( Subset( 0xCC28, 0xCE73, RID_SUBSETSTR_HANGUL_CHA ) );
857         aAllSubsets.push_back( Subset( 0xCE74, 0xD0BF, RID_SUBSETSTR_HANGUL_KA ) );
858         aAllSubsets.push_back( Subset( 0xD0C0, 0xD30B, RID_SUBSETSTR_HANGUL_TA ) );
859         aAllSubsets.push_back( Subset( 0xD30C, 0xD557, RID_SUBSETSTR_HANGUL_PA ) );
860         aAllSubsets.push_back( Subset( 0xD558, 0xD7A3, RID_SUBSETSTR_HANGUL_HA ) );
861     //  aAllSubsets.push_back( Subset( 0xAC00, 0xD7AF, RID_SUBSETSTR_HANGUL ) );
862 
863     //  aAllSubsets.push_back( Subset( 0xD800, 0xDFFF, RID_SUBSETSTR_SURROGATE ) );
864         aAllSubsets.push_back( Subset( 0xE000, 0xF8FF, RID_SUBSETSTR_PRIVATE_USE_AREA ) );
865         aAllSubsets.push_back( Subset( 0xF900, 0xFAFF, RID_SUBSETSTR_CJK_COMPAT_IDGRAPHS ) );
866         aAllSubsets.push_back( Subset( 0xFB00, 0xFB4F, RID_SUBSETSTR_ALPHA_PRESENTATION ) );
867         aAllSubsets.push_back( Subset( 0xFB50, 0xFDFF, RID_SUBSETSTR_ARABIC_PRESENT_A ) );
868         aAllSubsets.push_back( Subset( 0xFE20, 0xFE2F, RID_SUBSETSTR_COMBINING_HALF_MARKS ) );
869         aAllSubsets.push_back( Subset( 0xFE30, 0xFE4F, RID_SUBSETSTR_CJK_COMPAT_FORMS ) );
870         aAllSubsets.push_back( Subset( 0xFE50, 0xFE6F, RID_SUBSETSTR_SMALL_FORM_VARIANTS ) );
871         aAllSubsets.push_back( Subset( 0xFE70, 0xFEFF, RID_SUBSETSTR_ARABIC_PRESENT_B ) );
872         aAllSubsets.push_back( Subset( 0xFF00, 0xFFEF, RID_SUBSETSTR_HALFW_FULLW_FORMS ) );
873         aAllSubsets.push_back( Subset( 0xFFF0, 0xFFFF, RID_SUBSETSTR_SPECIALS ) );
874     }
875 
876     maSubsets = aAllSubsets;
877 }
878 
ApplyCharMap(const FontCharMap * pFontCharMap)879 void SubsetMap::ApplyCharMap( const FontCharMap* pFontCharMap )
880 {
881     if( !pFontCharMap )
882         return;
883 
884     // remove subsets that are not matched in any range
885     SubsetList::iterator it_next = maSubsets.begin();
886     while( it_next != maSubsets.end() )
887     {
888         SubsetList::iterator it = it_next++;
889         const Subset& rSubset = *it;
890         sal_uInt32 cMin = rSubset.GetRangeMin();
891         sal_uInt32 cMax = rSubset.GetRangeMax();
892 
893         int nCount =  pFontCharMap->CountCharsInRange( cMin, cMax );
894         if( nCount <= 0 )
895             maSubsets.erase( it );
896     }
897 }
898