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 );
62 // Create the "maximized" variable and register it in the manager
63 m_pVarMaximized = new VarBoolImpl( pIntf );
64 VarManager::instance( pIntf )->registerVar( VariablePtr( m_pVarMaximized ) );
68 TopWindow::~TopWindow()
70 // Unregister from the window manager
71 m_rWindowManager.unregisterWindow( *this );
75 void TopWindow::processEvent( EvtRefresh &rEvtRefresh )
77 // We override the behaviour defined in GenericWindow, because we don't
78 // want to draw on a video control!
79 if( m_pActiveLayout == NULL )
81 GenericWindow::processEvent( rEvtRefresh );
85 m_pActiveLayout->refreshRect( rEvtRefresh.getXStart(),
86 rEvtRefresh.getYStart(),
87 rEvtRefresh.getWidth(),
88 rEvtRefresh.getHeight() );
93 void TopWindow::processEvent( EvtFocus &rEvtFocus )
95 // fprintf(stderr, rEvtFocus.getAsString().c_str());
99 void TopWindow::processEvent( EvtMenu &rEvtMenu )
101 Popup *pPopup = m_rWindowManager.getActivePopup();
102 // We should never receive a menu event when there is no active popup!
105 msg_Warn( getIntf(), "unexpected menu event, ignoring" );
109 pPopup->handleEvent( rEvtMenu );
113 void TopWindow::processEvent( EvtMotion &rEvtMotion )
115 // New control hit by the mouse
116 CtrlGeneric *pNewHitControl =
117 findHitControl( rEvtMotion.getXPos() - getLeft(),
118 rEvtMotion.getYPos() - getTop() );
120 setLastHit( pNewHitControl );
122 /// Update the help text
123 VarManager *pVarManager = VarManager::instance( getIntf() );
126 pVarManager->getHelpText().set( pNewHitControl->getHelpText() );
129 // Send a motion event to the hit control, or to the control
130 // that captured the mouse, if any
131 CtrlGeneric *pActiveControl = pNewHitControl;
132 if( m_pCapturingControl )
134 pActiveControl = m_pCapturingControl;
138 // Compute the coordinates relative to the window
139 int xPos = rEvtMotion.getXPos() - getLeft();
140 int yPos = rEvtMotion.getYPos() - getTop();
141 // Send a motion event
142 EvtMotion evt( getIntf(), xPos, yPos );
143 pActiveControl->handleEvent( evt );
148 void TopWindow::processEvent( EvtLeave &rEvtLeave )
150 // No more hit control
153 if( !m_pCapturingControl )
155 m_rWindowManager.hideTooltip();
160 void TopWindow::processEvent( EvtMouse &rEvtMouse )
162 // Get the control hit by the mouse
163 CtrlGeneric *pNewHitControl = findHitControl( rEvtMouse.getXPos(),
164 rEvtMouse.getYPos() );
165 setLastHit( pNewHitControl );
167 // Change the focused control
168 if( rEvtMouse.getAction() == EvtMouse::kDown )
171 m_rWindowManager.raise( *this );
173 if( pNewHitControl && pNewHitControl->isFocusable() )
175 // If a new control gains the focus, the previous one loses it
176 if( m_pFocusControl && m_pFocusControl != pNewHitControl )
178 EvtFocus evt( getIntf(), false );
179 m_pFocusControl->handleEvent( evt );
181 if( pNewHitControl != m_pFocusControl )
183 m_pFocusControl = pNewHitControl;
184 EvtFocus evt( getIntf(), false );
185 pNewHitControl->handleEvent( evt );
188 else if( m_pFocusControl )
190 // The previous control loses the focus
191 EvtFocus evt( getIntf(), false );
192 m_pFocusControl->handleEvent( evt );
193 m_pFocusControl = NULL;
197 // Send a mouse event to the hit control, or to the control
198 // that captured the mouse, if any
199 CtrlGeneric *pActiveControl = pNewHitControl;
200 if( m_pCapturingControl )
202 pActiveControl = m_pCapturingControl;
206 pActiveControl->handleEvent( rEvtMouse );
211 void TopWindow::processEvent( EvtKey &rEvtKey )
213 // Forward the event to the focused control, if any
214 if( m_pFocusControl )
216 m_pFocusControl->handleEvent( rEvtKey );
220 // Only do the action when the key is down
221 if( rEvtKey.getAsString().find( "key:down") != string::npos )
223 //XXX not to be hardcoded!
224 // Ctrl-S = Change skin
225 if( (rEvtKey.getMod() & EvtInput::kModCtrl) &&
226 rEvtKey.getKey() == 's' )
228 CmdDlgChangeSkin cmd( getIntf() );
233 //XXX not to be hardcoded!
234 // Ctrl-T = Toggle on top
235 if( (rEvtKey.getMod() & EvtInput::kModCtrl) &&
236 rEvtKey.getKey() == 't' )
238 CmdOnTop cmd( getIntf() );
245 val.i_int = rEvtKey.getKey();
247 if( rEvtKey.getMod() & EvtInput::kModAlt )
249 val.i_int |= KEY_MODIFIER_ALT;
251 if( rEvtKey.getMod() & EvtInput::kModCtrl )
253 val.i_int |= KEY_MODIFIER_CTRL;
255 if( rEvtKey.getMod() & EvtInput::kModShift )
257 val.i_int |= KEY_MODIFIER_SHIFT;
260 var_Set( getIntf()->p_libvlc, "key-pressed", val );
263 // Always store the modifier, which can be needed for scroll events
264 m_currModifier = rEvtKey.getMod();
268 void TopWindow::processEvent( EvtScroll &rEvtScroll )
273 // Get the control hit by the mouse
274 CtrlGeneric *pNewHitControl = findHitControl( rEvtScroll.getXPos(),
275 rEvtScroll.getYPos());
276 setLastHit( pNewHitControl );
278 // Send a mouse event to the hit control, or to the control
279 // that captured the mouse, if any
280 CtrlGeneric *pActiveControl = pNewHitControl;
282 if( m_pCapturingControl )
284 pActiveControl = m_pCapturingControl;
288 pActiveControl->handleEvent( rEvtScroll );
292 // Treat the scroll event as a hotkey
294 if( rEvtScroll.getDirection() == EvtScroll::kUp )
296 val.i_int = KEY_MOUSEWHEELUP;
300 val.i_int = KEY_MOUSEWHEELDOWN;
303 val.i_int |= m_currModifier;
305 var_Set( getIntf()->p_libvlc, "key-pressed", val );
310 void TopWindow::forwardEvent( EvtGeneric &rEvt, CtrlGeneric &rCtrl )
312 // XXX: We should do some checks here
313 rCtrl.handleEvent( rEvt );
317 void TopWindow::refresh( int left, int top, int width, int height )
319 if( m_pActiveLayout )
321 m_pActiveLayout->getImage()->copyToWindow( *getOSWindow(), left, top,
322 width, height, left, top );
327 void TopWindow::setActiveLayout( GenericLayout *pLayout )
329 bool isVisible = getVisibleVar().get();
330 if( m_pActiveLayout )
334 m_pActiveLayout->onHide();
336 // The current layout becomes inactive
337 m_pActiveLayout->getActiveVar().set( false );
340 pLayout->setWindow( this );
341 m_pActiveLayout = pLayout;
342 // Get the size of the layout and resize the window
343 resize( pLayout->getWidth(), pLayout->getHeight() );
351 // The new layout is active
352 pLayout->getActiveVar().set( true );
356 const GenericLayout& TopWindow::getActiveLayout() const
358 return *m_pActiveLayout;
362 void TopWindow::innerShow()
364 // First, refresh the layout and update the shape of the window
365 if( m_pActiveLayout )
368 m_pActiveLayout->onShow();
372 GenericWindow::innerShow();
374 // place the top window on the screen (after show!)
375 move( getLeft(), getTop() );
379 void TopWindow::innerHide()
381 if( m_pActiveLayout )
383 // Notify the active layout
384 m_pActiveLayout->onHide();
387 GenericWindow::innerHide();
391 void TopWindow::updateShape()
393 // Set the shape of the window
394 if( m_pActiveLayout )
396 OSGraphics *pImage = m_pActiveLayout->getImage();
399 pImage->applyMaskToWindow( *getOSWindow() );
405 void TopWindow::onControlCapture( const CtrlGeneric &rCtrl )
407 // Set the capturing control
408 m_pCapturingControl = (CtrlGeneric*) &rCtrl;
412 void TopWindow::onControlRelease( const CtrlGeneric &rCtrl )
414 // Release the capturing control
415 if( m_pCapturingControl == &rCtrl )
417 m_pCapturingControl = NULL;
421 msg_Dbg( getIntf(), "control had not captured the mouse" );
424 // Send an enter event to the control under the mouse, if it doesn't
425 // have received it yet
426 if( m_pLastHitControl && m_pLastHitControl != &rCtrl )
428 EvtEnter evt( getIntf() );
429 m_pLastHitControl->handleEvent( evt );
432 m_rWindowManager.hideTooltip();
433 UString tipText = m_pLastHitControl->getTooltipText();
434 if( tipText.length() > 0 )
436 // Set the tooltip text variable
437 VarManager *pVarManager = VarManager::instance( getIntf() );
438 pVarManager->getTooltipText().set( tipText );
439 m_rWindowManager.showTooltip();
445 void TopWindow::onTooltipChange( const CtrlGeneric &rCtrl )
447 // Check that the control is the active one
448 if( m_pLastHitControl && m_pLastHitControl == &rCtrl )
450 if( rCtrl.getTooltipText().size() )
452 // Set the tooltip text variable
453 VarManager *pVarManager = VarManager::instance( getIntf() );
454 pVarManager->getTooltipText().set( rCtrl.getTooltipText() );
455 m_rWindowManager.showTooltip();
459 // Nothing to display, so hide the tooltip
460 m_rWindowManager.hideTooltip();
466 CtrlGeneric *TopWindow::findHitControl( int xPos, int yPos )
468 if( m_pActiveLayout == NULL )
473 // Get the controls in the active layout
474 const list<LayeredControl> &ctrlList = m_pActiveLayout->getControlList();
475 list<LayeredControl>::const_reverse_iterator iter;
477 // New control hit by the mouse
478 CtrlGeneric *pNewHitControl = NULL;
480 // Loop on the control list to find the uppest hit control
481 for( iter = ctrlList.rbegin(); iter != ctrlList.rend(); iter++ )
483 // Get the position of the control in the layout
484 const Position *pos = (*iter).m_pControl->getPosition();
487 // Compute the coordinates of the mouse relative to the control
488 int xRel = xPos - pos->getLeft();
489 int yRel = yPos - pos->getTop();
491 CtrlGeneric *pCtrl = (*iter).m_pControl;
493 if( pCtrl->isVisible() && pCtrl->mouseOver( xRel, yRel ) )
495 pNewHitControl = (*iter).m_pControl;
501 msg_Dbg( getIntf(), "control at NULL position" );
505 // If the hit control has just been entered, send it an enter event
506 if( pNewHitControl && (pNewHitControl != m_pLastHitControl) )
508 // Don't send the event if another control captured the mouse
509 if( !m_pCapturingControl || (m_pCapturingControl == pNewHitControl ) )
511 EvtEnter evt( getIntf() );
512 pNewHitControl->handleEvent( evt );
514 if( !m_pCapturingControl )
517 m_rWindowManager.hideTooltip();
518 UString tipText = pNewHitControl->getTooltipText();
519 if( tipText.length() > 0 )
521 // Set the tooltip text variable
522 VarManager *pVarManager = VarManager::instance( getIntf() );
523 pVarManager->getTooltipText().set( tipText );
524 m_rWindowManager.showTooltip();
530 return pNewHitControl;
535 void TopWindow::setLastHit( CtrlGeneric *pNewHitControl )
537 // Send a leave event to the left control
538 if( m_pLastHitControl && (pNewHitControl != m_pLastHitControl) )
540 // Don't send the event if another control captured the mouse
541 if( !m_pCapturingControl || (m_pCapturingControl == m_pLastHitControl))
543 EvtLeave evt( getIntf() );
544 m_pLastHitControl->handleEvent( evt );
548 m_pLastHitControl = pNewHitControl;