### Tutorial

« Statusbars | Spin Buttons »

## Text Entries

The Entry widget allows text to be typed and displayed in a single line text box. The text may be set with function calls that allow new text to replace, prepend or append the current contents of the Entry widget.
You can create a new Entry widget using one of the following constructors.

`Entry(); explicit Entry(int max_length);`

The max_length argument sets the maximum length of the text that can be displayed. The next method alters the text which is currently within the Entry widget.

`void set_text(const String& text);`

The function Gtk::Entry::set_text() sets the contents of the Entry widget, replacing the current contents. Note that the Entry class implements the Editable interface (yes, G::Object supports Java-like interfaces) which contains some more functions for manipulating the contents.
The contents of the Entry can be retrieved by using a call to the following method. This is useful in the signal handlers described below.

`String get_text() const;`

If we don't want the contents of the Entry to be changed by someone typing into it, we can change its editable state.

`void set_editable(``bool is_editable)`;

The method above is inherited from Gtk::Editable and allows us to toggle the editable state of the Entry widget by passing in a true or false value for the is_editable argument.
If we are using the Entry where we don't want the text entered to be visible, for example when a password is being entered, we can use the following method, which also takes a bool flag.

`void set_visibility(bool visible);`

A region of the text may be set as selected by using the following method. This would most often be used after setting some default text in an Entry, making it easy for the user to remove it.

`void select_region(int start, int end);`

If we want to catch when the user has entered text, we can connect to the activate or changed signal. Activate is emitted when the user hits the enter key within the Entry widget. Changed is emitted when the text changes at all, e.g., for every character entered or removed.
The following code is an example of using an Entry widget.
The header file for Inti Entry is entry.h:

`#include<inti/main.h> #include <inti/core.h> #include <inti/gtk/entry.h> #include <inti/gtk/checkbutton.h> using namespace Inti; class EntryWindow : public Gtk::Window { Pointer<Gtk::Entry> entry; protected: void on_enter(); void on_entry_toggle_editable(Gtk::CheckButton *button); void on_entry_toggle_visible(Gtk::CheckButton *button); public: EntryWindow(); ~EntryWindow(); };`

and the source file is entry.cc:

`#include"entry.h"
#include <inti/bind.h>
#include <iostream>
EntryWindow::EntryWindow()
{
set_title("Inti Entry");
set_size_request(200, 100);
Gtk::VBox *vbox = new Gtk::VBox;
add(*vbox);
vbox->show();
entry = new Gtk::Entry;
entry->set_max_length(50);
entry->sig_activate().connect(slot(this, &EntryWindow::on_enter));
entry->set_text("hello");
int tmp_pos = entry->gtk_entry()->text_length;
entry->insert_text(" world", tmp_pos);
entry->select_region(0, entry->gtk_entry()->text_length);
vbox->pack_start(*entry);
entry->show();
Gtk::HBox *hbox = new Gtk::HBox;
vbox->add(*hbox);
hbox->show();
Gtk::CheckButton *check = new Gtk::CheckButton("Editable");
hbox->pack_start(*check);
check->sig_toggled().connect(bind(slot(this, &EntryWindow::on_entry_toggle_editable), check));
check->set_active(true);
check->show();
check = new Gtk::CheckButton("Visible");
hbox->pack_start(*check);
check->sig_toggled().connect(bind(slot(this, &EntryWindow::on_entry_toggle_visible), check));
check->set_active(true);
check->show();
Gtk::Button *button = new Gtk::StockButton(GTK_STOCK_CLOSE);
button->sig_clicked().connect(slot(this, &EntryWindow::dispose));
vbox->pack_start(*button);
button->set_flags(Gtk::CAN_DEFAULT);
button->grab_default();
button->show();
}
EntryWindow::~EntryWindow()
{
}
void
EntryWindow::on_enter()
{
using namespace std;
String text = entry->get_text();
cout << "Entry contents: " << text.c_str() << endl;
}
void
EntryWindow::on_entry_toggle_editable(Gtk::CheckButton *button)
{
entry->set_editable(button->get_active());
}
void
EntryWindow::on_entry_toggle_visible(Gtk::CheckButton *button)
{
entry->set_visibility(button->get_active());
}
int main(int argc, char *argv[])
{
using namespace Main;
init(&argc, &argv);
EntryWindow window;
window.sig_destroy().connect(slot(&Inti::Main::quit));
window.show();
run();
return 0;
}`

« Statusbars | Index Miscellaneous Widgets Top | Spin Buttons »