Why does this wxRichTextCtrl load files as read-only?

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
katuday
Earned some good credits
Earned some good credits
Posts: 129
Joined: Fri Aug 17, 2012 2:22 am

Why does this wxRichTextCtrl load files as read-only?

Post by katuday » Sun Nov 22, 2020 3:55 am

MSW 3.1.4
Visual Studio 2017
x64 static build

The code below is complete and self contained.
It successfully loads a file ( I tried both .xml and .txt ) into wxRichTextCtrl.
Problem the file loaded is readonly.
What step am I missing?

Code: Select all

#include "wx/wx.h"
#include "wx/richtext/richtextctrl.h"
#include "wx/richtext/richtextxml.h"
enum
{
	Quit = wxID_EXIT, ID_EDITOR
};
class EditorFrame : public wxFrame
{
public:
	EditorFrame(const wxString& title);
	void OnQuit(wxCommandEvent& event);
private:
	wxRichTextCtrl* m_editor;
};
EditorFrame::EditorFrame(const wxString& title)
	: wxFrame(NULL, wxID_ANY, title)
{
	wxMenu *fileMenu = new wxMenu;
	fileMenu->Append(Quit, "E&xit\tAlt-X", "Quit this program");
	wxMenuBar *menuBar = new wxMenuBar();
	menuBar->Append(fileMenu, "&File");
	SetMenuBar(menuBar);
	m_editor = new wxRichTextCtrl(this, ID_EDITOR, wxEmptyString, wxDefaultPosition, wxSize(100, 100), wxRE_MULTILINE);
	
	m_editor->LoadFile("sample.txt");

	Bind(wxEVT_MENU, &EditorFrame::OnQuit, this, Quit);
}
void EditorFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
	Close(true);
}
class EditorApp : public wxApp
{
public:
	virtual bool OnInit() wxOVERRIDE;
};
wxIMPLEMENT_APP(EditorApp);
bool EditorApp::OnInit()
{
	if (!wxApp::OnInit())
		return false;
	wxRichTextBuffer::AddHandler(new wxRichTextXMLHandler);
	wxImage::AddHandler(new wxPNGHandler);
	EditorFrame *frame = new EditorFrame("Editor App");
	frame->Show(true);
	return true;
}

Kvaz1r
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 247
Joined: Tue Jun 07, 2016 1:07 pm

Re: Why does this wxRichTextCtrl load files as read-only?

Post by Kvaz1r » Sun Nov 22, 2020 8:11 am

For some reason it's not working because your ID isn't wxID_ANY. Change to it as workaround. If there won't be answer from more competent users than I - open ticket in bugtracker - http://trac.wxwidgets.org

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2660
Joined: Sun Jan 03, 2010 5:45 pm

Re: Why does this wxRichTextCtrl load files as read-only?

Post by PB » Sun Nov 22, 2020 8:26 am

Is there really a bug?

Due to the incorrect way you defined your IDs, your ID_EDITOR has the same value as wxID_UNDO.

The controls should have IDs outside the range between wxID_LOWEST and wxID_HIGHEST.

EDIT
Actually, there seems to be a bug where the issue happens if the control is created with ID < wxID_HIGHEST + 4 (master, Win10).

EDIT 2
The reason is ID clash, wxRichTextCtrl has

Code: Select all

#define wxID_RICHTEXT_PROPERTIES1   (wxID_HIGHEST + 1)
#define wxID_RICHTEXT_PROPERTIES2   (wxID_HIGHEST + 2)
#define wxID_RICHTEXT_PROPERTIES3   (wxID_HIGHEST + 3) 
I think this is a bug; however, I wonder how it is it was not noticed for so long?

Will you report it Kvaz1r, or should I?

EDIT 3
I have created the ticket:
https://trac.wxwidgets.org/ticket/18983

Post Reply