1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include <swtypes.hxx>
22 #include <globals.hrc>
26 #include <unotools.hrc>
27 #include <unotools.hxx>
28 #include <unoprnms.hxx>
29 #include <osl/diagnose.h>
30 #include <vcl/msgbox.hxx>
31 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
32 #include <com/sun/star/view/XScreenCursor.hpp>
33 #include <com/sun/star/view/DocumentZoomType.hpp>
34 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
35 #include <com/sun/star/style/XStyle.hpp>
36 #include <com/sun/star/text/XTextDocument.hpp>
37 #include <com/sun/star/awt/Toolkit.hpp>
38 #include <com/sun/star/awt/PosSize.hpp>
39 #include <com/sun/star/view/XViewSettingsSupplier.hpp>
40 #include <com/sun/star/container/XNameContainer.hpp>
41 #include <com/sun/star/frame/XLayoutManager.hpp>
42 #include <comphelper/processfactory.hxx>
43 #include <sfx2/dispatch.hxx>
44 #include <svl/stritem.hxx>
45 #include <shellio.hxx>
49 #include <swmodule.hxx>
50 #include <TextCursorHelper.hxx>
51 #include <unocrsr.hxx>
56 using namespace ::com::sun::star
;
58 const sal_Char cFrameControl
[] = "com.sun.star.frame.FrameControl";
59 const sal_Char cFactory
[] = "private:factory/swriter";
61 bool SwOneExampleFrame::bShowServiceNotAvailableMessage
= true;
63 SwOneExampleFrame::SwOneExampleFrame( Window
& rWin
,
65 const Link
* pInitializedLink
,
67 aTopWindow(&rWin
, this),
68 aMenuRes(SW_RES(RES_FRMEX_MENU
)),
69 pModuleView(SW_MOD()->GetView()),
71 bIsInitialized(sal_False
),
72 bServiceAvailable(sal_False
)
74 if (pURL
&& pURL
->Len())
77 aTopWindow
.SetPosSizePixel(Point(0, 0), rWin
.GetSizePixel());
79 if( pInitializedLink
)
80 aInitializedLink
= *pInitializedLink
;
82 // the controller is asynchronously set
83 aLoadedTimer
.SetTimeoutHdl(LINK(this, SwOneExampleFrame
, TimeoutHdl
));
84 aLoadedTimer
.SetTimeout(200);
91 void SwOneExampleFrame::CreateErrorMessage(Window
* pParent
)
93 if(SwOneExampleFrame::bShowServiceNotAvailableMessage
)
95 String
sInfo(SW_RES(STR_SERVICE_UNAVAILABLE
));
96 sInfo
+= OUString(cFrameControl
);
97 InfoBox(pParent
, sInfo
).Execute();
98 SwOneExampleFrame::bShowServiceNotAvailableMessage
= false;
102 SwOneExampleFrame::~SwOneExampleFrame()
107 void SwOneExampleFrame::CreateControl()
111 uno::Reference
< lang::XMultiServiceFactory
>
112 xMgr
= comphelper::getProcessServiceFactory();
113 uno::Reference
< uno::XComponentContext
> xContext
= comphelper::getProcessComponentContext();
114 uno::Reference
< uno::XInterface
> xInst
= xMgr
->createInstance( "com.sun.star.frame.FrameControl" );
115 _xControl
= uno::Reference
< awt::XControl
>(xInst
, uno::UNO_QUERY
);
118 uno::Reference
< awt::XWindowPeer
> xParent( aTopWindow
.GetComponentInterface() );
120 uno::Reference
< awt::XToolkit
> xToolkit( awt::Toolkit::create(xContext
), uno::UNO_QUERY_THROW
);
122 _xControl
->createPeer( xToolkit
, xParent
);
124 uno::Reference
< awt::XWindow
> xWin( _xControl
, uno::UNO_QUERY
);
125 xWin
->setVisible(sal_False
);
126 Size
aWinSize(aTopWindow
.GetOutputSizePixel());
127 xWin
->setPosSize( 0, 0, aWinSize
.Width(), aWinSize
.Height(), awt::PosSize::SIZE
);
129 uno::Reference
< beans::XPropertySet
> xPrSet(xInst
, uno::UNO_QUERY
);
132 OUString
sTempURL(cFactory
);
133 if(sArgumentURL
.Len())
134 sTempURL
= sArgumentURL
;
137 uno::Sequence
<beans::PropertyValue
> aSeq(3);
138 beans::PropertyValue
* pValues
= aSeq
.getArray();
139 pValues
[0].Name
= "ReadOnly";
140 sal_Bool bTrue
= sal_True
;
141 pValues
[0].Value
.setValue(&bTrue
, ::getBooleanCppuType());
142 pValues
[1].Name
= "OpenFlags";
143 pValues
[1].Value
<<= OUString("-RB");
144 pValues
[2].Name
= "Referer";
145 pValues
[2].Value
<<= OUString("private:user");
147 aArgs
.setValue(&aSeq
, ::getCppuType((uno::Sequence
<beans::PropertyValue
>*)0));
149 xPrSet
->setPropertyValue( "LoaderArguments", aArgs
);
150 //save and set readonly???
152 xPrSet
->setPropertyValue("ComponentURL", aURL
);
154 aLoadedTimer
.Start();
155 bServiceAvailable
= sal_True
;
159 void SwOneExampleFrame::DisposeControl()
163 _xControl
->dispose();
169 static void disableScrollBars(uno::Reference
< beans::XPropertySet
> xViewProps
,
170 bool bEnableOnlineMode
)
172 //the scrollbar logic is kind of busted looking in writer, when the hori scrollbar
173 //property is changed then the hori scrollbar is enabled if the property is
174 //true or browse (online) mode is enabled. So...
175 //disable online mode
176 //turn off scrollbars
177 //turn back on online mode if that's what we want
178 //which subverts the (dodgy/buggy) scrollbar setting
180 //To reproduce this problem, in edit->autotext and click through
181 //the examples and see if the preview gets a horizontal scrollbar
182 uno::Any
aFalseSet(uno::makeAny(sal_False
));
183 xViewProps
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_SHOW_ONLINE_LAYOUT
)), aFalseSet
);
185 xViewProps
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_SHOW_HORI_SCROLL_BAR
)), aFalseSet
);
186 xViewProps
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_SHOW_VERT_SCROLL_BAR
)), aFalseSet
);
188 if (bEnableOnlineMode
)
190 xViewProps
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_SHOW_ONLINE_LAYOUT
)), uno::makeAny(sal_True
));
194 IMPL_LINK( SwOneExampleFrame
, TimeoutHdl
, Timer
*, pTimer
)
200 uno::Reference
< beans::XPropertySet
> xPrSet(_xControl
, uno::UNO_QUERY
);
201 uno::Any aFrame
= xPrSet
->getPropertyValue("Frame");
202 uno::Reference
< frame::XFrame
> xFrm
;
205 uno::Reference
< beans::XPropertySet
> xPropSet( xFrm
, uno::UNO_QUERY
);
210 uno::Reference
< frame::XLayoutManager
> xLayoutManager
;
211 uno::Any aValue
= xPropSet
->getPropertyValue("LayoutManager");
212 aValue
>>= xLayoutManager
;
213 if ( xLayoutManager
.is() )
214 xLayoutManager
->setVisible( sal_False
);
216 catch (const uno::Exception
&)
221 _xController
= xFrm
->getController();
222 if(_xController
.is())
224 _xModel
= _xController
->getModel();
225 //now the ViewOptions should be set properly
226 uno::Reference
< view::XViewSettingsSupplier
> xSettings(_xController
, uno::UNO_QUERY
);
227 uno::Reference
< beans::XPropertySet
> xViewProps
= xSettings
->getViewSettings();
229 sal_Bool bTrue
= sal_True
;
230 sal_Bool bFalse
= sal_False
;
231 uno::Any
aTrueSet( &bTrue
, ::getBooleanCppuType() );
232 uno::Any
aFalseSet( &bFalse
, ::getBooleanCppuType() );
234 if( !bIsInitialized
)
236 xViewProps
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_SHOW_BREAKS
)), aFalseSet
);
237 xViewProps
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_SHOW_DRAWINGS
)), aTrueSet
);
238 xViewProps
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_SHOW_FIELD_COMMANDS
)), aFalseSet
);
239 xViewProps
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_SHOW_GRAPHICS
)), aTrueSet
);
240 xViewProps
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_SHOW_HIDDEN_PARAGRAPHS
)), aFalseSet
);
241 xViewProps
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_SHOW_HIDDEN_TEXT
)), aFalseSet
);
242 xViewProps
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_SHOW_HORI_RULER
)), aFalseSet
);
243 xViewProps
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_SHOW_PARA_BREAKS
)), aFalseSet
);
244 xViewProps
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_SHOW_PROTECTED_SPACES
)), aFalseSet
);
245 xViewProps
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_SHOW_SOFT_HYPHENS
)), aFalseSet
);
246 xViewProps
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_SHOW_SPACES
)), aFalseSet
);
247 xViewProps
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_SHOW_TABLES
)), aTrueSet
);
248 xViewProps
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_SHOW_TABSTOPS
)), aFalseSet
);
249 xViewProps
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_SHOW_VERT_RULER
)), aFalseSet
);
251 if(0 ==(nStyleFlags
&EX_SHOW_ONLINE_LAYOUT
))
254 aZoom
<<= (sal_Int16
)view::DocumentZoomType::PAGE_WIDTH_EXACT
;
255 xViewProps
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_ZOOM_TYPE
)), aZoom
);
260 aZoom
<<= (sal_Int16
)view::DocumentZoomType::BY_VALUE
;
261 xViewProps
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_ZOOM_TYPE
)), aZoom
);
263 sal_Int16 nZoomValue
= 50;
264 if(EX_SHOW_BUSINESS_CARDS
== nStyleFlags
)
268 aZoom
<<= nZoomValue
;
269 xViewProps
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_ZOOM_VALUE
)), aZoom
);
272 // set onlinelayout property after setting the zoom
273 disableScrollBars(xViewProps
, nStyleFlags
&EX_SHOW_ONLINE_LAYOUT
);
274 bIsInitialized
= sal_True
;
277 uno::Reference
< text::XTextDocument
> xDoc(_xModel
, uno::UNO_QUERY
);
278 uno::Reference
< text::XText
> xText
= xDoc
->getText();
279 _xCursor
= xText
->createTextCursor();
281 //From here, a cursor is defined, which goes through the template,
282 //and overwrites the template words where it is necessary.
284 uno::Reference
< lang::XUnoTunnel
> xTunnel( _xCursor
, uno::UNO_QUERY
);
287 OTextCursorHelper
* pCrsr
= reinterpret_cast<OTextCursorHelper
*>( xTunnel
->getSomething(
288 OTextCursorHelper::getUnoTunnelId() ));
291 SwEditShell
* pSh
= pCrsr
->GetDoc()->GetEditShell();
295 if (pSh
->GetCurWord() == String("HEADING1"))
297 pSh
->Overwrite(SW_RESSTR(STR_IDXEXAMPLE_IDXTXT_HEADING1
));
299 else if (pSh
->GetCurWord() == String("ENTRY1"))
301 pSh
->Overwrite(SW_RESSTR(STR_IDXEXAMPLE_IDXTXT_ENTRY1
));
303 else if (pSh
->GetCurWord() == String("HEADING11"))
305 pSh
->Overwrite(SW_RESSTR(STR_IDXEXAMPLE_IDXTXT_HEADING11
));
307 else if (pSh
->GetCurWord() == String("ENTRY11"))
309 pSh
->Overwrite(SW_RESSTR(STR_IDXEXAMPLE_IDXTXT_ENTRY11
));
311 else if (pSh
->GetCurWord() == String("HEADING12"))
313 pSh
->Overwrite(SW_RESSTR(STR_IDXEXAMPLE_IDXTXT_HEADING12
));
315 else if (pSh
->GetCurWord() == String("ENTRY12"))
317 pSh
->Overwrite(SW_RESSTR(STR_IDXEXAMPLE_IDXTXT_ENTRY12
));
319 else if (pSh
->GetCurWord() == String("TABLE1"))
321 pSh
->Overwrite(SW_RESSTR(STR_IDXEXAMPLE_IDXTXT_TABLE1
));
323 else if (pSh
->GetCurWord() == String("IMAGE1"))
325 pSh
->Overwrite(SW_RESSTR(STR_IDXEXAMPLE_IDXTXT_IMAGE1
));
330 while(pSh
->Right(sal_uInt16(1), sal_uInt16(1), sal_True
) == sal_True
);
334 uno::Reference
< beans::XPropertySet
> xCrsrProp(_xCursor
, uno::UNO_QUERY
);
335 uno::Any aPageStyle
= xCrsrProp
->getPropertyValue(
336 OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_PAGE_STYLE_NAME
)));
338 aPageStyle
>>= sPageStyle
;
340 uno::Reference
< style::XStyleFamiliesSupplier
> xSSupp( xDoc
, uno::UNO_QUERY
);
341 uno::Reference
< container::XNameAccess
> xStyles
= xSSupp
->getStyleFamilies();
342 uno::Any aPFamily
= xStyles
->getByName( "PageStyles" );
343 uno::Reference
< container::XNameContainer
> xPFamily
;
345 if( 0 == (EX_SHOW_DEFAULT_PAGE
== nStyleFlags
)
346 && (aPFamily
>>= xPFamily
) && !sPageStyle
.isEmpty() )
348 uno::Any aPStyle
= xPFamily
->getByName( sPageStyle
);
349 uno::Reference
< style::XStyle
> xPStyle
;
351 uno::Reference
< beans::XPropertySet
> xPProp(xPStyle
, uno::UNO_QUERY
);
352 uno::Any aSize
= xPProp
->getPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_SIZE
)));
355 //TODO: set page width to card width
356 aPSize
.Width
= 10000;
357 aSize
.setValue(&aPSize
, ::getCppuType((awt::Size
*)0));
358 xPProp
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_SIZE
)), aSize
);
360 uno::Any aZero
; aZero
<<= (sal_Int32
)0;
361 xPProp
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_LEFT_MARGIN
)), aZero
);
362 xPProp
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_RIGHT_MARGIN
)), aZero
);
365 uno::Reference
< awt::XWindow
> xWin( _xControl
, uno::UNO_QUERY
);
366 Size
aWinSize(aTopWindow
.GetOutputSizePixel());
367 xWin
->setPosSize( 0, 0, aWinSize
.Width(), aWinSize
.Height(), awt::PosSize::SIZE
);
369 // can only be done here - the SFX changes the ScrollBar values
370 disableScrollBars(xViewProps
, nStyleFlags
&EX_SHOW_ONLINE_LAYOUT
);
372 if (aInitializedLink
.IsSet())
373 aInitializedLink
.Call(this);
375 uno::Reference
< text::XTextViewCursorSupplier
> xCrsrSupp(_xController
, uno::UNO_QUERY
);
376 uno::Reference
< view::XScreenCursor
> xScrCrsr(xCrsrSupp
->getViewCursor(), uno::UNO_QUERY
);
378 xScrCrsr
->screenUp();
380 xWin
->setVisible( sal_True
);
385 OTextCursorHelper
* pCrsr
= reinterpret_cast<OTextCursorHelper
*>( xTunnel
->getSomething(
386 OTextCursorHelper::getUnoTunnelId() ));
389 SwEditShell
* pSh
= pCrsr
->GetDoc()->GetEditShell();
390 if( pSh
->ActionCount() )
398 SW_MOD()->SetView(pModuleView
);
405 void SwOneExampleFrame::ClearDocument( sal_Bool bStartUpdateTimer
)
407 uno::Reference
< lang::XUnoTunnel
> xTunnel( _xCursor
, uno::UNO_QUERY
);
410 OTextCursorHelper
* pCrsr
= reinterpret_cast<OTextCursorHelper
*>(xTunnel
->getSomething(
411 OTextCursorHelper::getUnoTunnelId()) );
414 SwDoc
* pDoc
= pCrsr
->GetDoc();
415 SwEditShell
* pSh
= pDoc
->GetEditShell();
417 pSh
->StartAllAction();
420 if( aLoadedTimer
.IsActive() || !bStartUpdateTimer
)
425 if( bStartUpdateTimer
)
426 aLoadedTimer
.Start();
430 _xCursor
->gotoStart(sal_False
);
431 _xCursor
->gotoEnd(sal_True
);
432 _xCursor
->setString(OUString());
437 static const sal_Int16 nZoomValues
[] =
447 #define ITEM_DOWN 200
448 #define ITEM_ZOOM 300
450 void SwOneExampleFrame::CreatePopup(const Point
& rPt
)
454 ResStringArray
& rArr
= aMenuRes
.GetMenuArray();
456 aPop
.InsertItem(ITEM_UP
, rArr
.GetString(rArr
.FindIndex(ST_MENU_UP
)));
457 aPop
.InsertItem(ITEM_DOWN
, rArr
.GetString(rArr
.FindIndex(ST_MENU_DOWN
)));
459 Link aSelLk
= LINK(this, SwOneExampleFrame
, PopupHdl
);
460 aPop
.SetSelectHdl(aSelLk
);
461 if(EX_SHOW_ONLINE_LAYOUT
== nStyleFlags
)
463 aPop
.InsertItem(ITEM_ZOOM
, rArr
.GetString(rArr
.FindIndex(ST_MENU_ZOOM
)));
465 uno::Reference
< view::XViewSettingsSupplier
> xSettings(_xController
, uno::UNO_QUERY
);
466 uno::Reference
< beans::XPropertySet
> xViewProps
= xSettings
->getViewSettings();
468 uno::Any aZoom
= xViewProps
->getPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_ZOOM_VALUE
)));
472 for (sal_uInt16 i
= 0;
473 i
< (sizeof(nZoomValues
)/sizeof(nZoomValues
[0])); ++i
)
476 sTemp
= OUString::number(nZoomValues
[i
]);
477 sTemp
+= OUString(" %");
478 aSubPop1
.InsertItem( ITEM_ZOOM
+ i
+ 1, sTemp
);
479 if(nZoom
== nZoomValues
[i
])
480 aSubPop1
.CheckItem(ITEM_ZOOM
+ i
+ 1);
482 aPop
.SetPopupMenu( ITEM_ZOOM
, &aSubPop1
);
483 aSubPop1
.SetSelectHdl(aSelLk
);
485 aPop
.Execute( &aTopWindow
, rPt
);
489 IMPL_LINK(SwOneExampleFrame
, PopupHdl
, Menu
*, pMenu
)
491 sal_uInt16 nId
= pMenu
->GetCurItemId();
492 if ((nId
> ITEM_ZOOM
) &&
493 (nId
<= (ITEM_ZOOM
+ (sizeof(nZoomValues
)/sizeof(nZoomValues
[0])))))
495 sal_Int16 nZoom
= nZoomValues
[nId
- ITEM_ZOOM
- 1];
496 uno::Reference
< view::XViewSettingsSupplier
> xSettings(_xController
, uno::UNO_QUERY
);
497 uno::Reference
< beans::XPropertySet
> xViewProps
= xSettings
->getViewSettings();
501 xViewProps
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_ZOOM_VALUE
)), aZoom
);
502 aZoom
<<= (sal_Int16
)view::DocumentZoomType::BY_VALUE
;
503 xViewProps
->setPropertyValue(OUString::createFromAscii(SW_PROP_NAME_STR(UNO_NAME_ZOOM_TYPE
)), aZoom
);
505 else if(ITEM_UP
== nId
|| ITEM_DOWN
== nId
)
507 uno::Reference
< text::XTextViewCursorSupplier
> xCrsrSupp(_xController
, uno::UNO_QUERY
);
508 uno::Reference
< view::XScreenCursor
> xScrCrsr(xCrsrSupp
->getViewCursor(), uno::UNO_QUERY
);
510 xScrCrsr
->screenUp();
512 xScrCrsr
->screenDown();
517 SwFrmCtrlWindow::SwFrmCtrlWindow(Window
* pParent
, SwOneExampleFrame
* pFrame
)
518 : VclEventBox(pParent
)
519 , pExampleFrame(pFrame
)
525 void SwFrmCtrlWindow::Command( const CommandEvent
& rCEvt
)
527 switch ( rCEvt
.GetCommand() )
529 case COMMAND_CONTEXTMENU
:
531 //#125881# quickly clicking crashes because the control is not fully initialized
532 if(pExampleFrame
->GetController().is())
533 pExampleFrame
->CreatePopup(rCEvt
.GetMousePosPixel());
537 case COMMAND_STARTAUTOSCROLL
:
538 case COMMAND_AUTOSCROLL
:
544 Size
SwFrmCtrlWindow::GetOptimalSize() const
546 return LogicToPixel(Size(82, 124), MapMode(MAP_APPFONT
));
549 void SwFrmCtrlWindow::Resize()
551 VclEventBox::Resize();
552 pExampleFrame
->ClearDocument(true);
555 MenuResource::MenuResource(const ResId
& rResId
) :
557 aMenuArray(ResId(1,*rResId
.GetResMgr()))
562 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */