]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/tooltip.cpp
b23571dd8831b09bd9e0cad17c76d1f512f691a8
[vlc] / modules / gui / skins2 / src / tooltip.cpp
1 /*****************************************************************************
2  * tooltip.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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 )
39 {
40     OSFactory *pOsFactory = OSFactory::instance( pIntf );
41     m_pTimer = pOsFactory->createOSTimer( Callback( this, &doShow ) );
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     SKINS_DELETE( m_pTimer );
53     SKINS_DELETE( m_pOsTooltip );
54     if( m_pImage )
55     {
56         delete m_pImage;
57     }
58 }
59
60
61 void Tooltip::show()
62 {
63     // (Re)start the timer
64     m_pTimer->start( m_delay, true );
65 }
66
67
68 void Tooltip::hide()
69 {
70     m_pTimer->stop();
71     m_pOsTooltip->hide();
72     m_xPos = -1;
73     m_yPos = -1;
74 }
75
76
77 void Tooltip::onUpdate( Subject<VarText> &rVariable )
78 {
79     // Redisplay the tooltip
80     displayText( ((VarText&)rVariable).get() );
81 }
82
83
84 void Tooltip::displayText( const UString &rText )
85 {
86     // Rebuild the image
87     makeImage( rText );
88
89     // Redraw the window if it is already visible
90     if( m_xPos != -1 )
91     {
92         m_pOsTooltip->show( m_xPos, m_yPos, *m_pImage );
93     }
94 }
95
96
97 void Tooltip::makeImage( const UString &rText )
98 {
99     // Render the text on a bitmap
100     GenericBitmap *pBmpTip = m_rFont.drawString( rText, 0 );
101     if( !pBmpTip )
102     {
103         return;
104     }
105     int w = pBmpTip->getWidth() + 10;
106     int h = m_rFont.getSize() + 8;
107
108     // Create the image of the tooltip
109     if( m_pImage )
110     {
111         delete m_pImage;
112     }
113     m_pImage = OSFactory::instance( getIntf() )->createOSGraphics( w, h );
114     m_pImage->fillRect( 0, 0, w, h, 0xffffd0 );
115     m_pImage->drawRect( 0, 0, w, h, 0x000000 );
116     m_pImage->drawBitmap( *pBmpTip, 0, 0, 5, 5 );
117
118     delete pBmpTip;
119 }
120
121
122 void Tooltip::doShow( SkinObject *pObj )
123 {
124     Tooltip *pThis = (Tooltip*)pObj;
125
126     if( pThis->m_pImage )
127     {
128         if( pThis->m_xPos == -1 )
129         {
130             // Get the mouse coordinates and the image size
131             OSFactory *pOsFactory = OSFactory::instance( pThis->getIntf() );
132             int x, y;
133             pOsFactory->getMousePos( x, y );
134             int scrWidth = pOsFactory->getScreenWidth();
135             int scrHeight = pOsFactory->getScreenHeight();
136             int w = pThis->m_pImage->getWidth();
137             int h = pThis->m_pImage->getHeight();
138
139             // Compute the position of the tooltip
140             x -= (w / 2 + 4);
141             y += (h + 5);
142             if( x + w > scrWidth )
143                 x -= (x + w - scrWidth);
144             else if( x < 0 )
145                 x = 0;
146             if( y + h > scrHeight )
147                 y -= (2 * h + 20);
148
149             pThis->m_xPos = x;
150             pThis->m_yPos = y;
151         }
152
153         // Show the tooltip window
154         pThis->m_pOsTooltip->show( pThis->m_xPos, pThis->m_yPos,
155                                    *(pThis->m_pImage) );
156     }
157 }
158