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_forms.hxx"
30 #include "richtextvclcontrol.hxx"
31 #include "richtextimplcontrol.hxx"
32 #include <svl/itempool.hxx>
33 #include <svl/itemset.hxx>
34 #include <svl/languageoptions.hxx>
35 #if OSL_DEBUG_LEVEL > 0
36     #ifndef _TOOLS_TEMPFILE_HXX
37     #include <tools/tempfile.hxx>
38     #endif
39     #ifndef _UNTOOLS_UCBSTREAMHELPER_HXX
40     #include <unotools/ucbstreamhelper.hxx>
41     #endif
42     #ifndef _SV_MSGBOX_HXX
43     #include <vcl/msgbox.hxx>
44     #endif
45     #ifndef _FILEDLGHELPER_HXX
46     #include <sfx2/filedlghelper.hxx>
47     #endif
48     #ifndef _URLOBJ_HXX
49     #include <tools/urlobj.hxx>
50     #endif
51     #include "com/sun/star/ui/dialogs/TemplateDescription.hpp"
52 #endif
53 #include <editeng/scripttypeitem.hxx>
54 #include <editeng/editeng.hxx>
55 #include <editeng/editview.hxx>
56 #include <editeng/eeitem.hxx>
57 #include <editeng/fontitem.hxx>
58 #include <editeng/fhgtitem.hxx>
59 #include <editeng/editids.hrc>
60 #include <svx/svxids.hrc>
61 #include <memory>
62 
63 //........................................................................
64 namespace frm
65 {
66 //........................................................................
67 
68     using namespace ::com::sun::star::awt;
69 
70     //====================================================================
71     //= RichTextControl
72     //====================================================================
73     //--------------------------------------------------------------------
74     RichTextControl::RichTextControl( RichTextEngine* _pEngine, Window* _pParent, WinBits _nStyle,
75         ITextAttributeListener* _pTextAttribListener, ITextSelectionListener* _pSelectionListener )
76         :Control( _pParent, implInitStyle( _nStyle ) )
77         ,m_pImpl( NULL )
78     {
79         implInit( _pEngine, _pTextAttribListener, _pSelectionListener );
80     }
81 
82     //--------------------------------------------------------------------
83     void RichTextControl::implInit( RichTextEngine* _pEngine, ITextAttributeListener* _pTextAttribListener, ITextSelectionListener* _pSelectionListener )
84     {
85         m_pImpl = new RichTextControlImpl( this, _pEngine, _pTextAttribListener, _pSelectionListener );
86         SetCompoundControl( sal_True );
87     }
88 
89     //--------------------------------------------------------------------
90     RichTextControl::~RichTextControl( )
91     {
92         delete m_pImpl;
93     }
94 
95     //--------------------------------------------------------------------
96     AttributeState RichTextControl::getState( AttributeId _nAttributeId ) const
97     {
98         return m_pImpl->getAttributeState( _nAttributeId );
99     }
100 
101     //--------------------------------------------------------------------
102     void RichTextControl::executeAttribute( AttributeId _nAttributeId, const SfxPoolItem* _pArgument )
103     {
104         SfxItemSet aToApplyAttributes( getView().GetEmptyItemSet() );
105         if ( !m_pImpl->executeAttribute( getView().GetAttribs(), aToApplyAttributes, _nAttributeId, _pArgument, m_pImpl->getSelectedScriptType() ) )
106         {
107             OSL_ENSURE( sal_False, "RichTextControl::executeAttribute: cannot handle the given attribute!" );
108             return;
109         }
110 
111         applyAttributes( aToApplyAttributes );
112     }
113 
114     //--------------------------------------------------------------------
115     void RichTextControl::applyAttributes( const SfxItemSet& _rAttributesToApply )
116     {
117         // apply
118         if ( HasChildPathFocus() )
119             getView().HideCursor();
120 
121         sal_Bool bOldUpdateMode = getEngine().GetUpdateMode();   // TODO: guard?
122         getEngine().SetUpdateMode( sal_False );
123 
124         getView().SetAttribs( _rAttributesToApply );
125 
126         getEngine().SetUpdateMode( bOldUpdateMode );
127         getView().Invalidate();
128 
129         if ( HasChildPathFocus() )
130             getView().ShowCursor();
131 
132         m_pImpl->updateAllAttributes();
133             // TODO: maybe we should have a list of attributes which need to be updated
134             // (the handler for the just executed attribute should know)
135     }
136 
137     //--------------------------------------------------------------------
138     void RichTextControl::enableAttributeNotification( AttributeId _nAttributeId, ITextAttributeListener* _pListener )
139     {
140         m_pImpl->enableAttributeNotification( _nAttributeId, _pListener );
141     }
142 
143     //--------------------------------------------------------------------
144     void RichTextControl::disableAttributeNotification( AttributeId _nAttributeId )
145     {
146         m_pImpl->disableAttributeNotification( _nAttributeId );
147     }
148 
149     //--------------------------------------------------------------------
150     bool RichTextControl::isMappableSlot( SfxSlotId _nSlotId )
151     {
152         switch ( _nSlotId )
153         {
154             case SID_ATTR_PARA_ADJUST_LEFT:
155             case SID_ATTR_PARA_ADJUST_CENTER:
156             case SID_ATTR_PARA_ADJUST_RIGHT:
157             case SID_ATTR_PARA_ADJUST_BLOCK:
158             case SID_SET_SUPER_SCRIPT:
159             case SID_SET_SUB_SCRIPT:
160             case SID_ATTR_PARA_LINESPACE_10:
161             case SID_ATTR_PARA_LINESPACE_15:
162             case SID_ATTR_PARA_LINESPACE_20:
163             case SID_ATTR_PARA_LEFT_TO_RIGHT:
164             case SID_ATTR_PARA_RIGHT_TO_LEFT:
165             case SID_TEXTDIRECTION_TOP_TO_BOTTOM:
166             case SID_TEXTDIRECTION_LEFT_TO_RIGHT:
167             case SID_ATTR_CHAR_LATIN_FONT:
168             case SID_ATTR_CHAR_LATIN_FONTHEIGHT:
169             case SID_ATTR_CHAR_LATIN_LANGUAGE:
170             case SID_ATTR_CHAR_LATIN_POSTURE:
171             case SID_ATTR_CHAR_LATIN_WEIGHT:
172                 return true;
173         }
174         return false;
175     }
176 
177     //--------------------------------------------------------------------
178     void RichTextControl::Resize()
179     {
180         m_pImpl->layoutWindow();
181         Invalidate();
182     }
183 
184     //--------------------------------------------------------------------
185     void RichTextControl::GetFocus()
186     {
187         getViewport().GrabFocus();
188     }
189 
190     //--------------------------------------------------------------------
191     WinBits RichTextControl::implInitStyle( WinBits nStyle )
192     {
193         if ( !( nStyle & WB_NOTABSTOP ) )
194             nStyle |= WB_TABSTOP;
195         return nStyle;
196     }
197 
198     //--------------------------------------------------------------------
199     void RichTextControl::StateChanged( StateChangedType _nStateChange )
200     {
201         if ( _nStateChange == STATE_CHANGE_STYLE )
202         {
203             SetStyle( implInitStyle( GetStyle() ) );
204             m_pImpl->notifyStyleChanged();
205         }
206         else if ( _nStateChange == STATE_CHANGE_ZOOM )
207         {
208             m_pImpl->notifyZoomChanged();
209         }
210         else if ( _nStateChange == STATE_CHANGE_INITSHOW )
211         {
212             m_pImpl->notifyInitShow();
213         }
214         Control::StateChanged( _nStateChange );
215     }
216 
217     //--------------------------------------------------------------------
218     long RichTextControl::PreNotify( NotifyEvent& _rNEvt )
219     {
220         if ( IsWindowOrChild( _rNEvt.GetWindow() ) )
221         {
222             if ( EVENT_KEYINPUT == _rNEvt.GetType() )
223             {
224 				const ::KeyEvent* pKeyEvent = _rNEvt.GetKeyEvent();
225 
226                 sal_uInt16 nCode = pKeyEvent->GetKeyCode().GetCode();
227                 sal_Bool   bShift = pKeyEvent->GetKeyCode().IsShift();
228                 sal_Bool   bCtrl = pKeyEvent->GetKeyCode().IsMod1();
229                 sal_Bool   bAlt = pKeyEvent->GetKeyCode().IsMod2();
230                 if ( ( KEY_TAB == nCode ) && bCtrl && !bAlt )
231                 {
232                     // Ctrl-Tab is used to step out of the control
233                     // -> build a new key event without the Ctrl-key, and let the very base class handle it
234                     KeyCode aNewCode( KEY_TAB, bShift, sal_False, sal_False, sal_False );
235 					::KeyEvent aNewEvent( pKeyEvent->GetCharCode(), aNewCode );
236                     Control::KeyInput( aNewEvent );
237                     return 1;   // handled
238                 }
239 
240 #if OSL_DEBUG_LEVEL > 0
241                 if  (   (   ( KEY_F12 == nCode )
242                         ||  ( KEY_F11 == nCode )
243                         )
244                     &&  bCtrl
245                     &&  bAlt
246                     )
247                 {
248                     bool bLoad = KEY_F11 == nCode;
249                     struct
250                     {
251                         const sal_Char* pDescription;
252                         const sal_Char* pExtension;
253                         EETextFormat    eFormat;
254                     } aExportFormats[] =
255                     {
256                         { "OASIS OpenDocument (*.xml)", "*.xml", EE_FORMAT_XML },
257                         { "HyperText Markup Language (*.html)", "*.html", EE_FORMAT_HTML },
258                         { "Rich Text format (*.rtf)", "*.rtf", EE_FORMAT_RTF },
259                         { "Text (*.txt)", "*.txt", EE_FORMAT_TEXT }
260                     };
261 
262                     ::sfx2::FileDialogHelper aFP( bLoad ? com::sun::star::ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE : com::sun::star::ui::dialogs::TemplateDescription::FILESAVE_AUTOEXTENSION, 0, this );
263 
264                     for ( size_t i = 0; i < sizeof( aExportFormats ) / sizeof( aExportFormats[ 0 ] ); ++i )
265                     {
266                         aFP.AddFilter(
267                             String::CreateFromAscii( aExportFormats[i].pDescription ),
268                             String::CreateFromAscii( aExportFormats[i].pExtension ) );
269                     }
270                     ErrCode nResult = aFP.Execute();
271                     if ( nResult == 0 )
272                     {
273                         String sFileName = aFP.GetPath();
274                         SvStream* pStream = ::utl::UcbStreamHelper::CreateStream(
275                             sFileName, ( bLoad ? STREAM_READ : STREAM_WRITE | STREAM_TRUNC ) | STREAM_SHARE_DENYALL
276                         );
277                         if ( pStream )
278                         {
279                             EETextFormat eFormat = EE_FORMAT_XML;
280                             String sFilter = aFP.GetCurrentFilter();
281                             for ( size_t i = 0; i < sizeof( aExportFormats ) / sizeof( aExportFormats[ 0 ] ); ++i )
282                             {
283                                 if ( sFilter.EqualsAscii( aExportFormats[i].pDescription ) )
284                                 {
285                                     eFormat = aExportFormats[i].eFormat;
286                                     break;
287                                 }
288                             }
289                             if ( bLoad )
290                             {
291                                 INetURLObject aURL( sFileName );
292                                 aURL.removeSegment();
293                                 getEngine().Read( *pStream, aURL.GetMainURL( INetURLObject::NO_DECODE ), eFormat );
294                             }
295                             else
296                             {
297                                 getEngine().Write( *pStream, eFormat );
298                             }
299                         }
300                         DELETEZ( pStream );
301                     }
302                     return 1;   // handled
303                 }
304 #endif
305             }
306         }
307         return Control::PreNotify( _rNEvt );
308     }
309 
310     //--------------------------------------------------------------------
311     long RichTextControl::Notify( NotifyEvent& _rNEvt )
312     {
313         long nDone = 0;
314         if ( _rNEvt.GetType() == EVENT_COMMAND )
315         {
316             const CommandEvent& rEvent = *_rNEvt.GetCommandEvent();
317             nDone = m_pImpl->HandleCommand( rEvent );
318         }
319         return nDone ? nDone : Control::Notify( _rNEvt );
320     }
321 
322     //--------------------------------------------------------------------
323     void RichTextControl::Draw( OutputDevice* _pDev, const Point& _rPos, const Size& _rSize, sal_uLong _nFlags )
324     {
325         m_pImpl->Draw( _pDev, _rPos, _rSize, _nFlags );
326     }
327 
328     //--------------------------------------------------------------------
329     EditView& RichTextControl::getView()
330     {
331         return *m_pImpl->getView( RichTextControlImpl::GrantAccess() );
332     }
333 
334     //--------------------------------------------------------------------
335     const EditView& RichTextControl::getView() const
336     {
337         return *m_pImpl->getView( RichTextControlImpl::GrantAccess() );
338     }
339 
340     //--------------------------------------------------------------------
341     EditEngine& RichTextControl::getEngine() const
342     {
343         return *m_pImpl->getEngine( RichTextControlImpl::GrantAccess() );
344     }
345 
346     //--------------------------------------------------------------------
347     Window& RichTextControl::getViewport() const
348     {
349         return *m_pImpl->getViewport( RichTextControlImpl::GrantAccess() );
350     }
351 
352     //--------------------------------------------------------------------
353     void RichTextControl::SetReadOnly( bool _bReadOnly )
354     {
355         m_pImpl->SetReadOnly( _bReadOnly );
356     }
357 
358     //--------------------------------------------------------------------
359     bool RichTextControl::IsReadOnly() const
360     {
361         return m_pImpl->IsReadOnly();
362     }
363 
364     //--------------------------------------------------------------------
365     void RichTextControl::SetBackgroundColor( )
366     {
367         m_pImpl->SetBackgroundColor( );
368     }
369 
370     //--------------------------------------------------------------------
371     void RichTextControl::SetBackgroundColor( const Color& _rColor )
372     {
373         m_pImpl->SetBackgroundColor( _rColor );
374     }
375 
376     //--------------------------------------------------------------------
377     void RichTextControl::SetHideInactiveSelection( bool _bHide )
378     {
379         m_pImpl->SetHideInactiveSelection( _bHide );
380     }
381 
382     //--------------------------------------------------------------------
383     bool RichTextControl::GetHideInactiveSelection() const
384     {
385         return m_pImpl->GetHideInactiveSelection( );
386     }
387 
388 //........................................................................
389 }   // namespace frm
390 //........................................................................
391 
392 
393