]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/tipwin.cpp
a902a891c78b657bb5102ccaa492bcb246d1a4e9
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/tipwin.cpp
3 // Purpose: implementation of wxTipWindow
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "tipwin.h"
24 // For compilers that support precompilatixon, includes "wx/wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/dcclient.h"
35 #include "wx/tipwin.h"
37 #include "wx/settings.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 static const wxCoord TEXT_MARGIN_X
= 3;
44 static const wxCoord TEXT_MARGIN_Y
= 3;
46 // ============================================================================
48 // ============================================================================
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
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
)
63 // Viewer window to put in the frame
64 class wxTipWindowView
: public wxWindow
67 wxTipWindowView(wxWindow
*parent
);
70 void OnPaint(wxPaintEvent
& event
);
71 void OnMouseClick(wxMouseEvent
& event
);
72 void OnKillFocus(wxFocusEvent
& event
);
74 // calculate the client rect we need to display the text
75 void Adjust(const wxString
& text
, wxCoord maxLength
);
82 BEGIN_EVENT_TABLE(wxTipWindowView
, wxWindow
)
83 EVT_PAINT(wxTipWindowView::OnPaint
)
84 EVT_LEFT_DOWN(wxTipWindowView::OnMouseClick
)
85 EVT_RIGHT_DOWN(wxTipWindowView::OnMouseClick
)
86 EVT_MIDDLE_DOWN(wxTipWindowView::OnMouseClick
)
87 EVT_KILL_FOCUS(wxTipWindowView::OnKillFocus
)
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 wxTipWindow::wxTipWindow(wxWindow
*parent
,
97 wxCoord maxLength
, wxTipWindow
** windowPtr
)
98 : wxPopupTransientWindow(parent
)
100 m_windowPtr
= windowPtr
;
103 SetForegroundColour(*wxBLACK
);
106 wxColour
bkCol(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_INFOBK
));
108 wxColour
bkCol(wxColour(255, 255, 225));
110 SetBackgroundColour(bkCol
);
112 // set size and position
113 Adjust(text
, maxLength
);
115 wxGetMousePosition(&x
, &y
);
116 Position(wxPoint(x
, y
+10), wxSize(0,0));
123 wxTipWindow::~wxTipWindow()
131 void wxTipWindow::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
137 void wxTipWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
142 wxSize size
= GetClientSize();
144 rect
.height
= size
.y
;
146 // first filll the background
147 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
149 dc
.SetPen( * wxBLACK_PEN
);
150 dc
.DrawRectangle(rect
);
152 // and then draw the text line by line
153 dc
.SetFont(GetFont());
156 pt
.x
= TEXT_MARGIN_X
;
157 pt
.y
= TEXT_MARGIN_Y
;
158 size_t count
= m_textLines
.GetCount();
159 for ( size_t n
= 0; n
< count
; n
++ )
161 dc
.DrawText(m_textLines
[n
], pt
);
163 pt
.y
+= m_heightLine
;
168 void wxTipWindow::Adjust(const wxString
& text
, wxCoord maxLength
)
171 dc
.SetFont(GetFont());
173 // calculate the length: we want each line be no longer than maxLength
174 // pixels and we only break lines at words boundary
176 wxCoord height
, width
,
180 bool breakLine
= FALSE
;
181 for ( const wxChar
*p
= text
.c_str(); ; p
++ )
183 if ( *p
== _T('\n') || *p
== _T('\0') )
185 dc
.GetTextExtent(current
, &width
, &height
);
186 if ( width
> widthMax
)
189 if ( height
> m_heightLine
)
190 m_heightLine
= height
;
192 m_textLines
.Add(current
);
203 else if ( breakLine
&& (*p
== _T(' ') || *p
== _T('\t')) )
205 // word boundary - break the line here
206 m_textLines
.Add(current
);
213 dc
.GetTextExtent(current
, &width
, &height
);
214 if ( width
> maxLength
)
217 if ( width
> widthMax
)
220 if ( height
> m_heightLine
)
221 m_heightLine
= height
;
225 // take into account the border size and the margins
226 SetClientSize(2*(TEXT_MARGIN_X
+ 1) + widthMax
,
227 2*(TEXT_MARGIN_Y
+ 1) + m_textLines
.GetCount() * m_heightLine
);
231 void wxTipWindow::Close()
238 // ----------------------------------------------------------------------------
240 // ----------------------------------------------------------------------------
242 wxTipWindowView::wxTipWindowView(wxWindow
*parent
)
243 : wxWindow(parent
, -1,
244 wxDefaultPosition
, wxDefaultSize
,
248 SetForegroundColour(*wxBLACK
);
250 wxColour
bkCol(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_INFOBK
));
252 wxColour
bkCol(wxColour(255, 255, 225));
254 SetBackgroundColour(bkCol
);
255 m_creationTime
= wxGetLocalTime();
258 void wxTipWindowView::Adjust(const wxString
& text
, wxCoord maxLength
)
260 wxTipWindow
* parent
= (wxTipWindow
*) GetParent();
262 dc
.SetFont(GetFont());
264 // calculate the length: we want each line be no longer than maxLength
265 // pixels and we only break lines at words boundary
267 wxCoord height
, width
,
269 parent
->m_heightLine
= 0;
271 bool breakLine
= FALSE
;
272 for ( const wxChar
*p
= text
.c_str(); ; p
++ )
274 if ( *p
== _T('\n') || *p
== _T('\0') )
276 dc
.GetTextExtent(current
, &width
, &height
);
277 if ( width
> widthMax
)
280 if ( height
> parent
->m_heightLine
)
281 parent
->m_heightLine
= height
;
283 parent
->m_textLines
.Add(current
);
294 else if ( breakLine
&& (*p
== _T(' ') || *p
== _T('\t')) )
296 // word boundary - break the line here
297 parent
->m_textLines
.Add(current
);
304 dc
.GetTextExtent(current
, &width
, &height
);
305 if ( width
> maxLength
)
308 if ( width
> widthMax
)
311 if ( height
> parent
->m_heightLine
)
312 parent
->m_heightLine
= height
;
316 // take into account the border size and the margins
317 GetParent()->SetClientSize(2*(TEXT_MARGIN_X
+ 1) + widthMax
,
318 2*(TEXT_MARGIN_Y
+ 1) + parent
->m_textLines
.GetCount()*parent
->m_heightLine
);
321 void wxTipWindowView::OnPaint(wxPaintEvent
& WXUNUSED(event
))
323 wxTipWindow
* parent
= (wxTipWindow
*) GetParent();
330 wxSize size
= GetClientSize();
332 rect
.height
= size
.y
;
334 // first filll the background
335 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
337 dc
.SetPen( * wxBLACK_PEN
);
338 dc
.DrawRectangle(rect
);
340 // and then draw the text line by line
341 dc
.SetFont(GetFont());
344 pt
.x
= TEXT_MARGIN_X
;
345 pt
.y
= TEXT_MARGIN_Y
;
346 size_t count
= parent
->m_textLines
.GetCount();
347 for ( size_t n
= 0; n
< count
; n
++ )
349 dc
.DrawText(parent
->m_textLines
[n
], pt
);
351 pt
.y
+= parent
->m_heightLine
;
355 void wxTipWindowView::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
357 GetParent()->Close();
360 void wxTipWindowView::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
362 // Workaround the kill focus event happening just after creation in wxGTK
363 if (wxGetLocalTime() > m_creationTime
+ 1)
364 GetParent()->Close();