Καταχώριση

8.2.1. Απλή χρήση

Τα γραφικά συστατικά καταχώρισης επιτρέπουν στον χρήστη να εισάγει κείμενο. Μπορείτε να αλλάξετε τα περιεχόμενα με τη μέθοδο set_text() και να διαβάσετε τα τρέχοντα περιεχόμενα με τη μέθοδο get_text().

Ενίοτε μπορεί να θέλετε να κάνετε ένα γραφικό συστατικό Entry μόνο για ανάγνωση. Αυτό μπορεί να γίνει περνώντας το false στη μέθοδο set_editable().

Για την είσοδο των κωδικών πρόσβασης, συνθηματικών και άλλων πληροφοριών που δεν θέλετε να εμφανίζονται στην οθόνη, η κλήση της set_visibility() με false θα προκαλέσει την απόκρυψη του κειμένου.

You might want to be notified whenever the user types in a text entry widget. Gtk::Entry provides two signals, activate and changed, for 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. Moving the keyboard focus to another widget may also signal that the user has finished entering text. The focus_out_event signal that Gtk::Entry inherits from Gtk::Widget can notify you when that happens. The ComboBox with an Entry section contains example programs that use these signals.

If you pass true to the set_activates_default() method, pressing Enter in the Gtk::Entry will activate the default widget for the window containing the Gtk::Entry. This is especially useful in dialog boxes. The default widget is usually one of the dialog buttons, which e.g. will close the dialog box. To set a widget as the default widget, use Gtk::Widget::set_can_default() and Gtk::Widget::grab_default().

Reference

8.2.1.1. Απλό παράδειγμα καταχώρισης

Αυτό το παράδειγμα χρησιμοποιεί την Gtk::Entry. Έχει επίσης δύο CheckButtons, με τα οποία μπορείτε να εναλλάξετε την επεξεργάσιμη και την ορατή σημαία.

Φιγούρα 8-2Καταχώριση

Source Code

File: examplewindow.h (For use with gtkmm 4)

#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

#include <gtkmm.h>

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

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

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

#endif //GTKMM_EXAMPLEWINDOW_H

File: main.cc (For use with gtkmm 4)

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

int main(int argc, char *argv[])
{
  auto app = Gtk::Application::create("org.gtkmm.example");

  //Shows the window and returns when it is closed.
  return app->make_window_and_run<ExampleWindow>(argc, argv);
}

File: examplewindow.cc (For use with gtkmm 4)

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

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

  set_child(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_Entry.set_expand(true);
  m_VBox.append(m_Entry);

  m_VBox.append(m_HBox);

  m_HBox.append(m_CheckButton_Editable);
  m_CheckButton_Editable.set_expand(true);
  m_CheckButton_Editable.signal_toggled().connect( sigc::mem_fun(*this,
              &ExampleWindow::on_checkbox_editable_toggled) );
  m_CheckButton_Editable.set_active(true);

  m_HBox.append(m_CheckButton_Visible);
  m_CheckButton_Visible.set_expand(true);
  m_CheckButton_Visible.signal_toggled().connect( sigc::mem_fun(*this,
              &ExampleWindow::on_checkbox_visibility_toggled) );
  m_CheckButton_Visible.set_active(true);

  m_Button_Close.signal_clicked().connect( sigc::mem_fun(*this,
              &ExampleWindow::on_button_close) );
  m_VBox.append(m_Button_Close);
  m_Button_Close.set_expand();
  set_default_widget(m_Button_Close);
}

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

8.2.2. Συμπλήρωση καταχώρισης

An Entry widget can offer a drop-down list of pre-existing choices based on the first few characters typed by the user. For instance, a search dialog could suggest text from previous searches.

To enable this functionality, you must create an EntryCompletion object, and provide it to the Entry widget via the set_completion() method.

Η EntryCompletion μπορεί να χρησιμοποιήσει ένα TreeModel που περιέχει πιθανές καταχωρίσεις, που ορίστηκαν με set_model(). Τότε θα πρέπει να καλέσετε την set_text_column() για να ορίσετε ποιες στήλες του προτύπου σας πρέπει να χρησιμοποιηθούν για να ταιριάζουν με πιθανές καταχωρίσεις κειμένου.

Εναλλακτικά, αν μια πλήρης λίστα των πιθανών καταχωρίσεων μπορεί να είναι υπερβολικά μεγάλη ή υπερβολικά άβολη για να δημιουργηθεί, μια σχισμή επανάκλησης μπορεί να οριστεί με set_match_func(). Αυτό είναι επίσης χρήσιμο, αν θέλετε να ταιριάξετε ένα μέρος της συμβολοσειράς διαφορετικό από την αρχή.

Reference

8.2.2.1. Παράδειγμα συμπλήρωσης καταχώρισης

Αυτό το παράδειγμα δημιουργεί μια Gtk::EntryCompletion και την συσχετίζει με ένα γραφικό συστατικό Gtk::Entry. Η συμπλήρωση χρησιμοποιεί μια Gtk::TreeModel των πιθανών καταχωρίσεων και κάποιες πρόσθετες ενέργειες.

Φιγούρα 8-3Συμπλήρωση καταχώρισης

Source Code

File: examplewindow.h (For use with gtkmm 4)

#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

#include <gtkmm.h>

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

protected:
  //Signal handlers:
  void on_button_close();

  //See the comment in the implementation:
  //bool on_completion_match(const Glib::ustring& key, const Gtk::TreeModel::const_iterator& iter);

  //Tree model columns, for the EntryCompletion's filter model:
  class ModelColumns : public Gtk::TreeModel::ColumnRecord
  {
  public:
    ModelColumns()
    { add(m_col_id); add(m_col_name); }

    Gtk::TreeModelColumn<unsigned int> m_col_id;
    Gtk::TreeModelColumn<Glib::ustring> m_col_name;
  };

  ModelColumns m_Columns;

  //Child widgets:
  Gtk::Box m_HBox;
  Gtk::Box m_VBox;
  Gtk::Entry m_Entry;
  Gtk::Label m_Label;
  Gtk::Button m_Button_Close;
};

#endif //GTKMM_EXAMPLEWINDOW_H

File: main.cc (For use with gtkmm 4)

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

int main(int argc, char *argv[])
{
  auto app = Gtk::Application::create("org.gtkmm.example");

  //Shows the window and returns when it is closed.
  return app->make_window_and_run<ExampleWindow>(argc, argv);
}

File: examplewindow.cc (For use with gtkmm 4)

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

ExampleWindow::ExampleWindow()
: m_VBox(Gtk::Orientation::VERTICAL),
  m_Label("Press a or b to see a list of possible completions."),
  m_Button_Close("Close")
{
  //set_size_request(200, 100);
  set_title("Gtk::EntryCompletion");

  set_child(m_VBox);
  m_VBox.append(m_Entry);

  m_VBox.append(m_Label);
  m_Label.set_expand(true);

  m_Button_Close.signal_clicked().connect( sigc::mem_fun(*this,
              &ExampleWindow::on_button_close) );
  m_VBox.append(m_Button_Close);
  set_default_widget(m_Button_Close);

  //Add an EntryCompletion:
  auto completion =
      Gtk::EntryCompletion::create();
  m_Entry.set_completion(completion);

  //Create and fill the completion's filter model
  auto refCompletionModel =
      Gtk::ListStore::create(m_Columns);
  completion->set_model(refCompletionModel);

  // For more complex comparisons, use a filter match callback, like this.
  // See the comment below for more details:
  //completion->set_match_func( sigc::mem_fun(*this,
              //&ExampleWindow::on_completion_match) );

  //Fill the TreeView's model
  auto row = *(refCompletionModel->append());
  row[m_Columns.m_col_id] = 1;
  row[m_Columns.m_col_name] = "Alan Zebedee";

  row = *(refCompletionModel->append());
  row[m_Columns.m_col_id] = 2;
  row[m_Columns.m_col_name] = "Adrian Boo";

  row = *(refCompletionModel->append());
  row[m_Columns.m_col_id] = 3;
  row[m_Columns.m_col_name] = "Bob McRoberts";

  row = *(refCompletionModel->append());
  row[m_Columns.m_col_id] = 4;
  row[m_Columns.m_col_name] = "Bob McBob";

  //Tell the completion what model column to use to
  //- look for a match (when we use the default matching, instead of
  //  set_match_func().
  //- display text in the entry when a match is found.
  completion->set_text_column(m_Columns.m_col_name);
}

ExampleWindow::~ExampleWindow()
{
}

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

/* You can do more complex matching with a handler like this.
 * For instance, you could check for substrings inside the string instead of the start,
 * or you could look for the key in extra model columns as well as the model column that will be displayed.
 * The code here is not actually more complex - it's a reimplementation of the default behaviour.
 *
bool ExampleWindow::on_completion_match(const Glib::ustring& key, const
        Gtk::TreeModel::const_iterator& iter)
{
  if(iter)
  {
    const auto row = *iter;

    const auto key_length = key.size();
    auto filter_string = row[m_Columns.m_col_name];

    auto filter_string_start = filter_string.substr(0, key_length);
    //The key is lower-case, even if the user input is not.
    filter_string_start = filter_string_start.lowercase();

    if(key == filter_string_start)
      return true; //A match was found.
  }

  return false; //No match.
}
*/

8.2.3. Εικονίδια καταχώρισης

Ένα γραφικό συστατικό Entry μπορεί να εμφανίσει ένα εικονίδιο στην αρχή ή το τέλος της περιοχής κειμένου. Το εικονίδιο μπορεί να οριστεί με μεθόδους όπως set_icon_from_pixbuf() ή set_icon_from_icon_name(). Μια εφαρμογή μπορεί να απαντά στο πάτημα του εικονιδίου από τον χρήστη με επεξεργασία του σήματος signal_icon_press.

8.2.3.1. Παράδειγμα εικονιδίου καταχώρισης

Αυτό το παράδειγμα δείχνει ένα γραφικό συστατικό Gtk::Entry με ένα επώνυμο εικονίδιο αναζήτησης και εκτυπώνει το κείμενο στο τερματικό, όταν το εικονίδιο πατιέται.

Φιγούρα 8-4Καταχώριση με εικονίδιο

Source Code

File: examplewindow.h (For use with gtkmm 4)

#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

#include <gtkmm.h>

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

protected:
  //Signal handlers:
  void on_icon_pressed(Gtk::Entry::IconPosition icon_pos);
  void on_button_close();

  //Child widgets:
  Gtk::Box m_VBox;
  Gtk::Entry m_Entry;
  Gtk::Button m_Button_Close;
};

#endif //GTKMM_EXAMPLEWINDOW_H

File: main.cc (For use with gtkmm 4)

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

int main(int argc, char *argv[])
{
  auto app = Gtk::Application::create("org.gtkmm.example");

  //Shows the window and returns when it is closed.
  return app->make_window_and_run<ExampleWindow>(argc, argv);
}

File: examplewindow.cc (For use with gtkmm 4)

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

ExampleWindow::ExampleWindow()
: m_VBox(Gtk::Orientation::VERTICAL),
  m_Button_Close("Close")
{
  set_title("Gtk::Entry");

  set_child(m_VBox);

  m_Entry.set_max_length(50);
  m_Entry.set_text("Hello world");
  m_VBox.append(m_Entry);

  m_Entry.set_icon_from_icon_name("edit-find");
  m_Entry.signal_icon_press().connect( sigc::mem_fun(*this, &ExampleWindow::on_icon_pressed) );


  m_Button_Close.signal_clicked().connect( sigc::mem_fun(*this,
              &ExampleWindow::on_button_close) );
  m_VBox.append(m_Button_Close);
  set_default_widget(m_Button_Close);
}

ExampleWindow::~ExampleWindow()
{
}

void ExampleWindow::on_icon_pressed(Gtk::Entry::IconPosition /* icon_pos */)
{
  std::cout << "Icon pressed." << std::endl;
}

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

8.2.4. Πρόοδος καταχώρισης

Ένα γραφικό συστατικό Entry μπορεί να προβάλει μια γραμμή προόδου μέσα στην περιοχή κειμένου, κάτω από το εισαγόμενο κείμενο. Η γραμμή προόδου θα εμφανιστεί αν οι μέθοδοι set_progress_fraction() ή set_progress_pulse_step() κληθούν.

8.2.4.1. Παράδειγμα προόδου καταχώρισης

Αυτό το παράδειγμα δείχνει ένα γραφικό συστατικό Gtk::Entry με μια γραμμή προόδου.

Φιγούρα 8-5Καταχώριση με γραμμή προόδου

Source Code

File: examplewindow.h (For use with gtkmm 4)

#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

#include <gtkmm.h>

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

protected:
  //Signal handlers:
  bool on_timeout();
  void on_button_close();

  //Child widgets:
  Gtk::Box m_VBox;
  Gtk::Entry m_Entry;
  Gtk::Button m_Button_Close;
};

#endif //GTKMM_EXAMPLEWINDOW_H

File: main.cc (For use with gtkmm 4)

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

int main(int argc, char *argv[])
{
  auto app = Gtk::Application::create("org.gtkmm.example");

  //Shows the window and returns when it is closed.
  return app->make_window_and_run<ExampleWindow>(argc, argv);
}

File: examplewindow.cc (For use with gtkmm 4)

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

ExampleWindow::ExampleWindow()
: m_VBox(Gtk::Orientation::VERTICAL),
  m_Button_Close("Close")
{
  set_title("Gtk::Entry");

  set_child(m_VBox);

  m_Entry.set_max_length(50);
  m_Entry.set_text("Hello world");
  m_VBox.append(m_Entry);

  //Change the progress fraction every 0.1 second:
  Glib::signal_timeout().connect(
    sigc::mem_fun(*this, &ExampleWindow::on_timeout),
    100
  );

  m_Button_Close.signal_clicked().connect( sigc::mem_fun(*this,
              &ExampleWindow::on_button_close) );
  m_VBox.append(m_Button_Close);
  set_default_widget(m_Button_Close);
}

ExampleWindow::~ExampleWindow()
{
}

bool ExampleWindow::on_timeout()
{
  static double fraction = 0;
  m_Entry.set_progress_fraction(fraction);

  fraction += 0.01;
  if(fraction > 1)
    fraction = 0;

  return true;
}

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