]>
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
& WXUNUSED(event
))
135 void wxTipWindow::OnActivate(wxActivateEvent
& event
)
137 if (!event
.GetActive())
141 void wxTipWindow::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
143 // Under Windows at least, we will get this immediately
144 // because when the view window is focussed, the
145 // tip window goes out of focus.
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
155 wxTipWindowView::wxTipWindowView(wxWindow
*parent
)
156 : wxWindow(parent
, -1,
157 wxDefaultPosition
, wxDefaultSize
,
161 SetForegroundColour(*wxBLACK
);
162 #if !defined(__WXPM__)
163 SetBackgroundColour(wxColour(0xc3ffff));
165 // What is 0xc3ffff, try some legable documentation for those of us who don't memorize hex codes??
166 SetBackgroundColour(wxColour(*wxWHITE
));
168 m_creationTime
= wxGetLocalTime();
171 void wxTipWindowView::Adjust(const wxString
& text
, wxCoord maxLength
)
173 wxTipWindow
* parent
= (wxTipWindow
*) GetParent();
175 dc
.SetFont(GetFont());
177 // calculate the length: we want each line be no longer than maxLength
178 // pixels and we only break lines at words boundary
180 wxCoord height
, width
,
182 parent
->m_heightLine
= 0;
184 bool breakLine
= FALSE
;
185 for ( const wxChar
*p
= text
.c_str(); ; p
++ )
187 if ( *p
== _T('\n') || *p
== _T('\0') )
189 dc
.GetTextExtent(current
, &width
, &height
);
190 if ( width
> widthMax
)
193 if ( height
> parent
->m_heightLine
)
194 parent
->m_heightLine
= height
;
196 parent
->m_textLines
.Add(current
);
207 else if ( breakLine
&& (*p
== _T(' ') || *p
== _T('\t')) )
209 // word boundary - break the line here
210 parent
->m_textLines
.Add(current
);
217 dc
.GetTextExtent(current
, &width
, &height
);
218 if ( width
> maxLength
)
221 if ( width
> widthMax
)
224 if ( height
> parent
->m_heightLine
)
225 parent
->m_heightLine
= height
;
229 // take into account the border size and the margins
230 GetParent()->SetClientSize(2*(TEXT_MARGIN_X
+ 1) + widthMax
,
231 2*(TEXT_MARGIN_Y
+ 1) + parent
->m_textLines
.GetCount()*parent
->m_heightLine
);
234 void wxTipWindowView::OnPaint(wxPaintEvent
& WXUNUSED(event
))
236 wxTipWindow
* parent
= (wxTipWindow
*) GetParent();
243 wxSize size
= GetClientSize();
245 rect
.height
= size
.y
;
247 // first filll the background
248 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
250 // Under Windows, you apparently get a thin black border whether you like it or not :-(
252 dc
.SetPen( * wxTRANSPARENT_PEN
);
254 dc
.SetPen( * wxBLACK_PEN
);
256 dc
.DrawRectangle(rect
);
258 // and then draw the text line by line
259 dc
.SetFont(GetFont());
262 pt
.x
= TEXT_MARGIN_X
;
263 pt
.y
= TEXT_MARGIN_Y
;
264 size_t count
= parent
->m_textLines
.GetCount();
265 for ( size_t n
= 0; n
< count
; n
++ )
267 dc
.DrawText(parent
->m_textLines
[n
], pt
);
269 pt
.y
+= parent
->m_heightLine
;
273 void wxTipWindowView::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
275 GetParent()->Close();
278 void wxTipWindowView::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
280 // Workaround the kill focus event happening just after creation in wxGTK
281 if (wxGetLocalTime() > m_creationTime
+ 1)
282 GetParent()->Close();