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