]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/top_window.cpp
Skins strings (Refs:#438)
[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_vlc, "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_vlc, "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     pLayout->setWindow( this );
326     m_pActiveLayout = pLayout;
327     // Get the size of the layout and resize the window
328     resize( pLayout->getWidth(), pLayout->getHeight() );
329     updateShape();
330     pLayout->refreshAll();
331 }
332
333
334 const GenericLayout& TopWindow::getActiveLayout() const
335 {
336     return *m_pActiveLayout;
337 }
338
339
340 void TopWindow::innerShow()
341 {
342     // First, refresh the layout and update the shape of the window
343     if( m_pActiveLayout )
344     {
345         updateShape();
346         m_pActiveLayout->refreshAll();
347     }
348     // Show the window
349     GenericWindow::innerShow();
350 }
351
352  
353 void TopWindow::updateShape()
354 {
355     // Set the shape of the window
356     if( m_pActiveLayout )
357     {
358         OSGraphics *pImage = m_pActiveLayout->getImage();
359         if( pImage )
360         {
361             pImage->applyMaskToWindow( *getOSWindow() );
362         }
363     }
364 }
365
366
367 void TopWindow::onControlCapture( const CtrlGeneric &rCtrl )
368 {
369     // Set the capturing control
370     m_pCapturingControl = (CtrlGeneric*) &rCtrl;
371 }
372
373
374 void TopWindow::onControlRelease( const CtrlGeneric &rCtrl )
375 {
376     // Release the capturing control
377     if( m_pCapturingControl == &rCtrl )
378     {
379         m_pCapturingControl = NULL;
380     }
381     else
382     {
383         msg_Dbg( getIntf(), "control had not captured the mouse" );
384     }
385
386     // Send an enter event to the control under the mouse, if it doesn't
387     // have received it yet
388     if( m_pLastHitControl && m_pLastHitControl != &rCtrl )
389     {
390         EvtEnter evt( getIntf() );
391         m_pLastHitControl->handleEvent( evt );
392
393         // Show the tooltip
394         m_rWindowManager.hideTooltip();
395         UString tipText = m_pLastHitControl->getTooltipText();
396         if( tipText.length() > 0 )
397         {
398             // Set the tooltip text variable
399             VarManager *pVarManager = VarManager::instance( getIntf() );
400             pVarManager->getTooltipText().set( tipText );
401             m_rWindowManager.showTooltip();
402         }
403     }
404 }
405
406
407 void TopWindow::onTooltipChange( const CtrlGeneric &rCtrl )
408 {
409     // Check that the control is the active one
410     if( m_pLastHitControl && m_pLastHitControl == &rCtrl )
411     {
412         // Set the tooltip text variable
413         VarManager *pVarManager = VarManager::instance( getIntf() );
414         pVarManager->getTooltipText().set( rCtrl.getTooltipText() );
415     }
416 }
417
418
419 CtrlGeneric *TopWindow::findHitControl( int xPos, int yPos )
420 {
421     if( m_pActiveLayout == NULL )
422     {
423         return NULL;
424     }
425
426     // Get the controls in the active layout
427     const list<LayeredControl> &ctrlList = m_pActiveLayout->getControlList();
428     list<LayeredControl>::const_reverse_iterator iter;
429
430     // New control hit by the mouse
431     CtrlGeneric *pNewHitControl = NULL;
432
433     // Loop on the control list to find the uppest hit control
434     for( iter = ctrlList.rbegin(); iter != ctrlList.rend(); iter++ )
435     {
436         // Get the position of the control in the layout
437         const Position *pos = (*iter).m_pControl->getPosition();
438         if( pos != NULL )
439         {
440             // Compute the coordinates of the mouse relative to the control
441             int xRel = xPos - pos->getLeft();
442             int yRel = yPos - pos->getTop();
443
444             CtrlGeneric *pCtrl = (*iter).m_pControl;
445             // Control hit ?
446             if( pCtrl->isVisible() && pCtrl->mouseOver( xRel, yRel ) )
447             {
448                 pNewHitControl = (*iter).m_pControl;
449                 break;
450             }
451         }
452         else
453         {
454             msg_Dbg( getIntf(), "control at NULL position" );
455         }
456     }
457
458     // If the hit control has just been entered, send it an enter event
459     if( pNewHitControl && (pNewHitControl != m_pLastHitControl) )
460     {
461         // Don't send the event if another control captured the mouse
462         if( !m_pCapturingControl || (m_pCapturingControl == pNewHitControl ) )
463         {
464             EvtEnter evt( getIntf() );
465             pNewHitControl->handleEvent( evt );
466
467             if( !m_pCapturingControl )
468             {
469                 // Show the tooltip
470                 m_rWindowManager.hideTooltip();
471                 UString tipText = pNewHitControl->getTooltipText();
472                 if( tipText.length() > 0 )
473                 {
474                     // Set the tooltip text variable
475                     VarManager *pVarManager = VarManager::instance( getIntf() );
476                     pVarManager->getTooltipText().set( tipText );
477                     m_rWindowManager.showTooltip();
478                 }
479             }
480         }
481     }
482
483     return pNewHitControl;
484 }
485
486
487
488 void TopWindow::setLastHit( CtrlGeneric *pNewHitControl )
489 {
490     // Send a leave event to the left control
491     if( m_pLastHitControl && (pNewHitControl != m_pLastHitControl) )
492     {
493         // Don't send the event if another control captured the mouse
494         if( !m_pCapturingControl || (m_pCapturingControl == m_pLastHitControl))
495         {
496             EvtLeave evt( getIntf() );
497             m_pLastHitControl->handleEvent( evt );
498         }
499     }
500
501     m_pLastHitControl = pNewHitControl;
502 }
503