nux-1.16.0
nux::TextEntry Class Reference
Inheritance diagram for nux::TextEntry:
nux::View nux::InputArea nux::Area nux::InitiallyUnownedObject nux::Focusable nux::Object nux::Trackable nux::Introspectable

List of all members.

Public Member Functions

 TextEntry (const TCHAR *text, NUX_FILE_LINE_PROTO)
AreaFindAreaUnderMouse (const Point &mouse_position, NuxEventType event_type)
virtual long ProcessEvent (IEvent &ievent, long TraverseInfo, long ProcessEventInfo)
virtual void Draw (GraphicsEngine &GfxContext, bool force_draw)
virtual void DrawContent (GraphicsEngine &GfxContext, bool force_draw)
virtual void PostDraw (GraphicsEngine &GfxContext, bool force_draw)
void PreLayoutManagement ()
long PostLayoutManagement (long layoutResult)
void RecvMouseDoubleClick (int x, int y, unsigned long button_flags, unsigned long key_flags)
void RecvMouseUp (int x, int y, unsigned long button_flags, unsigned long key_flags)
void RecvMouseDown (int x, int y, unsigned long button_flags, unsigned long key_flags)
void RecvMouseDrag (int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags)
void RecvKeyEvent (unsigned long eventType, unsigned long keysym, unsigned long state, const TCHAR *character, unsigned short keyCount)
void RecvStartKeyFocus ()
void RecvEndKeyFocus ()
void MainDraw ()
void ProcessMouseEvent (int event_type, int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags)
void ProcessKeyEvent (unsigned long eventType, unsigned long keysym, unsigned long state, const TCHAR *character, unsigned short keyCount)
void FocusInx ()
void FocusOutx ()
void SetText (const char *text)
std::string const & GetText () const
void SetTextColor (const Color &color)
Color const & GetTextColor () const
void SetFontFamily (const char *font)
void SetFontSize (double font_size)
void SetFontOptions (const cairo_font_options_t *options)
void Select (int start, int end)
 Select text between start and end.
void SelectAll ()
 Select all text.
CairoGraphics::Alignment GetAlign () const
void SetAlign (CairoGraphics::Alignment align)

Public Attributes

bool _size_match_text
BaseTexture_texture2D
sigc::signal< void, TextEntry * > sigTextChanged
 Text changed signal.
sigc::signal< void > activated
sigc::signal< void, int > cursor_moved

Protected Types

enum  MovementStep {
  VISUALLY, WORDS, DISPLAY_LINES, DISPLAY_LINE_ENDS,
  PAGES, BUFFER
}
 Enum used to specify different motion types.

Protected Member Functions

virtual void DoSetFocused (bool focused)
virtual void GeometryChanged ()
void QueueTextDraw ()
void ResetLayout ()
 Remove the cached layout.
PangoLayout * EnsureLayout ()
 Create pango layout on-demand.
PangoLayout * CreateLayout ()
 Create a new layout containning current edit content.
CairoGraphicsEnsureCanvas ()
 Create cairo canvas on-demand.
void AdjustScroll ()
 Adjust the scroll information.
void QueueRefresh (bool relayout, bool adjust_scroll)
 Send out a request to refresh all informations of the edit control and queue a draw request.
void ResetImContext ()
 Reset the input method context.
void ResetPreedit ()
 Reset preedit text.
void QueueCursorBlink ()
 Send out a request to blink the cursor if necessary.
void ShowCursor ()
void HideCursor ()
void DrawCursor (CairoGraphics *canvas)
 Draw the Cursor to the canvas.
void DrawText (CairoGraphics *canvas)
 Draw the text to the canvas.
void GetCursorRects (Rect *strong, Rect *weak)
void UpdateCursorRegion ()
void UpdateSelectionRegion ()
void MoveCursor (MovementStep step, int count, bool extend_selection)
 Move cursor.
int MoveVisually (int current_pos, int count)
 Move cursor visually, meaning left or right.
int MoveWords (int current_pos, int count)
 Move cursor in words.
int MoveDisplayLines (int current_pos, int count)
 Move cursor in display lines.
int MovePages (int current_pos, int count)
 Move cursor in pages.
int MoveLineEnds (int current_pos, int count)
 Move cursor to the beginning or end of a display line.
void SetCursor (int cursor)
 Set the current cursor offset, in number of characters.
int XYToTextIndex (int x, int y)
 Get the most reasonable character offset according to the pixel coordinate in the layout.
bool GetSelectionBounds (int *start, int *end)
 Get the offset range that is currently selected,in number of characters.
void SetSelectionBounds (int selection_bound, int cursor)
 Set the offest range that should be selected, in number of characters.
int TextIndexToLayoutIndex (int text_index, bool consider_preedit_cursor)
 Convert index in text_ into index in layout text.
int LayoutIndexToTextIndex (int layout_index)
 Convert index in layout text into index in text_.
int GetCharLength (int index)
 Get char length at index, in number of bytes.
int GetPrevCharLength (int index)
 Get previous char length before index, in number of bytes.
void EnterText (const char *str)
 Insert text at current caret position.
void DeleteText (int start, int end)
 Delete text in a specified range, in number of characters.
void SelectWord ()
 Select the current word under cursor.
void SelectLine ()
 Select the current display line under cursor.
void DeleteSelection ()
 Delete the text that is currently selected.
void CutClipboard ()
 Cut the current selected text to the clipboard.
void CopyClipboard ()
 Copy the current selected text to the clipboard.
void PasteClipboard ()
 Paste the text in the clipboard to current offset.
void BackSpace (MovementStep step)
 Delete a character before the offset of the cursor.
void Delete (MovementStep step)
 Delete a character at the offset of the cursor.
void ToggleOverwrite ()
 Switch between the overwrite mode and the insert mode.
Color GetSelectionBackgroundColor ()
 Gets the color of selection background.
Color GetSelectionTextColor ()
 Gets the color of selection text.
void GetCursorLocationInLayout (int *strong_x, int *strong_y, int *strong_height, int *weak_x, int *weak_y, int *weak_height)
 Gets the cursor location in pango layout.
virtual bool InspectKeyEvent (unsigned int eventType, unsigned int keysym, const char *character)
 Return True if the the area knows what to do with the key event.

Static Protected Member Functions

static bool CursorBlinkCallback (TextEntry *data)

Protected Attributes

bool _block_focus
CairoGraphicscanvas_
 The CairoCanvas which hold cairo_t inside.
PangoLayout * cached_layout_
 The cached Pango Layout.
std::string _text
 The text content of the edit control.
std::string _preedit
 The preedit text of the edit control.
PangoAttrList * preedit_attrs_
 Attribute list of the preedit text.
std::string password_char_
 The character that should be displayed in invisible mode.
t_u64 last_dblclick_time_
 Last time of mouse double click event.
int cursor_
 The current cursor position in number of bytes.
int preedit_cursor_
 The preedit cursor position within the preedit string, in number of bytes.
int selection_bound_
 The current selection bound in number of bytes, range between cursor_ and selection_bound_ are selected.
int scroll_offset_x_
 X offset of current scroll, in pixels.
int scroll_offset_y_
 Y offset of current scroll, in pixels.
int cursor_blink_timer_
 Timer id of cursor blink callback.
int cursor_blink_status_
 Indicates the status of cursor blinking, 0 means hide cursor otherwise means show cursor.
bool visible_
 Whether the text is visible, decided by password_char_.
bool focused_
 Whether the edit control is focused.
bool need_im_reset_
 Whether the input method should be reset.
bool overwrite_
 Whether the keyboard in overwrite mode.
bool select_words_
 Whether the button click should select words.
bool select_lines_
 Whether the button click should select lines.
bool button_
 Whether the left button is pressed.
bool bold_
 Whether the text should be bold.
bool underline_
 Whether the text should be underlined.
bool strikeout_
 Whether the text should be struck-out.
bool italic_
 Whether the text should be italic.
bool multiline_
 Whether the text could be shown in multilines.
bool wrap_
 Whether the text should be wrapped.
bool cursor_visible_
 whether the cursor should be displayed
bool readonly_
 whether the edit control is readonly
bool content_modified_
 Indicates if the content of the edit control has been modified since last draw.
bool selection_changed_
 Indicates if the selection region has been changed since last draw.
bool cursor_moved_
 Indicates if the cursor position has been moved since last draw.
bool update_canvas_
 Indicates if the canvas cache needs updating.
std::string font_family_
 The font family of the text.
double font_size_
 The font size of the text.
cairo_font_options_t * font_options_
double font_dpi_
Color _text_color
 The text color of the edit control.
CairoGraphics::Alignment align_
std::list< Rectlast_selection_region_
std::list< Rectselection_region_
std::list< Rectlast_cursor_region_
std::list< Rectcursor_region_
bool text_input_mode_
bool key_nav_mode_

Detailed Description

Definition at line 76 of file TextEntry.h.


Member Function Documentation

void nux::TextEntry::DeleteText ( int  start,
int  end 
) [protected]

Delete text in a specified range, in number of characters.

Definition at line 1469 of file TextEntry.cpp.

References _text, readonly_, ResetLayout(), selection_bound_, and sigTextChanged.

Referenced by BackSpace(), Delete(), DeleteSelection(), and EnterText().

  {
    if (readonly_) return;

    int text_length = static_cast<int>(_text.length());
    if (start < 0)
      start = 0;
    else if (start > text_length)
      start = text_length;

    if (end < 0)
      end = 0;
    else if (end > text_length)
      end = text_length;

    if (start > end)
      std::swap(start, end);
    else if (start == end)
      return;

    _text.erase(start, end - start);

    if (cursor_ >= end)
      cursor_ -= (end - start);
    if (selection_bound_ >= end)
      selection_bound_ -= (end - start);

    ResetLayout();
    sigTextChanged.emit (this);
  }
CairoGraphics * nux::TextEntry::EnsureCanvas ( ) [protected]

Create cairo canvas on-demand.

Definition at line 733 of file TextEntry.cpp.

References canvas_.

  {
    if (canvas_)
    {
      if ((GetBaseWidth () == canvas_->GetWidth()) && (GetBaseHeight () == canvas_->GetHeight()))
      {
        return canvas_;
      }
      else
      {
        nuxDebugMsg(TEXT("[TextEntry::EnsureCanvas] Recreate canvas"));
        delete canvas_;
        canvas_ = NULL;
      }
    }
    canvas_ = new CairoGraphics (CAIRO_FORMAT_ARGB32, GetBaseWidth (), GetBaseHeight ());
    nuxAssert (canvas_);
    return canvas_;
  }
PangoLayout * nux::TextEntry::EnsureLayout ( ) [protected]

Create pango layout on-demand.

If the layout is not changed, return the cached one.

Definition at line 1101 of file TextEntry.cpp.

References cached_layout_, and CreateLayout().

Referenced by AdjustScroll(), DrawText(), GetCursorLocationInLayout(), MoveDisplayLines(), MoveLineEnds(), MovePages(), MoveVisually(), MoveWords(), and XYToTextIndex().

Area * nux::TextEntry::FindAreaUnderMouse ( const Point mouse_position,
NuxEventType  event_type 
) [virtual]

Return the area under the mouse pointer.

Returns:
The Area under the mouse pointer.

Reimplemented from nux::View.

Definition at line 305 of file TextEntry.cpp.

  {
    Area* area = View::FindAreaUnderMouse(mouse_position, event_type);

    return area;
  }
int nux::TextEntry::GetCharLength ( int  index) [protected]

Get char length at index, in number of bytes.

Definition at line 1417 of file TextEntry.cpp.

References _text.

Referenced by Delete(), and EnterText().

  {
    const char *text = _text.c_str();
    const char *ptr = text + index;
    const char *end = text + _text.length();
    const char *next = g_utf8_find_next_char(ptr, end);
    return static_cast<int>(next ? static_cast<int>(next - ptr) : end - ptr);
  }
void nux::TextEntry::GetCursorLocationInLayout ( int *  strong_x,
int *  strong_y,
int *  strong_height,
int *  weak_x,
int *  weak_y,
int *  weak_height 
) [protected]

Gets the cursor location in pango layout.

The unit is pixel.

Definition at line 1076 of file TextEntry.cpp.

References EnsureLayout(), and TextIndexToLayoutIndex().

Referenced by AdjustScroll(), and DrawCursor().

  {
    PangoLayout *layout = EnsureLayout();
    int cursor_index = TextIndexToLayoutIndex(cursor_, true);

    PangoRectangle strong, weak;
    pango_layout_get_cursor_pos(layout, cursor_index, &strong, &weak);

    if (strong_x)
      *strong_x = PANGO_PIXELS(strong.x);
    if (strong_y)
      *strong_y = PANGO_PIXELS(strong.y);
    if (strong_height)
      *strong_height = PANGO_PIXELS(strong.height);
    if (weak_x)
      *weak_x = PANGO_PIXELS(weak.x);
    if (weak_y)
      *weak_y = PANGO_PIXELS(weak.y);
    if (weak_height)
      *weak_height = PANGO_PIXELS(weak.height);
  }
int nux::TextEntry::GetPrevCharLength ( int  index) [protected]

Get previous char length before index, in number of bytes.

Definition at line 1426 of file TextEntry.cpp.

References _text.

Referenced by BackSpace().

  {
    const char *text = _text.c_str();
    const char *ptr = text + index;
    const char *prev = g_utf8_find_prev_char(text, ptr);
    return static_cast<int>(prev ? static_cast<int>(ptr - prev) : ptr - text);
  }
Color nux::TextEntry::GetSelectionBackgroundColor ( ) [protected]

Gets the color of selection background.

Definition at line 1066 of file TextEntry.cpp.

Referenced by DrawText().

  {
    return kDefaultSelectionBackgroundColor;
  }
bool nux::TextEntry::GetSelectionBounds ( int *  start,
int *  end 
) [protected]

Get the offset range that is currently selected,in number of characters.

Definition at line 2066 of file TextEntry.cpp.

References cursor_, and selection_bound_.

Referenced by BackSpace(), Delete(), DeleteSelection(), and EnterText().

  {
    if (start)
      *start = Min<int>(selection_bound_, cursor_);
    if (end)
      *end = Max<int>(selection_bound_, cursor_);

    return(selection_bound_ != cursor_);
  }
Color nux::TextEntry::GetSelectionTextColor ( ) [protected]

Gets the color of selection text.

Definition at line 1071 of file TextEntry.cpp.

Referenced by DrawText().

  {
    return kDefaultSelectionTextColor;
  }
int nux::TextEntry::LayoutIndexToTextIndex ( int  layout_index) [protected]

Convert index in layout text into index in text_.

Definition at line 1382 of file TextEntry.cpp.

References _preedit, _text, cursor_, password_char_, and visible_.

Referenced by MoveDisplayLines(), MoveLineEnds(), MoveVisually(), MoveWords(), and XYToTextIndex().

  {
    if (visible_)
    {
      if (layout_index < cursor_)
        return layout_index;

      int preedit_length = static_cast<int>(_preedit.length());
      if (layout_index >= cursor_ + preedit_length)
        return layout_index - preedit_length;

      return cursor_;
    }

    int password_char_length = static_cast<int>(password_char_.length());
    nuxAssert(layout_index % password_char_length == 0);

    int offset = layout_index / password_char_length;

    const char *text = _text.c_str();
    int cursor_offset = static_cast<int>(
      g_utf8_pointer_to_offset(text, text + cursor_));
    int preedit_chars = static_cast<int>(
      g_utf8_strlen(_preedit.c_str(), _preedit.length()));

    if (offset < cursor_offset)
      return static_cast<int>(g_utf8_offset_to_pointer(text, offset) - text);

    if (offset >= cursor_offset + preedit_chars)
      return static_cast<int>(
      g_utf8_offset_to_pointer(text, offset - preedit_chars) - text);

    return cursor_;
  }
void nux::TextEntry::QueueRefresh ( bool  relayout,
bool  adjust_scroll 
) [protected]

Send out a request to refresh all informations of the edit control and queue a draw request.

If relayout is true then the layout will be regenerated.

Definition at line 811 of file TextEntry.cpp.

References AdjustScroll(), QueueCursorBlink(), and ResetLayout().

Referenced by MoveCursor(), Select(), and SelectAll().

  {
    if (relayout)
      ResetLayout();

    if (adjust_scroll)
      AdjustScroll();

    QueueTextDraw();
    QueueCursorBlink();
  }
void nux::TextEntry::ResetLayout ( ) [protected]

Remove the cached layout.

Definition at line 1143 of file TextEntry.cpp.

References cached_layout_, and content_modified_.

Referenced by DeleteText(), EnterText(), QueueRefresh(), and ResetPreedit().

  {
    if (cached_layout_)
    {
      g_object_unref(cached_layout_);
      cached_layout_ = NULL;
      content_modified_ = true;
    }
  }
void nux::TextEntry::Select ( int  start,
int  end 
)

Select text between start and end.

Definition at line 1514 of file TextEntry.cpp.

References _text, QueueRefresh(), and SetSelectionBounds().

                                           {
    int text_length = static_cast<int>(_text.length());
    if (start == -1)
      start = text_length;
    if (end == -1)
      end = text_length;

    start = Clamp(start, 0, text_length);
    end = Clamp(end, 0, text_length);
    SetSelectionBounds(start, end);
    QueueRefresh(false, true);
  }
void nux::TextEntry::SetCursor ( int  cursor) [protected]

Set the current cursor offset, in number of characters.

Definition at line 2011 of file TextEntry.cpp.

References cursor_moved_, ResetImContext(), selection_bound_, and selection_changed_.

Referenced by MoveCursor().

  {
    if (cursor != cursor_)
    {
      ResetImContext();
      // If there was a selection range, then the selection range will be cleared.
      // Then content_modified_ shall be set to true to force redrawing the text.
      if (cursor_ != selection_bound_)
        selection_changed_ = true;
      cursor_ = cursor;
      selection_bound_ = cursor;
      cursor_moved_ = true;

      cursor_moved.emit (cursor);
    }
  }
void nux::TextEntry::SetSelectionBounds ( int  selection_bound,
int  cursor 
) [protected]

Set the offest range that should be selected, in number of characters.

Definition at line 2076 of file TextEntry.cpp.

References cursor_moved_, selection_bound_, and selection_changed_.

Referenced by MoveCursor(), Select(), SelectAll(), SelectLine(), and SelectWord().

  {
    if (selection_bound_ != selection_bound || cursor_ != cursor)
    {
      selection_changed_ = true;
      selection_bound_ = selection_bound;
      if (cursor_ != cursor)
      {
        cursor_ = cursor;
        cursor_moved_ = true;
        cursor_moved.emit (cursor);
      }

      //ResetImContext();
    }
  }
int nux::TextEntry::TextIndexToLayoutIndex ( int  text_index,
bool  consider_preedit_cursor 
) [protected]

Convert index in text_ into index in layout text.

Definition at line 1343 of file TextEntry.cpp.

References _preedit, _text, password_char_, preedit_cursor_, and visible_.

Referenced by GetCursorLocationInLayout(), MoveDisplayLines(), MoveLineEnds(), MoveVisually(), and MoveWords().

  {
      if (visible_)
      {
        if (text_index < cursor_)
          return text_index;

        if (text_index == cursor_ && consider_preedit_cursor)
          return text_index + preedit_cursor_;

        return text_index + static_cast<int>(_preedit.length());
      }

      const char *text = _text.c_str();
      int offset = static_cast<int>(
        g_utf8_pointer_to_offset(text, text + text_index));
      int preedit_offset = 0;
      int preedit_chars = 0;
      if (_preedit.length())
      {
        const char *preedit_text = _preedit.c_str();
        preedit_offset = static_cast<int>(g_utf8_pointer_to_offset(
          preedit_text, preedit_text + preedit_cursor_));
        preedit_chars = static_cast<int>(g_utf8_strlen(
          preedit_text, _preedit.length()));
      }

      int password_char_length = static_cast<int>(password_char_.length());

      if (text_index < cursor_)
        return offset * password_char_length;

      if (text_index == cursor_ && consider_preedit_cursor)
        return (offset + preedit_offset) * password_char_length;

      return (offset + preedit_chars) * password_char_length;
  }

Member Data Documentation

int nux::TextEntry::cursor_ [protected]

The current cursor position in number of bytes.

Definition at line 301 of file TextEntry.h.

Referenced by CreateLayout(), GetSelectionBounds(), LayoutIndexToTextIndex(), MoveCursor(), and XYToTextIndex().

Indicates the status of cursor blinking, 0 means hide cursor otherwise means show cursor.

The maximum value would be 2, and decrased by one in each cursor blink callback, then there would be 2/3 visible time and 1/3 invisible time.

Definition at line 326 of file TextEntry.h.

Indicates if the cursor position has been moved since last draw.

Definition at line 368 of file TextEntry.h.

Referenced by MoveCursor(), SetCursor(), and SetSelectionBounds().

Last time of mouse double click event.

Definition at line 298 of file TextEntry.h.

std::string nux::TextEntry::password_char_ [protected]

The character that should be displayed in invisible mode.

If this is empty, then the edit control is visible

Definition at line 295 of file TextEntry.h.

Referenced by CreateLayout(), LayoutIndexToTextIndex(), and TextIndexToLayoutIndex().

Indicates if the selection region has been changed since last draw.

Definition at line 365 of file TextEntry.h.

Referenced by MoveCursor(), SetCursor(), and SetSelectionBounds().

Text changed signal.

This signal is emitted when the text has changed.

Definition at line 127 of file TextEntry.h.

Referenced by DeleteText(), and EnterText().

Indicates if the canvas cache needs updating.

Definition at line 371 of file TextEntry.h.

Referenced by DrawText().


The documentation for this class was generated from the following files:
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends