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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
|
/*
* Copyright (C) 2004 Tommi Komulainen
* Copyright (C) 2004, 2005 Christian Persch
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id$
*/
#include "config.h"
#include "ephy-find-toolbar.h"
#include "ephy-embed-find.h"
#include "ephy-embed-factory.h"
#include "ephy-debug.h"
#include <gdk/gdkkeysyms.h>
#include <glib/gi18n.h>
#include <gtk/gtkarrow.h>
#include <gtk/gtkhbox.h>
#include <gtk/gtkimage.h>
#include <gtk/gtkentry.h>
#include <gtk/gtklabel.h>
#include <gtk/gtkseparatortoolitem.h>
#include <gtk/gtkstock.h>
#include <gtk/gtktoolbutton.h>
#include <gtk/gtkalignment.h>
#include <gtk/gtkmain.h>
#include <string.h>
#define EPHY_FIND_TOOLBAR_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object),EPHY_TYPE_FIND_TOOLBAR, EphyFindToolbarPrivate))
struct _EphyFindToolbarPrivate
{
EphyEmbedFind *find;
EphyEmbed *embed;
GtkWidget *entry;
GtkToolItem *next;
GtkToolItem *prev;
gulong set_focus_handler;
guint preedit_changed : 1;
guint prevent_activate : 1;
guint explicit_focus : 1;
guint links_only : 1;
};
enum
{
PROP_0,
PROP_TEXT
};
enum
{
NEXT,
PREVIOUS,
CLOSE,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
static GObjectClass *parent_class = NULL;
/* private functions */
static EphyEmbedFind *
get_find (EphyFindToolbar *toolbar)
{
EphyFindToolbarPrivate *priv = toolbar->priv;
if (priv->find == NULL)
{
LOG ("Creating the finder now");
priv->find = EPHY_EMBED_FIND (ephy_embed_factory_new_object (EPHY_TYPE_EMBED_FIND));
g_return_val_if_fail (priv->embed == NULL || GTK_WIDGET_REALIZED (GTK_WIDGET (priv->embed)), priv->find);
ephy_embed_find_set_embed (priv->find, priv->embed);
}
return priv->find;
}
static void
set_controls (EphyFindToolbar *toolbar,
gboolean can_find_next,
gboolean can_find_prev)
{
EphyFindToolbarPrivate *priv = toolbar->priv;
gtk_widget_set_sensitive (GTK_WIDGET (priv->next), can_find_next);
gtk_widget_set_sensitive (GTK_WIDGET (priv->prev), can_find_prev);
}
static void
tab_content_changed_cb (EphyEmbed *embed,
const char *uri,
EphyFindToolbar *toolbar)
{
set_controls (toolbar, TRUE, TRUE);
}
#ifdef HAVE_TYPEAHEADFIND
/* Cut and paste from gtkwindow.c */
static void
send_focus_change (GtkWidget *widget,
gboolean in)
{
GdkEvent *event;
event = gdk_event_new (GDK_FOCUS_CHANGE);
g_object_ref (widget);
if (in)
{
GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
}
else
{
GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
}
event->focus_change.type = GDK_FOCUS_CHANGE;
event->focus_change.window = (GdkWindow *) g_object_ref (widget->window);
event->focus_change.in = in;
gtk_widget_event (widget, event);
g_object_notify (G_OBJECT (widget), "has_focus");
g_object_unref (widget);
gdk_event_free (event);
}
/* Code adapted from gtktreeview.c:gtk_tree_view_key_press() and
* gtk_tree_view_real_start_interactive_seach()
*/
static gboolean
tab_dom_key_press_cb (EphyEmbed *embed,
gpointer dom_event,
EphyFindToolbar *toolbar)
{
EphyFindToolbarPrivate *priv = toolbar->priv;
GtkWidget *widget = (GtkWidget *) toolbar;
GtkEntry *entry = (GtkEntry *) priv->entry;
GdkWindow *event_window;
GdkEvent *event;
GdkEventKey *event_key;
gboolean retval = FALSE;
guint oldhash, newhash;
event = gtk_get_current_event ();
if (event == NULL) return FALSE; /* shouldn't happen! */
g_return_val_if_fail (GDK_KEY_PRESS == event->type, FALSE);
event_key = (GdkEventKey *) event;
/* check for / and ' which open the find toolbar in text resp. link mode */
if (event_key->keyval == GDK_slash)
{
ephy_find_toolbar_open (toolbar, FALSE);
gdk_event_free (event);
return TRUE;
}
else if (event_key->keyval == GDK_apostrophe)
{
ephy_find_toolbar_open (toolbar, TRUE);
gdk_event_free (event);
return TRUE;
}
/* don't do anything if the find toolbar is hidden */
if (GTK_WIDGET_VISIBLE (widget) == FALSE ||
event_key->keyval == GDK_Return ||
event_key->keyval == GDK_KP_Enter)
{
gdk_event_free (event);
return FALSE;
}
oldhash = g_str_hash (gtk_entry_get_text (entry));
priv->preedit_changed = FALSE;
event_window = event_key->window;
event_key->window = priv->entry->window;
/* Send the event to the window. If the preedit_changed signal is emitted
* during this event, we will set priv->imcontext_changed */
priv->prevent_activate = TRUE;
retval = gtk_widget_event (priv->entry, event);
priv->prevent_activate = FALSE;
/* restore event window, else gdk_event_free below will crash */
event_key->window = event_window;
gdk_event_free (event);
newhash = g_str_hash (gtk_entry_get_text (entry));
/* FIXME: is this right? */
return (retval && oldhash != newhash) || priv->preedit_changed;
}
#endif /* HAVE_TYPEAHEADFIND */
static void
find_next_cb (EphyFindToolbar *toolbar)
{
g_signal_emit (toolbar, signals[NEXT], 0);
}
static void
find_prev_cb (EphyFindToolbar *toolbar)
{
g_signal_emit (toolbar, signals[PREVIOUS], 0);
}
static void
entry_changed_cb (GtkEntry *entry,
EphyFindToolbar *toolbar)
{
EphyFindToolbarPrivate *priv = toolbar->priv;
const char *text;
gboolean found = TRUE;
text = gtk_entry_get_text (GTK_ENTRY (priv->entry));
#ifdef HAVE_TYPEAHEADFIND
found = ephy_embed_find_find (get_find (toolbar), text, priv->links_only);
#else
ephy_embed_find_set_properties (get_find (toolbar), text, FALSE);
#endif
set_controls (toolbar, found, found);
}
static void
entry_preedit_changed_cb (GtkIMContext *context,
EphyFindToolbar *toolbar)
{
toolbar->priv->preedit_changed = TRUE;
}
static gboolean
entry_key_press_event_cb (GtkEntry *entry,
GdkEventKey *event,
EphyFindToolbar *toolbar)
{
guint mask = gtk_accelerator_get_default_mod_mask ();
gboolean handled = FALSE;
/* Hide the toolbar when ESC is pressed */
if ((event->state & mask) == 0 && event->keyval == GDK_Escape)
{
g_signal_emit (toolbar, signals[CLOSE], 0);
handled = TRUE;
}
return handled;
}
static void
entry_activate_cb (GtkWidget *entry,
EphyFindToolbar *toolbar)
{
EphyFindToolbarPrivate *priv = toolbar->priv;
if (priv->prevent_activate) return;
g_signal_emit (toolbar, signals[NEXT], 0);
}
static void
set_focus_cb (EphyWindow *window,
GtkWidget *widget,
EphyFindToolbar *toolbar)
{
EphyFindToolbarPrivate *priv = toolbar->priv;
GtkWidget *wtoolbar = GTK_WIDGET (toolbar);
while (widget != NULL && widget != wtoolbar)
{
widget = widget->parent;
}
/* if widget == toolbar, the new focus widget is in the toolbar */
if (widget == wtoolbar)
{
priv->explicit_focus = TRUE;
}
else if (priv->explicit_focus)
{
g_signal_emit (toolbar, signals[CLOSE], 0);
}
}
static void
ephy_find_toolbar_parent_set (GtkWidget *widget,
GtkWidget *previous_parent)
{
EphyFindToolbar *toolbar = EPHY_FIND_TOOLBAR (widget);
EphyFindToolbarPrivate *priv = toolbar->priv;
GtkWidget *toplevel;
if (widget->parent != NULL && priv->set_focus_handler == 0)
{
toplevel = gtk_widget_get_toplevel (widget);
priv->set_focus_handler =
g_signal_connect (toplevel, "set-focus",
G_CALLBACK (set_focus_cb), toolbar);
}
}
static void
ephy_find_toolbar_grab_focus (GtkWidget *widget)
{
EphyFindToolbar *toolbar = EPHY_FIND_TOOLBAR (widget);
EphyFindToolbarPrivate *priv = toolbar->priv;
gtk_widget_grab_focus (priv->entry);
g_return_if_fail (priv->explicit_focus);
}
static void
ephy_find_toolbar_init (EphyFindToolbar *toolbar)
{
EphyFindToolbarPrivate *priv;
GtkToolbar *gtoolbar;
GtkToolItem *item;
GtkWidget *alignment, *arrow, *box, *label;
priv = toolbar->priv = EPHY_FIND_TOOLBAR_GET_PRIVATE (toolbar);
gtoolbar = GTK_TOOLBAR (toolbar);
gtk_toolbar_set_style (gtoolbar, GTK_TOOLBAR_BOTH_HORIZ);
/* Find: |_____| */
alignment = gtk_alignment_new (0.0, 0.5, 1.0, 0.0);
gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 0, 0, 2, 2);
box = gtk_hbox_new (FALSE, 12);
gtk_container_add (GTK_CONTAINER (alignment), box);
label = gtk_label_new (NULL);
gtk_label_set_markup (GTK_LABEL (label), _("Find:"));
gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
priv->entry = gtk_entry_new ();
gtk_entry_set_width_chars (GTK_ENTRY (priv->entry), 32);
gtk_entry_set_max_length (GTK_ENTRY (priv->entry), 512);
gtk_box_pack_start (GTK_BOX (box), priv->entry, TRUE, TRUE, 0);
item = gtk_tool_item_new ();
gtk_container_add (GTK_CONTAINER (item), alignment);
//gtk_tool_item_set_expand (item, TRUE);
gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
gtk_widget_show_all (GTK_WIDGET (item));
/* Next */
arrow = gtk_arrow_new (GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
label = gtk_label_new (_("Find Next"));
priv->next = gtk_tool_button_new (arrow, _("Find Next"));
gtk_tool_item_set_is_important (priv->next, TRUE);
gtk_tool_item_set_tooltip (priv->next, gtoolbar->tooltips,
_("Find next occurrence of the search string"),
NULL);
gtk_toolbar_insert (GTK_TOOLBAR (toolbar), priv->next, -1);
gtk_widget_show_all (GTK_WIDGET (priv->next));
/* Prev */
arrow = gtk_arrow_new (GTK_ARROW_LEFT, GTK_SHADOW_NONE);
label = gtk_label_new (_("Find Previous"));
priv->prev = gtk_tool_button_new (arrow, _("Find Previous"));
gtk_tool_item_set_is_important (priv->prev, TRUE);
gtk_tool_item_set_tooltip (priv->prev, gtoolbar->tooltips,
_("Find previous occurrence of the search string"),
NULL);
gtk_toolbar_insert (GTK_TOOLBAR (toolbar), priv->prev, -1);
gtk_widget_show_all (GTK_WIDGET (priv->prev));
/* connect signals */
g_signal_connect (priv->entry, "key-press-event",
G_CALLBACK (entry_key_press_event_cb), toolbar);
g_signal_connect_after (priv->entry, "changed",
G_CALLBACK (entry_changed_cb), toolbar);
g_signal_connect (GTK_ENTRY (priv->entry)->im_context, "preedit-changed",
G_CALLBACK (entry_preedit_changed_cb), toolbar);
g_signal_connect (priv->entry, "activate",
G_CALLBACK (entry_activate_cb), toolbar);
g_signal_connect_swapped (priv->next, "clicked",
G_CALLBACK (find_next_cb), toolbar);
g_signal_connect_swapped (priv->prev, "clicked",
G_CALLBACK (find_prev_cb), toolbar);
}
static void
ephy_find_toolbar_finalize (GObject *object)
{
EphyFindToolbar *toolbar = EPHY_FIND_TOOLBAR (object);
EphyFindToolbarPrivate *priv = toolbar->priv;
if (priv->find != NULL)
{
g_object_unref (priv->find);
}
parent_class->finalize (object);
}
static void
ephy_find_toolbar_class_init (EphyFindToolbarClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
object_class->finalize = ephy_find_toolbar_finalize;
widget_class->parent_set = ephy_find_toolbar_parent_set;
widget_class->grab_focus = ephy_find_toolbar_grab_focus;
klass->next = ephy_find_toolbar_find_next;
klass->previous = ephy_find_toolbar_find_previous;
klass->close = ephy_find_toolbar_close;
signals[NEXT] =
g_signal_new ("next",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (EphyFindToolbarClass, next),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
signals[PREVIOUS] =
g_signal_new ("previous",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (EphyFindToolbarClass, previous),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
signals[CLOSE] =
g_signal_new ("close",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (EphyFindToolbarClass, close),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
g_type_class_add_private (klass, sizeof (EphyFindToolbarPrivate));
}
/* public functions */
GType
ephy_find_toolbar_get_type (void)
{
static GType type = 0;
if (G_UNLIKELY (type == 0))
{
static const GTypeInfo our_info =
{
sizeof (EphyFindToolbarClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) ephy_find_toolbar_class_init,
NULL,
NULL, /* class_data */
sizeof (EphyFindToolbar),
0, /* n_preallocs */
(GInstanceInitFunc) ephy_find_toolbar_init
};
type = g_type_register_static (GTK_TYPE_TOOLBAR,
"EphyFindToolbar",
&our_info, 0);
}
return type;
}
EphyFindToolbar *
ephy_find_toolbar_new (void)
{
return EPHY_FIND_TOOLBAR (g_object_new (EPHY_TYPE_FIND_TOOLBAR, NULL));
}
const char *
ephy_find_toolbar_get_text (EphyFindToolbar *toolbar)
{
EphyFindToolbarPrivate *priv = toolbar->priv;
return gtk_entry_get_text (GTK_ENTRY (priv->entry));
}
void
ephy_find_toolbar_set_embed (EphyFindToolbar *toolbar,
EphyEmbed *embed)
{
EphyFindToolbarPrivate *priv = toolbar->priv;
if (priv->embed == embed) return;
if (priv->embed != NULL)
{
g_signal_handlers_disconnect_matched (embed, G_SIGNAL_MATCH_DATA,
0, 0, NULL, NULL, toolbar);
}
priv->embed = embed;
if (embed != NULL)
{
set_controls (toolbar, TRUE, TRUE);
g_signal_connect_object (embed, "ge-content-change",
G_CALLBACK (tab_content_changed_cb),
toolbar, G_CONNECT_AFTER);
#ifdef HAVE_TYPEAHEADFIND
g_signal_connect_object (embed, "dom-key-press",
G_CALLBACK (tab_dom_key_press_cb),
toolbar, 0);
#endif
if (priv->find != NULL)
{
g_return_if_fail (GTK_WIDGET_REALIZED (GTK_WIDGET (priv->embed)));
ephy_embed_find_set_embed (priv->find, embed);
}
}
}
void
ephy_find_toolbar_find_next (EphyFindToolbar *toolbar)
{
gboolean found;
found = ephy_embed_find_find_again (get_find (toolbar), TRUE);
set_controls (toolbar, found, found);
}
void
ephy_find_toolbar_find_previous (EphyFindToolbar *toolbar)
{
gboolean found;
found = ephy_embed_find_find_again (get_find (toolbar), FALSE);
set_controls (toolbar, found, found);
}
void
ephy_find_toolbar_open (EphyFindToolbar *toolbar,
gboolean links_only)
{
EphyFindToolbarPrivate *priv = toolbar->priv;
g_return_if_fail (priv->embed != NULL);
priv->links_only = links_only;
priv->explicit_focus = FALSE;
gtk_entry_set_text (GTK_ENTRY (priv->entry), "");
gtk_widget_show (GTK_WIDGET (toolbar));
ephy_embed_activate (priv->embed);
#ifdef HAVE_TYPEAHEADFIND
send_focus_change (priv->entry, TRUE);
#endif
}
void
ephy_find_toolbar_close (EphyFindToolbar *toolbar)
{
EphyFindToolbarPrivate *priv = toolbar->priv;
g_return_if_fail (priv->embed != NULL);
gtk_widget_hide (GTK_WIDGET (toolbar));
/* first unset explicit_focus, else we get infinite recursion */
priv->explicit_focus = FALSE;
ephy_embed_activate (priv->embed);
}
|