diff --git a/dmenu/dmenu.cc b/dmenu/dmenu.cc index 3a79518..9bf566e 100644 --- a/dmenu/dmenu.cc +++ b/dmenu/dmenu.cc @@ -25,15 +25,16 @@ std::vector all_commands {}; bool dmenu_run = false; bool show_searchbox = true; +bool case_sensitive = true; int main(int argc, char *argv[]) { std::string custom_css_file {"style.css"}; pid_t pid = getpid(); std::string mypid = std::to_string(pid); - + std::string pid_file = "/var/run/user/" + std::to_string(getuid()) + "/nwgdmenu.pid"; - + int saved_pid {}; if (std::ifstream(pid_file)) { try { @@ -70,11 +71,11 @@ int main(int argc, char *argv[]) { // We will build dmenu out of commands found in $PATH if nothing has been passed by stdin dmenu_run = isatty(fileno(stdin)) == 1; - + if (input.cmdOptionExists("-run")){ dmenu_run = true; } - + // Otherwise let's build from stdin input if (!dmenu_run) { all_commands = {}; @@ -293,7 +294,7 @@ int main(int argc, char *argv[]) { break; } } - + menu.set_reserve_toggle_size(false); menu.set_property("width_request", (int)(w / 8)); diff --git a/dmenu/dmenu.h b/dmenu/dmenu.h index 3f54436..06c2792 100644 --- a/dmenu/dmenu.h +++ b/dmenu/dmenu.h @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -36,6 +37,7 @@ extern std::vector all_commands; extern bool dmenu_run; extern bool show_searchbox; +extern bool case_sensitive; class DMenu : public Gtk::Menu { public: diff --git a/dmenu/dmenu_classes.cc b/dmenu/dmenu_classes.cc index db3946d..e77dd51 100644 --- a/dmenu/dmenu_classes.cc +++ b/dmenu/dmenu_classes.cc @@ -47,7 +47,11 @@ bool Anchor::on_focus_in_event(GdkEventFocus* focus_event) { } DMenu::DMenu() { - searchbox.set_text("Type to search"); + if (case_sensitive) { + searchbox.set_text("Type To Search"); + } else { + searchbox.set_text("TYPE TO SEARCH"); + } searchbox.set_sensitive(false); searchbox.set_name("searchbox"); search_phrase = ""; @@ -71,6 +75,9 @@ bool DMenu::on_key_press_event(GdkEventKey* key_event) { && key_event->type == GDK_KEY_PRESS) { char character = key_event -> keyval; + if (!case_sensitive) { + character = toupper(character); + } this -> search_phrase += character; this -> searchbox.set_text(this -> search_phrase); @@ -87,6 +94,11 @@ bool DMenu::on_key_press_event(GdkEventKey* key_event) { this -> searchbox.set_text(this -> search_phrase); this -> filter_view(); return true; + } else if (key_event -> keyval == GDK_KEY_Insert) { + this -> search_phrase = ""; + case_sensitive = !case_sensitive; + this -> filter_view(); + return true; } } //if the event has not been handled, call the base class @@ -116,7 +128,17 @@ void DMenu::filter_view() { int cnt = 0; bool limit_exhausted = false; for (Glib::ustring command : all_commands) { - if (command.find(this -> search_phrase) == 0) { + std::string sf = this -> search_phrase; + std::string cm = command; + if (!case_sensitive) { + for(unsigned int l = 0; l < sf.length(); l++) { + sf[l] = toupper(sf[l]); + } + for(unsigned int l = 0; l < cm.length(); l++) { + cm[l] = toupper(cm[l]); + } + } + if (cm.find(sf) == 0) { Gtk::MenuItem *item = new Gtk::MenuItem(); item -> set_label(command); item -> signal_activate().connect(sigc::bind(sigc::mem_fun @@ -131,11 +153,21 @@ void DMenu::filter_view() { } if (!limit_exhausted) { for (Glib::ustring command : all_commands) { - if (command.find(this -> search_phrase) != std::string::npos && command.find(this -> search_phrase) != 0) { + std::string sf = this -> search_phrase; + std::string cm = command; + if (!case_sensitive) { + for(unsigned int l = 0; l < sf.length(); l++) { + sf[l] = toupper(sf[l]); + } + for(unsigned int l = 0; l < cm.length(); l++) { + cm[l] = toupper(cm[l]); + } + } + if (cm.find(sf) != std::string::npos && cm.find(sf) != 0) { Gtk::MenuItem *item = new Gtk::MenuItem(); item -> set_label(command); item -> signal_activate().connect(sigc::bind(sigc::mem_fun - (*this, &DMenu::on_item_clicked), command)); + (*this, &DMenu::on_item_clicked), cm)); this -> append(*item); cnt++; if (cnt > rows - 1) { @@ -147,7 +179,11 @@ void DMenu::filter_view() { this -> show_all(); } else { - this -> searchbox.set_text("Type to search"); + if (case_sensitive) { + this -> searchbox.set_text("Type To Search"); + } else { + this -> searchbox.set_text("TYPE TO SEARCH"); + } // remove all items except searchbox for (auto item : this -> get_children()) { if (item -> get_name() != "search_item") {