1*5b190011SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*5b190011SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*5b190011SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*5b190011SAndrew Rist  * distributed with this work for additional information
6*5b190011SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*5b190011SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*5b190011SAndrew Rist  * "License"); you may not use this file except in compliance
9*5b190011SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*5b190011SAndrew Rist  *
11*5b190011SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*5b190011SAndrew Rist  *
13*5b190011SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*5b190011SAndrew Rist  * software distributed under the License is distributed on an
15*5b190011SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5b190011SAndrew Rist  * KIND, either express or implied.  See the License for the
17*5b190011SAndrew Rist  * specific language governing permissions and limitations
18*5b190011SAndrew Rist  * under the License.
19*5b190011SAndrew Rist  *
20*5b190011SAndrew Rist  *************************************************************/
21*5b190011SAndrew Rist 
22*5b190011SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "precompiled_sd.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include "ChildWindowPane.hxx"
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include "PaneDockingWindow.hxx"
29cdf0e10cSrcweir #include "ViewShellBase.hxx"
30cdf0e10cSrcweir #include "ViewShellManager.hxx"
31cdf0e10cSrcweir #include "framework/FrameworkHelper.hxx"
32cdf0e10cSrcweir #include <toolkit/helper/vclunohelper.hxx>
33cdf0e10cSrcweir #include <vcl/svapp.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using namespace ::com::sun::star;
36cdf0e10cSrcweir using namespace ::com::sun::star::uno;
37cdf0e10cSrcweir using namespace ::com::sun::star::drawing::framework;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir namespace sd { namespace framework {
40cdf0e10cSrcweir 
41cdf0e10cSrcweir 
ChildWindowPane(const Reference<XResourceId> & rxPaneId,sal_uInt16 nChildWindowId,ViewShellBase & rViewShellBase,::std::auto_ptr<SfxShell> pShell)42cdf0e10cSrcweir ChildWindowPane::ChildWindowPane (
43cdf0e10cSrcweir     const Reference<XResourceId>& rxPaneId,
44cdf0e10cSrcweir     sal_uInt16 nChildWindowId,
45cdf0e10cSrcweir     ViewShellBase& rViewShellBase,
46cdf0e10cSrcweir     ::std::auto_ptr<SfxShell> pShell)
47cdf0e10cSrcweir     : ChildWindowPaneInterfaceBase(rxPaneId,(::Window*)NULL),
48cdf0e10cSrcweir       mnChildWindowId(nChildWindowId),
49cdf0e10cSrcweir       mrViewShellBase(rViewShellBase),
50cdf0e10cSrcweir       mpShell(pShell),
51cdf0e10cSrcweir       mbHasBeenActivated(false)
52cdf0e10cSrcweir {
53cdf0e10cSrcweir     mrViewShellBase.GetViewShellManager()->ActivateShell(mpShell.get());
54cdf0e10cSrcweir 
55cdf0e10cSrcweir     SfxViewFrame* pViewFrame = mrViewShellBase.GetViewFrame();
56cdf0e10cSrcweir     if (pViewFrame != NULL)
57cdf0e10cSrcweir     {
58cdf0e10cSrcweir         if (mrViewShellBase.IsActive())
59cdf0e10cSrcweir         {
60cdf0e10cSrcweir             if (pViewFrame->KnowsChildWindow(mnChildWindowId))
61cdf0e10cSrcweir             {
62cdf0e10cSrcweir                 if (pViewFrame->HasChildWindow(mnChildWindowId))
63cdf0e10cSrcweir                 {
64cdf0e10cSrcweir                     // The ViewShellBase has already been activated.  Make
65cdf0e10cSrcweir                     // the child window visible as soon as possible.
66cdf0e10cSrcweir                     pViewFrame->SetChildWindow(mnChildWindowId, sal_True);
67cdf0e10cSrcweir                     OSL_TRACE("ChildWindowPane:activating now");
68cdf0e10cSrcweir                 }
69cdf0e10cSrcweir                 else
70cdf0e10cSrcweir                 {
71cdf0e10cSrcweir                     // The window is created asynchronously.  Rely on the
72cdf0e10cSrcweir                     // ConfigurationUpdater to try another update, and with
73cdf0e10cSrcweir                     // that another request for this window, in a short
74cdf0e10cSrcweir                     // time.
75cdf0e10cSrcweir                     OSL_TRACE("ChildWindowPane:activated asynchronously");
76cdf0e10cSrcweir                 }
77cdf0e10cSrcweir             }
78cdf0e10cSrcweir             else
79cdf0e10cSrcweir             {
80cdf0e10cSrcweir                 OSL_TRACE("ChildWindowPane:not known");
81cdf0e10cSrcweir             }
82cdf0e10cSrcweir         }
83cdf0e10cSrcweir         else
84cdf0e10cSrcweir         {
85cdf0e10cSrcweir             // The ViewShellBase has not yet been activated.  Hide the
86cdf0e10cSrcweir             // window and wait a little before it is made visible.  See
87cdf0e10cSrcweir             // comments in the GetWindow() method for an explanation.
88cdf0e10cSrcweir             pViewFrame->SetChildWindow(mnChildWindowId, sal_False);
89cdf0e10cSrcweir             OSL_TRACE("ChildWindowPane:base not active");
90cdf0e10cSrcweir         }
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 
~ChildWindowPane(void)97cdf0e10cSrcweir ChildWindowPane::~ChildWindowPane (void) throw()
98cdf0e10cSrcweir {
99cdf0e10cSrcweir }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 
Hide(void)104cdf0e10cSrcweir void ChildWindowPane::Hide (void)
105cdf0e10cSrcweir {
106cdf0e10cSrcweir     SfxViewFrame* pViewFrame = mrViewShellBase.GetViewFrame();
107cdf0e10cSrcweir     if (pViewFrame != NULL)
108cdf0e10cSrcweir         if (pViewFrame->KnowsChildWindow(mnChildWindowId))
109cdf0e10cSrcweir             if (pViewFrame->HasChildWindow(mnChildWindowId))
110cdf0e10cSrcweir                 pViewFrame->SetChildWindow(mnChildWindowId, sal_False);
111cdf0e10cSrcweir 
112cdf0e10cSrcweir     // Release the window because when the child window is shown again it
113cdf0e10cSrcweir     // may use a different window.
114cdf0e10cSrcweir     mxWindow = NULL;
115cdf0e10cSrcweir }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 
disposing(void)120cdf0e10cSrcweir void SAL_CALL ChildWindowPane::disposing (void)
121cdf0e10cSrcweir {
122cdf0e10cSrcweir     ::osl::MutexGuard aGuard (maMutex);
123cdf0e10cSrcweir 
124cdf0e10cSrcweir     mrViewShellBase.GetViewShellManager()->DeactivateShell(mpShell.get());
125cdf0e10cSrcweir     mpShell.reset();
126cdf0e10cSrcweir 
127cdf0e10cSrcweir     if (mxWindow.is())
128cdf0e10cSrcweir     {
129cdf0e10cSrcweir         mxWindow->removeEventListener(this);
130cdf0e10cSrcweir     }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     Pane::disposing();
133cdf0e10cSrcweir }
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 
GetWindow(void)138cdf0e10cSrcweir ::Window* ChildWindowPane::GetWindow (void)
139cdf0e10cSrcweir {
140cdf0e10cSrcweir     do
141cdf0e10cSrcweir     {
142cdf0e10cSrcweir         if (mxWindow.is())
143cdf0e10cSrcweir             // Window already exists => nothing to do.
144cdf0e10cSrcweir             break;
145cdf0e10cSrcweir 
146cdf0e10cSrcweir         // When the window is not yet present then obtain it only when the
147cdf0e10cSrcweir         // shell has already been activated.  The activation is not
148cdf0e10cSrcweir         // necessary for the code to work properly but is used to optimize
149cdf0e10cSrcweir         // the layouting and displaying of the window.  When it is made
150cdf0e10cSrcweir         // visible to early then some layouting seems to be made twice or at
151cdf0e10cSrcweir         // an inconvenient time and the overall process of initializing the
152cdf0e10cSrcweir         // Impress takes longer.
153cdf0e10cSrcweir         if ( ! mbHasBeenActivated && mpShell.get()!=NULL && ! mpShell->IsActive())
154cdf0e10cSrcweir             break;
155cdf0e10cSrcweir 
156cdf0e10cSrcweir         mbHasBeenActivated = true;
157cdf0e10cSrcweir         SfxViewFrame* pViewFrame = mrViewShellBase.GetViewFrame();
158cdf0e10cSrcweir         if (pViewFrame == NULL)
159cdf0e10cSrcweir             break;
160cdf0e10cSrcweir         // The view frame has to know the child window.  This can be the
161cdf0e10cSrcweir         // case, when for example the document is in read-only mode:  the
162cdf0e10cSrcweir         // task pane is then not available.
163cdf0e10cSrcweir         if ( ! pViewFrame->KnowsChildWindow(mnChildWindowId))
164cdf0e10cSrcweir             break;
165cdf0e10cSrcweir 
166cdf0e10cSrcweir         pViewFrame->SetChildWindow(mnChildWindowId, sal_True);
167cdf0e10cSrcweir         SfxChildWindow* pChildWindow = pViewFrame->GetChildWindow(mnChildWindowId);
168cdf0e10cSrcweir         if (pChildWindow == NULL)
169cdf0e10cSrcweir             if (pViewFrame->HasChildWindow(mnChildWindowId))
170cdf0e10cSrcweir             {
171cdf0e10cSrcweir                 // The child window is not yet visible.  Ask the view frame
172cdf0e10cSrcweir                 // to show it and try again to get access to the child
173cdf0e10cSrcweir                 // window.
174cdf0e10cSrcweir                 pViewFrame->ShowChildWindow(mnChildWindowId, sal_True);
175cdf0e10cSrcweir                 pChildWindow = pViewFrame->GetChildWindow(mnChildWindowId);
176cdf0e10cSrcweir             }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir         // When the child window is still not visible then we have to try later.
179cdf0e10cSrcweir         if (pChildWindow == NULL)
180cdf0e10cSrcweir             break;
181cdf0e10cSrcweir 
182cdf0e10cSrcweir         // From the child window get the docking window and from that the
183cdf0e10cSrcweir         // content window that is the container for the actual content.
184cdf0e10cSrcweir         PaneDockingWindow* pDockingWindow = dynamic_cast<PaneDockingWindow*>(
185cdf0e10cSrcweir             pChildWindow->GetWindow());
186cdf0e10cSrcweir         if (pDockingWindow == NULL)
187cdf0e10cSrcweir             break;
188cdf0e10cSrcweir 
189cdf0e10cSrcweir         // At last, we have access to the window and its UNO wrapper.
190cdf0e10cSrcweir         mpWindow = &pDockingWindow->GetContentWindow();
191cdf0e10cSrcweir         mxWindow = VCLUnoHelper::GetInterface(mpWindow);
192cdf0e10cSrcweir 
193cdf0e10cSrcweir         // Register as window listener to be informed when the child window
194cdf0e10cSrcweir         // is hidden.
195cdf0e10cSrcweir         if (mxWindow.is())
196cdf0e10cSrcweir             mxWindow->addEventListener(this);
197cdf0e10cSrcweir     }
198cdf0e10cSrcweir     while (false);
199cdf0e10cSrcweir 
200cdf0e10cSrcweir     return mpWindow;
201cdf0e10cSrcweir }
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 
getWindow(void)206cdf0e10cSrcweir Reference<awt::XWindow> SAL_CALL ChildWindowPane::getWindow (void)
207cdf0e10cSrcweir     throw (RuntimeException)
208cdf0e10cSrcweir {
209cdf0e10cSrcweir     if (mpWindow == NULL || ! mxWindow.is())
210cdf0e10cSrcweir         GetWindow();
211cdf0e10cSrcweir     return Pane::getWindow();
212cdf0e10cSrcweir }
213cdf0e10cSrcweir 
214cdf0e10cSrcweir 
215cdf0e10cSrcweir 
216cdf0e10cSrcweir IMPLEMENT_FORWARD_XINTERFACE2(
217cdf0e10cSrcweir     ChildWindowPane,
218cdf0e10cSrcweir     ChildWindowPaneInterfaceBase,
219cdf0e10cSrcweir     Pane);
220cdf0e10cSrcweir IMPLEMENT_FORWARD_XTYPEPROVIDER2(
221cdf0e10cSrcweir     ChildWindowPane,
222cdf0e10cSrcweir     ChildWindowPaneInterfaceBase,
223cdf0e10cSrcweir     Pane);
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 
226cdf0e10cSrcweir 
227cdf0e10cSrcweir 
228cdf0e10cSrcweir //----- XEventListener --------------------------------------------------------
229cdf0e10cSrcweir 
disposing(const lang::EventObject & rEvent)230cdf0e10cSrcweir void SAL_CALL ChildWindowPane::disposing (const lang::EventObject& rEvent)
231cdf0e10cSrcweir     throw (RuntimeException)
232cdf0e10cSrcweir {
233cdf0e10cSrcweir     ThrowIfDisposed();
234cdf0e10cSrcweir 
235cdf0e10cSrcweir     if (rEvent.Source == mxWindow)
236cdf0e10cSrcweir     {
237cdf0e10cSrcweir         // The window is gone but the pane remains alive.  The next call to
238cdf0e10cSrcweir         // GetWindow() may create the window anew.
239cdf0e10cSrcweir         mxWindow = NULL;
240cdf0e10cSrcweir         mpWindow = NULL;
241cdf0e10cSrcweir     }
242cdf0e10cSrcweir }
243cdf0e10cSrcweir 
244cdf0e10cSrcweir 
245cdf0e10cSrcweir 
246cdf0e10cSrcweir 
247cdf0e10cSrcweir } } // end of namespace sd::framework
248