1 /*
2 * Copyright (C) 2006 Michael Emmel mike.emmel@gmail.com
3 * Copyright (C) 2007, 2009 Holger Hans Peter Freyther
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "config.h"
29 #include "Widget.h"
30
31 #include "Cursor.h"
32 #include "FrameView.h"
33 #include "GraphicsContext.h"
34 #include "HostWindow.h"
35 #include "IntRect.h"
36 #include "RenderObject.h"
37
38 #include <gdk/gdk.h>
39 #include <gtk/gtk.h>
40
41 namespace WebCore {
42
43 static GdkCursor* lastSetCursor;
44
Widget(PlatformWidget widget)45 Widget::Widget(PlatformWidget widget)
46 {
47 init(widget);
48 }
49
~Widget()50 Widget::~Widget()
51 {
52 ASSERT(!parent());
53 releasePlatformWidget();
54 }
55
setFocus()56 void Widget::setFocus()
57 {
58 gtk_widget_grab_focus(platformWidget() ? platformWidget() : GTK_WIDGET(root()->hostWindow()->platformPageClient()));
59 }
60
gdkDrawable(PlatformWidget widget)61 static GdkDrawable* gdkDrawable(PlatformWidget widget)
62 {
63 return widget ? widget->window : 0;
64 }
65
setCursor(const Cursor & cursor)66 void Widget::setCursor(const Cursor& cursor)
67 {
68 GdkCursor* platformCursor = cursor.impl();
69
70 // http://bugs.webkit.org/show_bug.cgi?id=16388
71 // [GTK] Widget::setCursor() gets called frequently
72 //
73 // gdk_window_set_cursor() in certain GDK backends seems to be an
74 // expensive operation, so avoid it if possible.
75
76 if (platformCursor == lastSetCursor)
77 return;
78
79 gdk_window_set_cursor(gdkDrawable(platformWidget()) ? GDK_WINDOW(gdkDrawable(platformWidget())) : GTK_WIDGET(root()->hostWindow()->platformPageClient())->window, platformCursor);
80 lastSetCursor = platformCursor;
81 }
82
show()83 void Widget::show()
84 {
85 if (!platformWidget())
86 return;
87 gtk_widget_show(platformWidget());
88 }
89
hide()90 void Widget::hide()
91 {
92 if (!platformWidget())
93 return;
94 gtk_widget_hide(platformWidget());
95 }
96
paint(GraphicsContext * context,const IntRect & rect)97 void Widget::paint(GraphicsContext* context, const IntRect& rect)
98 {
99 }
100
setIsSelected(bool isSelected)101 void Widget::setIsSelected(bool isSelected)
102 {
103 if (!platformWidget())
104 return;
105
106 // See if the platformWidget has a webkit-widget-is-selected property
107 // and set it afterwards.
108 GParamSpec* spec = g_object_class_find_property(G_OBJECT_GET_CLASS(platformWidget()),
109 "webkit-widget-is-selected");
110 if (!spec)
111 return;
112
113 g_object_set(platformWidget(), "webkit-widget-is-selected", isSelected, NULL);
114 }
115
frameRect() const116 IntRect Widget::frameRect() const
117 {
118 return m_frame;
119 }
120
setFrameRect(const IntRect & rect)121 void Widget::setFrameRect(const IntRect& rect)
122 {
123 m_frame = rect;
124 }
125
releasePlatformWidget()126 void Widget::releasePlatformWidget()
127 {
128 if (!platformWidget())
129 return;
130 g_object_unref(platformWidget());
131 }
132
retainPlatformWidget()133 void Widget::retainPlatformWidget()
134 {
135 if (!platformWidget())
136 return;
137 g_object_ref_sink(platformWidget());
138 }
139
140 }
141