]> git.saurik.com Git - wxWidgets.git/blob - src/generic/tipwin.cpp
fixed memory allocation code of wxStreamBuffer to not realloc() new[]ed memory any...
[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
37 // ----------------------------------------------------------------------------
38 // constants
39 // ----------------------------------------------------------------------------
40
41 static const wxCoord TEXT_MARGIN_X = 3;
42 static const wxCoord TEXT_MARGIN_Y = 3;
43
44 // ============================================================================
45 // implementation
46 // ============================================================================
47
48 // ----------------------------------------------------------------------------
49 // event tables
50 // ----------------------------------------------------------------------------
51
52 BEGIN_EVENT_TABLE(wxTipWindow, wxFrame)
53 EVT_PAINT(wxTipWindow::OnPaint)
54
55 EVT_LEFT_DOWN(wxTipWindow::OnMouseClick)
56 EVT_RIGHT_DOWN(wxTipWindow::OnMouseClick)
57 EVT_MIDDLE_DOWN(wxTipWindow::OnMouseClick)
58 EVT_KILL_FOCUS(wxTipWindow::OnKillFocus)
59 EVT_ACTIVATE(wxTipWindow::OnActivate)
60 END_EVENT_TABLE()
61
62 // ----------------------------------------------------------------------------
63 // wxTipWindow
64 // ----------------------------------------------------------------------------
65
66 wxTipWindow::wxTipWindow(wxWindow *parent,
67 const wxString& text,
68 wxCoord maxLength)
69 : wxFrame(parent, -1, _T(""),
70 wxDefaultPosition, wxDefaultSize,
71 wxNO_BORDER | wxFRAME_FLOAT_ON_PARENT)
72 {
73 // set colours
74 SetForegroundColour(*wxBLACK);
75 #if !defined(__WXPM__)
76 SetBackgroundColour(wxColour(0xc3ffff));
77 #else
78 // What is 0xc3ffff, try some legable documentation for those of us who don't memorize hex codes??
79 SetBackgroundColour(wxColour(*wxWHITE));
80 #endif
81 // set position and size
82 int x, y;
83 wxGetMousePosition(&x, &y);
84 Move(x, y + 20);
85
86 Adjust(text, maxLength);
87
88 SetFocus();
89
90 Show(TRUE);
91 }
92
93 void wxTipWindow::Adjust(const wxString& text, wxCoord maxLength)
94 {
95 wxClientDC dc(this);
96 dc.SetFont(GetFont());
97
98 // calculate the length: we want each line be no longer than maxLength
99 // pixels and we only break lines at words boundary
100 wxString current;
101 wxCoord height, width,
102 widthMax = 0;
103 m_heightLine = 0;
104
105 bool breakLine = FALSE;
106 for ( const wxChar *p = text.c_str(); ; p++ )
107 {
108 if ( *p == _T('\n') || *p == _T('\0') )
109 {
110 dc.GetTextExtent(current, &width, &height);
111 if ( width > widthMax )
112 widthMax = width;
113
114 if ( height > m_heightLine )
115 m_heightLine = height;
116
117 m_textLines.Add(current);
118
119 if ( !*p )
120 {
121 // end of text
122 break;
123 }
124
125 current.clear();
126 breakLine = FALSE;
127 }
128 else if ( breakLine && (*p == _T(' ') || *p == _T('\t')) )
129 {
130 // word boundary - break the line here
131 m_textLines.Add(current);
132 current.clear();
133 breakLine = FALSE;
134 }
135 else // line goes on
136 {
137 current += *p;
138 dc.GetTextExtent(current, &width, &height);
139 if ( width > maxLength )
140 breakLine = TRUE;
141
142 if ( width > widthMax )
143 widthMax = width;
144
145 if ( height > m_heightLine )
146 m_heightLine = height;
147 }
148 }
149
150 // take into account the border size and the margins
151 SetClientSize(2*(TEXT_MARGIN_X + 1) + widthMax,
152 2*(TEXT_MARGIN_Y + 1) + m_textLines.GetCount()*m_heightLine);
153 }
154
155 void wxTipWindow::OnPaint(wxPaintEvent& event)
156 {
157 wxPaintDC dc(this);
158
159 wxRect rect;
160 wxSize size = GetClientSize();
161 rect.width = size.x;
162 rect.height = size.y;
163
164 // first filll the background
165 dc.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID));
166
167 // Under Windows, you apparently get a thin black border whether you like it or not :-(
168 #ifdef __WXMSW__
169 dc.SetPen( * wxTRANSPARENT_PEN );
170 #else
171 dc.SetPen( * wxBLACK_PEN );
172 #endif
173 dc.DrawRectangle(rect);
174
175 // and then draw the text line by line
176 dc.SetFont(GetFont());
177
178 wxPoint pt;
179 pt.x = TEXT_MARGIN_X;
180 pt.y = TEXT_MARGIN_Y;
181 size_t count = m_textLines.GetCount();
182 for ( size_t n = 0; n < count; n++ )
183 {
184 dc.DrawText(m_textLines[n], pt);
185
186 pt.y += m_heightLine;
187 }
188 }
189
190 void wxTipWindow::OnMouseClick(wxMouseEvent& event)
191 {
192 Close();
193 }
194
195 void wxTipWindow::OnActivate(wxActivateEvent& event)
196 {
197 if (!event.GetActive())
198 Close();
199 }
200
201 void wxTipWindow::OnKillFocus(wxFocusEvent& event)
202 {
203 Close();
204 }