Update ooo320-m1
[ooovba.git] / sw / source / ui / utlui / unotools.cxx
blob1b23e3560e6ff09d93ed38d46bce95ed12b7a4ee
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: unotools.cxx,v $
10 * $Revision: 1.30 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
34 #include <swtypes.hxx>
35 #include <globals.hrc>
36 #include <misc.hrc>
38 #include <utlui.hrc>
39 #include <unotools.hrc>
40 #include <unotools.hxx>
41 #include <unoprnms.hxx>
42 #include <tools/debug.hxx>
43 #include <vcl/msgbox.hxx>
44 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
45 #include <com/sun/star/view/XScreenCursor.hpp>
46 #include <com/sun/star/view/DocumentZoomType.hpp>
47 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
48 #include <com/sun/star/style/XStyle.hpp>
49 #include <com/sun/star/text/XTextDocument.hpp>
50 #include <com/sun/star/awt/PosSize.hpp>
51 #include <com/sun/star/view/XViewSettingsSupplier.hpp>
52 #include <com/sun/star/container/XNameContainer.hpp>
53 #include <com/sun/star/frame/XLayoutManager.hpp>
54 #include <comphelper/processfactory.hxx>
55 #include <sfx2/dispatch.hxx>
56 #include <svtools/stritem.hxx>
57 #include <shellio.hxx>
58 #include <docsh.hxx>
59 #include <view.hxx>
60 #include <wrtsh.hxx>
61 #include <swmodule.hxx>
62 #include <unoobj.hxx>
63 #include <unocrsr.hxx>
64 #include <doc.hxx>
66 #include <unomid.h>
68 using namespace ::com::sun::star;
69 using ::rtl::OUString;
71 const sal_Char cFrameControl[] = "com.sun.star.frame.FrameControl";
72 const sal_Char cFactory[] = "private:factory/swriter";
73 /************************************************************************
75 ************************************************************************/
77 sal_Bool SwOneExampleFrame::bShowServiceNotAvailableMessage = sal_True;
79 /* -----------------27.07.99 15:26-------------------
81 --------------------------------------------------*/
82 SwOneExampleFrame::SwOneExampleFrame( Window& rWin,
83 sal_uInt32 nFlags,
84 const Link* pInitializedLink,
85 String* pURL ) :
86 aTopWindow( rWin.GetParent(), 0, this ),
87 rWindow(rWin),
88 aMenuRes(SW_RES(RES_FRMEX_MENU)),
90 pModuleView(SW_MOD()->GetView()),
92 nStyleFlags(nFlags),
93 bIsInitialized(sal_False),
94 bServiceAvailable(sal_False)
96 if(pURL && pURL->Len())
97 sArgumentURL = *pURL;
99 aTopWindow.SetPaintTransparent(sal_True);
100 aTopWindow.SetPosSizePixel(rWin.GetPosPixel(), rWin.GetSizePixel());
101 aTopWindow.SetZOrder( &rWin, WINDOW_ZORDER_FIRST );
103 if( pInitializedLink )
104 aInitializedLink = *pInitializedLink;
106 // the controller is asynchronously set
107 aLoadedTimer.SetTimeoutHdl(LINK(this, SwOneExampleFrame, TimeoutHdl));
108 // aLoadedTimer.SetTimeout(500);
109 aLoadedTimer.SetTimeout(200);
111 rWin.Enable(sal_False);
112 CreateControl();
114 aTopWindow.Show();
117 /* -----------------------------08.12.99 13:44--------------------------------
119 ---------------------------------------------------------------------------*/
120 void SwOneExampleFrame::CreateErrorMessage(Window* pParent)
122 if(SwOneExampleFrame::bShowServiceNotAvailableMessage)
124 String sInfo(SW_RES(STR_SERVICE_UNAVAILABLE));
125 sInfo += C2S(cFrameControl);
126 InfoBox(pParent, sInfo).Execute();
127 SwOneExampleFrame::bShowServiceNotAvailableMessage = sal_False;
130 /* -----------------27.07.99 15:26-------------------
132 --------------------------------------------------*/
133 SwOneExampleFrame::~SwOneExampleFrame()
135 DisposeControl();
137 /* -----------------------------21.12.00 10:16--------------------------------
139 ---------------------------------------------------------------------------*/
140 void SwOneExampleFrame::CreateControl()
142 if(_xControl.is())
143 return ;
144 uno::Reference< lang::XMultiServiceFactory >
145 xMgr = comphelper::getProcessServiceFactory();
146 uno::Reference< uno::XInterface > xInst = xMgr->createInstance( C2U("com.sun.star.frame.FrameControl") );
147 _xControl = uno::Reference< awt::XControl >(xInst, uno::UNO_QUERY);
148 if(_xControl.is())
150 uno::Reference< awt::XWindowPeer > xParent( rWindow.GetComponentInterface() );
152 uno::Reference< awt::XToolkit > xToolkit( xMgr->createInstance( C2U("com.sun.star.awt.Toolkit") ), uno::UNO_QUERY );
153 if(xToolkit.is())
155 _xControl->createPeer( xToolkit, xParent );
157 uno::Reference< awt::XWindow > xWin( _xControl, uno::UNO_QUERY );
158 xWin->setVisible( sal_False );
159 Size aWinSize(rWindow.GetOutputSizePixel());
160 xWin->setPosSize( 0, 0, aWinSize.Width(), aWinSize.Height(), awt::PosSize::SIZE );
162 uno::Reference< beans::XPropertySet > xPrSet(xInst, uno::UNO_QUERY);
163 uno::Any aURL;
165 // create new doc
166 String sTempURL = C2S(cFactory);
167 if(sArgumentURL.Len())
168 sTempURL = sArgumentURL;
169 aURL <<= OUString(sTempURL);
171 uno::Sequence<beans::PropertyValue> aSeq(3);
172 beans::PropertyValue* pValues = aSeq.getArray();
173 pValues[0].Name = C2U("ReadOnly");
174 BOOL bTrue = sal_True;
175 pValues[0].Value.setValue(&bTrue, ::getBooleanCppuType());
176 pValues[1].Name = C2U("OpenFlags");
177 pValues[1].Value <<= C2U("-RB");
178 pValues[2].Name = C2U("Referer");
179 pValues[2].Value <<= C2U("private:user");
180 uno::Any aArgs;
181 aArgs.setValue(&aSeq, ::getCppuType((uno::Sequence<beans::PropertyValue>*)0));
183 xPrSet->setPropertyValue( C2U("LoaderArguments"), aArgs );
184 //save and set readonly???
186 xPrSet->setPropertyValue(C2U("ComponentURL"), aURL);
188 aLoadedTimer.Start();
189 bServiceAvailable = sal_True;
195 /* -----------------------------21.12.00 10:16--------------------------------
197 ---------------------------------------------------------------------------*/
198 void SwOneExampleFrame::DisposeControl()
200 _xCursor = 0;
201 if(_xControl.is())
202 _xControl->dispose();
203 _xControl = 0;
204 _xModel = 0;
205 _xController = 0;
207 /* -----------------27.07.99 15:26-------------------
209 --------------------------------------------------*/
210 IMPL_LINK( SwOneExampleFrame, TimeoutHdl, Timer*, pTimer )
212 if(!_xControl.is())
213 return 0;
215 // now get the model
216 uno::Reference< beans::XPropertySet > xPrSet(_xControl, uno::UNO_QUERY);
217 uno::Any aFrame = xPrSet->getPropertyValue(C2U("Frame"));
218 uno::Reference< frame::XFrame > xFrm;
219 aFrame >>= xFrm;
221 uno::Reference< beans::XPropertySet > xPropSet( xFrm, uno::UNO_QUERY );
222 if ( xPropSet.is() )
226 uno::Reference< frame::XLayoutManager > xLayoutManager;
227 uno::Any aValue = xPropSet->getPropertyValue(C2U("LayoutManager"));
228 aValue >>= xLayoutManager;
229 if ( xLayoutManager.is() )
230 xLayoutManager->setVisible( sal_False );
232 catch ( uno::Exception& )
237 _xController = xFrm->getController();
238 if(_xController.is())
240 _xModel = _xController->getModel();
241 //now the ViewOptions should be set properly
242 uno::Reference< view::XViewSettingsSupplier > xSettings(_xController, uno::UNO_QUERY);
243 uno::Reference< beans::XPropertySet > xViewProps = xSettings->getViewSettings();
245 sal_Bool bTrue = sal_True;
246 sal_Bool bFalse = sal_False;
247 uno::Any aTrueSet( &bTrue, ::getBooleanCppuType() );
248 uno::Any aFalseSet( &bFalse, ::getBooleanCppuType() );
250 if( !bIsInitialized )
252 xViewProps->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_SHOW_BREAKS )), aFalseSet);
253 xViewProps->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_SHOW_DRAWINGS )), aTrueSet);
254 xViewProps->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_SHOW_FIELD_COMMANDS )), aFalseSet);
255 xViewProps->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_SHOW_GRAPHICS )), aTrueSet);
256 xViewProps->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_SHOW_HIDDEN_PARAGRAPHS )), aFalseSet);
257 xViewProps->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_SHOW_HIDDEN_TEXT )), aFalseSet);
258 xViewProps->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_SHOW_HORI_RULER )), aFalseSet);
259 xViewProps->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_SHOW_PARA_BREAKS )), aFalseSet);
260 xViewProps->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_SHOW_PROTECTED_SPACES )), aFalseSet);
261 xViewProps->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_SHOW_SOFT_HYPHENS )), aFalseSet);
262 xViewProps->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_SHOW_SPACES )), aFalseSet);
263 xViewProps->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_SHOW_TABLES )), aTrueSet);
264 xViewProps->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_SHOW_TABSTOPS )), aFalseSet);
265 xViewProps->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_SHOW_VERT_RULER )), aFalseSet);
267 if(0 ==(nStyleFlags&EX_SHOW_ONLINE_LAYOUT))
269 uno::Any aZoom;
270 aZoom <<= (sal_Int16)view::DocumentZoomType::PAGE_WIDTH_EXACT;
271 xViewProps->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_ZOOM_TYPE)), aZoom);
273 else
275 uno::Any aZoom;
276 aZoom <<= (sal_Int16)view::DocumentZoomType::BY_VALUE;
277 xViewProps->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_ZOOM_TYPE)), aZoom);
279 sal_Int16 nZoomValue = 50;
280 if(EX_SHOW_BUSINESS_CARDS == nStyleFlags)
282 nZoomValue = 80;
284 aZoom <<= nZoomValue;
285 xViewProps->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_ZOOM_VALUE)), aZoom);
287 // set onlinelayout property behind setting the zoom
288 xViewProps->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_SHOW_ONLINE_LAYOUT)),
289 (nStyleFlags&EX_SHOW_ONLINE_LAYOUT) ? aTrueSet : aFalseSet );
290 bIsInitialized = sal_True;
293 uno::Reference< text::XTextDocument > xDoc(_xModel, uno::UNO_QUERY);
294 uno::Reference< text::XText > xText = xDoc->getText();
295 _xCursor = xText->createTextCursor();
296 uno::Reference< beans::XPropertySet > xCrsrProp(_xCursor, uno::UNO_QUERY);
297 uno::Any aPageStyle = xCrsrProp->getPropertyValue(
298 C2U(SW_PROP_NAME_STR(UNO_NAME_PAGE_STYLE_NAME)));
299 OUString sPageStyle;
300 aPageStyle >>= sPageStyle;
302 uno::Reference< style::XStyleFamiliesSupplier > xSSupp( xDoc, uno::UNO_QUERY);
303 uno::Reference< container::XNameAccess > xStyles = xSSupp->getStyleFamilies();
304 uno::Any aPFamily = xStyles->getByName( C2U("PageStyles" ) );
305 uno::Reference< container::XNameContainer > xPFamily;
307 if( 0 == (EX_SHOW_DEFAULT_PAGE == nStyleFlags)
308 && (aPFamily >>= xPFamily) && sPageStyle.getLength() )
310 uno::Any aPStyle = xPFamily->getByName( sPageStyle );
311 uno::Reference< style::XStyle > xPStyle;
312 aPStyle >>= xPStyle;
313 uno::Reference< beans::XPropertySet > xPProp(xPStyle, uno::UNO_QUERY);
314 uno::Any aSize = xPProp->getPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_SIZE)));
315 awt::Size aPSize;
316 aSize >>= aPSize;
317 //TODO: set page width to card width
318 aPSize.Width = 10000;
319 aSize.setValue(&aPSize, ::getCppuType((awt::Size*)0));
320 xPProp->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_SIZE)), aSize);
322 uno::Any aZero; aZero <<= (sal_Int32)0;
323 xPProp->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_LEFT_MARGIN)), aZero);
324 xPProp->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_RIGHT_MARGIN)), aZero);
327 // can only be done here - the SFX changes the ScrollBar values
328 xViewProps->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_SHOW_HORI_SCROLL_BAR )), aFalseSet);
329 xViewProps->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_SHOW_VERT_SCROLL_BAR )), aFalseSet);
331 if( aInitializedLink.IsSet() )
333 rWindow.Enable(sal_False, sal_True);
334 //rWindow.Enable(sal_True, sal_False);
335 aInitializedLink.Call(this);
338 uno::Reference< text::XTextViewCursorSupplier > xCrsrSupp(_xController, uno::UNO_QUERY);
339 uno::Reference< view::XScreenCursor > xScrCrsr(xCrsrSupp->getViewCursor(), uno::UNO_QUERY);
340 if(xScrCrsr.is())
341 xScrCrsr->screenUp();
343 uno::Reference< awt::XWindow > xWin( _xControl, uno::UNO_QUERY );
344 xWin->setVisible( sal_True );
345 rWindow.Show();
347 uno::Reference< lang::XUnoTunnel> xTunnel( _xCursor, uno::UNO_QUERY);
348 if( xTunnel.is() )
350 OTextCursorHelper* pCrsr = reinterpret_cast<OTextCursorHelper*>( xTunnel->getSomething(
351 OTextCursorHelper::getUnoTunnelId() ));
352 if( pCrsr )
354 SwEditShell* pSh = pCrsr->GetDoc()->GetEditShell();
355 if( pSh->ActionCount() )
357 pSh->EndAllAction();
358 pSh->UnlockPaint();
363 SW_MOD()->SetView(pModuleView);
365 else
366 pTimer->Start();
367 return 0;
369 /* -----------------------------27.12.99 09:59--------------------------------
371 ---------------------------------------------------------------------------*/
372 void SwOneExampleFrame::ClearDocument( BOOL bStartUpdateTimer )
374 uno::Reference< lang::XUnoTunnel> xTunnel( _xCursor, uno::UNO_QUERY);
375 if( xTunnel.is() )
377 OTextCursorHelper* pCrsr = reinterpret_cast<OTextCursorHelper*>(xTunnel->getSomething(
378 OTextCursorHelper::getUnoTunnelId()) );
379 if( pCrsr )
381 SwDoc* pDoc = pCrsr->GetDoc();
382 SwEditShell* pSh = pDoc->GetEditShell();
383 pSh->LockPaint();
384 pSh->StartAllAction();
385 pDoc->ClearDoc();
387 if( aLoadedTimer.IsActive() || !bStartUpdateTimer )
389 pSh->EndAllAction();
390 pSh->UnlockPaint();
392 if( bStartUpdateTimer )
393 aLoadedTimer.Start();
395 else
397 _xCursor->gotoStart(FALSE);
398 _xCursor->gotoEnd(TRUE);
399 _xCursor->setString(OUString());
403 /* -----------------------------15.12.99 11:09--------------------------------
405 ---------------------------------------------------------------------------*/
406 static const sal_Int16 nZoomValues[] =
414 //---------------------------------------------------------------------------
415 #define ITEM_UP 100
416 #define ITEM_DOWN 200
417 #define ITEM_ZOOM 300
419 void SwOneExampleFrame::CreatePopup(const Point& rPt)
421 PopupMenu aPop;
422 PopupMenu aSubPop1;
423 ResStringArray& rArr = aMenuRes.GetMenuArray();
425 aPop.InsertItem(ITEM_UP, rArr.GetString(rArr.FindIndex(ST_MENU_UP )));
426 aPop.InsertItem(ITEM_DOWN, rArr.GetString(rArr.FindIndex(ST_MENU_DOWN )));
428 Link aSelLk = LINK(this, SwOneExampleFrame, PopupHdl );
429 aPop.SetSelectHdl(aSelLk);
430 if(EX_SHOW_ONLINE_LAYOUT == nStyleFlags)
432 aPop.InsertItem(ITEM_ZOOM, rArr.GetString(rArr.FindIndex(ST_MENU_ZOOM )));
434 uno::Reference< view::XViewSettingsSupplier > xSettings(_xController, uno::UNO_QUERY);
435 uno::Reference< beans::XPropertySet > xViewProps = xSettings->getViewSettings();
437 uno::Any aZoom = xViewProps->getPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_ZOOM_VALUE)));
438 sal_Int16 nZoom = 0;
439 aZoom >>= nZoom;
441 for(sal_uInt16 i = 0; i < 5; i++ )
443 String sTemp;
444 sTemp = String::CreateFromInt32(nZoomValues[i]);
445 sTemp += String::CreateFromAscii(" %");
446 aSubPop1.InsertItem( ITEM_ZOOM + i + 1, sTemp);
447 if(nZoom == nZoomValues[i])
448 aSubPop1.CheckItem(ITEM_ZOOM + i + 1);
450 aPop.SetPopupMenu( ITEM_ZOOM, &aSubPop1 );
451 aSubPop1.SetSelectHdl(aSelLk);
453 aPop.Execute( &aTopWindow, rPt );
456 /* -----------------------------15.12.99 11:09--------------------------------
458 ---------------------------------------------------------------------------*/
459 IMPL_LINK(SwOneExampleFrame, PopupHdl, Menu*, pMenu )
461 sal_uInt16 nId = pMenu->GetCurItemId();
462 if( nId > ITEM_ZOOM && nId < ITEM_ZOOM + 100 )
464 sal_Int16 nZoom = nZoomValues[nId - ITEM_ZOOM - 1];
465 uno::Reference< view::XViewSettingsSupplier > xSettings(_xController, uno::UNO_QUERY);
466 uno::Reference< beans::XPropertySet > xViewProps = xSettings->getViewSettings();
468 uno::Any aZoom;
469 aZoom <<= nZoom;
470 xViewProps->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_ZOOM_VALUE)), aZoom);
471 aZoom <<= (sal_Int16)view::DocumentZoomType::BY_VALUE;
472 xViewProps->setPropertyValue(C2U(SW_PROP_NAME_STR(UNO_NAME_ZOOM_TYPE)), aZoom);
474 else if(ITEM_UP == nId || ITEM_DOWN == nId)
476 uno::Reference< text::XTextViewCursorSupplier > xCrsrSupp(_xController, uno::UNO_QUERY);
477 uno::Reference< view::XScreenCursor > xScrCrsr(xCrsrSupp->getViewCursor(), uno::UNO_QUERY);
478 if(ITEM_UP == nId)
479 xScrCrsr->screenUp();
480 else
481 xScrCrsr->screenDown();
483 return 0;
485 /* -----------------------------15.12.99 10:37--------------------------------
487 ---------------------------------------------------------------------------*/
488 SwFrmCtrlWindow::SwFrmCtrlWindow(Window* pParent, WinBits nBits,
489 SwOneExampleFrame* pFrame) :
490 Window(pParent, nBits),
491 pExampleFrame(pFrame)
494 /* -----------------------------15.12.99 09:57--------------------------------
496 ---------------------------------------------------------------------------*/
497 void SwFrmCtrlWindow::Command( const CommandEvent& rCEvt )
499 switch ( rCEvt.GetCommand() )
501 case COMMAND_CONTEXTMENU:
503 //#125881# quickly clicking crashes because the control is not fully initialized
504 if(pExampleFrame->GetController().is())
505 pExampleFrame->CreatePopup(rCEvt.GetMousePosPixel());
507 break;
508 case COMMAND_WHEEL:
509 case COMMAND_STARTAUTOSCROLL:
510 case COMMAND_AUTOSCROLL:
511 break;
512 default:;
515 /* -----------------------------15.12.99 12:57--------------------------------
517 ---------------------------------------------------------------------------*/
518 MenuResource::MenuResource(const ResId& rResId) :
519 Resource(rResId),
520 aMenuArray(ResId(1,*rResId.GetResMgr()))
522 FreeResource();