Xfce Foundation Classes
 « Main Page | Index

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();

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 set_text() method sets the contents of the Entry widget, replacing the current contents. Note that the Entry widget implements the Gtk::Editable interface which contains more methods for manipulating the contents.

The contents of the entry can be retrieved by calling to the following method:

String get_text() const;

If you don't want the contents of the Entry widget to be changed by someone typing into it, you can set its editable state:

void set_editable(bool is_editable);

This method is inherited from Gtk::Editable and allows you to toggle the editable state of an entry widget by passing true or false for the 'is_editable' argument.

If you are using an Entry widget where you don't want the text entered to be visible, for example when a password is being entered, you 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 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 you want to intercept when the user has entered text, you can connect to the 'activate' or 'changed' signal. An activate signal is emitted when the user hits the enter key within the Entry widget. A changed signal is emitted when the text changes, e.g., for every character entered or removed.

Text Entry Example

The following example displays a window with an Entry widget and the text 'hello world' highlighted. Checking and unchecking the editable checkbutton will toggle the editable state of the entry widget. Checking and unchecking the visible checkbutton will toggle the visibility state of the text in Entry widget.

The header file for XFC Entry is <entry.hh>:

#include <xfc/main.hh>
#include <xfc/gtk/entry.hh>
#include <xfc/gtk/checkbutton.hh>
#include <xfc/gtk/window.hh>

using namespace Xfc;

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.hh"
#include <xfc/gtk/box.hh>
#include <xfc/gtk/stockid.hh>
#include <iostream>

EntryWindow::EntryWindow()
{
    set_title("XFC 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->signal_activate().connect(sigc::mem_fun(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->signal_toggled().connect(bind(sigc::mem_fun(this, &EntryWindow::on_entry_toggle_editable), check));
    check->set_active(true);
    check->show();

    check = new Gtk::CheckButton("Visible");
    hbox->pack_start(*check);
    check->signal_toggled().connect(sigc::bind(sigc::mem_fun(this, &EntryWindow::on_entry_toggle_visible), check));
    check->set_active(true);
    check->show();

    Gtk::Button *button = new Gtk::Button(GTK_STOCK_CLOSE);
    button->signal_clicked().connect(sigc::mem_fun(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.signal_destroy().connect(sigc::ptr_fun(&Xfc::Main::quit));
    window.show();

    run();
    return 0;
}

Compiling Text Entry

If you compiled and installed XFC yourself, you will find the source code for Text Entry in the <examples/howto/entry> source directory along with a Makefile. If XFC came pre-installed, or you installed it from an RPM package, you will find the source code in the </usr/share/doc/xfcui-X.X/examples/howto/entry> subdirectory. In this case you will have to create the Makefile yourself (replace X.X with the version number of the libXFCui library you have installed).

To create a Makefile for Text Entry, add the following lines to a new text file and save it using the name "Makefile":

CC = g++

CFLAGS = -Wall -O2

entry: entry.cc entry.hh
    $(CC) entry.cc -o entry $(CFLAGS) `pkg-config xfcui-X.X --cflags --libs`

clean:
    rm -f *.o entry


If you cut and paste these lines make sure the whitespace before $(CC) and rm is a tab character. When you compile and run this program you will see the following window appear:




Copyright © 2004-2005 The XFC Development Team Top
XFC 4.4