Overriding OnPaint for wxTextCtrl Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
remi
Earned a small fee
Earned a small fee
Posts: 17
Joined: Fri Nov 25, 2005 10:49 pm

Overriding OnPaint for wxTextCtrl

Post by remi » Sat Mar 28, 2015 7:47 pm

Hi

I would like to change the drawing (including decorations) of wxTextCtrl when it doesn't have the focus. So as a first step, I just wanted to catch all events that repaint the native control and paint in place a simple black rectangle that fill the entire window:

Code: Select all

CU3DCtrlText::CU3DCtrlText(wxWindow * 	parent, wxWindowID 	id, const wxPoint &pos , const wxSize	&size, long style)
	: wxTextCtrl(parent, id, wxEmptyString, pos, size, style)
{
	Bind(wxEVT_PAINT, &CU3DCtrlText::OnPaint, this, -1);
	Bind(wxEVT_ERASE_BACKGROUND, &CU3DCtrlText::OnEraseBackground, this, -1);
	Bind(wxEVT_ENTER_WINDOW, &CU3DCtrlText::OnMouse, this, -1);
	Bind(wxEVT_LEAVE_WINDOW, &CU3DCtrlText::OnMouse, this, -1);
}

void CU3DCtrlText::OnPaint(wxPaintEvent &event)
{
	wxWindowDC dc(this); // use wxWindowDC not wxPaintDc to have the full area of the window (including decorations)
	Render(dc);
}

void CU3DCtrlText::OnMouse(wxMouseEvent& event)
{
	wxWindowDC dc(this);
	Render(dc);
}

void CU3DCtrlText::OnEraseBackground(wxEraseEvent& event)
{
	wxWindowDC dc(this);
	Render(dc);
}

void CU3DCtrlText::Render(wxDC &dc) 
{
	// Black Rectangle that should fill the entire window
	dc.SetPen(*wxBLACK_PEN);
	dc.SetBrush(*wxBLACK_BRUSH);
	dc.DrawRectangle(0,0,GetSize().GetWidth(), GetSize().GetHeight());
}
- When the window is simply repainted, I have a completelly black window (which is what I want)
- When the mouse enter/leave the window, I see a flash that paint the text of the wxTextCtrl (it seems that the base onPaint method is called since i see the default bg color), then the client area of the window is paint in black, but the decorations are still drawn.

I don't understand what is happening, It would perhaps seems that I miss some events that still call the drawing of the base class / decorations, but I am not sure. I would want just no more the decorations, and also avoiding the flash when the mouse enter/leave the window.


Any help would be very much appreciated.

Rémi

User avatar
doublemax
Moderator
Moderator
Posts: 15289
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Overriding OnPaint for wxTextCtrl

Post by doublemax » Sat Mar 28, 2015 8:51 pm

Just forget about this. You can't modify the look of native controls that way (or any other way).
Use the source, Luke!

DenDev
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 223
Joined: Mon Jan 19, 2015 1:45 pm

Re: Overriding OnPaint for wxTextCtrl

Post by DenDev » Sun Mar 29, 2015 11:58 am

The idea of wxWidgets is all about the standard controls, and what you are trying to do makes little sense, but.. What catches my eye is that you do not invalidate the area you want to repaint. Instead you call a draw function (Render) that "breaks" the usual chain of drawing events (invalidate an area, render the invalidated area, validate the rendered area). In the mouse events you should therefore call "Refresh" rather than "Render". Another thing is that you should use the right DC's; Use the wxEraseEvent.GetDC() in the erase handler and a wxPaintDC in the paint handler. I'm not sure about this, but using the wrong DC's might cause the invalidated area not to be validated and this will cause the base paint handler to be called.
I have a bad habbit of not testing the code I post :D

User avatar
eranon
Can't get richer than this
Can't get richer than this
Posts: 867
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: Overriding OnPaint for wxTextCtrl

Post by eranon » Sun Mar 29, 2015 5:12 pm

Hello. Why not simply put your text control on a black panel with the exact same size? When focus goes to the panel, the text control is shown. When the text control looses the focus, it hides itself.
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]

remi
Earned a small fee
Earned a small fee
Posts: 17
Joined: Fri Nov 25, 2005 10:49 pm

Re: Overriding OnPaint for wxTextCtrl

Post by remi » Mon Mar 30, 2015 10:12 am

Thank you for your help guys, I know that wx is intended to use the native controls, but when you have more than 10000 lines made around wx and a release in 2 months, it is not the time to change the windows library :)

The users of my software push me to change its look for something darker since many years. Its a graphic application and the white color of the main theme seems to bother them, the aim is to have the main frame that have an average grey around 40%, if you're interrested, this is the current look: Image. I easily remade some controls around wxControl, like check boxes, buttons and toggle buttons for the main frame, but for text controls, the work for coding a custom one is far beyond...

DenEnv, Eranon, yes, I will try a bit using your tips, and if it don't work or become to much complicated I will replace the text control by a static text at focus changes. Thank you.

If any one has experience on working on wx look it is welcomed. Also, I am providing OSx and Linux version of that app, so if you think that I should switch to wxUniversal please let me know.

cheers

User avatar
doublemax
Moderator
Moderator
Posts: 15289
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Overriding OnPaint for wxTextCtrl

Post by doublemax » Mon Mar 30, 2015 1:36 pm

Completely switching to wxUniversal is probably not an option. The wxUniversal port is not complete and mainly untested under the main platforms, i'm almost certain this would create more problems than it solves.

Maybe you could try to take the wxTextCtrl implementation from wxUniversal and add it to your code. This at least contains all the logic you need for a text control, but it's probably still a lot of work to get rid of all dependencies that the code needs, especially the renderer.
Use the source, Luke!

remi
Earned a small fee
Earned a small fee
Posts: 17
Joined: Fri Nov 25, 2005 10:49 pm

Re: Overriding OnPaint for wxTextCtrl

Post by remi » Wed Apr 01, 2015 10:02 am

Thats good to know. Thanks

Finally, for the wxTextCtrl, I am using wxBORDER_NONE, so I simply remove the decoration to the control. And thats ok.

Post Reply