]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/top_window.cpp
udev: handle "change" action properly, fix disc ejection/insertion
[vlc] / modules / gui / skins2 / src / top_window.cpp
1 /*****************************************************************************
2  * top_window.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
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"
30 #include "theme.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"
47
48 #include <vlc_keys.h>
49
50
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 )
58 {
59     // Register as a moving window
60     m_rWindowManager.registerWindow( *this );
61
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 ) );
65 }
66
67
68 TopWindow::~TopWindow()
69 {
70     // Unregister from the window manager
71     m_rWindowManager.unregisterWindow( *this );
72 }
73
74
75 void TopWindow::processEvent( EvtRefresh &rEvtRefresh )
76 {
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 )
80     {
81         GenericWindow::processEvent( rEvtRefresh );
82     }
83     else
84     {
85         m_pActiveLayout->refreshRect( rEvtRefresh.getXStart(),
86                                       rEvtRefresh.getYStart(),
87                                       rEvtRefresh.getWidth(),
88                                       rEvtRefresh.getHeight() );
89     }
90 }
91
92
93 void TopWindow::processEvent( EvtFocus &rEvtFocus )
94 {
95 //    fprintf(stderr, rEvtFocus.getAsString().c_str());
96 }
97
98
99 void TopWindow::processEvent( EvtMenu &rEvtMenu )
100 {
101     Popup *pPopup = m_rWindowManager.getActivePopup();
102     // We should never receive a menu event when there is no active popup!
103     if( pPopup == NULL )
104     {
105         msg_Warn( getIntf(), "unexpected menu event, ignoring" );
106         return;
107     }
108
109     pPopup->handleEvent( rEvtMenu );
110 }
111
112
113 void TopWindow::processEvent( EvtMotion &rEvtMotion )
114 {
115     // New control hit by the mouse
116     CtrlGeneric *pNewHitControl =
117         findHitControl( rEvtMotion.getXPos() - getLeft(),
118                         rEvtMotion.getYPos() - getTop() );
119
120     setLastHit( pNewHitControl );
121
122     /// Update the help text
123     VarManager *pVarManager = VarManager::instance( getIntf() );
124     if( pNewHitControl )
125     {
126         pVarManager->getHelpText().set( pNewHitControl->getHelpText() );
127     }
128
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 )
133     {
134         pActiveControl = m_pCapturingControl;
135     }
136     if( pActiveControl )
137     {
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 );
144     }
145 }
146
147
148 void TopWindow::processEvent( EvtLeave &rEvtLeave )
149 {
150     // No more hit control
151     setLastHit( NULL );
152
153     if( !m_pCapturingControl )
154     {
155         m_rWindowManager.hideTooltip();
156     }
157 }
158
159
160 void TopWindow::processEvent( EvtMouse &rEvtMouse )
161 {
162     // Get the control hit by the mouse
163     CtrlGeneric *pNewHitControl = findHitControl( rEvtMouse.getXPos(),
164                                                   rEvtMouse.getYPos() );
165     setLastHit( pNewHitControl );
166
167     // Change the focused control
168     if( rEvtMouse.getAction() == EvtMouse::kDown )
169     {
170         // Raise the window
171         m_rWindowManager.raise( *this );
172
173         if( pNewHitControl && pNewHitControl->isFocusable() )
174         {
175             // If a new control gains the focus, the previous one loses it
176             if( m_pFocusControl && m_pFocusControl != pNewHitControl )
177             {
178                 EvtFocus evt( getIntf(), false );
179                 m_pFocusControl->handleEvent( evt );
180             }
181             if( pNewHitControl != m_pFocusControl )
182             {
183                 m_pFocusControl = pNewHitControl;
184                 EvtFocus evt( getIntf(), false );
185                 pNewHitControl->handleEvent( evt );
186             }
187         }
188         else if( m_pFocusControl )
189         {
190             // The previous control loses the focus
191             EvtFocus evt( getIntf(), false );
192             m_pFocusControl->handleEvent( evt );
193             m_pFocusControl = NULL;
194         }
195     }
196
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 )
201     {
202         pActiveControl = m_pCapturingControl;
203     }
204     if( pActiveControl )
205     {
206         pActiveControl->handleEvent( rEvtMouse );
207     }
208 }
209
210
211 void TopWindow::processEvent( EvtKey &rEvtKey )
212 {
213     // Forward the event to the focused control, if any
214     if( m_pFocusControl )
215     {
216         m_pFocusControl->handleEvent( rEvtKey );
217         return;
218     }
219
220     // Only do the action when the key is down
221     if( rEvtKey.getAsString().find( "key:down") != string::npos )
222     {
223         //XXX not to be hardcoded!
224         // Ctrl-S = Change skin
225         if( (rEvtKey.getMod() & EvtInput::kModCtrl) &&
226             rEvtKey.getKey() == 's' )
227         {
228             CmdDlgChangeSkin cmd( getIntf() );
229             cmd.execute();
230             return;
231         }
232
233         //XXX not to be hardcoded!
234         // Ctrl-T = Toggle on top
235         if( (rEvtKey.getMod() & EvtInput::kModCtrl) &&
236             rEvtKey.getKey() == 't' )
237         {
238             CmdOnTop cmd( getIntf() );
239             cmd.execute();
240             return;
241         }
242
243         vlc_value_t val;
244         // Set the key
245         val.i_int = rEvtKey.getKey();
246         // Set the modifiers
247         if( rEvtKey.getMod() & EvtInput::kModAlt )
248         {
249             val.i_int |= KEY_MODIFIER_ALT;
250         }
251         if( rEvtKey.getMod() & EvtInput::kModCtrl )
252         {
253             val.i_int |= KEY_MODIFIER_CTRL;
254         }
255         if( rEvtKey.getMod() & EvtInput::kModShift )
256         {
257             val.i_int |= KEY_MODIFIER_SHIFT;
258         }
259
260         var_Set( getIntf()->p_libvlc, "key-pressed", val );
261     }
262
263     // Always store the modifier, which can be needed for scroll events
264     m_currModifier = rEvtKey.getMod();
265 }
266
267
268 void TopWindow::processEvent( EvtScroll &rEvtScroll )
269 {
270     // Raise the windows
271     raise();
272
273     // Get the control hit by the mouse
274     CtrlGeneric *pNewHitControl = findHitControl( rEvtScroll.getXPos(),
275                                                   rEvtScroll.getYPos());
276     setLastHit( pNewHitControl );
277
278     // Send a mouse event to the hit control, or to the control
279     // that captured the mouse, if any
280     CtrlGeneric *pActiveControl = pNewHitControl;
281
282     if( m_pCapturingControl )
283     {
284         pActiveControl = m_pCapturingControl;
285     }
286     if( pActiveControl )
287     {
288         pActiveControl->handleEvent( rEvtScroll );
289     }
290     else
291     {
292         // Treat the scroll event as a hotkey
293         vlc_value_t val;
294         if( rEvtScroll.getDirection() == EvtScroll::kUp )
295         {
296             val.i_int = KEY_MOUSEWHEELUP;
297         }
298         else
299         {
300             val.i_int = KEY_MOUSEWHEELDOWN;
301         }
302         // Add the modifiers
303         val.i_int |= m_currModifier;
304
305         var_Set( getIntf()->p_libvlc, "key-pressed", val );
306     }
307 }
308
309
310 void TopWindow::forwardEvent( EvtGeneric &rEvt, CtrlGeneric &rCtrl )
311 {
312     // XXX: We should do some checks here
313     rCtrl.handleEvent( rEvt );
314 }
315
316
317 void TopWindow::refresh( int left, int top, int width, int height )
318 {
319     if( m_pActiveLayout )
320     {
321         m_pActiveLayout->getImage()->copyToWindow( *getOSWindow(), left, top,
322                                                    width, height, left, top );
323     }
324 }
325
326
327 void TopWindow::setActiveLayout( GenericLayout *pLayout )
328 {
329     bool isVisible = getVisibleVar().get();
330     if( m_pActiveLayout )
331     {
332         if( isVisible )
333         {
334             m_pActiveLayout->onHide();
335         }
336         // The current layout becomes inactive
337         m_pActiveLayout->getActiveVar().set( false );
338     }
339
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() );
344
345     updateShape();
346     if( isVisible )
347     {
348         pLayout->onShow();
349     }
350
351     // The new layout is active
352     pLayout->getActiveVar().set( true );
353 }
354
355
356 const GenericLayout& TopWindow::getActiveLayout() const
357 {
358     return *m_pActiveLayout;
359 }
360
361
362 void TopWindow::innerShow()
363 {
364     // First, refresh the layout and update the shape of the window
365     if( m_pActiveLayout )
366     {
367         updateShape();
368         m_pActiveLayout->onShow();
369     }
370
371     // Show the window
372     GenericWindow::innerShow();
373
374     // place the top window on the screen (after show!)
375     move( getLeft(), getTop() );
376 }
377
378
379 void TopWindow::innerHide()
380 {
381     if( m_pActiveLayout )
382     {
383         // Notify the active layout
384         m_pActiveLayout->onHide();
385     }
386     // Hide the window
387     GenericWindow::innerHide();
388 }
389
390
391 void TopWindow::updateShape()
392 {
393     // Set the shape of the window
394     if( m_pActiveLayout )
395     {
396         OSGraphics *pImage = m_pActiveLayout->getImage();
397         if( pImage )
398         {
399             pImage->applyMaskToWindow( *getOSWindow() );
400         }
401     }
402 }
403
404
405 void TopWindow::onControlCapture( const CtrlGeneric &rCtrl )
406 {
407     // Set the capturing control
408     m_pCapturingControl = (CtrlGeneric*) &rCtrl;
409 }
410
411
412 void TopWindow::onControlRelease( const CtrlGeneric &rCtrl )
413 {
414     // Release the capturing control
415     if( m_pCapturingControl == &rCtrl )
416     {
417         m_pCapturingControl = NULL;
418     }
419     else
420     {
421         msg_Dbg( getIntf(), "control had not captured the mouse" );
422     }
423
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 )
427     {
428         EvtEnter evt( getIntf() );
429         m_pLastHitControl->handleEvent( evt );
430
431         // Show the tooltip
432         m_rWindowManager.hideTooltip();
433         UString tipText = m_pLastHitControl->getTooltipText();
434         if( tipText.length() > 0 )
435         {
436             // Set the tooltip text variable
437             VarManager *pVarManager = VarManager::instance( getIntf() );
438             pVarManager->getTooltipText().set( tipText );
439             m_rWindowManager.showTooltip();
440         }
441     }
442 }
443
444
445 void TopWindow::onTooltipChange( const CtrlGeneric &rCtrl )
446 {
447     // Check that the control is the active one
448     if( m_pLastHitControl && m_pLastHitControl == &rCtrl )
449     {
450         if( rCtrl.getTooltipText().size() )
451         {
452             // Set the tooltip text variable
453             VarManager *pVarManager = VarManager::instance( getIntf() );
454             pVarManager->getTooltipText().set( rCtrl.getTooltipText() );
455             m_rWindowManager.showTooltip();
456         }
457         else
458         {
459             // Nothing to display, so hide the tooltip
460             m_rWindowManager.hideTooltip();
461         }
462     }
463 }
464
465
466 CtrlGeneric *TopWindow::findHitControl( int xPos, int yPos )
467 {
468     if( m_pActiveLayout == NULL )
469     {
470         return NULL;
471     }
472
473     // Get the controls in the active layout
474     const list<LayeredControl> &ctrlList = m_pActiveLayout->getControlList();
475     list<LayeredControl>::const_reverse_iterator iter;
476
477     // New control hit by the mouse
478     CtrlGeneric *pNewHitControl = NULL;
479
480     // Loop on the control list to find the uppest hit control
481     for( iter = ctrlList.rbegin(); iter != ctrlList.rend(); iter++ )
482     {
483         // Get the position of the control in the layout
484         const Position *pos = (*iter).m_pControl->getPosition();
485         if( pos != NULL )
486         {
487             // Compute the coordinates of the mouse relative to the control
488             int xRel = xPos - pos->getLeft();
489             int yRel = yPos - pos->getTop();
490
491             CtrlGeneric *pCtrl = (*iter).m_pControl;
492             // Control hit ?
493             if( pCtrl->isVisible() && pCtrl->mouseOver( xRel, yRel ) )
494             {
495                 pNewHitControl = (*iter).m_pControl;
496                 break;
497             }
498         }
499         else
500         {
501             msg_Dbg( getIntf(), "control at NULL position" );
502         }
503     }
504
505     // If the hit control has just been entered, send it an enter event
506     if( pNewHitControl && (pNewHitControl != m_pLastHitControl) )
507     {
508         // Don't send the event if another control captured the mouse
509         if( !m_pCapturingControl || (m_pCapturingControl == pNewHitControl ) )
510         {
511             EvtEnter evt( getIntf() );
512             pNewHitControl->handleEvent( evt );
513
514             if( !m_pCapturingControl )
515             {
516                 // Show the tooltip
517                 m_rWindowManager.hideTooltip();
518                 UString tipText = pNewHitControl->getTooltipText();
519                 if( tipText.length() > 0 )
520                 {
521                     // Set the tooltip text variable
522                     VarManager *pVarManager = VarManager::instance( getIntf() );
523                     pVarManager->getTooltipText().set( tipText );
524                     m_rWindowManager.showTooltip();
525                 }
526             }
527         }
528     }
529
530     return pNewHitControl;
531 }
532
533
534
535 void TopWindow::setLastHit( CtrlGeneric *pNewHitControl )
536 {
537     // Send a leave event to the left control
538     if( m_pLastHitControl && (pNewHitControl != m_pLastHitControl) )
539     {
540         // Don't send the event if another control captured the mouse
541         if( !m_pCapturingControl || (m_pCapturingControl == m_pLastHitControl))
542         {
543             EvtLeave evt( getIntf() );
544             m_pLastHitControl->handleEvent( evt );
545         }
546     }
547
548     m_pLastHitControl = pNewHitControl;
549 }
550