wxRichTextCtrl Text Not Centering 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
User avatar
Deluge
Earned some good credits
Earned some good credits
Posts: 122
Joined: Fri Apr 30, 2010 4:52 am
Location: USA
Contact:

wxRichTextCtrl Text Not Centering

Post by Deluge »

I'm trying to center justify the text of a wxRichTextCtrl. The docs say to use wxRichTextCtrl::StartAlignment(wxTEXT_ALIGNMENT_CENTER), but it's not working:

Code: Select all

#include <wx/wx.h>
#include <wx/richtext/richtextctrl.h>

class Window : public wxFrame {
public:
	Window();
private:
	wxRichTextCtrl* te;
};

Window::Window() :
		wxFrame(NULL, -1, "Rich Text") {
	te = new wxRichTextCtrl(this, -1, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxRE_READONLY);
	te->BeginAlignment(wxTEXT_ALIGNMENT_CENTER); // centering text not working
	te->WriteText("Hello rich text!");
}

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

wxIMPLEMENT_APP(App);

bool App::OnInit() {
	Window* frame = new Window();
	SetTopWindow(frame);
	frame->Show(true);

	return true;
}
This is the result:
rich_text_center_justify_fail.png
rich_text_center_justify_fail.png (2.37 KiB) Viewed 1903 times

What am I doing wrong?
Projects:
Debreate
MyABCs
Stendhal

OSes:
Windows 10 Home (missing my Linux & Freebsd :()
Kvaz1r
Super wx Problem Solver
Super wx Problem Solver
Posts: 357
Joined: Tue Jun 07, 2016 1:07 pm

Re: wxRichTextCtrl Text Not Centering

Post by Kvaz1r »

It seems like a bug (alignment don't applied to one-line text) so I would create new ticket on https://trac.wxwidgets.org.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxRichTextCtrl Text Not Centering

Post by doublemax »

You need a matching EndAlignment() call.
Use the source, Luke!
Kvaz1r
Super wx Problem Solver
Super wx Problem Solver
Posts: 357
Joined: Tue Jun 07, 2016 1:07 pm

Re: wxRichTextCtrl Text Not Centering

Post by Kvaz1r »

doublemax wrote: Sun Jul 14, 2019 7:45 am You need a matching EndAlignment() call.
It didn't work with this changes either. But if called Newline/0 style works as expected.
User avatar
Deluge
Earned some good credits
Earned some good credits
Posts: 122
Joined: Fri Apr 30, 2010 4:52 am
Location: USA
Contact:

Re: wxRichTextCtrl Text Not Centering

Post by Deluge »

Kvaz1r wrote: Sun Jul 14, 2019 8:06 am
doublemax wrote: Sun Jul 14, 2019 7:45 am You need a matching EndAlignment() call.
It didn't work with this changes either. But if called Newline/0 style works as expected.
Adding a new line after the text worked for me. Thank you Kvaz1r.

Code: Select all

te->WriteText("Hello rich text!\n");
or

Code: Select all

te->WriteText("Hello rich text!");
te->Newline();
--- Edit ---

This is what my actual code looked like:

Code: Select all

	textarea->BeginAlignment(wxTEXT_ALIGNMENT_CENTER);
	textarea->BeginFontSize(14);
	textarea->BeginBold();
	textarea->WriteText("HOW TO PLAY");
	textarea->EndAllStyles();

	textarea->BeginBold();
	textarea->BeginUnderline();
	textarea->WriteText("\n\nSelecting a category:\n");
So I changed it to this:

Code: Select all

	textarea->BeginAlignment(wxTEXT_ALIGNMENT_CENTER);
	textarea->BeginFontSize(14);
	textarea->BeginBold();
	textarea->WriteText("HOW TO PLAY\n");
	textarea->EndAllStyles();

	textarea->BeginBold();
	textarea->BeginUnderline();
	textarea->WriteText("\nSelecting a category:\n");
It seems that calling EndAllStyles or EndAlignment before a newline removes the alignment for the current line. Which actually makes sense. But it makes it look like a bug when there is only a single line without calling one of those.

--- Edit ---

Alignment is not applied to final line if not followed by a newline character:

Code: Select all

Window::Window() :
		wxFrame(NULL, -1, "Rich Text") {
	te = new wxRichTextCtrl(this, -1, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxRE_READONLY);
	te->BeginAlignment(wxTEXT_ALIGNMENT_CENTER); // centering text not working
	te->WriteText("Hello rich text!\n");
	te->WriteText("Goodbye rich text");
}
Result:
rich_text_center_justify2.png
rich_text_center_justify2.png (2.78 KiB) Viewed 1872 times
Projects:
Debreate
MyABCs
Stendhal

OSes:
Windows 10 Home (missing my Linux & Freebsd :()
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxRichTextCtrl Text Not Centering

Post by ONEEYEMAN »

Hi,
It probably is a bug, because if you didn't close the alignment tag, after pressing Enter cursor needs to be moved to the middle of the control.

I'd create a ticket on trac.wxwidgets.org to trac it down.

Thank you.
Kvaz1r
Super wx Problem Solver
Super wx Problem Solver
Posts: 357
Joined: Tue Jun 07, 2016 1:07 pm

Re: wxRichTextCtrl Text Not Centering

Post by Kvaz1r »

I've opened a new ticket - https://trac.wxwidgets.org/ticket/18464
Post Reply