bump product version to 4.1.6.2
[LibreOffice.git] / forms / source / richtext / richtextvclcontrol.cxx
blob2f4349b247c678233aa7d2a44a2acfa30aec3895
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "richtextvclcontrol.hxx"
21 #include "richtextimplcontrol.hxx"
22 #include <svl/itempool.hxx>
23 #include <svl/itemset.hxx>
24 #include <svl/languageoptions.hxx>
25 #if OSL_DEBUG_LEVEL > 0
26 #include <unotools/ucbstreamhelper.hxx>
27 #include <vcl/msgbox.hxx>
28 #include <sfx2/filedlghelper.hxx>
29 #include <tools/urlobj.hxx>
30 #include "com/sun/star/ui/dialogs/TemplateDescription.hpp"
31 #endif
32 #include <editeng/scripttypeitem.hxx>
33 #include <editeng/editeng.hxx>
34 #include <editeng/editview.hxx>
35 #include <editeng/eeitem.hxx>
36 #include <editeng/fontitem.hxx>
37 #include <editeng/fhgtitem.hxx>
38 #include <editeng/editids.hrc>
39 #include <svx/svxids.hrc>
40 #include <memory>
42 //........................................................................
43 namespace frm
45 //........................................................................
47 using namespace ::com::sun::star::awt;
49 //====================================================================
50 //= RichTextControl
51 //====================================================================
52 //--------------------------------------------------------------------
53 RichTextControl::RichTextControl( RichTextEngine* _pEngine, Window* _pParent, WinBits _nStyle,
54 ITextAttributeListener* _pTextAttribListener, ITextSelectionListener* _pSelectionListener )
55 :Control( _pParent, implInitStyle( _nStyle ) )
56 ,m_pImpl( NULL )
58 implInit( _pEngine, _pTextAttribListener, _pSelectionListener );
61 //--------------------------------------------------------------------
62 void RichTextControl::implInit( RichTextEngine* _pEngine, ITextAttributeListener* _pTextAttribListener, ITextSelectionListener* _pSelectionListener )
64 m_pImpl = new RichTextControlImpl( this, _pEngine, _pTextAttribListener, _pSelectionListener );
65 SetCompoundControl( sal_True );
68 //--------------------------------------------------------------------
69 RichTextControl::~RichTextControl( )
71 delete m_pImpl;
74 //--------------------------------------------------------------------
75 AttributeState RichTextControl::getState( AttributeId _nAttributeId ) const
77 return m_pImpl->getAttributeState( _nAttributeId );
80 //--------------------------------------------------------------------
81 void RichTextControl::executeAttribute( AttributeId _nAttributeId, const SfxPoolItem* _pArgument )
83 SfxItemSet aToApplyAttributes( getView().GetEmptyItemSet() );
84 if ( !m_pImpl->executeAttribute( getView().GetAttribs(), aToApplyAttributes, _nAttributeId, _pArgument, m_pImpl->getSelectedScriptType() ) )
86 OSL_FAIL( "RichTextControl::executeAttribute: cannot handle the given attribute!" );
87 return;
90 applyAttributes( aToApplyAttributes );
93 //--------------------------------------------------------------------
94 void RichTextControl::applyAttributes( const SfxItemSet& _rAttributesToApply )
96 // apply
97 if ( HasChildPathFocus() )
98 getView().HideCursor();
100 sal_Bool bOldUpdateMode = getEngine().GetUpdateMode(); // TODO: guard?
101 getEngine().SetUpdateMode( sal_False );
103 getView().SetAttribs( _rAttributesToApply );
105 getEngine().SetUpdateMode( bOldUpdateMode );
106 getView().Invalidate();
108 if ( HasChildPathFocus() )
109 getView().ShowCursor();
111 m_pImpl->updateAllAttributes();
112 // TODO: maybe we should have a list of attributes which need to be updated
113 // (the handler for the just executed attribute should know)
116 //--------------------------------------------------------------------
117 void RichTextControl::enableAttributeNotification( AttributeId _nAttributeId, ITextAttributeListener* _pListener )
119 m_pImpl->enableAttributeNotification( _nAttributeId, _pListener );
122 //--------------------------------------------------------------------
123 void RichTextControl::disableAttributeNotification( AttributeId _nAttributeId )
125 m_pImpl->disableAttributeNotification( _nAttributeId );
128 //--------------------------------------------------------------------
129 bool RichTextControl::isMappableSlot( SfxSlotId _nSlotId )
131 switch ( _nSlotId )
133 case SID_ATTR_PARA_ADJUST_LEFT:
134 case SID_ATTR_PARA_ADJUST_CENTER:
135 case SID_ATTR_PARA_ADJUST_RIGHT:
136 case SID_ATTR_PARA_ADJUST_BLOCK:
137 case SID_SET_SUPER_SCRIPT:
138 case SID_SET_SUB_SCRIPT:
139 case SID_ATTR_PARA_LINESPACE_10:
140 case SID_ATTR_PARA_LINESPACE_15:
141 case SID_ATTR_PARA_LINESPACE_20:
142 case SID_ATTR_PARA_LEFT_TO_RIGHT:
143 case SID_ATTR_PARA_RIGHT_TO_LEFT:
144 case SID_TEXTDIRECTION_TOP_TO_BOTTOM:
145 case SID_TEXTDIRECTION_LEFT_TO_RIGHT:
146 case SID_ATTR_CHAR_LATIN_FONT:
147 case SID_ATTR_CHAR_LATIN_FONTHEIGHT:
148 case SID_ATTR_CHAR_LATIN_LANGUAGE:
149 case SID_ATTR_CHAR_LATIN_POSTURE:
150 case SID_ATTR_CHAR_LATIN_WEIGHT:
151 return true;
153 return false;
156 //--------------------------------------------------------------------
157 void RichTextControl::Resize()
159 m_pImpl->layoutWindow();
160 Invalidate();
163 //--------------------------------------------------------------------
164 void RichTextControl::GetFocus()
166 getViewport().GrabFocus();
169 //--------------------------------------------------------------------
170 WinBits RichTextControl::implInitStyle( WinBits nStyle )
172 if ( !( nStyle & WB_NOTABSTOP ) )
173 nStyle |= WB_TABSTOP;
174 return nStyle;
177 //--------------------------------------------------------------------
178 void RichTextControl::StateChanged( StateChangedType _nStateChange )
180 if ( _nStateChange == STATE_CHANGE_STYLE )
182 SetStyle( implInitStyle( GetStyle() ) );
183 m_pImpl->notifyStyleChanged();
185 else if ( _nStateChange == STATE_CHANGE_ZOOM )
187 m_pImpl->notifyZoomChanged();
189 else if ( _nStateChange == STATE_CHANGE_INITSHOW )
191 m_pImpl->notifyInitShow();
193 Control::StateChanged( _nStateChange );
196 //--------------------------------------------------------------------
197 long RichTextControl::PreNotify( NotifyEvent& _rNEvt )
199 if ( IsWindowOrChild( _rNEvt.GetWindow() ) )
201 if ( EVENT_KEYINPUT == _rNEvt.GetType() )
203 const ::KeyEvent* pKeyEvent = _rNEvt.GetKeyEvent();
205 sal_uInt16 nCode = pKeyEvent->GetKeyCode().GetCode();
206 sal_Bool bShift = pKeyEvent->GetKeyCode().IsShift();
207 sal_Bool bCtrl = pKeyEvent->GetKeyCode().IsMod1();
208 sal_Bool bAlt = pKeyEvent->GetKeyCode().IsMod2();
209 if ( ( KEY_TAB == nCode ) && bCtrl && !bAlt )
211 // Ctrl-Tab is used to step out of the control
212 // -> build a new key event without the Ctrl-key, and let the very base class handle it
213 KeyCode aNewCode( KEY_TAB, bShift, sal_False, sal_False, sal_False );
214 ::KeyEvent aNewEvent( pKeyEvent->GetCharCode(), aNewCode );
215 Control::KeyInput( aNewEvent );
216 return 1; // handled
219 #if OSL_DEBUG_LEVEL > 0
220 if ( ( ( KEY_F12 == nCode )
221 || ( KEY_F11 == nCode )
223 && bCtrl
224 && bAlt
227 bool bLoad = KEY_F11 == nCode;
228 struct
230 const sal_Char* pDescription;
231 const sal_Char* pExtension;
232 EETextFormat eFormat;
233 } aExportFormats[] =
235 { "OASIS OpenDocument (*.xml)", "*.xml", EE_FORMAT_XML },
236 { "HyperText Markup Language (*.html)", "*.html", EE_FORMAT_HTML },
237 { "Rich Text format (*.rtf)", "*.rtf", EE_FORMAT_RTF },
238 { "Text (*.txt)", "*.txt", EE_FORMAT_TEXT }
241 ::sfx2::FileDialogHelper aFP( bLoad ? com::sun::star::ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE : com::sun::star::ui::dialogs::TemplateDescription::FILESAVE_AUTOEXTENSION, 0, this );
243 for ( size_t i = 0; i < SAL_N_ELEMENTS( aExportFormats ); ++i )
245 aFP.AddFilter(
246 OUString::createFromAscii( aExportFormats[i].pDescription ),
247 OUString::createFromAscii( aExportFormats[i].pExtension ) );
249 ErrCode nResult = aFP.Execute();
250 if ( nResult == 0 )
252 String sFileName = aFP.GetPath();
253 SvStream* pStream = ::utl::UcbStreamHelper::CreateStream(
254 sFileName, ( bLoad ? STREAM_READ : STREAM_WRITE | STREAM_TRUNC ) | STREAM_SHARE_DENYALL
256 if ( pStream )
258 EETextFormat eFormat = EE_FORMAT_XML;
259 String sFilter = aFP.GetCurrentFilter();
260 for ( size_t i = 0; i < SAL_N_ELEMENTS( aExportFormats ); ++i )
262 if ( sFilter.EqualsAscii( aExportFormats[i].pDescription ) )
264 eFormat = aExportFormats[i].eFormat;
265 break;
268 if ( bLoad )
270 INetURLObject aURL( sFileName );
271 aURL.removeSegment();
272 getEngine().Read( *pStream, aURL.GetMainURL( INetURLObject::NO_DECODE ), eFormat );
274 else
276 getEngine().Write( *pStream, eFormat );
279 DELETEZ( pStream );
281 return 1; // handled
283 #endif
286 return Control::PreNotify( _rNEvt );
289 //--------------------------------------------------------------------
290 long RichTextControl::Notify( NotifyEvent& _rNEvt )
292 long nDone = 0;
293 if ( _rNEvt.GetType() == EVENT_COMMAND )
295 const CommandEvent& rEvent = *_rNEvt.GetCommandEvent();
296 nDone = m_pImpl->HandleCommand( rEvent );
298 return nDone ? nDone : Control::Notify( _rNEvt );
301 //--------------------------------------------------------------------
302 void RichTextControl::Draw( OutputDevice* _pDev, const Point& _rPos, const Size& _rSize, sal_uLong _nFlags )
304 m_pImpl->Draw( _pDev, _rPos, _rSize, _nFlags );
307 //--------------------------------------------------------------------
308 EditView& RichTextControl::getView()
310 return *m_pImpl->getView( RichTextControlImpl::GrantAccess() );
313 //--------------------------------------------------------------------
314 const EditView& RichTextControl::getView() const
316 return *m_pImpl->getView( RichTextControlImpl::GrantAccess() );
319 //--------------------------------------------------------------------
320 EditEngine& RichTextControl::getEngine() const
322 return *m_pImpl->getEngine( RichTextControlImpl::GrantAccess() );
325 //--------------------------------------------------------------------
326 Window& RichTextControl::getViewport() const
328 return *m_pImpl->getViewport( RichTextControlImpl::GrantAccess() );
331 //--------------------------------------------------------------------
332 void RichTextControl::SetReadOnly( bool _bReadOnly )
334 m_pImpl->SetReadOnly( _bReadOnly );
337 //--------------------------------------------------------------------
338 bool RichTextControl::IsReadOnly() const
340 return m_pImpl->IsReadOnly();
343 //--------------------------------------------------------------------
344 void RichTextControl::SetBackgroundColor( )
346 m_pImpl->SetBackgroundColor( );
349 //--------------------------------------------------------------------
350 void RichTextControl::SetBackgroundColor( const Color& _rColor )
352 m_pImpl->SetBackgroundColor( _rColor );
355 //--------------------------------------------------------------------
356 void RichTextControl::SetHideInactiveSelection( bool _bHide )
358 m_pImpl->SetHideInactiveSelection( _bHide );
361 //--------------------------------------------------------------------
362 bool RichTextControl::GetHideInactiveSelection() const
364 return m_pImpl->GetHideInactiveSelection( );
367 //........................................................................
368 } // namespace frm
369 //........................................................................
372 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */