Browse Source

Moved completiondialog-code to seperate file: selectiondialog.h/cc. The selectiondialog is now started from Source::ClangView. Some cleanup. See source.cc lines 340-345 for example use of SelectionDialog.

merge-requests/365/head
eidheim 11 years ago
parent
commit
d33df03dd7
  1. 3
      juci/CMakeLists.txt
  2. 195
      juci/notebook.cc
  3. 17
      juci/notebook.h
  4. 72
      juci/source.cc
  5. 1
      juci/source.h
  6. 7
      juci/window.cc
  7. 1
      juci/window.h

3
juci/CMakeLists.txt

@ -99,6 +99,7 @@ endif()
# name of the executable on Windows will be example.exe # name of the executable on Windows will be example.exe
add_executable(${project_name} add_executable(${project_name}
#list of every needed file to create the executable #list of every needed file to create the executable
juci.h
juci.cc juci.cc
keybindings.h keybindings.h
keybindings.cc keybindings.cc
@ -106,6 +107,8 @@ add_executable(${project_name}
menu.cc menu.cc
source.h source.h
source.cc source.cc
selectiondialog.h
selectiondialog.cc
config.h config.h
config.cc config.cc
sourcefile.h sourcefile.h

195
juci/notebook.cc

@ -22,7 +22,6 @@ Notebook::Controller::Controller(Gtk::Window* window,
INFO("Create notebook"); INFO("Create notebook");
window_ = window; window_ = window;
refClipboard_ = Gtk::Clipboard::get(); refClipboard_ = Gtk::Clipboard::get();
ispopup = false;
view().pack1(directories_.widget(), true, true); view().pack1(directories_.widget(), true, true);
CreateKeybindings(keybindings); CreateKeybindings(keybindings);
INFO("Notebook Controller Success"); INFO("Notebook Controller Success");
@ -176,114 +175,6 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller
INFO("Notebook signal handlers sucsess"); INFO("Notebook signal handlers sucsess");
} }
bool Notebook::Controller:: OnMouseRelease(GdkEventButton* button) {
if (button->button == 1 && ispopup) {
popup_.response(Gtk::RESPONSE_DELETE_EVENT);
return true;
}
return false;
}
bool Notebook::Controller::OnKeyRelease(GdkEventKey* key) {
return GeneratePopup(key->keyval);
}
bool Notebook::Controller::GeneratePopup(int key_id) {
INFO("Notebook genereate popup, getting iters");
std::string path = text_vec_.at(CurrentPage())->view->file_path;
if (!source_config().legal_extension(path.substr(path.find_last_of(".") + 1))) return false;
// Get function to fill popup with suggests item vector under is for testing
Gtk::TextIter beg = CurrentTextView().get_buffer()->get_insert()->get_iter();
Gtk::TextIter end = CurrentTextView().get_buffer()->get_insert()->get_iter();
Gtk::TextIter tmp = CurrentTextView().get_buffer()->get_insert()->get_iter();
Gtk::TextIter tmp1 = CurrentTextView().get_buffer()->get_insert()->get_iter();
Gtk::TextIter line =
CurrentTextView().get_buffer()->get_iter_at_line(tmp.get_line());
if (end.backward_char() && end.backward_char()) {
bool illegal_chars =
end.backward_search("\"", Gtk::TEXT_SEARCH_VISIBLE_ONLY, tmp, tmp1, line)
||
end.backward_search("//", Gtk::TEXT_SEARCH_VISIBLE_ONLY, tmp, tmp1, line);
INFO("Notebook genereate popup, checking key_id");
if (illegal_chars) {
return false;
}
std::string c = text_vec_[CurrentPage()]->buffer()->get_text(end, beg);
switch (key_id) {
case 46:
break;
case 58:
if (c != "::") return false;
break;
case 60:
if (c != "->") return false;
break;
case 62:
if (c != "->") return false;
break;
default:
return false;
}
} else {
return false;
}
INFO("Notebook genereate popup, getting autocompletions");
std::vector<Source::AutoCompleteData> acdata=text_vec_.at(CurrentPage())->view->
get_autocomplete_suggestions(beg.get_line()+1,
beg.get_line_offset()+2);
std::map<std::string, std::string> items;
for (auto &data : acdata) {
std::stringstream ss;
std::string return_value;
for (auto &chunk : data.chunks) {
switch (chunk.kind) {
case clang::CompletionChunk_ResultType:
return_value = chunk.chunk;
break;
case clang::CompletionChunk_Informative: break;
default: ss << chunk.chunk; break;
}
}
if (ss.str().length() > 0) { // if length is 0 the result is empty
items[ss.str() + " --> " + return_value] = ss.str();
}
}
Gtk::ScrolledWindow popup_scroll_;
Gtk::ListViewText listview_(1, false, Gtk::SelectionMode::SELECTION_SINGLE);
popup_scroll_.set_policy(Gtk::PolicyType::POLICY_NEVER,
Gtk::PolicyType::POLICY_NEVER);
listview_.set_enable_search(true);
listview_.set_headers_visible(false);
listview_.set_hscroll_policy(Gtk::ScrollablePolicy::SCROLL_NATURAL);
listview_.set_activate_on_single_click(true);
if (items.empty()) {
items["No suggestions found..."] = "";
}
for (auto &i : items) {
listview_.append(i.first);
}
popup_scroll_.add(listview_);
popup_.get_vbox()->pack_start(popup_scroll_);
popup_.set_transient_for(*window_);
popup_.show_all();
INFO("Notebook genereate popup, moving popup");
int popup_x = popup_.get_width();
int popup_y = items.size() * 20;
PopupSetSize(popup_scroll_, popup_x, popup_y);
int x, y;
FindPopupPosition(CurrentTextView(), popup_x, popup_y, x, y);
popup_.move(x, y+15);
INFO("Notebook genereate popup, create handler");
PopupSelectHandler(popup_, listview_, &items);
ispopup = true;
INFO("Notebook genereate popup, run popup");
popup_.run();
INFO("Notebook genereate popup, hide popup");
popup_.hide();
ispopup = false;
return true;
}
Notebook::Controller::~Controller() { Notebook::Controller::~Controller() {
INFO("Notebook destructor"); INFO("Notebook destructor");
for (auto &i : editor_vec_) delete i; for (auto &i : editor_vec_) delete i;
@ -467,12 +358,6 @@ void Notebook::Controller::BufferChangeHandler(Glib::RefPtr<Gtk::TextBuffer>
} }
void Notebook::Controller::set_source_handlers(Source::Controller& controller) { void Notebook::Controller::set_source_handlers(Source::Controller& controller) {
controller.view->signal_button_release_event().
connect(sigc::mem_fun(*this, &Notebook::Controller::OnMouseRelease), false);
controller.view->signal_key_release_event().
connect(sigc::mem_fun(*this, &Notebook::Controller::OnKeyRelease), false);
//Add star on tab label when the page is not saved: //Add star on tab label when the page is not saved:
controller.buffer()->signal_changed().connect([this]() { controller.buffer()->signal_changed().connect([this]() {
if(text_vec_.at(CurrentPage())->is_saved) { if(text_vec_.at(CurrentPage())->is_saved) {
@ -487,86 +372,6 @@ void Notebook::Controller::set_source_handlers(Source::Controller& controller) {
}); });
} }
void Notebook::Controller::PopupSelectHandler(Gtk::Dialog &popup,
Gtk::ListViewText &listview,
std::map<std::string, std::string>
*items) {
listview.signal_row_activated().
connect([this, &listview, &popup, items](const Gtk::TreeModel::Path& path,
Gtk::TreeViewColumn*) {
std::string selected = items->
at(listview.get_text(listview.get_selected()[0]));
CurrentTextView().get_buffer()->insert_at_cursor(selected);
popup.response(Gtk::RESPONSE_DELETE_EVENT);
});
}
void Notebook::Controller::PopupSetSize(Gtk::ScrolledWindow &scroll,
int &current_x,
int &current_y) {
INFO("Notebook popup set size");
int textview_x = CurrentTextView().get_width();
int textview_y = 150;
bool is_never_scroll_x = true;
bool is_never_scroll_y = true;
if (current_x > textview_x) {
current_x = textview_x;
is_never_scroll_x = false;
}
if (current_y > textview_y) {
current_y = textview_y;
is_never_scroll_y = false;
}
scroll.set_size_request(current_x, current_y);
if (!is_never_scroll_x && !is_never_scroll_y) {
scroll.set_policy(Gtk::PolicyType::POLICY_AUTOMATIC,
Gtk::PolicyType::POLICY_AUTOMATIC);
} else if (!is_never_scroll_x && is_never_scroll_y) {
scroll.set_policy(Gtk::PolicyType::POLICY_AUTOMATIC,
Gtk::PolicyType::POLICY_NEVER);
} else if (is_never_scroll_x && !is_never_scroll_y) {
scroll.set_policy(Gtk::PolicyType::POLICY_NEVER,
Gtk::PolicyType::POLICY_AUTOMATIC);
}
}
std::string Notebook::Controller::CurrentPagePath(){
return text_vec_.at(CurrentPage())->view->file_path;
}
void Notebook::Controller::FindPopupPosition(Gtk::TextView& textview,
int popup_x,
int popup_y,
int &x,
int &y) {
INFO("Notebook popup find position");
Gdk::Rectangle temp1, temp2;
textview.get_cursor_locations(
CurrentTextView().
get_buffer()->get_insert()->
get_iter(), temp1, temp2);
int textview_edge_x = 0;
int textview_edge_y = 0;
textview.buffer_to_window_coords(
Gtk::TextWindowType::TEXT_WINDOW_WIDGET,
temp1.get_x(),
temp1.get_y(),
x, y);
Glib::RefPtr<Gdk::Window> gdkw =
CurrentTextView().get_window(Gtk::TextWindowType::TEXT_WINDOW_WIDGET);
gdkw->get_origin(textview_edge_x, textview_edge_y);
x += textview_edge_x;
y += textview_edge_y;
if ((textview_edge_x-x)*-1 > textview.get_width()-popup_x) {
x -= popup_x;
if (x < textview_edge_x) x = textview_edge_x;
}
if ((textview_edge_y-y)*-1 > textview.get_height()-popup_y) {
y -= (popup_y+14) + 15;
if (x < textview_edge_y) y = textview_edge_y +15;
}
}
bool Notebook::Controller:: OnSaveFile() { bool Notebook::Controller:: OnSaveFile() {
std::string path=text_vec_.at(CurrentPage())->view->file_path; std::string path=text_vec_.at(CurrentPage())->view->file_path;
return OnSaveFile(path); return OnSaveFile(path);

17
juci/notebook.h

@ -63,30 +63,15 @@ namespace Notebook {
int Pages(); int Pages();
Directories::Controller& directories() { return directories_; } Directories::Controller& directories() { return directories_; }
Gtk::Paned& view(); Gtk::Paned& view();
bool GeneratePopup(int key);
void Search(bool forward); void Search(bool forward);
Source::Config& source_config() { return source_config_; } Source::Config& source_config() { return source_config_; }
bool OnMouseRelease(GdkEventButton* button);
bool OnKeyRelease(GdkEventKey* key);
std::string OnSaveFileAs(); std::string OnSaveFileAs();
bool LegalExtension(std::string extension); bool LegalExtension(std::string extension);
std::string project_path; std::string project_path;
protected: protected:
void set_source_handlers(Source::Controller& controller); void set_source_handlers(Source::Controller& controller);
void PopupSelectHandler(Gtk::Dialog &popup,
Gtk::ListViewText &listview,
std::map<std::string, std::string>
*items);
private: private:
void CreateKeybindings(Keybindings::Controller& keybindings); void CreateKeybindings(Keybindings::Controller& keybindings);
void FindPopupPosition(Gtk::TextView& textview,
int popup_x,
int popup_y,
int &x,
int &y);
void PopupSetSize(Gtk::ScrolledWindow& scroll,
int &current_x,
int &current_y);
void AskToSaveDialog(); void AskToSaveDialog();
Glib::RefPtr<Gtk::Builder> m_refBuilder; Glib::RefPtr<Gtk::Builder> m_refBuilder;
Glib::RefPtr<Gio::SimpleActionGroup> refActionGroup; Glib::RefPtr<Gio::SimpleActionGroup> refActionGroup;
@ -104,8 +89,6 @@ namespace Notebook {
Gtk::TextIter search_match_end_; Gtk::TextIter search_match_end_;
Gtk::TextIter search_match_start_; Gtk::TextIter search_match_start_;
Glib::RefPtr<Gtk::Clipboard> refClipboard_; Glib::RefPtr<Gtk::Clipboard> refClipboard_;
bool ispopup;
Gtk::Dialog popup_;
Gtk::Window* window_; Gtk::Window* window_;
}; // class controller }; // class controller
} // namespace Notebook } // namespace Notebook

72
juci/source.cc

@ -6,6 +6,7 @@
#include "logging.h" #include "logging.h"
#include <algorithm> #include <algorithm>
#include <regex> #include <regex>
#include "selectiondialog.h"
bool Source::Config::legal_extension(std::string e) const { bool Source::Config::legal_extension(std::string e) const {
std::transform(e.begin(), e.end(),e.begin(), ::tolower); std::transform(e.begin(), e.end(),e.begin(), ::tolower);
@ -119,6 +120,7 @@ parse_thread_go(true), parse_thread_mapped(false), parse_thread_stop(false) {
}); });
signal_key_press_event().connect(sigc::mem_fun(*this, &Source::ClangView::on_key_press), false); signal_key_press_event().connect(sigc::mem_fun(*this, &Source::ClangView::on_key_press), false);
signal_key_release_event().connect(sigc::mem_fun(*this, &Source::ClangView::on_key_release), false);
} }
Source::ClangView::~ClangView() { Source::ClangView::~ClangView() {
@ -275,6 +277,76 @@ highlight_token(clang::Token *token,
end_offset), token_kind); end_offset), token_kind);
} }
bool Source::ClangView::on_key_release(GdkEventKey* key) {
INFO("Source::ClangView::on_key_release getting iters");
// Get function to fill popup with suggests item vector under is for testing
Gtk::TextIter beg = get_source_buffer()->get_insert()->get_iter();
Gtk::TextIter end = get_source_buffer()->get_insert()->get_iter();
Gtk::TextIter tmp = get_source_buffer()->get_insert()->get_iter();
Gtk::TextIter tmp1 = get_source_buffer()->get_insert()->get_iter();
Gtk::TextIter line = get_source_buffer()->get_iter_at_line(tmp.get_line());
if (end.backward_char() && end.backward_char()) {
bool illegal_chars =
end.backward_search("\"", Gtk::TEXT_SEARCH_VISIBLE_ONLY, tmp, tmp1, line)
||
end.backward_search("//", Gtk::TEXT_SEARCH_VISIBLE_ONLY, tmp, tmp1, line);
INFO("Source::ClangView::on_key_release checking key->keyval");
if (illegal_chars) {
return false;
}
std::string c = get_source_buffer()->get_text(end, beg);
switch (key->keyval) {
case 46:
break;
case 58:
if (c != "::") return false;
break;
case 60:
if (c != "->") return false;
break;
case 62:
if (c != "->") return false;
break;
default:
return false;
}
} else {
return false;
}
INFO("Source::ClangView::on_key_release getting autocompletions");
std::vector<Source::AutoCompleteData> acdata=get_autocomplete_suggestions(beg.get_line()+1,
beg.get_line_offset()+2);
std::map<std::string, std::string> rows;
for (auto &data : acdata) {
std::stringstream ss;
std::string return_value;
for (auto &chunk : data.chunks) {
switch (chunk.kind) {
case clang::CompletionChunk_ResultType:
return_value = chunk.chunk;
break;
case clang::CompletionChunk_Informative: break;
default: ss << chunk.chunk; break;
}
}
if (ss.str().length() > 0) { // if length is 0 the result is empty
rows[ss.str() + " --> " + return_value] = ss.str();
}
}
if (rows.empty()) {
rows["No suggestions found..."] = "";
}
SelectionDialog selection_dialog(*this);
selection_dialog.on_select=[this, &rows](Gtk::ListViewText& list_view_text){
std::string selected = rows.at(list_view_text.get_text(list_view_text.get_selected()[0]));
get_source_buffer()->insert_at_cursor(selected);
};
selection_dialog.show(rows);
return true;
}
//TODO: replace indentation methods with a better implementation or //TODO: replace indentation methods with a better implementation or
//maybe use libclang //maybe use libclang
bool Source::ClangView::on_key_press(GdkEventKey* key) { bool Source::ClangView::on_key_press(GdkEventKey* key) {

1
juci/source.h

@ -103,6 +103,7 @@ namespace Source {
std::vector<Range> *source_ranges); std::vector<Range> *source_ranges);
std::vector<std::string> get_compilation_commands(); std::vector<std::string> get_compilation_commands();
bool on_key_press(GdkEventKey* key); bool on_key_press(GdkEventKey* key);
bool on_key_release(GdkEventKey* key);
Glib::Dispatcher parse_done; Glib::Dispatcher parse_done;
Glib::Dispatcher parse_start; Glib::Dispatcher parse_start;

7
juci/window.cc

@ -104,10 +104,6 @@ Window::Window() :
execute.detach(); execute.detach();
} }
}); });
this->signal_button_release_event().
connect(sigc::mem_fun(*this,&Window::OnMouseRelease),false);
terminal_.Terminal().signal_button_release_event().
connect(sigc::mem_fun(*this,&Window::OnMouseRelease),false);
add_accel_group(keybindings_.ui_manager_menu()->get_accel_group()); add_accel_group(keybindings_.ui_manager_menu()->get_accel_group());
add_accel_group(keybindings_.ui_manager_hidden()->get_accel_group()); add_accel_group(keybindings_.ui_manager_hidden()->get_accel_group());
@ -203,9 +199,6 @@ void Window::OnOpenFile() {
} }
} }
} }
bool Window::OnMouseRelease(GdkEventButton *button){
return notebook_.OnMouseRelease(button);
}
bool Window::SaveFile() { bool Window::SaveFile() {
if(notebook_.OnSaveFile()) { if(notebook_.OnSaveFile()) {

1
juci/window.h

@ -32,7 +32,6 @@ public:
void OnWindowHide(); void OnWindowHide();
void OnOpenFile(); void OnOpenFile();
void OnFileOpenFolder(); void OnFileOpenFolder();
bool OnMouseRelease(GdkEventButton* button);
bool SaveFile(); bool SaveFile();
bool SaveFileAs(); bool SaveFileAs();
}; };

Loading…
Cancel
Save