]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/tipwin.cpp
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"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 static const wxCoord TEXT_MARGIN_X
= 3;
43 static const wxCoord TEXT_MARGIN_Y
= 3;
45 // ============================================================================
47 // ============================================================================
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 BEGIN_EVENT_TABLE(wxTipWindow
, wxFrame
)
54 EVT_LEFT_DOWN(wxTipWindow::OnMouseClick
)
55 EVT_RIGHT_DOWN(wxTipWindow::OnMouseClick
)
56 EVT_MIDDLE_DOWN(wxTipWindow::OnMouseClick
)
57 EVT_KILL_FOCUS(wxTipWindow::OnKillFocus
)
58 EVT_ACTIVATE(wxTipWindow::OnActivate
)
61 // Viewer window to put in the frame
62 class wxTipWindowView
: public wxWindow
65 wxTipWindowView(wxWindow
*parent
);
68 void OnPaint(wxPaintEvent
& event
);
69 void OnMouseClick(wxMouseEvent
& event
);
70 void OnKillFocus(wxFocusEvent
& event
);
72 // calculate the client rect we need to display the text
73 void Adjust(const wxString
& text
, wxCoord maxLength
);
80 BEGIN_EVENT_TABLE(wxTipWindowView
, wxWindow
)
81 EVT_PAINT(wxTipWindowView::OnPaint
)
82 EVT_LEFT_DOWN(wxTipWindowView::OnMouseClick
)
83 EVT_RIGHT_DOWN(wxTipWindowView::OnMouseClick
)
84 EVT_MIDDLE_DOWN(wxTipWindowView::OnMouseClick
)
85 EVT_KILL_FOCUS(wxTipWindowView::OnKillFocus
)
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 wxTipWindow::wxTipWindow(wxWindow
*parent
,
95 wxCoord maxLength
, wxTipWindow
** windowPtr
)
96 : wxFrame(parent
, -1, _T(""),
97 wxDefaultPosition
, wxDefaultSize
,
98 wxNO_BORDER
| wxFRAME_FLOAT_ON_PARENT
)
101 SetForegroundColour(*wxBLACK
);
102 SetBackgroundColour(wxColour(255, 255, 231));
103 // set position and size
105 wxGetMousePosition(&x
, &y
);
108 wxTipWindowView
* tipWindowView
= new wxTipWindowView(this);
109 tipWindowView
->Adjust(text
, maxLength
);
111 m_windowPtr
= windowPtr
;
114 tipWindowView
->SetFocus();
117 wxTipWindow::~wxTipWindow()
125 void wxTipWindow::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
130 void wxTipWindow::OnActivate(wxActivateEvent
& event
)
132 if (!event
.GetActive())
136 void wxTipWindow::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
138 // Under Windows at least, we will get this immediately
139 // because when the view window is focussed, the
140 // tip window goes out of focus.
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
150 wxTipWindowView::wxTipWindowView(wxWindow
*parent
)
151 : wxWindow(parent
, -1,
152 wxDefaultPosition
, wxDefaultSize
,
156 SetForegroundColour(*wxBLACK
);
157 SetBackgroundColour(wxColour(255, 255, 231));
158 m_creationTime
= wxGetLocalTime();
161 void wxTipWindowView::Adjust(const wxString
& text
, wxCoord maxLength
)
163 wxTipWindow
* parent
= (wxTipWindow
*) GetParent();
165 dc
.SetFont(GetFont());
167 // calculate the length: we want each line be no longer than maxLength
168 // pixels and we only break lines at words boundary
170 wxCoord height
, width
,
172 parent
->m_heightLine
= 0;
174 bool breakLine
= FALSE
;
175 for ( const wxChar
*p
= text
.c_str(); ; p
++ )
177 if ( *p
== _T('\n') || *p
== _T('\0') )
179 dc
.GetTextExtent(current
, &width
, &height
);
180 if ( width
> widthMax
)
183 if ( height
> parent
->m_heightLine
)
184 parent
->m_heightLine
= height
;
186 parent
->m_textLines
.Add(current
);
197 else if ( breakLine
&& (*p
== _T(' ') || *p
== _T('\t')) )
199 // word boundary - break the line here
200 parent
->m_textLines
.Add(current
);
207 dc
.GetTextExtent(current
, &width
, &height
);
208 if ( width
> maxLength
)
211 if ( width
> widthMax
)
214 if ( height
> parent
->m_heightLine
)
215 parent
->m_heightLine
= height
;
219 // take into account the border size and the margins
220 GetParent()->SetClientSize(2*(TEXT_MARGIN_X
+ 1) + widthMax
,
221 2*(TEXT_MARGIN_Y
+ 1) + parent
->m_textLines
.GetCount()*parent
->m_heightLine
);
224 void wxTipWindowView::OnPaint(wxPaintEvent
& WXUNUSED(event
))
226 wxTipWindow
* parent
= (wxTipWindow
*) GetParent();
233 wxSize size
= GetClientSize();
235 rect
.height
= size
.y
;
237 // first filll the background
238 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
240 // Under Windows, you apparently get a thin black border whether you like it or not :-(
242 dc
.SetPen( * wxTRANSPARENT_PEN
);
244 dc
.SetPen( * wxBLACK_PEN
);
246 dc
.DrawRectangle(rect
);
248 // and then draw the text line by line
249 dc
.SetFont(GetFont());
252 pt
.x
= TEXT_MARGIN_X
;
253 pt
.y
= TEXT_MARGIN_Y
;
254 size_t count
= parent
->m_textLines
.GetCount();
255 for ( size_t n
= 0; n
< count
; n
++ )
257 dc
.DrawText(parent
->m_textLines
[n
], pt
);
259 pt
.y
+= parent
->m_heightLine
;
263 void wxTipWindowView::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
265 GetParent()->Close();
268 void wxTipWindowView::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
270 // Workaround the kill focus event happening just after creation in wxGTK
271 if (wxGetLocalTime() > m_creationTime
+ 1)
272 GetParent()->Close();