| 1 |
/* Copyright (C) 2003 The gtkmm Development Team |
| 2 |
* |
| 3 |
* This library is free software; you can redistribute it and/or |
| 4 |
* modify it under the terms of the GNU Lesser General Public |
| 5 |
* License as published by the Free Software Foundation; either |
| 6 |
* version 2.1 of the License, or (at your option) any later version. |
| 7 |
* |
| 8 |
* This library is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
* Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public |
| 14 |
* License along with this library; if not, see <https://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <gtkmm/widget.h> |
| 18 |
#include <gtkmm/celllayout.h> |
| 19 |
#include <gtkmm/treemodel.h> |
| 20 |
|
| 21 |
_DEFS(gtkmm,gtk) |
| 22 |
_PINCLUDE(glibmm/private/object_p.h) |
| 23 |
|
| 24 |
|
| 25 |
namespace Gtk |
| 26 |
{ |
| 27 |
|
| 28 |
class GTKMM_API Entry; |
| 29 |
|
| 30 |
/** Completion functionality for Gtk::Entry. |
| 31 |
* |
| 32 |
* Gtk::EntryCompletion is an auxiliary object to be used in conjunction with |
| 33 |
* Gtk::Entry to provide the completion functionality. |
| 34 |
* |
| 35 |
* It derives from Gtk::CellLayout, to allow the user to add extra cells to the Gtk::TreeView with completion matches. |
| 36 |
* |
| 37 |
* "Completion functionality" means that when the user modifies the text in the |
| 38 |
* entry, Gtk::EntryCompletion checks which rows in the model match the current |
| 39 |
* content of the entry, and displays a list of matches. By default, the |
| 40 |
* matching is done by comparing the entry text case-insensitively against |
| 41 |
* the text column of the model (see set_text_column()), |
| 42 |
* but this can be overridden with a custom match function (see set_match_func()). |
| 43 |
* |
| 44 |
* When the user selects a completion, the content of the entry is updated. |
| 45 |
* By default, the content of the entry is replaced by the text column of the |
| 46 |
* model, but this can be overridden by connecting to the match_selected signal |
| 47 |
* and updating the entry in the signal handler. Note that you should return true |
| 48 |
* from the signal handler to suppress the default behaviour. |
| 49 |
* |
| 50 |
* To add completion functionality to an entry, use Gtk::Entry::set_completion(). |
| 51 |
* |
| 52 |
* In addition to regular completion matches, which will be inserted into |
| 53 |
* the entry when they are selected, Gtk::EntryCompletion also allows the display of |
| 54 |
* "actions" in the popup window. Their appearance is similar to menu items, |
| 55 |
* to differentiate them clearly from completion strings. When an action is |
| 56 |
* selected, the action_activated signal is emitted. |
| 57 |
*/ |
| 58 |
class GTKMM_API EntryCompletion |
| 59 |
: public Glib::Object, |
| 60 |
public Gtk::CellLayout, |
| 61 |
public Gtk::Buildable |
| 62 |
{ |
| 63 |
_CLASS_GOBJECT(EntryCompletion, GtkEntryCompletion, GTK_ENTRY_COMPLETION, Glib::Object, GObject, , , GTKMM_API) |
| 64 |
_IMPLEMENTS_INTERFACE(CellLayout) |
| 65 |
_IMPLEMENTS_INTERFACE(Buildable) |
| 66 |
|
| 67 |
protected: |
| 68 |
_CTOR_DEFAULT() |
| 69 |
|
| 70 |
public: |
| 71 |
_WRAP_CREATE() |
| 72 |
|
| 73 |
//Careful, this actually returns a GtkWidget*, so it might not always be a GtkEntry in future GTK+ versions. |
| 74 |
_WRAP_METHOD(Entry* get_entry(), gtk_entry_completion_get_entry) |
| 75 |
_WRAP_METHOD(const Entry* get_entry() const, gtk_entry_completion_get_entry) |
| 76 |
|
| 77 |
_WRAP_METHOD(void set_model(const Glib::RefPtr<TreeModel>& model), gtk_entry_completion_set_model) |
| 78 |
_WRAP_METHOD(Glib::RefPtr<TreeModel> get_model(), gtk_entry_completion_get_model, refreturn) |
| 79 |
_WRAP_METHOD(Glib::RefPtr<const TreeModel> get_model() const, gtk_entry_completion_get_model, refreturn, constversion) |
| 80 |
|
| 81 |
/** Remove the model from the EntryCompletion. |
| 82 |
* |
| 83 |
* @see set_model(). |
| 84 |
* |
| 85 |
* @newin{2,16} |
| 86 |
*/ |
| 87 |
void unset_model(); |
| 88 |
|
| 89 |
/// For example, bool on_match(const Glib::ustring& key, const TreeModel::const_iterator& iter); |
| 90 |
typedef sigc::slot<bool, const Glib::ustring&, const TreeModel::const_iterator&> SlotMatch; |
| 91 |
|
| 92 |
void set_match_func(const SlotMatch& slot); |
| 93 |
_IGNORE(gtk_entry_completion_set_match_func) |
| 94 |
|
| 95 |
_WRAP_METHOD(void set_minimum_key_length(int length), gtk_entry_completion_set_minimum_key_length) |
| 96 |
_WRAP_METHOD(int get_minimum_key_length() const, gtk_entry_completion_get_minimum_key_length) |
| 97 |
_WRAP_METHOD(Glib::ustring compute_prefix(const Glib::ustring& key), gtk_entry_completion_compute_prefix) |
| 98 |
_WRAP_METHOD(void complete(), gtk_entry_completion_complete) |
| 99 |
|
| 100 |
_WRAP_METHOD(void insert_prefix(), gtk_entry_completion_insert_prefix) |
| 101 |
|
| 102 |
//We reordered the parameters, compared to the C version, so that we can have method overloads without the index. |
| 103 |
|
| 104 |
// TODO: We would really like an insert() which before-inserts an iterator, like ListStore::insert(), |
| 105 |
// but there is no EntryCompletion::insert_before() for us to use. |
| 106 |
void insert_action_text(const Glib::ustring& text, int index); |
| 107 |
void prepend_action_text(const Glib::ustring& text); |
| 108 |
//TODO: Add append_action_text() somehow? It would be slow if we count the children each time. murrayc. |
| 109 |
_IGNORE(gtk_entry_completion_insert_action_text) |
| 110 |
void insert_action_markup(const Glib::ustring& markup, int index); |
| 111 |
void prepend_action_markup(const Glib::ustring& markup); |
| 112 |
_IGNORE(gtk_entry_completion_insert_action_markup) |
| 113 |
|
| 114 |
//TODO: Change default - it would be nicer to delete the last action instead of the first. |
| 115 |
_WRAP_METHOD(void delete_action(int index = 0), gtk_entry_completion_delete_action) |
| 116 |
|
| 117 |
_WRAP_METHOD(void set_inline_completion(bool inline_completion = true), gtk_entry_completion_set_inline_completion) |
| 118 |
_WRAP_METHOD(bool get_inline_completion() const, gtk_entry_completion_get_inline_completion) |
| 119 |
_WRAP_METHOD(void set_inline_selection(bool inline_selection = true), gtk_entry_completion_set_inline_selection) |
| 120 |
_WRAP_METHOD(bool get_inline_selection() const, gtk_entry_completion_get_inline_selection) |
| 121 |
_WRAP_METHOD(void set_popup_completion(bool popup_completion = true), gtk_entry_completion_set_popup_completion) |
| 122 |
_WRAP_METHOD(bool get_popup_completion() const, gtk_entry_completion_get_popup_completion) |
| 123 |
|
| 124 |
_WRAP_METHOD(void set_popup_set_width(bool popup_set_width = true), gtk_entry_completion_set_popup_set_width) |
| 125 |
_WRAP_METHOD(bool get_popup_set_width() const, gtk_entry_completion_get_popup_set_width) |
| 126 |
|
| 127 |
_WRAP_METHOD(void set_popup_single_match(bool popup_single_match = true), gtk_entry_completion_set_popup_single_match) |
| 128 |
|
| 129 |
_WRAP_METHOD(bool get_popup_single_match() const, gtk_entry_completion_get_popup_single_match) |
| 130 |
_WRAP_METHOD(Glib::ustring get_completion_prefix() const, gtk_entry_completion_get_completion_prefix) |
| 131 |
|
| 132 |
_WRAP_METHOD(void set_text_column(const TreeModelColumnBase& column), gtk_entry_completion_set_text_column) |
| 133 |
_WRAP_METHOD(void set_text_column(int column), gtk_entry_completion_set_text_column) |
| 134 |
_WRAP_METHOD(int get_text_column() const, gtk_entry_completion_get_text_column) |
| 135 |
|
| 136 |
_WRAP_SIGNAL(void action_activated(int index), action_activated) |
| 137 |
|
| 138 |
//We completely hand-code these signals because we want to change how the parameters are wrapped, |
| 139 |
//because we need both the iter and the model to make the C++ iter. |
| 140 |
_IGNORE_SIGNAL(match_selected) |
| 141 |
_IGNORE_SIGNAL(cursor_on_match) |
| 142 |
|
| 143 |
/** Emitted when a match from the list is selected. |
| 144 |
* The default behaviour is to replace the contents of the |
| 145 |
* entry with the contents of the text column in the row |
| 146 |
* pointed to by @a iter. |
| 147 |
* |
| 148 |
* It is necessary to connect your signal handler <i>before</i> |
| 149 |
* the default one, which would otherwise return <tt>true</tt>, |
| 150 |
* a value which signifies that the signal has been handled, |
| 151 |
* thus preventing any other handler from being invoked. |
| 152 |
* |
| 153 |
* To do this, pass <tt>false</tt> to this signal proxy's |
| 154 |
* <tt>connect()</tt> method. For example: |
| 155 |
* <tt> completion->signal_match_selected().connect(sigc::mem_fun(*this, &YourClass::on_completion_match_selected), false); </tt> |
| 156 |
* |
| 157 |
* @param model The TreeModel containing the matches |
| 158 |
* @param iter A TreeModel::iterator positioned at the selected match |
| 159 |
* @result true if the signal has been handled |
| 160 |
* |
| 161 |
* @par Prototype: |
| 162 |
* <tt>bool %on_match_selected(const TreeModel::iterator& iter)</tt> |
| 163 |
*/ |
| 164 |
Glib::SignalProxy1< bool, const TreeModel::iterator& > signal_match_selected(); |
| 165 |
|
| 166 |
/** Emitted when a match from the cursor is on a match |
| 167 |
* of the list. The default behaviour is to replace the contents |
| 168 |
* of the entry with the contents of the text column in the row |
| 169 |
* pointed to by @a iter. |
| 170 |
* |
| 171 |
* @param model The TreeModel containing the matches |
| 172 |
* @param iter A TreeModel::iterator positioned at the selected match |
| 173 |
* @result true if the signal has been handled |
| 174 |
* |
| 175 |
* @par Prototype: |
| 176 |
* <tt>bool %on_cursor_on_match(const TreeModel::iterator& iter)</tt> |
| 177 |
* |
| 178 |
* @newin{2,12} |
| 179 |
*/ |
| 180 |
Glib::SignalProxy1< bool, const TreeModel::iterator& > signal_cursor_on_match(); |
| 181 |
|
| 182 |
|
| 183 |
#m4begin |
| 184 |
dnl// Hook in custom callback. |
| 185 |
dnl// It will use the callback. |
| 186 |
dnl |
| 187 |
_PUSH(SECTION_PCC_CLASS_INIT_DEFAULT_SIGNAL_HANDLERS) |
| 188 |
klass->match_selected = &match_selected_callback_custom; |
| 189 |
_SECTION(SECTION_PH_DEFAULT_SIGNAL_HANDLERS) |
| 190 |
static gboolean match_selected_callback_custom(GtkEntryCompletion* self, GtkTreeModel* c_model, GtkTreeIter* c_iter); |
| 191 |
_POP() |
| 192 |
#m4end |
| 193 |
|
| 194 |
#m4 _CONVERSION(`const char*',`const Glib::ustring&',__GCHARP_TO_USTRING) |
| 195 |
|
| 196 |
|
| 197 |
_WRAP_SIGNAL(bool insert_prefix(const Glib::ustring& prefix), insert_prefix) |
| 198 |
|
| 199 |
//TODO: Remove no_default_handler when we can break ABI. |
| 200 |
_WRAP_SIGNAL(void no_matches(), no_matches, no_default_handler) |
| 201 |
|
| 202 |
|
| 203 |
_WRAP_PROPERTY("model", Glib::RefPtr<Gtk::TreeModel>) |
| 204 |
_WRAP_PROPERTY("minimum_key_length", int) |
| 205 |
_WRAP_PROPERTY("text_column", int) |
| 206 |
_WRAP_PROPERTY("inline_completion", bool) |
| 207 |
_WRAP_PROPERTY("popup_completion", bool) |
| 208 |
_WRAP_PROPERTY("popup_set_width", bool) |
| 209 |
_WRAP_PROPERTY("popup_single_match", bool) |
| 210 |
_WRAP_PROPERTY("inline-selection", bool) |
| 211 |
_WRAP_PROPERTY("cell-area", Glib::RefPtr<CellArea>) |
| 212 |
|
| 213 |
protected: |
| 214 |
|
| 215 |
//Default Signal Handler: |
| 216 |
virtual bool on_match_selected(const TreeModel::iterator& iter); |
| 217 |
//No default handler for on_cursor_on_match(), to preserve ABI. TODO: Add this when we can break ABI. |
| 218 |
}; |
| 219 |
|
| 220 |
} // namespace Gtk |