1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_dbaccess.hxx"
30 #ifndef DBAUI_APPSWAPWINDOW_HXX
31 #include "AppSwapWindow.hxx"
32 #endif
33 #ifndef _TOOLS_DEBUG_HXX
34 #include <tools/debug.hxx>
35 #endif
36 #ifndef _DBA_DBACCESS_HELPID_HRC_
37 #include "dbaccess_helpid.hrc"
38 #endif
39 #ifndef _DBU_APP_HRC_
40 #include "dbu_app.hrc"
41 #endif
42 #ifndef DBAUI_APPVIEW_HXX
43 #include "AppView.hxx"
44 #endif
45 #ifndef _SV_SVAPP_HXX
46 #include <vcl/svapp.hxx>
47 #endif
48 #ifndef _SV_SYSWIN_HXX
49 #include <vcl/syswin.hxx>
50 #endif
51 #ifndef _SV_MENU_HXX
52 #include <vcl/menu.hxx>
53 #endif
54 #ifndef _SV_MNEMONIC_HXX
55 #include <vcl/mnemonic.hxx>
56 #endif
57 #include "IApplicationController.hxx"
58 
59 #include <memory>
60 
61 using namespace ::dbaui;
62 using namespace ::com::sun::star::uno;
63 using namespace ::com::sun::star::sdbc;
64 using namespace ::com::sun::star::lang;
65 using namespace ::com::sun::star::container;
66 
67 //==================================================================
68 // class OApplicationSwapWindow
69 DBG_NAME(OApplicationSwapWindow)
70 //==================================================================
71 OApplicationSwapWindow::OApplicationSwapWindow( Window* _pParent, OAppBorderWindow& _rBorderWindow )
72     :Window(_pParent,WB_DIALOGCONTROL )
73 	,m_aIconControl(this)
74 	,m_eLastType(E_NONE)
75 	,m_rBorderWin( _rBorderWindow )
76 {
77     DBG_CTOR(OApplicationSwapWindow,NULL);
78 //	SetCompoundControl( sal_True );
79 
80 	ImplInitSettings( sal_True, sal_True, sal_True );
81 
82 	m_aIconControl.SetClickHdl(LINK(this, OApplicationSwapWindow, OnContainerSelectHdl));
83 	m_aIconControl.setControlActionListener( &m_rBorderWin.getView()->getAppController() );
84 	m_aIconControl.SetHelpId(HID_APP_SWAP_ICONCONTROL);
85 	m_aIconControl.Show();
86 	//m_aIconControl.Enable(sal_True);
87 }
88 // -----------------------------------------------------------------------------
89 OApplicationSwapWindow::~OApplicationSwapWindow()
90 {
91 
92     DBG_DTOR(OApplicationSwapWindow,NULL);
93 }
94 // -----------------------------------------------------------------------------
95 void OApplicationSwapWindow::Resize()
96 {
97     Size aFLSize = LogicToPixel( Size( 8, 0 ), MAP_APPFONT );
98     long nX = 0;
99     if ( m_aIconControl.GetEntryCount() != 0 )
100         nX = m_aIconControl.GetBoundingBox( m_aIconControl.GetEntry(0) ).GetWidth() + aFLSize.Width();
101 
102     Size aOutputSize = GetOutputSize();
103 
104     m_aIconControl.SetPosSizePixel(	Point(static_cast<long>((aOutputSize.Width() - nX)*0.5), 0)  ,Size(nX,aOutputSize.Height()));
105 	m_aIconControl.ArrangeIcons();
106 }
107 // -----------------------------------------------------------------------------
108 void OApplicationSwapWindow::ImplInitSettings( sal_Bool bFont, sal_Bool bForeground, sal_Bool bBackground )
109 {
110 	const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
111 	if( bFont )
112 	{
113 		Font aFont;
114 		aFont = rStyleSettings.GetFieldFont();
115 		aFont.SetColor( rStyleSettings.GetWindowTextColor() );
116 		SetPointFont( aFont );
117 	}
118 
119 	if( bForeground || bFont )
120 	{
121 		SetTextColor( rStyleSettings.GetFieldTextColor() );
122 		SetTextFillColor();
123 	}
124 
125 	if( bBackground )
126 		SetBackground( rStyleSettings.GetFieldColor() );
127 }
128 // -----------------------------------------------------------------------
129 void OApplicationSwapWindow::DataChanged( const DataChangedEvent& rDCEvt )
130 {
131 	Window::DataChanged( rDCEvt );
132 	if ( (rDCEvt.GetType() == DATACHANGED_FONTS) ||
133 		(rDCEvt.GetType() == DATACHANGED_DISPLAY) ||
134 		(rDCEvt.GetType() == DATACHANGED_FONTSUBSTITUTION) ||
135 		((rDCEvt.GetType() == DATACHANGED_SETTINGS) &&
136 		(rDCEvt.GetFlags() & SETTINGS_STYLE)) )
137 	{
138 		ImplInitSettings( sal_True, sal_True, sal_True );
139 		Invalidate();
140 	}
141 }
142 // -----------------------------------------------------------------------------
143 void OApplicationSwapWindow::clearSelection()
144 {
145 	m_aIconControl.SetNoSelection();
146 	sal_uLong nPos = 0;
147 	SvxIconChoiceCtrlEntry* pEntry = m_aIconControl.GetSelectedEntry(nPos);
148 	if ( pEntry )
149 		m_aIconControl.InvalidateEntry(pEntry);
150 	m_aIconControl.GetClickHdl().Call(&m_aIconControl);
151 }
152 
153 // -----------------------------------------------------------------------------
154 void OApplicationSwapWindow::createIconAutoMnemonics( MnemonicGenerator& _rMnemonics )
155 {
156     m_aIconControl.CreateAutoMnemonics( _rMnemonics );
157 }
158 
159 // -----------------------------------------------------------------------------
160 bool OApplicationSwapWindow::interceptKeyInput( const KeyEvent& _rEvent )
161 {
162     const KeyCode& rKeyCode = _rEvent.GetKeyCode();
163     if ( rKeyCode.GetModifier() == KEY_MOD2 )
164         return m_aIconControl.DoKeyInput( _rEvent );
165 
166     // not handled
167     return false;
168 }
169 
170 // -----------------------------------------------------------------------------
171 ElementType OApplicationSwapWindow::getElementType() const
172 {
173 	sal_uLong nPos = 0;
174 	SvxIconChoiceCtrlEntry* pEntry = m_aIconControl.GetSelectedEntry(nPos);
175 	return ( pEntry ) ? *static_cast<ElementType*>(pEntry->GetUserData()) : E_NONE;
176 }
177 
178 // -----------------------------------------------------------------------------
179 bool OApplicationSwapWindow::onContainerSelected( ElementType _eType )
180 {
181 	if ( m_eLastType == _eType )
182         return true;
183 
184     if ( m_rBorderWin.getView()->getAppController().onContainerSelect( _eType ) )
185     {
186 		if ( _eType != E_NONE )
187 			m_eLastType = _eType;
188         return true;
189     } // if ( m_rBorderWin.getView()->getAppController().onContainerSelect( _eType ) )
190 
191 	PostUserEvent( LINK( this, OApplicationSwapWindow, ChangeToLastSelected ) );
192     return false;
193 }
194 
195 // -----------------------------------------------------------------------------
196 IMPL_LINK(OApplicationSwapWindow, OnContainerSelectHdl, SvtIconChoiceCtrl*, _pControl)
197 {
198 	sal_uLong nPos = 0;
199 	SvxIconChoiceCtrlEntry*	pEntry = _pControl->GetSelectedEntry( nPos );
200 	ElementType eType = E_NONE;
201 	if ( pEntry )
202     {
203 		eType = *static_cast<ElementType*>(pEntry->GetUserData());
204 	    onContainerSelected( eType ); // i87582
205 	}
206 
207 	return 1L;
208 }
209 //------------------------------------------------------------------------------
210 IMPL_LINK(OApplicationSwapWindow, ChangeToLastSelected, void*, EMPTYARG)
211 {
212 	selectContainer(m_eLastType);
213 	return 0L;
214 }
215 // -----------------------------------------------------------------------------
216 void OApplicationSwapWindow::selectContainer(ElementType _eType)
217 {
218 	sal_uLong nCount = m_aIconControl.GetEntryCount();
219 	SvxIconChoiceCtrlEntry*	pEntry = NULL;
220 	for (sal_uLong i=0; i < nCount; ++i)
221 	{
222 		pEntry = m_aIconControl.GetEntry(i);
223 		if ( pEntry && *static_cast<ElementType*>(pEntry->GetUserData()) == _eType )
224 			break;
225 		pEntry = NULL;
226 	}
227 
228     if ( pEntry )
229 		m_aIconControl.SetCursor(pEntry); // this call also initiates a onContainerSelected call
230     else
231 	    onContainerSelected( _eType );
232 }
233