2014-09-02 17:33:29 +03:00
|
|
|
/*
|
|
|
|
|
Copyright (C) 2014 Waves Audio Ltd.
|
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "waves_dropdown.h"
|
|
|
|
|
|
2014-09-03 09:46:26 +03:00
|
|
|
WavesDropdown::WavesDropdown (const std::string& title)
|
|
|
|
|
: WavesIconButton (title)
|
2014-09-29 13:15:37 +03:00
|
|
|
, _selected_item_number (-1)
|
2014-09-02 17:33:29 +03:00
|
|
|
{
|
2014-09-29 13:15:37 +03:00
|
|
|
signal_button_press_event().connect (sigc::mem_fun(*this, &WavesDropdown::_on_mouse_pressed));
|
2014-09-11 12:49:45 +03:00
|
|
|
_menu.signal_hide ().connect (sigc::bind (sigc::mem_fun (*this, &CairoWidget::set_active), false));
|
2014-09-02 17:33:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WavesDropdown::~WavesDropdown ()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-29 13:15:37 +03:00
|
|
|
void
|
|
|
|
|
WavesDropdown::clear_items ()
|
2014-09-02 17:33:29 +03:00
|
|
|
{
|
2014-09-29 13:15:37 +03:00
|
|
|
_menu.items().clear ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
WavesDropdown::set_selected_item (int selected_item_number)
|
|
|
|
|
{
|
|
|
|
|
Gtk::Menu_Helpers::MenuList& items = _menu.items ();
|
|
|
|
|
|
|
|
|
|
if ((selected_item_number < 0) || (selected_item_number >= items.size ())) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-30 16:03:47 +03:00
|
|
|
if (selected_item_number == _selected_item_number) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-29 13:15:37 +03:00
|
|
|
Gtk::Menu_Helpers::MenuList::iterator i = items.begin();
|
2014-09-30 16:03:47 +03:00
|
|
|
std::advance (i, selected_item_number);
|
2014-09-30 22:53:44 +03:00
|
|
|
Gtk::RadioMenuItem* radio_menu_item = dynamic_cast <Gtk::RadioMenuItem*> (&(*i));
|
|
|
|
|
if (radio_menu_item) {
|
|
|
|
|
radio_menu_item->set_active (true);
|
|
|
|
|
}
|
2014-09-29 13:15:37 +03:00
|
|
|
|
2014-09-30 16:03:47 +03:00
|
|
|
_on_menu_item (selected_item_number, (*i).get_data ("waves_dropdown_item_cookie"));
|
2014-09-02 17:33:29 +03:00
|
|
|
}
|
|
|
|
|
|
2014-09-03 10:44:01 +03:00
|
|
|
Gtk::MenuItem&
|
|
|
|
|
WavesDropdown::add_menu_item (const std::string& item, void* cookie)
|
2014-09-02 17:33:29 +03:00
|
|
|
{
|
|
|
|
|
Gtk::Menu_Helpers::MenuList& items = _menu.items ();
|
|
|
|
|
|
2014-09-03 18:14:34 +03:00
|
|
|
items.push_back (Gtk::Menu_Helpers::MenuElem (item, sigc::bind (sigc::mem_fun(*this, &WavesDropdown::_on_menu_item), items.size (), cookie)));
|
2014-09-03 10:44:01 +03:00
|
|
|
|
2014-09-09 07:46:51 +03:00
|
|
|
Gtk::MenuItem& menuitem = _menu.items ().back ();
|
|
|
|
|
ensure_style();
|
|
|
|
|
Widget* child = menuitem.get_child ();
|
|
|
|
|
if (child) {
|
|
|
|
|
child->set_style (get_style());
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-29 13:15:37 +03:00
|
|
|
menuitem.set_data ("waves_dropdown_item_cookie", cookie);
|
|
|
|
|
|
2014-09-09 07:46:51 +03:00
|
|
|
return menuitem;
|
2014-09-02 17:33:29 +03:00
|
|
|
}
|
|
|
|
|
|
2014-09-23 11:57:35 +03:00
|
|
|
Gtk::RadioMenuItem&
|
|
|
|
|
WavesDropdown::add_radio_menu_item (const std::string& item, void* cookie)
|
|
|
|
|
{
|
|
|
|
|
Gtk::Menu_Helpers::MenuList& items = _menu.items ();
|
|
|
|
|
|
|
|
|
|
items.push_back (Gtk::Menu_Helpers::RadioMenuElem (_radio_menu_items_group, item, sigc::bind (sigc::mem_fun(*this, &WavesDropdown::_on_menu_item), items.size (), cookie)));
|
|
|
|
|
|
|
|
|
|
Gtk::RadioMenuItem& menuitem = *dynamic_cast <Gtk::RadioMenuItem*> (&_menu.items ().back ());
|
|
|
|
|
ensure_style();
|
|
|
|
|
Widget* child = menuitem.get_child ();
|
|
|
|
|
if (child) {
|
|
|
|
|
child->set_style (get_style());
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-29 13:15:37 +03:00
|
|
|
menuitem.set_data ("waves_dropdown_item_cookie", cookie);
|
|
|
|
|
|
2014-09-23 11:57:35 +03:00
|
|
|
return menuitem;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-24 15:14:05 +03:00
|
|
|
Gtk::CheckMenuItem&
|
|
|
|
|
WavesDropdown::add_check_menu_item (const std::string& item, void* cookie)
|
|
|
|
|
{
|
|
|
|
|
Gtk::Menu_Helpers::MenuList& items = _menu.items ();
|
|
|
|
|
|
|
|
|
|
items.push_back (Gtk::Menu_Helpers::CheckMenuElem (item, sigc::bind (sigc::mem_fun(*this, &WavesDropdown::_on_menu_item), items.size (), cookie)));
|
|
|
|
|
|
|
|
|
|
Gtk::CheckMenuItem& menuitem = *dynamic_cast <Gtk::CheckMenuItem*> (&_menu.items ().back ());
|
|
|
|
|
ensure_style();
|
|
|
|
|
Widget* child = menuitem.get_child ();
|
|
|
|
|
if (child) {
|
|
|
|
|
child->set_style (get_style());
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-29 13:15:37 +03:00
|
|
|
menuitem.set_data ("waves_dropdown_item_cookie", cookie);
|
|
|
|
|
|
2014-09-24 15:14:05 +03:00
|
|
|
return menuitem;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-08 16:20:14 +03:00
|
|
|
void
|
2014-09-29 13:15:37 +03:00
|
|
|
WavesDropdown::_on_menu_item (int item_number, void* cookie)
|
2014-09-08 16:20:14 +03:00
|
|
|
{
|
2014-09-29 13:15:37 +03:00
|
|
|
_selected_item_number = item_number;
|
2014-09-08 16:20:14 +03:00
|
|
|
|
2014-09-03 18:14:34 +03:00
|
|
|
Gtk::Menu_Helpers::MenuList& items = _menu.items ();
|
|
|
|
|
Gtk::Menu_Helpers::MenuList::iterator i = items.begin();
|
2014-09-29 13:15:37 +03:00
|
|
|
std::advance (i, _selected_item_number);
|
2014-09-03 18:14:34 +03:00
|
|
|
set_text ((*i).get_label());
|
2014-09-29 13:15:37 +03:00
|
|
|
selected_item_changed (this, cookie);
|
2014-09-02 17:33:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
WavesDropdown::_on_popup_menu_position (int& x, int& y, bool& push_in)
|
|
|
|
|
{
|
2014-09-03 09:46:26 +03:00
|
|
|
Gtk::Container *toplevel = get_toplevel ();
|
|
|
|
|
if (toplevel) {
|
|
|
|
|
translate_coordinates (*toplevel, 0, 0, x, y);
|
|
|
|
|
Gtk::Allocation a = toplevel->get_allocation ();
|
|
|
|
|
x = a.get_x ();
|
|
|
|
|
y = a.get_y ();
|
|
|
|
|
|
|
|
|
|
a = get_allocation ();
|
|
|
|
|
y += a.get_height ();
|
|
|
|
|
|
|
|
|
|
int xo;
|
|
|
|
|
int yo;
|
|
|
|
|
get_window ()->get_origin (xo, yo);
|
|
|
|
|
x += xo;
|
|
|
|
|
y += yo;
|
|
|
|
|
}
|
2014-09-02 17:33:29 +03:00
|
|
|
}
|
2014-09-29 13:15:37 +03:00
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
WavesDropdown::_on_mouse_pressed (GdkEventButton*)
|
|
|
|
|
{
|
|
|
|
|
_menu.popup (sigc::mem_fun(this, &WavesDropdown::_on_popup_menu_position), 1, gtk_get_current_event_time());
|
|
|
|
|
_hovering = _pushed = false;
|
|
|
|
|
set_active (true);
|
|
|
|
|
return true;
|
|
|
|
|
}
|