如何将文件删除到哪个gtkmm图像小部件

时间:2013-09-03 12:47:48

标签: c++ drag-and-drop event-handling gtk gtkmm

....
std::vector<Gtk::TargetEntry> listTargets;
listTargets.push_back( Gtk::TargetEntry("STRING") );
listTargets.push_back( Gtk::TargetEntry("text/plain") );

image1->drag_dest_set(listTargets);
image1->signal_drag_data_received().connect(sigc::mem_fun(*this,
              &mainWindow::drop_event) );

image2->drag_dest_set(listTargets);
image2->signal_drag_data_received().connect(sigc::mem_fun(*this,
                  &mainWindow::drop_event) );
....

和我的drop&amp; drop事件处理函数:

void mainWindow::drop_event(
        const Glib::RefPtr<Gdk::DragContext>& context, int, int,
        const Gtk::SelectionData& selection_data, guint, guint time)
{
    std::cout << selection_data.get_data_as_string()  << std::endl;
}

我可以使用此代码获取“拖动到图像小部件”的文件位置。输出是这样的:

file:////opt/google/chrome/google-chrome.desktop
file:////var/www/index.html
file:///opt/libreoffice4.1/LICENSE.html

没关系,我可以。但是,我怎么能得到:文件被丢弃到哪个图像(image1或image2小部件),如下所示:

dropped to **image1** : file:////opt/google/chrome/google-chrome.desktop
dropped to **image2** : file:////var/www/index.html
dropped to **image1** : file:///opt/libreoffice4.1/LICENSE.html

感谢...

1 个答案:

答案 0 :(得分:1)

sigc allows you to bind extra arguments to your handlers.

Hander成为:

void mainWindow::drop_event(
        const Glib::RefPtr<Gdk::DragContext>& context, int, int,
        const Gtk::SelectionData& selection_data, guint, guint time, 
        Glib::ustring whichImage)
{
    std::cout << "dropped to" << whichImage << ":" << selection_data.get_data_as_string()  << std::endl;
}

然后connect是:

image1->signal_drag_data_received().connect(sigc::bind<Glib::ustring>(sigc::mem_fun(*this,
              &mainWindow::drop_event), "image1" ));

image2->signal_drag_data_received().connect( sigc::bind<Glib::ustring>(sigc::mem_fun(*this,
                  &mainWindow::drop_event), "image2"));