aboutsummaryrefslogtreecommitdiffstats
path: root/lib/widgets/ephy-icon-entry.c
blob: 1ab2dc0b64979c386da48468f857812c1b8a3f2b (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
/*
 *  Copyright (C) 2003, 2004, 2005  Christian Persch
 *
 *  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, write to the
 *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 *
 *  Adapted and modified from gtk+ code:
 *
 *  Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *  Modified by the GTK+ Team and others 1997-2005.  See the AUTHORS
 *  file in the gtk+ distribution for a list of people on the GTK+ Team.
 *  See the ChangeLog in the gtk+ distribution files for a list of changes.
 *  These files are distributed with GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 *
 *  $Id$
 */

#include "config.h"

#include "ephy-icon-entry.h"

#include <gtk/gtkentry.h>
#include <gtk/gtkbox.h>
#include <gtk/gtkhbox.h>

#define EPHY_ICON_ENTRY_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_ICON_ENTRY, EphyIconEntryPrivate))

struct _EphyIconEntryPrivate
{
    GtkWidget *hbox;
};

static GtkWidgetClass *parent_class = NULL;

/* private helper functions */

static gboolean
entry_focus_change_cb (GtkWidget *widget,
               GdkEventFocus *event,
               GtkWidget *entry)
{
    gtk_widget_queue_draw (entry);

    return FALSE;
}

static void
ephy_icon_entry_get_borders (GtkWidget *widget,
                 GtkWidget *entry,
                 int *xborder,
                 int *yborder)
{
    int focus_width;
    gboolean interior_focus;

    g_return_if_fail (entry->style != NULL);

    gtk_widget_style_get (entry,
                  "focus-line-width", &focus_width,
                  "interior-focus", &interior_focus,
                  NULL);

    *xborder = entry->style->xthickness;
    *yborder = entry->style->ythickness;

    if (!interior_focus)
    {
        *xborder += focus_width;
        *yborder += focus_width;
    }
}

static void
ephy_icon_entry_paint (GtkWidget *widget,
               GdkEventExpose *event)
{
    EphyIconEntry *entry = EPHY_ICON_ENTRY (widget);
    GtkWidget *entry_widget = entry->entry;
    int x = 0, y = 0, width, height, focus_width;
    gboolean interior_focus;

    gtk_widget_style_get (entry_widget,
                  "interior-focus", &interior_focus,
                  "focus-line-width", &focus_width,
                  NULL);

    gdk_drawable_get_size (widget->window, &width, &height);

    if (GTK_WIDGET_HAS_FOCUS (entry_widget) && !interior_focus)
    {
        x += focus_width;
        y += focus_width;
        width -= 2 * focus_width;
        height -= 2 * focus_width;
    }

    gtk_paint_flat_box (entry_widget->style, widget->window, 
                GTK_WIDGET_STATE (entry_widget), GTK_SHADOW_NONE,
                NULL, entry_widget, "entry_bg", 
                /* FIXME: was 0, 0 in gtk_entry_expose, but I think this is correct: */
                x, y, width, height);
     
    gtk_paint_shadow (entry_widget->style, widget->window,
              GTK_STATE_NORMAL, GTK_SHADOW_IN,
              NULL, entry_widget, "entry",
              x, y, width, height);

    if (GTK_WIDGET_HAS_FOCUS (entry_widget) && !interior_focus)
    {
        x -= focus_width;
        y -= focus_width;
        width += 2 * focus_width;
        height += 2 * focus_width;

        gtk_paint_focus (entry_widget->style, widget->window,
                 GTK_WIDGET_STATE (entry_widget),
                 NULL, entry_widget, "entry",
                 /* FIXME: was 0, 0 in gtk_entry_draw_frame, but I think this is correct: */
                 x, y, width, height);
    }
}

/* Class implementation */

static void
ephy_icon_entry_init (EphyIconEntry *entry)
{
    EphyIconEntryPrivate *priv;
    GtkWidget *widget = (GtkWidget *) entry;

    priv = entry->priv = EPHY_ICON_ENTRY_GET_PRIVATE (entry);

    GTK_WIDGET_UNSET_FLAGS (widget, GTK_NO_WINDOW);

    priv->hbox = gtk_hbox_new (FALSE, /* FIXME */ 0);
    gtk_container_add (GTK_CONTAINER (entry), priv->hbox);

    entry->entry = gtk_entry_new ();
    gtk_entry_set_has_frame (GTK_ENTRY (entry->entry), FALSE);
    gtk_box_pack_start (GTK_BOX (priv->hbox), entry->entry, TRUE, TRUE, /* FIXME */ 0);

    /* We need to queue a redraw when focus changes, to comply with themes
     * (like Clearlooks) which draw focused and unfocused entries differently.
     */
    g_signal_connect_after (entry->entry, "focus-in-event",
                G_CALLBACK (entry_focus_change_cb), entry);
    g_signal_connect_after (entry->entry, "focus-out-event",
                G_CALLBACK (entry_focus_change_cb), entry);
}

static void
ephy_icon_entry_realize (GtkWidget *widget)
{
    GdkWindowAttr attributes;
    gint attributes_mask;
    gint border_width;

    GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);

    border_width = GTK_CONTAINER (widget)->border_width;

    attributes.x = widget->allocation.x + border_width;
    attributes.y = widget->allocation.y + border_width;
    attributes.width = widget->allocation.width - 2 * border_width;
    attributes.height = widget->allocation.height - 2 * border_width;
    attributes.window_type = GDK_WINDOW_CHILD;
    attributes.event_mask = gtk_widget_get_events (widget)
                | GDK_EXPOSURE_MASK;

    attributes.visual = gtk_widget_get_visual (widget);
    attributes.colormap = gtk_widget_get_colormap (widget);
    attributes.wclass = GDK_INPUT_OUTPUT;
    attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;

    widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
                     &attributes, attributes_mask);
    gdk_window_set_user_data (widget->window, widget);

    widget->style = gtk_style_attach (widget->style, widget->window);

    gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
}

static void
ephy_icon_entry_size_request (GtkWidget *widget,
                  GtkRequisition *requisition)
{
    EphyIconEntry *entry = EPHY_ICON_ENTRY (widget);
    GtkContainer *container = GTK_CONTAINER (widget);
    GtkBin *bin = GTK_BIN (widget);
    int xborder, yborder;

    requisition->width = requisition->height = container->border_width * 2;

    gtk_widget_ensure_style (entry->entry);
    ephy_icon_entry_get_borders (widget, entry->entry, &xborder, &yborder);

    if (GTK_WIDGET_VISIBLE (bin->child))
    {
        GtkRequisition child_requisition;

        gtk_widget_size_request (bin->child, &child_requisition);
        requisition->width += child_requisition.width;
        requisition->height += child_requisition.height;
    }

    requisition->width += 2 * xborder;
    requisition->height += 2 * yborder;
}

static void
ephy_icon_entry_size_allocate (GtkWidget *widget,
                   GtkAllocation *allocation)
{
    EphyIconEntry *entry = EPHY_ICON_ENTRY (widget);
    GtkContainer *container = GTK_CONTAINER (widget);
    GtkBin *bin = GTK_BIN (widget);
    GtkAllocation child_allocation;
    int xborder, yborder;

    widget->allocation = *allocation;

    ephy_icon_entry_get_borders (widget, entry->entry, &xborder, &yborder);

    if (GTK_WIDGET_REALIZED (widget))
    {
        child_allocation.x = container->border_width;
        child_allocation.y = container->border_width;
        child_allocation.width = MAX (allocation->width - container->border_width * 2, 0);
        child_allocation.height = MAX (allocation->height - container->border_width * 2, 0);

        gdk_window_move_resize (widget->window,
                    allocation->x + child_allocation.x,
                    allocation->y + child_allocation.y,
                    child_allocation.width,
                    child_allocation.height);
    }
  
    child_allocation.x = container->border_width + xborder;
    child_allocation.y = container->border_width + yborder;
    child_allocation.width = MAX (allocation->width - (container->border_width + xborder) * 2, 0);
    child_allocation.height = MAX (allocation->height - (container->border_width + yborder) * 2, 0);

    gtk_widget_size_allocate (bin->child, &child_allocation);
}

static gboolean
ephy_icon_entry_expose (GtkWidget *widget,
            GdkEventExpose *event)
{
    if (GTK_WIDGET_DRAWABLE (widget) &&
        event->window == widget->window)
    {
        ephy_icon_entry_paint (widget, event);
    }

    return parent_class->expose_event (widget, event);
}

static void
ephy_icon_entry_class_init (EphyIconEntryClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

    parent_class = GTK_WIDGET_CLASS (g_type_class_peek_parent (klass));

    widget_class->realize = ephy_icon_entry_realize;
    widget_class->size_request = ephy_icon_entry_size_request;
    widget_class->size_allocate = ephy_icon_entry_size_allocate;
    widget_class->expose_event = ephy_icon_entry_expose;

    g_type_class_add_private (object_class, sizeof (EphyIconEntryPrivate));
}

GType
ephy_icon_entry_get_type (void)
{
    static GType type = 0;

    if (G_UNLIKELY (type == 0))
    {
        static const GTypeInfo our_info =
        {
            sizeof (EphyIconEntryClass),
            NULL,
            NULL,
            (GClassInitFunc) ephy_icon_entry_class_init,
            NULL,
            NULL,
            sizeof (EphyIconEntry),
            0,
            (GInstanceInitFunc) ephy_icon_entry_init
        };

        type = g_type_register_static (GTK_TYPE_BIN,
                           "EphyIconEntry",
                           &our_info, 0);
    }

    return type;
}

/* public functions */

GtkWidget *
ephy_icon_entry_new (void)
{
    return GTK_WIDGET (g_object_new (EPHY_TYPE_ICON_ENTRY, NULL));
}

void
ephy_icon_entry_pack_widget (EphyIconEntry *entry,
                 GtkWidget *widget,
                 gboolean start)
{
    EphyIconEntryPrivate *priv;

    g_return_if_fail (EPHY_IS_ICON_ENTRY (entry));

    priv = entry->priv;

    if (start)
    {
        gtk_box_pack_start (GTK_BOX (priv->hbox), widget, FALSE, FALSE, /* FIXME */ 2);
        gtk_box_reorder_child (GTK_BOX (priv->hbox), widget, 0);
    }
    else
    {
        gtk_box_pack_end (GTK_BOX (priv->hbox), widget, FALSE, FALSE, /* FIXME */ 2);
    }
}