How to display char buffer in a wxTextCtrl? Topic is solved

This forum can be used to talk about general design strategies, new ideas and questions in general related to wxWidgets. If you feel your questions doesn't fit anywhere, put it here.
Post Reply
eager2no
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat Sep 27, 2008 7:32 pm

How to display char buffer in a wxTextCtrl?

Post by eager2no »

I have a character buffer I want to display in a wxTextCtrl control.

Code: Select all

size_t qq = ResultsFile.Length();
char *rr = (char *)malloc(qq);
ResultsFile.Read(rr, qq);
ID_ResultsBox->Replace(0, qq, ?????); // wants wxString&
How should I do that?

As always, apologies for these lower-than-basic questions...
Thank you.
timg
Earned some good credits
Earned some good credits
Posts: 148
Joined: Mon Jan 23, 2006 6:52 pm

Post by timg »

Code: Select all


ID_ResultsBox->Replace(0, qq, wxString::Format("%s",rr));

DavidHart
Site Admin
Site Admin
Posts: 4252
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Post by DavidHart »

timg wrote:

Code: Select all

ID_ResultsBox->Replace(0, qq, wxString::Format("%s",rr));
That won't work too well in a unicode build. Better would be wxString( rr, wxConvUTF8).

But better still would be avoiding the problem. wxWidgets copes with both ansi and unicode very well, so long as you use wxString; so avoid char* as much as possible. In this case (judging by your earlier post) you are reading data from a wxFFile. So use http://docs.wxwidgets.org/stable/wx_wxf ... ilereadall instead.

Regards,

David
eager2no
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat Sep 27, 2008 7:32 pm

Post by eager2no »

timg,
Thank you, but didn't work:

Code: Select all

error: no matching function for call to `wxString::Format(const char[3], char*&)'
DavidHart,
Thank you, but still something wrong:

Code: Select all

size_t qq = ResultsFile.Length();
wxString *rr = (wxString *)malloc(qq);
ResultsFile.ReadAll(rr, wxConvUTF8);
ID_ResultsBox->Replace(0, qq, rr);

// Last line causes a compile error:
// error: conversion from `wxString*' to `const wxString' is ambiguous
Or I still don't get something...
timg
Earned some good credits
Earned some good credits
Posts: 148
Joined: Mon Jan 23, 2006 6:52 pm

Post by timg »

did you:

#include <wx/string.h>

Also, for David's suggestion, you should do something more like:

Code: Select all

size_t qq = ResultsFile.Length();
wxString rr;
ResultsFile.ReadAll(&rr, wxConvUTF8);
ID_ResultsBox->Replace(0, qq, rr);
eager2no
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat Sep 27, 2008 7:32 pm

Post by eager2no »

timg,
<wx/string.h> is included.

Code: Select all

size_t qq = ResultsFile.Length();
wxString rr;
ResultsFile.ReadAll(&rr, wxConvUTF8);
ID_ResultsBox->Replace(0, qq, rr);
compiles and runs, but ReadAll creates unexpected characters in rr:

Code: Select all

"\0\0wxArrayStrin"
???
and nothing appears in ID_ResultsBox.
ResultsFile contains the text I need, I just can't show it...

Edit:
Forgot to add that displaying more of rr in the watch window (watching e.g. 30 chars in hex in Code::Blocks) triggers a segfault.
timg
Earned some good credits
Earned some good credits
Posts: 148
Joined: Mon Jan 23, 2006 6:52 pm

Post by timg »

I tried this:

Code: Select all

    wxString   filename;
    wxFileDialog	fdlog(this, "Open a file","","","*.*");
    
    if(fdlog.ShowModal() != wxID_OK) return;
    filename = fdlog.GetPath();
    wxFFile    ResultsFile;
    ResultsFile.Open(filename,wxT("r"));

    size_t qq = ResultsFile.Length();
    wxString rr;
    ResultsFile.ReadAll(&rr, wxConvUTF8);
    WxTextCtrl1->Replace(0, qq, rr);
and it worked fine. I don't have wxWidgets compiled for Unicode though, so I can't check that.

Are you sure the file you are trying to read is a text file?
eager2no
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat Sep 27, 2008 7:32 pm

Post by eager2no »

timg,
Thanks for taking the trouble.
Are you sure the file you are trying to read is a text file?
Yes. I generate the file myself. Also, if I read the file into a char buffer, the text shows up fine.
Unicode is enabled for my project (I am using Code::Blocks).
DavidHart
Site Admin
Site Admin
Posts: 4252
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Post by DavidHart »

size_t qq = ResultsFile.Length();
You don't need to do this. A wxString knows its own length, and is better able to cope with unicode. So your 'paste' line could be:
WxTextCtrl1->Replace(0, rr.len(), rr);
(assuming that this is actually what you want to do. Can you be certain that there is enough data already in the textctrl? If not, rr.len() will be past the end of the textctrl's contents, with undefined results (translation: "I don't know what would happen, but it's likely to be messy."). If not, or if there is no data already in the textctrl, you should use wxTextCtrl::SetValue instead. I'm also unsure if using the string length in Replace() is going to cope with different line-endings: 2 bytes in MSWin, 1 in Linux.)
but ReadAll creates unexpected characters in rr
Another possible problem is the wxConvUTF8. This is usually the correct conv to use, but maybe it's wrong for your system.

I expect you'd like to work out what's going wrong with your current code; but just to mention that there are other ways of loading a textctrl: wxTextCtrl::LoadFile for example.
eager2no
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat Sep 27, 2008 7:32 pm

Post by eager2no »

Just tried

Code: Select all

ID_ResultsBox->LoadFile(results_name, wxTEXT_TYPE_ANY);
results_name is declared as

Code: Select all

const wxString results_name = wxT("C:\\Tmp\\__reslts.res");
- Checking results_name contents before calling LoadFile shows that the file does contain what I want.
- The LoadFile call returns true.
- Yet ID_ResultsBox displays nothing :shock:
Drives me crazy. I must be missing something very obvious...
DavidHart
Site Admin
Site Admin
Posts: 4252
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Post by DavidHart »

Drives me crazy. I must be missing something very obvious...
Hmm. The 'text' sample does LoadFile(). Try building that; copy your file into the dir holding the 'text' executable and rename it 'dummy.txt' (that being the name of the file that the sample tries to load).

Then run the sample, choose File > Load File, and see what appears in the top-right textctrl.
eager2no
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat Sep 27, 2008 7:32 pm

Post by eager2no »

What I did instead (building the sampe app is still beyond me...) was this:
1- stopped my app once the results file had been created and closed
2. replaced the contents with plain text contaning no accented characters
And voila, the text displayed fine in the text control 8)
3. Ran the program again, did steps 1 & 2, but added accented characters in the replaced content of the results file -- nothing displayed in the control :shock: :shock: :shock:

By accented characters I mean aacute, eacute, íacute, oacute, uacute, oumlaut, uumlaut, as well as o and u with two strokes on top (hungarumlaut -- I am living in Hungary).

I am running Windows XP Pro English.

Looks like I have a Unicode issue??

Edit: Also, when generating the results file, I use the file handle:

Code: Select all

ResultsFile_fp = ResultsFile.fp();
(Reason is that with my less than limited C++ knowledge, I wanted to migrate the C console version to a Windows app as fast and easy as possible, and writing through the handle was more familiar...)
So files are opened with <filename>.Open(), but written using the handle.
Could this cause the problem with the accented characters?
DavidHart
Site Admin
Site Admin
Posts: 4252
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Post by DavidHart »

I'm not going to be much help to you, I'm afraid; accents aren't common in English. ;)

However I did have a report that my app wouldn't display accented filenames correctly. After experimenting, it turned out that to display them properly, both the app and wxWidgets had to have been built in a utf8 locale.

I don't know if that applies at all to your situation, but I'd check your locale is utf8 and if it's not, consider changing and rebuilding.

Of course it might just be a feature of MSWindows. ;)
eager2no
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat Sep 27, 2008 7:32 pm

Post by eager2no »

David,
Now that it turned out I have problems with Unicode, I re-built the application without Unicode support, and no more display problems :D 8) 8) 8)

You (and others in this excellent forum) have been a lot of help to me.
Also, thank you for bearing with my questions. Not to threaten, but I may be back with more :D

Edit:
Mods,
Instead of clicking Accept on DavidHart's post, I clicked it on my own reply. Please correct my mistake. Thank you.
Post Reply