Inkscape
Vector Graphics Editor
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages Concepts
popover-menu-item.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
5/*
6 * Authors:
7 * Daniel Boles <dboles.src+inkscape@gmail.com>
8 *
9 * Copyright (C) 2023 Daniel Boles
10 *
11 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
12 */
13
15
16#include <pangomm/layout.h>
17#include <giomm/themedicon.h>
18#include <gtkmm/box.h>
19#include <gtkmm/image.h>
20#include <gtkmm/label.h>
21#include <gtkmm/tooltip.h>
22
23#include "ui/util.h"
25
26namespace Inkscape::UI::Widget {
27
28PopoverMenuItem::PopoverMenuItem(Glib::ustring const &text,
29 bool const mnemonic,
30 Glib::ustring const &icon_name,
31 Gtk::IconSize const icon_size,
32 bool const popdown_on_activate)
33 : Glib::ObjectBase{"PopoverMenuItem"}
34 , CssNameClassInit{"menuitem"}
35 , Gtk::Button{}
36{
37 get_style_context()->add_class("menuitem");
38 add_css_class("regular-item");
39 set_has_frame(false);
40
41 Gtk::Image *image = nullptr;
42
43 if (!text.empty()) {
44 _label = Gtk::make_managed<Gtk::Label>(text, Gtk::Align::START, Gtk::Align::CENTER, mnemonic);
45 }
46
47 if (!icon_name.empty()) {
48 image = Gtk::make_managed<Gtk::Image>(Gio::ThemedIcon::create(icon_name));
49 image->set_icon_size(icon_size);
50 }
51
52 if (_label && image) {
53 auto &hbox = *Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 8);
54 hbox.append(*image);
55 hbox.append(*_label);
56 set_child(hbox);
57 } else if (_label) {
58 set_child(*_label);
59 } else if (image) {
60 set_child(*image);
61 }
62
63 if (popdown_on_activate) {
64 signal_activate().connect([this]
65 {
66 if (auto const menu = get_menu()) {
67 menu->popdown();
68 }
69 });
70 }
71}
72
73Glib::SignalProxy<void ()> PopoverMenuItem::signal_activate()
74{
75 return signal_clicked();
76}
77
79{
80 PopoverMenu *result = nullptr;
81 for_each_parent(*this, [&](Gtk::Widget &parent)
82 {
83 if (auto const menu = dynamic_cast<PopoverMenu *>(&parent)) {
84 result = menu;
86 }
88 });
89 return result;
90}
91
92void PopoverMenuItem::set_label(Glib::ustring const &name)
93{
94 if (_label) {
95 _label->set_text(name);
96 } else {
97 _label = Gtk::make_managed<Gtk::Label>(name, Gtk::Align::START, Gtk::Align::CENTER);
98 set_child(*_label);
99 }
100}
101
102} // namespace Inkscape::UI::Widget
103
104/*
105 Local Variables:
106 mode:c++
107 c-file-style:"stroustrup"
108 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
109 indent-tabs-mode:nil
110 fill-column:99
111 End:
112*/
113// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
A class that can be inherited to set the CSS name of a Gtk::Widget subclass.
Glib::SignalProxy< void()> signal_activate()
A convenience, “drop-in” alias for signal_clicked().
PopoverMenuItem(Glib::ustring const &text={}, bool mnemonic=false, Glib::ustring const &icon_name={}, Gtk::IconSize icon_size=Gtk::IconSize::NORMAL, bool popdown_on_activate=true)
void set_label(Glib::ustring const &name)
A replacement for GTK3ʼs Gtk::Menu, as removed in GTK4.
Css & result
static char const *const parent
Definition dir-util.cpp:70
std::unique_ptr< Magick::Image > image
Definition desktop.h:50
Custom widgets.
Definition desktop.h:126
Gtk::Widget * for_each_parent(Gtk::Widget &widget, Func &&func)
Call Func with a reference to successive parents, until Func returns _break.
Definition util.h:137
A replacement for GTK3ʼs Gtk::MenuItem, as removed in GTK4.
A replacement for GTK3ʼs Gtk::Menu, as removed in GTK4.
Glib::ustring name
Definition toolbars.cpp:55