1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: generic_window.cpp,v 1.3 2004/02/29 16:49:55 asmax Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 #include "generic_window.hpp"
26 #include "generic_layout.hpp"
27 #include "os_graphics.hpp"
28 #include "os_window.hpp"
29 #include "os_factory.hpp"
31 #include "ft2_font.hpp"
32 #include "tooltip.hpp"
33 #include "dialogs.hpp"
34 #include "var_manager.hpp"
35 #include "../commands/cmd_on_top.hpp"
36 #include "../controls/ctrl_generic.hpp"
37 #include "../events/evt_enter.hpp"
38 #include "../events/evt_focus.hpp"
39 #include "../events/evt_leave.hpp"
40 #include "../events/evt_motion.hpp"
41 #include "../events/evt_mouse.hpp"
42 #include "../events/evt_key.hpp"
43 #include "../events/evt_refresh.hpp"
44 #include "../events/evt_special.hpp"
45 #include "../events/evt_scroll.hpp"
46 #include "../utils/position.hpp"
47 #include "../utils/ustring.hpp"
52 GenericWindow::GenericWindow( intf_thread_t *pIntf, int left, int top,
53 WindowManager &rWindowManager,
54 const GenericFont &rTipFont,
55 bool dragDrop, bool playOnDrop ):
56 SkinObject( pIntf ), m_rWindowManager( rWindowManager ),
57 m_left( left ), m_top( top ), m_width( 0 ), m_height( 0 ),
58 m_pActiveLayout( NULL ), m_pLastHitControl( NULL ),
59 m_pCapturingControl( NULL ), m_pFocusControl( NULL ), m_varVisible( pIntf )
61 // Register as a moving window
62 m_rWindowManager.registerWindow( this );
65 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
67 // Create an OSWindow to handle OS specific processing
68 m_pOsWindow = pOsFactory->createOSWindow( *this, dragDrop, playOnDrop );
70 // Create the tooltip window
71 m_pTooltip = new Tooltip( getIntf(), rTipFont, 500 );
73 // Observe the visibility variable
74 m_varVisible.addObserver( this );
78 GenericWindow::~GenericWindow()
80 m_varVisible.delObserver( this );
81 // Unregister from the window manager
82 m_rWindowManager.unregisterWindow( this );
95 void GenericWindow::processEvent( EvtFocus &rEvtFocus )
97 // fprintf(stderr, rEvtFocus.getAsString().c_str()) ;
101 void GenericWindow::processEvent( EvtMotion &rEvtMotion )
103 // New control hit by the mouse
104 CtrlGeneric *pNewHitControl =
105 findHitControl( rEvtMotion.getXPos() - m_left,
106 rEvtMotion.getYPos() - m_top );
108 setLastHit( pNewHitControl );
110 /// Update the help text
111 VarManager *pVarManager = VarManager::instance( getIntf() );
114 pVarManager->getHelpText().set( pNewHitControl->getHelpText() );
117 // Send a motion event to the hit control, or to the control
118 // that captured the mouse, if any
119 CtrlGeneric *pActiveControl = pNewHitControl;
120 if( m_pCapturingControl )
122 pActiveControl = m_pCapturingControl;
126 // Compute the coordinates relative to the window
127 int xPos = rEvtMotion.getXPos() - m_left;
128 int yPos = rEvtMotion.getYPos() - m_top;
129 // Send a motion event
130 EvtMotion evt( getIntf(), xPos, yPos );
131 pActiveControl->handleEvent( evt );
136 void GenericWindow::processEvent( EvtLeave &rEvtLeave )
138 // No more hit control
141 if( !m_pCapturingControl )
148 void GenericWindow::processEvent( EvtMouse &rEvtMouse )
150 // Get the control hit by the mouse
151 CtrlGeneric *pNewHitControl = findHitControl( rEvtMouse.getXPos(),
152 rEvtMouse.getYPos() );
153 setLastHit( pNewHitControl );
155 // Change the focused control
156 if( rEvtMouse.getAction() == EvtMouse::kDown )
158 // Raise all the windows
159 m_rWindowManager.raise( this );
161 if( pNewHitControl && pNewHitControl->isFocusable() )
163 // If a new control gains the focus, the previous one loses it
164 if( m_pFocusControl && m_pFocusControl != pNewHitControl )
166 EvtFocus evt( getIntf(), false );
167 m_pFocusControl->handleEvent( evt );
169 if( pNewHitControl != m_pFocusControl )
171 m_pFocusControl = pNewHitControl;
172 EvtFocus evt( getIntf(), false );
173 pNewHitControl->handleEvent( evt );
176 else if( m_pFocusControl )
178 // The previous control loses the focus
179 EvtFocus evt( getIntf(), false );
180 m_pFocusControl->handleEvent( evt );
181 m_pFocusControl = NULL;
185 // Send a mouse event to the hit control, or to the control
186 // that captured the mouse, if any
187 CtrlGeneric *pActiveControl = pNewHitControl;
188 if( m_pCapturingControl )
190 pActiveControl = m_pCapturingControl;
194 pActiveControl->handleEvent( rEvtMouse );
199 void GenericWindow::processEvent( EvtKey &rEvtKey )
201 // Forward the event to the focused control, if any
202 if( m_pFocusControl )
204 m_pFocusControl->handleEvent( rEvtKey );
207 // Only do the action when the key is down
208 else if( rEvtKey.getAsString().find( "key:down") != string::npos )
210 //XXX not to be hardcoded !
211 // Ctrl-S = Change skin
212 if( (rEvtKey.getMod() & EvtInput::kModCtrl) &&
213 rEvtKey.getKey() == 's' )
215 Dialogs::instance( getIntf() )->showChangeSkin();
219 //XXX not to be hardcoded !
220 // Ctrl-T = Toggle on top
221 if( (rEvtKey.getMod() & EvtInput::kModCtrl) &&
222 rEvtKey.getKey() == 't' )
224 CmdOnTop cmd( getIntf() );
231 val.i_int = rEvtKey.getKey();
233 if( rEvtKey.getMod() & EvtInput::kModAlt )
235 val.i_int |= KEY_MODIFIER_ALT;
237 if( rEvtKey.getMod() & EvtInput::kModCtrl )
239 val.i_int |= KEY_MODIFIER_CTRL;
241 if( rEvtKey.getMod() & EvtInput::kModShift )
243 val.i_int |= KEY_MODIFIER_SHIFT;
246 var_Set( getIntf()->p_vlc, "key-pressed", val );
251 void GenericWindow::processEvent( EvtRefresh &rEvtRefresh )
253 // Refresh the given area
254 refresh( rEvtRefresh.getXStart(), rEvtRefresh.getYStart(),
255 rEvtRefresh.getWidth(), rEvtRefresh.getHeight() );
259 void GenericWindow::processEvent( EvtScroll &rEvtScroll )
264 // Get the control hit by the mouse
265 CtrlGeneric *pNewHitControl = findHitControl( rEvtScroll.getXPos(),
266 rEvtScroll.getYPos());
268 setLastHit( pNewHitControl );
270 // Send a mouse event to the hit control, or to the control
271 // that captured the mouse, if any
272 CtrlGeneric *pActiveControl = pNewHitControl;
274 if( m_pCapturingControl )
276 pActiveControl = m_pCapturingControl;
280 pActiveControl->handleEvent( rEvtScroll );
285 void GenericWindow::forwardEvent( EvtGeneric &rEvt, CtrlGeneric &rCtrl )
287 // XXX: We should do some checks here
288 rCtrl.handleEvent( rEvt );
292 void GenericWindow::show()
294 m_varVisible.set( true );
298 void GenericWindow::hide()
300 m_varVisible.set( false );
304 void GenericWindow::refresh( int left, int top, int width, int height )
306 if( m_pActiveLayout )
308 m_pActiveLayout->getImage()->copyToWindow( *m_pOsWindow, left, top,
309 width, height, left, top );
314 void GenericWindow::move( int left, int top )
316 // Update the window coordinates
320 m_pOsWindow->moveResize( left, top, m_width, m_height );
324 void GenericWindow::resize( int width, int height )
326 // Update the window size
330 m_pOsWindow->moveResize( m_left, m_top, width, height );
334 void GenericWindow::raise()
336 m_pOsWindow->raise();
340 void GenericWindow::setOpacity( uint8_t value )
342 m_pOsWindow->setOpacity( value );
346 void GenericWindow::toggleOnTop( bool onTop )
348 m_pOsWindow->toggleOnTop( onTop );
352 void GenericWindow::setActiveLayout( GenericLayout *pLayout )
354 pLayout->setWindow( this );
355 m_pActiveLayout = pLayout;
356 // Get the size of the layout and resize the window
357 m_width = pLayout->getWidth();
358 m_height = pLayout->getHeight();
359 m_pOsWindow->moveResize( m_left, m_top, m_width, m_height );
361 pLayout->refreshAll();
365 void GenericWindow::updateShape()
367 // Set the shape of the window
368 if( m_pActiveLayout )
370 OSGraphics *pImage = m_pActiveLayout->getImage();
373 pImage->applyMaskToWindow( *m_pOsWindow );
379 const list<Anchor*> GenericWindow::getAnchorList() const
385 void GenericWindow::addAnchor( Anchor *pAnchor )
387 m_anchorList.push_back( pAnchor );
391 void GenericWindow::onControlCapture( const CtrlGeneric &rCtrl )
393 // Set the capturing control
394 m_pCapturingControl = (CtrlGeneric*) &rCtrl;
398 void GenericWindow::onControlRelease( const CtrlGeneric &rCtrl )
400 // Release the capturing control
401 if( m_pCapturingControl == &rCtrl )
403 m_pCapturingControl = NULL;
407 msg_Dbg( getIntf(), "Control had not captured the mouse" );
410 // Send an enter event to the control under the mouse, if it doesn't
411 // have received it yet
412 if( m_pLastHitControl && m_pLastHitControl != &rCtrl )
414 EvtEnter evt( getIntf() );
415 m_pLastHitControl->handleEvent( evt );
419 UString tipText = m_pLastHitControl->getTooltipText();
420 if( tipText.length() > 0 )
422 // Set the tooltip text variable
423 VarManager *pVarManager = VarManager::instance( getIntf() );
424 pVarManager->getTooltipText().set( tipText );
431 void GenericWindow::onTooltipChange( const CtrlGeneric &rCtrl )
433 // Check that the control is the active one
434 if( m_pLastHitControl && m_pLastHitControl == &rCtrl )
436 // Set the tooltip text variable
437 VarManager *pVarManager = VarManager::instance( getIntf() );
438 pVarManager->getTooltipText().set( rCtrl.getTooltipText() );
443 void GenericWindow::onUpdate( Subject<VarBool> &rVariable )
445 if( m_varVisible.get() )
456 void GenericWindow::innerShow()
458 // First, refresh the layout and update the shape of the window
459 if( m_pActiveLayout )
462 m_pActiveLayout->refreshAll();
467 m_pOsWindow->show( m_left, m_top );
472 void GenericWindow::innerHide()
481 CtrlGeneric *GenericWindow::findHitControl( int xPos, int yPos )
483 if( m_pActiveLayout == NULL )
488 // Get the controls in the active layout
489 const list<LayeredControl> &ctrlList = m_pActiveLayout->getControlList();
490 list<LayeredControl>::const_reverse_iterator iter;
492 // New control hit by the mouse
493 CtrlGeneric *pNewHitControl = NULL;
495 // Loop on the control list to find the uppest hit control
496 for( iter = ctrlList.rbegin(); iter != ctrlList.rend(); iter++ )
498 // Get the position of the control in the layout
499 const Position *pos = (*iter).m_pControl->getPosition();
502 // Compute the coordinates of the mouse relative to the control
503 int xRel = xPos - pos->getLeft();
504 int yRel = yPos - pos->getTop();
506 CtrlGeneric *pCtrl = (*iter).m_pControl;
508 if( pCtrl->isVisible() && pCtrl->mouseOver( xRel, yRel ) )
510 pNewHitControl = (*iter).m_pControl;
516 msg_Dbg( getIntf(), "Control at NULL position" );
520 // If the hit control has just been entered, send it an enter event
521 if( pNewHitControl && (pNewHitControl != m_pLastHitControl) )
523 // Don't send the event if another control captured the mouse
524 if( !m_pCapturingControl || (m_pCapturingControl == pNewHitControl ) )
526 EvtEnter evt( getIntf() );
527 pNewHitControl->handleEvent( evt );
529 if( !m_pCapturingControl )
533 UString tipText = pNewHitControl->getTooltipText();
534 if( tipText.length() > 0 )
536 // Set the tooltip text variable
537 VarManager *pVarManager = VarManager::instance( getIntf() );
538 pVarManager->getTooltipText().set( tipText );
545 return pNewHitControl;
550 void GenericWindow::setLastHit( CtrlGeneric *pNewHitControl )
552 // Send a leave event to the left control
553 if( m_pLastHitControl && (pNewHitControl != m_pLastHitControl) )
555 // Don't send the event if another control captured the mouse
556 if( !m_pCapturingControl || (m_pCapturingControl == m_pLastHitControl))
558 EvtLeave evt( getIntf() );
559 m_pLastHitControl->handleEvent( evt );
563 m_pLastHitControl = pNewHitControl;