1 /* 2 * Copyright (C) 2013-2020 Graeme Gott <graeme@gottcode.org> 3 * 4 * This library is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this library. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #ifndef WHISKERMENU_ELEMENT_H 19 #define WHISKERMENU_ELEMENT_H 20 21 #include "util.h" 22 23 #include <climits> 24 25 #include <gdk/gdk.h> 26 27 namespace WhiskerMenu 28 { 29 30 class Query; 31 32 class Element 33 { 34 public: ~Element()35 virtual ~Element() 36 { 37 if (m_icon) 38 { 39 g_object_unref(m_icon); 40 } 41 g_free(m_text); 42 g_free(m_tooltip); 43 g_free(m_sort_key); 44 } 45 46 Element(const Element&) = delete; 47 Element(Element&&) = delete; 48 Element& operator=(const Element&) = delete; 49 Element& operator=(Element&&) = delete; 50 get_icon()51 GIcon* get_icon() const 52 { 53 return m_icon; 54 } 55 get_text()56 const gchar* get_text() const 57 { 58 return m_text; 59 } 60 get_tooltip()61 const gchar* get_tooltip() const 62 { 63 return m_tooltip; 64 } 65 run(GdkScreen *)66 virtual void run(GdkScreen*) const 67 { 68 } 69 search(const Query &)70 virtual unsigned int search(const Query&) 71 { 72 return UINT_MAX; 73 } 74 less_than(const Element * lhs,const Element * rhs)75 static bool less_than(const Element* lhs, const Element* rhs) 76 { 77 return g_strcmp0(lhs->m_sort_key, rhs->m_sort_key) < 0; 78 } 79 80 protected: 81 Element() = default; 82 83 void set_icon(const gchar* icon, bool use_fallbacks = false); 84 set_text(const gchar * text)85 void set_text(const gchar* text) 86 { 87 g_free(m_text); 88 g_free(m_sort_key); 89 m_text = g_strdup(text); 90 m_sort_key = g_utf8_collate_key(m_text, -1); 91 } 92 set_text(gchar * text)93 void set_text(gchar* text) 94 { 95 g_free(m_text); 96 g_free(m_sort_key); 97 m_text = text; 98 m_sort_key = g_utf8_collate_key(m_text, -1); 99 } 100 set_tooltip(const gchar * tooltip)101 void set_tooltip(const gchar* tooltip) 102 { 103 g_free(m_tooltip); 104 m_tooltip = !xfce_str_is_empty(tooltip) ? g_markup_escape_text(tooltip, -1) : nullptr; 105 } 106 107 void spawn(GdkScreen* screen, const gchar* command, const gchar* working_directory, gboolean startup_notify, const gchar* icon_name) const; 108 109 private: 110 GIcon* m_icon = nullptr; 111 gchar* m_text = nullptr; 112 gchar* m_tooltip = nullptr; 113 gchar* m_sort_key = nullptr; 114 }; 115 116 } 117 118 #endif // WHISKERMENU_ELEMENT_H 119