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)
|
PopUpCommon::PopUpCommon (Gtk::Button* thisButton, const Glib::ustring& label)
|
||||||
: buttonImage (nullptr)
|
: buttonImage (nullptr)
|
||||||
, menu (nullptr)
|
, menu(new Gtk::Menu())
|
||||||
, selected (-1) // -1 means that the button is invalid
|
, selected (-1) // -1 means that the button is invalid
|
||||||
{
|
{
|
||||||
button = thisButton;
|
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);
|
setExpandAlignProperties(buttonGroup, true, false, Gtk::ALIGN_FILL, Gtk::ALIGN_CENTER);
|
||||||
buttonGroup->attach(*button, 0, 0, 1, 1);
|
buttonGroup->attach(*button, 0, 0, 1, 1);
|
||||||
buttonGroup->get_style_context()->add_class("image-combo");
|
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 ()
|
PopUpCommon::~PopUpCommon ()
|
||||||
{
|
{
|
||||||
delete menu;
|
|
||||||
delete buttonImage;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PopUpCommon::addEntry (const Glib::ustring& fileName, const Glib::ustring& label)
|
bool PopUpCommon::addEntry (const Glib::ustring& fileName, const Glib::ustring& label)
|
||||||
{
|
{
|
||||||
if (label.empty ())
|
return insertEntry(getEntryCount(), fileName, label);
|
||||||
return false;
|
}
|
||||||
|
|
||||||
|
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
|
// Create the menu item and image
|
||||||
MyImageMenuItem* newItem = Gtk::manage (new MyImageMenuItem (label, fileName));
|
MyImageMenuItem *newItem = Gtk::make_managed<MyImageMenuItem>(label, image);
|
||||||
imageFilenames.push_back (fileName);
|
imageIcons.insert(imageIcons.begin() + position, gIcon);
|
||||||
images.push_back (newItem->getImage ());
|
imageFilenames.insert(imageFilenames.begin() + position, fileName);
|
||||||
|
images.insert(images.begin() + position, 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// When there is at least 1 choice, we add the arrow button
|
// When there is at least 1 choice, we add the arrow button
|
||||||
if (images.size() == 1) {
|
if (images.size() == 1) {
|
||||||
Gtk::Button* arrowButton = Gtk::manage( new Gtk::Button() );
|
changeImage(fileName, gIcon);
|
||||||
Gtk::Image *arrowImage = Gtk::manage(new Gtk::Image());
|
buttonImage->show();
|
||||||
arrowImage->set_from_icon_name("pan-down-symbolic", Gtk::ICON_SIZE_BUTTON);
|
selected = 0;
|
||||||
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) );
|
|
||||||
button->get_style_context()->add_class("Left");
|
button->get_style_context()->add_class("Left");
|
||||||
arrowButton->get_style_context()->add_class("Right");
|
arrowButton->show();
|
||||||
arrowButton->get_style_context()->add_class("popupbutton-arrow");
|
|
||||||
hasMenu = true;
|
hasMenu = true;
|
||||||
|
} else if (position <= selected) {
|
||||||
|
selected++;
|
||||||
}
|
}
|
||||||
|
|
||||||
newItem->signal_activate ().connect (sigc::bind (sigc::mem_fun (*this, &PopUpCommon::entrySelected), images.size () - 1));
|
void (PopUpCommon::*entrySelectedFunc)(Gtk::Widget *) = &PopUpCommon::entrySelected;
|
||||||
menu->append (*newItem);
|
newItem->signal_activate ().connect (sigc::bind (sigc::mem_fun (*this, entrySelectedFunc), newItem));
|
||||||
|
menu->insert(*newItem, position);
|
||||||
return true;
|
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
|
// Emit a signal if the selected item has changed
|
||||||
if (setSelected (posToIndex(i)))
|
if (setSelected (posToIndex(i)))
|
||||||
@@ -130,7 +219,7 @@ bool PopUpCommon::setSelected (int entryNum)
|
|||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
// Maybe we could do something better than loading the image file each time the selection is changed !?
|
// 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;
|
selected = entryNum;
|
||||||
setButtonHint();
|
setButtonHint();
|
||||||
return true;
|
return true;
|
||||||
|
@@ -20,12 +20,19 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "glibmm/refptr.h"
|
||||||
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <glibmm/ustring.h>
|
#include <glibmm/ustring.h>
|
||||||
|
|
||||||
#include <sigc++/signal.h>
|
#include <sigc++/signal.h>
|
||||||
|
|
||||||
|
namespace Gio
|
||||||
|
{
|
||||||
|
class Icon;
|
||||||
|
}
|
||||||
|
|
||||||
namespace Gtk
|
namespace Gtk
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -33,6 +40,7 @@ class Grid;
|
|||||||
class Menu;
|
class Menu;
|
||||||
class Button;
|
class Button;
|
||||||
class ImageMenuItem;
|
class ImageMenuItem;
|
||||||
|
class Widget;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,9 +61,12 @@ public:
|
|||||||
explicit PopUpCommon (Gtk::Button* button, const Glib::ustring& label = "");
|
explicit PopUpCommon (Gtk::Button* button, const Glib::ustring& label = "");
|
||||||
virtual ~PopUpCommon ();
|
virtual ~PopUpCommon ();
|
||||||
bool addEntry (const Glib::ustring& fileName, const Glib::ustring& label);
|
bool addEntry (const Glib::ustring& fileName, const Glib::ustring& label);
|
||||||
|
bool insertEntry(int position, const Glib::ustring& fileName, const Glib::ustring& label);
|
||||||
|
bool insertEntry(int position, const Glib::RefPtr<const Gio::Icon>& gIcon, const Glib::ustring& label);
|
||||||
int getEntryCount () const;
|
int getEntryCount () const;
|
||||||
bool setSelected (int entryNum);
|
bool setSelected (int entryNum);
|
||||||
int getSelected () const;
|
int getSelected () const;
|
||||||
|
void removeEntry(int position);
|
||||||
void setButtonHint();
|
void setButtonHint();
|
||||||
void show ();
|
void show ();
|
||||||
void set_tooltip_text (const Glib::ustring &text);
|
void set_tooltip_text (const Glib::ustring &text);
|
||||||
@@ -65,16 +76,22 @@ private:
|
|||||||
type_signal_changed messageChanged;
|
type_signal_changed messageChanged;
|
||||||
type_signal_item_selected messageItemSelected;
|
type_signal_item_selected messageItemSelected;
|
||||||
|
|
||||||
|
std::vector<Glib::RefPtr<const Gio::Icon>> imageIcons;
|
||||||
std::vector<Glib::ustring> imageFilenames;
|
std::vector<Glib::ustring> imageFilenames;
|
||||||
std::vector<const RTImage*> images;
|
std::vector<const RTImage*> images;
|
||||||
Glib::ustring buttonHint;
|
Glib::ustring buttonHint;
|
||||||
RTImage* buttonImage;
|
RTImage* buttonImage;
|
||||||
Gtk::Grid* imageContainer;
|
Gtk::Grid* imageContainer;
|
||||||
Gtk::Menu* menu;
|
std::unique_ptr<Gtk::Menu> menu;
|
||||||
Gtk::Button* button;
|
Gtk::Button* button;
|
||||||
|
Gtk::Button* arrowButton;
|
||||||
int selected;
|
int selected;
|
||||||
bool hasMenu;
|
bool hasMenu;
|
||||||
|
|
||||||
|
void changeImage(int position);
|
||||||
|
void changeImage(const Glib::ustring& fileName, const Glib::RefPtr<const Gio::Icon>& gIcon);
|
||||||
|
void entrySelected(Gtk::Widget* menuItem);
|
||||||
|
bool insertEntryImpl(int position, const Glib::ustring& fileName, const Glib::RefPtr<const Gio::Icon>& gIcon, RTImage* image, const Glib::ustring& label);
|
||||||
void showMenu(GdkEventButton* event);
|
void showMenu(GdkEventButton* event);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
Reference in New Issue
Block a user