Entry
Prev | Chapter 6. Miscellaneous Widgets | Next

## Entry

Entry widgets allow the user to enter text (surprisingly enough).

You can change the contents with the `set_text()` method, and read the current contents with the `get_text()` method.

Occasionally you might want to make an `Entry` widget read-only. This can be done by passing `false` to the `set_editable()` method.

For the input of passwords, passphrases and other information you don't want echoed on the screen, calling `set_visibility()` with `false` will cause the text to be hidden.

You might want to be notified whenever the user types in a text entry widget. `Gtk::Entry` provides two signals, `activate` and `changed`, for just this purpose. `activate` is emitted when the user presses the enter key in a text-entry widget; `changed` is emitted when the text in the widget changes. You can use these, for instance, to validate or filter the text the user types.

Reference

### Example

Here is an example using `Gtk::Entry`. As well as a `Gtk::Entry` widget, it has two `CheckButton`s, with which you can toggle the editable and visible flags.

Figure 6.2. Entry

Source Code

File: examplewindow.h

    #ifndef GTKMM_EXAMPLEWINDOW_H
    #define GTKMM_EXAMPLEWINDOW_H

    #include <gtkmm.h>

    class ExampleWindow : public Gtk::Window
    {
    public:
      ExampleWindow();
      virtual ~ExampleWindow();

    protected:
      //Signal handlers:
      virtual void on_checkbox_editable_toggled();
      virtual void on_checkbox_visibility_toggled();
      virtual void on_button_close();

      //Child widgets:
      Gtk::HBox m_HBox;
      Gtk::VBox m_VBox;
      Gtk::Entry m_Entry;
      Gtk::Button m_Button_Close;
      Gtk::CheckButton m_CheckButton_Editable, m_CheckButton_Visible;
    };

    #endif //GTKMM_EXAMPLEWINDOW_H

File: examplewindow.cc

    #include "examplewindow.h"
    #include <iostream>

    ExampleWindow::ExampleWindow()
    : m_Button_Close("Close"),
      m_CheckButton_Editable("Editable"),
      m_CheckButton_Visible("Visible")
    {
      set_size_request(200, 100);
      set_title("Gtk::Entry");

      add(m_VBox);

      m_Entry.set_max_length(50);
      m_Entry.set_text("hello");
      m_Entry.set_text(m_Entry.get_text() + " world");
      m_Entry.select_region(0, m_Entry.get_text_length());
      m_VBox.pack_start(m_Entry);

      // Note that add() can also be used instead of pack_xxx()
      m_VBox.add(m_HBox);

      m_HBox.pack_start(m_CheckButton_Editable);
      m_CheckButton_Editable.signal_toggled().connect( SigC::slot(*this, &ExampleWindow::on_checkbox_editable_toggled) );
      m_CheckButton_Editable.set_active(true);

      m_HBox.pack_start(m_CheckButton_Visible);
      m_CheckButton_Visible.signal_toggled().connect( SigC::slot(*this, &ExampleWindow::on_checkbox_visibility_toggled) );
      m_CheckButton_Visible.set_active(true);

      m_Button_Close.signal_clicked().connect( SigC::slot(*this, &ExampleWindow::on_button_close) );
      m_VBox.pack_start(m_Button_Close);
      m_Button_Close.set_flags(Gtk::CAN_DEFAULT);
      m_Button_Close.grab_default();

      show_all_children();
    }

    ExampleWindow::~ExampleWindow()
    {
    }

    void ExampleWindow::on_checkbox_editable_toggled()
    {
      m_Entry.set_editable(m_CheckButton_Editable.get_active());
    }

    void ExampleWindow::on_checkbox_visibility_toggled()
    {
      m_Entry.set_visibility(m_CheckButton_Visible.get_active());
    }

    void ExampleWindow::on_button_close()
    {
      hide();
    }

File: main.cc

    #include <gtkmm/main.h>
    #include "examplewindow.h"

    int main(int argc, char *argv[])
    {
      Gtk::Main kit(argc, argv);

      ExampleWindow window;
      Gtk::Main::run(window); //Shows the window and returns when it is closed.

      return 0;
    }

Prev | Up | Next
Chapter 6. Miscellaneous Widgets | Home | Combo