made wxToolTip::Enable() and SetDelay() static (as in wxGTK) and added some

code in the controls sample to test them


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@1693 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
1999-02-14 21:59:48 +00:00
parent 9fc0fe377b
commit 16f6dfd814
6 changed files with 243 additions and 98 deletions

View File

@@ -112,10 +112,20 @@ public:
bool IsModalShowing() const { return m_modalShowing; } bool IsModalShowing() const { return m_modalShowing; }
// tooltip management
#if wxUSE_TOOLTIPS
WXHWND GetToolTipCtrl() const { return m_hwndToolTip; }
void SetToolTipCtrl(WXHWND hwndTT) { m_hwndToolTip = hwndTT; }
#endif // tooltips
protected: protected:
bool m_modalShowing; bool m_modalShowing;
WXHWND m_hwndOldFocus; // the window which had focus before we were shown WXHWND m_hwndOldFocus; // the window which had focus before we were shown
#if wxUSE_TOOLTIPS
WXHWND m_hwndToolTip;
#endif // tooltips
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
}; };

View File

@@ -25,15 +25,11 @@ public:
void SetWindow(wxWindow *win); void SetWindow(wxWindow *win);
wxWindow *GetWindow() const { return m_window; } wxWindow *GetWindow() const { return m_window; }
// controlling tooltip behaviour: under MSW, these functions change the // controlling tooltip behaviour: globally change tooltip parameters
// behaviour of the tooltips for all controls in the same frame as this
// one (it is an implementation limitation). Also, these functions won't
// do anything before the tooltip control is associated with a window, so
// SetWindow() should be called first
// enable or disable the tooltips globally // enable or disable the tooltips globally
void Enable(bool flag); static void Enable(bool flag);
// set the delay after which the tooltip appears // set the delay after which the tooltip appears
void SetDelay(long milliseconds); static void SetDelay(long milliseconds);
// implementation // implementation
void RelayEvent(WXMSG *msg); void RelayEvent(WXMSG *msg);

View File

@@ -271,6 +271,7 @@ public:
void SetToolTip(wxToolTip *tooltip); void SetToolTip(wxToolTip *tooltip);
// get the current tooltip (may return NULL if none) // get the current tooltip (may return NULL if none)
wxToolTip* GetToolTip() const { return m_tooltip; } wxToolTip* GetToolTip() const { return m_tooltip; }
#endif // wxUSE_TOOLTIPS #endif // wxUSE_TOOLTIPS
// Accept files for dragging // Accept files for dragging
@@ -767,7 +768,7 @@ private:
// the associated tooltip (may be NULL if none) // the associated tooltip (may be NULL if none)
#if wxUSE_TOOLTIPS #if wxUSE_TOOLTIPS
wxToolTip *m_tooltip; wxToolTip *m_tooltip;
#endif #endif // tooltips
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
}; };

View File

@@ -128,6 +128,10 @@ public:
void OnQuit(wxCommandEvent& event); void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event); void OnAbout(wxCommandEvent& event);
#if wxUSE_TOOLTIPS
void OnSetTooltipDelay(wxCommandEvent& event);
void OnToggleTooltips(wxCommandEvent& event);
#endif // wxUSE_TOOLTIPS
void OnIdle( wxIdleEvent& event ); void OnIdle( wxIdleEvent& event );
void OnSize( wxSizeEvent& event ); void OnSize( wxSizeEvent& event );
@@ -145,9 +149,16 @@ IMPLEMENT_APP(MyApp)
// MyApp // MyApp
//---------------------------------------------------------------------- //----------------------------------------------------------------------
const int MINIMAL_QUIT = 100; enum
const int MINIMAL_TEXT = 101; {
const int MINIMAL_ABOUT = 102; MINIMAL_QUIT = 100,
MINIMAL_TEXT,
MINIMAL_ABOUT,
// tooltip menu
MINIMAL_SET_TOOLTIP_DELAY = 200,
MINIMAL_ENABLE_TOOLTIPS
};
bool MyApp::OnInit() bool MyApp::OnInit()
{ {
@@ -163,11 +174,22 @@ bool MyApp::OnInit()
frame->SetIcon( wxICON(mondrian) ); frame->SetIcon( wxICON(mondrian) );
wxMenu *file_menu = new wxMenu; wxMenu *file_menu = new wxMenu;
file_menu->Append(MINIMAL_ABOUT, "&About"); file_menu->Append(MINIMAL_ABOUT, "&About");
file_menu->Append(MINIMAL_QUIT, "E&xit"); file_menu->Append(MINIMAL_QUIT, "E&xit");
wxMenuBar *menu_bar = new wxMenuBar; wxMenuBar *menu_bar = new wxMenuBar;
menu_bar->Append(file_menu, "&File"); menu_bar->Append(file_menu, "&File");
#if wxUSE_TOOLTIPS
wxMenu *tooltip_menu = new wxMenu;
tooltip_menu->Append(MINIMAL_SET_TOOLTIP_DELAY, "Set &delay");
tooltip_menu->AppendSeparator();
tooltip_menu->Append(MINIMAL_ENABLE_TOOLTIPS, "&Toggle tooltips",
"enable/disable tooltips", TRUE);
tooltip_menu->Check(MINIMAL_ENABLE_TOOLTIPS, TRUE);
menu_bar->Append(tooltip_menu, "&Tooltips");
#endif // wxUSE_TOOLTIPS
frame->SetMenuBar(menu_bar); frame->SetMenuBar(menu_bar);
frame->Show(TRUE); frame->Show(TRUE);
@@ -401,7 +423,7 @@ MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h )
m_listbox = new wxListBox( panel, ID_LISTBOX, wxPoint(10,10), wxSize(120,70), 5, choices ); m_listbox = new wxListBox( panel, ID_LISTBOX, wxPoint(10,10), wxSize(120,70), 5, choices );
#if wxUSE_TOOLTIPS #if wxUSE_TOOLTIPS
m_listbox->SetToolTip( "This is a list box" ); m_listbox->SetToolTip( "This is a list box" );
#endif #endif // wxUSE_TOOLTIPS
(void)new wxButton( panel, ID_LISTBOX_SEL_NUM, "Select #2", wxPoint(180,30), wxSize(140,30) ); (void)new wxButton( panel, ID_LISTBOX_SEL_NUM, "Select #2", wxPoint(180,30), wxSize(140,30) );
(void)new wxButton( panel, ID_LISTBOX_SEL_STR, "Select 'This'", wxPoint(340,30), wxSize(140,30) ); (void)new wxButton( panel, ID_LISTBOX_SEL_STR, "Select 'This'", wxPoint(340,30), wxSize(140,30) );
@@ -411,13 +433,13 @@ MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h )
button = new wxButton( panel, ID_LISTBOX_FONT, "Set Italic font", wxPoint(340,130), wxSize(140,30) ); button = new wxButton( panel, ID_LISTBOX_FONT, "Set Italic font", wxPoint(340,130), wxSize(140,30) );
#if wxUSE_TOOLTIPS #if wxUSE_TOOLTIPS
button->SetToolTip( "Press here to set italic font" ); button->SetToolTip( "Press here to set italic font" );
#endif #endif // wxUSE_TOOLTIPS
m_checkbox = new wxCheckBox( panel, ID_LISTBOX_ENABLE, "Disable", wxPoint(20,130), wxSize(140,30) ); m_checkbox = new wxCheckBox( panel, ID_LISTBOX_ENABLE, "Disable", wxPoint(20,130), wxSize(140,30) );
m_checkbox->SetValue(FALSE); m_checkbox->SetValue(FALSE);
#if wxUSE_TOOLTIPS #if wxUSE_TOOLTIPS
m_checkbox->SetToolTip( "Click here to disable the listbox" ); m_checkbox->SetToolTip( "Click here to disable the listbox" );
#endif #endif // wxUSE_TOOLTIPS
m_notebook->AddPage(panel, "wxListBox", TRUE, Image_List); m_notebook->AddPage(panel, "wxListBox", TRUE, Image_List);
panel = new wxPanel(m_notebook); panel = new wxPanel(m_notebook);
@@ -459,6 +481,10 @@ MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h )
new MyTextCtrl( panel, -1, "This one is with wxTE_PROCESS_TAB style.", new MyTextCtrl( panel, -1, "This one is with wxTE_PROCESS_TAB style.",
wxPoint(10,120), wxSize(320,70), wxTE_MULTILINE | wxTE_PROCESS_TAB); wxPoint(10,120), wxSize(320,70), wxTE_MULTILINE | wxTE_PROCESS_TAB);
#if wxUSE_TOOLTIPS
m_multitext->AppendText( "\nThis ctrl has a tooltip. " );
m_multitext->SetToolTip("Press F1 here.");
#endif // wxUSE_TOOLTIPS
(void)new wxStaticBox( panel, -1, "&Move cursor to the end of:", wxPoint(345, 0), wxSize(160, 100) ); (void)new wxStaticBox( panel, -1, "&Move cursor to the end of:", wxPoint(345, 0), wxSize(160, 100) );
(void)new wxButton( panel, ID_MOVE_END_ENTRY, "Text &entry", wxPoint(370, 20), wxSize(110, 30) ); (void)new wxButton( panel, ID_MOVE_END_ENTRY, "Text &entry", wxPoint(370, 20), wxSize(110, 30) );
@@ -661,7 +687,7 @@ void MyPanel::OnListBoxButtons( wxCommandEvent &event )
cb->SetToolTip( "Click to enable listbox" ); cb->SetToolTip( "Click to enable listbox" );
else else
cb->SetToolTip( "Click to disable listbox" ); cb->SetToolTip( "Click to disable listbox" );
#endif #endif // wxUSE_TOOLTIPS
m_listbox->Enable( event.GetInt() == 0 ); m_listbox->Enable( event.GetInt() == 0 );
break; break;
} }
@@ -864,6 +890,10 @@ MyPanel::~MyPanel()
BEGIN_EVENT_TABLE(MyFrame, wxFrame) BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(MINIMAL_QUIT, MyFrame::OnQuit) EVT_MENU(MINIMAL_QUIT, MyFrame::OnQuit)
EVT_MENU(MINIMAL_ABOUT, MyFrame::OnAbout) EVT_MENU(MINIMAL_ABOUT, MyFrame::OnAbout)
#if wxUSE_TOOLTIPS
EVT_MENU(MINIMAL_SET_TOOLTIP_DELAY, MyFrame::OnSetTooltipDelay)
EVT_MENU(MINIMAL_ENABLE_TOOLTIPS, MyFrame::OnToggleTooltips)
#endif // wxUSE_TOOLTIPS
EVT_SIZE(MyFrame::OnSize) EVT_SIZE(MyFrame::OnSize)
EVT_IDLE(MyFrame::OnIdle) EVT_IDLE(MyFrame::OnIdle)
END_EVENT_TABLE() END_EVENT_TABLE()
@@ -887,6 +917,40 @@ void MyFrame::OnAbout( wxCommandEvent& WXUNUSED(event) )
dialog.ShowModal(); dialog.ShowModal();
} }
#if wxUSE_TOOLTIPS
void MyFrame::OnSetTooltipDelay(wxCommandEvent& event)
{
static long s_delay = 5000;
wxString delay;
delay.Printf("%ld", s_delay);
delay = wxGetTextFromUser("Enter delay (in milliseconds)",
"Set tooltip delay",
delay,
this);
if ( !delay )
return; // cancelled
sscanf(delay, "%ld", &s_delay);
wxToolTip::SetDelay(s_delay);
wxLogStatus(this, "Tooltip delay set to %ld milliseconds", s_delay);
}
void MyFrame::OnToggleTooltips(wxCommandEvent& event)
{
static bool s_enabled = TRUE;
s_enabled = !s_enabled;
wxToolTip::Enable(s_enabled);
wxLogStatus(this, "Tooltips %sabled", s_enabled ? "en" : "dis");
}
#endif // tooltips
void MyFrame::OnSize( wxSizeEvent& event ) void MyFrame::OnSize( wxSizeEvent& event )
{ {
wxString msg; wxString msg;

View File

@@ -69,28 +69,110 @@ inline LRESULT SendTooltipMessage(WXHWND hwnd,
: 0; : 0;
} }
// send a message to all existing tooltip controls
static void SendTooltipMessageToAll(UINT msg, WPARAM wParam, LPARAM lParam)
{
// NB: it might be somewhat easier to maintain a list of all existing
// wxToolTip controls (put them there in ctor, delete from the list
// in dtor) - may be it's worth doing it this way? OTOH, typical
// application won't have many top level windows, so iterating over all
// of them shouldnt' take much time neither...
// iterate over all top level windows and send message to the tooltip
// control of each and every of them (or more precisely to all dialogs and
// frames)
wxDialog *dialog = NULL;
wxFrame *frame = NULL;
wxNode *node = wxTopLevelWindows.First();
while ( node )
{
wxWindow *win = (wxWindow *)node->Data();
node = node->Next();
if ( win->IsKindOf(CLASSINFO(wxFrame)) )
{
frame = (wxFrame *)win;
dialog = NULL;
}
else if ( win->IsKindOf(CLASSINFO(wxDialog)) )
{
dialog = (wxDialog *)win;
frame = NULL;
}
else
{
// skip this strange top level window
continue;
}
wxASSERT_MSG( dialog || frame, "logic error" );
WXHWND hwndTT = frame ? frame->GetToolTipCtrl()
: dialog->GetToolTipCtrl();
if ( hwndTT )
{
(void)SendTooltipMessage(hwndTT, msg, wParam, (void *)lParam);
}
}
}
// ============================================================================ // ============================================================================
// implementation // implementation
// ============================================================================ // ============================================================================
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// "semiglobal" functions - these methods work with the tooltip control which // static functions
// is shared among all the wxToolTips of the same frame
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void wxToolTip::Enable(bool flag)
{
SendTooltipMessageToAll(TTM_ACTIVATE, flag, 0);
}
void wxToolTip::SetDelay(long milliseconds)
{
SendTooltipMessageToAll(TTM_SETDELAYTIME, TTDT_INITIAL, milliseconds);
}
// ---------------------------------------------------------------------------
// implementation helpers
// ---------------------------------------------------------------------------
// create the tooltip ctrl for our parent frame if it doesn't exist yet // create the tooltip ctrl for our parent frame if it doesn't exist yet
WXHWND wxToolTip::GetToolTipCtrl() WXHWND wxToolTip::GetToolTipCtrl()
{ {
// find either parent dialog or parent frame - tooltip controls are managed
// by these 2 classes only (it doesn't make sense to create one tooltip per
// each and every wxWindow)
wxFrame *frame = NULL;
wxDialog *dialog = NULL;
wxWindow *parent = m_window; wxWindow *parent = m_window;
while ( parent && !parent->IsKindOf(CLASSINFO(wxFrame)) ) while ( parent )
{ {
if ( parent->IsKindOf(CLASSINFO(wxFrame)) )
{
frame = (wxFrame *)parent;
break;
}
else if ( parent->IsKindOf(CLASSINFO(wxDialog)) )
{
dialog = (wxDialog *)parent;
break;
}
parent = parent->GetParent(); parent = parent->GetParent();
} }
wxCHECK_MSG( parent, 0, "can't create tooltip control outside a frame" ); wxCHECK_MSG( frame || dialog, 0,
"can't create tooltip control outside a frame or a dialog" );
wxFrame *frame = (wxFrame *)parent; HWND hwndTT = (HWND)(frame ? frame->GetToolTipCtrl()
HWND hwndTT = (HWND)frame->GetToolTipCtrl(); : dialog->GetToolTipCtrl());
if ( !hwndTT ) if ( !hwndTT )
{ {
hwndTT = ::CreateWindow(TOOLTIPS_CLASS, hwndTT = ::CreateWindow(TOOLTIPS_CLASS,
@@ -103,7 +185,10 @@ WXHWND wxToolTip::GetToolTipCtrl()
if ( hwndTT ) if ( hwndTT )
{ {
if ( frame )
frame->SetToolTipCtrl((WXHWND)hwndTT); frame->SetToolTipCtrl((WXHWND)hwndTT);
else
dialog->SetToolTipCtrl((WXHWND)hwndTT);
} }
else else
{ {
@@ -114,22 +199,11 @@ WXHWND wxToolTip::GetToolTipCtrl()
return (WXHWND)hwndTT; return (WXHWND)hwndTT;
} }
void wxToolTip::Enable(bool flag)
{
(void)SendTooltipMessage(GetToolTipCtrl(), TTM_ACTIVATE, flag, 0);
}
void wxToolTip::RelayEvent(WXMSG *msg) void wxToolTip::RelayEvent(WXMSG *msg)
{ {
(void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT, 0, msg); (void)SendTooltipMessage(GetToolTipCtrl(), TTM_RELAYEVENT, 0, msg);
} }
void wxToolTip::SetDelay(long milliseconds)
{
(void)SendTooltipMessage(GetToolTipCtrl(), TTM_SETDELAYTIME,
TTDT_INITIAL, (void *)milliseconds);
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// ctor & dtor // ctor & dtor
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------