]>
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 #if !defined(__WXPM__)
103 SetBackgroundColour(wxColour(0xc3ffff));
105 // What is 0xc3ffff, try some legable documentation for those of us who don't memorize hex codes??
106 SetBackgroundColour(wxColour(*wxWHITE
));
108 // set position and size
110 wxGetMousePosition(&x
, &y
);
113 wxTipWindowView
* tipWindowView
= new wxTipWindowView(this);
114 tipWindowView
->Adjust(text
, maxLength
);
116 m_windowPtr
= windowPtr
;
119 tipWindowView
->SetFocus();
122 wxTipWindow::~wxTipWindow()
130 void wxTipWindow::OnMouseClick(wxMouseEvent
& event
)
135 void wxTipWindow::OnActivate(wxActivateEvent
& event
)
137 if (!event
.GetActive())
141 void wxTipWindow::OnKillFocus(wxFocusEvent
& event
)
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
150 wxTipWindowView::wxTipWindowView(wxWindow
*parent
)
151 : wxWindow(parent
, -1,
152 wxDefaultPosition
, wxDefaultSize
,
156 SetForegroundColour(*wxBLACK
);
157 #if !defined(__WXPM__)
158 SetBackgroundColour(wxColour(0xc3ffff));
160 // What is 0xc3ffff, try some legable documentation for those of us who don't memorize hex codes??
161 SetBackgroundColour(wxColour(*wxWHITE
));
163 m_creationTime
= wxGetLocalTime();
166 void wxTipWindowView::Adjust(const wxString
& text
, wxCoord maxLength
)
168 wxTipWindow
* parent
= (wxTipWindow
*) GetParent();
170 dc
.SetFont(GetFont());
172 // calculate the length: we want each line be no longer than maxLength
173 // pixels and we only break lines at words boundary
175 wxCoord height
, width
,
177 parent
->m_heightLine
= 0;
179 bool breakLine
= FALSE
;
180 for ( const wxChar
*p
= text
.c_str(); ; p
++ )
182 if ( *p
== _T('\n') || *p
== _T('\0') )
184 dc
.GetTextExtent(current
, &width
, &height
);
185 if ( width
> widthMax
)
188 if ( height
> parent
->m_heightLine
)
189 parent
->m_heightLine
= height
;
191 parent
->m_textLines
.Add(current
);
202 else if ( breakLine
&& (*p
== _T(' ') || *p
== _T('\t')) )
204 // word boundary - break the line here
205 parent
->m_textLines
.Add(current
);
212 dc
.GetTextExtent(current
, &width
, &height
);
213 if ( width
> maxLength
)
216 if ( width
> widthMax
)
219 if ( height
> parent
->m_heightLine
)
220 parent
->m_heightLine
= height
;
224 // take into account the border size and the margins
225 GetParent()->SetClientSize(2*(TEXT_MARGIN_X
+ 1) + widthMax
,
226 2*(TEXT_MARGIN_Y
+ 1) + parent
->m_textLines
.GetCount()*parent
->m_heightLine
);
229 void wxTipWindowView::OnPaint(wxPaintEvent
& event
)
231 wxTipWindow
* parent
= (wxTipWindow
*) GetParent();
238 wxSize size
= GetClientSize();
240 rect
.height
= size
.y
;
242 // first filll the background
243 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
245 // Under Windows, you apparently get a thin black border whether you like it or not :-(
247 dc
.SetPen( * wxTRANSPARENT_PEN
);
249 dc
.SetPen( * wxBLACK_PEN
);
251 dc
.DrawRectangle(rect
);
253 // and then draw the text line by line
254 dc
.SetFont(GetFont());
257 pt
.x
= TEXT_MARGIN_X
;
258 pt
.y
= TEXT_MARGIN_Y
;
259 size_t count
= parent
->m_textLines
.GetCount();
260 for ( size_t n
= 0; n
< count
; n
++ )
262 dc
.DrawText(parent
->m_textLines
[n
], pt
);
264 pt
.y
+= parent
->m_heightLine
;
268 void wxTipWindowView::OnMouseClick(wxMouseEvent
& event
)
270 GetParent()->Close();
273 void wxTipWindowView::OnKillFocus(wxFocusEvent
& event
)
275 // Workaround the kill focus event happening just after creation in wxGTK
276 if (wxGetLocalTime() > m_creationTime
+ 1)
277 GetParent()->Close();