void
wx_gtk_insert_text_callback(GtkEditable *editable,
                            const gchar * new_text,
                            gint WXUNUSED(new_text_length),
                            gint * WXUNUSED(position),
                            wxTextEntry *text)
{
    GtkEntry *entry = GTK_ENTRY (editable);

#if GTK_CHECK_VERSION(3,0,0) || defined(GSEAL_ENABLE)
    const int text_max_length = gtk_entry_buffer_get_max_length(gtk_entry_get_buffer(entry));
#else
    const int text_max_length = entry->text_max_length;
#endif

    bool handled = false;

    // check that we don't overflow the max length limit if we have it
    if ( text_max_length )
    {
        const int text_length = gtk_entry_get_text_length(entry);

        // We can't use new_text_length as it is in bytes while we want to count
        // characters (in first approximation, anyhow...).
        if ( text_length + g_utf8_strlen(new_text, -1) > text_max_length )
        {
            // Prevent the new text from being inserted.
            handled = true;

            // Currently we don't insert anything at all, but it would be better to
            // insert as many characters as would fit into the text control and
            // only discard the rest.

            // Notify the user code about overflow.
            text->SendMaxLenEvent();
        }
    }

    if ( !handled && text->GTKEntryOnInsertText(new_text) )
    {
        // If we already handled the new text insertion, don't do it again.
        handled = true;
    }

    if ( handled )
        g_signal_stop_emission_by_name (editable, "insert_text");
}
Beispiel #2
0
static void
gtk_entry_buffer_get_property (GObject    *obj,
                               guint       prop_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
  GtkEntryBuffer *buffer = GTK_ENTRY_BUFFER (obj);

  switch (prop_id)
    {
    case PROP_TEXT:
      g_value_set_string (value, gtk_entry_buffer_get_text (buffer));
      break;
    case PROP_LENGTH:
      g_value_set_uint (value, gtk_entry_buffer_get_length (buffer));
      break;
    case PROP_MAX_LENGTH:
      g_value_set_int (value, gtk_entry_buffer_get_max_length (buffer));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
      break;
    }
}
Beispiel #3
0
void
wx_gtk_insert_text_callback(GtkEditable *editable,
                            const gchar * new_text,
                            gint new_text_length,
                            gint * position,
                            wxTextEntry *text)
{
    GtkEntry *entry = GTK_ENTRY (editable);

#if GTK_CHECK_VERSION(3,0,0) || defined(GSEAL_ENABLE)
    const int text_max_length = gtk_entry_buffer_get_max_length(gtk_entry_get_buffer(entry));
#else
    const int text_max_length = entry->text_max_length;
#endif

    bool handled = false;

    // check that we don't overflow the max length limit if we have it
    if ( text_max_length )
    {
        const int text_length = gtk_entry_get_text_length(entry);

        // We can't use new_text_length as it is in bytes while we want to count
        // characters (in first approximation, anyhow...).
        if ( text_length + g_utf8_strlen(new_text, -1) > text_max_length )
        {
            // Prevent the new text from being inserted.
            handled = true;

            // Currently we don't insert anything at all, but it would be better to
            // insert as many characters as would fit into the text control and
            // only discard the rest.

            // Notify the user code about overflow.
            text->SendMaxLenEvent();
        }
    }

    // Check if we have to convert all input to upper-case
    if ( !handled && text->GTKIsUpperCase() )
    {
        const wxGtkString upper(g_utf8_strup(new_text, new_text_length));

        // Use the converted text to generate events
        if ( !text->GTKEntryOnInsertText(upper) )
        {
            // Event not handled, so do insert the text: we have to do it
            // ourselves to use the upper-case version of it

            // Prevent recursive call to this handler again
            g_signal_handlers_block_by_func
            (
                editable,
                (gpointer)wx_gtk_insert_text_callback,
                text
            );

            gtk_editable_insert_text(editable, upper, strlen(upper), position);

            g_signal_handlers_unblock_by_func
            (
                editable,
                (gpointer)wx_gtk_insert_text_callback,
                text
            );
        }

        // Don't call the default handler in any case, either the event was
        // handled in the user code or we've already inserted the text.
        handled = true;
    }

    if ( !handled && text->GTKEntryOnInsertText(new_text) )
    {
        // If we already handled the new text insertion, don't do it again.
        handled = true;
    }

    if ( handled )
    {
        // We must update the position to point after the newly inserted text,
        // as expected by GTK+.
        *position = text->GetInsertionPoint();

        g_signal_stop_emission_by_name (editable, "insert_text");
    }
}