1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program 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 General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #include "top_window.hpp"
26 #include "generic_layout.hpp"
27 #include "os_graphics.hpp"
28 #include "os_window.hpp"
29 #include "os_factory.hpp"
31 #include "var_manager.hpp"
32 #include "../commands/cmd_on_top.hpp"
33 #include "../commands/cmd_dialogs.hpp"
34 #include "../controls/ctrl_generic.hpp"
35 #include "../events/evt_refresh.hpp"
36 #include "../events/evt_enter.hpp"
37 #include "../events/evt_focus.hpp"
38 #include "../events/evt_leave.hpp"
39 #include "../events/evt_menu.hpp"
40 #include "../events/evt_motion.hpp"
41 #include "../events/evt_mouse.hpp"
42 #include "../events/evt_key.hpp"
43 #include "../events/evt_special.hpp"
44 #include "../events/evt_scroll.hpp"
45 #include "../utils/position.hpp"
46 #include "../utils/ustring.hpp"
51 TopWindow::TopWindow( intf_thread_t *pIntf, int left, int top,
52 WindowManager &rWindowManager,
53 bool dragDrop, bool playOnDrop, bool visible ):
54 GenericWindow( pIntf, left, top, dragDrop, playOnDrop, NULL ),
55 m_visible( visible ), m_rWindowManager( rWindowManager ),
56 m_pActiveLayout( NULL ), m_pLastHitControl( NULL ),
57 m_pCapturingControl( NULL ), m_pFocusControl( NULL ), m_currModifier( 0 )
59 // Register as a moving window
60 m_rWindowManager.registerWindow( *this );
64 TopWindow::~TopWindow()
66 // Unregister from the window manager
67 m_rWindowManager.unregisterWindow( *this );
71 void TopWindow::processEvent( EvtRefresh &rEvtRefresh )
73 // We override the behaviour defined in GenericWindow, because we don't
74 // want to draw on a video control!
75 if( m_pActiveLayout == NULL )
77 GenericWindow::processEvent( rEvtRefresh );
81 m_pActiveLayout->refreshRect( rEvtRefresh.getXStart(),
82 rEvtRefresh.getYStart(),
83 rEvtRefresh.getWidth(),
84 rEvtRefresh.getHeight() );
89 void TopWindow::processEvent( EvtFocus &rEvtFocus )
91 // fprintf(stderr, rEvtFocus.getAsString().c_str());
95 void TopWindow::processEvent( EvtMenu &rEvtMenu )
97 Popup *pPopup = m_rWindowManager.getActivePopup();
98 // We should never receive a menu event when there is no active popup!
101 msg_Warn( getIntf(), "Unexpected menu event, ignoring" );
105 pPopup->handleEvent( rEvtMenu );
109 void TopWindow::processEvent( EvtMotion &rEvtMotion )
111 // New control hit by the mouse
112 CtrlGeneric *pNewHitControl =
113 findHitControl( rEvtMotion.getXPos() - getLeft(),
114 rEvtMotion.getYPos() - getTop() );
116 setLastHit( pNewHitControl );
118 /// Update the help text
119 VarManager *pVarManager = VarManager::instance( getIntf() );
122 pVarManager->getHelpText().set( pNewHitControl->getHelpText() );
125 // Send a motion event to the hit control, or to the control
126 // that captured the mouse, if any
127 CtrlGeneric *pActiveControl = pNewHitControl;
128 if( m_pCapturingControl )
130 pActiveControl = m_pCapturingControl;
134 // Compute the coordinates relative to the window
135 int xPos = rEvtMotion.getXPos() - getLeft();
136 int yPos = rEvtMotion.getYPos() - getTop();
137 // Send a motion event
138 EvtMotion evt( getIntf(), xPos, yPos );
139 pActiveControl->handleEvent( evt );
144 void TopWindow::processEvent( EvtLeave &rEvtLeave )
146 // No more hit control
149 if( !m_pCapturingControl )
151 m_rWindowManager.hideTooltip();
156 void TopWindow::processEvent( EvtMouse &rEvtMouse )
158 // Get the control hit by the mouse
159 CtrlGeneric *pNewHitControl = findHitControl( rEvtMouse.getXPos(),
160 rEvtMouse.getYPos() );
161 setLastHit( pNewHitControl );
163 // Change the focused control
164 if( rEvtMouse.getAction() == EvtMouse::kDown )
167 m_rWindowManager.raise( *this );
169 if( pNewHitControl && pNewHitControl->isFocusable() )
171 // If a new control gains the focus, the previous one loses it
172 if( m_pFocusControl && m_pFocusControl != pNewHitControl )
174 EvtFocus evt( getIntf(), false );
175 m_pFocusControl->handleEvent( evt );
177 if( pNewHitControl != m_pFocusControl )
179 m_pFocusControl = pNewHitControl;
180 EvtFocus evt( getIntf(), false );
181 pNewHitControl->handleEvent( evt );
184 else if( m_pFocusControl )
186 // The previous control loses the focus
187 EvtFocus evt( getIntf(), false );
188 m_pFocusControl->handleEvent( evt );
189 m_pFocusControl = NULL;
193 // Send a mouse event to the hit control, or to the control
194 // that captured the mouse, if any
195 CtrlGeneric *pActiveControl = pNewHitControl;
196 if( m_pCapturingControl )
198 pActiveControl = m_pCapturingControl;
202 pActiveControl->handleEvent( rEvtMouse );
207 void TopWindow::processEvent( EvtKey &rEvtKey )
209 // Forward the event to the focused control, if any
210 if( m_pFocusControl )
212 m_pFocusControl->handleEvent( rEvtKey );
216 // Only do the action when the key is down
217 if( rEvtKey.getAsString().find( "key:down") != string::npos )
219 //XXX not to be hardcoded!
220 // Ctrl-S = Change skin
221 if( (rEvtKey.getMod() & EvtInput::kModCtrl) &&
222 rEvtKey.getKey() == 's' )
224 CmdDlgChangeSkin cmd( getIntf() );
229 //XXX not to be hardcoded!
230 // Ctrl-T = Toggle on top
231 if( (rEvtKey.getMod() & EvtInput::kModCtrl) &&
232 rEvtKey.getKey() == 't' )
234 CmdOnTop cmd( getIntf() );
241 val.i_int = rEvtKey.getKey();
243 if( rEvtKey.getMod() & EvtInput::kModAlt )
245 val.i_int |= KEY_MODIFIER_ALT;
247 if( rEvtKey.getMod() & EvtInput::kModCtrl )
249 val.i_int |= KEY_MODIFIER_CTRL;
251 if( rEvtKey.getMod() & EvtInput::kModShift )
253 val.i_int |= KEY_MODIFIER_SHIFT;
256 var_Set( getIntf()->p_vlc, "key-pressed", val );
259 // Always store the modifier, which can be needed for scroll events
260 m_currModifier = rEvtKey.getMod();
264 void TopWindow::processEvent( EvtScroll &rEvtScroll )
269 // Get the control hit by the mouse
270 CtrlGeneric *pNewHitControl = findHitControl( rEvtScroll.getXPos(),
271 rEvtScroll.getYPos());
272 setLastHit( pNewHitControl );
274 // Send a mouse event to the hit control, or to the control
275 // that captured the mouse, if any
276 CtrlGeneric *pActiveControl = pNewHitControl;
278 if( m_pCapturingControl )
280 pActiveControl = m_pCapturingControl;
284 pActiveControl->handleEvent( rEvtScroll );
288 // Treat the scroll event as a hotkey
290 if( rEvtScroll.getDirection() == EvtScroll::kUp )
292 val.i_int = KEY_MOUSEWHEELUP;
296 val.i_int = KEY_MOUSEWHEELDOWN;
299 val.i_int |= m_currModifier;
301 var_Set( getIntf()->p_vlc, "key-pressed", val );
306 void TopWindow::forwardEvent( EvtGeneric &rEvt, CtrlGeneric &rCtrl )
308 // XXX: We should do some checks here
309 rCtrl.handleEvent( rEvt );
313 void TopWindow::refresh( int left, int top, int width, int height )
315 if( m_pActiveLayout )
317 m_pActiveLayout->getImage()->copyToWindow( *getOSWindow(), left, top,
318 width, height, left, top );
323 void TopWindow::setActiveLayout( GenericLayout *pLayout )
325 pLayout->setWindow( this );
326 m_pActiveLayout = pLayout;
327 // Get the size of the layout and resize the window
328 resize( pLayout->getWidth(), pLayout->getHeight() );
330 pLayout->refreshAll();
334 const GenericLayout& TopWindow::getActiveLayout() const
336 return *m_pActiveLayout;
340 void TopWindow::innerShow()
342 // First, refresh the layout and update the shape of the window
343 if( m_pActiveLayout )
346 m_pActiveLayout->refreshAll();
349 GenericWindow::innerShow();
353 void TopWindow::updateShape()
355 // Set the shape of the window
356 if( m_pActiveLayout )
358 OSGraphics *pImage = m_pActiveLayout->getImage();
361 pImage->applyMaskToWindow( *getOSWindow() );
367 void TopWindow::onControlCapture( const CtrlGeneric &rCtrl )
369 // Set the capturing control
370 m_pCapturingControl = (CtrlGeneric*) &rCtrl;
374 void TopWindow::onControlRelease( const CtrlGeneric &rCtrl )
376 // Release the capturing control
377 if( m_pCapturingControl == &rCtrl )
379 m_pCapturingControl = NULL;
383 msg_Dbg( getIntf(), "Control had not captured the mouse" );
386 // Send an enter event to the control under the mouse, if it doesn't
387 // have received it yet
388 if( m_pLastHitControl && m_pLastHitControl != &rCtrl )
390 EvtEnter evt( getIntf() );
391 m_pLastHitControl->handleEvent( evt );
394 m_rWindowManager.hideTooltip();
395 UString tipText = m_pLastHitControl->getTooltipText();
396 if( tipText.length() > 0 )
398 // Set the tooltip text variable
399 VarManager *pVarManager = VarManager::instance( getIntf() );
400 pVarManager->getTooltipText().set( tipText );
401 m_rWindowManager.showTooltip();
407 void TopWindow::onTooltipChange( const CtrlGeneric &rCtrl )
409 // Check that the control is the active one
410 if( m_pLastHitControl && m_pLastHitControl == &rCtrl )
412 // Set the tooltip text variable
413 VarManager *pVarManager = VarManager::instance( getIntf() );
414 pVarManager->getTooltipText().set( rCtrl.getTooltipText() );
419 CtrlGeneric *TopWindow::findHitControl( int xPos, int yPos )
421 if( m_pActiveLayout == NULL )
426 // Get the controls in the active layout
427 const list<LayeredControl> &ctrlList = m_pActiveLayout->getControlList();
428 list<LayeredControl>::const_reverse_iterator iter;
430 // New control hit by the mouse
431 CtrlGeneric *pNewHitControl = NULL;
433 // Loop on the control list to find the uppest hit control
434 for( iter = ctrlList.rbegin(); iter != ctrlList.rend(); iter++ )
436 // Get the position of the control in the layout
437 const Position *pos = (*iter).m_pControl->getPosition();
440 // Compute the coordinates of the mouse relative to the control
441 int xRel = xPos - pos->getLeft();
442 int yRel = yPos - pos->getTop();
444 CtrlGeneric *pCtrl = (*iter).m_pControl;
446 if( pCtrl->isVisible() && pCtrl->mouseOver( xRel, yRel ) )
448 pNewHitControl = (*iter).m_pControl;
454 msg_Dbg( getIntf(), "Control at NULL position" );
458 // If the hit control has just been entered, send it an enter event
459 if( pNewHitControl && (pNewHitControl != m_pLastHitControl) )
461 // Don't send the event if another control captured the mouse
462 if( !m_pCapturingControl || (m_pCapturingControl == pNewHitControl ) )
464 EvtEnter evt( getIntf() );
465 pNewHitControl->handleEvent( evt );
467 if( !m_pCapturingControl )
470 m_rWindowManager.hideTooltip();
471 UString tipText = pNewHitControl->getTooltipText();
472 if( tipText.length() > 0 )
474 // Set the tooltip text variable
475 VarManager *pVarManager = VarManager::instance( getIntf() );
476 pVarManager->getTooltipText().set( tipText );
477 m_rWindowManager.showTooltip();
483 return pNewHitControl;
488 void TopWindow::setLastHit( CtrlGeneric *pNewHitControl )
490 // Send a leave event to the left control
491 if( m_pLastHitControl && (pNewHitControl != m_pLastHitControl) )
493 // Don't send the event if another control captured the mouse
494 if( !m_pCapturingControl || (m_pCapturingControl == m_pLastHitControl))
496 EvtLeave evt( getIntf() );
497 m_pLastHitControl->handleEvent( evt );
501 m_pLastHitControl = pNewHitControl;