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_libvlc, "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_libvlc, "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 bool isVisible = getVisibleVar().get();
326 if( m_pActiveLayout )
330 m_pActiveLayout->onHide();
332 // The current layout becomes inactive
333 m_pActiveLayout->getActiveVar().set( false );
336 pLayout->setWindow( this );
337 m_pActiveLayout = pLayout;
338 // Get the size of the layout and resize the window
339 resize( pLayout->getWidth(), pLayout->getHeight() );
347 // The new layout is active
348 pLayout->getActiveVar().set( true );
352 const GenericLayout& TopWindow::getActiveLayout() const
354 return *m_pActiveLayout;
358 void TopWindow::innerShow()
360 // First, refresh the layout and update the shape of the window
361 if( m_pActiveLayout )
364 m_pActiveLayout->onShow();
367 GenericWindow::innerShow();
371 void TopWindow::innerHide()
373 if( m_pActiveLayout )
375 // Notify the active layout
376 m_pActiveLayout->onHide();
379 GenericWindow::innerHide();
383 void TopWindow::updateShape()
385 // Set the shape of the window
386 if( m_pActiveLayout )
388 OSGraphics *pImage = m_pActiveLayout->getImage();
391 pImage->applyMaskToWindow( *getOSWindow() );
397 void TopWindow::onControlCapture( const CtrlGeneric &rCtrl )
399 // Set the capturing control
400 m_pCapturingControl = (CtrlGeneric*) &rCtrl;
404 void TopWindow::onControlRelease( const CtrlGeneric &rCtrl )
406 // Release the capturing control
407 if( m_pCapturingControl == &rCtrl )
409 m_pCapturingControl = NULL;
413 msg_Dbg( getIntf(), "control had not captured the mouse" );
416 // Send an enter event to the control under the mouse, if it doesn't
417 // have received it yet
418 if( m_pLastHitControl && m_pLastHitControl != &rCtrl )
420 EvtEnter evt( getIntf() );
421 m_pLastHitControl->handleEvent( evt );
424 m_rWindowManager.hideTooltip();
425 UString tipText = m_pLastHitControl->getTooltipText();
426 if( tipText.length() > 0 )
428 // Set the tooltip text variable
429 VarManager *pVarManager = VarManager::instance( getIntf() );
430 pVarManager->getTooltipText().set( tipText );
431 m_rWindowManager.showTooltip();
437 void TopWindow::onTooltipChange( const CtrlGeneric &rCtrl )
439 // Check that the control is the active one
440 if( m_pLastHitControl && m_pLastHitControl == &rCtrl )
442 if( rCtrl.getTooltipText().size() )
444 // Set the tooltip text variable
445 VarManager *pVarManager = VarManager::instance( getIntf() );
446 pVarManager->getTooltipText().set( rCtrl.getTooltipText() );
447 m_rWindowManager.showTooltip();
451 // Nothing to display, so hide the tooltip
452 m_rWindowManager.hideTooltip();
458 CtrlGeneric *TopWindow::findHitControl( int xPos, int yPos )
460 if( m_pActiveLayout == NULL )
465 // Get the controls in the active layout
466 const list<LayeredControl> &ctrlList = m_pActiveLayout->getControlList();
467 list<LayeredControl>::const_reverse_iterator iter;
469 // New control hit by the mouse
470 CtrlGeneric *pNewHitControl = NULL;
472 // Loop on the control list to find the uppest hit control
473 for( iter = ctrlList.rbegin(); iter != ctrlList.rend(); iter++ )
475 // Get the position of the control in the layout
476 const Position *pos = (*iter).m_pControl->getPosition();
479 // Compute the coordinates of the mouse relative to the control
480 int xRel = xPos - pos->getLeft();
481 int yRel = yPos - pos->getTop();
483 CtrlGeneric *pCtrl = (*iter).m_pControl;
485 if( pCtrl->isVisible() && pCtrl->mouseOver( xRel, yRel ) )
487 pNewHitControl = (*iter).m_pControl;
493 msg_Dbg( getIntf(), "control at NULL position" );
497 // If the hit control has just been entered, send it an enter event
498 if( pNewHitControl && (pNewHitControl != m_pLastHitControl) )
500 // Don't send the event if another control captured the mouse
501 if( !m_pCapturingControl || (m_pCapturingControl == pNewHitControl ) )
503 EvtEnter evt( getIntf() );
504 pNewHitControl->handleEvent( evt );
506 if( !m_pCapturingControl )
509 m_rWindowManager.hideTooltip();
510 UString tipText = pNewHitControl->getTooltipText();
511 if( tipText.length() > 0 )
513 // Set the tooltip text variable
514 VarManager *pVarManager = VarManager::instance( getIntf() );
515 pVarManager->getTooltipText().set( tipText );
516 m_rWindowManager.showTooltip();
522 return pNewHitControl;
527 void TopWindow::setLastHit( CtrlGeneric *pNewHitControl )
529 // Send a leave event to the left control
530 if( m_pLastHitControl && (pNewHitControl != m_pLastHitControl) )
532 // Don't send the event if another control captured the mouse
533 if( !m_pCapturingControl || (m_pCapturingControl == m_pLastHitControl))
535 EvtLeave evt( getIntf() );
536 m_pLastHitControl->handleEvent( evt );
540 m_pLastHitControl = pNewHitControl;