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 #include <querycontainerwindow.hxx>
21 #include <QueryDesignView.hxx>
22 #include <vcl/svapp.hxx>
23 #include <vcl/settings.hxx>
24 #include <JoinController.hxx>
25 #include <toolkit/helper/vclunohelper.hxx>
26 #include <stringconstants.hxx>
27 #include <strings.hxx>
28 #include <sfx2/sfxsids.hrc>
29 #include <vcl/event.hxx>
30 #include <vcl/fixed.hxx>
31 #include <UITools.hxx>
32 #include <com/sun/star/beans/XPropertySet.hpp>
33 #include <com/sun/star/frame/Frame.hpp>
34 #include <com/sun/star/util/XCloseable.hpp>
35 #include <memory>
36 
37 namespace dbaui
38 {
39 
40     using namespace ::com::sun::star::uno;
41     using namespace ::com::sun::star::lang;
42     using namespace ::com::sun::star::frame;
43     using namespace ::com::sun::star::beans;
44 
45     // OQueryContainerWindow
OQueryContainerWindow(vcl::Window * pParent,OQueryController & _rController,const Reference<XComponentContext> & _rxContext)46     OQueryContainerWindow::OQueryContainerWindow(vcl::Window* pParent, OQueryController& _rController,const Reference< XComponentContext >& _rxContext)
47         :ODataView( pParent, _rController, _rxContext )
48         ,m_pViewSwitch(nullptr)
49         ,m_pBeamer(nullptr)
50     {
51         m_pViewSwitch = new OQueryViewSwitch( this, _rController, _rxContext );
52 
53         m_pSplitter = VclPtr<Splitter>::Create(this,WB_VSCROLL);
54         m_pSplitter->Hide();
55         m_pSplitter->SetSplitHdl( LINK( this, OQueryContainerWindow, SplitHdl ) );
56         m_pSplitter->SetBackground( Wallpaper( Application::GetSettings().GetStyleSettings().GetDialogColor() ) );
57     }
~OQueryContainerWindow()58     OQueryContainerWindow::~OQueryContainerWindow()
59     {
60         disposeOnce();
61     }
dispose()62     void OQueryContainerWindow::dispose()
63     {
64         {
65             std::unique_ptr<OQueryViewSwitch> aTemp(m_pViewSwitch);
66             m_pViewSwitch = nullptr;
67         }
68         if ( m_pBeamer )
69             ::dbaui::notifySystemWindow(this,m_pBeamer,::comphelper::mem_fun(&TaskPaneList::RemoveWindow));
70         m_pBeamer.clear();
71         if ( m_xBeamer.is() )
72         {
73             Reference< css::util::XCloseable > xCloseable(m_xBeamer,UNO_QUERY);
74             m_xBeamer = nullptr;
75             if(xCloseable.is())
76                 xCloseable->close(false); // false - holds the ownership of this frame
77         }
78 
79         m_pSplitter.disposeAndClear();
80         ODataView::dispose();
81     }
switchView(::dbtools::SQLExceptionInfo * _pErrorInfo)82     bool OQueryContainerWindow::switchView( ::dbtools::SQLExceptionInfo* _pErrorInfo )
83     {
84         return m_pViewSwitch->switchView( _pErrorInfo );
85     }
86 
forceInitialView()87     void OQueryContainerWindow::forceInitialView()
88     {
89         return m_pViewSwitch->forceInitialView();
90     }
91 
resizeAll(const tools::Rectangle & _rPlayground)92     void OQueryContainerWindow::resizeAll( const tools::Rectangle& _rPlayground )
93     {
94         tools::Rectangle aPlayground( _rPlayground );
95 
96         if ( m_pBeamer && m_pBeamer->IsVisible() )
97         {
98             // calc pos and size of the splitter
99             Point aSplitPos     = m_pSplitter->GetPosPixel();
100             Size aSplitSize     = m_pSplitter->GetOutputSizePixel();
101             aSplitSize.setWidth( aPlayground.GetWidth() );
102 
103             if ( aSplitPos.Y() <= aPlayground.Top() )
104                 aSplitPos.setY( aPlayground.Top() + sal_Int32( aPlayground.GetHeight() * 0.2 ) );
105 
106             if ( aSplitPos.Y() + aSplitSize.Height() > aPlayground.GetHeight() )
107                 aSplitPos.setY( aPlayground.GetHeight() - aSplitSize.Height() );
108 
109             // set pos and size of the splitter
110             m_pSplitter->SetPosSizePixel( aSplitPos, aSplitSize );
111             m_pSplitter->SetDragRectPixel(  aPlayground );
112 
113             // set pos and size of the beamer
114             Size aBeamerSize( aPlayground.GetWidth(), aSplitPos.Y() );
115             m_pBeamer->SetPosSizePixel( aPlayground.TopLeft(), aBeamerSize );
116 
117             // shrink the playground by the size which is occupied by the beamer
118             aPlayground.SetTop( aSplitPos.Y() + aSplitSize.Height() );
119         }
120 
121         ODataView::resizeAll( aPlayground );
122     }
123 
resizeDocumentView(tools::Rectangle & _rPlayground)124     void OQueryContainerWindow::resizeDocumentView( tools::Rectangle& _rPlayground )
125     {
126         m_pViewSwitch->SetPosSizePixel( _rPlayground.TopLeft(), Size( _rPlayground.GetWidth(), _rPlayground.GetHeight() ) );
127 
128         ODataView::resizeDocumentView( _rPlayground );
129     }
130 
GetFocus()131     void OQueryContainerWindow::GetFocus()
132     {
133         ODataView::GetFocus();
134         if(m_pViewSwitch)
135             m_pViewSwitch->GrabFocus();
136     }
IMPL_LINK_NOARG(OQueryContainerWindow,SplitHdl,Splitter *,void)137     IMPL_LINK_NOARG( OQueryContainerWindow, SplitHdl, Splitter*, void )
138     {
139         m_pSplitter->SetPosPixel( Point( m_pSplitter->GetPosPixel().X(),m_pSplitter->GetSplitPosPixel() ) );
140         Resize();
141     }
142 
Construct()143     void OQueryContainerWindow::Construct()
144     {
145         m_pViewSwitch->Construct();
146     }
147 
disposingPreview()148     void OQueryContainerWindow::disposingPreview()
149     {
150         if ( m_pBeamer )
151         {
152             // here I know that we will be destroyed from the frame
153             ::dbaui::notifySystemWindow(this,m_pBeamer,::comphelper::mem_fun(&TaskPaneList::RemoveWindow));
154             m_pBeamer = nullptr;
155             m_xBeamer = nullptr;
156             m_pSplitter->Hide();
157             Resize();
158         }
159     }
PreNotify(NotifyEvent & rNEvt)160     bool OQueryContainerWindow::PreNotify( NotifyEvent& rNEvt )
161     {
162         if (rNEvt.GetType() == MouseNotifyEvent::GETFOCUS && m_pViewSwitch)
163         {
164             OJoinController& rController = m_pViewSwitch->getDesignView()->getController();
165             rController.InvalidateFeature(SID_CUT);
166             rController.InvalidateFeature(SID_COPY);
167             rController.InvalidateFeature(SID_PASTE);
168         }
169         return ODataView::PreNotify(rNEvt);
170     }
showPreview(const Reference<XFrame> & _xFrame)171     void OQueryContainerWindow::showPreview(const Reference<XFrame>& _xFrame)
172     {
173         if(!m_pBeamer)
174         {
175             m_pBeamer = VclPtr<OBeamer>::Create(this);
176 
177             ::dbaui::notifySystemWindow(this,m_pBeamer,::comphelper::mem_fun(&TaskPaneList::AddWindow));
178 
179             m_xBeamer = Frame::create( m_pViewSwitch->getORB() );
180             m_xBeamer->initialize( VCLUnoHelper::GetInterface ( m_pBeamer ) );
181 
182             // notify layout manager to not create internal toolbars
183             try
184             {
185                 Reference < XPropertySet > xLMPropSet(m_xBeamer->getLayoutManager(), UNO_QUERY);
186                 if ( xLMPropSet.is() )
187                 {
188                     const OUString aAutomaticToolbars( "AutomaticToolbars" );
189                     xLMPropSet->setPropertyValue( aAutomaticToolbars, Any( false ));
190                 }
191             }
192             catch( Exception& )
193             {
194             }
195 
196             m_xBeamer->setName(FRAME_NAME_QUERY_PREVIEW);
197 
198             // append our frame
199             Reference < XFramesSupplier > xSup(_xFrame,UNO_QUERY);
200             Reference < XFrames > xFrames = xSup->getFrames();
201             xFrames->append( Reference<XFrame>(m_xBeamer,UNO_QUERY_THROW) );
202 
203             Size aSize = GetOutputSizePixel();
204             Size aBeamer(aSize.Width(),sal_Int32(aSize.Height()*0.33));
205 
206             const long  nFrameHeight = LogicToPixel(Size(0, 3), MapMode(MapUnit::MapAppFont)).Height();
207             Point aPos(0,aBeamer.Height()+nFrameHeight);
208 
209             m_pBeamer->SetPosSizePixel(Point(0,0),aBeamer);
210             m_pBeamer->Show();
211 
212             m_pSplitter->SetPosSizePixel( Point(0,aBeamer.Height()), Size(aSize.Width(),nFrameHeight) );
213             // a default pos for the splitter, so that the listbox is about 80 (logical) pixels wide
214             m_pSplitter->SetSplitPosPixel( aBeamer.Height() );
215             m_pViewSwitch->SetPosSizePixel(aPos,Size(aBeamer.Width(),aSize.Height() - aBeamer.Height()-nFrameHeight));
216 
217             m_pSplitter->Show();
218 
219             Resize();
220         }
221     }
222 
223 }   // namespace dbaui
224 
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
226