1 /*
2  *  This file is part of RawTherapee.
3  *
4  *  Copyright (c) 2004-2010 Gabor Horvath <hgabor@rawtherapee.com>
5  *
6  *  RawTherapee is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  RawTherapee is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with RawTherapee.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *  Class created by Jean-Christophe FRISCH, aka 'Hombre'
20  */
21 
22 #include <gtkmm.h>
23 #include "multilangmgr.h"
24 #include "popupcommon.h"
25 #include "rtimage.h"
26 #include "guiutils.h"
27 
PopUpCommon(Gtk::Button * thisButton,const Glib::ustring & label)28 PopUpCommon::PopUpCommon (Gtk::Button* thisButton, const Glib::ustring& label)
29     : buttonImage (nullptr)
30     , menu (nullptr)
31     , arrowButton(nullptr)
32     , selected (-1) // -1 means that the button is invalid
33 {
34     button = thisButton;
35     hasMenu = false;
36     imageContainer = Gtk::manage( new Gtk::Grid());
37     setExpandAlignProperties(imageContainer, true, false, Gtk::ALIGN_FILL, Gtk::ALIGN_CENTER);
38     button->set_relief (Gtk::RELIEF_NORMAL);
39     button->add(*imageContainer);
40 
41     if (!label.empty()) {
42         Gtk::Label* buttonLabel = Gtk::manage ( new Gtk::Label(label + " ") );
43         setExpandAlignProperties(buttonLabel, false, false, Gtk::ALIGN_START, Gtk::ALIGN_CENTER);
44         imageContainer->attach(*buttonLabel, 0, 0, 1, 1);
45     }
46 
47     // Create the global container and put the button in it
48     buttonGroup = Gtk::manage( new Gtk::Grid());
49     setExpandAlignProperties(buttonGroup, true, false, Gtk::ALIGN_FILL, Gtk::ALIGN_CENTER);
50     buttonGroup->attach(*button, 0, 0, 1, 1);
51     buttonGroup->get_style_context()->add_class("image-combo");
52 }
53 
~PopUpCommon()54 PopUpCommon::~PopUpCommon ()
55 {
56     delete menu;
57     delete buttonImage;
58 }
59 
addEntry(const Glib::ustring & fileName,const Glib::ustring & label)60 bool PopUpCommon::addEntry (const Glib::ustring& fileName, const Glib::ustring& label)
61 {
62     if (label.empty ())
63          return false;
64 
65     // Create the menu item and image
66     MyImageMenuItem* newItem = Gtk::manage (new MyImageMenuItem (label, fileName));
67     imageFilenames.push_back (fileName);
68     images.push_back (newItem->getImage ());
69 
70     if (selected == -1) {
71         // Create the menu on the first item
72         menu = new Gtk::Menu ();
73         // Create the image for the button
74         buttonImage = new RTImage(fileName);
75         setExpandAlignProperties(buttonImage, true, false, Gtk::ALIGN_FILL, Gtk::ALIGN_CENTER);
76         // Use the first image by default
77         imageContainer->attach_next_to(*buttonImage, Gtk::POS_RIGHT, 1, 1);
78         selected = 0;
79     }
80 
81     // When there is at least 1 choice, we add the arrow button
82     if (images.size() == 1) {
83         arrowButton = Gtk::manage( new Gtk::Button() );
84         Gtk::Image *arrowImage = Gtk::manage(new Gtk::Image());
85         arrowImage->set_from_icon_name("pan-down-symbolic", Gtk::ICON_SIZE_BUTTON);
86         setExpandAlignProperties(arrowButton, false, false, Gtk::ALIGN_CENTER, Gtk::ALIGN_FILL);
87         arrowButton->add(*arrowImage); //menuSymbol);
88         buttonGroup->attach_next_to(*arrowButton, *button, Gtk::POS_RIGHT, 1, 1);
89         arrowButton->signal_button_release_event().connect_notify( sigc::mem_fun(*this, &PopUpCommon::showMenu) );
90         button->get_style_context()->add_class("Left");
91         arrowButton->get_style_context()->add_class("Right");
92         arrowButton->get_style_context()->add_class("popupbutton-arrow");
93         hasMenu = true;
94     }
95 
96     newItem->signal_activate ().connect (sigc::bind (sigc::mem_fun (*this, &PopUpCommon::entrySelected), images.size () - 1));
97     menu->append (*newItem);
98 
99     return true;
100 }
101 
102 // TODO: 'PopUpCommon::removeEntry' method to be created...
103 
entrySelected(int i)104 void PopUpCommon::entrySelected (int i)
105 {
106     // Emit a signal if the selected item has changed
107     if (setSelected (posToIndex(i)))
108         messageChanged (posToIndex(selected));
109 
110     // Emit a signal in all case (i.e. propagate the signal_activate event)
111     messageItemSelected (posToIndex(selected));
112 }
113 
setItemSensitivity(int index,bool isSensitive)114 void PopUpCommon::setItemSensitivity (int index, bool isSensitive) {
115     const auto items = menu->get_children ();
116     size_t pos = indexToPos(index);
117     if (pos < items.size ()) {
118         items[pos]->set_sensitive (isSensitive);
119     }
120 }
121 
122 
123 /*
124  * Set the button image with the selected item
125  */
setSelected(int entryNum)126 bool PopUpCommon::setSelected (int entryNum)
127 {
128     entryNum = indexToPos(entryNum);
129 
130     if (entryNum < 0 || entryNum > ((int)images.size() - 1) || (int)entryNum == selected) {
131         return false;
132     } else {
133         // Maybe we could do something better than loading the image file each time the selection is changed !?
134         buttonImage->changeImage(imageFilenames.at(entryNum));
135         selected = entryNum;
136         setButtonHint();
137         return true;
138     }
139 }
140 
show()141 void PopUpCommon::show()
142 {
143     menu->reposition();
144     setButtonHint();
145     menu->show_all();
146     buttonGroup->show_all();
147 }
148 
setButtonHint()149 void PopUpCommon::setButtonHint()
150 {
151     Glib::ustring hint;
152 
153     if (!buttonHint.empty()) {
154         hint = buttonHint;
155 
156         if (selected > -1) {
157             hint += " ";
158         }
159     }
160 
161     if (selected > -1) {
162         auto widget = menu->get_children ()[selected];
163         auto item = dynamic_cast<MyImageMenuItem*>(widget);
164 
165         if (item) {
166             hint += item->getLabel ()->get_text ();
167         }
168     }
169 
170     button->set_tooltip_markup(hint);
171 }
172 
showMenu(GdkEventButton * event)173 void PopUpCommon::showMenu(GdkEventButton* event)
174 {
175     if (event->button == 1) {
176         menu->popup(event->button, event->time);
177     }
178 }
179 
set_tooltip_text(const Glib::ustring & text)180 void PopUpCommon::set_tooltip_text (const Glib::ustring &text)
181 {
182     buttonHint = text;
183     setButtonHint();
184 }
185 
186 
setRelief(Gtk::ReliefStyle s)187 void PopUpCommon::setRelief(Gtk::ReliefStyle s)
188 {
189     button->set_relief(s);
190     if (arrowButton) {
191         arrowButton->set_relief(s);
192     }
193 }
194