Improve pop-up common with gicons and item ops
Add ability to use gicon images, insert pop-up entries in any location, and remove entries.
This commit is contained in:
@@ -27,7 +27,7 @@
|
||||
|
||||
PopUpCommon::PopUpCommon (Gtk::Button* thisButton, const Glib::ustring& label)
|
||||
: buttonImage (nullptr)
|
||||
, menu (nullptr)
|
||||
, menu(new Gtk::Menu())
|
||||
, selected (-1) // -1 means that the button is invalid
|
||||
{
|
||||
button = thisButton;
|
||||
@@ -48,59 +48,148 @@ PopUpCommon::PopUpCommon (Gtk::Button* thisButton, const Glib::ustring& label)
|
||||
setExpandAlignProperties(buttonGroup, true, false, Gtk::ALIGN_FILL, Gtk::ALIGN_CENTER);
|
||||
buttonGroup->attach(*button, 0, 0, 1, 1);
|
||||
buttonGroup->get_style_context()->add_class("image-combo");
|
||||
|
||||
// Create the image for the button
|
||||
buttonImage = Gtk::make_managed<RTImage>();
|
||||
setExpandAlignProperties(buttonImage, true, false, Gtk::ALIGN_FILL, Gtk::ALIGN_CENTER);
|
||||
imageContainer->attach_next_to(*buttonImage, Gtk::POS_RIGHT, 1, 1);
|
||||
buttonImage->set_no_show_all();
|
||||
|
||||
// Create the button for showing the pop-up.
|
||||
arrowButton = Gtk::make_managed<Gtk::Button>();
|
||||
Gtk::Image *arrowImage = Gtk::make_managed<Gtk::Image>();
|
||||
arrowImage->set_from_icon_name("pan-down-symbolic", Gtk::ICON_SIZE_BUTTON);
|
||||
setExpandAlignProperties(arrowButton, false, false, Gtk::ALIGN_CENTER, Gtk::ALIGN_FILL);
|
||||
arrowButton->add(*arrowImage); //menuSymbol);
|
||||
arrowImage->show();
|
||||
buttonGroup->attach_next_to(*arrowButton, *button, Gtk::POS_RIGHT, 1, 1);
|
||||
arrowButton->signal_button_release_event().connect_notify(sigc::mem_fun(*this, &PopUpCommon::showMenu));
|
||||
arrowButton->get_style_context()->add_class("Right");
|
||||
arrowButton->get_style_context()->add_class("popupbutton-arrow");
|
||||
arrowButton->set_no_show_all();
|
||||
}
|
||||
|
||||
PopUpCommon::~PopUpCommon ()
|
||||
{
|
||||
delete menu;
|
||||
delete buttonImage;
|
||||
}
|
||||
|
||||
bool PopUpCommon::addEntry (const Glib::ustring& fileName, const Glib::ustring& label)
|
||||
{
|
||||
if (label.empty ())
|
||||
return false;
|
||||
return insertEntry(getEntryCount(), fileName, label);
|
||||
}
|
||||
|
||||
bool PopUpCommon::insertEntry(int position, const Glib::ustring& fileName, const Glib::ustring& label)
|
||||
{
|
||||
RTImage* image = nullptr;
|
||||
if (!fileName.empty()) {
|
||||
image = Gtk::make_managed<RTImage>(fileName);
|
||||
}
|
||||
bool success = insertEntryImpl(position, fileName, Glib::RefPtr<const Gio::Icon>(), image, label);
|
||||
if (!success && image) {
|
||||
delete image;
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
bool PopUpCommon::insertEntry(int position, const Glib::RefPtr<const Gio::Icon>& gIcon, const Glib::ustring& label)
|
||||
{
|
||||
RTImage* image = Gtk::make_managed<RTImage>(gIcon, Gtk::ICON_SIZE_BUTTON);
|
||||
bool success = insertEntryImpl(position, "", gIcon, image, label);
|
||||
if (!success) {
|
||||
delete image;
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
bool PopUpCommon::insertEntryImpl(int position, const Glib::ustring& fileName, const Glib::RefPtr<const Gio::Icon>& gIcon, RTImage* image, const Glib::ustring& label)
|
||||
{
|
||||
if (label.empty() || position < 0 || position > getEntryCount())
|
||||
return false;
|
||||
|
||||
// Create the menu item and image
|
||||
MyImageMenuItem* newItem = Gtk::manage (new MyImageMenuItem (label, fileName));
|
||||
imageFilenames.push_back (fileName);
|
||||
images.push_back (newItem->getImage ());
|
||||
|
||||
if (selected == -1) {
|
||||
// Create the menu on the first item
|
||||
menu = new Gtk::Menu ();
|
||||
// Create the image for the button
|
||||
buttonImage = new RTImage(fileName);
|
||||
setExpandAlignProperties(buttonImage, true, false, Gtk::ALIGN_FILL, Gtk::ALIGN_CENTER);
|
||||
// Use the first image by default
|
||||
imageContainer->attach_next_to(*buttonImage, Gtk::POS_RIGHT, 1, 1);
|
||||
selected = 0;
|
||||
}
|
||||
MyImageMenuItem *newItem = Gtk::make_managed<MyImageMenuItem>(label, image);
|
||||
imageIcons.insert(imageIcons.begin() + position, gIcon);
|
||||
imageFilenames.insert(imageFilenames.begin() + position, fileName);
|
||||
images.insert(images.begin() + position, newItem->getImage());
|
||||
|
||||
// When there is at least 1 choice, we add the arrow button
|
||||
if (images.size() == 1) {
|
||||
Gtk::Button* arrowButton = Gtk::manage( new Gtk::Button() );
|
||||
Gtk::Image *arrowImage = Gtk::manage(new Gtk::Image());
|
||||
arrowImage->set_from_icon_name("pan-down-symbolic", Gtk::ICON_SIZE_BUTTON);
|
||||
setExpandAlignProperties(arrowButton, false, false, Gtk::ALIGN_CENTER, Gtk::ALIGN_FILL);
|
||||
arrowButton->add(*arrowImage); //menuSymbol);
|
||||
buttonGroup->attach_next_to(*arrowButton, *button, Gtk::POS_RIGHT, 1, 1);
|
||||
arrowButton->signal_button_release_event().connect_notify( sigc::mem_fun(*this, &PopUpCommon::showMenu) );
|
||||
changeImage(fileName, gIcon);
|
||||
buttonImage->show();
|
||||
selected = 0;
|
||||
button->get_style_context()->add_class("Left");
|
||||
arrowButton->get_style_context()->add_class("Right");
|
||||
arrowButton->get_style_context()->add_class("popupbutton-arrow");
|
||||
arrowButton->show();
|
||||
hasMenu = true;
|
||||
} else if (position <= selected) {
|
||||
selected++;
|
||||
}
|
||||
|
||||
newItem->signal_activate ().connect (sigc::bind (sigc::mem_fun (*this, &PopUpCommon::entrySelected), images.size () - 1));
|
||||
menu->append (*newItem);
|
||||
|
||||
void (PopUpCommon::*entrySelectedFunc)(Gtk::Widget *) = &PopUpCommon::entrySelected;
|
||||
newItem->signal_activate ().connect (sigc::bind (sigc::mem_fun (*this, entrySelectedFunc), newItem));
|
||||
menu->insert(*newItem, position);
|
||||
return true;
|
||||
}
|
||||
|
||||
// TODO: 'PopUpCommon::removeEntry' method to be created...
|
||||
void PopUpCommon::removeEntry(int position)
|
||||
{
|
||||
if (position < 0 || position >= getEntryCount()) {
|
||||
return;
|
||||
}
|
||||
|
||||
void PopUpCommon::entrySelected (int i)
|
||||
if (getEntryCount() == 1) { // Last of the entries.
|
||||
// Hide the arrow button.
|
||||
button->get_style_context()->remove_class("Left");
|
||||
arrowButton->hide();
|
||||
hasMenu = false;
|
||||
// Remove the button image.
|
||||
buttonImage->hide();
|
||||
selected = -1;
|
||||
}
|
||||
else if (position < selected) {
|
||||
selected--;
|
||||
}
|
||||
else if (position == selected) { // Select a different entry before removing.
|
||||
int nextSelection = position + (position == getEntryCount() - 1 ? -1 : 1);
|
||||
changeImage(nextSelection);
|
||||
setButtonHint();
|
||||
}
|
||||
|
||||
MyImageMenuItem *menuItem = dynamic_cast<MyImageMenuItem *>(menu->get_children()[position]);
|
||||
menu->remove(*menuItem);
|
||||
delete menuItem;
|
||||
imageIcons.erase(imageIcons.begin() + position);
|
||||
imageFilenames.erase(imageFilenames.begin() + position);
|
||||
images.erase(images.begin() + position);
|
||||
}
|
||||
|
||||
void PopUpCommon::changeImage(int position)
|
||||
{
|
||||
changeImage(imageFilenames.at(position), imageIcons.at(position));
|
||||
}
|
||||
|
||||
void PopUpCommon::changeImage(const Glib::ustring& fileName, const Glib::RefPtr<const Gio::Icon>& gIcon)
|
||||
{
|
||||
if (!fileName.empty()) {
|
||||
buttonImage->changeImage(fileName);
|
||||
} else {
|
||||
buttonImage->changeImage(gIcon, static_cast<Gtk::IconSize>(Gtk::ICON_SIZE_BUTTON));
|
||||
}
|
||||
}
|
||||
|
||||
void PopUpCommon::entrySelected(Gtk::Widget* widget)
|
||||
{
|
||||
int i = 0;
|
||||
for (const auto & child : menu->get_children()) {
|
||||
if (widget == child) {
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
entrySelected(i);
|
||||
}
|
||||
|
||||
void PopUpCommon::entrySelected(int i)
|
||||
{
|
||||
// Emit a signal if the selected item has changed
|
||||
if (setSelected (posToIndex(i)))
|
||||
@@ -130,7 +219,7 @@ bool PopUpCommon::setSelected (int entryNum)
|
||||
return false;
|
||||
} else {
|
||||
// Maybe we could do something better than loading the image file each time the selection is changed !?
|
||||
buttonImage->changeImage(imageFilenames.at(entryNum));
|
||||
changeImage(entryNum);
|
||||
selected = entryNum;
|
||||
setButtonHint();
|
||||
return true;
|
||||
|
Reference in New Issue
Block a user