summaryrefslogtreecommitdiffstats
path: root/widgets/src/lightbox.c
blob: 4ece61ed977fa0c7ea9801dab7486f8516e82ee5 (plain)
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
/*
 * Copyright (C) 2011  Red Hat, Inc.
 *
 * 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 of the License, 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, see <http://www.gnu.org/licenses/>.
 *
 * Author: Ales Kozumplik <akozumpl@redhat.com>
 */

/* based on an example by Ray Strode <rstrode@redhat.com> */

/**
 * SECTION: lightbox
 * @title: Lightbox
 * @short_description: Functions to draw a window over a shaded background
 *
 * The lightbox is a set of functions used to display one window (a dialog or
 * other similar window, typically) over top of the main window in the
 * background.  The main window is shaded out to make the foreground window
 * stand out more, as well as to reinforce to the user that the background
 * window may not be interacted with.
 */

#include <cairo.h>
#include <gdk/gdk.h>
#include <gtk/gtk.h>

#include "lightbox.h"

static void anaconda_lb_move_window_to_parent(GtkWidget *parent,
                                              GdkEventConfigure *e,
                                              GtkWindow *window)
{
    GdkWindow *p_window, *w_window;
    int pwidth, pheight, width, height, px, py, x, y, nx, ny;

    if (!GTK_IS_WIDGET(parent) || !GTK_IS_WINDOW(window))
        return;

    p_window = gtk_widget_get_window (parent);
    w_window = gtk_widget_get_window (GTK_WIDGET(window));

    if (!GDK_IS_WINDOW(p_window) || !GDK_IS_WINDOW(w_window))
        return;

    pwidth = gdk_window_get_width (p_window);
    pheight = gdk_window_get_height (p_window);
    gdk_window_get_position(p_window, &px, &py);

    width = gdk_window_get_width (w_window);
    height = gdk_window_get_height (w_window);
    gdk_window_get_position(w_window, &x, &y);

    nx = px + pwidth / 2 - width / 2;
    ny = py + pheight / 2 - height / 2;

    if (x != nx || y != ny)
    {
        gdk_window_move (w_window, nx, ny);
        gdk_window_restack(w_window, p_window, TRUE);
    }
}

/**
 * anaconda_lb_show_over:
 * @window: (in) A #GtkWindow
 *
 * Show lightbox over window.
 *
 * Return value: (transfer none): the lightbox widget.
 *
 * Since: 1.0
 */
GtkWindow *anaconda_lb_show_over(GtkWindow *window)
{
    GtkWindow *lightbox;
    GdkWindow *w_window;
    GdkWindow *l_window;
    int width, height;
    cairo_t *cr;
    cairo_pattern_t *pattern;
    cairo_surface_t *surface;

    lightbox = (GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL)));
    gtk_window_set_transient_for(lightbox, window);
    gtk_window_set_decorated(lightbox, FALSE);
    gtk_window_set_has_resize_grip(lightbox, FALSE);
    gtk_window_set_position(lightbox, GTK_WIN_POS_CENTER_ON_PARENT);
    gtk_window_set_type_hint (lightbox, GDK_WINDOW_TYPE_HINT_SPLASHSCREEN);
    gtk_widget_set_app_paintable(GTK_WIDGET(lightbox), TRUE);

    w_window = gtk_widget_get_window (GTK_WIDGET(window));
    width = gdk_window_get_width(w_window);
    height = gdk_window_get_height(w_window);
    gtk_window_set_default_size(lightbox, width, height);
    gtk_widget_realize(GTK_WIDGET(lightbox));
    l_window = gtk_widget_get_window (GTK_WIDGET(lightbox));
    gdk_window_set_background_pattern (l_window, NULL);
    gtk_widget_show(GTK_WIDGET(lightbox));
    surface = gdk_window_create_similar_surface(l_window,
                                                CAIRO_CONTENT_COLOR_ALPHA,
                                                width, height);

    cr = cairo_create (surface);
    gdk_cairo_set_source_window(cr, w_window, 0, 0);
    cairo_paint(cr);
    cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.5);
    cairo_paint(cr);
    cairo_destroy(cr);

    pattern = cairo_pattern_create_for_surface (surface);
    gdk_window_set_background_pattern(l_window, pattern);
    cairo_pattern_destroy (pattern);

    /* make the shade move with the parent window */
    g_signal_connect(window, "configure-event",
                     G_CALLBACK (anaconda_lb_move_window_to_parent), lightbox);

    return lightbox;
}

/**
 * anaconda_lb_destroy:
 * @lightbox: a #GtkWindow
 *
 * Destroys the previously used lightbox.
 *
 * Since: 1.0
 */
void anaconda_lb_destroy(GtkWindow *lightbox)
{
    gtk_widget_destroy(GTK_WIDGET(lightbox));
}