0
votes

I am trying to show a string on the screen while the user still types it. Normally wxDC's DrawText function is used for a string where the contents of the string is already known.

void DrawText(const wxString& text, wxCoord x, wxCoord y) 

As we know, this function takes a wxString and prints on the screen. However, how can I let the user type something on the screen (on a wxWindow, wxFrame ). All and all, I am working nowadays on a drawing program where users can draw certain shapes. Certain shapes are easy to draw, move etc... But having a textbox and letting user type something in it is bugging my mind so far. Of course, this should be editable such as when the user clicks again on the same "text box" I should be able to edit it.

One last point, I am trying to avoid using controls such as wxTextCtrl and in a sense trying to make my own textctrl. What would be the best starting point?

Any ideas would be appreciated.

2

2 Answers

1
votes

Writing your own wxTextCtrl is difficult, I'd strongly recommend against doing this. But if you really want to (why?), then it's, of course, possible, after all wxRichTextCtrl which has significantly more features than the plain wxTextCtrl is written using only wxWidgets API.

And there is no particular magic here, you just need to update the window (ideally, use wxWindow::RefreshRect() instead of refreshing the entire window) whenever the text displayed in it changes and your wxEVT_PAINT handler must be capable of drawing the text correctly.

0
votes

I don't think you need to do anything special here. As long as the user is in some kind of "edit" mode - i.e. you're expecting them to type text - then all you need to do is to redraw everything whenever the user enters more text.

Something like this:

  1. User clicks inside a rect to edit text.
  2. Program starts edit mode (some internal state initialises an empty string).
  3. While user didn't click outside the rect
    • add the entered character to the text string
    • clear the drawing area inside the rect to background
    • call DrawText or more likely DrawLabel to draw the string in the rect

Then the text is redrawn as it changes and you have a string with the content for persistence if necessary.

Alternatively, you can just pop up a wxDialog with a wxTextCtrl to ask the user for the whole text string and then draw after the dialog is closed.

Makes sense?