gtksearchentry.c 24.9 KB
Newer Older
Bastien Nocera's avatar
Bastien Nocera committed
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
/* GTK - The GIMP Toolkit
 * Copyright (C) 2012 Red Hat, Inc.
 *
 * Authors:
 * - Bastien Nocera <bnocera@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Modified by the GTK+ Team and others 2012.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
 */

#include "config.h"

30
#include "gtksearchentryprivate.h"
Benjamin Otte's avatar
Benjamin Otte committed
31

32
#include "gtkaccessibleprivate.h"
Matthias Clasen's avatar
Matthias Clasen committed
33
#include "gtkeditable.h"
Timm Bäder's avatar
Timm Bäder committed
34
#include "gtkboxlayout.h"
35
#include "gtkgestureclick.h"
Matthias Clasen's avatar
Matthias Clasen committed
36
37
#include "gtktextprivate.h"
#include "gtkimage.h"
Benjamin Otte's avatar
Benjamin Otte committed
38
#include "gtkintl.h"
Matthias Clasen's avatar
Matthias Clasen committed
39
#include "gtkprivate.h"
Benjamin Otte's avatar
Benjamin Otte committed
40
41
#include "gtkmarshalers.h"
#include "gtkstylecontext.h"
42
#include "gtkeventcontrollerkey.h"
Matthias Clasen's avatar
Matthias Clasen committed
43
#include "gtkwidgetprivate.h"
44

Bastien Nocera's avatar
Bastien Nocera committed
45
46

/**
Matthias Clasen's avatar
Matthias Clasen committed
47
 * GtkSearchEntry:
Bastien Nocera's avatar
Bastien Nocera committed
48
 *
Matthias Clasen's avatar
Matthias Clasen committed
49
50
51
52
53
54
55
 * `GtkSearchEntry` is an entry widget that has been tailored for use
 * as a search entry.
 *
 * The main API for interacting with a `GtkSearchEntry` as entry
 * is the `GtkEditable` interface.
 *
 * ![An example GtkSearchEntry](search-entry.png)
Bastien Nocera's avatar
Bastien Nocera committed
56
 *
Matthias Clasen's avatar
Matthias Clasen committed
57
58
59
 * It will show an inactive symbolic “find” icon when the search
 * entry is empty, and a symbolic “clear” icon when there is text.
 * Clicking on the “clear” icon will empty the search entry.
Bastien Nocera's avatar
Bastien Nocera committed
60
 *
61
62
 * To make filtering appear more reactive, it is a good idea to
 * not react to every change in the entry text immediately, but
Matthias Clasen's avatar
Matthias Clasen committed
63
64
65
 * only after a short delay. To support this, `GtkSearchEntry`
 * emits the [signal@Gtk.SearchEntry::search-changed] signal which
 * can be used instead of the [signal@Gtk.Editable::changed] signal.
66
 *
Matthias Clasen's avatar
Matthias Clasen committed
67
68
69
70
 * The [signal@Gtk.SearchEntry::previous-match],
 * [signal@Gtk.SearchEntry::next-match] and
 * [signal@Gtk.SearchEntry::stop-search] signals can be used to
 * implement moving between search results and ending the search.
Matthias Clasen's avatar
Matthias Clasen committed
71
 *
Matthias Clasen's avatar
Matthias Clasen committed
72
73
74
75
 * Often, `GtkSearchEntry` will be fed events by means of being
 * placed inside a [class@Gtk.SearchBar]. If that is not the case,
 * you can use [method@Gtk.SearchEntry.set_key_capture_widget] to
 * let it capture key input from another widget.
76
 *
77
78
79
 * `GtkSearchEntry` provides only minimal API and should be used with
 * the [iface@Gtk.Editable] API.
 *
Matthias Clasen's avatar
Matthias Clasen committed
80
 * ## CSS Nodes
81
 *
82
 * ```
83
84
 * entry.search
 * ╰── text
85
 * ```
86
 *
Matthias Clasen's avatar
Matthias Clasen committed
87
88
 * `GtkSearchEntry` has a single CSS node with name entry that carries
 * a `.search` style class, and the text node is a child of that.
89
 *
Matthias Clasen's avatar
Matthias Clasen committed
90
 * ## Accessibility
91
 *
Matthias Clasen's avatar
Matthias Clasen committed
92
 * `GtkSearchEntry` uses the %GTK_ACCESSIBLE_ROLE_SEARCH_BOX role.
Bastien Nocera's avatar
Bastien Nocera committed
93
94
 */

95
enum {
Matthias Clasen's avatar
Matthias Clasen committed
96
  ACTIVATE,
97
  SEARCH_CHANGED,
Matthias Clasen's avatar
Matthias Clasen committed
98
99
100
  NEXT_MATCH,
  PREVIOUS_MATCH,
  STOP_SEARCH,
101
  SEARCH_STARTED,
102
103
104
  LAST_SIGNAL
};

Matthias Clasen's avatar
Matthias Clasen committed
105
106
107
108
109
110
111
enum {
  PROP_0,
  PROP_PLACEHOLDER_TEXT,
  PROP_ACTIVATES_DEFAULT,
  NUM_PROPERTIES,
};

112
113
static guint signals[LAST_SIGNAL] = { 0 };

Matthias Clasen's avatar
Matthias Clasen committed
114
115
static GParamSpec *props[NUM_PROPERTIES] = { NULL, };

Matthias Clasen's avatar
Matthias Clasen committed
116
117
118
119
120
typedef struct _GtkSearchEntryClass  GtkSearchEntryClass;

struct _GtkSearchEntry
{
  GtkWidget parent;
121
122
123
124
125
126
127
128
129
130

  GtkWidget *capture_widget;
  GtkEventController *capture_widget_controller;

  GtkWidget *entry;
  GtkWidget *icon;

  guint delayed_changed_id;
  gboolean content_changed;
  gboolean search_stopped;
Matthias Clasen's avatar
Matthias Clasen committed
131
132
133
134
135
136
137
138
139
140
141
142
143
};

struct _GtkSearchEntryClass
{
  GtkWidgetClass parent_class;

  void (* activate)       (GtkSearchEntry *entry);
  void (* search_changed) (GtkSearchEntry *entry);
  void (* next_match)     (GtkSearchEntry *entry);
  void (* previous_match) (GtkSearchEntry *entry);
  void (* stop_search)    (GtkSearchEntry *entry);
};

144
static void gtk_search_entry_editable_init (GtkEditableInterface *iface);
145
static void gtk_search_entry_accessible_init (GtkAccessibleInterface *iface);
146

Matthias Clasen's avatar
Matthias Clasen committed
147
G_DEFINE_TYPE_WITH_CODE (GtkSearchEntry, gtk_search_entry, GTK_TYPE_WIDGET,
148
149
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_ACCESSIBLE,
                                                gtk_search_entry_accessible_init)
150
151
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
                                                gtk_search_entry_editable_init))
152

153
154
155
/* 150 mseconds of delay */
#define DELAYED_TIMEOUT_ID 150

Matthias Clasen's avatar
Matthias Clasen committed
156
static void
Matthias Clasen's avatar
Matthias Clasen committed
157
text_changed (GtkSearchEntry *entry)
Matthias Clasen's avatar
Matthias Clasen committed
158
{
159
  entry->content_changed = TRUE;
Matthias Clasen's avatar
Matthias Clasen committed
160
161
162
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
163
gtk_search_entry_finalize (GObject *object)
Matthias Clasen's avatar
Matthias Clasen committed
164
{
Matthias Clasen's avatar
Matthias Clasen committed
165
  GtkSearchEntry *entry = GTK_SEARCH_ENTRY (object);
Matthias Clasen's avatar
Matthias Clasen committed
166

Matthias Clasen's avatar
Matthias Clasen committed
167
  gtk_editable_finish_delegate (GTK_EDITABLE (entry));
Matthias Clasen's avatar
Matthias Clasen committed
168

169
170
  gtk_widget_unparent (gtk_widget_get_first_child (GTK_WIDGET (entry)));

171
172
  g_clear_pointer (&entry->entry, gtk_widget_unparent);
  g_clear_pointer (&entry->icon, gtk_widget_unparent);
173

174
175
  if (entry->delayed_changed_id > 0)
    g_source_remove (entry->delayed_changed_id);
176

177
178
  gtk_search_entry_set_key_capture_widget (GTK_SEARCH_ENTRY (object), NULL);

179
180
181
  G_OBJECT_CLASS (gtk_search_entry_parent_class)->finalize (object);
}

Matthias Clasen's avatar
Matthias Clasen committed
182
183
184
static void
gtk_search_entry_stop_search (GtkSearchEntry *entry)
{
185
  entry->search_stopped = TRUE;
Matthias Clasen's avatar
Matthias Clasen committed
186
187
}

Matthias Clasen's avatar
Matthias Clasen committed
188
189
190
191
192
193
194
static void
gtk_search_entry_set_property (GObject      *object,
                               guint         prop_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
  GtkSearchEntry *entry = GTK_SEARCH_ENTRY (object);
195
  const char *text;
Matthias Clasen's avatar
Matthias Clasen committed
196
197

  if (gtk_editable_delegate_set_property (object, prop_id, value, pspec))
198
199
200
201
202
203
204
205
206
207
    {
      if (prop_id == NUM_PROPERTIES + GTK_EDITABLE_PROP_EDITABLE)
        {
          gtk_accessible_update_property (GTK_ACCESSIBLE (entry),
                                          GTK_ACCESSIBLE_PROPERTY_READ_ONLY, !g_value_get_boolean (value),
                                          -1);
        }

      return;
    }
Matthias Clasen's avatar
Matthias Clasen committed
208
209
210
211

  switch (prop_id)
    {
    case PROP_PLACEHOLDER_TEXT:
212
213
214
215
216
      text = g_value_get_string (value);
      gtk_text_set_placeholder_text (GTK_TEXT (entry->entry), text);
      gtk_accessible_update_property (GTK_ACCESSIBLE (entry),
                                      GTK_ACCESSIBLE_PROPERTY_PLACEHOLDER, text,
                                      -1);
Matthias Clasen's avatar
Matthias Clasen committed
217
218
219
      break;

    case PROP_ACTIVATES_DEFAULT:
220
      if (gtk_text_get_activates_default (GTK_TEXT (entry->entry)) != g_value_get_boolean (value))
221
        {
222
          gtk_text_set_activates_default (GTK_TEXT (entry->entry), g_value_get_boolean (value));
223
224
          g_object_notify_by_pspec (object, pspec);
        }
Matthias Clasen's avatar
Matthias Clasen committed
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
gtk_search_entry_get_property (GObject    *object,
                               guint       prop_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
  GtkSearchEntry *entry = GTK_SEARCH_ENTRY (object);

  if (gtk_editable_delegate_get_property (object, prop_id, value, pspec))
    return;

  switch (prop_id)
    {
    case PROP_PLACEHOLDER_TEXT:
246
      g_value_set_string (value, gtk_text_get_placeholder_text (GTK_TEXT (entry->entry)));
Matthias Clasen's avatar
Matthias Clasen committed
247
248
249
      break;

    case PROP_ACTIVATES_DEFAULT:
250
      g_value_set_boolean (value, gtk_text_get_activates_default (GTK_TEXT (entry->entry)));
Matthias Clasen's avatar
Matthias Clasen committed
251
252
253
254
255
256
257
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

258
static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
259
260
261
262
gtk_search_entry_grab_focus (GtkWidget *widget)
{
  GtkSearchEntry *entry = GTK_SEARCH_ENTRY (widget);

263
  return gtk_text_grab_focus_without_selecting (GTK_TEXT (entry->entry));
Matthias Clasen's avatar
Matthias Clasen committed
264
265
}

266
static gboolean
267
268
269
270
271
gtk_search_entry_mnemonic_activate (GtkWidget *widget,
                                    gboolean   group_cycling)
{
  GtkSearchEntry *entry = GTK_SEARCH_ENTRY (widget);

272
  gtk_widget_grab_focus (entry->entry);
273
274
275
276

  return TRUE;
}

Bastien Nocera's avatar
Bastien Nocera committed
277
278
279
static void
gtk_search_entry_class_init (GtkSearchEntryClass *klass)
{
280
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
Matthias Clasen's avatar
Matthias Clasen committed
281
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
282
283

  object_class->finalize = gtk_search_entry_finalize;
Matthias Clasen's avatar
Matthias Clasen committed
284
285
286
287
  object_class->get_property = gtk_search_entry_get_property;
  object_class->set_property = gtk_search_entry_set_property;

  widget_class->grab_focus = gtk_search_entry_grab_focus;
Matthias Clasen's avatar
Matthias Clasen committed
288
  widget_class->focus = gtk_widget_focus_child;
289
  widget_class->mnemonic_activate = gtk_search_entry_mnemonic_activate;
Matthias Clasen's avatar
Matthias Clasen committed
290
291

  klass->stop_search = gtk_search_entry_stop_search;
Bastien Nocera's avatar
Bastien Nocera committed
292

Matthias Clasen's avatar
Matthias Clasen committed
293
294
295
296
297
298
  /**
   * GtkSearchEntry:placeholder-text:
   *
   * The text that will be displayed in the `GtkSearchEntry`
   * when it is empty and unfocused.
   */
Matthias Clasen's avatar
Matthias Clasen committed
299
300
301
302
303
  props[PROP_PLACEHOLDER_TEXT] =
      g_param_spec_string ("placeholder-text",
                           P_("Placeholder text"),
                           P_("Show text in the entry when it’s empty and unfocused"),
                           NULL,
304
                           GTK_PARAM_READWRITE);
Matthias Clasen's avatar
Matthias Clasen committed
305

Matthias Clasen's avatar
Matthias Clasen committed
306
307
308
309
310
  /**
   * GtkSearchEntry:activates-default:
   *
   * Whether to activate the default widget when Enter is pressed.
   */
Matthias Clasen's avatar
Matthias Clasen committed
311
312
313
314
315
316
317
318
319
320
  props[PROP_ACTIVATES_DEFAULT] =
      g_param_spec_boolean ("activates-default",
                            P_("Activates default"),
                            P_("Whether to activate the default widget (such as the default button in a dialog) when Enter is pressed"),
                            FALSE,
                            GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);

  g_object_class_install_properties (object_class, NUM_PROPERTIES, props);
  gtk_editable_install_properties (object_class, NUM_PROPERTIES);

321
  /**
322
   * GtkSearchEntry::activate:
323
324
   * @self: The widget on which the signal is emitted
   *
Matthias Clasen's avatar
Matthias Clasen committed
325
326
327
   * Emitted when the entry is activated.
   *
   * The keybindings for this signal are all forms of the Enter key.
328
   */
Matthias Clasen's avatar
Matthias Clasen committed
329
330
331
332
333
334
335
336
  signals[ACTIVATE] =
    g_signal_new (I_("activate"),
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkSearchEntryClass, activate),
                  NULL, NULL,
                  NULL,
                  G_TYPE_NONE, 0);
337
338
339
340
341

  /**
   * GtkSearchEntry::search-changed:
   * @entry: the entry on which the signal was emitted
   *
Matthias Clasen's avatar
Matthias Clasen committed
342
343
   * Emitted with a short delay of 150 milliseconds after the
   * last change to the entry text.
344
345
346
347
348
349
350
   */
  signals[SEARCH_CHANGED] =
    g_signal_new (I_("search-changed"),
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkSearchEntryClass, search_changed),
                  NULL, NULL,
351
                  NULL,
352
                  G_TYPE_NONE, 0);
Matthias Clasen's avatar
Matthias Clasen committed
353
354
355
356
357

  /**
   * GtkSearchEntry::next-match:
   * @entry: the entry on which the signal was emitted
   *
Matthias Clasen's avatar
Matthias Clasen committed
358
   * Emitted when the user initiates a move to the next match
Matthias Clasen's avatar
Matthias Clasen committed
359
360
   * for the current search string.
   *
Matthias Clasen's avatar
Matthias Clasen committed
361
362
363
364
   * This is a [keybinding signal](class.SignalAction.html).
   *
   * Applications should connect to it, to implement moving
   * between matches.
Matthias Clasen's avatar
Matthias Clasen committed
365
366
367
368
369
370
371
372
373
   *
   * The default bindings for this signal is Ctrl-g.
   */
  signals[NEXT_MATCH] =
    g_signal_new (I_("next-match"),
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkSearchEntryClass, next_match),
                  NULL, NULL,
374
                  NULL,
Matthias Clasen's avatar
Matthias Clasen committed
375
376
377
378
379
380
                  G_TYPE_NONE, 0);

  /**
   * GtkSearchEntry::previous-match:
   * @entry: the entry on which the signal was emitted
   *
Matthias Clasen's avatar
Matthias Clasen committed
381
   * Emitted when the user initiates a move to the previous match
Matthias Clasen's avatar
Matthias Clasen committed
382
383
   * for the current search string.
   *
Matthias Clasen's avatar
Matthias Clasen committed
384
385
386
387
   * This is a [keybinding signal](class.SignalAction.html).
   *
   * Applications should connect to it, to implement moving
   * between matches.
Matthias Clasen's avatar
Matthias Clasen committed
388
389
390
391
392
393
394
395
396
   *
   * The default bindings for this signal is Ctrl-Shift-g.
   */
  signals[PREVIOUS_MATCH] =
    g_signal_new (I_("previous-match"),
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkSearchEntryClass, previous_match),
                  NULL, NULL,
397
                  NULL,
Matthias Clasen's avatar
Matthias Clasen committed
398
399
400
401
402
403
                  G_TYPE_NONE, 0);

  /**
   * GtkSearchEntry::stop-search:
   * @entry: the entry on which the signal was emitted
   *
Matthias Clasen's avatar
Matthias Clasen committed
404
405
406
   * Emitted when the user stops a search via keyboard input.
   *
   * This is a [keybinding signal](class.SignalAction.html).
Matthias Clasen's avatar
Matthias Clasen committed
407
   *
Matthias Clasen's avatar
Matthias Clasen committed
408
409
   * Applications should connect to it, to implement hiding
   * the search entry in this case.
Matthias Clasen's avatar
Matthias Clasen committed
410
411
412
413
414
415
416
417
418
   *
   * The default bindings for this signal is Escape.
   */
  signals[STOP_SEARCH] =
    g_signal_new (I_("stop-search"),
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkSearchEntryClass, stop_search),
                  NULL, NULL,
419
                  NULL,
Matthias Clasen's avatar
Matthias Clasen committed
420
421
                  G_TYPE_NONE, 0);

422
423
424
425
  /**
   * GtkSearchEntry::search-started:
   * @entry: the entry on which the signal was emitted
   *
Matthias Clasen's avatar
Matthias Clasen committed
426
   * Emitted when the user initiated a search on the entry.
427
428
429
430
431
432
433
434
435
   */
  signals[SEARCH_STARTED] =
    g_signal_new (I_("search-started"),
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST, 0,
                  NULL, NULL,
                  NULL,
                  G_TYPE_NONE, 0);

436
437
438
439
440
441
442
443
444
445
446
447
  gtk_widget_class_add_binding_signal (widget_class,
                                       GDK_KEY_g, GDK_CONTROL_MASK,
                                       "next-match",
                                       NULL);
  gtk_widget_class_add_binding_signal (widget_class,
                                       GDK_KEY_g, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
                                       "previous-match",
                                       NULL);
  gtk_widget_class_add_binding_signal (widget_class,
                                       GDK_KEY_Escape, 0,
                                       "stop-search",
                                       NULL);
Matthias Clasen's avatar
Matthias Clasen committed
448

Timm Bäder's avatar
Timm Bäder committed
449
  gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BOX_LAYOUT);
Matthias Clasen's avatar
Matthias Clasen committed
450
  gtk_widget_class_set_css_name (widget_class, I_("entry"));
451
  gtk_widget_class_set_accessible_role (widget_class, GTK_ACCESSIBLE_ROLE_SEARCH_BOX);
Matthias Clasen's avatar
Matthias Clasen committed
452
453
454
455
456
}

static GtkEditable *
gtk_search_entry_get_delegate (GtkEditable *editable)
{
457
  return GTK_EDITABLE (GTK_SEARCH_ENTRY (editable)->entry);
458
459
460
461
462
}

static void
gtk_search_entry_editable_init (GtkEditableInterface *iface)
{
Matthias Clasen's avatar
Matthias Clasen committed
463
  iface->get_delegate = gtk_search_entry_get_delegate;
464
465
}

466
467
468
469
470
471
472
473
474
475
476
477
static gboolean
gtk_search_entry_accessible_get_platform_state (GtkAccessible              *self,
                                                GtkAccessiblePlatformState  state)
{
  GtkSearchEntry *entry = GTK_SEARCH_ENTRY (self);

  switch (state)
    {
    case GTK_ACCESSIBLE_PLATFORM_STATE_FOCUSABLE:
      return gtk_widget_get_focusable (GTK_WIDGET (entry->entry));
    case GTK_ACCESSIBLE_PLATFORM_STATE_FOCUSED:
      return gtk_widget_has_focus (GTK_WIDGET (entry->entry));
478
479
    case GTK_ACCESSIBLE_PLATFORM_STATE_ACTIVE:
      return FALSE;
480
481
482
483
484
485
486
487
488
489
490
491
492
    default:
      g_assert_not_reached ();
    }
}

static void
gtk_search_entry_accessible_init (GtkAccessibleInterface *iface)
{
  GtkAccessibleInterface *parent_iface = g_type_interface_peek_parent (iface);
  iface->get_at_context = parent_iface->get_at_context;
  iface->get_platform_state = gtk_search_entry_accessible_get_platform_state;
}

493
494
495
496
497
498
499
500
501
502
static void
gtk_search_entry_icon_press (GtkGestureClick *press,
                             int              n_press,
                             double           x,
                             double           y,
                             GtkSearchEntry  *entry)
{
  gtk_gesture_set_state (GTK_GESTURE (press), GTK_EVENT_SEQUENCE_CLAIMED);
}

503
static void
504
505
506
507
508
gtk_search_entry_icon_release (GtkGestureClick *press,
                               int              n_press,
                               double           x,
                               double           y,
                               GtkSearchEntry  *entry)
509
{
510
  gtk_editable_set_text (GTK_EDITABLE (entry->entry), "");
Bastien Nocera's avatar
Bastien Nocera committed
511
512
}

513
514
515
516
517
static gboolean
gtk_search_entry_changed_timeout_cb (gpointer user_data)
{
  GtkSearchEntry *entry = user_data;

518
  g_signal_emit (entry, signals[SEARCH_CHANGED], 0);
519
  entry->delayed_changed_id = 0;
520
521
522
523
524
525
526

  return G_SOURCE_REMOVE;
}

static void
reset_timeout (GtkSearchEntry *entry)
{
527
528
529
  if (entry->delayed_changed_id > 0)
    g_source_remove (entry->delayed_changed_id);
  entry->delayed_changed_id = g_timeout_add (DELAYED_TIMEOUT_ID,
530
531
                                            gtk_search_entry_changed_timeout_cb,
                                            entry);
532
  gdk_source_set_static_name_by_id (entry->delayed_changed_id, "[gtk] gtk_search_entry_changed_timeout_cb");
533
534
}

Bastien Nocera's avatar
Bastien Nocera committed
535
static void
536
gtk_search_entry_changed (GtkEditable    *editable,
Matthias Clasen's avatar
Matthias Clasen committed
537
                          GtkSearchEntry *entry)
Bastien Nocera's avatar
Bastien Nocera committed
538
{
Matthias Clasen's avatar
Matthias Clasen committed
539
  const char *str;
Bastien Nocera's avatar
Bastien Nocera committed
540

541
  /* Update the icons first */
542
  str = gtk_editable_get_text (GTK_EDITABLE (entry->entry));
Bastien Nocera's avatar
Bastien Nocera committed
543
544
545

  if (str == NULL || *str == '\0')
    {
546
      gtk_widget_set_child_visible (entry->icon, FALSE);
547

548
      if (entry->delayed_changed_id > 0)
549
        {
550
551
          g_source_remove (entry->delayed_changed_id);
          entry->delayed_changed_id = 0;
552
553
554
555
556
        }
      g_signal_emit (entry, signals[SEARCH_CHANGED], 0);
    }
  else
    {
557
      gtk_widget_set_child_visible (entry->icon, TRUE);
Matthias Clasen's avatar
Matthias Clasen committed
558

559
560
561
      /* Queue up the timeout */
      reset_timeout (entry);
    }
Bastien Nocera's avatar
Bastien Nocera committed
562
563
}

Matthias Clasen's avatar
Matthias Clasen committed
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
static void
notify_cb (GObject    *object,
           GParamSpec *pspec,
           gpointer    data)
{
  /* The editable interface properties are already forwarded by the editable delegate setup */
  if (g_str_equal (pspec->name, "placeholder-text") ||
      g_str_equal (pspec->name, "activates-default"))
    g_object_notify (data, pspec->name);
}

static void
activate_cb (GtkText  *text,
             gpointer  data)
{
  g_signal_emit (data, signals[ACTIVATE], 0);
}

582
583
584
585
586
587
588
589
590
591
static void
catchall_click_press (GtkGestureClick *gesture,
                      int              n_press,
                      double           x,
                      double           y,
                      gpointer         user_data)
{
  gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_CLAIMED);
}

Bastien Nocera's avatar
Bastien Nocera committed
592
593
594
static void
gtk_search_entry_init (GtkSearchEntry *entry)
{
595
  GtkWidget *icon;
596
  GtkGesture *press, *catchall;
Matthias Clasen's avatar
Matthias Clasen committed
597

598
  /* The search icon is purely presentational */
599
  icon = g_object_new (GTK_TYPE_IMAGE,
600
                       "accessible-role", GTK_ACCESSIBLE_ROLE_PRESENTATION,
601
602
603
604
                       "icon-name", "system-search-symbolic",
                       NULL);
  gtk_widget_set_parent (icon, GTK_WIDGET (entry));

605
606
607
  entry->entry = gtk_text_new ();
  gtk_widget_set_parent (entry->entry, GTK_WIDGET (entry));
  gtk_widget_set_hexpand (entry->entry, TRUE);
Matthias Clasen's avatar
Matthias Clasen committed
608
  gtk_editable_init_delegate (GTK_EDITABLE (entry));
609
610
611
612
613
  g_signal_connect_swapped (entry->entry, "changed", G_CALLBACK (text_changed), entry);
  g_signal_connect_after (entry->entry, "changed", G_CALLBACK (gtk_search_entry_changed), entry);
  g_signal_connect_swapped (entry->entry, "preedit-changed", G_CALLBACK (text_changed), entry);
  g_signal_connect (entry->entry, "notify", G_CALLBACK (notify_cb), entry);
  g_signal_connect (entry->entry, "activate", G_CALLBACK (activate_cb), entry);
Matthias Clasen's avatar
Matthias Clasen committed
614

615
616
617
618
  entry->icon = g_object_new (GTK_TYPE_IMAGE,
                              "accessible-role", GTK_ACCESSIBLE_ROLE_PRESENTATION,
                              "icon-name", "edit-clear-symbolic",
                              NULL);
619
620
621
  gtk_widget_set_tooltip_text (entry->icon, _("Clear entry"));
  gtk_widget_set_parent (entry->icon, GTK_WIDGET (entry));
  gtk_widget_set_child_visible (entry->icon, FALSE);
622

623
  press = gtk_gesture_click_new ();
624
  g_signal_connect (press, "pressed", G_CALLBACK (gtk_search_entry_icon_press), entry);
Matthias Clasen's avatar
Matthias Clasen committed
625
  g_signal_connect (press, "released", G_CALLBACK (gtk_search_entry_icon_release), entry);
626
  gtk_widget_add_controller (entry->icon, GTK_EVENT_CONTROLLER (press));
627

628
629
630
631
632
633
  catchall = gtk_gesture_click_new ();
  g_signal_connect (catchall, "pressed",
                    G_CALLBACK (catchall_click_press), entry);
  gtk_widget_add_controller (GTK_WIDGET (entry),
                             GTK_EVENT_CONTROLLER (catchall));

634
  gtk_widget_add_css_class (GTK_WIDGET (entry), I_("search"));
Bastien Nocera's avatar
Bastien Nocera committed
635
636
637
638
639
}

/**
 * gtk_search_entry_new:
 *
Matthias Clasen's avatar
Matthias Clasen committed
640
 * Creates a `GtkSearchEntry`.
Bastien Nocera's avatar
Bastien Nocera committed
641
 *
Matthias Clasen's avatar
Matthias Clasen committed
642
 * Returns: a new `GtkSearchEntry`
Bastien Nocera's avatar
Bastien Nocera committed
643
644
645
646
647
648
 */
GtkWidget *
gtk_search_entry_new (void)
{
  return GTK_WIDGET (g_object_new (GTK_TYPE_SEARCH_ENTRY, NULL));
}
Matthias Clasen's avatar
Matthias Clasen committed
649

650
gboolean
651
652
gtk_search_entry_is_keynav (guint           keyval,
                            GdkModifierType state)
Matthias Clasen's avatar
Matthias Clasen committed
653
654
655
656
657
658
659
660
661
662
{
  if (keyval == GDK_KEY_Tab       || keyval == GDK_KEY_KP_Tab ||
      keyval == GDK_KEY_Up        || keyval == GDK_KEY_KP_Up ||
      keyval == GDK_KEY_Down      || keyval == GDK_KEY_KP_Down ||
      keyval == GDK_KEY_Left      || keyval == GDK_KEY_KP_Left ||
      keyval == GDK_KEY_Right     || keyval == GDK_KEY_KP_Right ||
      keyval == GDK_KEY_Home      || keyval == GDK_KEY_KP_Home ||
      keyval == GDK_KEY_End       || keyval == GDK_KEY_KP_End ||
      keyval == GDK_KEY_Page_Up   || keyval == GDK_KEY_KP_Page_Up ||
      keyval == GDK_KEY_Page_Down || keyval == GDK_KEY_KP_Page_Down ||
663
      ((state & (GDK_CONTROL_MASK | GDK_ALT_MASK)) != 0))
Matthias Clasen's avatar
Matthias Clasen committed
664
665
666
667
668
669
670
671
        return TRUE;

  /* Other navigation events should get automatically
   * ignored as they will not change the content of the entry
   */
  return FALSE;
}

672
673
674
675
676
static gboolean
capture_widget_key_handled (GtkEventControllerKey *controller,
                            guint                  keyval,
                            guint                  keycode,
                            GdkModifierType        state,
677
                            GtkWidget             *widget)
678
{
679
  GtkSearchEntry *entry = GTK_SEARCH_ENTRY (widget);
680
  gboolean handled, was_empty;
681
682
683
684
685
686

  if (gtk_search_entry_is_keynav (keyval, state) ||
      keyval == GDK_KEY_space ||
      keyval == GDK_KEY_Menu)
    return FALSE;

687
688
689
  entry->content_changed = FALSE;
  entry->search_stopped = FALSE;
  was_empty = (gtk_text_get_text_length (GTK_TEXT (entry->entry)) == 0);
690

691
  handled = gtk_event_controller_key_forward (controller, entry->entry);
692

693
  if (handled)
694
    {
695
      if (was_empty && entry->content_changed && !entry->search_stopped)
696
697
698
699
700
701
        g_signal_emit (entry, signals[SEARCH_STARTED], 0);

      return GDK_EVENT_STOP;
    }

  return GDK_EVENT_PROPAGATE;
702
703
704
705
}

/**
 * gtk_search_entry_set_key_capture_widget:
Matthias Clasen's avatar
Matthias Clasen committed
706
 * @entry: a `GtkSearchEntry`
Matthias Clasen's avatar
Matthias Clasen committed
707
 * @widget: (nullable) (transfer none): a `GtkWidget`
708
 *
709
710
 * Sets @widget as the widget that @entry will capture key
 * events from.
711
712
713
714
 *
 * Key events are consumed by the search entry to start or
 * continue a search.
 *
Matthias Clasen's avatar
Matthias Clasen committed
715
716
717
718
 * If the entry is part of a `GtkSearchBar`, it is preferable
 * to call [method@Gtk.SearchBar.set_key_capture_widget] instead,
 * which will reveal the entry in addition to triggering the
 * search entry.
719
720
721
722
723
724
 *
 * Note that despite the name of this function, the events
 * are only 'captured' in the bubble phase, which means that
 * editable child widgets of @widget will receive text input
 * before it gets captured. If that is not desired, you can
 * capture and forward the events yourself with
Matthias Clasen's avatar
Matthias Clasen committed
725
726
 * [method@Gtk.EventControllerKey.forward].
 */
727
728
729
730
731
732
733
void
gtk_search_entry_set_key_capture_widget (GtkSearchEntry *entry,
                                         GtkWidget      *widget)
{
  g_return_if_fail (GTK_IS_SEARCH_ENTRY (entry));
  g_return_if_fail (!widget || GTK_IS_WIDGET (widget));

734
  if (entry->capture_widget)
735
    {
736
737
738
739
      gtk_widget_remove_controller (entry->capture_widget,
                                    entry->capture_widget_controller);
      g_object_remove_weak_pointer (G_OBJECT (entry->capture_widget),
                                    (gpointer *) &entry->capture_widget);
740
741
    }

742
  entry->capture_widget = widget;
743
744
745

  if (widget)
    {
746
747
      g_object_add_weak_pointer (G_OBJECT (entry->capture_widget),
                                 (gpointer *) &entry->capture_widget);
748

749
750
      entry->capture_widget_controller = gtk_event_controller_key_new ();
      gtk_event_controller_set_propagation_phase (entry->capture_widget_controller,
751
                                                  GTK_PHASE_BUBBLE);
752
      g_signal_connect (entry->capture_widget_controller, "key-pressed",
753
                        G_CALLBACK (capture_widget_key_handled), entry);
754
      g_signal_connect (entry->capture_widget_controller, "key-released",
755
                        G_CALLBACK (capture_widget_key_handled), entry);
756
      gtk_widget_add_controller (widget, entry->capture_widget_controller);
757
758
759
760
761
    }
}

/**
 * gtk_search_entry_get_key_capture_widget:
Matthias Clasen's avatar
Matthias Clasen committed
762
 * @entry: a `GtkSearchEntry`
763
764
765
 *
 * Gets the widget that @entry is capturing key events from.
 *
766
 * Returns: (transfer none): The key capture widget.
Matthias Clasen's avatar
Matthias Clasen committed
767
 */
768
769
770
771
772
GtkWidget *
gtk_search_entry_get_key_capture_widget (GtkSearchEntry *entry)
{
  g_return_val_if_fail (GTK_IS_SEARCH_ENTRY (entry), NULL);

773
  return entry->capture_widget;
774
775
}

Matthias Clasen's avatar
Matthias Clasen committed
776
777
778
GtkEventController *
gtk_search_entry_get_key_controller (GtkSearchEntry *entry)
{
779
  return gtk_text_get_key_controller (GTK_TEXT (entry->entry));
Matthias Clasen's avatar
Matthias Clasen committed
780
}
781
782
783
784

GtkText *
gtk_search_entry_get_text_widget (GtkSearchEntry *entry)
{
785
  return GTK_TEXT (entry->entry);
786
}