1/*
2 * Copyright (C) 2011, 2013 Igalia S.L.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include "config.h"
20#include "GtkUtilities.h"
21
22#include "IntPoint.h"
23#include <gtk/gtk.h>
24#include <wtf/glib/GUniquePtr.h>
25
26namespace WebCore {
27
28IntPoint convertWidgetPointToScreenPoint(GtkWidget* widget, const IntPoint& point)
29{
30 // FIXME: This is actually a very tricky operation and the results of this function should
31 // only be thought of as a guess. For instance, sometimes it may not correctly take into
32 // account window decorations.
33
34 GtkWidget* toplevelWidget = gtk_widget_get_toplevel(widget);
35 if (!toplevelWidget || !gtk_widget_is_toplevel(toplevelWidget) || !GTK_IS_WINDOW(toplevelWidget))
36 return point;
37
38 GdkWindow* gdkWindow = gtk_widget_get_window(toplevelWidget);
39 if (!gdkWindow)
40 return point;
41
42 int xInWindow, yInWindow;
43 gtk_widget_translate_coordinates(widget, toplevelWidget, point.x(), point.y(), &xInWindow, &yInWindow);
44
45 int windowOriginX, windowOriginY;
46 gdk_window_get_origin(gdkWindow, &windowOriginX, &windowOriginY);
47
48 return IntPoint(windowOriginX + xInWindow, windowOriginY + yInWindow);
49}
50
51bool widgetIsOnscreenToplevelWindow(GtkWidget* widget)
52{
53 return widget && gtk_widget_is_toplevel(widget) && GTK_IS_WINDOW(widget) && !GTK_IS_OFFSCREEN_WINDOW(widget);
54}
55
56template<>
57WallTime wallTimeForEvent(const GdkEvent* event)
58{
59 // This works if and only if the X server or Wayland compositor happens to
60 // be using CLOCK_MONOTONIC for its monotonic time, and so long as
61 // g_get_monotonic_time() continues to do so as well, and so long as
62 // WTF::MonotonicTime continues to use g_get_monotonic_time().
63 auto time = gdk_event_get_time(event);
64 if (time == GDK_CURRENT_TIME)
65 return WallTime::now();
66 return MonotonicTime::fromRawSeconds(time / 1000.).approximateWallTime();
67}
68
69String defaultGtkSystemFont()
70{
71 GUniqueOutPtr<char> fontString;
72 g_object_get(gtk_settings_get_default(), "gtk-font-name", &fontString.outPtr(), nullptr);
73 // We need to remove the size from the value of the property,
74 // which is separated from the font family using a space.
75 if (auto* spaceChar = strrchr(fontString.get(), ' '))
76 *spaceChar = '\0';
77 return String::fromUTF8(fontString.get());
78}
79
80unsigned stateModifierForGdkButton(unsigned button)
81{
82 return 1 << (8 + button - 1);
83}
84
85} // namespace WebCore
86