summaryrefslogtreecommitdiff
path: root/libs/gtkmm2/gtk/src/entrycompletion.hg
blob: a1ca2ae49ac3905207c88d649becd5da9b10aad5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* $Id: entrycompletion.hg,v 1.24 2006/07/19 16:58:50 murrayc Exp $ */

/* Copyright (C) 2003 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <gtkmm/widget.h>
#include <gtkmm/treemodel.h>
 
_DEFS(gtkmm,gtk)
_PINCLUDE(glibmm/private/object_p.h)


namespace Gtk
{

class Entry;

//TODO: This should derive+implement from CellLayout, when we can break ABI.
//Then we should add "It derives from the Gtk::CellLayout, to allow the user to add extra cells to the Gtk::TreeView with completion matches".

/** Completion functionality for Gtk::Entry.
 *
 * Gtk::EntryCompletion is an auxiliary object to be used in conjunction with 
 * Gtk::Entry to provide the completion functionality. 
 * 
 * "Completion functionality" means that when the user modifies the text in the 
 * entry, Gtk::EntryCompletion checks which rows in the model match the current 
 * content of the entry, and displays a list of matches. By default, the
 * matching is done by comparing the entry text case-insensitively against 
 * the text column of the model (see set_text_column()), 
 * but this can be overridden with a custom match function (see set_match_func()).
 *
 * When the user selects a completion, the content of the entry is updated. 
 * By default, the content of the entry is replaced by the text column of the 
 * model, but this can be overridden by connecting to the match_selected signal 
 * and updating the entry in the signal handler. Note that you should return true 
 * from the signal handler to suppress the default behaviour.
 * 
 * To add completion functionality to an entry, use Gtk::Entry::set_completion().
 * 
 * In addition to regular completion matches, which will be inserted into 
 * the entry when they are selected, Gtk::EntryCompletion also allows the display of  
 * "actions" in the popup window. Their appearance is similar to menu items, 
 * to differentiate them clearly from completion strings. When an action is 
 * selected, the action_activated signal is emitted. 
 */
class EntryCompletion : public Glib::Object
{
  _CLASS_GOBJECT(EntryCompletion, GtkEntryCompletion, GTK_ENTRY_COMPLETION, Glib::Object, GObject)

protected:
  _CTOR_DEFAULT()

public:
  _WRAP_CREATE()

  //Careful, this actually returns a GtkWidget*, so it might not always be a GtkEntry in future GTK+ versions.
  _WRAP_METHOD(Entry* get_entry(), gtk_entry_completion_get_entry)
  _WRAP_METHOD(const Entry* get_entry() const, gtk_entry_completion_get_entry)

  _WRAP_METHOD(void set_model(const Glib::RefPtr<TreeModel>& model), gtk_entry_completion_set_model)
  _WRAP_METHOD(Glib::RefPtr<TreeModel> get_model(), gtk_entry_completion_get_model, refreturn)
  _WRAP_METHOD(Glib::RefPtr<const TreeModel> get_model() const, gtk_entry_completion_get_model, refreturn, constversion)

  /// For example, bool on_match(const Glib::ustring& key, const TreeModel::const_iterator& iter);
  typedef sigc::slot<bool, const Glib::ustring&, const TreeModel::const_iterator&> SlotMatch;
  
  void set_match_func(const SlotMatch& slot);
  _IGNORE(gtk_entry_completion_set_match_func)
  
  _WRAP_METHOD(void set_minimum_key_length(int length), gtk_entry_completion_set_minimum_key_length)
  _WRAP_METHOD(int get_minimum_key_length() const, gtk_entry_completion_get_minimum_key_length)
  _WRAP_METHOD(void complete(), gtk_entry_completion_complete)

  _WRAP_METHOD(void insert_prefix(), gtk_entry_completion_insert_prefix)

  //We reordered the parameters, compared to the C version, so that we can have method overloads without the index.

  // TODO: We would really like an insert() which before-inserts an iterator, like ListStore::insert(),
  // but there is no EntryCompletion::insert_before() for us to use.
  void insert_action_text(const Glib::ustring& text, int index);
  void prepend_action_text(const Glib::ustring& text);
  //TODO: Add append_action_text() somehow? It would be slow if we count the children each time. murrayc.
  _IGNORE(gtk_entry_completion_insert_action_text)
  void insert_action_markup(const Glib::ustring& markup, int index);
  void prepend_action_markup(const Glib::ustring& markup);
  _IGNORE(gtk_entry_completion_insert_action_markup)

  //TODO: Change default - it would be nicer to delete the last action instead of the first.
  _WRAP_METHOD(void delete_action(int index = 0), gtk_entry_completion_delete_action)

  _WRAP_METHOD(void set_inline_completion(bool inline_completion = true), gtk_entry_completion_set_inline_completion)
  _WRAP_METHOD(bool get_inline_completion() const, gtk_entry_completion_get_inline_completion)
  _WRAP_METHOD(void set_inline_selection(bool inline_selection = true), gtk_entry_completion_set_inline_selection)
  _WRAP_METHOD(bool get_inline_selection() const, gtk_entry_completion_get_inline_selection)
  _WRAP_METHOD(void set_popup_completion(bool popup_completion = true), gtk_entry_completion_set_popup_completion)
  _WRAP_METHOD(bool get_popup_completion() const, gtk_entry_completion_get_popup_completion)

  _WRAP_METHOD(void set_popup_set_width(bool popup_set_width = true), gtk_entry_completion_set_popup_set_width)
  _WRAP_METHOD(bool get_popup_set_width() const, gtk_entry_completion_get_popup_set_width)

  //TODO This is wrongly named: Deprecate it and add set_popup_single_match().
  _WRAP_METHOD(void set_popup_single_width(bool popup_single_match = true), gtk_entry_completion_set_popup_single_match)

  _WRAP_METHOD(bool get_popup_single_match() const, gtk_entry_completion_get_popup_single_match)
  _WRAP_METHOD(Glib::ustring get_completion_prefix() const, gtk_entry_completion_get_completion_prefix)

  _WRAP_METHOD(void set_text_column(const TreeModelColumnBase& column), gtk_entry_completion_set_text_column)
  _WRAP_METHOD(void set_text_column(int column), gtk_entry_completion_set_text_column)
  _WRAP_METHOD(int get_text_column(), gtk_entry_completion_get_text_column)

  /** Emitted when an action is activated.
   *
   * @param index The index of the activated action.
   */
  _WRAP_SIGNAL(void action_activated(int index), action_activated)

  //We completely hand-code these signals because we want to change how the parameters are wrapped,
  //because we need both the iter and the model to make the C++ iter.
  _IGNORE_SIGNAL(match_selected)
  _IGNORE_SIGNAL(cursor_on_match)

  /** Emitted when a match from the list is selected. 
   * The default behaviour is to replace the contents of the 
   * entry with the contents of the text column in the row 
   * pointed to by @a iter.
   *
   * @param model The TreeModel containing the matches
   * @param iter A TreeModel::iterator positioned at the selected match
   * @result true if the signal has been handled
   * 
   * @par Prototype:
   * <tt>bool %on_match_selected(const TreeModel::iterator& iter)</tt>
   */
  Glib::SignalProxy1< bool, const TreeModel::iterator& > signal_match_selected();

  /** Emitted when a match from the cursor is on a match
   * of the list. The default behaviour is to replace the contents
   * of the entry with the contents of the text column in the row 
   * pointed to by @a iter.
   *
   * @param model The TreeModel containing the matches
   * @param iter A TreeModel::iterator positioned at the selected match
   * @result true if the signal has been handled
   * 
   * @par Prototype:
   * <tt>bool %on_cursor_on_match(const TreeModel::iterator& iter)</tt>
   *
   * @newin2p12
   */
  Glib::SignalProxy1< bool, const TreeModel::iterator& > signal_cursor_on_match();


  #m4begin
dnl// Hook in custom callback.
dnl// It will use the callback.
dnl
  _PUSH(SECTION_PCC_CLASS_INIT_DEFAULT_SIGNAL_HANDLERS)
    klass->match_selected = &match_selected_callback_custom;
  _SECTION(SECTION_PH_DEFAULT_SIGNAL_HANDLERS)
    static gboolean match_selected_callback_custom(GtkEntryCompletion* self, GtkTreeModel* c_model, GtkTreeIter* c_iter);
  _POP()
#m4end

  //We use no_default_handler for these signals, because we can not add a new vfunc without breaking ABI.
  //TODO: Remove no_default_handler when we do an ABI-break-with-parallel-install.

  /** Emitted when the inline autocompletion is triggered. 
   * The default behaviour is to make the entry display the 
   * whole prefix and select the newly inserted part.
   *
   * Applications may connect to this signal in order to insert only a
   * smaller part of the @a prefix into the entry - e.g. the entry used in
   * the FileChooser inserts only the part of the prefix up to the 
   * next '/'.
   * 
   * @newin2p6
   *
   * @param prefix The common prefix of all possible completions.
   * @result true if the signal has been handled
   */ 
  _WRAP_SIGNAL(bool insert_prefix(const Glib::ustring& prefix), insert_prefix, no_default_handler)

  _WRAP_PROPERTY("model", Glib::RefPtr<Gtk::TreeModel>)
  _WRAP_PROPERTY("minimum_key_length", int)
  _WRAP_PROPERTY("text_column, int)
  _WRAP_PROPERTY("inline_completion", bool)
  _WRAP_PROPERTY("popup_completion", bool)
  _WRAP_PROPERTY("popup_set_width", bool)
  _WRAP_PROPERTY("popup_single_match", bool)
  _WRAP_PROPERTY("inline-selection", bool)

protected:

#ifdef GLIBMM_DEFAULT_SIGNAL_HANDLERS_ENABLED
  //Default Signal Handler:
  virtual bool on_match_selected(const TreeModel::iterator& iter);  
  //No default handler for on_cursor_on_match(), to preserver ABI. TODO: Add this when we can break ABI.      
#endif //GLIBMM_DEFAULT_SIGNAL_HANDLERS_ENABLED
};

} // namespace Gtk