]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/top_window.cpp
Include vlc_plugin.h as needed
[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     // Show the window
371     GenericWindow::innerShow();
372 }
373
374
375 void TopWindow::innerHide()
376 {
377     if( m_pActiveLayout )
378     {
379         // Notify the active layout
380         m_pActiveLayout->onHide();
381     }
382     // Hide the window
383     GenericWindow::innerHide();
384 }
385
386
387 void TopWindow::updateShape()
388 {
389     // Set the shape of the window
390     if( m_pActiveLayout )
391     {
392         OSGraphics *pImage = m_pActiveLayout->getImage();
393         if( pImage )
394         {
395             pImage->applyMaskToWindow( *getOSWindow() );
396         }
397     }
398 }
399
400
401 void TopWindow::onControlCapture( const CtrlGeneric &rCtrl )
402 {
403     // Set the capturing control
404     m_pCapturingControl = (CtrlGeneric*) &rCtrl;
405 }
406
407
408 void TopWindow::onControlRelease( const CtrlGeneric &rCtrl )
409 {
410     // Release the capturing control
411     if( m_pCapturingControl == &rCtrl )
412     {
413         m_pCapturingControl = NULL;
414     }
415     else
416     {
417         msg_Dbg( getIntf(), "control had not captured the mouse" );
418     }
419
420     // Send an enter event to the control under the mouse, if it doesn't
421     // have received it yet
422     if( m_pLastHitControl && m_pLastHitControl != &rCtrl )
423     {
424         EvtEnter evt( getIntf() );
425         m_pLastHitControl->handleEvent( evt );
426
427         // Show the tooltip
428         m_rWindowManager.hideTooltip();
429         UString tipText = m_pLastHitControl->getTooltipText();
430         if( tipText.length() > 0 )
431         {
432             // Set the tooltip text variable
433             VarManager *pVarManager = VarManager::instance( getIntf() );
434             pVarManager->getTooltipText().set( tipText );
435             m_rWindowManager.showTooltip();
436         }
437     }
438 }
439
440
441 void TopWindow::onTooltipChange( const CtrlGeneric &rCtrl )
442 {
443     // Check that the control is the active one
444     if( m_pLastHitControl && m_pLastHitControl == &rCtrl )
445     {
446         if( rCtrl.getTooltipText().size() )
447         {
448             // Set the tooltip text variable
449             VarManager *pVarManager = VarManager::instance( getIntf() );
450             pVarManager->getTooltipText().set( rCtrl.getTooltipText() );
451             m_rWindowManager.showTooltip();
452         }
453         else
454         {
455             // Nothing to display, so hide the tooltip
456             m_rWindowManager.hideTooltip();
457         }
458     }
459 }
460
461
462 CtrlGeneric *TopWindow::findHitControl( int xPos, int yPos )
463 {
464     if( m_pActiveLayout == NULL )
465     {
466         return NULL;
467     }
468
469     // Get the controls in the active layout
470     const list<LayeredControl> &ctrlList = m_pActiveLayout->getControlList();
471     list<LayeredControl>::const_reverse_iterator iter;
472
473     // New control hit by the mouse
474     CtrlGeneric *pNewHitControl = NULL;
475
476     // Loop on the control list to find the uppest hit control
477     for( iter = ctrlList.rbegin(); iter != ctrlList.rend(); iter++ )
478     {
479         // Get the position of the control in the layout
480         const Position *pos = (*iter).m_pControl->getPosition();
481         if( pos != NULL )
482         {
483             // Compute the coordinates of the mouse relative to the control
484             int xRel = xPos - pos->getLeft();
485             int yRel = yPos - pos->getTop();
486
487             CtrlGeneric *pCtrl = (*iter).m_pControl;
488             // Control hit ?
489             if( pCtrl->isVisible() && pCtrl->mouseOver( xRel, yRel ) )
490             {
491                 pNewHitControl = (*iter).m_pControl;
492                 break;
493             }
494         }
495         else
496         {
497             msg_Dbg( getIntf(), "control at NULL position" );
498         }
499     }
500
501     // If the hit control has just been entered, send it an enter event
502     if( pNewHitControl && (pNewHitControl != m_pLastHitControl) )
503     {
504         // Don't send the event if another control captured the mouse
505         if( !m_pCapturingControl || (m_pCapturingControl == pNewHitControl ) )
506         {
507             EvtEnter evt( getIntf() );
508             pNewHitControl->handleEvent( evt );
509
510             if( !m_pCapturingControl )
511             {
512                 // Show the tooltip
513                 m_rWindowManager.hideTooltip();
514                 UString tipText = pNewHitControl->getTooltipText();
515                 if( tipText.length() > 0 )
516                 {
517                     // Set the tooltip text variable
518                     VarManager *pVarManager = VarManager::instance( getIntf() );
519                     pVarManager->getTooltipText().set( tipText );
520                     m_rWindowManager.showTooltip();
521                 }
522             }
523         }
524     }
525
526     return pNewHitControl;
527 }
528
529
530
531 void TopWindow::setLastHit( CtrlGeneric *pNewHitControl )
532 {
533     // Send a leave event to the left control
534     if( m_pLastHitControl && (pNewHitControl != m_pLastHitControl) )
535     {
536         // Don't send the event if another control captured the mouse
537         if( !m_pCapturingControl || (m_pCapturingControl == m_pLastHitControl))
538         {
539             EvtLeave evt( getIntf() );
540             m_pLastHitControl->handleEvent( evt );
541         }
542     }
543
544     m_pLastHitControl = pNewHitControl;
545 }
546