Browse Source

Added drag-and-drop in directory view, and WiP right click

merge-requests/365/head
eidheim 10 years ago
parent
commit
33794a95b5
  1. 106
      src/directories.cc
  2. 43
      src/directories.h

106
src/directories.cc

@ -3,6 +3,8 @@
#include <algorithm> #include <algorithm>
#include <unordered_set> #include <unordered_set>
#include "source.h" #include "source.h"
#include "terminal.h"
#include "notebook.h"
#include <iostream> //TODO: remove #include <iostream> //TODO: remove
using namespace std; //TODO: remove using namespace std; //TODO: remove
@ -21,10 +23,87 @@ namespace sigc {
#endif #endif
} }
bool Directories::TreeStore::row_drop_possible_vfunc(const Gtk::TreeModel::Path& path, const Gtk::SelectionData& selection_data) const {
return true;
}
bool Directories::TreeStore::drag_data_received_vfunc(const TreeModel::Path &path, const Gtk::SelectionData &selection_data) {
auto &directories=Directories::get();
auto get_target_folder=[this, &directories](const TreeModel::Path &path) {
if(path.size()==1)
return directories.current_path;
else {
auto it=get_iter(path);
if(it) {
auto prev_path=path;
prev_path.up();
it=get_iter(prev_path);
if(it)
return it->get_value(directories.column_record.path);
}
else {
auto prev_path=path;
prev_path.up();
if(prev_path.size()==1)
return directories.current_path;
else {
prev_path.up();
it=get_iter(prev_path);
if(it)
return it->get_value(directories.column_record.path);
}
}
}
return boost::filesystem::path();
};
auto it=directories.get_selection()->get_selected();
if(it) {
auto source_path=it->get_value(directories.column_record.path);
auto target_path=get_target_folder(path);
target_path/=source_path.filename();
if(source_path==target_path)
return false;
if(boost::filesystem::exists(target_path)) {
Terminal::get().print("Error: Could not move file: "+target_path.string()+" already exists\n", true);
return false;
}
boost::system::error_code ec;
boost::filesystem::rename(source_path, target_path, ec);
if(ec) {
Terminal::get().print("Error: Could not move file: "+ec.message()+'\n', true);
return false;
}
for(int c=0;c<Notebook::get().size();c++) {
auto view=Notebook::get().get_view(c);
if(view->file_path==source_path) {
view->file_path=target_path;
break;
}
}
Directories::get().update();
directories.select(target_path);
}
return false;
}
bool Directories::TreeStore::drag_data_delete_vfunc (const Gtk::TreeModel::Path &path) {
return false;
}
Directories::Directories() : Gtk::TreeView(), stop_update_thread(false) { Directories::Directories() : Gtk::TreeView(), stop_update_thread(false) {
this->set_enable_tree_lines(true); this->set_enable_tree_lines(true);
tree_store = Gtk::TreeStore::create(column_record); tree_store = TreeStore::create();
tree_store->set_column_types(column_record);
set_model(tree_store); set_model(tree_store);
append_column("", column_record.name); append_column("", column_record.name);
auto renderer=dynamic_cast<Gtk::CellRendererText*>(get_column(0)->get_first_cell()); auto renderer=dynamic_cast<Gtk::CellRendererText*>(get_column(0)->get_first_cell());
@ -112,6 +191,18 @@ Directories::Directories() : Gtk::TreeView(), stop_update_thread(false) {
update_mutex.unlock(); update_mutex.unlock();
} }
}); });
enable_model_drag_source();
enable_model_drag_dest();
menu_item_delete.set_label("Delete");
menu_item_delete.signal_activate().connect([this] {
std::cout << "delete " << menu_popup_row_path << std::endl;
});
menu.append(menu_item_delete);
menu.show_all();
menu.accelerate(*this);
} }
Directories::~Directories() { Directories::~Directories() {
@ -209,6 +300,19 @@ void Directories::select(const boost::filesystem::path &path) {
JDEBUG("end"); JDEBUG("end");
} }
bool Directories::on_button_press_event(GdkEventButton* event) {
if(event->type==GDK_BUTTON_PRESS && event->button==3) {
Gtk::TreeModel::Path path;
if(get_path_at_pos(static_cast<int>(event->x), static_cast<int>(event->y), path)) {
menu_popup_row_path=get_model()->get_iter(path)->get_value(column_record.path);
menu.popup(event->button, event->time);
return true;
}
}
return Gtk::TreeView::on_button_press_event(event);
}
void Directories::add_path(const boost::filesystem::path& dir_path, const Gtk::TreeModel::Row &parent) { void Directories::add_path(const boost::filesystem::path& dir_path, const Gtk::TreeModel::Row &parent) {
boost::system::error_code ec; boost::system::error_code ec;
auto last_write_time=boost::filesystem::last_write_time(dir_path, ec); auto last_write_time=boost::filesystem::last_write_time(dir_path, ec);

43
src/directories.h

@ -13,18 +13,30 @@
class Directories : public Gtk::TreeView { class Directories : public Gtk::TreeView {
public: public:
class ColumnRecord : public Gtk::TreeModel::ColumnRecord { class TreeStore : public Gtk::TreeStore {
protected:
TreeStore() {}
bool row_drop_possible_vfunc(const Gtk::TreeModel::Path& path, const Gtk::SelectionData& selection_data) const override;
bool drag_data_received_vfunc(const TreeModel::Path &path, const Gtk::SelectionData &selection_data) override;
bool drag_data_delete_vfunc (const Gtk::TreeModel::Path &path) override;
public: public:
ColumnRecord() { class ColumnRecord : public Gtk::TreeModel::ColumnRecord {
add(id); public:
add(name); ColumnRecord() {
add(path); add(id);
add(color); add(name);
} add(path);
Gtk::TreeModelColumn<std::string> id; add(color);
Gtk::TreeModelColumn<std::string> name; }
Gtk::TreeModelColumn<boost::filesystem::path> path; Gtk::TreeModelColumn<std::string> id;
Gtk::TreeModelColumn<Gdk::RGBA> color; Gtk::TreeModelColumn<std::string> name;
Gtk::TreeModelColumn<boost::filesystem::path> path;
Gtk::TreeModelColumn<Gdk::RGBA> color;
};
static Glib::RefPtr<TreeStore> create() {return Glib::RefPtr<TreeStore>(new TreeStore());}
}; };
private: private:
@ -43,10 +55,13 @@ public:
std::unique_ptr<CMake> cmake; std::unique_ptr<CMake> cmake;
boost::filesystem::path current_path; boost::filesystem::path current_path;
protected:
bool on_button_press_event(GdkEventButton* event) override;
private: private:
void add_path(const boost::filesystem::path& dir_path, const Gtk::TreeModel::Row &row); void add_path(const boost::filesystem::path& dir_path, const Gtk::TreeModel::Row &row);
Glib::RefPtr<Gtk::TreeStore> tree_store; Glib::RefPtr<Gtk::TreeStore> tree_store;
ColumnRecord column_record; TreeStore::ColumnRecord column_record;
std::unordered_map<std::string, std::pair<Gtk::TreeModel::Row, std::time_t> > last_write_times; std::unordered_map<std::string, std::pair<Gtk::TreeModel::Row, std::time_t> > last_write_times;
std::mutex update_mutex; std::mutex update_mutex;
@ -54,6 +69,10 @@ private:
std::atomic<bool> stop_update_thread; std::atomic<bool> stop_update_thread;
Dispatcher dispatcher; Dispatcher dispatcher;
std::vector<std::string> update_paths; std::vector<std::string> update_paths;
Gtk::Menu menu;
Gtk::MenuItem menu_item_delete;
boost::filesystem::path menu_popup_row_path;
}; };
#endif // JUCI_DIRECTORIES_H_ #endif // JUCI_DIRECTORIES_H_

Loading…
Cancel
Save