<commit_before>/* Icon Browser
 *
 * Demonstrates many named icons.
 *
 * This demo shows Gtk::HeaderBar, Gtk::ListBox, Gtk::IconView and
 * Gtk::SearchBar in action, but its primary aim is to show an assortment
 * of named icons.
 *
 * Click on an icon to see it in different sizes.
 *
 * Press the search button and enter a search string, to show icons
 * whose names match the search string.
 *
 * You can drag icons to other programs. Either drag an icon's name from
 * the icon view, or drag an image from the dialog box that opens when you
 * click on an icon.
 */

#include <gtkmm.h>
#include <string>
#include <sstream>
#include <iostream>

// Labels in the ListBox with icon context names.
// The icon context ids in this program are not the same as in the Icon Naming Specification
// at http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html.
class IconContextLabel : public Gtk::Label
{
public:
  IconContextLabel(const Glib::ustring& id, const Glib::ustring& name)
  : Gtk::Label(name), m_id(id)
  { }

  Glib::ustring get_id() const { return m_id; }

private:
  Glib::ustring m_id;

}; // end IconContextLabel

// A ListStore that stores names, descriptions and context ids of icons.
class IconInfoStore : public Gtk::ListStore
{
public:
  static Glib::RefPtr<IconInfoStore> create();
  ~IconInfoStore() override;

  void set_text_column(bool symbolic)
  { m_text_column = symbolic ? m_columns.symbolic_name : m_columns.name; }

  Gtk::TreeModelColumn<Glib::ustring> get_text_column() const
  { return m_text_column; }

  struct ModelColumns : public Gtk::TreeModelColumnRecord
  {
    Gtk::TreeModelColumn<Glib::ustring> name;
    Gtk::TreeModelColumn<Glib::ustring> symbolic_name;
    Gtk::TreeModelColumn<Glib::ustring> description;
    Gtk::TreeModelColumn<Glib::ustring> context_id;

    ModelColumns()
    {
      add(name);
      add(symbolic_name);
      add(description);
      add(context_id);
    }
  };

  const ModelColumns m_columns;

protected:
  IconInfoStore();

  // Overridden virtual function:
  bool drag_data_get_vfunc(const Gtk::TreeModel::Path& path,
    Gtk::SelectionData& selection_data) const override;

private:
  Gtk::TreeModelColumn<Glib::ustring> m_text_column;

}; // end IconInfoStore

// Dialog box, showing icon details.
class DetailDialog : public Gtk::Dialog
{
public:
  explicit DetailDialog(Gtk::Window& parent);
  ~DetailDialog() override;

  void set_image(const Glib::ustring& icon_name, const Glib::ustring& description);

protected:
  // Signal handler:
  void on_image_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context,
    Gtk::SelectionData& selection_data, guint info, guint time, int size_index);

  Glib::RefPtr<Gdk::Pixbuf> get_icon(int size_index);

  static const int n_icon_sizes = 5;
  static const int m_icon_size[n_icon_sizes];

  Glib::ustring m_icon_name;

  // Child widgets:
  Gtk::Grid m_grid;
  Gtk::EventBox m_event_box[n_icon_sizes];
  Gtk::Image m_image[n_icon_sizes];
  Gtk::Label m_label[n_icon_sizes];
  Gtk::Label m_description;

}; // end DetailDialog

// Main window.
class Example_IconBrowser : public Gtk::Window
{
public:
  Example_IconBrowser();
  ~Example_IconBrowser() override;

protected:
  // Signal handlers:
  bool on_window_key_press_event(GdkEventKey* event);
  void on_symbolic_radio_toggled();
  void on_context_list_selected_rows_changed();
  void on_icon_view_item_activated(const Gtk::TreeModel::Path& path);
  bool on_icon_view_query_tooltip(int x, int y,
    bool keyboard_tooltip, const Glib::RefPtr<Gtk::Tooltip>& tooltip);
  void on_search_bar_search_mode_enabled_changed();
  void on_search_entry_search_changed();

  void populate();
  void add_context(const Glib::ustring& id,
    const Glib::ustring& name, const Glib::ustring& description);
  void add_icon(const Glib::ustring& name,
    const Glib::ustring& description, const Glib::ustring& context_id);
  bool is_icon_visible(const Gtk::TreeModel::const_iterator& iter) const;

  // Child widgets, header bar:
  Gtk::HeaderBar m_header;
  Gtk::Box m_header_radio_button_box;
  Gtk::RadioButton m_normal_radio;
  Gtk::RadioButton m_symbolic_radio;
  Gtk::ToggleButton m_search_button;

  Glib::RefPtr<Gtk::SizeGroup> m_button_size_group;

  // Child widgets, main part of window:
  Gtk::Box m_hbox;
  Gtk::ListBox m_context_list;
  Gtk::Separator m_vseparator;
  Gtk::Box m_vcontent_box;
  Gtk::SearchBar m_search_bar;
  Gtk::SearchEntry m_search_entry;
  Gtk::ScrolledWindow m_scrolled_window;
  Gtk::IconView m_icon_view;

  Gtk::CellRendererPixbuf m_icon_cell;
  Gtk::CellRendererText m_text_cell;

  // Dialog box, showing icon details:
  DetailDialog m_details;

  Glib::RefPtr<Glib::Binding> m_binding_search_button_search_entry;
  Glib::ustring m_current_context_id;
  Glib::RefPtr<Gtk::TreeModelFilter> m_filter_model;
  Glib::RefPtr<IconInfoStore> m_store;

}; // end Example_IconBrowser


Gtk::Window* do_iconbrowser()
{
  return new Example_IconBrowser();
}

// Definition of main window methods.
Example_IconBrowser::Example_IconBrowser()
:
  m_header_radio_button_box(Gtk::Orientation::HORIZONTAL, 0),
  m_normal_radio("Normal"),
  m_symbolic_radio("Symbolic"),
  m_hbox(Gtk::Orientation::HORIZONTAL, 0),
  m_vseparator(Gtk::Orientation::VERTICAL),
  m_vcontent_box(Gtk::Orientation::VERTICAL, 0),
  m_details(*this)
{
  //set_title("Icon Browser"); // Not shown when header bar is shown
  set_default_size(600, 800);

  m_store = IconInfoStore::create();
  m_filter_model = Gtk::TreeModelFilter::create(m_store);

  // The header bar.
  set_titlebar(m_header);
  m_header.set_title("Icon Browser");
  m_header.set_show_close_button(true);
  m_header.pack_end(m_search_button);
  m_search_button.set_image_from_icon_name("edit-find-symbolic", Gtk::BuiltinIconSize::MENU);
  m_header.pack_end(m_header_radio_button_box);
  m_header_radio_button_box.pack_start(m_normal_radio);
  m_header_radio_button_box.pack_start(m_symbolic_radio);
  m_normal_radio.set_draw_indicator(false); // Make it look as a normal button
  m_symbolic_radio.set_draw_indicator(false);
  m_symbolic_radio.join_group(m_normal_radio);

  m_button_size_group = Gtk::SizeGroup::create(Gtk::SizeGroup::Mode::VERTICAL);
  m_button_size_group->add_widget(m_normal_radio);
  m_button_size_group->add_widget(m_symbolic_radio);
  m_button_size_group->add_widget(m_search_button);

  // Main part of the window.
  add(m_hbox);
  m_hbox.pack_start(m_context_list, Gtk::PACK_SHRINK);
  m_context_list.set_selection_mode(Gtk::SelectionMode::SINGLE);
  m_hbox.pack_start(m_vseparator, Gtk::PACK_SHRINK);
  m_hbox.pack_start(m_vcontent_box);
  m_vcontent_box.pack_start(m_search_bar, Gtk::PACK_SHRINK);
  m_search_bar.add(m_search_entry);
  // The search bar is shown when the search toggle button is pressed.
  m_binding_search_button_search_entry = Glib::Binding::bind_property(
    m_search_button.property_active(),
    m_search_bar.property_search_mode_enabled(),
    Glib::BindingFlags::BIDIRECTIONAL);
  m_vcontent_box.pack_start(m_scrolled_window);
  m_scrolled_window.set_policy(Gtk::PolicyType::NEVER, Gtk::PolicyType::AUTOMATIC);
  m_scrolled_window.add(m_icon_view);
  m_icon_view.set_model(m_filter_model);
  m_icon_view.set_selection_mode(Gtk::SelectionMode::NONE);
  m_icon_view.set_activate_on_single_click(true);
  m_icon_view.pack_start(m_icon_cell);
  m_icon_view.pack_start(m_text_cell);
  m_icon_cell.set_padding(10, 10);
  m_icon_cell.property_stock_size() = static_cast<guint>(Gtk::BuiltinIconSize::DND);
  m_text_cell.set_padding(10, 10);
  m_text_cell.set_alignment(0.5, 0.5);

  // Enable dragging an icon name, and copying it to another program.
  m_icon_view.enable_model_drag_source(
    std::vector<Gtk::TargetEntry>(), Gdk::ModifierType::BUTTON1_MASK, Gdk::DragAction::COPY);
  m_icon_view.drag_source_add_text_targets();

  m_icon_view.set_has_tooltip(true);
  m_filter_model->set_visible_func(
    sigc::mem_fun(*this, &Example_IconBrowser::is_icon_visible));

  // Connect signal handlers.
  signal_key_press_event().connect(
    sigc::mem_fun(*this, &Example_IconBrowser::on_window_key_press_event), false);
  m_symbolic_radio.signal_toggled().connect(
    sigc::mem_fun(*this, &Example_IconBrowser::on_symbolic_radio_toggled));
  m_context_list.signal_selected_rows_changed().connect(
    sigc::mem_fun(*this, &Example_IconBrowser::on_context_list_selected_rows_changed));
  m_icon_view.signal_item_activated().connect(
    sigc::mem_fun(*this, &Example_IconBrowser::on_icon_view_item_activated));<commit_msg>Update for enum now in class: Glib::Binding::Flags.<commit_after>/* Icon Browser
 *
 * Demonstrates many named icons.
 *
 * This demo shows Gtk::HeaderBar, Gtk::ListBox, Gtk::IconView and
 * Gtk::SearchBar in action, but its primary aim is to show an assortment
 * of named icons.
 *
 * Click on an icon to see it in different sizes.
 *
 * Press the search button and enter a search string, to show icons
 * whose names match the search string.
 *
 * You can drag icons to other programs. Either drag an icon's name from
 * the icon view, or drag an image from the dialog box that opens when you
 * click on an icon.
 */

#include <gtkmm.h>
#include <string>
#include <sstream>
#include <iostream>

// Labels in the ListBox with icon context names.
// The icon context ids in this program are not the same as in the Icon Naming Specification
// at http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html.
class IconContextLabel : public Gtk::Label
{
public:
  IconContextLabel(const Glib::ustring& id, const Glib::ustring& name)
  : Gtk::Label(name), m_id(id)
  { }

  Glib::ustring get_id() const { return m_id; }

private:
  Glib::ustring m_id;

}; // end IconContextLabel

// A ListStore that stores names, descriptions and context ids of icons.
class IconInfoStore : public Gtk::ListStore
{
public:
  static Glib::RefPtr<IconInfoStore> create();
  ~IconInfoStore() override;

  void set_text_column(bool symbolic)
  { m_text_column = symbolic ? m_columns.symbolic_name : m_columns.name; }

  Gtk::TreeModelColumn<Glib::ustring> get_text_column() const
  { return m_text_column; }

  struct ModelColumns : public Gtk::TreeModelColumnRecord
  {
    Gtk::TreeModelColumn<Glib::ustring> name;
    Gtk::TreeModelColumn<Glib::ustring> symbolic_name;
    Gtk::TreeModelColumn<Glib::ustring> description;
    Gtk::TreeModelColumn<Glib::ustring> context_id;

    ModelColumns()
    {
      add(name);
      add(symbolic_name);
      add(description);
      add(context_id);
    }
  };

  const ModelColumns m_columns;

protected:
  IconInfoStore();

  // Overridden virtual function:
  bool drag_data_get_vfunc(const Gtk::TreeModel::Path& path,
    Gtk::SelectionData& selection_data) const override;

private:
  Gtk::TreeModelColumn<Glib::ustring> m_text_column;

}; // end IconInfoStore

// Dialog box, showing icon details.
class DetailDialog : public Gtk::Dialog
{
public:
  explicit DetailDialog(Gtk::Window& parent);
  ~DetailDialog() override;

  void set_image(const Glib::ustring& icon_name, const Glib::ustring& description);

protected:
  // Signal handler:
  void on_image_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context,
    Gtk::SelectionData& selection_data, guint info, guint time, int size_index);

  Glib::RefPtr<Gdk::Pixbuf> get_icon(int size_index);

  static const int n_icon_sizes = 5;
  static const int m_icon_size[n_icon_sizes];

  Glib::ustring m_icon_name;

  // Child widgets:
  Gtk::Grid m_grid;
  Gtk::EventBox m_event_box[n_icon_sizes];
  Gtk::Image m_image[n_icon_sizes];
  Gtk::Label m_label[n_icon_sizes];
  Gtk::Label m_description;

}; // end DetailDialog

// Main window.
class Example_IconBrowser : public Gtk::Window
{
public:
  Example_IconBrowser();
  ~Example_IconBrowser() override;

protected:
  // Signal handlers:
  bool on_window_key_press_event(GdkEventKey* event);
  void on_symbolic_radio_toggled();
  void on_context_list_selected_rows_changed();
  void on_icon_view_item_activated(const Gtk::TreeModel::Path& path);
  bool on_icon_view_query_tooltip(int x, int y,
    bool keyboard_tooltip, const Glib::RefPtr<Gtk::Tooltip>& tooltip);
  void on_search_bar_search_mode_enabled_changed();
  void on_search_entry_search_changed();

  void populate();
  void add_context(const Glib::ustring& id,
    const Glib::ustring& name, const Glib::ustring& description);
  void add_icon(const Glib::ustring& name,
    const Glib::ustring& description, const Glib::ustring& context_id);
  bool is_icon_visible(const Gtk::TreeModel::const_iterator& iter) const;

  // Child widgets, header bar:
  Gtk::HeaderBar m_header;
  Gtk::Box m_header_radio_button_box;
  Gtk::RadioButton m_normal_radio;
  Gtk::RadioButton m_symbolic_radio;
  Gtk::ToggleButton m_search_button;

  Glib::RefPtr<Gtk::SizeGroup> m_button_size_group;

  // Child widgets, main part of window:
  Gtk::Box m_hbox;
  Gtk::ListBox m_context_list;
  Gtk::Separator m_vseparator;
  Gtk::Box m_vcontent_box;
  Gtk::SearchBar m_search_bar;
  Gtk::SearchEntry m_search_entry;
  Gtk::ScrolledWindow m_scrolled_window;
  Gtk::IconView m_icon_view;

  Gtk::CellRendererPixbuf m_icon_cell;
  Gtk::CellRendererText m_text_cell;

  // Dialog box, showing icon details:
  DetailDialog m_details;

  Glib::RefPtr<Glib::Binding> m_binding_search_button_search_entry;
  Glib::ustring m_current_context_id;
  Glib::RefPtr<Gtk::TreeModelFilter> m_filter_model;
  Glib::RefPtr<IconInfoStore> m_store;

}; // end Example_IconBrowser


Gtk::Window* do_iconbrowser()
{
  return new Example_IconBrowser();
}

// Definition of main window methods.
Example_IconBrowser::Example_IconBrowser()
:
  m_header_radio_button_box(Gtk::Orientation::HORIZONTAL, 0),
  m_normal_radio("Normal"),
  m_symbolic_radio("Symbolic"),
  m_hbox(Gtk::Orientation::HORIZONTAL, 0),
  m_vseparator(Gtk::Orientation::VERTICAL),
  m_vcontent_box(Gtk::Orientation::VERTICAL, 0),
  m_details(*this)
{
  //set_title("Icon Browser"); // Not shown when header bar is shown
  set_default_size(600, 800);

  m_store = IconInfoStore::create();
  m_filter_model = Gtk::TreeModelFilter::create(m_store);

  // The header bar.
  set_titlebar(m_header);
  m_header.set_title("Icon Browser");
  m_header.set_show_close_button(true);
  m_header.pack_end(m_search_button);
  m_search_button.set_image_from_icon_name("edit-find-symbolic", Gtk::BuiltinIconSize::MENU);
  m_header.pack_end(m_header_radio_button_box);
  m_header_radio_button_box.pack_start(m_normal_radio);
  m_header_radio_button_box.pack_start(m_symbolic_radio);
  m_normal_radio.set_draw_indicator(false); // Make it look as a normal button
  m_symbolic_radio.set_draw_indicator(false);
  m_symbolic_radio.join_group(m_normal_radio);

  m_button_size_group = Gtk::SizeGroup::create(Gtk::SizeGroup::Mode::VERTICAL);
  m_button_size_group->add_widget(m_normal_radio);
  m_button_size_group->add_widget(m_symbolic_radio);
  m_button_size_group->add_widget(m_search_button);

  // Main part of the window.
  add(m_hbox);
  m_hbox.pack_start(m_context_list, Gtk::PACK_SHRINK);
  m_context_list.set_selection_mode(Gtk::SelectionMode::SINGLE);
  m_hbox.pack_start(m_vseparator, Gtk::PACK_SHRINK);
  m_hbox.pack_start(m_vcontent_box);
  m_vcontent_box.pack_start(m_search_bar, Gtk::PACK_SHRINK);
  m_search_bar.add(m_search_entry);
  // The search bar is shown when the search toggle button is pressed.
  m_binding_search_button_search_entry = Glib::Binding::bind_property(
    m_search_button.property_active(),
    m_search_bar.property_search_mode_enabled(),
    Glib::Binding::Flags::BIDIRECTIONAL);
  m_vcontent_box.pack_start(m_scrolled_window);
  m_scrolled_window.set_policy(Gtk::PolicyType::NEVER, Gtk::PolicyType::AUTOMATIC);
  m_scrolled_window.add(m_icon_view);
  m_icon_view.set_model(m_filter_model);
  m_icon_view.set_selection_mode(Gtk::SelectionMode::NONE);
  m_icon_view.set_activate_on_single_click(true);
  m_icon_view.pack_start(m_icon_cell);
  m_icon_view.pack_start(m_text_cell);
  m_icon_cell.set_padding(10, 10);
  m_icon_cell.property_stock_size() = static_cast<guint>(Gtk::BuiltinIconSize::DND);
  m_text_cell.set_padding(10, 10);
  m_text_cell.set_alignment(0.5, 0.5);

  // Enable dragging an icon name, and copying it to another program.
  m_icon_view.enable_model_drag_source(
    std::vector<Gtk::TargetEntry>(), Gdk::ModifierType::BUTTON1_MASK, Gdk::DragAction::COPY);
  m_icon_view.drag_source_add_text_targets();

  m_icon_view.set_has_tooltip(true);
  m_filter_model->set_visible_func(
    sigc::mem_fun(*this, &Example_IconBrowser::is_icon_visible));

  // Connect signal handlers.
  signal_key_press_event().connect(
    sigc::mem_fun(*this, &Example_IconBrowser::on_window_key_press_event), false);
  m_symbolic_radio.signal_toggled().connect(
    sigc::mem_fun(*this, &Example_IconBrowser::on_symbolic_radio_toggled));
  m_context_list.signal_selected_rows_changed().connect(
    sigc::mem_fun(*this, &Example_IconBrowser::on_context_list_selected_rows_changed));
  m_icon_view.signal_item_activated().connect(
    sigc::mem_fun(*this, &Example_IconBrowser::on_icon_view_item_activated));
{'fraction_non_alphanumeric': 0.11211340206185567, 'fraction_numerical': 0.0026944704779756326, 'mean_word_length': 6.090116279069767, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '113613', 'n_tokens_mistral': 6166, 'n_tokens_neox': 6033, 'n_words': 1117}