Wx::TextCtrl

From WxPerl wiki

Jump to: navigation, search

Contents

Wx::TextCtrl

A text control allows text to be displayed and edited. It may be single line or multi-line.


Window styles constants

  • wxTE_PROCESS_ENTER The control will generate the message wxEVENT_TYPE_TEXT_ENTER_COMMAND (otherwise pressing <Enter> is either processed internally by the control or used for navigation between dialog controls).
  • wxTE_PROCESS_TAB The control will receive EVT_CHAR messages for TAB pressed - normally, TAB is used for passing to the next control in a dialog instead. For the control created with this style, you can still use Ctrl-Enter to pass to the next control from the keyboard.
  • wxTE_MULTILINE The text control allows multiple lines.
  • wxTE_PASSWORD The text will be echoed as asterisks.
  • wxTE_READONLY The text will not be user-editable.
  • wxTE_RICH Use rich text control under Win32, this allows to have more than 64Kb of text in the control even under Win9x. This style is ignored under other platforms.
  • wxTE_RICH2 Use rich text control version 2.0 or 3.0 under Win32, this style is ignored under other platforms
  • wxTE_AUTO_URL Highlight the URLs and generate the wxTextUrlEvents when mouse events occur over them. This style is supported under Win32 only and requires wxTE_RICH.
  • wxTE_NOHIDESEL By default, the Windows text control doesn't show the selection when it doesn't have focus - use this style to force it to always show it. It doesn't do anything under other platforms.
  • wxHSCROLL A horizontal scrollbar will be created. No effect under GTK+.
  • wxTE_LEFT The text control will be left-justified (default).
  • wxTE_CENTRE The text control will be centre-justified.
  • wxTE_RIGHT The text control will be right-justified.
  • wxTE_DONTWRAP Same as wxHSCROLL style.
  • wxTE_LINEWRAP Wrap the lines too long to be shown entirely at any position (wxUniv only currently).
  • wxTE_WORDWRAP Wrap the lines too long to be shown entirely at word boundaries only (wxUniv only currently).

new($parent, $id, $value, [$x,$y], [$w,$h], windowStyle, $validator, $name)

Creates a new Wx::TextCtrl

This are the possible parameters:

  • $parent - Parent window. Should not be NULL.
  • $id - Control identifier. A value of -1 denotes a default value.
  • $value - Default text value.
  • [$x,$y] - Desired position of text control. Either use an array reference or the wxDefaultPosition constant.
  • [$w,$h] - Desired size, width and height, of text control. Eitehr use an array reference or the wxDefaultSize constant.
  • windowStyle - see the /*Window styles constants*/ section for possible constants
  • $validator - If you know more about that please edit
  • $name - Window name - if you know more about that please edit
my $textctrl = Wx::TextCtrl->new(
                   $parent, 
                   -1,
                   "Sample text", 
                   wxDefaultPosition, 
                   [300,300], 
                   wxTE_MULTILINE|wxHSCROLL 
                  );

AppendText($text)

Appends the given text to the end of the text control.

$textctrl->AppendText($text); 

GetValue()

Gets the contents of the control. Notice that for a multiline text control, the lines will be separated by (Unix-style) \n characters, even under Windows where they are separated by a \r\n sequence in the native control. (As you are using Perl you won't have to worry about \n and \r\n, but in some cases it is good to know that)

$sometext = $textctrl->GetValue(); 

SetStyle($start, $end, $style)

Changes the style of the selection. If either of the font, foreground, or background colour is not set in style, the values of GetDefaultStyle() are used.

Parameters:

  • $start - Start of selection
  • $end - End of selection
  • $style - Is a WxTextAttr object

Return value TRUE on success, FALSE if an error occured - may also mean that the styles are not supported under this platform.

Sample:

# set a demo value
$textctrl->SetValue("red and blue");

# set the font color of "red"
$textctrl->SetStyle( 
                     0,2,
                     Wx::TextAttr->new(
                                         Wx::Colour->new('red'),
                                      )
                   );

# set the font color of "blue"
$textctrl->SetStyle(
                     7,11,
                     Wx::TextAttr->new(
                                         Wx::Colour->new('blue'),
                                      )
                   );

You should definitely take a loog at WxTextAttr and WxColour, to get the most out of the /SetStyle()/

Personal tools
Google AdSense