]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/tooltip.cpp
Merge branch 'master' into lpcm_encoder
[vlc] / modules / gui / skins2 / src / tooltip.cpp
1 /*****************************************************************************
2  * tooltip.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 "tooltip.hpp"
26 #include "generic_bitmap.hpp"
27 #include "generic_font.hpp"
28 #include "os_factory.hpp"
29 #include "os_graphics.hpp"
30 #include "os_tooltip.hpp"
31 #include "os_timer.hpp"
32 #include "var_manager.hpp"
33 #include "../utils/ustring.hpp"
34
35
36 Tooltip::Tooltip( intf_thread_t *pIntf, const GenericFont &rFont, int delay ):
37     SkinObject( pIntf ), m_rFont( rFont ), m_delay( delay ), m_pImage( NULL ),
38     m_xPos( -1 ), m_yPos( -1 ), m_cmdShow( this )
39 {
40     OSFactory *pOsFactory = OSFactory::instance( pIntf );
41     m_pTimer = pOsFactory->createOSTimer( m_cmdShow );
42     m_pOsTooltip = pOsFactory->createOSTooltip();
43
44     // Observe the tooltip text variable
45     VarManager::instance( pIntf )->getTooltipText().addObserver( this );
46 }
47
48
49 Tooltip::~Tooltip()
50 {
51     VarManager::instance( getIntf() )->getTooltipText().delObserver( this );
52     delete m_pTimer;
53     delete m_pOsTooltip;
54     delete m_pImage;
55 }
56
57
58 void Tooltip::show()
59 {
60     // (Re)start the timer
61     m_pTimer->start( m_delay, true );
62 }
63
64
65 void Tooltip::hide()
66 {
67     m_pTimer->stop();
68     m_pOsTooltip->hide();
69     m_xPos = -1;
70     m_yPos = -1;
71 }
72
73
74 void Tooltip::onUpdate( Subject<VarText> &rVariable , void *arg)
75 {
76     // Redisplay the tooltip
77     displayText( ((VarText&)rVariable).get() );
78 }
79
80
81 void Tooltip::displayText( const UString &rText )
82 {
83     // Rebuild the image
84     makeImage( rText );
85
86     // Redraw the window if it is already visible
87     if( m_xPos != -1 )
88     {
89         m_pOsTooltip->show( m_xPos, m_yPos, *m_pImage );
90     }
91 }
92
93
94 void Tooltip::makeImage( const UString &rText )
95 {
96     // Render the text on a bitmap
97     GenericBitmap *pBmpTip = m_rFont.drawString( rText, 0 );
98     if( !pBmpTip )
99     {
100         return;
101     }
102     int w = pBmpTip->getWidth() + 10;
103     int h = m_rFont.getSize() + 8;
104
105     // Create the image of the tooltip
106     delete m_pImage;
107     m_pImage = OSFactory::instance( getIntf() )->createOSGraphics( w, h );
108     m_pImage->fillRect( 0, 0, w, h, 0xffffd0 );
109     m_pImage->drawRect( 0, 0, w, h, 0x000000 );
110     m_pImage->drawBitmap( *pBmpTip, 0, 0, 5, 5, -1, -1, true );
111
112     delete pBmpTip;
113 }
114
115
116 void Tooltip::CmdShow::execute()
117 {
118     if( m_pParent->m_pImage )
119     {
120         if( m_pParent->m_xPos == -1 )
121         {
122             // Get the mouse coordinates and the image size
123             OSFactory *pOsFactory = OSFactory::instance( m_pParent->getIntf() );
124             int x, y;
125             pOsFactory->getMousePos( x, y );
126             int scrWidth = pOsFactory->getScreenWidth();
127             int scrHeight = pOsFactory->getScreenHeight();
128             int w = m_pParent->m_pImage->getWidth();
129             int h = m_pParent->m_pImage->getHeight();
130
131             // Compute the position of the tooltip
132             x -= (w / 2 + 4);
133             y += (h + 5);
134             if( x + w > scrWidth )
135                 x -= (x + w - scrWidth);
136             else if( x < 0 )
137                 x = 0;
138             if( y + h > scrHeight )
139                 y -= (2 * h + 20);
140
141             m_pParent->m_xPos = x;
142             m_pParent->m_yPos = y;
143         }
144
145         // Show the tooltip window
146         m_pParent->m_pOsTooltip->show( m_pParent->m_xPos, m_pParent->m_yPos,
147                                    *(m_pParent->m_pImage) );
148     }
149 }
150