]>
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
);
104 wxColour
bkCol(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_INFOBK
));
106 wxColour
bkCol(wxColour(255, 255, 225));
108 SetBackgroundColour(bkCol
);
110 // set position and size
112 wxGetMousePosition(&x
, &y
);
115 wxTipWindowView
* tipWindowView
= new wxTipWindowView(this);
116 tipWindowView
->Adjust(text
, maxLength
);
118 m_windowPtr
= windowPtr
;
121 tipWindowView
->SetFocus();
124 wxTipWindow::~wxTipWindow()
132 void wxTipWindow::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
137 void wxTipWindow::OnActivate(wxActivateEvent
& event
)
139 if (!event
.GetActive())
143 void wxTipWindow::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
145 // Under Windows at least, we will get this immediately
146 // because when the view window is focussed, the
147 // tip window goes out of focus.
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
157 wxTipWindowView::wxTipWindowView(wxWindow
*parent
)
158 : wxWindow(parent
, -1,
159 wxDefaultPosition
, wxDefaultSize
,
163 SetForegroundColour(*wxBLACK
);
165 wxColour
bkCol(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_INFOBK
));
167 wxColour
bkCol(wxColour(255, 255, 225));
169 SetBackgroundColour(bkCol
);
170 m_creationTime
= wxGetLocalTime();
173 void wxTipWindowView::Adjust(const wxString
& text
, wxCoord maxLength
)
175 wxTipWindow
* parent
= (wxTipWindow
*) GetParent();
177 dc
.SetFont(GetFont());
179 // calculate the length: we want each line be no longer than maxLength
180 // pixels and we only break lines at words boundary
182 wxCoord height
, width
,
184 parent
->m_heightLine
= 0;
186 bool breakLine
= FALSE
;
187 for ( const wxChar
*p
= text
.c_str(); ; p
++ )
189 if ( *p
== _T('\n') || *p
== _T('\0') )
191 dc
.GetTextExtent(current
, &width
, &height
);
192 if ( width
> widthMax
)
195 if ( height
> parent
->m_heightLine
)
196 parent
->m_heightLine
= height
;
198 parent
->m_textLines
.Add(current
);
209 else if ( breakLine
&& (*p
== _T(' ') || *p
== _T('\t')) )
211 // word boundary - break the line here
212 parent
->m_textLines
.Add(current
);
219 dc
.GetTextExtent(current
, &width
, &height
);
220 if ( width
> maxLength
)
223 if ( width
> widthMax
)
226 if ( height
> parent
->m_heightLine
)
227 parent
->m_heightLine
= height
;
231 // take into account the border size and the margins
232 GetParent()->SetClientSize(2*(TEXT_MARGIN_X
+ 1) + widthMax
,
233 2*(TEXT_MARGIN_Y
+ 1) + parent
->m_textLines
.GetCount()*parent
->m_heightLine
);
236 void wxTipWindowView::OnPaint(wxPaintEvent
& WXUNUSED(event
))
238 wxTipWindow
* parent
= (wxTipWindow
*) GetParent();
245 wxSize size
= GetClientSize();
247 rect
.height
= size
.y
;
249 // first filll the background
250 dc
.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID
));
252 // Under Windows, you apparently get a thin black border whether you like it or not :-(
254 dc
.SetPen( * wxTRANSPARENT_PEN
);
256 dc
.SetPen( * wxBLACK_PEN
);
258 dc
.DrawRectangle(rect
);
260 // and then draw the text line by line
261 dc
.SetFont(GetFont());
264 pt
.x
= TEXT_MARGIN_X
;
265 pt
.y
= TEXT_MARGIN_Y
;
266 size_t count
= parent
->m_textLines
.GetCount();
267 for ( size_t n
= 0; n
< count
; n
++ )
269 dc
.DrawText(parent
->m_textLines
[n
], pt
);
271 pt
.y
+= parent
->m_heightLine
;
275 void wxTipWindowView::OnMouseClick(wxMouseEvent
& WXUNUSED(event
))
277 GetParent()->Close();
280 void wxTipWindowView::OnKillFocus(wxFocusEvent
& WXUNUSED(event
))
282 // Workaround the kill focus event happening just after creation in wxGTK
283 if (wxGetLocalTime() > m_creationTime
+ 1)
284 GetParent()->Close();