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 <uielement/edittoolbarcontroller.hxx>
21
22 #include <com/sun/star/util/XURLTransformer.hpp>
23 #include <com/sun/star/beans/PropertyValue.hpp>
24
25 #include <svtools/toolboxcontroller.hxx>
26 #include <vcl/svapp.hxx>
27 #include <vcl/mnemonic.hxx>
28 #include <vcl/toolbox.hxx>
29 #include <vcl/event.hxx>
30 #include <vcl/edit.hxx>
31
32 using namespace ::com::sun::star;
33 using namespace css::uno;
34 using namespace css::beans;
35 using namespace css::lang;
36 using namespace css::frame;
37 using namespace css::util;
38
39 namespace framework
40 {
41
42 // Wrapper class to notify controller about events from edit.
43 // Unfortunaltly the events are notified through virtual methods instead
44 // of Listeners.
45
46 class EditControl : public Edit
47 {
48 public:
49 EditControl( vcl::Window* pParent, WinBits nStyle, EditToolbarController* pEditToolbarController );
50 virtual ~EditControl() override;
51 virtual void dispose() override;
52
53 virtual void Modify() override;
54 virtual void GetFocus() override;
55 virtual void LoseFocus() override;
56 virtual bool PreNotify( NotifyEvent& rNEvt ) override;
57
58 private:
59 EditToolbarController* m_pEditToolbarController;
60 };
61
EditControl(vcl::Window * pParent,WinBits nStyle,EditToolbarController * pEditToolbarController)62 EditControl::EditControl( vcl::Window* pParent, WinBits nStyle, EditToolbarController* pEditToolbarController ) :
63 Edit( pParent, nStyle )
64 , m_pEditToolbarController( pEditToolbarController )
65 {
66 }
67
~EditControl()68 EditControl::~EditControl()
69 {
70 disposeOnce();
71 }
72
dispose()73 void EditControl::dispose()
74 {
75 m_pEditToolbarController = nullptr;
76 Edit::dispose();
77 }
78
Modify()79 void EditControl::Modify()
80 {
81 Edit::Modify();
82 if ( m_pEditToolbarController )
83 m_pEditToolbarController->Modify();
84 }
85
GetFocus()86 void EditControl::GetFocus()
87 {
88 Edit::GetFocus();
89 if ( m_pEditToolbarController )
90 m_pEditToolbarController->GetFocus();
91 }
92
LoseFocus()93 void EditControl::LoseFocus()
94 {
95 Edit::LoseFocus();
96 if ( m_pEditToolbarController )
97 m_pEditToolbarController->LoseFocus();
98 }
99
PreNotify(NotifyEvent & rNEvt)100 bool EditControl::PreNotify( NotifyEvent& rNEvt )
101 {
102 bool bRet = false;
103 if ( m_pEditToolbarController )
104 bRet = m_pEditToolbarController->PreNotify( rNEvt );
105 if ( !bRet )
106 bRet = Edit::PreNotify( rNEvt );
107
108 return bRet;
109 }
110
EditToolbarController(const Reference<XComponentContext> & rxContext,const Reference<XFrame> & rFrame,ToolBox * pToolbar,sal_uInt16 nID,sal_Int32 nWidth,const OUString & aCommand)111 EditToolbarController::EditToolbarController(
112 const Reference< XComponentContext >& rxContext,
113 const Reference< XFrame >& rFrame,
114 ToolBox* pToolbar,
115 sal_uInt16 nID,
116 sal_Int32 nWidth,
117 const OUString& aCommand ) :
118 ComplexToolbarController( rxContext, rFrame, pToolbar, nID, aCommand )
119 , m_pEditControl( nullptr )
120 {
121 m_pEditControl = VclPtr<EditControl>::Create( m_pToolbar, WB_BORDER, this );
122 if ( nWidth == 0 )
123 nWidth = 100;
124
125 // Calculate height of the edit field according to the application font height
126 sal_Int32 nHeight = getFontSizePixel( m_pEditControl ) + 6 + 1;
127
128 m_pEditControl->SetSizePixel( ::Size( nWidth, nHeight ));
129 m_pToolbar->SetItemWindow( m_nID, m_pEditControl );
130 }
131
~EditToolbarController()132 EditToolbarController::~EditToolbarController()
133 {
134 }
135
dispose()136 void SAL_CALL EditToolbarController::dispose()
137 {
138 SolarMutexGuard aSolarMutexGuard;
139
140 m_pToolbar->SetItemWindow( m_nID, nullptr );
141 m_pEditControl.disposeAndClear();
142
143 ComplexToolbarController::dispose();
144 }
145
getExecuteArgs(sal_Int16 KeyModifier) const146 Sequence<PropertyValue> EditToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
147 {
148 Sequence<PropertyValue> aArgs( 2 );
149 OUString aSelectedText = m_pEditControl->GetText();
150
151 // Add key modifier to argument list
152 aArgs[0].Name = "KeyModifier";
153 aArgs[0].Value <<= KeyModifier;
154 aArgs[1].Name = "Text";
155 aArgs[1].Value <<= aSelectedText;
156 return aArgs;
157 }
158
Modify()159 void EditToolbarController::Modify()
160 {
161 notifyTextChanged( m_pEditControl->GetText() );
162 }
163
GetFocus()164 void EditToolbarController::GetFocus()
165 {
166 notifyFocusGet();
167 }
168
LoseFocus()169 void EditToolbarController::LoseFocus()
170 {
171 notifyFocusLost();
172 }
173
PreNotify(NotifyEvent const & rNEvt)174 bool EditToolbarController::PreNotify( NotifyEvent const & rNEvt )
175 {
176 if( rNEvt.GetType() == MouseNotifyEvent::KEYINPUT )
177 {
178 const ::KeyEvent* pKeyEvent = rNEvt.GetKeyEvent();
179 const vcl::KeyCode& rKeyCode = pKeyEvent->GetKeyCode();
180 if(( rKeyCode.GetModifier() | rKeyCode.GetCode()) == KEY_RETURN )
181 {
182 // Call execute only with non-empty text
183 if ( !m_pEditControl->GetText().isEmpty() )
184 execute( rKeyCode.GetModifier() );
185 return true;
186 }
187 }
188
189 return false;
190 }
191
executeControlCommand(const css::frame::ControlCommand & rControlCommand)192 void EditToolbarController::executeControlCommand( const css::frame::ControlCommand& rControlCommand )
193 {
194 if ( rControlCommand.Command.startsWith( "SetText" ))
195 {
196 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
197 {
198 if ( rControlCommand.Arguments[i].Name.startsWith( "Text" ))
199 {
200 OUString aText;
201 rControlCommand.Arguments[i].Value >>= aText;
202 m_pEditControl->SetText( aText );
203
204 // send notification
205 notifyTextChanged( aText );
206 break;
207 }
208 }
209 }
210 }
211
212 } // namespace
213
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
215