# include <iostream>
# include <functional>
# include <boost/tokenizer.hpp>
# include <gtkmm.h>
# include <gtkmm/widget.h>
# include "command_bar.hh"
# include "astroid.hh"
# include "main_window.hh"
# include "modes/mode.hh"
# include "modes/thread_index/thread_index.hh"
# include "modes/help_mode.hh"
# include "modes/saved_searches.hh"
# include "utils/utils.hh"
# include "db.hh"
using namespace std;
namespace Astroid {
CommandBar::CommandBar () {
set_show_close_button ();
connect_entry (entry);
hbox.set_orientation (Gtk::ORIENTATION_HORIZONTAL);
hbox.pack_start (mode_label, false, false, 5);
hbox.pack_start (entry, true, true, 5);
add(hbox);
entry.signal_activate ().connect (
sigc::mem_fun (*this, &CommandBar::on_entry_activated)
);
entry.signal_event ().connect ([&] (GdkEvent* event)->bool {
if (event->type == GDK_KEY_PRESS) {
return entry_key_press ((GdkEventKey *) event);
} else {
return false;
}
});
entry.signal_changed ().connect (
sigc::mem_fun (this, &CommandBar::entry_changed));
/* set up tags */
{
Db db (Db::DbMode::DATABASE_READ_ONLY);
db.load_tags ();
}
tag_completion = refptr<TagCompletion> (new TagCompletion());
search_completion = refptr<SearchCompletion> (new SearchCompletion());
text_search_completion = refptr<SearchTextCompletion> (new SearchTextCompletion ());
difftag_completion = refptr<DiffTagCompletion> (new DiffTagCompletion ());
}
void CommandBar::set_main_window (MainWindow * mw) {
main_window = mw;
}
ustring CommandBar::get_text () {
return entry.get_text ();
}
void CommandBar::set_text (ustring txt) {
entry.set_text (txt);
}
void CommandBar::on_entry_activated () {
/* handle input */
ustring cmd = get_text ();
LOG (debug) << "cb: cmd (in mode: " << mode << "): " << cmd;
switch (mode) {
case CommandMode::Search:
if (callback == NULL && (cmd.size() > 0)) {
Mode * m = new ThreadIndex (main_window, cmd);
/* add to saved searches */
SavedSearches::add_query_to_history (cmd);
main_window->add_mode (m);
}
break;
case CommandMode::Filter:
case CommandMode::SearchText:
{
text_search_completion->add_query (cmd);
}
case CommandMode::AttachMids:
case CommandMode::DiffTag:
case CommandMode::Tag:
{
}
break;
}
if (callback != NULL) callback (cmd);
callback = NULL;
set_search_mode (false); // emits changed -> disables search
}
void CommandBar::enable_command (
CommandMode m,
ustring title,
ustring cmd,
std::function<void(ustring)> f) {
enable_command (m, cmd, f);
mode_label.set_text (title);
}
void CommandBar::enable_command (
CommandMode m,
ustring cmd,
std::function<void(ustring)> f) {
mode = m;
ustring edit_mode_str = astroid->config ().get<string> ("general.tagbar_move");
if (edit_mode_str == "tag") {
edit_mode = EditMode::Tags;
} else {
edit_mode = EditMode::Chars;
}
reset_bar ();
switch (mode) {
case CommandMode::AttachMids:
{
entry.set_icon_from_icon_name ("mail-attachment-symbolic");
start_generic (cmd);
}
break;
case CommandMode::Search:
{
mode_label.set_text ("");
entry.set_icon_from_icon_name ("edit-find-symbolic");
start_searching (cmd);
}
break;
case CommandMode::Filter:
case CommandMode::SearchText:
{
mode_label.set_text ("Find text");
entry.set_icon_from_icon_name ("edit-find-symbolic");
start_text_searching (cmd);
}
break;
case CommandMode::Tag:
{
mode_label.set_text ("Change tags");
entry.set_icon_from_icon_name ("system-run-symbolic");
start_tagging (cmd);
}
break;
case CommandMode::DiffTag:
{
mode_label.set_text