#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.
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;
DECLARE_EVENT_TABLE()
};
+#endif // wxUSE_POPUPWIN
#endif // _WX_TIPWIN_H_
// 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
{
EVT_MIDDLE_DOWN(wxTipWindowView::OnMouseClick)
EVT_KILL_FOCUS(wxTipWindowView::OnKillFocus)
END_EVENT_TABLE()
-
+#endif
// ----------------------------------------------------------------------------
// wxTipWindow
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);
#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()
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,
GetParent()->Close();
}
+#endif