From 8962e1d9383d615ea48979cd76bd53d5aa5b3026 Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Fri, 30 Nov 2001 23:02:27 +0000 Subject: [PATCH] wxTipWindow is now a wxPopupTransientWindow instead of a wxFrame. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@12773 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/latex/wx/tipwin.tex | 3 +- include/wx/tipwin.h | 15 +++-- src/generic/tipwin.cpp | 139 +++++++++++++++++++++++++++++++-------- 3 files changed, 124 insertions(+), 33 deletions(-) diff --git a/docs/latex/wx/tipwin.tex b/docs/latex/wx/tipwin.tex index 602af2bba5..9983b59c77 100644 --- a/docs/latex/wx/tipwin.tex +++ b/docs/latex/wx/tipwin.tex @@ -9,7 +9,8 @@ will create it when required. \wxheading{Derived from} -\helpref{wxFrame}{wxframe}\\ +wxPopupTransientWindow\\ +wxPopupWindow\\ \helpref{wxWindow}{wxwindow}\\ \helpref{wxEvtHandler}{wxevthandler}\\ \helpref{wxObject}{wxobject} diff --git a/include/wx/tipwin.h b/include/wx/tipwin.h index 37591de446..ce4e3ec69a 100644 --- a/include/wx/tipwin.h +++ b/include/wx/tipwin.h @@ -17,15 +17,15 @@ #pragma interface "tipwin.h" #endif -#include "wx/frame.h" +#include "wx/popupwin.h" +#if wxUSE_POPUPWIN // ---------------------------------------------------------------------------- // wxTipWindow // ---------------------------------------------------------------------------- -class WXDLLEXPORT wxTipWindow : public wxFrame +class WXDLLEXPORT wxTipWindow : public wxPopupTransientWindow { - friend class wxTipWindowView; public: // Supply windowPtr for it to null the given address // when the window has closed. @@ -36,11 +36,15 @@ public: void SetTipWindowPtr(wxTipWindow** windowPtr) { m_windowPtr = windowPtr; } + // calculate the client rect we need to display the text + void Adjust(const wxString& text, wxCoord maxLength); + + void Close(); + protected: // event handlers void OnMouseClick(wxMouseEvent& event); - void OnActivate(wxActivateEvent& event); - void OnKillFocus(wxFocusEvent& event); + void OnPaint(wxPaintEvent& event); private: wxArrayString m_textLines; @@ -50,4 +54,5 @@ private: DECLARE_EVENT_TABLE() }; +#endif // wxUSE_POPUPWIN #endif // _WX_TIPWIN_H_ diff --git a/src/generic/tipwin.cpp b/src/generic/tipwin.cpp index ed4939a286..a902a891c7 100644 --- a/src/generic/tipwin.cpp +++ b/src/generic/tipwin.cpp @@ -51,14 +51,15 @@ static const wxCoord TEXT_MARGIN_Y = 3; // event tables // ---------------------------------------------------------------------------- -BEGIN_EVENT_TABLE(wxTipWindow, wxFrame) +BEGIN_EVENT_TABLE(wxTipWindow, wxPopupTransientWindow) EVT_LEFT_DOWN(wxTipWindow::OnMouseClick) EVT_RIGHT_DOWN(wxTipWindow::OnMouseClick) EVT_MIDDLE_DOWN(wxTipWindow::OnMouseClick) - EVT_KILL_FOCUS(wxTipWindow::OnKillFocus) - EVT_ACTIVATE(wxTipWindow::OnActivate) + EVT_PAINT(wxTipWindow::OnPaint) END_EVENT_TABLE() + +#if 0 // Viewer window to put in the frame class wxTipWindowView: public wxWindow { @@ -85,7 +86,7 @@ BEGIN_EVENT_TABLE(wxTipWindowView, wxWindow) EVT_MIDDLE_DOWN(wxTipWindowView::OnMouseClick) EVT_KILL_FOCUS(wxTipWindowView::OnKillFocus) END_EVENT_TABLE() - +#endif // ---------------------------------------------------------------------------- // wxTipWindow @@ -94,10 +95,10 @@ END_EVENT_TABLE() wxTipWindow::wxTipWindow(wxWindow *parent, const wxString& text, wxCoord maxLength, wxTipWindow** windowPtr) - : wxFrame(parent, -1, _T(""), - wxDefaultPosition, wxDefaultSize, - wxNO_BORDER | wxFRAME_NO_TASKBAR ) + : wxPopupTransientWindow(parent) { + m_windowPtr = windowPtr; + // set colours SetForegroundColour(*wxBLACK); @@ -108,18 +109,15 @@ wxTipWindow::wxTipWindow(wxWindow *parent, #endif SetBackgroundColour(bkCol); - // set position and size + // set size and position + Adjust(text, maxLength); int x, y; wxGetMousePosition(&x, &y); - Move(x, y + 20); + Position(wxPoint(x, y+10), wxSize(0,0)); - wxTipWindowView* tipWindowView = new wxTipWindowView(this); - tipWindowView->Adjust(text, maxLength); - - m_windowPtr = windowPtr; - - Show(TRUE); - tipWindowView->SetFocus(); + //Show(TRUE); + Popup(); + SetFocus(); } wxTipWindow::~wxTipWindow() @@ -135,26 +133,112 @@ void wxTipWindow::OnMouseClick(wxMouseEvent& WXUNUSED(event)) Close(); } -void wxTipWindow::OnActivate(wxActivateEvent& event) + +void wxTipWindow::OnPaint(wxPaintEvent& WXUNUSED(event)) { - if (!event.GetActive()) - Close(); + wxPaintDC dc(this); + + wxRect rect; + wxSize size = GetClientSize(); + rect.width = size.x; + rect.height = size.y; + + // first filll the background + dc.SetBrush(wxBrush(GetBackgroundColour(), wxSOLID)); + + dc.SetPen( * wxBLACK_PEN ); + dc.DrawRectangle(rect); + + // and then draw the text line by line + dc.SetFont(GetFont()); + + wxPoint pt; + pt.x = TEXT_MARGIN_X; + pt.y = TEXT_MARGIN_Y; + size_t count = m_textLines.GetCount(); + for ( size_t n = 0; n < count; n++ ) + { + dc.DrawText(m_textLines[n], pt); + + pt.y += m_heightLine; + } } -void wxTipWindow::OnKillFocus(wxFocusEvent& WXUNUSED(event)) + +void wxTipWindow::Adjust(const wxString& text, wxCoord maxLength) { - // Under Windows at least, we will get this immediately - // because when the view window is focussed, the - // tip window goes out of focus. -#ifdef __WXGTK__ - Close(); -#endif + wxClientDC dc(this); + dc.SetFont(GetFont()); + + // calculate the length: we want each line be no longer than maxLength + // pixels and we only break lines at words boundary + wxString current; + wxCoord height, width, + widthMax = 0; + m_heightLine = 0; + + bool breakLine = FALSE; + for ( const wxChar *p = text.c_str(); ; p++ ) + { + if ( *p == _T('\n') || *p == _T('\0') ) + { + dc.GetTextExtent(current, &width, &height); + if ( width > widthMax ) + widthMax = width; + + if ( height > m_heightLine ) + m_heightLine = height; + + m_textLines.Add(current); + + if ( !*p ) + { + // end of text + break; + } + + current.clear(); + breakLine = FALSE; + } + else if ( breakLine && (*p == _T(' ') || *p == _T('\t')) ) + { + // word boundary - break the line here + m_textLines.Add(current); + current.clear(); + breakLine = FALSE; + } + else // line goes on + { + current += *p; + dc.GetTextExtent(current, &width, &height); + if ( width > maxLength ) + breakLine = TRUE; + + if ( width > widthMax ) + widthMax = width; + + if ( height > m_heightLine ) + m_heightLine = height; + } + } + + // take into account the border size and the margins + SetClientSize(2*(TEXT_MARGIN_X + 1) + widthMax, + 2*(TEXT_MARGIN_Y + 1) + m_textLines.GetCount() * m_heightLine); } + +void wxTipWindow::Close() +{ + Show(FALSE); + Destroy(); +} + + // ---------------------------------------------------------------------------- // wxTipWindowView // ---------------------------------------------------------------------------- - +#if 0 wxTipWindowView::wxTipWindowView(wxWindow *parent) : wxWindow(parent, -1, wxDefaultPosition, wxDefaultSize, @@ -280,3 +364,4 @@ void wxTipWindowView::OnKillFocus(wxFocusEvent& WXUNUSED(event)) GetParent()->Close(); } +#endif