TextView and Entry

There are two widgets in maemomm for text entry, both of which are inherited from gtkmm equivalents, but with extra Hildon-specific additions.

Entry

A Hildon::Entry widget shows a single-line text entry field, enabling the user to enter freeform text. You can set the placeholder text that is shown when the widget is not focused and is empty. A Hildon::Entry is derived from a Gtk::Entry.

Entry reference

Example

This example shows a simple window that contains a Hildon::Entry, a Hildon::Button and a Gtk::Label. The Hildon::Entry has placeholder text set, which disappears when text is entered into the widget. When the Hildon::Button is clicked, or the Hildon::Entry is activated, the current text in the Hildon::Entry is sent to standard output.

Figure 4.13. Entry

Entry

Source Code

File: examplewindow.h

#ifndef _MAEMOMM_EXAMPLEWINDOW_H
#define _MAEMOMM_EXAMPLEWINDOW_H

#include <hildonmm/window.h>
#include <hildonmm/button.h>
#include <hildonmm/entry.h>
#include <gtkmm/box.h>
#include <gtkmm/buttonbox.h>
#include <gtkmm/table.h>
#include <gtkmm/label.h>

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

private:
  // Signal handlers:
  void on_button_clicked();
  void on_entry_activated();

  // Child widgets:
  Gtk::HBox hbox_;
  Gtk::VBox vbox_;
  Gtk::HButtonBox buttonbox_;
  Hildon::Button button_;
  Gtk::Label label_;
  Hildon::Entry entry_;
};

#endif /* _MAEMOMM_EXAMPLEWINDOW_H */

File: main.cc

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

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

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

  return 0;
}

File: examplewindow.cc

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

ExampleWindow::ExampleWindow() :
  hbox_(false, 6),  //TODO: See https://bugs.maemo.org/show_bug.cgi?id=4464 for correct spacing
  vbox_(false, 6), //TODO: See https://bugs.maemo.org/show_bug.cgi?id=4464 for correct spacing.
  button_(Gtk::Hildon::SIZE_HALFSCREEN_WIDTH | Gtk::Hildon::SIZE_FINGER_HEIGHT,
    Hildon::BUTTON_ARRANGEMENT_VERTICAL,
    "Click Me",
    "to print text in entry"),
  label_("Hildon::Entry example"),
  entry_(Gtk::Hildon::SIZE_HALFSCREEN_WIDTH | Gtk::Hildon::SIZE_FINGER_HEIGHT)
{
  set_title("Hildon::Entry Example");

  entry_.set_placeholder("This is placeholder text");

  buttonbox_.pack_start(button_);

  hbox_.pack_start(label_, Gtk::PACK_SHRINK);
  hbox_.pack_start(entry_);

  vbox_.pack_start(buttonbox_, Gtk::PACK_SHRINK);
  vbox_.pack_start(hbox_, Gtk::PACK_SHRINK);

  add(vbox_);

  button_.signal_clicked().connect(
    sigc::mem_fun(*this, &ExampleWindow::on_button_clicked));
  entry_.signal_activate().connect(
    sigc::mem_fun(*this, &ExampleWindow::on_entry_activated));

  show_all_children();
}

ExampleWindow::~ExampleWindow()
{
}

void ExampleWindow::on_button_clicked()
{
  std::cout << "Button clicked. Current entry text=\"" << entry_.get_text() <<
    "\"." << std::endl;
}

void ExampleWindow::on_entry_activated()
{
  std::cout << "Entry activated. Current text=\"" << entry_.get_text() << "\"."
    << std::endl;
}

TextView

Hildon::TextView is the multi-line equivalent to Hildon::Entry. As with Hildon::Entry, Hildon:TextView is derived from its gtkmm equivalent, in this case Gtk::TextView. The placeholder text functionality that exists in Hildon::Entry, where text can be shown by the widget when it is empty and the widget is not focused, is also present in Hildon::TextView.

[Note] Note

Although Hildon::TextView is derived from Gtk::TextView, Gtk::TextView::get_buffer() and Gtk::TextView::set_buffer() must never be used to get/set the buffer in this widget. Hildon::TextView::get_buffer() and Hildon::TextView::set_buffer() must be used instead.

TextView reference

Example

This example shows a simple window that contains a Hildon::TextView, a Hildon::Button and a Gtk::Label. The Hildon::TextView has placeholder text set, which disappears when text is entered into the widget. Clicking the Hildon::Button , or activating the Hildon::Entry, sends the current text in the Hildon::TextView to standard output.

Figure 4.14. TextView

TextView

Source Code

File: examplewindow.h

#ifndef _MAEMOMM_EXAMPLEWINDOW_H
#define _MAEMOMM_EXAMPLEWINDOW_H

#include <hildonmm/window.h>
#include <hildonmm/button.h>
#include <hildonmm/text-view.h>
#include <gtkmm/table.h>
#include <gtkmm/label.h>

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

private:
  // Signal handlers:
  void on_button_clicked();

  // Child widgets:
  Gtk::VBox vbox_;
  Hildon::Button button_;
  Hildon::TextView textview_;
};

#endif /* _MAEMOMM_EXAMPLEWINDOW_H */

File: main.cc

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

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

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

  return 0;
}

File: examplewindow.cc

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

ExampleWindow::ExampleWindow() :
  button_(Gtk::Hildon::SIZE_HALFSCREEN_WIDTH | Gtk::Hildon::SIZE_FINGER_HEIGHT,
    Hildon::BUTTON_ARRANGEMENT_VERTICAL,
    "Click Me",
    "to print text in textview")
{
  set_title("Hildon::TextView Example");

  vbox_.pack_start(button_, Gtk::PACK_SHRINK);
  vbox_.pack_start(textview_);
  
  textview_.set_placeholder("This is placeholder text");

  add(vbox_);

  button_.signal_clicked().connect(
    sigc::mem_fun(*this, &ExampleWindow::on_button_clicked));

  show_all_children();
}

ExampleWindow::~ExampleWindow()
{
}

void ExampleWindow::on_button_clicked()
{
  std::cout << "Button clicked. Current textview text=\"" <<
    textview_.get_buffer()->get_text() << "\"." << std::endl;
}