How to set wxTextCtrl text centered vertically?

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
freepro
Experienced Solver
Experienced Solver
Posts: 63
Joined: Fri Nov 16, 2012 11:52 am

How to set wxTextCtrl text centered vertically?

Post by freepro »

The wxTextCtrl document have not the method.
ps: Single-line
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to set wxTextCtrl text centered vertically?

Post by doublemax »

Not supported.
Use the source, Luke!
freepro
Experienced Solver
Experienced Solver
Posts: 63
Joined: Fri Nov 16, 2012 11:52 am

Re: How to set wxTextCtrl text centered vertically?

Post by freepro »

doublemax wrote:Not supported.
:( why?
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to set wxTextCtrl text centered vertically?

Post by doublemax »

No idea. Most likely reasons are:
a) The native controls don't support it
b) It wasn't implemented because nobody ever needed it
Use the source, Luke!
freepro
Experienced Solver
Experienced Solver
Posts: 63
Joined: Fri Nov 16, 2012 11:52 am

Re: How to set wxTextCtrl text centered vertically?

Post by freepro »

doublemax wrote:No idea. Most likely reasons are:
a) The native controls don't support it
b) It wasn't implemented because nobody ever needed it
Oh, i just need it.I only makt it narrow. :(
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: How to set wxTextCtrl text centered vertically?

Post by eranon »

Not sure to understand your problem, freepro (maybe a screenshot would improve perception), but maybe this top vertical alignment just happens when you use wxNO_BORDER or wxSTATIC_BORDER (seen in several threads like this one http://forums.wxwidgets.org/viewtopic.p ... 80&p=31067 and this one http://forums.wxwidgets.org/viewtopic.p ... 3&p=101541).
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]
freepro
Experienced Solver
Experienced Solver
Posts: 63
Joined: Fri Nov 16, 2012 11:52 am

Re: How to set wxTextCtrl text centered vertically?

Post by freepro »

eranon wrote:Not sure to understand your problem, freepro (maybe a screenshot would improve perception), but maybe this top vertical alignment just happens when you use wxNO_BORDER or wxSTATIC_BORDER (seen in several threads like this one http://forums.wxwidgets.org/viewtopic.p ... 80&p=31067 and this one http://forums.wxwidgets.org/viewtopic.p ... 3&p=101541).
Yes, my problem is second. but not solution! :(
Manolo
Can't get richer than this
Can't get richer than this
Posts: 827
Joined: Mon Apr 30, 2012 11:07 pm

Re: How to set wxTextCtrl text centered vertically?

Post by Manolo »

I see two solutions:
a) Add a vertical margin to the wxTextCtrl. This does not work for Windows. It does for Gtk+ Also, you must choose the margin depending on the font size.

b) If you don't have borders, you can set the height for the wxTextCtrl same as font pitch (not the font size).

I would test any solution with both upper/lower case letters.
freepro
Experienced Solver
Experienced Solver
Posts: 63
Joined: Fri Nov 16, 2012 11:52 am

Re: How to set wxTextCtrl text centered vertically?

Post by freepro »

Manolo wrote:I see two solutions:
a) Add a vertical margin to the wxTextCtrl. This does not work for Windows. It does for Gtk+ Also, you must choose the margin depending on the font size.

b) If you don't have borders, you can set the height for the wxTextCtrl same as font pitch (not the font size).

I would test any solution with both upper/lower case letters.
Thank you, I to try! :)
freepro
Experienced Solver
Experienced Solver
Posts: 63
Joined: Fri Nov 16, 2012 11:52 am

Re: How to set wxTextCtrl text centered vertically?

Post by freepro »

freepro wrote:
Manolo wrote:I see two solutions:
a) Add a vertical margin to the wxTextCtrl. This does not work for Windows. It does for Gtk+ Also, you must choose the margin depending on the font size.

b) If you don't have borders, you can set the height for the wxTextCtrl same as font pitch (not the font size).

I would test any solution with both upper/lower case letters.
Thank you, I to try! :)
Oh, no solve!
pete_b
Knows some wx things
Knows some wx things
Posts: 41
Joined: Fri Jan 05, 2007 9:52 am

Re: How to set wxTextCtrl text centered vertically?

Post by pete_b »

This is possible on Windows by modifying the non-client area of the control.
To do that, subclasswxTextCtrl and override MSWWindowProc.

Code: Select all

WXLRESULT
TextEdit::MSWWindowProc(WXUINT msg, WXWPARAM wParam, WXLPARAM lParam)
{
	LRESULT r = super::MSWWindowProc(msg, wParam, lParam);
	if (valign_ != 0) {
		// TODO: on non-windows platforms, consider using SetMargins 
		// to acheive vertical alignment.
		if (msg == WM_SIZE) {
			// Ensure we get a WM_NCCALCSIZE message
			SetWindowPos((HWND)GetHWND(), NULL, 0, 0, 0, 0, 
			    SWP_NOOWNERZORDER | SWP_NOSIZE | SWP_NOMOVE | SWP_FRAMECHANGED);
		}
		else if (msg == WM_NCCALCSIZE) {
			// We already called the base wndproc so the ncrect will 
			// take into account any themed border etc.
			auto* lpncsp = (NCCALCSIZE_PARAMS*)lParam;
			auto& ncrect = lpncsp->rgrc[0];
			auto outer_rect = GetRect();

			wxRect client = wxRectFromRECT(ncrect);
			nc_fill_rect_ = client;
			nc_fill_rect_.x -= outer_rect.x;
			nc_fill_rect_.y -= outer_rect.y;
			auto char_height = wxWindowDC(this).GetCharHeight();
			int offset = 0;

			if (valign_ == wxALIGN_CENTER_VERTICAL) {
				wxRect rect = wxSize(1, char_height);
				rect = rect.CenterIn(client, wxVERTICAL);
				offset = rect.y - client.y;
			}
			else {
				offset = client.GetBottom() - char_height;
			}
			nc_fill_rect_.height = offset;

			ncrect.top += offset;
		}
		else if (msg == WM_NCPAINT) {
			// Fill missing area
			wxWindowDC dc(this);
			dc.SetPen(*wxTRANSPARENT_PEN);
			dc.SetBrush(GetBackgroundColour());
			dc.DrawRectangle(nc_fill_rect_);
		}
	}
	return r;
}
There are a couple of member variables

Code: Select all

int valign_
and

Code: Select all

wxRect nc_fill_rect_
that should be obvious enough.
Based upon code found here https://stackoverflow.com/questions/867 ... n-edit-box
Post Reply