tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / framework / source / uielement / togglebuttontoolbarcontroller.cxx
blobbe57703f87ff579c4bce30711a27d056063794ba
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 .
20 #include <uielement/togglebuttontoolbarcontroller.hxx>
22 #include <comphelper/propertyvalue.hxx>
23 #include <vcl/svapp.hxx>
24 #include <vcl/toolbox.hxx>
25 #include <vcl/menu.hxx>
27 using namespace ::com::sun::star;
28 using namespace ::com::sun::star::awt;
29 using namespace ::com::sun::star::uno;
30 using namespace ::com::sun::star::beans;
31 using namespace ::com::sun::star::frame;
33 namespace framework
36 ToggleButtonToolbarController::ToggleButtonToolbarController(
37 const Reference< XComponentContext >& rxContext,
38 const Reference< XFrame >& rFrame,
39 ToolBox* pToolbar,
40 ToolBoxItemId nID,
41 Style eStyle,
42 const OUString& aCommand ) :
43 ComplexToolbarController( rxContext, rFrame, pToolbar, nID, aCommand )
45 if ( eStyle == Style::DropDownButton )
46 m_xToolbar->SetItemBits( m_nID, ToolBoxItemBits::DROPDOWNONLY | m_xToolbar->GetItemBits( m_nID ) );
47 else // Style::ToggleDropDownButton
48 m_xToolbar->SetItemBits( m_nID, ToolBoxItemBits::DROPDOWN | m_xToolbar->GetItemBits( m_nID ) );
51 ToggleButtonToolbarController::~ToggleButtonToolbarController()
55 void SAL_CALL ToggleButtonToolbarController::dispose()
57 SolarMutexGuard aSolarMutexGuard;
58 ComplexToolbarController::dispose();
61 Sequence<PropertyValue> ToggleButtonToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
63 Sequence<PropertyValue> aArgs{ // Add key modifier to argument list
64 comphelper::makePropertyValue(u"KeyModifier"_ustr, KeyModifier),
65 comphelper::makePropertyValue(u"Text"_ustr, m_aCurrentSelection) };
66 return aArgs;
69 uno::Reference< awt::XWindow > SAL_CALL ToggleButtonToolbarController::createPopupWindow()
71 uno::Reference< awt::XWindow > xWindow;
73 SolarMutexGuard aSolarMutexGuard;
75 // create popup menu
76 ScopedVclPtrInstance<::PopupMenu> aPopup;
77 const sal_uInt32 nCount = m_aDropdownMenuList.size();
78 for ( sal_uInt32 i = 0; i < nCount; i++ )
80 const OUString & rLabel = m_aDropdownMenuList[i].mLabel;
81 aPopup->InsertItem( sal_uInt16( i+1 ), rLabel );
82 if ( rLabel == m_aCurrentSelection )
83 aPopup->CheckItem( sal_uInt16( i+1 ) );
84 else
85 aPopup->CheckItem( sal_uInt16( i+1 ), false );
87 if ( !m_aDropdownMenuList[i].mTipHelpText.isEmpty() )
88 aPopup->SetTipHelpText( sal_uInt16( i+1 ), m_aDropdownMenuList[i].mTipHelpText );
91 m_xToolbar->SetItemDown( m_nID, true );
92 aPopup->SetSelectHdl( LINK( this, ToggleButtonToolbarController, MenuSelectHdl ));
93 aPopup->Execute( m_xToolbar, m_xToolbar->GetItemRect( m_nID ));
94 m_xToolbar->SetItemDown( m_nID, false );
96 return xWindow;
99 void ToggleButtonToolbarController::executeControlCommand( const css::frame::ControlCommand& rControlCommand )
101 SolarMutexGuard aSolarMutexGuard;
103 if ( rControlCommand.Command == "SetList" )
105 for ( auto const & arg : rControlCommand.Arguments )
107 if ( arg.Name == "List" )
109 Sequence< OUString > aList;
110 m_aDropdownMenuList.clear();
111 m_aCurrentSelection.clear();
113 arg.Value >>= aList;
114 for (OUString const& label : aList)
116 m_aDropdownMenuList.push_back( DropdownMenuItem() );
117 m_aDropdownMenuList.back().mLabel = label;
120 // send notification
121 uno::Sequence< beans::NamedValue > aInfo { { u"List"_ustr, css::uno::Any(aList) } };
122 addNotifyInfo( u"ListChanged"_ustr,
123 getDispatchFromCommand( m_aCommandURL ),
124 aInfo );
126 break;
130 else if ( rControlCommand.Command == "CheckItemPos" )
132 for ( auto const & arg : rControlCommand.Arguments )
134 if ( arg.Name == "Pos" )
136 sal_Int32 nPos( -1 );
138 arg.Value >>= nPos;
139 if ( nPos >= 0 &&
140 ( sal::static_int_cast< sal_uInt32 >(nPos)
141 < m_aDropdownMenuList.size() ) )
143 m_aCurrentSelection = m_aDropdownMenuList[nPos].mLabel;
145 // send notification
146 uno::Sequence< beans::NamedValue > aInfo { { u"ItemChecked"_ustr, css::uno::Any(nPos) } };
147 addNotifyInfo( u"Pos"_ustr,
148 getDispatchFromCommand( m_aCommandURL ),
149 aInfo );
151 break;
155 else if ( rControlCommand.Command == "AddEntry" )
157 OUString aText;
158 OUString aTipHelpText;
160 for ( auto const & arg : rControlCommand.Arguments )
162 if ( arg.Name == "Text" )
164 arg.Value >>= aText;
166 else if ( arg.Name == "TipHelpText" )
168 arg.Value >>= aTipHelpText;
172 if (!aText.isEmpty())
174 m_aDropdownMenuList.push_back( DropdownMenuItem() );
175 m_aDropdownMenuList.back().mLabel = aText;
176 m_aDropdownMenuList.back().mTipHelpText = aTipHelpText;
179 else if ( rControlCommand.Command == "InsertEntry" )
181 sal_Int32 nPos(0);
182 sal_Int32 nSize = sal_Int32( m_aDropdownMenuList.size() );
183 OUString aText;
184 for ( auto const & arg : rControlCommand.Arguments )
186 if ( arg.Name == "Pos" )
188 sal_Int32 nTmpPos = 0;
189 if ( arg.Value >>= nTmpPos )
191 if (( nTmpPos >= 0 ) && ( nTmpPos < nSize ))
192 nPos = nTmpPos;
195 else if ( arg.Name == "Text" )
196 arg.Value >>= aText;
199 std::vector< DropdownMenuItem >::iterator aIter = m_aDropdownMenuList.begin();
200 aIter += nPos;
201 aIter = m_aDropdownMenuList.insert(aIter, DropdownMenuItem());
202 if (aIter != m_aDropdownMenuList.end())
203 aIter->mLabel = aText;
205 else if ( rControlCommand.Command == "RemoveEntryPos" )
207 for ( auto const & arg : rControlCommand.Arguments )
209 if ( arg.Name == "Pos" )
211 sal_Int32 nPos( -1 );
212 if ( arg.Value >>= nPos )
214 if ( nPos < sal_Int32( m_aDropdownMenuList.size() ))
216 m_aDropdownMenuList.erase(m_aDropdownMenuList.begin() + nPos);
219 break;
223 else if ( rControlCommand.Command == "RemoveEntryText" )
225 for ( auto const & arg : rControlCommand.Arguments )
227 if ( arg.Name == "Text" )
229 OUString aText;
230 if ( arg.Value >>= aText )
232 sal_Int32 nSize = sal_Int32( m_aDropdownMenuList.size() );
233 for ( sal_Int32 j = 0; j < nSize; j++ )
235 if ( m_aDropdownMenuList[j].mLabel == aText )
237 m_aDropdownMenuList.erase(m_aDropdownMenuList.begin() + j);
238 break;
242 break;
246 else if ( rControlCommand.Command == "createPopupMenu" )
248 createPopupWindow();
252 IMPL_LINK( ToggleButtonToolbarController, MenuSelectHdl, Menu *, pMenu, bool )
254 SolarMutexGuard aGuard;
256 sal_uInt16 nItemId = pMenu->GetCurItemId();
257 if ( nItemId > 0 && nItemId <= m_aDropdownMenuList.size() )
259 m_aCurrentSelection = m_aDropdownMenuList[nItemId-1].mLabel;
261 execute( 0 );
263 return false;
266 } // namespace
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */