From: Ian Gotenks on
Hi,

Below is a minimal.cpp program (with removed comments),
which has wxTextCtrl added. This wxTextCtrl captures
mouse, but unfortunately it looses it when it is clicked
(just clik the control twice and see a StatusBar).
Is this an expected behavior ? It happens under wxMSW
(not tested on other platorms).

--

gotenks


/////////////////////////////////////////////////////////////
// Name: minimal.cpp
// Purpose: Minimal wxWidgets sample
// Author: Julian Smart
// Modified by:
// Created: 04/01/98
// RCS-ID: $Id: minimal.cpp 43915 2006-12-11 09:33:34Z CE $
// Copyright: (c) Julian Smart
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////

#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#if !defined(__WXMSW__) && !defined(__WXPM__)
#include "../sample.xpm"
#endif

class MyApp : public wxApp
{
public:
virtual bool OnInit();
};

class MyFrame : public wxFrame
{
public:
MyFrame(const wxString& title);

void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);

private:
DECLARE_EVENT_TABLE()
};

class MyTextCtrl : public wxTextCtrl
{
public:
MyTextCtrl(wxWindow* parent, const wxString& value)
: wxTextCtrl(parent, wxID_ANY, value) {};

void OnMouse(wxMouseEvent& event);
void OnCaptureLost(wxMouseCaptureLostEvent& event);

private:
DECLARE_EVENT_TABLE()
};

enum {
Minimal_Quit = wxID_EXIT,
Minimal_About = wxID_ABOUT
};

BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
EVT_MENU(Minimal_About, MyFrame::OnAbout)
END_EVENT_TABLE()

BEGIN_EVENT_TABLE(MyTextCtrl, wxTextCtrl)
EVT_LEFT_DOWN(MyTextCtrl::OnMouse)
EVT_MOUSE_CAPTURE_LOST(MyTextCtrl::OnCaptureLost)
END_EVENT_TABLE()

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{
if ( !wxApp::OnInit() )
return false;
MyFrame *frame = new MyFrame(_T("Minimal wxWidgets App"));
frame->Show(true);
return true;
}

MyFrame::MyFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title)
{
SetIcon(wxICON(sample));

#if wxUSE_MENUS
wxMenu *fileMenu = new wxMenu;

wxMenu *helpMenu = new wxMenu;
helpMenu->Append(Minimal_About, _T("&About...\tF1"), _T("Show about
dialog"));

fileMenu->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this
program"));

wxMenuBar *menuBar = new wxMenuBar();
menuBar->Append(fileMenu, _T("&File"));
menuBar->Append(helpMenu, _T("&Help"));

SetMenuBar(menuBar);

MyTextCtrl *myTextCtrl = new MyTextCtrl(this, "This is a TextCtrl,
which looses its CaptureMouse unexpectadly");
myTextCtrl->CaptureMouse();

#endif // wxUSE_MENUS

#if wxUSE_STATUSBAR
CreateStatusBar(2);
SetStatusText(_T("Welcome to wxWidgets!"));
#endif // wxUSE_STATUSBAR
}

void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Close(true);
}

void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox(wxString::Format(
_T("Welcome to %s!\n")
_T("\n")
_T("This is the minimal wxWidgets sample\n")
_T("running under %s."),
wxVERSION_STRING,
wxGetOsDescription().c_str()
),
_T("About wxWidgets minimal sample"),
wxOK | wxICON_INFORMATION,
this);
}

void MyTextCtrl::OnMouse(wxMouseEvent& event) {
if( !HasCapture() )
wxLogStatus("CaptureMouse LOST !");
event.Skip();
}

void MyTextCtrl::OnCaptureLost(wxMouseCaptureLostEvent& event) {
wxLogStatus("Unexpected OnCaptureLost called");
}

---------------------------------------------------------------------
To unsubscribe, e-mail: wx-users-unsubscribe(a)lists.wxwidgets.org
For additional commands, e-mail: wx-users-help(a)lists.wxwidgets.org

From: Vadim Zeitlin on
On Fri, 28 Dec 2007 12:21:55 +0100 Ian Gotenks <gotenks(a)poczta.gazeta.pl> wrote:

IG> Below is a minimal.cpp program (with removed comments),
IG> which has wxTextCtrl added. This wxTextCtrl captures
IG> mouse, but unfortunately it looses it when it is clicked
IG> (just clik the control twice and see a StatusBar).
IG> Is this an expected behavior ?

It is if you think about how the native control works: it needs to capture
the mouse to allow extending selection by dragging the mouse pointer
(possibly outside of the text control area). If you need to prevent this
from happening you shouldn't call event.Skip() in your OnMouse() handler.
But then the selection in the control won't work of course.

Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: wx-users-unsubscribe(a)lists.wxwidgets.org
For additional commands, e-mail: wx-users-help(a)lists.wxwidgets.org