LibreOffice Module vcl (master)  1
previewer.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  */
9 
10 #include <com/sun/star/lang/XComponent.hpp>
11 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
12 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
13 #include <com/sun/star/ucb/UniversalContentBroker.hpp>
15 #include <cppuhelper/bootstrap.hxx>
16 #include <osl/file.hxx>
17 #include <vcl/builder.hxx>
18 #include <vcl/toolkit/dialog.hxx>
19 #include <vcl/help.hxx>
20 #include <vcl/svapp.hxx>
21 #include <vcl/vclmain.hxx>
22 
23 namespace {
24 
25 class UIPreviewApp : public Application
26 {
27 public:
28  virtual void Init() override;
29  virtual int Main() override;
30 };
31 
32 }
33 
34 using namespace com::sun::star;
35 
36 void UIPreviewApp::Init()
37 {
38  uno::Reference<uno::XComponentContext> xContext =
40  uno::Reference<lang::XMultiComponentFactory> xFactory =
41  xContext->getServiceManager();
42  uno::Reference<lang::XMultiServiceFactory> xSFactory(xFactory, uno::UNO_QUERY_THROW);
44 
45  // Create UCB (for backwards compatibility, in case some code still uses
46  // plain createInstance w/o args directly to obtain an instance):
47  ::ucb::UniversalContentBroker::create(
49 }
50 
51 int UIPreviewApp::Main()
52 {
53  std::vector<OUString> uifiles;
54  for (sal_uInt16 i = 0; i < GetCommandLineParamCount(); ++i)
55  {
56  OUString aFileUrl;
57  osl::File::getFileURLFromSystemPath(GetCommandLineParam(i), aFileUrl);
58  uifiles.push_back(aFileUrl);
59  }
60 
61  if (uifiles.empty())
62  {
63  fprintf(stderr, "Usage: ui-previewer file.ui\n");
64  return EXIT_FAILURE;
65  }
66 
67  // turn on tooltips
69 
70  int nRet = EXIT_SUCCESS;
71 
72  try
73  {
74  VclPtrInstance<Dialog> pDialog(nullptr, WB_STDDIALOG | WB_SIZEABLE, Dialog::InitFlag::NoParent);
75  {
76  VclBuilder aBuilder(pDialog, OUString(), uifiles[0]);
77  vcl::Window *pRoot = aBuilder.get_widget_root();
78  Dialog *pRealDialog = dynamic_cast<Dialog*>(pRoot);
79 
80  if (!pRealDialog)
81  pRealDialog = pDialog;
82 
83  pRealDialog->SetText("LibreOffice ui-previewer");
84  pRealDialog->SetStyle(pDialog->GetStyle()|WB_CLOSEABLE);
85  /*
86  Force a new StateChangedType::InitShow for the edge case where pRoot
87  is not a dialog or contents of a dialog, but instead a visible floating window
88  which may have had initshow already done before it was given children
89  */
90  pRoot->Hide();
91  pRoot->Show();
92  pRealDialog->Execute();
93  }
94 
95  pDialog.disposeAndClear();
96  }
97  catch (const uno::Exception &e)
98  {
99  fprintf(stderr, "fatal error: %s\n", OUStringToOString(e.Message, osl_getThreadTextEncoding()).getStr());
100  nRet = EXIT_FAILURE;
101  }
102  catch (const std::exception &e)
103  {
104  fprintf(stderr, "fatal error: %s\n", e.what());
105  nRet = EXIT_FAILURE;
106  }
107 
108  return nRet;
109 }
110 
112 {
113  static UIPreviewApp aApp;
114 }
115 
116 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Creates a hierarchy of vcl::Windows (widgets) from a .ui file for dialogs, sidebar, etc.
Definition: builder.hxx:64
void Hide()
Definition: window.hxx:936
void createApplication()
Definition: previewer.cxx:111
int i
CPPUHELPER_DLLPUBLIC css::uno::Reference< css::uno::XComponentContext > SAL_CALL defaultBootstrap_InitialComponentContext()
Base class used mainly for the LibreOffice Desktop class.
Definition: svapp.hxx:237
void SetStyle(WinBits nStyle)
Definition: window.cxx:1979
short Execute()
Definition: dialog.cxx:1011
void setProcessServiceFactory(const Reference< XMultiServiceFactory > &xSMgr)
OString OUStringToOString(const OUString &str, ConnectionSettings const *settings)
A construction helper for a temporary VclPtr.
Definition: vclptr.hxx:275
virtual void Init()
Initialize the application itself.
Definition: svapp.cxx:194
virtual void SetText(const OUString &rStr) override
Definition: syswin.cxx:1103
Reference< XComponentContext > getProcessComponentContext()
static void EnableQuickHelp()
Definition: help.cxx:165
Reference< XSingleServiceFactory > xFactory
WinBits const WB_CLOSEABLE
void Show(bool bVisible=true, ShowFlags nFlags=ShowFlags::NONE)
Definition: window.cxx:2204
virtual int Main()
Pure virtual entrypoint to the application.
Definition: svapp.cxx:173