Page 1 of 1

wxRichTextCtrl Text Not Centering

Posted: Sun Jul 14, 2019 1:15 am
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 1943 times

What am I doing wrong?

Re: wxRichTextCtrl Text Not Centering

Posted: Sun Jul 14, 2019 7:41 am
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.

Re: wxRichTextCtrl Text Not Centering

Posted: Sun Jul 14, 2019 7:45 am
by doublemax
You need a matching EndAlignment() call.

Re: wxRichTextCtrl Text Not Centering

Posted: Sun Jul 14, 2019 8:06 am
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.

Re: wxRichTextCtrl Text Not Centering

Posted: Sun Jul 14, 2019 9:32 am
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 1912 times

Re: wxRichTextCtrl Text Not Centering

Posted: Mon Jul 15, 2019 2:43 pm
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.

Re: wxRichTextCtrl Text Not Centering

Posted: Sun Aug 11, 2019 8:00 am
by Kvaz1r
I've opened a new ticket - https://trac.wxwidgets.org/ticket/18464