00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "wgui_include_config.h"
00025 #include "wg_view.h"
00026 #include "wg_tooltip.h"
00027
00028 namespace wGui
00029 {
00030
00031 CToolTip::CToolTip(CWindow* pToolWindow, std::wstring sText, CRGBColor& FontColor, CRGBColor& BackgroundColor, CFontEngine* pFontEngine) :
00032 CWindow(CRect(), pToolWindow),
00033 m_FontColor(FontColor)
00034 {
00035 m_sWindowText = sText;
00036 if (pFontEngine)
00037 {
00038 m_pFontEngine = pFontEngine;
00039 }
00040 else
00041 {
00042 m_pFontEngine = CApplication::Instance()->GetDefaultFontEngine();
00043 }
00044 std::auto_ptr<CRenderedString> pRenderedString(new CRenderedString(
00045 m_pFontEngine, sText, CRenderedString::VALIGN_TOP, CRenderedString::HALIGN_LEFT));
00046 m_pRenderedString = pRenderedString;
00047
00048 m_pTimer = new CTimer(this);
00049
00050
00051 CPoint Dims;
00052 m_pRenderedString->GetMetrics(&Dims, 0, 0);
00053 m_BoundingRect = CRect(CPoint(0, 0), Dims + CPoint(4, 4));
00054
00055 m_BackgroundColor = BackgroundColor;
00056 CMessageServer::Instance().RegisterMessageClient(this, CMessage::MOUSE_MOVE);
00057 CMessageServer::Instance().RegisterMessageClient(this, CMessage::CTRL_TIMER);
00058 }
00059
00060
00061 CToolTip::~CToolTip(void)
00062 {
00063 delete m_pTimer;
00064 }
00065
00066
00067 void CToolTip::ShowTip(const CPoint& DrawPoint)
00068 {
00069 SetWindowRect(m_pParentWindow->ViewToClient(m_BoundingRect + DrawPoint));
00070 SetVisible(true);
00071 Draw();
00072 }
00073
00074
00075 void CToolTip::HideTip(void)
00076 {
00077 SetVisible(false);
00078 CMessageServer::Instance().QueueMessage(new CMessage(CMessage::APP_PAINT, 0, this));
00079 }
00080
00081
00082 void CToolTip::Draw(void) const
00083 {
00084 CWindow::Draw();
00085
00086 if (m_pSDLSurface)
00087 {
00088 CPainter Painter(m_pSDLSurface, CPainter::PAINT_REPLACE);
00089 Painter.DrawRect(m_WindowRect.SizeRect(), false);
00090 CRect SubRect(m_WindowRect.SizeRect());
00091 SubRect.Grow(-2);
00092 if (m_pRenderedString.get())
00093 {
00094 m_pRenderedString->Draw(m_pSDLSurface, SubRect, SubRect.TopLeft(), m_FontColor);
00095 }
00096 }
00097 }
00098
00099
00100 void CToolTip::MoveWindow(const CPoint& MoveDistance)
00101 {
00102 CWindow::MoveWindow(MoveDistance);
00103 m_BoundingRect = m_BoundingRect + MoveDistance;
00104 }
00105
00106
00107 void CToolTip::PaintToSurface(SDL_Surface& , SDL_Surface& FloatingSurface, const CPoint& Offset) const
00108 {
00109 if (m_bVisible)
00110 {
00111 SDL_Rect SourceRect = CRect(m_WindowRect.SizeRect()).SDLRect();
00112 SDL_Rect DestRect = CRect(m_WindowRect + Offset).SDLRect();
00113 SDL_BlitSurface(m_pSDLSurface, &SourceRect, &FloatingSurface, &DestRect);
00114 CPoint NewOffset = m_ClientRect.TopLeft() + m_WindowRect.TopLeft() + Offset;
00115 for (std::list<CWindow*>::const_iterator iter = m_ChildWindows.begin(); iter != m_ChildWindows.end(); ++iter)
00116 {
00117 (*iter)->PaintToSurface(FloatingSurface, FloatingSurface, NewOffset);
00118 }
00119 }
00120 }
00121
00122
00123 bool CToolTip::HandleMessage(CMessage* pMessage)
00124 {
00125 bool bHandled = false;
00126
00127 if (pMessage)
00128 {
00129 switch(pMessage->MessageType())
00130 {
00131 case CMessage::CTRL_TIMER:
00132 {
00133 wGui::TIntMessage* pTimerMessage = dynamic_cast<wGui::TIntMessage*>(pMessage);
00134 if (pTimerMessage && pMessage->Destination() == this)
00135 {
00136
00137 ShowTip(m_LastMousePosition + CPoint(-6, 18));
00138 bHandled = true;
00139 }
00140 break;
00141 }
00142 case CMessage::MOUSE_MOVE:
00143 {
00144
00145 CMouseMessage* pMouseMessage = dynamic_cast<CMouseMessage*>(pMessage);
00146 if (pMouseMessage)
00147 {
00148 m_LastMousePosition = pMouseMessage->Point;
00149 m_pTimer->StopTimer();
00150 if (IsVisible())
00151 {
00152 HideTip();
00153 }
00154 CView* pView = GetView();
00155 bool bHitFloating = pView && pView->GetFloatingWindow() && pView->GetFloatingWindow()->HitTest(pMouseMessage->Point) &&
00156 pView->GetFloatingWindow() != m_pParentWindow;
00157 if (m_pParentWindow->GetWindowRect().SizeRect().HitTest(
00158 m_pParentWindow->ViewToWindow(m_LastMousePosition)) == CRect::RELPOS_INSIDE && !bHitFloating)
00159 {
00160 m_pTimer->StartTimer(1000);
00161 }
00162 }
00163 }
00164 default :
00165 bHandled = CWindow::HandleMessage(pMessage);
00166 break;
00167 }
00168 }
00169
00170 return bHandled;
00171 }
00172
00173 }
00174