Blame synfig-studio/src/gui/helpers.cpp

491f32
/* === S Y N F I G ========================================================= */
491f32
/*!	\file helpers.cpp
491f32
**	\brief Helpers File
491f32
**
491f32
**	$Id$
491f32
**
491f32
**	\legal
491f32
**	......... ... 2018 Ivan Mahonin
491f32
**
491f32
**	This package is free software; you can redistribute it and/or
491f32
**	modify it under the terms of the GNU General Public License as
491f32
**	published by the Free Software Foundation; either version 2 of
491f32
**	the License, or (at your option) any later version.
491f32
**
491f32
**	This package is distributed in the hope that it will be useful,
491f32
**	but WITHOUT ANY WARRANTY; without even the implied warranty of
491f32
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
491f32
**	General Public License for more details.
491f32
**	\endlegal
491f32
*/
491f32
/* ========================================================================= */
491f32
491f32
/* === H E A D E R S ======================================================= */
491f32
491f32
#ifdef USING_PCH
491f32
#	include "pch.h"
491f32
#else
491f32
#ifdef HAVE_CONFIG_H
491f32
#	include <config.h></config.h>
491f32
#endif
491f32
a64bc0
#include <gtk gtk.h=""></gtk>
aeb01c
#include <glibmm main.h=""></glibmm>
a64bc0
491f32
#include <synfig general.h=""></synfig>
491f32
491f32
#include "helpers.h"
491f32
491f32
#include <gui localization.h=""></gui>
491f32
491f32
#endif
491f32
491f32
/* === U S I N G =========================================================== */
491f32
491f32
using namespace synfig;
491f32
using namespace studio;
491f32
491f32
/* === M A C R O S ========================================================= */
491f32
491f32
/* === G L O B A L S ======================================================= */
491f32
491f32
/* === P R O C E D U R E S ================================================= */
491f32
a64bc0
static bool
a64bc0
is_old_gtk_adjustment() {
a64bc0
	static bool is_old = gtk_check_version(3, 18, 0) != NULL;
a64bc0
	return is_old;
a64bc0
}
a64bc0
491f32
/* === M E T H O D S ======================================================= */
491f32
aeb01c
AdjustmentGroup::AdjustmentGroup():
aeb01c
	lock() { }
aeb01c
aeb01c
AdjustmentGroup::~AdjustmentGroup()
aeb01c
{
aeb01c
	for(std::list<item>::iterator i = items.begin(); i != items.end(); ++i) {</item>
aeb01c
		i->connection_changed.disconnect();
aeb01c
		i->connection_value_changed.disconnect();
aeb01c
	}
aeb01c
	connection_timeout.disconnect();
aeb01c
}
aeb01c
aeb01c
void AdjustmentGroup::add(Glib::RefPtr<gtk::adjustment> adjustment)</gtk::adjustment>
aeb01c
{
aeb01c
	for(std::list<item>::iterator i = items.begin(); i != items.end(); ++i)</item>
aeb01c
		if (i->adjustment == adjustment) return;
aeb01c
aeb01c
	items.push_back(Item());
aeb01c
	Item &item = items.back();
aeb01c
aeb01c
	item.adjustment = adjustment;
aeb01c
	item.connection_changed = item.adjustment->signal_changed().connect(
aeb01c
		sigc::bind( sigc::mem_fun(this, &AdjustmentGroup::changed), adjustment ) );
aeb01c
	item.connection_value_changed = item.adjustment->signal_value_changed().connect(
aeb01c
		sigc::bind( sigc::mem_fun(this, &AdjustmentGroup::changed), adjustment ) );
aeb01c
aeb01c
	changed( adjustment );
aeb01c
}
aeb01c
aeb01c
void AdjustmentGroup::remove(Glib::RefPtr<gtk::adjustment> adjustment)</gtk::adjustment>
aeb01c
{
aeb01c
	bool found = false;
aeb01c
	for(std::list<item>::iterator i = items.begin(); i != items.end(); )</item>
aeb01c
		if (i->adjustment == adjustment) {
aeb01c
			i->connection_changed.disconnect();
aeb01c
			i->connection_value_changed.disconnect();
aeb01c
			i = items.erase(i);
aeb01c
			found = true;
aeb01c
		} else ++i;
aeb01c
	if (found) changed(adjustment);
aeb01c
}
aeb01c
  
aeb01c
void
aeb01c
AdjustmentGroup::changed(Glib::RefPtr<gtk::adjustment> adjustment)</gtk::adjustment>
aeb01c
{
aeb01c
	if (lock || items.empty()) return;
aeb01c
aeb01c
	double position = items.front().adjustment->get_value();
aeb01c
aeb01c
	double maxSize = 0;
aeb01c
	for(std::list<item>::iterator i = items.begin(); i != items.end(); ++i) {</item>
aeb01c
		if (i->adjustment == adjustment) {
aeb01c
			i->origSize = i->adjustment->get_upper()
aeb01c
			            - i->adjustment->get_page_size();
aeb01c
			position = i->adjustment->get_value();
aeb01c
		}
aeb01c
		maxSize = std::max(maxSize, i->origSize);
aeb01c
	}
aeb01c
aeb01c
	connection_timeout.disconnect();
aeb01c
	connection_timeout = Glib::signal_timeout().connect(
aeb01c
		sigc::bind_return(
aeb01c
			sigc::bind( sigc::mem_fun(this, &AdjustmentGroup::set), position, maxSize ),
aeb01c
			false ),
aeb01c
		0 );
aeb01c
}
aeb01c
aeb01c
void
aeb01c
AdjustmentGroup::set(double position, double size)
aeb01c
{
aeb01c
	BoolLock boollock(lock);
aeb01c
	connection_timeout.disconnect();
aeb01c
	for(std::list<item>::iterator i = items.begin(); i != items.end(); ++i) {</item>
aeb01c
		double value = i->adjustment->get_value();
aeb01c
		double page = i->adjustment->get_page_size();
aeb01c
		double upper = i->adjustment->get_upper();
aeb01c
		double newUpper = size + page;
aeb01c
aeb01c
		if (fabs(newUpper - upper) > 0.1)
aeb01c
			i->adjustment->set_upper(newUpper);
aeb01c
		if (fabs(position - value) > 0.1)
aeb01c
			i->adjustment->set_value(position);
aeb01c
	}
aeb01c
};
aeb01c
aeb01c
a64bc0
void
a64bc0
ConfigureAdjustment::emit_changed()
a64bc0
	{ if (is_old_gtk_adjustment()) adjustment->changed(); }
a64bc0
a64bc0
void
a64bc0
ConfigureAdjustment::emit_value_changed()
a64bc0
{ if (is_old_gtk_adjustment()) adjustment->value_changed(); }