Merge branch 'bare-repo'
[TortoiseGit.git] / src / Utils / MiscUI / ScrollTool.cpp
blob8760bcf17cee5a3c48958976599b7c44ed4ccfe9
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2006,2009-2010 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "stdafx.h"
20 #include "ScrollTool.h"
23 CScrollTool::CScrollTool()
24 : m_bInitCalled(false)
25 , m_bRightAligned(false)
29 CScrollTool::~CScrollTool()
34 BEGIN_MESSAGE_MAP(CScrollTool, CWnd)
35 END_MESSAGE_MAP()
38 bool CScrollTool::Init(LPPOINT pos, bool bRightAligned /* = false */)
40 if (!m_bInitCalled)
42 // create the tooltip window
43 if (!CreateEx(WS_EX_TOPMOST,
44 TOOLTIPS_CLASS,
45 NULL,
46 TTS_NOPREFIX | TTS_ALWAYSTIP,
47 CW_USEDEFAULT,
48 CW_USEDEFAULT,
49 CW_USEDEFAULT,
50 CW_USEDEFAULT,
51 NULL,
52 NULL,
53 NULL))
55 return false;
58 ti.cbSize = sizeof(TOOLINFO);
59 ti.uFlags = TTF_TRACK;
60 ti.hwnd = NULL;
61 ti.hinst = NULL;
62 ti.uId = 0;
63 ti.lpszText = _T(" ");
65 // ToolTip control will cover the whole window
66 ti.rect.left = 0;
67 ti.rect.top = 0;
68 ti.rect.right = 0;
69 ti.rect.bottom = 0;
71 CPoint point;
72 ::GetCursorPos(&point);
74 SendMessage(TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &ti);
76 SendMessage(TTM_TRACKPOSITION, 0, (LPARAM)(DWORD) MAKELONG(point.x, point.y));
77 SendMessage(TTM_TRACKACTIVATE, true, (LPARAM)(LPTOOLINFO) &ti);
78 SendMessage(TTM_TRACKPOSITION, 0, MAKELONG(pos->x, pos->y));
79 m_bRightAligned = bRightAligned;
80 m_bInitCalled = true;
82 return true;
85 void CScrollTool::SetText(LPPOINT pos, const TCHAR * fmt, ...)
87 CString s;
88 va_list marker;
90 va_start( marker, fmt );
91 s.FormatV(fmt, marker);
92 va_end( marker );
94 CSize textsize(0);
95 if (m_bRightAligned)
97 CDC *pDC = GetDC();
98 textsize = pDC->GetTextExtent(s);
99 ReleaseDC(pDC);
102 ti.lpszText = s.GetBuffer();
103 SendMessage(TTM_UPDATETIPTEXT, 0, (LPARAM)(LPTOOLINFO) &ti);
104 SendMessage(TTM_TRACKPOSITION, 0, MAKELONG(pos->x-textsize.cx, pos->y));
105 s.ReleaseBuffer();
108 void CScrollTool::Clear()
110 if (m_bInitCalled)
112 SendMessage(TTM_DELTOOL, 0, (LPARAM)(LPTOOLINFO) &ti);
113 DestroyWindow();
115 m_bInitCalled = false;
118 LONG CScrollTool::GetTextWidth(LPCTSTR szText)
120 CDC *pDC = GetDC();
121 CSize textsize = pDC->GetTextExtent(szText, _tcslen(szText));
122 ReleaseDC(pDC);
123 return textsize.cx;