13
0

added files

git-svn-id: svn://localhost/trunk/ardour2@67 d708f5d6-7413-0410-9779-e7cbd77b26cf
This commit is contained in:
Paul Davis 2005-10-24 13:43:53 +00:00
parent e0946701e1
commit d8efcf6da2
2 changed files with 170 additions and 0 deletions

View File

@ -0,0 +1,103 @@
#include <cstdio>
#include <gtkmm2ext/dndtreeview.h>
using namespace std;
using namespace sigc;
using namespace Gdk;
using namespace Gtk;
using namespace Glib;
using namespace Gtkmm2ext;
DnDTreeView::DnDTreeView ()
: TreeView ()
{
draggable.push_back (TargetEntry ("GTK_TREE_MODEL_ROW", TARGET_SAME_WIDGET));
enable_model_drag_source (draggable);
enable_model_drag_dest (draggable);
suggested_action = Gdk::DragAction (0);
}
void
DnDTreeView::add_object_drag (int column, string type_name)
{
draggable.push_back (TargetEntry (type_name, TargetFlags(0)));
data_column = column;
enable_model_drag_source (draggable);
enable_model_drag_dest (draggable);
}
DnDTreeView::SerializedObjectPointers*
DnDTreeView::serialize_pointers (RefPtr<TreeModel> model, TreeSelection::ListHandle_Path* selection, ustring type)
{
uint32_t cnt = selection->size();
uint32_t sz = (sizeof (void*) * cnt) + sizeof (SerializedObjectPointers);
char* buf = new char[sz];
SerializedObjectPointers* sr = new (buf) SerializedObjectPointers;
sr->cnt = cnt;
sr->size = sz;
snprintf (sr->type, sizeof (sr->type), "%s", type.c_str());
cnt = 0;
for (TreeSelection::ListHandle_Path::iterator x = selection->begin(); x != selection->end(); ++x, ++cnt) {
TreeModel::Row row = *(model->get_iter (*x));
row.get_value (data_column, sr->ptr[cnt]);
}
return sr;
}
void
DnDTreeView::on_drag_data_get(const RefPtr<DragContext>& context, SelectionData& selection_data, guint info, guint time)
{
if (selection_data.get_target() == "GTK_TREE_MODEL_ROW") {
TreeView::on_drag_data_get (context, selection_data, info, time);
} else {
Gtk::TreeSelection::ListHandle_Path selection = get_selection()->get_selected_rows ();
SerializedObjectPointers* sr = serialize_pointers (get_model(), &selection, selection_data.get_target());
selection_data.set (8, (guchar*)sr, sr->size);
}
}
void
DnDTreeView::on_drag_data_received(const RefPtr<DragContext>& context, int x, int y, const SelectionData& selection_data, guint info, guint time)
{
if (suggested_action) {
/* this is a drag motion callback. just update the status to
say that we are still dragging, and that's it.
*/
suggested_action = Gdk::DragAction (0);
TreeView::on_drag_data_received (context, x, y, selection_data, info, time);
return;
}
if (selection_data.get_target() == "GTK_TREE_MODEL_ROW") {
TreeView::on_drag_data_received (context, x, y, selection_data, info, time);
} else {
/* object D-n-D */
const SerializedObjectPointers* sr = reinterpret_cast<const SerializedObjectPointers *>(selection_data.get_data());
if (sr) {
signal_object_drop (sr->type, sr->cnt, const_cast<void**>(sr->ptr));
}
context->drag_finish (true, false, time);
}
}

View File

@ -0,0 +1,67 @@
#ifndef __gtkmm2ext_dndtreeview_h__
#define __gtkmm2ext_dndtreeview_h__
#include <stdint.h>
#include <string>
#include <gtkmm/treeview.h>
#include <gtkmm/treeselection.h>
#include <gtkmm/selectiondata.h>
namespace Gtkmm2ext {
class DnDTreeView : public Gtk::TreeView
{
private:
public:
DnDTreeView ();
~DnDTreeView() {}
void add_object_drag (int column, std::string type_name);
sigc::signal<void,std::string,uint32_t,void**> signal_object_drop;
void on_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context) {
TreeView::on_drag_begin (context);
}
void on_drag_end(const Glib::RefPtr<Gdk::DragContext>& context) {
TreeView::on_drag_end (context);
}
void on_drag_data_delete(const Glib::RefPtr<Gdk::DragContext>& context) {
TreeView::on_drag_data_delete (context);
}
void on_drag_leave(const Glib::RefPtr<Gdk::DragContext>& context, guint time) {
suggested_action = context->get_suggested_action();
TreeView::on_drag_leave (context, time);
}
bool on_drag_motion(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, guint time) {
suggested_action = context->get_suggested_action();
return TreeView::on_drag_motion (context, x, y, time);
}
bool on_drag_drop(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, guint time) {
suggested_action = Gdk::DragAction (0);
return TreeView::on_drag_drop (context, x, y, time);
}
void on_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, Gtk::SelectionData& selection_data, guint info, guint time);
void on_drag_data_received(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, const Gtk::SelectionData& selection_data, guint info, guint time);
private:
std::list<Gtk::TargetEntry> draggable;
Gdk::DragAction suggested_action;
int data_column;
struct SerializedObjectPointers {
uint32_t size;
uint32_t cnt;
char type[32];
void* ptr[0];
};
SerializedObjectPointers* serialize_pointers (Glib::RefPtr<Gtk::TreeModel> m,
Gtk::TreeSelection::ListHandle_Path*,
Glib::ustring type);
};
} // namespace
#endif /* __gtkmm2ext_dndtreeview_h__ */