]> git.saurik.com Git - wxWidgets.git/blob - src/generic/tipwin.cpp
fc18fbff7bf706f9f71985348f697fdcc04ca8b0
[wxWidgets.git] / src / generic / tipwin.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/tipwin.cpp
3 // Purpose: implementation of wxTipWindow
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 10.09.00
7 // RCS-ID: $Id$
8 // Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "tipwin.h"
22 #endif
23
24 // For compilers that support precompilatixon, includes "wx/wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/dcclient.h"
33 #endif // WX_PRECOMP
34
35 #include "wx/tipwin.h"
36 #include "wx/timer.h"
37 #include "wx/settings.h"
38
39 // ----------------------------------------------------------------------------
40 // constants
41 // ----------------------------------------------------------------------------
42
43 static const wxCoord TEXT_MARGIN_X = 3;
44 static const wxCoord TEXT_MARGIN_Y = 3;
45
46 // ============================================================================
47 // implementation
48 // ============================================================================
49
50 // ----------------------------------------------------------------------------
51 // event tables
52 // ----------------------------------------------------------------------------
53
54 BEGIN_EVENT_TABLE(wxTipWindow, wxPopupTransientWindow)
55 EVT_LEFT_DOWN(wxTipWindow::OnMouseClick)
56 EVT_RIGHT_DOWN(wxTipWindow::OnMouseClick)
57 EVT_MIDDLE_DOWN(wxTipWindow::OnMouseClick)
58 EVT_PAINT(wxTipWindow::OnPaint)
59 END_EVENT_TABLE()
60
61
62 // ----------------------------------------------------------------------------
63 // wxTipWindow
64 // ----------------------------------------------------------------------------
65
66 wxTipWindow::wxTipWindow(wxWindow *parent,
67 const wxString& text,
68 wxCoord maxLength, wxTipWindow** windowPtr)
69 : wxPopupTransientWindow(parent)
70 {
71 m_windowPtr = windowPtr;
72
73 // set colours
74 SetForegroundColour(*wxBLACK);
75
76 #ifdef __WXMSW__
77 wxColour bkCol(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_INFOBK));
78 #else
79 wxColour bkCol(wxColour(255, 255, 225));
80 #endif
81 SetBackgroundColour(bkCol);
82
83 // set size and position
84 Adjust(text, maxLength);
85 int x, y;
86 wxGetMousePosition(&x, &y);
87 Position(wxPoint(x, y+10), wxSize(0,0));
88
89 //Show(TRUE);
90 Popup();
91 SetFocus();
92 }
93
94 wxTipWindow::~wxTipWindow()
95 {
96 if (m_windowPtr)
97 {
98 *m_windowPtr = NULL;
99 }
100 }
101
102 void wxTipWindow::OnMouseClick(wxMouseEvent& WXUNUSED(event))
103 {
104 Close();
105 }
106
107
108 void wxTipWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
109 {
110 wxPaintDC dc(this);
111
112 wxRect rect;
113 wxSize size = GetClientSize();
114 rect.width = size.x;
115 rect.height = size.y;
116
117 // first filll the background
118 dc.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID));
119
120 dc.SetPen( * wxBLACK_PEN );
121 dc.DrawRectangle(rect);
122
123 // and then draw the text line by line
124 dc.SetFont(GetFont());
125
126 wxPoint pt;
127 pt.x = TEXT_MARGIN_X;
128 pt.y = TEXT_MARGIN_Y;
129 size_t count = m_textLines.GetCount();
130 for ( size_t n = 0; n < count; n++ )
131 {
132 dc.DrawText(m_textLines[n], pt);
133
134 pt.y += m_heightLine;
135 }
136 }
137
138
139 void wxTipWindow::Adjust(const wxString& text, wxCoord maxLength)
140 {
141 wxClientDC dc(this);
142 dc.SetFont(GetFont());
143
144 // calculate the length: we want each line be no longer than maxLength
145 // pixels and we only break lines at words boundary
146 wxString current;
147 wxCoord height, width,
148 widthMax = 0;
149 m_heightLine = 0;
150
151 bool breakLine = FALSE;
152 for ( const wxChar *p = text.c_str(); ; p++ )
153 {
154 if ( *p == _T('\n') || *p == _T('\0') )
155 {
156 dc.GetTextExtent(current, &width, &height);
157 if ( width > widthMax )
158 widthMax = width;
159
160 if ( height > m_heightLine )
161 m_heightLine = height;
162
163 m_textLines.Add(current);
164
165 if ( !*p )
166 {
167 // end of text
168 break;
169 }
170
171 current.clear();
172 breakLine = FALSE;
173 }
174 else if ( breakLine && (*p == _T(' ') || *p == _T('\t')) )
175 {
176 // word boundary - break the line here
177 m_textLines.Add(current);
178 current.clear();
179 breakLine = FALSE;
180 }
181 else // line goes on
182 {
183 current += *p;
184 dc.GetTextExtent(current, &width, &height);
185 if ( width > maxLength )
186 breakLine = TRUE;
187
188 if ( width > widthMax )
189 widthMax = width;
190
191 if ( height > m_heightLine )
192 m_heightLine = height;
193 }
194 }
195
196 // take into account the border size and the margins
197 SetClientSize(2*(TEXT_MARGIN_X + 1) + widthMax,
198 2*(TEXT_MARGIN_Y + 1) + m_textLines.GetCount() * m_heightLine);
199 }
200
201
202 void wxTipWindow::Close()
203 {
204 Show(FALSE);
205 Destroy();
206 }
207
208