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.getKeyState() == EvtKey::kDown )
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() );
243 var_SetInteger( getIntf()->p_libvlc, "key-pressed",
244 rEvtKey.getModKey() );
247 // Always store the modifier, which can be needed for scroll events.
248 m_currModifier = rEvtKey.getMod();
251 void TopWindow::processEvent( EvtScroll &rEvtScroll )
256 // Get the control hit by the mouse
257 CtrlGeneric *pNewHitControl = findHitControl( rEvtScroll.getXPos(),
258 rEvtScroll.getYPos());
259 setLastHit( pNewHitControl );
261 // Send a mouse event to the hit control, or to the control
262 // that captured the mouse, if any
263 CtrlGeneric *pActiveControl = pNewHitControl;
265 if( m_pCapturingControl )
267 pActiveControl = m_pCapturingControl;
271 pActiveControl->handleEvent( rEvtScroll );
275 // Treat the scroll event as a hotkey plus current modifiers
276 int i = (rEvtScroll.getDirection() == EvtScroll::kUp ?
277 KEY_MOUSEWHEELUP : KEY_MOUSEWHEELDOWN) | m_currModifier;
279 var_SetInteger( getIntf()->p_libvlc, "key-pressed", i );
284 void TopWindow::forwardEvent( EvtGeneric &rEvt, CtrlGeneric &rCtrl )
286 // XXX: We should do some checks here
287 rCtrl.handleEvent( rEvt );
291 void TopWindow::refresh( int left, int top, int width, int height )
293 if( m_pActiveLayout )
295 m_pActiveLayout->getImage()->copyToWindow( *getOSWindow(), left, top,
296 width, height, left, top );
301 void TopWindow::setActiveLayout( GenericLayout *pLayout )
303 bool isVisible = getVisibleVar().get();
304 if( m_pActiveLayout )
308 m_pActiveLayout->onHide();
310 // The current layout becomes inactive
311 m_pActiveLayout->getActiveVar().set( false );
314 pLayout->setWindow( this );
315 m_pActiveLayout = pLayout;
316 // Get the size of the layout and resize the window
317 resize( pLayout->getWidth(), pLayout->getHeight() );
324 // The new layout is active
325 pLayout->getActiveVar().set( true );
329 const GenericLayout& TopWindow::getActiveLayout() const
331 return *m_pActiveLayout;
335 void TopWindow::innerShow()
337 // First, refresh the layout
338 if( m_pActiveLayout )
340 m_pActiveLayout->onShow();
344 GenericWindow::innerShow();
346 // place the top window on the screen (after show!)
347 move( getLeft(), getTop() );
351 void TopWindow::innerHide()
353 if( m_pActiveLayout )
355 // Notify the active layout
356 m_pActiveLayout->onHide();
359 GenericWindow::innerHide();
363 void TopWindow::updateShape()
365 // Set the shape of the window
366 if( m_pActiveLayout )
368 OSGraphics *pImage = m_pActiveLayout->getImage();
371 pImage->applyMaskToWindow( *getOSWindow() );
377 void TopWindow::onControlCapture( const CtrlGeneric &rCtrl )
379 // Set the capturing control
380 m_pCapturingControl = (CtrlGeneric*) &rCtrl;
384 void TopWindow::onControlRelease( const CtrlGeneric &rCtrl )
386 // Release the capturing control
387 if( m_pCapturingControl == &rCtrl )
389 m_pCapturingControl = NULL;
393 msg_Dbg( getIntf(), "control had not captured the mouse" );
396 // Send an enter event to the control under the mouse, if it doesn't
397 // have received it yet
398 if( m_pLastHitControl && m_pLastHitControl != &rCtrl )
400 EvtEnter evt( getIntf() );
401 m_pLastHitControl->handleEvent( evt );
404 m_rWindowManager.hideTooltip();
405 UString tipText = m_pLastHitControl->getTooltipText();
406 if( tipText.length() > 0 )
408 // Set the tooltip text variable
409 VarManager *pVarManager = VarManager::instance( getIntf() );
410 pVarManager->getTooltipText().set( tipText );
411 m_rWindowManager.showTooltip();
417 void TopWindow::onTooltipChange( const CtrlGeneric &rCtrl )
419 // Check that the control is the active one
420 if( m_pLastHitControl && m_pLastHitControl == &rCtrl )
422 if( rCtrl.getTooltipText().size() )
424 // Set the tooltip text variable
425 VarManager *pVarManager = VarManager::instance( getIntf() );
426 pVarManager->getTooltipText().set( rCtrl.getTooltipText() );
427 m_rWindowManager.showTooltip();
431 // Nothing to display, so hide the tooltip
432 m_rWindowManager.hideTooltip();
438 CtrlGeneric *TopWindow::findHitControl( int xPos, int yPos )
440 if( m_pActiveLayout == NULL )
445 // Get the controls in the active layout
446 const list<LayeredControl> &ctrlList = m_pActiveLayout->getControlList();
447 list<LayeredControl>::const_reverse_iterator iter;
449 // New control hit by the mouse
450 CtrlGeneric *pNewHitControl = NULL;
452 // Loop on the control list to find the uppest hit control
453 for( iter = ctrlList.rbegin(); iter != ctrlList.rend(); iter++ )
455 // Get the position of the control in the layout
456 const Position *pos = (*iter).m_pControl->getPosition();
459 // Compute the coordinates of the mouse relative to the control
460 int xRel = xPos - pos->getLeft();
461 int yRel = yPos - pos->getTop();
463 CtrlGeneric *pCtrl = (*iter).m_pControl;
465 if( pCtrl->isVisible() && pCtrl->mouseOver( xRel, yRel ) )
467 pNewHitControl = (*iter).m_pControl;
473 msg_Dbg( getIntf(), "control at NULL position" );
477 // If the hit control has just been entered, send it an enter event
478 if( pNewHitControl && (pNewHitControl != m_pLastHitControl) )
480 // Don't send the event if another control captured the mouse
481 if( !m_pCapturingControl || (m_pCapturingControl == pNewHitControl ) )
483 EvtEnter evt( getIntf() );
484 pNewHitControl->handleEvent( evt );
486 if( !m_pCapturingControl )
489 m_rWindowManager.hideTooltip();
490 UString tipText = pNewHitControl->getTooltipText();
491 if( tipText.length() > 0 )
493 // Set the tooltip text variable
494 VarManager *pVarManager = VarManager::instance( getIntf() );
495 pVarManager->getTooltipText().set( tipText );
496 m_rWindowManager.showTooltip();
502 return pNewHitControl;
507 void TopWindow::setLastHit( CtrlGeneric *pNewHitControl )
509 // Send a leave event to the left control
510 if( m_pLastHitControl && (pNewHitControl != m_pLastHitControl) )
512 // Don't send the event if another control captured the mouse
513 if( !m_pCapturingControl || (m_pCapturingControl == m_pLastHitControl))
515 EvtLeave evt( getIntf() );
516 m_pLastHitControl->handleEvent( evt );
520 m_pLastHitControl = pNewHitControl;