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  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 
21 #include "baside2.hxx"
22 #include <baside3.hxx>
23 #include <localizationmgr.hxx>
24 #include <dlgedview.hxx>
25 #include <xmlscript/xmldlg_imexp.hxx>
26 #include <sfx2/dispatch.hxx>
27 #include <sfx2/request.hxx>
28 #include <sfx2/sfxsids.hrc>
29 #include <sfx2/viewfrm.hxx>
30 #include <tools/diagnose_ex.h>
31 #include <comphelper/processfactory.hxx>
32 #include <com/sun/star/uno/XComponentContext.hpp>
33 
34 namespace basctl
35 {
36 
37 using namespace comphelper;
38 using namespace ::com::sun::star;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::io;
41 
CreateDlgWin(const ScriptDocument & rDocument,const OUString & rLibName,const OUString & rDlgName)42 VclPtr<DialogWindow> Shell::CreateDlgWin( const ScriptDocument& rDocument, const OUString& rLibName, const OUString& rDlgName )
43 {
44     bCreatingWindow = true;
45 
46     sal_uInt16 nKey = 0;
47     VclPtr<DialogWindow> pWin;
48     OUString aLibName( rLibName );
49     OUString aDlgName( rDlgName );
50 
51     if ( aLibName.isEmpty() )
52         aLibName = "Standard" ;
53 
54     rDocument.getOrCreateLibrary( E_DIALOGS, aLibName );
55 
56     if ( aDlgName.isEmpty() )
57         aDlgName = rDocument.createObjectName( E_DIALOGS, aLibName );
58 
59     // maybe there's a suspended one?
60     pWin = FindDlgWin( rDocument, aLibName, aDlgName, false, true );
61 
62     if ( !pWin )
63     {
64         try
65         {
66             Reference< io::XInputStreamProvider > xISP;
67             if ( rDocument.hasDialog( aLibName, aDlgName ) )
68                 rDocument.getDialog( aLibName, aDlgName, xISP );
69             else
70                 rDocument.createDialog( aLibName, aDlgName, xISP );
71 
72             if ( xISP.is() )
73             {
74                 // create dialog model
75                 Reference< XComponentContext > xContext = comphelper::getProcessComponentContext();
76                 Reference< container::XNameContainer > xDialogModel( xContext->getServiceManager()->createInstanceWithContext
77                     ( "com.sun.star.awt.UnoControlDialogModel", xContext ), UNO_QUERY );
78                 Reference< XInputStream > xInput( xISP->createInputStream() );
79                 ::xmlscript::importDialogModel( xInput, xDialogModel, xContext, rDocument.isDocument() ? rDocument.getDocument() : Reference< frame::XModel >() );
80                 LocalizationMgr::setStringResourceAtDialog( rDocument, rLibName, aDlgName, xDialogModel );
81 
82                 // new dialog window
83                 if (!pDialogLayout)
84                     pDialogLayout.reset(VclPtr<DialogWindowLayout>::Create(&GetViewFrame()->GetWindow(), *aObjectCatalog));
85                 pWin = VclPtr<DialogWindow>::Create(pDialogLayout.get(), rDocument, aLibName, aDlgName, xDialogModel);
86                 nKey = InsertWindowInTable( pWin );
87             }
88         }
89         catch (const uno::Exception& )
90         {
91             DBG_UNHANDLED_EXCEPTION("basctl.basicide");
92         }
93     }
94     else
95     {
96         pWin->SetStatus( pWin->GetStatus() & ~BASWIN_SUSPENDED );
97         nKey = GetWindowId( pWin );
98         DBG_ASSERT( nKey, "CreateDlgWin: No Key - Window not found!" );
99     }
100 
101     if( pWin )
102     {
103         pWin->GrabScrollBars( aHScrollBar.get(), aVScrollBar.get() );
104         pTabBar->InsertPage( nKey, aDlgName );
105         pTabBar->Sort();
106         if ( !pCurWin )
107             SetCurWindow( pWin, false, false );
108     }
109 
110     bCreatingWindow = false;
111     return pWin;
112 }
113 
FindDlgWin(ScriptDocument const & rDocument,OUString const & rLibName,OUString const & rName,bool bCreateIfNotExist,bool bFindSuspended)114 VclPtr<DialogWindow> Shell::FindDlgWin (
115     ScriptDocument const& rDocument,
116     OUString const& rLibName, OUString const& rName,
117     bool bCreateIfNotExist, bool bFindSuspended
118 )
119 {
120     if (VclPtr<BaseWindow> pWin = FindWindow(rDocument, rLibName, rName, TYPE_DIALOG, bFindSuspended))
121         return static_cast<DialogWindow*>(pWin.get());
122     return bCreateIfNotExist ? CreateDlgWin(rDocument, rLibName, rName) : nullptr;
123 }
124 
GetWindowId(const BaseWindow * pWin) const125 sal_uInt16 Shell::GetWindowId(const BaseWindow* pWin) const
126 {
127     for (auto const& window : aWindowTable)
128         if ( window.second == pWin )
129             return window.first;
130     return 0;
131 }
132 
GetCurDlgView() const133 SdrView* Shell::GetCurDlgView() const
134 {
135     if (DialogWindow* pDCurWin = dynamic_cast<DialogWindow*>(pCurWin.get()))
136         return &pDCurWin->GetView();
137     else
138         return nullptr;
139 }
140 
141 // only if dialogue window above:
ExecuteDialog(SfxRequest & rReq)142 void Shell::ExecuteDialog( SfxRequest& rReq )
143 {
144     if (pCurWin && (dynamic_cast<DialogWindow*>(pCurWin.get()) || rReq.GetSlot() == SID_IMPORT_DIALOG))
145         pCurWin->ExecuteCommand(rReq);
146 }
147 
148 } // namespace basctl
149 
150 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
151