summaryrefslogtreecommitdiff
path: root/libs
diff options
context:
space:
mode:
Diffstat (limited to 'libs')
-rw-r--r--libs/ardour/audio_diskstream.cc7
-rw-r--r--libs/ardour/audio_track.cc5
-rw-r--r--libs/ardour/audiofilesource.cc5
-rw-r--r--libs/ardour/audiosource.cc2
-rw-r--r--libs/ardour/diskstream.cc2
-rw-r--r--libs/ardour/file_source.cc4
-rw-r--r--libs/ardour/region.cc2
-rw-r--r--libs/ardour/send.cc1
-rw-r--r--libs/ardour/session.cc85
-rw-r--r--libs/ardour/session_state.cc8
-rw-r--r--libs/ardour/source.cc2
-rw-r--r--libs/ardour/source_factory.cc43
-rw-r--r--libs/gtkmm2ext/gtkmm2ext/dndvbox.h322
13 files changed, 431 insertions, 57 deletions
diff --git a/libs/ardour/audio_diskstream.cc b/libs/ardour/audio_diskstream.cc
index 3248cd9b2d..faa4684745 100644
--- a/libs/ardour/audio_diskstream.cc
+++ b/libs/ardour/audio_diskstream.cc
@@ -122,6 +122,8 @@ AudioDiskstream::init (Diskstream::Flag f)
AudioDiskstream::~AudioDiskstream ()
{
+ cerr << "AD going away\n";
+
notify_callbacks ();
{
@@ -1915,6 +1917,11 @@ AudioDiskstream::use_new_write_source (uint32_t n)
/* do not remove destructive files even if they are empty */
chan->write_source->set_allow_remove_if_empty (!destructive());
+
+ /* until we write, this file is considered removable */
+
+ chan->write_source->mark_for_remove ();
+ cerr << "New write source " << chan->write_source->path() << " flags " << enum_2_string (chan->write_source->flags()) << endl;
return 0;
}
diff --git a/libs/ardour/audio_track.cc b/libs/ardour/audio_track.cc
index e2101479fe..1a92cd5459 100644
--- a/libs/ardour/audio_track.cc
+++ b/libs/ardour/audio_track.cc
@@ -23,6 +23,7 @@
#include "pbd/error.h"
#include "pbd/enumwriter.h"
+#include "pbd/boost_debug.h"
#include "evoral/Curve.hpp"
@@ -83,8 +84,8 @@ AudioTrack::use_new_diskstream ()
dflags = AudioDiskstream::Flag(dflags | AudioDiskstream::NonLayered);
}
-
- boost::shared_ptr<AudioDiskstream> ds (new AudioDiskstream (_session, name(), dflags));
+ AudioDiskstream* dsp (new AudioDiskstream (_session, name(), dflags));
+ boost::shared_ptr<AudioDiskstream> ds (dsp);
_session.add_diskstream (ds);
diff --git a/libs/ardour/audiofilesource.cc b/libs/ardour/audiofilesource.cc
index 8de786eb0e..e9eb3641da 100644
--- a/libs/ardour/audiofilesource.cc
+++ b/libs/ardour/audiofilesource.cc
@@ -99,6 +99,7 @@ AudioFileSource::AudioFileSource (Session& s, const ustring& path, bool embedded
if (init (path, true)) {
throw failed_constructor ();
}
+ cerr << "AFS1 created, " << path << endl;
}
/** Constructor used for new internal-to-session files. */
@@ -113,6 +114,7 @@ AudioFileSource::AudioFileSource (Session& s, const ustring& path, bool embedded
if (init (path, false)) {
throw failed_constructor ();
}
+ cerr << "AFS2 created, " << path << endl;
}
/** Constructor used for existing internal-to-session files. File must exist. */
@@ -128,11 +130,14 @@ AudioFileSource::AudioFileSource (Session& s, const XMLNode& node, bool must_exi
if (init (_name, must_exist)) {
throw failed_constructor ();
}
+ cerr << "AFS3 created, " << path() << endl;
}
AudioFileSource::~AudioFileSource ()
{
+ cerr << "AFS " << _name << " destructor, path = " << path() << endl;
if (removable()) {
+ cerr << "\tremoving file\n";
unlink (_path.c_str());
unlink (peakpath.c_str());
}
diff --git a/libs/ardour/audiosource.cc b/libs/ardour/audiosource.cc
index 883bc0b41b..f7d6088aec 100644
--- a/libs/ardour/audiosource.cc
+++ b/libs/ardour/audiosource.cc
@@ -276,6 +276,8 @@ nframes_t
AudioSource::write (Sample *dst, nframes_t cnt)
{
Glib::Mutex::Lock lm (_lock);
+ /* any write makes the fill not removable */
+ _flags = Flag (_flags & ~Removable);
return write_unlocked (dst, cnt);
}
diff --git a/libs/ardour/diskstream.cc b/libs/ardour/diskstream.cc
index 0d27cb508a..b6cc9d6a04 100644
--- a/libs/ardour/diskstream.cc
+++ b/libs/ardour/diskstream.cc
@@ -41,6 +41,7 @@
#include "ardour/ardour.h"
#include "ardour/audioengine.h"
+#include "ardour/debug.h"
#include "ardour/diskstream.h"
#include "ardour/utils.h"
#include "ardour/configuration.h"
@@ -130,6 +131,7 @@ Diskstream::~Diskstream ()
{
if (_playlist)
_playlist->release ();
+ DEBUG_TRACE (DEBUG::Destruction, string_compose ("Diskstream %1 deleted\n", _name));
}
void
diff --git a/libs/ardour/file_source.cc b/libs/ardour/file_source.cc
index e5232f2384..6ebbf0b523 100644
--- a/libs/ardour/file_source.cc
+++ b/libs/ardour/file_source.cc
@@ -77,8 +77,8 @@ bool
FileSource::removable () const
{
return (_flags & Removable)
- && ( (_flags & RemoveAtDestroy)
- || ((_flags & RemovableIfEmpty) && length(timeline_position()) == 0));
+ && ((_flags & RemoveAtDestroy) ||
+ ((_flags & RemovableIfEmpty) && length(timeline_position()) == 0));
}
int
diff --git a/libs/ardour/region.cc b/libs/ardour/region.cc
index 20c87fb1de..c59a5bb562 100644
--- a/libs/ardour/region.cc
+++ b/libs/ardour/region.cc
@@ -31,6 +31,7 @@
#include "pbd/stacktrace.h"
#include "pbd/enumwriter.h"
+#include "ardour/debug.h"
#include "ardour/region.h"
#include "ardour/playlist.h"
#include "ardour/session.h"
@@ -340,6 +341,7 @@ Region::~Region ()
{
notify_callbacks ();
GoingAway (); /* EMIT SIGNAL */
+ DEBUG_TRACE (DEBUG::Destruction, string_compose ("Region %1 deleted\n", _name));
}
void
diff --git a/libs/ardour/send.cc b/libs/ardour/send.cc
index 3039a7fc3b..91e6827e61 100644
--- a/libs/ardour/send.cc
+++ b/libs/ardour/send.cc
@@ -163,6 +163,7 @@ Send::set_state (const XMLNode& node, int version)
_bitslot = _session.next_send_id();
} else {
sscanf (prop->value().c_str(), "%" PRIu32, &_bitslot);
+ cerr << this << " scanned " << prop->value() << " to get " << _bitslot << endl;
_session.mark_send_id (_bitslot);
}
diff --git a/libs/ardour/session.cc b/libs/ardour/session.cc
index ce1fbca846..febaf6ed11 100644
--- a/libs/ardour/session.cc
+++ b/libs/ardour/session.cc
@@ -37,6 +37,7 @@
#include "pbd/error.h"
#include <glibmm/thread.h>
+#include "pbd/boost_debug.h"
#include "pbd/pathscanner.h"
#include "pbd/stl_delete.h"
#include "pbd/basename.h"
@@ -62,6 +63,7 @@
#include "ardour/crossfade.h"
#include "ardour/cycle_timer.h"
#include "ardour/data_type.h"
+#include "ardour/debug.h"
#include "ardour/filename_extensions.h"
#include "ardour/internal_send.h"
#include "ardour/io_processor.h"
@@ -329,6 +331,8 @@ Session::~Session ()
void
Session::destroy ()
{
+ vector<void*> debug_pointers;
+
/* if we got to here, leaving pending capture state around
is a mistake.
*/
@@ -378,10 +382,7 @@ Session::destroy ()
Route::SyncOrderKeys.clear();
-#undef TRACK_DESTRUCTION
-#ifdef TRACK_DESTRUCTION
- cerr << "delete named selections\n";
-#endif /* TRACK_DESTRUCTION */
+ DEBUG_TRACE (DEBUG::Destruction, "delete named selections\n");
for (NamedSelectionList::iterator i = named_selections.begin(); i != named_selections.end(); ) {
NamedSelectionList::iterator tmp;
@@ -392,16 +393,16 @@ Session::destroy ()
i = tmp;
}
-#ifdef TRACK_DESTRUCTION
- cerr << "delete playlists\n";
-#endif /* TRACK_DESTRUCTION */
+ DEBUG_TRACE (DEBUG::Destruction, "delete playlists\n");
for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ) {
PlaylistList::iterator tmp;
tmp = i;
++tmp;
+ DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for used playlist %1 ; pre-ref = %2\n", (*i)->name(), (*i).use_count()));
(*i)->drop_references ();
+ DEBUG_TRACE(DEBUG::Destruction, string_compose ("post-ref = %1\n", (*i).use_count()));
i = tmp;
}
@@ -412,7 +413,9 @@ Session::destroy ()
tmp = i;
++tmp;
+ DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for unused playlist %1 ; pre-ref = %2\n", (*i)->name(), (*i).use_count()));
(*i)->drop_references ();
+ DEBUG_TRACE(DEBUG::Destruction, string_compose ("post-ref = %2\n", (*i).use_count()));
i = tmp;
}
@@ -420,70 +423,68 @@ Session::destroy ()
playlists.clear ();
unused_playlists.clear ();
-#ifdef TRACK_DESTRUCTION
- cerr << "delete regions\n";
-#endif /* TRACK_DESTRUCTION */
-
+ DEBUG_TRACE (DEBUG::Destruction, "delete regions\n");
for (RegionList::iterator i = regions.begin(); i != regions.end(); ) {
RegionList::iterator tmp;
tmp = i;
++tmp;
-
+
+ DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for region %1 ; pre-ref = %2\n", i->second->name(), i->second.use_count()));
i->second->drop_references ();
+ DEBUG_TRACE(DEBUG::Destruction, string_compose ("\tpost-ref%2\n", i->second.use_count()));
i = tmp;
}
regions.clear ();
-#ifdef TRACK_DESTRUCTION
- cerr << "delete routes\n";
-#endif /* TRACK_DESTRUCTION */
+ DEBUG_TRACE (DEBUG::Destruction, "delete routes\n");
{
RCUWriter<RouteList> writer (routes);
boost::shared_ptr<RouteList> r = writer.get_copy ();
for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
+ DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for route %1 ; pre-ref = %2\n", (*i)->name(), (*i).use_count()));
(*i)->drop_references ();
+ DEBUG_TRACE(DEBUG::Destruction, string_compose ("post-ref = %1\n", (*i).use_count()));
+ debug_pointers.push_back ((*i).get());
}
r->clear ();
/* writer goes out of scope and updates master */
}
-
routes.flush ();
-#ifdef TRACK_DESTRUCTION
- cerr << "delete diskstreams\n";
-#endif /* TRACK_DESTRUCTION */
- {
- RCUWriter<DiskstreamList> dwriter (diskstreams);
- boost::shared_ptr<DiskstreamList> dsl = dwriter.get_copy();
- for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
- (*i)->drop_references ();
- }
- dsl->clear ();
- }
- diskstreams.flush ();
-
-#ifdef TRACK_DESTRUCTION
- cerr << "delete audio sources\n";
-#endif /* TRACK_DESTRUCTION */
+ DEBUG_TRACE (DEBUG::Destruction, "delete diskstreams\n");
+ {
+ RCUWriter<DiskstreamList> dwriter (diskstreams);
+ boost::shared_ptr<DiskstreamList> dsl = dwriter.get_copy();
+ for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
+ DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for diskstream %1 ; pre-ref = %2\n", (*i)->name(), (*i).use_count()));
+ (*i)->drop_references ();
+ DEBUG_TRACE(DEBUG::Destruction, string_compose ("post-ref = %1\n", (*i).use_count()));
+ }
+ dsl->clear ();
+ }
+ diskstreams.flush ();
+
+ DEBUG_TRACE (DEBUG::Destruction, "delete sources\n");
for (SourceMap::iterator i = sources.begin(); i != sources.end(); ) {
SourceMap::iterator tmp;
tmp = i;
++tmp;
+ DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for source %1 ; pre-ref = %2\n", i->second->path(), i->second.use_count()));
i->second->drop_references ();
+ DEBUG_TRACE(DEBUG::Destruction, string_compose ("\tpost-ref%1\n", i->second.use_count()));
i = tmp;
}
+ cerr << "Pre source clear, we have " << sources.size() << endl;
sources.clear ();
+ cerr << "Post source clear, we have " << sources.size() << endl;
-
-#ifdef TRACK_DESTRUCTION
- cerr << "delete route groups\n";
-#endif /* TRACK_DESTRUCTION */
+ DEBUG_TRACE (DEBUG::Destruction, "delete route groups\n");
for (list<RouteGroup *>::iterator i = _route_groups.begin(); i != _route_groups.end(); ++i) {
delete *i;
}
@@ -491,6 +492,10 @@ Session::destroy ()
Crossfade::set_buffer_size (0);
delete mmc;
+
+ for (vector<void*>::iterator x = debug_pointers.begin(); x != debug_pointers.end(); ++x) {
+ boost_debug_shared_ptr_show (cerr, *x);
+ }
}
void
@@ -1769,7 +1774,9 @@ Session::new_audio_track (int input_channels, int output_channels, TrackMode mod
shared_ptr<AudioTrack> track;
try {
- track = boost::shared_ptr<AudioTrack>((new AudioTrack (*this, track_name, Route::Flag (0), mode)));
+ AudioTrack* at = new AudioTrack (*this, track_name, Route::Flag (0), mode);
+ boost_debug_shared_ptr_mark_interesting (at, typeid (at).name());
+ track = boost::shared_ptr<AudioTrack>(at);
if (track->input()->ensure_io (ChanCount(DataType::AUDIO, input_channels), false, this)) {
error << string_compose (_("cannot configure %1 in/%2 out configuration for new audio track"),
@@ -3330,9 +3337,9 @@ Session::create_audio_source_for_session (AudioDiskstream& ds, uint32_t chan, bo
const size_t n_chans = ds.n_channels().n_audio();
const string name = new_audio_source_name (ds.name(), n_chans, chan, destructive);
const string path = new_source_path_from_name(DataType::AUDIO, name);
+
return boost::dynamic_pointer_cast<AudioFileSource> (
- SourceFactory::createWritable (
- DataType::AUDIO, *this, path, true, destructive, frame_rate()));
+ SourceFactory::createWritable (DataType::AUDIO, *this, path, true, destructive, frame_rate()));
}
/** Return a unique name based on \a base for a new internal MIDI source */
diff --git a/libs/ardour/session_state.cc b/libs/ardour/session_state.cc
index 2e564b7eee..f53dd7106e 100644
--- a/libs/ardour/session_state.cc
+++ b/libs/ardour/session_state.cc
@@ -57,6 +57,7 @@
#include "midi++/mmc.h"
#include "midi++/port.h"
+#include "pbd/boost_debug.h"
#include "pbd/error.h"
#include "pbd/pathscanner.h"
#include "pbd/pthread_utils.h"
@@ -567,7 +568,8 @@ Session::load_diskstreams (const XMLNode& node)
try {
/* diskstreams added automatically by DiskstreamCreated handler */
if ((*citer)->name() == "AudioDiskstream" || (*citer)->name() == "DiskStream") {
- boost::shared_ptr<AudioDiskstream> dstream (new AudioDiskstream (*this, **citer));
+ AudioDiskstream* dsp (new AudioDiskstream (*this, **citer));
+ boost::shared_ptr<AudioDiskstream> dstream (dsp);
add_diskstream (dstream);
} else if ((*citer)->name() == "MidiDiskstream") {
boost::shared_ptr<MidiDiskstream> dstream (new MidiDiskstream (*this, **citer));
@@ -1402,7 +1404,9 @@ Session::XMLRouteFactory (const XMLNode& node, int version)
if (has_diskstream) {
if (type == DataType::AUDIO) {
- boost::shared_ptr<Route> ret (new AudioTrack (*this, node, version));
+ AudioTrack* at = new AudioTrack (*this, node, version);
+ boost_debug_shared_ptr_mark_interesting (at, typeid (at).name());
+ boost::shared_ptr<Route> ret (at);
return ret;
} else {
boost::shared_ptr<Route> ret (new MidiTrack (*this, node, version));
diff --git a/libs/ardour/source.cc b/libs/ardour/source.cc
index 394d4b39af..10b7ead270 100644
--- a/libs/ardour/source.cc
+++ b/libs/ardour/source.cc
@@ -36,6 +36,7 @@
#include "pbd/pthread_utils.h"
#include "pbd/enumwriter.h"
+#include "ardour/debug.h"
#include "ardour/session.h"
#include "ardour/source.h"
#include "ardour/transient_detector.h"
@@ -75,6 +76,7 @@ Source::Source (Session& s, const XMLNode& node)
Source::~Source ()
{
notify_callbacks ();
+ DEBUG_TRACE (DEBUG::Destruction, string_compose ("Source %1 deleted\n", _name));
}
diff --git a/libs/ardour/source_factory.cc b/libs/ardour/source_factory.cc
index 198a429c5d..999ae667dd 100644
--- a/libs/ardour/source_factory.cc
+++ b/libs/ardour/source_factory.cc
@@ -22,6 +22,7 @@
#include "libardour-config.h"
#endif
+#include "pbd/boost_debug.h"
#include "pbd/error.h"
#include "pbd/convert.h"
#include "pbd/pthread_utils.h"
@@ -122,7 +123,9 @@ SourceFactory::setup_peakfile (boost::shared_ptr<Source> s, bool async)
boost::shared_ptr<Source>
SourceFactory::createSilent (Session& s, const XMLNode& node, nframes_t nframes, float sr)
{
- boost::shared_ptr<Source> ret (new SilentFileSource (s, node, nframes, sr));
+ Source* src = new SilentFileSource (s, node, nframes, sr);
+ // boost_debug_shared_ptr_mark_interesting (src, typeid(src).name());
+ boost::shared_ptr<Source> ret (src);
// no analysis data - the file is non-existent
SourceCreated (ret);
return ret;
@@ -142,7 +145,9 @@ SourceFactory::create (Session& s, const XMLNode& node, bool defer_peaks)
try {
- boost::shared_ptr<Source> ret (new SndFileSource (s, node));
+ Source* src = new SndFileSource (s, node);
+ // boost_debug_shared_ptr_mark_interesting (src, typeid(src).name());
+ boost::shared_ptr<Source> ret (src);
if (setup_peakfile (ret, defer_peaks)) {
return boost::shared_ptr<Source>();
}
@@ -157,7 +162,9 @@ SourceFactory::create (Session& s, const XMLNode& node, bool defer_peaks)
/* this is allowed to throw */
- boost::shared_ptr<Source> ret (new CoreAudioSource (s, node));
+ Source *s = new CoreAudioSource (s, node);
+ // boost_debug_shared_ptr_mark_interesting (src, typeid(src).name());
+ boost::shared_ptr<Source> ret (src);
if (setup_peakfile (ret, defer_peaks)) {
return boost::shared_ptr<Source>();
@@ -172,7 +179,9 @@ SourceFactory::create (Session& s, const XMLNode& node, bool defer_peaks)
}
} else if (type == DataType::MIDI) {
- boost::shared_ptr<Source> ret (new SMFSource (s, node));
+ Source* src = new SMFSource (s, node);
+ // boost_debug_shared_ptr_mark_interesting (src, typeid(src).name());
+ boost::shared_ptr<Source> ret (src);
ret->check_for_analysis_data_on_disk ();
SourceCreated (ret);
return ret;
@@ -191,8 +200,10 @@ SourceFactory::createReadable (DataType type, Session& s, const string& path, bo
try {
- boost::shared_ptr<Source> ret (new SndFileSource (s, path, embedded, chn, flags));
-
+ Source* src = new SndFileSource (s, path, embedded, chn, flags);
+ // boost_debug_shared_ptr_mark_interesting (src, typeid(src).name());
+ boost::shared_ptr<Source> ret (src);
+
if (setup_peakfile (ret, defer_peaks)) {
return boost::shared_ptr<Source>();
}
@@ -207,7 +218,9 @@ SourceFactory::createReadable (DataType type, Session& s, const string& path, bo
catch (failed_constructor& err) {
#ifdef USE_COREAUDIO_FOR_FILES
- boost::shared_ptr<Source> ret (new CoreAudioSource (s, path, embedded, chn, flags));
+ Source* src = new CoreAudioSource (s, path, embedded, chn, flags);
+ // boost_debug_shared_ptr_mark_interesting (src, typeid(src).name());
+ boost::shared_ptr<Source> ret (src);
if (setup_peakfile (ret, defer_peaks)) {
return boost::shared_ptr<Source>();
}
@@ -227,8 +240,10 @@ SourceFactory::createReadable (DataType type, Session& s, const string& path, bo
}
} else if (type == DataType::MIDI) {
-
- boost::shared_ptr<Source> ret (new SMFSource (s, path, embedded, SMFSource::Flag(0)));
+
+ Source* src = new SMFSource (s, path, embedded, SMFSource::Flag(0));
+ // boost_debug_shared_ptr_mark_interesting (src, typeid(src).name());
+ boost::shared_ptr<Source> ret (src);
if (announce) {
SourceCreated (ret);
@@ -248,13 +263,15 @@ SourceFactory::createWritable (DataType type, Session& s, const std::string& pat
/* this might throw failed_constructor(), which is OK */
if (type == DataType::AUDIO) {
- boost::shared_ptr<Source> ret (new SndFileSource (s, path, embedded,
+ Source* src = new SndFileSource (s, path, embedded,
s.config.get_native_file_data_format(),
s.config.get_native_file_header_format(),
rate,
(destructive
? Source::Flag (SndFileSource::default_writable_flags | Source::Destructive)
- : SndFileSource::default_writable_flags)));
+ : SndFileSource::default_writable_flags));
+ // boost_debug_shared_ptr_mark_interesting (src, typeid(src).name());
+ boost::shared_ptr<Source> ret (src);
if (setup_peakfile (ret, defer_peaks)) {
return boost::shared_ptr<Source>();
@@ -269,7 +286,9 @@ SourceFactory::createWritable (DataType type, Session& s, const std::string& pat
} else if (type == DataType::MIDI) {
- boost::shared_ptr<Source> ret (new SMFSource (s, path, embedded, Source::Flag(0)));
+ Source* src = new SMFSource (s, path, embedded, Source::Flag(0));
+ // boost_debug_shared_ptr_mark_interesting (src, typeid(src).name());
+ boost::shared_ptr<Source> ret (src);
// no analysis data - this is a new file
diff --git a/libs/gtkmm2ext/gtkmm2ext/dndvbox.h b/libs/gtkmm2ext/gtkmm2ext/dndvbox.h
new file mode 100644
index 0000000000..7a011e9dcc
--- /dev/null
+++ b/libs/gtkmm2ext/gtkmm2ext/dndvbox.h
@@ -0,0 +1,322 @@
+/*
+ Copyright (C) 2009 Paul Davis
+
+ 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 <gtkmm/box.h>
+
+namespace Gtkmm2ext {
+
+/** Parent class for children of a DnDVBox */
+class DnDVBoxChild
+{
+public:
+ virtual ~DnDVBoxChild () {}
+
+ /** @return The widget that is to be put into the DnDVBox */
+ virtual Gtk::Widget& widget () = 0;
+
+ /** @return An EventBox containing the widget that should be used for selection, dragging etc. */
+ virtual Gtk::EventBox& action_widget () = 0;
+
+ /** @return Text to use in the icon that is dragged */
+ virtual std::string drag_text () const = 0;
+};
+
+/** A VBox whose contents can be dragged and dropped */
+template <class T>
+class DnDVBox : public Gtk::EventBox
+{
+public:
+ DnDVBox () : _drag_icon (0), _expecting_unwanted_button_event (false)
+ {
+ _targets.push_back (Gtk::TargetEntry ("processor"));
+
+ add (_internal_vbox);
+ add_events (Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK | Gdk::ENTER_NOTIFY_MASK | Gdk::LEAVE_NOTIFY_MASK | Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
+
+ signal_button_press_event().connect (bind (mem_fun (*this, &DnDVBox::button_press), (T *) 0));
+ signal_button_release_event().connect (bind (mem_fun (*this, &DnDVBox::button_release), (T *) 0));
+
+ drag_dest_set (_targets);
+ signal_drag_data_received().connect (mem_fun (*this, &DnDVBox::drag_data_received));
+ }
+
+ virtual ~DnDVBox ()
+ {
+ clear ();
+
+ delete _drag_icon;
+ }
+
+ /** Add a child at the end of the widget. The DnDVBox will take responsibility for deleting the child */
+ void add_child (T* child)
+ {
+ child->action_widget().drag_source_set (_targets);
+ child->action_widget().signal_drag_begin().connect (sigc::bind (mem_fun (*this, &DnDVBox::drag_begin), child));
+ child->action_widget().signal_drag_data_get().connect (sigc::bind (mem_fun (*this, &DnDVBox::drag_data_get), child));
+ child->action_widget().signal_drag_end().connect (sigc::bind (mem_fun (*this, &DnDVBox::drag_end), child));
+ child->action_widget().signal_button_press_event().connect (sigc::bind (mem_fun (*this, &DnDVBox::button_press), child));
+ child->action_widget().signal_button_release_event().connect (sigc::bind (mem_fun (*this, &DnDVBox::button_release), child));
+
+ _internal_vbox.pack_start (child->widget(), false, false);
+
+ _children.push_back (child);
+ child->widget().show_all ();
+ }
+
+ /** @return Children, sorted into the order that they are currently being displayed in the widget */
+ std::list<T*> children ()
+ {
+ std::list<T*> sorted_children;
+
+ std::list<Gtk::Widget*> widget_children = _internal_vbox.get_children ();
+ for (std::list<Gtk::Widget*>::iterator i = widget_children.begin(); i != widget_children.end(); ++i) {
+ T* c = child_from_widget (*i);
+
+ if (c) {
+ sorted_children.push_back (c);
+ }
+ }
+
+ return sorted_children;
+ }
+
+ /** @return Selected children */
+ std::list<T*> selection () const {
+ return _selection;
+ }
+
+ /** @param Child
+ * @return true if the child is selected, otherwise false */
+ bool selected (T* child) const {
+ return (find (_selection.begin(), _selection.end(), child) != _selection.end());
+ }
+
+ /** Clear all children from the widget */
+ void clear ()
+ {
+ _selection.clear ();
+
+ for (typename std::list<T*>::iterator i = _children.begin(); i != _children.end(); ++i) {
+ _internal_vbox.remove ((*i)->widget());
+ delete *i;
+ }
+
+ _children.clear ();
+ }
+
+
+ void select_all ()
+ {
+ clear_selection ();
+ for (typename std::list<T*>::iterator i = _children.begin(); i != _children.end(); ++i) {
+ add_to_selection (*i);
+ }
+
+ SelectionChanged (); /* EMIT SIGNAL */
+ }
+
+ void select_none ()
+ {
+ clear_selection ();
+
+ SelectionChanged (); /* EMIT SIGNAL */
+ }
+
+ /** @param x x coordinate.
+ * @param y y coordinate.
+ * @return Pair consisting of the child under (x, y) (or 0) and the index of the child under (x, y) (or -1)
+ */
+ std::pair<T*, int> get_child_at_position (int x, int y) const
+ {
+ std::list<Gtk::Widget const *> children = _internal_vbox.get_children ();
+ std::list<Gtk::Widget const *>::iterator i = children.begin();
+
+ int n = 0;
+ while (i != children.end()) {
+ Gdk::Rectangle const a = (*i)->get_allocation ();
+ if (y >= a.get_y() && y <= (a.get_y() + a.get_height())) {
+ break;
+ }
+ ++i;
+ ++n;
+ }
+
+ if (i == children.end()) {
+ return std::make_pair ((T *) 0, -1);
+ }
+
+ return std::make_pair (child_from_widget (*i), n);
+ }
+
+ /** Children have been reordered by a drag */
+ sigc::signal<void> Reordered;
+
+ /** A button has been pressed over the widget */
+ sigc::signal<bool, GdkEventButton*, T*> ButtonPress;
+
+ /** A button has been release over the widget */
+ sigc::signal<bool, GdkEventButton*, T*> ButtonRelease;
+
+ /** A child has been dropped onto this DnDVBox from another one;
+ * Parameters are the source DnDVBox, our child which the other one was dropped on (or 0) and the DragContext.
+ */
+ sigc::signal<void, DnDVBox*, T*, Glib::RefPtr<Gdk::DragContext> const & > DropFromAnotherBox;
+ sigc::signal<void> SelectionChanged;
+
+private:
+ void drag_begin (Glib::RefPtr<Gdk::DragContext> const & context, T* child)
+ {
+ /* make up an icon for the drag */
+ Gtk::Window* _drag_icon = new Gtk::Window (Gtk::WINDOW_POPUP);
+ _drag_icon->set_name (get_name ());
+ Gtk::Label* l = new Gtk::Label (child->drag_text ());
+ l->set_padding (4, 4);
+ _drag_icon->add (*Gtk::manage (l));
+ _drag_icon->show_all_children ();
+ int w, h;
+ _drag_icon->get_size (w, h);
+ _drag_icon->drag_set_as_icon (context, w / 2, h);
+
+ _drag_source = this;
+ }
+
+ void drag_data_get (Glib::RefPtr<Gdk::DragContext> const &, Gtk::SelectionData & selection_data, guint, guint, T* child)
+ {
+ selection_data.set (selection_data.get_target(), 8, (const guchar *) &child, sizeof (&child));
+ }
+
+ void drag_data_received (
+ Glib::RefPtr<Gdk::DragContext> const & context, int x, int y, Gtk::SelectionData const & selection_data, guint info, guint time
+ )
+ {
+ /* work out where it was dropped */
+ std::pair<T*, int> const drop = get_child_at_position (x, y);
+
+ if (_drag_source == this) {
+
+ /* dropped from ourselves onto ourselves */
+
+ T* child = *((T **) selection_data.get_data());
+
+ /* reorder child widgets accordingly */
+ if (drop.first == 0) {
+ _internal_vbox.reorder_child (child->widget(), -1);
+ } else {
+ _internal_vbox.reorder_child (child->widget(), drop.second);
+ }
+
+ } else {
+
+ /* drag started in another DnDVBox; raise a signal to say what happened */
+
+ std::list<T*> dropped = _drag_source->selection ();
+ DropFromAnotherBox (_drag_source, drop.first, context);
+ }
+
+ context->drag_finish (false, false, time);
+ }
+
+ void drag_end (Glib::RefPtr<Gdk::DragContext> const &, T *)
+ {
+ delete _drag_icon;
+ _drag_icon = 0;
+
+ Reordered (); /* EMIT SIGNAL */
+ }
+
+ bool button_press (GdkEventButton* ev, T* child)
+ {
+ if (_expecting_unwanted_button_event == true && child == 0) {
+ _expecting_unwanted_button_event = false;
+ return true;
+ }
+
+ if (child) {
+ _expecting_unwanted_button_event = true;
+ }
+
+ if (ev->button == 1 || ev->button == 3) {
+ if (!selected (child)) {
+ clear_selection ();
+ if (child) {
+ add_to_selection (child);
+ }
+ SelectionChanged (); /* EMIT SIGNAL */
+ }
+ }
+
+ return ButtonPress (ev, child); /* EMIT SIGNAL */
+ }
+
+ bool button_release (GdkEventButton* ev, T* child)
+ {
+ if (_expecting_unwanted_button_event == true && child == 0) {
+ _expecting_unwanted_button_event = false;
+ return true;
+ }
+
+ if (child) {
+ _expecting_unwanted_button_event = true;
+ }
+
+ return ButtonRelease (ev, child); /* EMIT SIGNAL */
+ }
+
+ void clear_selection ()
+ {
+ for (typename std::list<T*>::iterator i = _selection.begin(); i != _selection.end(); ++i) {
+ (*i)->action_widget().set_state (Gtk::STATE_NORMAL);
+ }
+ _selection.clear ();
+ }
+
+ void add_to_selection (T* child)
+ {
+ child->action_widget().set_state (Gtk::STATE_SELECTED);
+ _selection.push_back (child);
+ }
+
+ T* child_from_widget (Gtk::Widget const * w) const
+ {
+ typename std::list<T*>::const_iterator i = _children.begin();
+ while (i != _children.end() && &(*i)->widget() != w) {
+ ++i;
+ }
+
+ if (i == _children.end()) {
+ return 0;
+ }
+
+ return *i;
+ }
+
+ Gtk::VBox _internal_vbox;
+ std::list<Gtk::TargetEntry> _targets;
+ std::list<T*> _children;
+ std::list<T*> _selection;
+ Gtk::Window* _drag_icon;
+ bool _expecting_unwanted_button_event;
+
+ static DnDVBox* _drag_source;
+};
+
+template <class T>
+DnDVBox<T>* DnDVBox<T>::_drag_source = 0;
+
+}