aboutsummaryrefslogtreecommitdiffstats
path: root/embed/find-dialog.c
blob: 41309c4824951130921f82a4d0a1ef943dfc606c (plain) (blame)
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
/*
 *  Copyright (C) 2002 Jorn Baayen
 *
 *  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, 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 "find-dialog.h"
#include "ephy-prefs.h"
#include "ephy-embed.h"
#include <gtk/gtk.h>

#define CONF_FIND_MATCH_CASE "/apps/epiphany/find/match_case"
#define CONF_FIND_AUTOWRAP "/apps/epiphany/find/autowrap"
#define CONF_FIND_WORD "/apps/epiphany/find/word"

static void find_dialog_class_init (FindDialogClass *klass);
static void find_dialog_init (FindDialog *dialog);
static void find_dialog_finalize (GObject *object);

static void
impl_construct (EphyDialog *dialog,
                const EphyDialogProperty *properties,
                const char *file,
                const char *name);
static void
impl_destruct (EphyDialog *dialog);
static void
impl_show (EphyDialog *dialog);


/* Glade callbacks */
void find_close_button_clicked_cb (GtkWidget *button, EphyDialog *dialog);
void find_next_button_clicked_cb (GtkWidget *button, EphyDialog *dialog);
void find_prev_button_clicked_cb (GtkWidget *button, EphyDialog *dialog);
void find_entry_changed_cb  (GtkWidget *editable, EphyDialog *dialog);
void find_check_toggled_cb (GtkWidget *toggle, EphyDialog *dialog);

static GObjectClass *parent_class = NULL;

struct FindDialogPrivate
{
    EmbedFindInfo *properties;
    GtkWidget *window;
    gboolean can_go_prev;
    gboolean can_go_next;
    gboolean constructed;
};

enum
{
    SEARCH,
    LAST_SIGNAL
};

enum
{
    WINDOW_PROP,
    MATCH_CASE_PROP,
    AUTOWRAP_PROP,
    WORD_PROP,
    BACK_BUTTON,
    FORWARD_BUTTON
};

static const
EphyDialogProperty properties [] =
{
    { WINDOW_PROP, "find_dialog", NULL, PT_NORMAL, NULL },
    { MATCH_CASE_PROP, "case_check", CONF_FIND_MATCH_CASE, PT_NORMAL, NULL },
    { AUTOWRAP_PROP, "wrap_check", CONF_FIND_AUTOWRAP, PT_NORMAL, NULL },
    { WORD_PROP, "find_entry", CONF_FIND_WORD, PT_NORMAL, NULL },
    { BACK_BUTTON, "back_button", NULL, PT_NORMAL, NULL },
    { FORWARD_BUTTON, "forward_button", NULL, PT_NORMAL, NULL },
    { -1, NULL, NULL }
};

static guint find_dialog_signals[LAST_SIGNAL] = { 0 };

GType
find_dialog_get_type (void)
{
        static GType find_dialog_type = 0;

        if (find_dialog_type == 0)
        {
                static const GTypeInfo our_info =
                {
                        sizeof (FindDialogClass),
                        NULL, /* base_init */
                        NULL, /* base_finalize */
                        (GClassInitFunc) find_dialog_class_init,
                        NULL,
                        NULL, /* class_data */
                        sizeof (FindDialog),
                        0, /* n_preallocs */
                        (GInstanceInitFunc) find_dialog_init
                };

                find_dialog_type = g_type_register_static (EPHY_EMBED_DIALOG_TYPE,
                                   "FindDialog",
                                   &our_info, 0);
        }

        return find_dialog_type;

}

static void
find_dialog_class_init (FindDialogClass *klass)
{
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
    EphyDialogClass *ephy_dialog_class;

        parent_class = g_type_class_peek_parent (klass);
    ephy_dialog_class = EPHY_DIALOG_CLASS (klass);

        object_class->finalize = find_dialog_finalize;

    ephy_dialog_class->construct = impl_construct;
    ephy_dialog_class->destruct = impl_destruct;
    ephy_dialog_class->show = impl_show;

    find_dialog_signals[SEARCH] =
                g_signal_new ("search",
                              G_OBJECT_CLASS_TYPE (object_class),
                              G_SIGNAL_RUN_FIRST,
                              G_STRUCT_OFFSET (FindDialogClass, search),
                              NULL, NULL,
                              g_cclosure_marshal_VOID__VOID,
                              G_TYPE_NONE,
                              0);
}

static void
find_update_nav (EphyDialog *dialog)
{
    GtkWidget *forward_button;
    GtkWidget *back_button;

    g_signal_emit (G_OBJECT (dialog), find_dialog_signals[SEARCH], 0);

    if (!FIND_DIALOG(dialog)->priv->constructed) return;

    forward_button = ephy_dialog_get_control (dialog, FORWARD_BUTTON);
        gtk_widget_set_sensitive (forward_button,
                  FIND_DIALOG(dialog)->priv->can_go_next);

    back_button = ephy_dialog_get_control (dialog, BACK_BUTTON);
        gtk_widget_set_sensitive (back_button,
                  FIND_DIALOG(dialog)->priv->can_go_prev);
}

static void
ensure_constructed (FindDialog *dialog)
{
    if (!dialog->priv->constructed)
    {
        ephy_dialog_construct (EPHY_DIALOG(dialog),
                       properties,
                       "epiphany.glade",
                       "find_dialog");
    }
}

static void
find_dialog_init (FindDialog *dialog)
{
    dialog->priv = g_new0 (FindDialogPrivate, 1);

    dialog->priv->properties = NULL;
    dialog->priv->can_go_prev = TRUE;
    dialog->priv->can_go_next = TRUE;
    dialog->priv->constructed = FALSE;

    ensure_constructed (dialog);
}

static void
impl_construct (EphyDialog *dialog,
                const EphyDialogProperty *properties,
                const char *file,
                const char *name)
{
    FIND_DIALOG(dialog)->priv->constructed = TRUE;

    EPHY_DIALOG_CLASS (parent_class)->construct (dialog, properties, file, name);
}

static void
impl_destruct (EphyDialog *dialog)
{
    FIND_DIALOG(dialog)->priv->constructed = FALSE;

    EPHY_DIALOG_CLASS (parent_class)->destruct (dialog);
}

static void
find_get_info (EphyDialog *dialog)
{
        EmbedFindInfo *properties;
        char *search_string;
    GValue word = {0, };
    GValue match_case = {0, };
    GValue wrap = {0, };
    FindDialog *find_dialog = FIND_DIALOG(dialog);

        /* get the search string from the entry field */
    ephy_dialog_get_value (dialog, WORD_PROP, &word);
        search_string = g_strdup(g_value_get_string (&word));
    g_value_unset (&word);

        /* don't do null searches */
        if (search_string[0] == '\0')
        {
        find_dialog->priv->can_go_prev = FALSE;
        find_dialog->priv->can_go_next = FALSE;
                return;
        }

    if (find_dialog->priv->properties != NULL)
    {
        g_free (find_dialog->priv->properties->search_string);
        g_free (find_dialog->priv->properties);
    }

        /* build search structure */
        properties = g_new0 (EmbedFindInfo,1);
        properties->search_string = search_string;

    ephy_dialog_get_value (dialog, MATCH_CASE_PROP, &match_case);
        properties->match_case = g_value_get_boolean (&match_case);

    ephy_dialog_get_value (dialog, AUTOWRAP_PROP, &wrap);
        properties->wrap = g_value_get_boolean (&wrap);

        properties->entire_word = FALSE;
    properties->search_frames = TRUE;

    find_dialog->priv->properties = properties;
}

static void
dialog_constrain_height (FindDialog *dialog)
{
    GdkGeometry geometry;
    GtkWindow *window = GTK_WINDOW (dialog->priv->window);

    /* Do not allow to resize the widget vertically */
    geometry.max_height  = 0;
    geometry.max_width = gdk_screen_get_width
            (gtk_widget_get_screen (GTK_WIDGET (window)));
    gtk_window_set_geometry_hints (window, GTK_WIDGET (window),
                       &geometry,
                       GDK_HINT_MAX_SIZE);
}

static void
impl_show (EphyDialog *dialog)
{
    GdkPixbuf *icon;
    FindDialog *find_dialog = FIND_DIALOG(dialog);
    ensure_constructed (find_dialog);

    find_dialog->priv->can_go_prev = TRUE;
    find_dialog->priv->can_go_next = TRUE;
    find_dialog->priv->window = ephy_dialog_get_control (dialog, WINDOW_PROP);
    find_get_info (dialog);
    find_update_nav (dialog);

    /* Focus the text entry.  This will correctly select or leave
     * unselected the existing text in the entry depending on the
     * 'gtk-entry-select-on-focus = 0 / 1' setting in user's gtkrc.
     */
    gtk_widget_grab_focus (ephy_dialog_get_control (dialog, WORD_PROP));

    
    icon = gtk_widget_render_icon (find_dialog->priv->window, 
                              GTK_STOCK_FIND,
                              GTK_ICON_SIZE_MENU,
                              "find_dialog");
    gtk_window_set_icon (GTK_WINDOW(find_dialog->priv->window), icon);
    g_object_unref (icon);
    
    dialog_constrain_height (find_dialog);
    
    EPHY_DIALOG_CLASS (parent_class)->show (dialog);
}

static void
find_dialog_finalize (GObject *object)
{
    FindDialog *dialog;

        g_return_if_fail (object != NULL);
        g_return_if_fail (IS_FIND_DIALOG (object));

    dialog = FIND_DIALOG (object);

        g_return_if_fail (dialog->priv != NULL);

        g_free (dialog->priv);

        G_OBJECT_CLASS (parent_class)->finalize (object);
}

EphyDialog *
find_dialog_new (EphyEmbed *embed)
{
    FindDialog *dialog;

    dialog = FIND_DIALOG (g_object_new (FIND_DIALOG_TYPE,
                     "EphyEmbed", embed,
                     NULL));

    return EPHY_DIALOG(dialog);
}

EphyDialog *
find_dialog_new_with_parent (GtkWidget *window,
                 EphyEmbed *embed)
{
    FindDialog *dialog;

    dialog = FIND_DIALOG (g_object_new (FIND_DIALOG_TYPE,
                     "EphyEmbed", embed,
                     "ParentWindow", window,
                     NULL));

    return EPHY_DIALOG(dialog);
}

gboolean
find_dialog_can_go_next (FindDialog *dialog)
{
    return dialog->priv->can_go_next;
}

gboolean
find_dialog_can_go_prev (FindDialog *dialog)
{
    return dialog->priv->can_go_prev;
}

void
find_dialog_go_next (FindDialog *dialog,
             gboolean interactive)
{
    gresult result;
    EphyEmbed *embed;

    if (!find_dialog_can_go_next (dialog)) return;

    dialog->priv->properties->backwards = FALSE;
    dialog->priv->properties->interactive = interactive;

    embed = ephy_embed_dialog_get_embed (EPHY_EMBED_DIALOG(dialog));
    g_return_if_fail (embed != NULL);

        result = ephy_embed_find (embed,
                  dialog->priv->properties);

    dialog->priv->can_go_prev = TRUE;
    if (result != G_OK)
    {
        dialog->priv->can_go_next = FALSE;
    }

    find_update_nav (EPHY_DIALOG(dialog));
}

void
find_dialog_go_prev (FindDialog *dialog,
             gboolean interactive)
{
    gresult result;
    EphyEmbed *embed;

    if (!find_dialog_can_go_prev (dialog)) return;

    dialog->priv->properties->backwards = TRUE;
    dialog->priv->properties->interactive = interactive;

    embed = ephy_embed_dialog_get_embed (EPHY_EMBED_DIALOG(dialog));
    g_return_if_fail (embed != NULL);

    result = ephy_embed_find (embed,
                  dialog->priv->properties);

    dialog->priv->can_go_next = TRUE;
    if (result != G_OK)
    {
        dialog->priv->can_go_prev = FALSE;
    }

    find_update_nav (EPHY_DIALOG(dialog));
}

void
find_close_button_clicked_cb (GtkWidget *button,
                  EphyDialog *dialog)
{
    ephy_dialog_destruct (dialog);
    g_object_unref (dialog);
}

void find_next_button_clicked_cb (GtkWidget *button,
                  EphyDialog *dialog)
{
    find_dialog_go_next (FIND_DIALOG(dialog), TRUE);
}

void
find_prev_button_clicked_cb (GtkWidget *button,
                 EphyDialog *dialog)
{
    find_dialog_go_prev (FIND_DIALOG(dialog), TRUE);
}

void
find_entry_changed_cb  (GtkWidget *editable,
            EphyDialog *dialog)
{
    FindDialog *find_dialog = FIND_DIALOG(dialog);

    find_dialog->priv->can_go_prev = TRUE;
    find_dialog->priv->can_go_next = TRUE;

    find_get_info (dialog);

    find_update_nav (dialog);
}

void
find_check_toggled_cb (GtkWidget *toggle,
                       EphyDialog *dialog)
{
    FindDialog *find_dialog = FIND_DIALOG(dialog);

    find_dialog->priv->can_go_prev = TRUE;
    find_dialog->priv->can_go_next = TRUE;

    find_get_info (dialog);

        find_update_nav (dialog);
}