Get the style color and number just once
[LibreOffice.git] / sfx2 / source / sidebar / SidebarDockingWindow.cxx
blobf0939c8433bc03cb06ec0bf832d3d5f442a46762
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
19 #include <sfx2/sidebar/SidebarDockingWindow.hxx>
20 #include <sfx2/sidebar/SidebarChildWindow.hxx>
21 #include <sfx2/sidebar/SidebarController.hxx>
22 #include <sidebar/PanelDescriptor.hxx>
24 #include <comphelper/dispatchcommand.hxx>
25 #include <comphelper/lok.hxx>
26 #include <comphelper/processfactory.hxx>
27 #include <sfx2/bindings.hxx>
28 #include <sfx2/dispatch.hxx>
29 #include <sfx2/viewfrm.hxx>
30 #include <svtools/acceleratorexecute.hxx>
31 #include <tools/gen.hxx>
32 #include <vcl/event.hxx>
33 #include <osl/diagnose.h>
35 #include <boost/property_tree/json_parser.hpp>
37 using namespace css;
38 using namespace css::uno;
40 namespace sfx2::sidebar {
42 SidebarDockingWindow::SidebarDockingWindow(SfxBindings* pSfxBindings, SidebarChildWindow& rChildWindow,
43 vcl::Window* pParentWindow, WinBits nBits)
44 : SfxDockingWindow(pSfxBindings, &rChildWindow, pParentWindow, nBits)
45 , mbIsReadyToDrag(false)
47 // Get the XFrame from the bindings.
48 if (pSfxBindings==nullptr || pSfxBindings->GetDispatcher()==nullptr)
50 assert(pSfxBindings != nullptr);
51 OSL_ASSERT(pSfxBindings->GetDispatcher()!=nullptr);
53 else if (!comphelper::LibreOfficeKit::isActive())
55 GetOrCreateSidebarController();
59 rtl::Reference<sfx2::sidebar::SidebarController>& SidebarDockingWindow::GetOrCreateSidebarController()
61 if (!mpSidebarController)
63 const SfxViewFrame* pViewFrame = GetBindings().GetDispatcher()->GetFrame();
64 mpSidebarController = sfx2::sidebar::SidebarController::create(this, pViewFrame);
67 return mpSidebarController;
70 SidebarDockingWindow::~SidebarDockingWindow()
72 disposeOnce();
75 void SidebarDockingWindow::dispose()
77 Reference<lang::XComponent> xComponent(mpSidebarController);
78 mpSidebarController.clear();
79 if (xComponent.is())
80 xComponent->dispose();
82 SfxDockingWindow::dispose();
85 void SidebarDockingWindow::GetFocus()
87 if (mpSidebarController.is())
89 mpSidebarController->RequestOpenDeck();
90 mpSidebarController->GetFocusManager().GrabFocus();
92 else
93 SfxDockingWindow::GetFocus();
96 bool SidebarDockingWindow::Close()
98 if (mpSidebarController.is())
99 mpSidebarController->SetFloatingDeckClosed(true);
101 return SfxDockingWindow::Close();
104 void SidebarDockingWindow::SyncUpdate()
106 if (mpSidebarController.is())
107 mpSidebarController->SyncUpdate();
110 SfxChildAlignment SidebarDockingWindow::CheckAlignment (
111 SfxChildAlignment eCurrentAlignment,
112 SfxChildAlignment eRequestedAlignment)
114 switch (eRequestedAlignment)
116 case SfxChildAlignment::TOP:
117 case SfxChildAlignment::HIGHESTTOP:
118 case SfxChildAlignment::LOWESTTOP:
119 case SfxChildAlignment::BOTTOM:
120 case SfxChildAlignment::LOWESTBOTTOM:
121 case SfxChildAlignment::HIGHESTBOTTOM:
122 return eCurrentAlignment;
124 case SfxChildAlignment::LEFT:
125 case SfxChildAlignment::RIGHT:
126 case SfxChildAlignment::FIRSTLEFT:
127 case SfxChildAlignment::LASTLEFT:
128 case SfxChildAlignment::FIRSTRIGHT:
129 case SfxChildAlignment::LASTRIGHT:
130 return eRequestedAlignment;
132 default:
133 return eRequestedAlignment;
137 bool SidebarDockingWindow::EventNotify(NotifyEvent& rEvent)
139 if (mpSidebarController)
141 NotifyEventType nType = rEvent.GetType();
142 if (NotifyEventType::KEYINPUT == nType)
144 const vcl::KeyCode& rKeyCode = rEvent.GetKeyEvent()->GetKeyCode();
145 switch (rKeyCode.GetCode())
147 case KEY_UP:
148 case KEY_DOWN:
149 case KEY_PAGEUP:
150 case KEY_PAGEDOWN:
151 case KEY_HOME:
152 case KEY_END:
153 case KEY_LEFT:
154 case KEY_RIGHT:
155 case KEY_BACKSPACE:
156 case KEY_DELETE:
157 case KEY_INSERT:
158 case KEY_RETURN:
159 case KEY_ESCAPE:
161 return true;
163 default:
164 break;
166 if (!mpAccel)
168 mpAccel = svt::AcceleratorExecute::createAcceleratorHelper();
169 mpAccel->init(comphelper::getProcessComponentContext(), mpSidebarController->getXFrame());
171 const OUString aCommand(mpAccel->findCommand(svt::AcceleratorExecute::st_VCLKey2AWTKey(rKeyCode)));
172 if (".uno:DesignerDialog" == aCommand)
174 std::shared_ptr<PanelDescriptor> xPanelDescriptor =
175 mpSidebarController->GetResourceManager()->GetPanelDescriptor( u"StyleListPanel" );
176 if ( xPanelDescriptor && mpSidebarController->IsDeckVisible( xPanelDescriptor->msDeckId ) )
177 Close();
178 return true;
180 if (".uno:Undo" == aCommand || ".uno:Redo" == aCommand)
182 comphelper::dispatchCommand(aCommand, {});
183 return true;
186 else if (NotifyEventType::MOUSEBUTTONDOWN == nType)
188 const MouseEvent *mEvt = rEvent.GetMouseEvent();
189 if (mEvt->IsLeft())
191 tools::Rectangle aGrip = mpSidebarController->GetDeckDragArea();
192 if ( aGrip.Contains( mEvt->GetPosPixel() ) )
193 mbIsReadyToDrag = true;
196 else if (NotifyEventType::MOUSEMOVE == nType)
198 const MouseEvent *mEvt = rEvent.GetMouseEvent();
199 tools::Rectangle aGrip = mpSidebarController->GetDeckDragArea();
200 if (mEvt->IsLeft() && aGrip.Contains( mEvt->GetPosPixel() ) && mbIsReadyToDrag )
202 Point aPos = mEvt->GetPosPixel();
203 vcl::Window* pWindow = rEvent.GetWindow();
204 if ( pWindow != this )
206 aPos = pWindow->OutputToScreenPixel( aPos );
207 aPos = ScreenToOutputPixel( aPos );
209 ImplStartDocking( aPos );
214 return SfxDockingWindow::EventNotify(rEvent);
217 } // end of namespace sfx2::sidebar
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */