A simple Text Editor

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
wxNewbi
Knows some wx things
Knows some wx things
Posts: 26
Joined: Mon Nov 15, 2004 11:27 pm
Location: London UK
Contact:

A simple Text Editor

Post by wxNewbi » Wed Dec 29, 2004 9:41 pm

Hello guys, I am trying to write an editor, nothing fancy. Just want it to be able to open a text file for now but for some reason my derivate version of the wxTextCtrl don't work. Basical I have a menu bar that allows the user to open, close and save files. For opening files I tried using the LoadFile function like this

Code: Select all

void CEditFrame::OnOpenFile(wxCommandEvent&)
{
	wxString Data;
	int len = 0, Readlen = 0;
	wxFileDialog *fileDialog = new wxFileDialog(this, 
							_("select file to open"),_(""), _(""),
							_("text files (*.txt)|*.txt|All files (*.*)|*.*"));
	fileDialog->ShowModal();
        TextBox->LoadFile(fileDialog->GetPath());
}
this does nothing but in my terminal in linux I get this

(Cedit:3588): Gtk-CRITICAL **: file gtktextbuffer.c: line 557 (gtk_text_buffer_emit_insert): assertion `g_utf8_validate (text, len, NULL)' failed


So I tried a different approach. I decide to use the wxFile type to load the file into a wxString and then use SetValue to load it into my wxTextCtrl. The code is give below

Code: Select all

void CEditFrame::OnOpenFile(wxCommandEvent&)
{
	wxString Data;
	int len = 0, Readlen = 0;
	wxFileDialog *fileDialog = new wxFileDialog(this, 
							_("select file to open"),_(""), _(""),
							_("text files (*.txt)|*.txt|All files (*.*)|*.*"));
	fileDialog->ShowModal();
	wxFile *newFile = new wxFile(fileDialog->GetPath().c_str());
	if( newFile->IsOpened())
	{
		len = newFile->Length();
		Data.Alloc(len);
		Readlen = newFile->Read((void*)&Data,len);
		if (Readlen > 0)
		{
			TextBox->SetValue(Data);
		}
		else
		{
			wxMessageDialog *msg1 = new wxMessageDialog(this, _("File Error"),
													_("Can't Open File"));
			msg1->ShowModal();
		}
	}
	else
	{
		wxMessageDialog *msg = new wxMessageDialog(this, _("File Error"),
													_("Can't Open File"));
		msg->ShowModal();
	}
}
This promptly generates a segmentation fault when it gets to "TextBox->SetValue(Data);". Does anyone have any idea what I am doing wrong.

Thanks
Keep it real, Keep it free, Keep it GPL

eco
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 203
Joined: Tue Aug 31, 2004 7:06 pm
Location: Behind a can of Mountain Dew
Contact:

Post by eco » Thu Dec 30, 2004 12:55 am

If you are using wxWidgets 2.4.2 try upgrading to 2.5.3. Looking through the mailing list, it seemed to have solved another person's problems. It appears another solution would be to disable unicode but if you are using the GTK2 version of wxWidgets, that is not really an option (and it's not really a good solution in the first place).

wxNewbi
Knows some wx things
Knows some wx things
Posts: 26
Joined: Mon Nov 15, 2004 11:27 pm
Location: London UK
Contact:

Post by wxNewbi » Thu Dec 30, 2004 3:02 am

thanks eco, I am using a GTK2 version and I am already in version 2.5.3 of wxWidget. but I still can't solve the problem. I don't really want ti disable UNICODE and I have update my GTK2 lib to version 2.6.0. Why the Hell doesn't it work :( ?????
Keep it real, Keep it free, Keep it GPL

wxNewbi
Knows some wx things
Knows some wx things
Posts: 26
Joined: Mon Nov 15, 2004 11:27 pm
Location: London UK
Contact:

Post by wxNewbi » Thu Dec 30, 2004 2:23 pm

It's Programming amazing, after a few hours of causing, sleeping, programming, sleeping, research and more programming and causing, I finally solved it :D I am so the man :D . problem was to do with UNICODE ( the most demanding of all masters :D ) Any way all I had to to what read the file into a char buffer and then convert that to UNICODE using the provided function and it worked like a charm :P . So join me everone ans do the victory dance :D . Code give below.

Code: Select all

void CEditFrame::OnOpenFile(wxCommandEvent& event)
{
	char* afile;
	wxChar* buffer;
	wxMBConvUTF8 AsciiToUinCode;
	int len = 0, Readlen = 0;
	wxFileDialog *fileDialog = new wxFileDialog(this, 
							_("select file to open"),_(""), _(""),
							_("text files (*.txt)|*.txt|All files (*.*)|*.*"));
	fileDialog->ShowModal();
	wxFile *newFile = new wxFile(fileDialog->GetPath().c_str());
	if( newFile->IsOpened())
	{
		len = newFile->Length();
		afile = new char[len]; 
		buffer = new wxChar[len];
		Readlen = newFile->Read(afile,len);
		if (Readlen > 0)
		{
			AsciiToUinCode.MB2WC(buffer,afile,len);
			wxString Data(buffer,len);
			TextBox->SetValue(Data);
		}
		else
		{
			wxMessageDialog *msg1 = new wxMessageDialog(this, _("File Error"),
													_("Can't Open File"));
			msg1->ShowModal();
		}
	}
	else
	{
		wxMessageDialog *msg = new wxMessageDialog(this, _("File Error"),
													_("Can't Open File"));
		msg->ShowModal();
	}
	
	// setup editor Title
	wxString title = m_Title;
	title +=_(" - ") + fileDialog->GetFilename();
	this->SetTitle(title);
}
Thanks a lot guys I can finally leave my computer for a little while. I know it's a small victory but I ever little counts right :wink:
Keep it real, Keep it free, Keep it GPL

wxNewbi
Knows some wx things
Knows some wx things
Posts: 26
Joined: Mon Nov 15, 2004 11:27 pm
Location: London UK
Contact:

Post by wxNewbi » Mon Jan 03, 2005 1:12 am

I have another problem. I am trying to use the UNDO facility of the wxTextCtrl but I can't get it to work, when I delete something from the text in the window and press control z it doesn't undo what I did. Thi s is the same thing that happens when I typing text into the window. I can copy and paste but not undo. the Wx manual says the wxUNDO_ID event is handled internally by the control. So why don't it work? Do I have to enable it? if so How?

Also if I want my editor to be able to open a file of >1Mb, how will I do it ?

thanks
Keep it real, Keep it free, Keep it GPL

leio
Can't get richer than this
Can't get richer than this
Posts: 802
Joined: Mon Dec 27, 2004 10:46 am
Location: Estonia, Tallinn
Contact:

Post by leio » Wed Jan 05, 2005 12:27 pm

wxTextCtrl in wxGTK must be entered UTF-8 encoding internally. If LoadFile doesn't do it then it might be a bug. I'll look into it at one point as I have made wxTextCtrl in wxGTK one of my primary concerns.

wxGTK2 can very well be compiled without unicode, it is a perfectly legal option and not suggested for the same reason it isn't for all the other ports. Text is converted into UTF-8 regardless of this setting. I have a huge application running only in non-unicode and I have no problems compiling and using it with wxGTK non-unicode.

wxGTK's wxTextCtrl implementation does not have an undo manager, hence it can't work.
Implementing it is in my TODO list.

I fully agree that for wxGTK 2.5.3 or newer should be used and not 2.4.2.
Compilers: gcc-3.3.6, gcc-3.4.5, gcc-4.0.2, gcc-4.1.0 and MSVC6
OS's: Gentoo Linux, WinXP; WX: CVS HEAD

Project Manager of wxMUD - http://wxmud.sf.net/
Developer of wxGTK;
gtk+ port maintainer of OMGUI - http://www.omgui.org/

wxNewbi
Knows some wx things
Knows some wx things
Posts: 26
Joined: Mon Nov 15, 2004 11:27 pm
Location: London UK
Contact:

Post by wxNewbi » Wed Jan 05, 2005 4:31 pm

Thanks leio,

I am having problems with unicode on wxGTK. I am using the latest Gtk2 (2.6 last time I built the source). I am trying to display the line number in my status bar, but for some reason after converting the line and column number to strings from the wxTextCtrl::PositionToXY function, I can't send that string to the status bar, pango complains of an invalid UTF8 data. I have tried converting the string to UTF8 data using UnicodeToUTF8::MC2WC but that gives the same problem.

I think I will recompile gtk and wx without unicode and see if that is any better.

Thanks again
Keep it real, Keep it free, Keep it GPL

leio
Can't get richer than this
Can't get richer than this
Posts: 802
Joined: Mon Dec 27, 2004 10:46 am
Location: Estonia, Tallinn
Contact:

Post by leio » Thu Jan 06, 2005 4:10 am

wxGTK actually compiles and works with gtk+-2.6.x?
I haven't heard anyone try it yet and I was to at one point.
That's good news then.
Compilers: gcc-3.3.6, gcc-3.4.5, gcc-4.0.2, gcc-4.1.0 and MSVC6
OS's: Gentoo Linux, WinXP; WX: CVS HEAD

Project Manager of wxMUD - http://wxmud.sf.net/
Developer of wxGTK;
gtk+ port maintainer of OMGUI - http://www.omgui.org/

Post Reply