Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / pagedlg / tphfedit.cxx
blobe58e3c2e3cac6c84e6997a21109c7a2c43d59ad8
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 "scitems.hxx"
21 #include <editeng/eeitem.hxx>
24 #include <editeng/editobj.hxx>
25 #include <editeng/editstat.hxx>
26 #include <editeng/editview.hxx>
27 #include <editeng/flditem.hxx>
28 #include <editeng/adjustitem.hxx>
29 #include <sfx2/basedlgs.hxx>
30 #include <sfx2/objsh.hxx>
31 #include <vcl/msgbox.hxx>
32 #include <vcl/svapp.hxx>
34 #include "tphfedit.hxx"
35 #include "editutil.hxx"
36 #include "global.hxx"
37 #include "attrib.hxx"
38 #include "patattr.hxx"
39 #include "scresid.hxx"
40 #include "sc.hrc"
41 #include "globstr.hrc"
42 #include "tabvwsh.hxx"
43 #include "prevwsh.hxx"
44 #include "AccessibleEditObject.hxx"
46 #include "scabstdlg.hxx"
49 // STATIC DATA -----------------------------------------------------------
50 static ScEditWindow* pActiveEdWnd = NULL;
52 ScEditWindow* GetScEditWindow ()
54 return pActiveEdWnd;
57 //========================================================================
59 static void lcl_GetFieldData( ScHeaderFieldData& rData )
61 SfxViewShell* pShell = SfxViewShell::Current();
62 if (pShell)
64 if (pShell->ISA(ScTabViewShell))
65 ((ScTabViewShell*)pShell)->FillFieldData(rData);
66 else if (pShell->ISA(ScPreviewShell))
67 ((ScPreviewShell*)pShell)->FillFieldData(rData);
71 //========================================================================
72 // class ScEditWindow
73 //========================================================================
75 ScEditWindow::ScEditWindow( Window* pParent, WinBits nBits, ScEditWindowLocation eLoc )
76 : Control( pParent, nBits ),
77 eLocation(eLoc),
78 pAcc(NULL)
80 EnableRTL(false);
82 const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
83 Color aBgColor = rStyleSettings.GetWindowColor();
85 SetMapMode( MAP_TWIP );
86 SetPointer( POINTER_TEXT );
87 SetBackground( aBgColor );
89 Size aSize( GetOutputSize() );
90 aSize.Height() *= 4;
92 pEdEngine = new ScHeaderEditEngine( EditEngine::CreatePool(), sal_True );
93 pEdEngine->SetPaperSize( aSize );
94 pEdEngine->SetRefDevice( this );
96 ScHeaderFieldData aData;
97 lcl_GetFieldData( aData );
99 // Feldbefehle:
100 pEdEngine->SetData( aData );
101 pEdEngine->SetControlWord( pEdEngine->GetControlWord() | EE_CNTRL_MARKFIELDS );
102 mbRTL = ScGlobal::IsSystemRTL();
103 if (mbRTL)
104 pEdEngine->SetDefaultHorizontalTextDirection(EE_HTEXTDIR_R2L);
106 pEdView = new EditView( pEdEngine, this );
107 pEdView->SetOutputArea( Rectangle( Point(0,0), GetOutputSize() ) );
109 pEdView->SetBackgroundColor( aBgColor );
110 pEdEngine->InsertView( pEdView );
113 void ScEditWindow::Resize()
115 Size aOutputSize(GetOutputSize());
116 Size aSize(aOutputSize);
117 aSize.Height() *= 4;
118 pEdEngine->SetPaperSize(aSize);
119 pEdView->SetOutputArea(Rectangle(Point(0,0), aOutputSize));
120 Control::Resize();
123 // -----------------------------------------------------------------------
125 ScEditWindow::~ScEditWindow()
127 // delete Accessible object before deleting EditEngine and EditView
128 if (pAcc)
130 ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessible > xTemp = xAcc;
131 if (xTemp.is())
132 pAcc->dispose();
134 delete pEdEngine;
135 delete pEdView;
138 extern "C" SAL_DLLPUBLIC_EXPORT Window* SAL_CALL makeScEditWindow(Window *pParent, VclBuilder::stringmap &)
140 return new ScEditWindow (pParent, WB_BORDER|WB_TABSTOP, Left);
143 // -----------------------------------------------------------------------
145 void ScEditWindow::SetNumType(SvxNumType eNumType)
147 pEdEngine->SetNumType(eNumType);
148 pEdEngine->UpdateFields();
151 // -----------------------------------------------------------------------
153 EditTextObject* ScEditWindow::CreateTextObject()
155 // Absatzattribute zuruecksetzen
156 // (GetAttribs beim Format-Dialog-Aufruf gibt immer gesetzte Items zurueck)
158 const SfxItemSet& rEmpty = pEdEngine->GetEmptyItemSet();
159 sal_Int32 nParCnt = pEdEngine->GetParagraphCount();
160 for (sal_Int32 i=0; i<nParCnt; i++)
161 pEdEngine->SetParaAttribs( i, rEmpty );
163 return pEdEngine->CreateTextObject();
166 // -----------------------------------------------------------------------
168 void ScEditWindow::SetFont( const ScPatternAttr& rPattern )
170 SfxItemSet* pSet = new SfxItemSet( pEdEngine->GetEmptyItemSet() );
171 rPattern.FillEditItemSet( pSet );
172 // FillEditItemSet adjusts font height to 1/100th mm,
173 // but for header/footer twips is needed, as in the PatternAttr:
174 pSet->Put( rPattern.GetItem(ATTR_FONT_HEIGHT), EE_CHAR_FONTHEIGHT );
175 pSet->Put( rPattern.GetItem(ATTR_CJK_FONT_HEIGHT), EE_CHAR_FONTHEIGHT_CJK );
176 pSet->Put( rPattern.GetItem(ATTR_CTL_FONT_HEIGHT), EE_CHAR_FONTHEIGHT_CTL );
177 if (mbRTL)
178 pSet->Put( SvxAdjustItem( SVX_ADJUST_RIGHT, EE_PARA_JUST ) );
179 pEdEngine->SetDefaults( pSet );
182 // -----------------------------------------------------------------------
184 void ScEditWindow::SetText( const EditTextObject& rTextObject )
186 pEdEngine->SetText( rTextObject );
189 // -----------------------------------------------------------------------
191 void ScEditWindow::InsertField( const SvxFieldItem& rFld )
193 pEdView->InsertField( rFld );
196 // -----------------------------------------------------------------------
198 void ScEditWindow::SetCharAttriutes()
200 SfxObjectShell* pDocSh = SfxObjectShell::Current();
202 SfxViewShell* pViewSh = SfxViewShell::Current();
204 ScTabViewShell* pTabViewSh = PTR_CAST(ScTabViewShell, SfxViewShell::Current());
207 OSL_ENSURE( pDocSh, "Current DocShell not found" );
208 OSL_ENSURE( pViewSh, "Current ViewShell not found" );
210 if ( pDocSh && pViewSh )
212 if(pTabViewSh!=NULL) pTabViewSh->SetInFormatDialog(sal_True);
214 SfxItemSet aSet( pEdView->GetAttribs() );
216 ScAbstractDialogFactory* pFact = ScAbstractDialogFactory::Create();
217 OSL_ENSURE(pFact, "ScAbstractFactory create fail!");
219 SfxAbstractTabDialog* pDlg = pFact->CreateScCharDlg( GetParent(), &aSet,
220 pDocSh,RID_SCDLG_CHAR );
221 OSL_ENSURE(pDlg, "Dialog create fail!");
222 pDlg->SetText( ScGlobal::GetRscString( STR_TEXTATTRS ) );
223 if ( pDlg->Execute() == RET_OK )
225 aSet.ClearItem();
226 aSet.Put( *pDlg->GetOutputItemSet() );
227 pEdView->SetAttribs( aSet );
230 if(pTabViewSh!=NULL) pTabViewSh->SetInFormatDialog(false);
231 delete pDlg;
235 // -----------------------------------------------------------------------
237 void ScEditWindow::Paint( const Rectangle& rRect )
239 const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
240 Color aBgColor = rStyleSettings.GetWindowColor();
242 pEdView->SetBackgroundColor( aBgColor );
244 SetBackground( aBgColor );
246 Control::Paint( rRect );
248 pEdView->Paint( rRect );
251 // -----------------------------------------------------------------------
253 void ScEditWindow::MouseMove( const MouseEvent& rMEvt )
255 pEdView->MouseMove( rMEvt );
258 // -----------------------------------------------------------------------
260 void ScEditWindow::MouseButtonDown( const MouseEvent& rMEvt )
262 if ( !HasFocus() )
263 GrabFocus();
265 pEdView->MouseButtonDown( rMEvt );
268 // -----------------------------------------------------------------------
270 void ScEditWindow::MouseButtonUp( const MouseEvent& rMEvt )
272 pEdView->MouseButtonUp( rMEvt );
275 // -----------------------------------------------------------------------
277 void ScEditWindow::KeyInput( const KeyEvent& rKEvt )
279 sal_uInt16 nKey = rKEvt.GetKeyCode().GetModifier()
280 + rKEvt.GetKeyCode().GetCode();
282 if ( nKey == KEY_TAB || nKey == KEY_TAB + KEY_SHIFT )
284 Control::KeyInput( rKEvt );
286 else if ( !pEdView->PostKeyEvent( rKEvt ) )
288 Control::KeyInput( rKEvt );
292 // -----------------------------------------------------------------------
294 void ScEditWindow::Command( const CommandEvent& rCEvt )
296 pEdView->Command( rCEvt );
299 // -----------------------------------------------------------------------
301 void ScEditWindow::GetFocus()
303 pActiveEdWnd = this;
305 ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessible > xTemp = xAcc;
306 if (xTemp.is() && pAcc)
308 pAcc->GotFocus();
310 else
311 pAcc = NULL;
314 void ScEditWindow::LoseFocus()
316 ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessible > xTemp = xAcc;
317 if (xTemp.is() && pAcc)
319 pAcc->LostFocus();
321 else
322 pAcc = NULL;
325 // -----------------------------------------------------------------------
327 ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessible > ScEditWindow::CreateAccessible()
329 OUString sName;
330 OUString sDescription(GetHelpText());
331 switch (eLocation)
333 case Left:
335 sName = OUString(ScResId(STR_ACC_LEFTAREA_NAME));
337 break;
338 case Center:
340 sName = OUString(ScResId(STR_ACC_CENTERAREA_NAME));
342 break;
343 case Right:
345 sName = OUString(ScResId(STR_ACC_RIGHTAREA_NAME));
347 break;
349 pAcc = new ScAccessibleEditObject(GetAccessibleParentWindow()->GetAccessible(), pEdView, this,
350 OUString(sName), OUString(sDescription), ScAccessibleEditObject::EditControl);
351 ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessible > xAccessible = pAcc;
352 xAcc = xAccessible;
353 return pAcc;
356 ScExtIButton::ScExtIButton(Window* pParent, WinBits nBits )
357 : ImageButton(pParent,nBits), pPopupMenu(NULL)
359 nSelected=0;
360 aTimer.SetTimeout(600);
361 SetDropDown( true);
364 extern "C" SAL_DLLPUBLIC_EXPORT Window* SAL_CALL makeScExtIButton(Window *pParent, VclBuilder::stringmap &)
366 return new ScExtIButton (pParent, 0);// WB_BORDER|WB_TABSTOP);
369 void ScExtIButton::SetPopupMenu(PopupMenu* pPopUp)
371 pPopupMenu=pPopUp;
374 sal_uInt16 ScExtIButton::GetSelected() const
376 return nSelected;
379 OString ScExtIButton::GetSelectedIdent() const
381 return aSelectedIdent;
384 void ScExtIButton::MouseButtonDown( const MouseEvent& rMEvt )
386 if(!aTimer.IsActive())
388 aTimer.Start();
389 aTimer.SetTimeoutHdl(LINK( this, ScExtIButton, TimerHdl));
392 ImageButton::MouseButtonDown(rMEvt );
395 void ScExtIButton::MouseButtonUp( const MouseEvent& rMEvt)
397 aTimer.Stop();
398 aTimer.SetTimeoutHdl(Link());
399 ImageButton::MouseButtonUp(rMEvt );
402 void ScExtIButton::Click()
404 aTimer.Stop();
405 aTimer.SetTimeoutHdl(Link());
406 ImageButton::Click();
409 void ScExtIButton::StartPopup()
411 nSelected=0;
412 aSelectedIdent = OString();
414 if(pPopupMenu!=NULL)
416 SetPressed( sal_True );
417 EndSelection();
418 Point aPoint(0,0);
419 aPoint.Y()=GetOutputSizePixel().Height();
421 nSelected = pPopupMenu->Execute( this, aPoint );
423 if(nSelected)
425 aSelectedIdent = pPopupMenu->GetItemIdent(nSelected);
426 aMLink.Call(this);
429 SetPressed( false);
433 long ScExtIButton::PreNotify( NotifyEvent& rNEvt )
435 sal_uInt16 nSwitch=rNEvt.GetType();
436 if(nSwitch==EVENT_MOUSEBUTTONUP)
438 MouseButtonUp(*rNEvt.GetMouseEvent());
441 return ImageButton::PreNotify(rNEvt );
444 IMPL_LINK_NOARG(ScExtIButton, TimerHdl)
446 StartPopup();
447 return 0;
451 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */