2008-06-02 17:41:35 -04:00
|
|
|
/*
|
2015-10-04 14:51:05 -04:00
|
|
|
Copyright (C) 2000-2007 Paul Davis
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
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 <string>
|
|
|
|
#include <iostream>
|
|
|
|
|
2010-03-26 17:24:17 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
#include <gtkmm/main.h>
|
|
|
|
|
2017-07-16 22:55:52 -04:00
|
|
|
#include "widgets/stateful_button.h"
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
using namespace Gtk;
|
|
|
|
using namespace Glib;
|
2017-07-16 22:55:52 -04:00
|
|
|
using namespace ArdourWidgets;
|
2008-06-02 17:41:35 -04:00
|
|
|
using namespace std;
|
|
|
|
|
2015-10-04 14:51:05 -04:00
|
|
|
StateButton::StateButton ()
|
2010-03-26 17:24:17 -04:00
|
|
|
: visual_state (0)
|
|
|
|
, _self_managed (false)
|
|
|
|
, _is_realized (false)
|
|
|
|
, style_changing (false)
|
|
|
|
, state_before_prelight (Gtk::STATE_NORMAL)
|
|
|
|
, is_toggle (false)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StateButton::set_visual_state (int n)
|
|
|
|
{
|
|
|
|
if (!_is_realized) {
|
|
|
|
/* not yet realized */
|
|
|
|
visual_state = n;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == visual_state) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
string name = get_widget_name ();
|
|
|
|
name = name.substr (0, name.find_last_of ('-'));
|
|
|
|
|
|
|
|
switch (n) {
|
|
|
|
case 0:
|
|
|
|
/* relax */
|
|
|
|
break;
|
|
|
|
case 1:
|
2010-03-26 17:24:17 -04:00
|
|
|
name += "-active";
|
2008-06-02 17:41:35 -04:00
|
|
|
break;
|
2010-03-26 17:24:17 -04:00
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
case 2:
|
|
|
|
name += "-alternate";
|
|
|
|
break;
|
2010-04-20 08:38:37 -04:00
|
|
|
|
|
|
|
case 3:
|
|
|
|
name += "-alternate2";
|
|
|
|
break;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
set_widget_name (name);
|
|
|
|
visual_state = n;
|
|
|
|
}
|
|
|
|
|
2010-03-26 17:24:17 -04:00
|
|
|
void
|
|
|
|
StateButton::avoid_prelight_on_style_changed (const Glib::RefPtr<Gtk::Style>& /* old_style */, GtkWidget* widget)
|
|
|
|
{
|
|
|
|
/* don't go into an endless recursive loop if we're changing
|
|
|
|
the style in response to an existing style change.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (style_changing) {
|
|
|
|
return;
|
|
|
|
}
|
2015-10-04 14:51:05 -04:00
|
|
|
|
2010-03-26 17:24:17 -04:00
|
|
|
if (gtk_widget_get_state (widget) == GTK_STATE_PRELIGHT) {
|
2015-10-04 14:51:05 -04:00
|
|
|
|
2010-03-26 17:24:17 -04:00
|
|
|
/* avoid PRELIGHT: make sure that the prelight colors in this new style match
|
|
|
|
the colors of the new style in whatever state we were in
|
|
|
|
before we switched to prelight.
|
|
|
|
*/
|
|
|
|
|
|
|
|
GtkRcStyle* rcstyle = gtk_widget_get_modifier_style (widget);
|
|
|
|
GtkStyle* style = gtk_widget_get_style (widget);
|
|
|
|
|
|
|
|
rcstyle->fg[GTK_STATE_PRELIGHT] = style->fg[state_before_prelight];
|
|
|
|
rcstyle->bg[GTK_STATE_PRELIGHT] = style->bg[state_before_prelight];
|
|
|
|
rcstyle->color_flags[GTK_STATE_PRELIGHT] = (GtkRcFlags) (GTK_RC_FG|GTK_RC_BG);
|
2015-10-04 14:51:05 -04:00
|
|
|
|
2010-03-26 17:24:17 -04:00
|
|
|
style_changing = true;
|
|
|
|
g_object_ref (rcstyle);
|
|
|
|
gtk_widget_modify_style (widget, rcstyle);
|
|
|
|
|
|
|
|
Widget* child = get_child_widget();
|
|
|
|
if (child) {
|
|
|
|
gtk_widget_modify_style (GTK_WIDGET(child->gobj()), rcstyle);
|
|
|
|
}
|
|
|
|
|
2011-07-20 14:13:03 -04:00
|
|
|
|
2010-03-26 17:24:17 -04:00
|
|
|
g_object_unref (rcstyle);
|
|
|
|
style_changing = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StateButton::avoid_prelight_on_state_changed (Gtk::StateType old_state, GtkWidget* widget)
|
|
|
|
{
|
|
|
|
GtkStateType state = gtk_widget_get_state (widget);
|
|
|
|
|
|
|
|
if (state == GTK_STATE_PRELIGHT) {
|
|
|
|
|
|
|
|
state_before_prelight = old_state;
|
2015-10-04 14:51:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
/* avoid PRELIGHT when currently ACTIVE:
|
|
|
|
if we just went into PRELIGHT, make sure that the colors
|
|
|
|
match those of whatever state we were in before.
|
2010-03-26 17:24:17 -04:00
|
|
|
*/
|
2015-10-04 14:51:05 -04:00
|
|
|
|
2010-03-26 17:24:17 -04:00
|
|
|
GtkRcStyle* rcstyle = gtk_widget_get_modifier_style (widget);
|
|
|
|
GtkStyle* style = gtk_widget_get_style (widget);
|
2015-10-04 14:51:05 -04:00
|
|
|
|
2010-03-26 17:24:17 -04:00
|
|
|
rcstyle->fg[GTK_STATE_PRELIGHT] = style->fg[old_state];
|
|
|
|
rcstyle->bg[GTK_STATE_PRELIGHT] = style->bg[old_state];
|
|
|
|
rcstyle->color_flags[GTK_STATE_PRELIGHT] = (GtkRcFlags) (GTK_RC_FG|GTK_RC_BG);
|
2015-10-04 14:51:05 -04:00
|
|
|
|
2010-03-26 17:24:17 -04:00
|
|
|
g_object_ref (rcstyle);
|
|
|
|
gtk_widget_modify_style (widget, rcstyle);
|
|
|
|
|
|
|
|
Widget* child = get_child_widget ();
|
|
|
|
|
|
|
|
if (child) {
|
|
|
|
gtk_widget_modify_style (GTK_WIDGET(child->gobj()), rcstyle);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_object_unref (rcstyle);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
/* ----------------------------------------------------------------- */
|
|
|
|
|
2010-03-26 17:24:17 -04:00
|
|
|
StatefulToggleButton::StatefulToggleButton ()
|
|
|
|
{
|
|
|
|
is_toggle = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
StatefulToggleButton::StatefulToggleButton (const std::string& label)
|
|
|
|
: ToggleButton (label)
|
|
|
|
{
|
|
|
|
is_toggle = true;
|
|
|
|
}
|
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
void
|
|
|
|
StatefulToggleButton::on_realize ()
|
|
|
|
{
|
|
|
|
ToggleButton::on_realize ();
|
|
|
|
|
|
|
|
_is_realized = true;
|
|
|
|
visual_state++; // to force transition
|
|
|
|
set_visual_state (visual_state - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StatefulButton::on_realize ()
|
|
|
|
{
|
|
|
|
Button::on_realize ();
|
|
|
|
|
|
|
|
_is_realized = true;
|
|
|
|
visual_state++; // to force transition
|
|
|
|
set_visual_state (visual_state - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StatefulToggleButton::on_toggled ()
|
|
|
|
{
|
|
|
|
if (!_self_managed) {
|
|
|
|
if (get_active()) {
|
2010-03-26 17:24:17 -04:00
|
|
|
set_state (Gtk::STATE_ACTIVE);
|
2008-06-02 17:41:35 -04:00
|
|
|
} else {
|
2010-03-26 17:24:17 -04:00
|
|
|
set_state (Gtk::STATE_NORMAL);
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-12-08 22:05:14 -05:00
|
|
|
|
2010-03-26 17:24:17 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
StatefulToggleButton::on_style_changed (const Glib::RefPtr<Gtk::Style>& style)
|
|
|
|
{
|
|
|
|
avoid_prelight_on_style_changed (style, GTK_WIDGET(gobj()));
|
|
|
|
Button::on_style_changed (style);
|
2015-10-04 14:51:05 -04:00
|
|
|
}
|
2010-03-26 17:24:17 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
StatefulToggleButton::on_state_changed (Gtk::StateType old_state)
|
|
|
|
{
|
|
|
|
avoid_prelight_on_state_changed (old_state, GTK_WIDGET(gobj()));
|
|
|
|
Button::on_state_changed (old_state);
|
2015-10-04 14:51:05 -04:00
|
|
|
}
|
2010-03-26 17:24:17 -04:00
|
|
|
|
|
|
|
Widget*
|
|
|
|
StatefulToggleButton::get_child_widget ()
|
|
|
|
{
|
|
|
|
return get_child();
|
|
|
|
}
|
|
|
|
|
2009-12-08 22:05:14 -05:00
|
|
|
void
|
|
|
|
StatefulToggleButton::set_widget_name (const std::string& name)
|
|
|
|
{
|
2015-10-04 14:51:05 -04:00
|
|
|
set_name (name);
|
2009-12-08 22:05:14 -05:00
|
|
|
Widget* w = get_child();
|
|
|
|
|
|
|
|
if (w) {
|
2015-10-04 14:51:05 -04:00
|
|
|
w->set_name (name);
|
|
|
|
}
|
2009-12-08 22:05:14 -05:00
|
|
|
}
|
|
|
|
|
2010-03-26 17:24:17 -04:00
|
|
|
/*--------------------------------------------- */
|
|
|
|
|
|
|
|
StatefulButton::StatefulButton ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
StatefulButton::StatefulButton (const std::string& label)
|
|
|
|
: Button (label)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StatefulButton::on_style_changed (const Glib::RefPtr<Gtk::Style>& style)
|
|
|
|
{
|
|
|
|
avoid_prelight_on_style_changed (style, GTK_WIDGET(gobj()));
|
|
|
|
Button::on_style_changed (style);
|
2015-10-04 14:51:05 -04:00
|
|
|
}
|
2010-03-26 17:24:17 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
StatefulButton::on_state_changed (Gtk::StateType old_state)
|
|
|
|
{
|
|
|
|
avoid_prelight_on_state_changed (old_state, GTK_WIDGET(gobj()));
|
|
|
|
Button::on_state_changed (old_state);
|
2015-10-04 14:51:05 -04:00
|
|
|
}
|
2010-03-26 17:24:17 -04:00
|
|
|
|
|
|
|
Widget*
|
|
|
|
StatefulButton::get_child_widget ()
|
|
|
|
{
|
|
|
|
return get_child();
|
|
|
|
}
|
|
|
|
|
2009-12-08 22:05:14 -05:00
|
|
|
void
|
|
|
|
StatefulButton::set_widget_name (const std::string& name)
|
|
|
|
{
|
2015-10-04 14:51:05 -04:00
|
|
|
set_name (name);
|
2009-12-08 22:05:14 -05:00
|
|
|
Widget* w = get_child();
|
|
|
|
|
|
|
|
if (w) {
|
2015-10-04 14:51:05 -04:00
|
|
|
w->set_name (name);
|
|
|
|
}
|
2009-12-08 22:05:14 -05:00
|
|
|
}
|