LibreOffice Module framework (master) 1
togglebuttontoolbarcontroller.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 * 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
21
23#include <vcl/svapp.hxx>
24#include <vcl/toolbox.hxx>
25#include <vcl/menu.hxx>
26
27using namespace ::com::sun::star;
28using namespace ::com::sun::star::awt;
29using namespace ::com::sun::star::uno;
30using namespace ::com::sun::star::beans;
31using namespace ::com::sun::star::lang;
32using namespace ::com::sun::star::frame;
33using namespace ::com::sun::star::util;
34
35namespace framework
36{
37
39 const Reference< XComponentContext >& rxContext,
40 const Reference< XFrame >& rFrame,
41 ToolBox* pToolbar,
42 ToolBoxItemId nID,
43 Style eStyle,
44 const OUString& aCommand ) :
45 ComplexToolbarController( rxContext, rFrame, pToolbar, nID, aCommand )
46{
47 if ( eStyle == Style::DropDownButton )
48 m_xToolbar->SetItemBits( m_nID, ToolBoxItemBits::DROPDOWNONLY | m_xToolbar->GetItemBits( m_nID ) );
49 else // Style::ToggleDropDownButton
50 m_xToolbar->SetItemBits( m_nID, ToolBoxItemBits::DROPDOWN | m_xToolbar->GetItemBits( m_nID ) );
51}
52
54{
55}
56
58{
59 SolarMutexGuard aSolarMutexGuard;
61}
62
63Sequence<PropertyValue> ToggleButtonToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
64{
65 Sequence<PropertyValue> aArgs{ // Add key modifier to argument list
66 comphelper::makePropertyValue("KeyModifier", KeyModifier),
68 return aArgs;
69}
70
71uno::Reference< awt::XWindow > SAL_CALL ToggleButtonToolbarController::createPopupWindow()
72{
73 uno::Reference< awt::XWindow > xWindow;
74
75 SolarMutexGuard aSolarMutexGuard;
76
77 // create popup menu
79 const sal_uInt32 nCount = m_aDropdownMenuList.size();
80 for ( sal_uInt32 i = 0; i < nCount; i++ )
81 {
82 const OUString & rLabel = m_aDropdownMenuList[i].mLabel;
83 aPopup->InsertItem( sal_uInt16( i+1 ), rLabel );
84 if ( rLabel == m_aCurrentSelection )
85 aPopup->CheckItem( sal_uInt16( i+1 ) );
86 else
87 aPopup->CheckItem( sal_uInt16( i+1 ), false );
88
89 if ( !m_aDropdownMenuList[i].mTipHelpText.isEmpty() )
90 aPopup->SetTipHelpText( sal_uInt16( i+1 ), m_aDropdownMenuList[i].mTipHelpText );
91 }
92
93 m_xToolbar->SetItemDown( m_nID, true );
94 aPopup->SetSelectHdl( LINK( this, ToggleButtonToolbarController, MenuSelectHdl ));
95 aPopup->Execute( m_xToolbar, m_xToolbar->GetItemRect( m_nID ));
96 m_xToolbar->SetItemDown( m_nID, false );
97
98 return xWindow;
99}
100
101void ToggleButtonToolbarController::executeControlCommand( const css::frame::ControlCommand& rControlCommand )
102{
103 SolarMutexGuard aSolarMutexGuard;
104
105 if ( rControlCommand.Command == "SetList" )
106 {
107 for ( auto const & arg : rControlCommand.Arguments )
108 {
109 if ( arg.Name == "List" )
110 {
111 Sequence< OUString > aList;
112 m_aDropdownMenuList.clear();
113 m_aCurrentSelection.clear();
114
115 arg.Value >>= aList;
116 for ( OUString const & label : std::as_const(aList) )
117 {
119 m_aDropdownMenuList.back().mLabel = label;
120 }
121
122 // send notification
123 uno::Sequence< beans::NamedValue > aInfo { { "List", css::uno::Any(aList) } };
124 addNotifyInfo( "ListChanged",
126 aInfo );
127
128 break;
129 }
130 }
131 }
132 else if ( rControlCommand.Command == "CheckItemPos" )
133 {
134 for ( auto const & arg : rControlCommand.Arguments )
135 {
136 if ( arg.Name == "Pos" )
137 {
138 sal_Int32 nPos( -1 );
139
140 arg.Value >>= nPos;
141 if ( nPos >= 0 &&
142 ( sal::static_int_cast< sal_uInt32 >(nPos)
143 < m_aDropdownMenuList.size() ) )
144 {
146
147 // send notification
148 uno::Sequence< beans::NamedValue > aInfo { { "ItemChecked", css::uno::Any(nPos) } };
149 addNotifyInfo( "Pos",
151 aInfo );
152 }
153 break;
154 }
155 }
156 }
157 else if ( rControlCommand.Command == "AddEntry" )
158 {
159 OUString aText;
160 OUString aTipHelpText;
161
162 for ( auto const & arg : rControlCommand.Arguments )
163 {
164 if ( arg.Name == "Text" )
165 {
166 arg.Value >>= aText;
167 }
168 else if ( arg.Name == "TipHelpText" )
169 {
170 arg.Value >>= aTipHelpText;
171 }
172 }
173
174 if (!aText.isEmpty())
175 {
177 m_aDropdownMenuList.back().mLabel = aText;
178 m_aDropdownMenuList.back().mTipHelpText = aTipHelpText;
179 }
180 }
181 else if ( rControlCommand.Command == "InsertEntry" )
182 {
183 sal_Int32 nPos(0);
184 sal_Int32 nSize = sal_Int32( m_aDropdownMenuList.size() );
185 OUString aText;
186 for ( auto const & arg : rControlCommand.Arguments )
187 {
188 if ( arg.Name == "Pos" )
189 {
190 sal_Int32 nTmpPos = 0;
191 if ( arg.Value >>= nTmpPos )
192 {
193 if (( nTmpPos >= 0 ) && ( nTmpPos < nSize ))
194 nPos = nTmpPos;
195 }
196 }
197 else if ( arg.Name == "Text" )
198 arg.Value >>= aText;
199 }
200
201 std::vector< DropdownMenuItem >::iterator aIter = m_aDropdownMenuList.begin();
202 aIter += nPos;
203 aIter = m_aDropdownMenuList.insert(aIter, DropdownMenuItem());
204 if (aIter != m_aDropdownMenuList.end())
205 aIter->mLabel = aText;
206 }
207 else if ( rControlCommand.Command == "RemoveEntryPos" )
208 {
209 for ( auto const & arg : rControlCommand.Arguments )
210 {
211 if ( arg.Name == "Pos" )
212 {
213 sal_Int32 nPos( -1 );
214 if ( arg.Value >>= nPos )
215 {
216 if ( nPos < sal_Int32( m_aDropdownMenuList.size() ))
217 {
219 }
220 }
221 break;
222 }
223 }
224 }
225 else if ( rControlCommand.Command == "RemoveEntryText" )
226 {
227 for ( auto const & arg : rControlCommand.Arguments )
228 {
229 if ( arg.Name == "Text" )
230 {
231 OUString aText;
232 if ( arg.Value >>= aText )
233 {
234 sal_Int32 nSize = sal_Int32( m_aDropdownMenuList.size() );
235 for ( sal_Int32 j = 0; j < nSize; j++ )
236 {
237 if ( m_aDropdownMenuList[j].mLabel == aText )
238 {
239 m_aDropdownMenuList.erase(m_aDropdownMenuList.begin() + j);
240 break;
241 }
242 }
243 }
244 break;
245 }
246 }
247 }
248 else if ( rControlCommand.Command == "createPopupMenu" )
249 {
251 }
252}
253
254IMPL_LINK( ToggleButtonToolbarController, MenuSelectHdl, Menu *, pMenu, bool )
255{
256 SolarMutexGuard aGuard;
257
258 sal_uInt16 nItemId = pMenu->GetCurItemId();
259 if ( nItemId > 0 && nItemId <= m_aDropdownMenuList.size() )
260 {
261 m_aCurrentSelection = m_aDropdownMenuList[nItemId-1].mLabel;
262
263 execute( 0 );
264 }
265 return false;
266}
267
268} // namespace
269
270/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::frame::XDispatch > getDispatchFromCommand(const OUString &aCommand) const
virtual void SAL_CALL dispose() override
void addNotifyInfo(const OUString &aEventName, const css::uno::Reference< css::frame::XDispatch > &xDispatch, const css::uno::Sequence< css::beans::NamedValue > &rInfo)
virtual css::uno::Reference< css::awt::XWindow > SAL_CALL createPopupWindow() override
virtual css::uno::Sequence< css::beans::PropertyValue > getExecuteArgs(sal_Int16 KeyModifier) const override
ToggleButtonToolbarController(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const css::uno::Reference< css::frame::XFrame > &rFrame, ToolBox *pToolBar, ToolBoxItemId nID, Style eStyle, const OUString &aCommand)
virtual void executeControlCommand(const css::frame::ControlCommand &rControlCommand) override
int nCount
sal_uInt16 nPos
def label(st)
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
IMPL_LINK(WindowCommandDispatch, impl_notifyCommand, VclWindowEvent &, rEvent, void)
int i
OUString aCommand