Examples with a Search Entry

The dropdown menu may contain an Entry that allows to search for items in the list. Call set_enable_search() and set_expression(). For instance:

m_DropDown.set_enable_search(true);
auto expression = Gtk::ClosureExpression<Glib::ustring>::create(
  sigc::mem_fun(*this, &ExampleWindow::get_col_name));
m_DropDown.set_expression(expression);

//-------
Glib::ustring ExampleWindow::get_col_name(const Glib::RefPtr<Glib::ObjectBase>& item)
{
  const auto col = std::dynamic_pointer_cast<ModelColumns>(item);
  return col ? col->m_col_name : "";
}

String Example

Figure 12.2. Search String

Search String

Source Code

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

#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

#include <gtkmm/window.h>
#include <gtkmm/dropdown.h>
#include <gtkmm/stringlist.h>

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

protected:
  // Signal handler:
  void on_dropdown_changed();

  // Child widget:
  Gtk::DropDown m_DropDown;
  
  Glib::RefPtr<Gtk::StringList> m_StringList;
};

#endif //GTKMM_EXAMPLEWINDOW_H

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

#include "examplewindow.h"
#include <gtkmm/expression.h>
#include <gtkmm/stringobject.h>
#include <iostream>

ExampleWindow::ExampleWindow()
{
  set_title("Searchable DropDown example");

  set_child(m_DropDown);

  // Fill the dropdown:
  const std::vector<Glib::ustring> many_times{
    "1 minute", "2 minutes", "5 minutes", "10 minutes", "15 minutes", "20 minutes",
    "25 minutes", "30 minutes", "35 minutes", "40 minutes", "45 minutes", "50 minutes",
    "55 minutes", "1 hour", "2 hours", "3 hours", "5 hours", "6 hours", "7 hours",
    "8 hours", "9 hours", "10 hours", "11 hours", "12 hours"
  };
  m_StringList = Gtk::StringList::create(many_times);
  m_DropDown.set_model(m_StringList);
  m_DropDown.set_selected(0);

  // Show a search entry.
  m_DropDown.set_enable_search(true);
  auto expression = Gtk::ClosureExpression<Glib::ustring>::create(
    [](const Glib::RefPtr<Glib::ObjectBase>& item)->Glib::ustring
    {
      // An item in a StringList is a StringObject.
      const auto string_object = std::dynamic_pointer_cast<Gtk::StringObject>(item);
      return string_object ? string_object->get_string() : "";
    });
  m_DropDown.set_expression(expression);

  // Connect signal handler:
  m_DropDown.property_selected().signal_changed().connect(
    sigc::mem_fun(*this, &ExampleWindow::on_dropdown_changed));
}

ExampleWindow::~ExampleWindow()
{
}

void ExampleWindow::on_dropdown_changed()
{
  const auto selected = m_DropDown.get_selected();
  std::cout << "DropDown changed: Row=" << selected
    << ", String=" << m_StringList->get_string(selected) << std::endl;
}

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

Font Example

This example uses a Pango::FontMap as its model. This is possible because Pango::FontMap implements the Gio::ListModel interface. Of course you can use a FontDialogButton instead.

Figure 12.3. Search Font

Search Font

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();
  ~ExampleWindow() override;

protected:
  // Signal handler:
  void on_dropdown_changed();

  Glib::ustring get_family_name(const Glib::RefPtr<Glib::ObjectBase>& item);

  // Child widget:
  Gtk::DropDown m_DropDown;
};

#endif //GTKMM_EXAMPLEWINDOW_H

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

#include "examplewindow.h"
#include <pangomm/cairofontmap.h>
#include <iostream>
//#include <typeinfo>

ExampleWindow::ExampleWindow()
{
  set_title("Searchable DropDown example");

  // Add the DropDown to the window.
  set_child(m_DropDown);

  // Show a search entry.
  m_DropDown.set_enable_search(true);
  auto expression = Gtk::ClosureExpression<Glib::ustring>::create(
    sigc::mem_fun(*this, &ExampleWindow::get_family_name));
  m_DropDown.set_expression(expression);

  // Pango::FontMap is a Gio::ListModel.
  auto model = Pango::CairoFontMap::get_default();
  m_DropDown.set_model(model);
  m_DropDown.set_selected(0);

  // Connect signal handler.
  m_DropDown.property_selected().signal_changed().connect(
    sigc::mem_fun(*this, &ExampleWindow::on_dropdown_changed));
}

ExampleWindow::~ExampleWindow()
{
}

void ExampleWindow::on_dropdown_changed()
{
  const auto selected = m_DropDown.get_selected();
  const auto item = m_DropDown.get_selected_item();
  std::cout << "DropDown changed: Row=" << selected
    << ", Font=" << get_family_name(item) << std::endl;

  // Pango::FontMap is often a list of items of type Pango::FontFamily.
  // From the description of Pango::CairoFontMap: "The actual type of the font map
  // will depend on the particular font technology Cairo was compiled to use."
  // If get_family_name() does not return a family name, you can add
  // #include <typeinfo> and query the type of the items with:
  //   std::cout << typeid(*item.get()).name() << std::endl;
}

Glib::ustring ExampleWindow::get_family_name(const Glib::RefPtr<Glib::ObjectBase>& item)
{
  auto family = std::dynamic_pointer_cast<Pango::FontFamily>(item);
  return family ? family->get_name() : "";
}

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