Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

tooltip.cpp

00001 /*****************************************************************************
00002  * tooltip.cpp
00003  *****************************************************************************
00004  * Copyright (C) 2003 the VideoLAN team
00005  * $Id: tooltip.cpp 12207 2005-08-15 15:54:32Z asmax $
00006  *
00007  * Authors: Cyril Deguet     <[email protected]>
00008  *          Olivier Teulière <[email protected]>
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00023  *****************************************************************************/
00024 
00025 #include "tooltip.hpp"
00026 #include "generic_bitmap.hpp"
00027 #include "generic_font.hpp"
00028 #include "os_factory.hpp"
00029 #include "os_graphics.hpp"
00030 #include "os_tooltip.hpp"
00031 #include "os_timer.hpp"
00032 #include "var_manager.hpp"
00033 #include "../utils/ustring.hpp"
00034 
00035 
00036 Tooltip::Tooltip( intf_thread_t *pIntf, const GenericFont &rFont, int delay ):
00037     SkinObject( pIntf ), m_rFont( rFont ), m_delay( delay ), m_pImage( NULL ),
00038     m_xPos( -1 ), m_yPos( -1 ), m_cmdShow( this )
00039 {
00040     OSFactory *pOsFactory = OSFactory::instance( pIntf );
00041     m_pTimer = pOsFactory->createOSTimer( m_cmdShow );
00042     m_pOsTooltip = pOsFactory->createOSTooltip();
00043 
00044     // Observe the tooltip text variable
00045     VarManager::instance( pIntf )->getTooltipText().addObserver( this );
00046 }
00047 
00048 
00049 Tooltip::~Tooltip()
00050 {
00051     VarManager::instance( getIntf() )->getTooltipText().delObserver( this );
00052     SKINS_DELETE( m_pTimer );
00053     SKINS_DELETE( m_pOsTooltip );
00054     if( m_pImage )
00055     {
00056         delete m_pImage;
00057     }
00058 }
00059 
00060 
00061 void Tooltip::show()
00062 {
00063     // (Re)start the timer
00064     m_pTimer->start( m_delay, true );
00065 }
00066 
00067 
00068 void Tooltip::hide()
00069 {
00070     m_pTimer->stop();
00071     m_pOsTooltip->hide();
00072     m_xPos = -1;
00073     m_yPos = -1;
00074 }
00075 
00076 
00077 void Tooltip::onUpdate( Subject<VarText> &rVariable )
00078 {
00079     // Redisplay the tooltip
00080     displayText( ((VarText&)rVariable).get() );
00081 }
00082 
00083 
00084 void Tooltip::displayText( const UString &rText )
00085 {
00086     // Rebuild the image
00087     makeImage( rText );
00088 
00089     // Redraw the window if it is already visible
00090     if( m_xPos != -1 )
00091     {
00092         m_pOsTooltip->show( m_xPos, m_yPos, *m_pImage );
00093     }
00094 }
00095 
00096 
00097 void Tooltip::makeImage( const UString &rText )
00098 {
00099     // Render the text on a bitmap
00100     GenericBitmap *pBmpTip = m_rFont.drawString( rText, 0 );
00101     if( !pBmpTip )
00102     {
00103         return;
00104     }
00105     int w = pBmpTip->getWidth() + 10;
00106     int h = m_rFont.getSize() + 8;
00107 
00108     // Create the image of the tooltip
00109     if( m_pImage )
00110     {
00111         delete m_pImage;
00112     }
00113     m_pImage = OSFactory::instance( getIntf() )->createOSGraphics( w, h );
00114     m_pImage->fillRect( 0, 0, w, h, 0xffffd0 );
00115     m_pImage->drawRect( 0, 0, w, h, 0x000000 );
00116     m_pImage->drawBitmap( *pBmpTip, 0, 0, 5, 5, -1, -1, true );
00117 
00118     delete pBmpTip;
00119 }
00120 
00121 
00122 void Tooltip::CmdShow::execute()
00123 {
00124     if( m_pParent->m_pImage )
00125     {
00126         if( m_pParent->m_xPos == -1 )
00127         {
00128             // Get the mouse coordinates and the image size
00129             OSFactory *pOsFactory = OSFactory::instance( m_pParent->getIntf() );
00130             int x, y;
00131             pOsFactory->getMousePos( x, y );
00132             int scrWidth = pOsFactory->getScreenWidth();
00133             int scrHeight = pOsFactory->getScreenHeight();
00134             int w = m_pParent->m_pImage->getWidth();
00135             int h = m_pParent->m_pImage->getHeight();
00136 
00137             // Compute the position of the tooltip
00138             x -= (w / 2 + 4);
00139             y += (h + 5);
00140             if( x + w > scrWidth )
00141                 x -= (x + w - scrWidth);
00142             else if( x < 0 )
00143                 x = 0;
00144             if( y + h > scrHeight )
00145                 y -= (2 * h + 20);
00146 
00147             m_pParent->m_xPos = x;
00148             m_pParent->m_yPos = y;
00149         }
00150 
00151         // Show the tooltip window
00152         m_pParent->m_pOsTooltip->show( m_pParent->m_xPos, m_pParent->m_yPos,
00153                                    *(m_pParent->m_pImage) );
00154     }
00155 }
00156 

Generated on Tue Dec 20 10:14:42 2005 for vlc-0.8.4a by  doxygen 1.4.2