]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/top_window.cpp
skins2: first proposal for a skinnable fullscreen controller (fsc)
[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.getKeyState() == EvtKey::kDown )
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         var_SetInteger( getIntf()->p_libvlc, "key-pressed",
244                         rEvtKey.getModKey() );
245     }
246
247     // Always store the modifier, which can be needed for scroll events.
248     m_currModifier = rEvtKey.getMod();
249 }
250
251 void TopWindow::processEvent( EvtScroll &rEvtScroll )
252 {
253     // Raise the windows
254     raise();
255
256     // Get the control hit by the mouse
257     CtrlGeneric *pNewHitControl = findHitControl( rEvtScroll.getXPos(),
258                                                   rEvtScroll.getYPos());
259     setLastHit( pNewHitControl );
260
261     // Send a mouse event to the hit control, or to the control
262     // that captured the mouse, if any
263     CtrlGeneric *pActiveControl = pNewHitControl;
264
265     if( m_pCapturingControl )
266     {
267         pActiveControl = m_pCapturingControl;
268     }
269     if( pActiveControl )
270     {
271         pActiveControl->handleEvent( rEvtScroll );
272     }
273     else
274     {
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;
278
279         var_SetInteger( getIntf()->p_libvlc, "key-pressed", i );
280     }
281 }
282
283
284 void TopWindow::forwardEvent( EvtGeneric &rEvt, CtrlGeneric &rCtrl )
285 {
286     // XXX: We should do some checks here
287     rCtrl.handleEvent( rEvt );
288 }
289
290
291 void TopWindow::refresh( int left, int top, int width, int height )
292 {
293     if( m_pActiveLayout )
294     {
295         m_pActiveLayout->getImage()->copyToWindow( *getOSWindow(), left, top,
296                                                    width, height, left, top );
297     }
298 }
299
300
301 void TopWindow::setActiveLayout( GenericLayout *pLayout )
302 {
303     bool isVisible = getVisibleVar().get();
304     if( m_pActiveLayout )
305     {
306         if( isVisible )
307         {
308             m_pActiveLayout->onHide();
309         }
310         // The current layout becomes inactive
311         m_pActiveLayout->getActiveVar().set( false );
312     }
313
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() );
318
319     if( isVisible )
320     {
321         pLayout->onShow();
322     }
323
324     // The new layout is active
325     pLayout->getActiveVar().set( true );
326 }
327
328
329 const GenericLayout& TopWindow::getActiveLayout() const
330 {
331     return *m_pActiveLayout;
332 }
333
334
335 void TopWindow::innerShow()
336 {
337     // First, refresh the layout
338     if( m_pActiveLayout )
339     {
340         m_pActiveLayout->onShow();
341     }
342
343     // Show the window
344     GenericWindow::innerShow();
345
346     // place the top window on the screen (after show!)
347     move( getLeft(), getTop() );
348 }
349
350
351 void TopWindow::innerHide()
352 {
353     if( m_pActiveLayout )
354     {
355         // Notify the active layout
356         m_pActiveLayout->onHide();
357     }
358     // Hide the window
359     GenericWindow::innerHide();
360 }
361
362
363 void TopWindow::updateShape()
364 {
365     // Set the shape of the window
366     if( m_pActiveLayout )
367     {
368         OSGraphics *pImage = m_pActiveLayout->getImage();
369         if( pImage )
370         {
371             pImage->applyMaskToWindow( *getOSWindow() );
372         }
373     }
374 }
375
376
377 void TopWindow::onControlCapture( const CtrlGeneric &rCtrl )
378 {
379     // Set the capturing control
380     m_pCapturingControl = (CtrlGeneric*) &rCtrl;
381 }
382
383
384 void TopWindow::onControlRelease( const CtrlGeneric &rCtrl )
385 {
386     // Release the capturing control
387     if( m_pCapturingControl == &rCtrl )
388     {
389         m_pCapturingControl = NULL;
390     }
391     else
392     {
393         msg_Dbg( getIntf(), "control had not captured the mouse" );
394     }
395
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 )
399     {
400         EvtEnter evt( getIntf() );
401         m_pLastHitControl->handleEvent( evt );
402
403         // Show the tooltip
404         m_rWindowManager.hideTooltip();
405         UString tipText = m_pLastHitControl->getTooltipText();
406         if( tipText.length() > 0 )
407         {
408             // Set the tooltip text variable
409             VarManager *pVarManager = VarManager::instance( getIntf() );
410             pVarManager->getTooltipText().set( tipText );
411             m_rWindowManager.showTooltip();
412         }
413     }
414 }
415
416
417 void TopWindow::onTooltipChange( const CtrlGeneric &rCtrl )
418 {
419     // Check that the control is the active one
420     if( m_pLastHitControl && m_pLastHitControl == &rCtrl )
421     {
422         if( rCtrl.getTooltipText().size() )
423         {
424             // Set the tooltip text variable
425             VarManager *pVarManager = VarManager::instance( getIntf() );
426             pVarManager->getTooltipText().set( rCtrl.getTooltipText() );
427             m_rWindowManager.showTooltip();
428         }
429         else
430         {
431             // Nothing to display, so hide the tooltip
432             m_rWindowManager.hideTooltip();
433         }
434     }
435 }
436
437
438 CtrlGeneric *TopWindow::findHitControl( int xPos, int yPos )
439 {
440     if( m_pActiveLayout == NULL )
441     {
442         return NULL;
443     }
444
445     // Get the controls in the active layout
446     const list<LayeredControl> &ctrlList = m_pActiveLayout->getControlList();
447     list<LayeredControl>::const_reverse_iterator iter;
448
449     // New control hit by the mouse
450     CtrlGeneric *pNewHitControl = NULL;
451
452     // Loop on the control list to find the uppest hit control
453     for( iter = ctrlList.rbegin(); iter != ctrlList.rend(); iter++ )
454     {
455         // Get the position of the control in the layout
456         const Position *pos = (*iter).m_pControl->getPosition();
457         if( pos != NULL )
458         {
459             // Compute the coordinates of the mouse relative to the control
460             int xRel = xPos - pos->getLeft();
461             int yRel = yPos - pos->getTop();
462
463             CtrlGeneric *pCtrl = (*iter).m_pControl;
464             // Control hit ?
465             if( pCtrl->isVisible() && pCtrl->mouseOver( xRel, yRel ) )
466             {
467                 pNewHitControl = (*iter).m_pControl;
468                 break;
469             }
470         }
471         else
472         {
473             msg_Dbg( getIntf(), "control at NULL position" );
474         }
475     }
476
477     // If the hit control has just been entered, send it an enter event
478     if( pNewHitControl && (pNewHitControl != m_pLastHitControl) )
479     {
480         // Don't send the event if another control captured the mouse
481         if( !m_pCapturingControl || (m_pCapturingControl == pNewHitControl ) )
482         {
483             EvtEnter evt( getIntf() );
484             pNewHitControl->handleEvent( evt );
485
486             if( !m_pCapturingControl )
487             {
488                 // Show the tooltip
489                 m_rWindowManager.hideTooltip();
490                 UString tipText = pNewHitControl->getTooltipText();
491                 if( tipText.length() > 0 )
492                 {
493                     // Set the tooltip text variable
494                     VarManager *pVarManager = VarManager::instance( getIntf() );
495                     pVarManager->getTooltipText().set( tipText );
496                     m_rWindowManager.showTooltip();
497                 }
498             }
499         }
500     }
501
502     return pNewHitControl;
503 }
504
505
506
507 void TopWindow::setLastHit( CtrlGeneric *pNewHitControl )
508 {
509     // Send a leave event to the left control
510     if( m_pLastHitControl && (pNewHitControl != m_pLastHitControl) )
511     {
512         // Don't send the event if another control captured the mouse
513         if( !m_pCapturingControl || (m_pCapturingControl == m_pLastHitControl))
514         {
515             EvtLeave evt( getIntf() );
516             m_pLastHitControl->handleEvent( evt );
517         }
518     }
519
520     m_pLastHitControl = pNewHitControl;
521 }
522