1/*
2 * Copyright © 2018 Benjamin Otte
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.1 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, see <http://www.gnu.org/licenses/>.
16 *
17 * Authors: Benjamin Otte <otte@gnome.org>
18 */
19
20#include "config.h"
21
22#include "focusoverlay.h"
23
24#include "gtkintl.h"
25#include "gtkwidget.h"
26#include "gtkroot.h"
27#include "gtknative.h"
28
29struct _GtkFocusOverlay
30{
31 GtkInspectorOverlay parent_instance;
32
33 GdkRGBA color;
34};
35
36struct _GtkFocusOverlayClass
37{
38 GtkInspectorOverlayClass parent_class;
39};
40
41G_DEFINE_TYPE (GtkFocusOverlay, gtk_focus_overlay, GTK_TYPE_INSPECTOR_OVERLAY)
42
43static void
44gtk_focus_overlay_snapshot (GtkInspectorOverlay *overlay,
45 GtkSnapshot *snapshot,
46 GskRenderNode *node,
47 GtkWidget *widget)
48{
49 GtkFocusOverlay *self = GTK_FOCUS_OVERLAY (ptr: overlay);
50 GtkWidget *focus;
51 graphene_rect_t bounds;
52
53 if (!GTK_IS_NATIVE (ptr: widget))
54 return;
55
56 focus = gtk_root_get_focus (self: GTK_ROOT (ptr: gtk_widget_get_root (widget)));
57 if (!focus)
58 return;
59
60 if (!gtk_widget_is_ancestor (widget: focus, ancestor: widget))
61 return;
62
63 if (GTK_WIDGET (gtk_widget_get_native (focus)) != widget)
64 return;
65
66 if (!gtk_widget_compute_bounds (widget: focus, target: widget, out_bounds: &bounds))
67 return;
68
69 gtk_snapshot_append_color (snapshot, color: &self->color, bounds: &bounds);
70}
71
72static void
73gtk_focus_overlay_queue_draw (GtkInspectorOverlay *overlay)
74{
75}
76
77static void
78gtk_focus_overlay_class_init (GtkFocusOverlayClass *klass)
79{
80 GtkInspectorOverlayClass *overlay_class = GTK_INSPECTOR_OVERLAY_CLASS (ptr: klass);
81
82 overlay_class->snapshot = gtk_focus_overlay_snapshot;
83 overlay_class->queue_draw = gtk_focus_overlay_queue_draw;
84}
85
86static void
87gtk_focus_overlay_init (GtkFocusOverlay *self)
88{
89 self->color = (GdkRGBA) { 0.5, 0.0, 1.0, 0.2 };
90}
91
92GtkInspectorOverlay *
93gtk_focus_overlay_new (void)
94{
95 GtkFocusOverlay *self;
96
97 self = g_object_new (GTK_TYPE_FOCUS_OVERLAY, NULL);
98
99 return GTK_INSPECTOR_OVERLAY (ptr: self);
100}
101
102void
103gtk_focus_overlay_set_color (GtkFocusOverlay *self,
104 const GdkRGBA *color)
105{
106 if (gdk_rgba_equal (p1: &self->color, p2: color))
107 return;
108
109 self->color = *color;
110 gtk_inspector_overlay_queue_draw (self: GTK_INSPECTOR_OVERLAY (ptr: self));
111}
112

source code of gtk/gtk/inspector/focusoverlay.c