window: Add initial support for client-side decorations under Wayland

This commit is contained in:
Kristian Høgsberg
2012-04-11 17:55:54 +01:00
committed by Rob Bradford
parent 437f2c7853
commit 00bc6df2d5
3 changed files with 401 additions and 10 deletions

42
gtk.css Normal file
View File

@@ -0,0 +1,42 @@
/* Copy this to .config/gtk-3.0/gtk.css */
.titlebar > GtkLabel:backdrop {
color: darker (@bg_color);
}
.titlebar > GtkLabel {
font: Sans Bold 10;
text-shadow: 1 1 lighter (@bg_color);
}
.titlebar GtkButton {
border-width: 0;
background: none;
}
.titlebar:backdrop {
background-image: none;
}
.titlebar {
background-image: -gtk-gradient (linear, center top, center bottom,
from (white),
to (darker(@bg_color)));
border-radius: 5 5 0 0;
}
.foo {
/* Nag Company until he implements the border-image-slice fill property */
border-image: url("toolButton.png") 0 5 / 0 5 stretch stretch;
border-width: 0 5 0 5;
}
.window-border {
background-color: blue;
}
.window-border {
border-image: url("theme.png") 21 24 22 24 / 21 24 22 24 stretch stretch;
border-width: 21 24 22 24;
border-style: solid;
}

View File

@@ -49,6 +49,9 @@
#include "gtkintl.h"
#include "gtkstylecontextprivate.h"
#include "gtktypebuiltins.h"
#include "gtklabel.h"
#include "gtkbox.h"
#include "gtkbutton.h"
#include "a11y/gtkwindowaccessible.h"
#include "deprecated/gtkstyle.h"
@@ -57,6 +60,10 @@
#include "x11/gdkx.h"
#endif
#ifdef GDK_WINDOWING_WAYLAND
#include "wayland/gdkwayland.h"
#endif
/**
* SECTION:gtkwindow
* @title: GtkWindow
@@ -134,6 +141,13 @@ struct _GtkWindowPrivate
guint auto_mnemonics_timeout_id;
GtkWidget *title_box;
GtkWidget *title_label;
GtkWidget *title_close_button;
int title_height;
GtkBorder title_border;
GtkBorder window_border;
/* The following flags are initially TRUE (before a window is mapped).
* They cause us to compute a configure request that involves
* default-only parameters. Once mapped, we set them to FALSE.
@@ -348,6 +362,12 @@ static void gtk_window_style_updated (GtkWidget *widget);
static gboolean gtk_window_state_event (GtkWidget *widget,
GdkEventWindowState *event);
static void gtk_window_check_resize (GtkContainer *container);
static void gtk_window_forall (GtkContainer *container,
gboolean include_internals,
GtkCallback callback,
gpointer callback_data);
static void gtk_window_remove (GtkContainer *container,
GtkWidget *child);
static gint gtk_window_focus (GtkWidget *widget,
GtkDirectionType direction);
static void gtk_window_move_focus (GtkWidget *widget,
@@ -429,6 +449,14 @@ static void gtk_window_on_theme_variant_changed (GtkSettings *settings,
#endif
static void gtk_window_set_theme_variant (GtkWindow *window);
static void gtk_window_get_preferred_width (GtkWidget *widget,
gint *minimum_size,
gint *natural_size);
static void gtk_window_get_preferred_height (GtkWidget *widget,
gint *minimum_size,
gint *natural_size);
static GSList *toplevel_list = NULL;
static guint window_signals[LAST_SIGNAL] = { 0 };
static GList *default_icon_list = NULL;
@@ -589,8 +617,12 @@ gtk_window_class_init (GtkWindowClass *klass)
widget_class->direction_changed = gtk_window_direction_changed;
widget_class->state_changed = gtk_window_state_changed;
widget_class->style_updated = gtk_window_style_updated;
widget_class->get_preferred_width = gtk_window_get_preferred_width;
widget_class->get_preferred_height = gtk_window_get_preferred_height;
container_class->check_resize = gtk_window_check_resize;
container_class->forall = gtk_window_forall;
container_class->remove = gtk_window_remove;
klass->set_focus = gtk_window_real_set_focus;
@@ -1115,6 +1147,22 @@ gtk_window_class_init (GtkWindowClass *klass)
gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_WINDOW_ACCESSIBLE);
}
static void
gtk_window_title_close_clicked (GtkWidget *button, void *data)
{
/* Synthesize delete_event to close dialog. */
GdkEvent *event;
event = gdk_event_new (GDK_DELETE);
event->any.window = g_object_ref (gtk_widget_get_window (button));
event->any.send_event = TRUE;
gtk_main_do_event (event);
gdk_event_free (event);
}
static void
gtk_window_init (GtkWindow *window)
{
@@ -1639,6 +1687,9 @@ gtk_window_set_title (GtkWindow *window,
priv->title);
}
if (priv->title_label)
gtk_label_set_text (GTK_LABEL (priv->title_label), priv->title);
g_object_notify (G_OBJECT (window), "title");
}
@@ -4755,6 +4806,43 @@ gtk_window_finalize (GObject *object)
G_OBJECT_CLASS (gtk_window_parent_class)->finalize (object);
}
static void
create_decoration (GtkWidget *widget)
{
GtkWindow *window = GTK_WINDOW (widget);
GtkWindowPrivate *priv = window->priv;
GtkStyleContext *context;
const char *title = "GtkWindow";
#ifdef GDK_WINDOWING_WAYLAND
if (!GDK_IS_WAYLAND_DISPLAY_MANAGER (gdk_display_manager_get ()))
return;
#else
return;
#endif
if (priv->type == GTK_WINDOW_POPUP || !priv->decorated || priv->title_box)
return;
priv->title_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10);
context = gtk_widget_get_style_context (priv->title_box);
gtk_style_context_add_class (context, "titlebar");
gtk_widget_set_parent (priv->title_box, GTK_WIDGET (window));
priv->title_label = gtk_label_new (NULL);
if (priv->title)
title = priv->title;
gtk_label_set_markup (GTK_LABEL (priv->title_label), title);
gtk_box_pack_start (GTK_BOX (priv->title_box),
priv->title_label, TRUE, TRUE, 0);
priv->title_close_button = gtk_button_new_with_label ("×");
gtk_box_pack_end (GTK_BOX (priv->title_box),
priv->title_close_button, FALSE, FALSE, 0);
g_signal_connect (priv->title_close_button, "clicked",
G_CALLBACK (gtk_window_title_close_clicked), window);
}
static void
gtk_window_show (GtkWidget *widget)
{
@@ -4765,12 +4853,17 @@ gtk_window_show (GtkWidget *widget)
gboolean need_resize;
gboolean is_plug;
create_decoration (widget);
if (!gtk_widget_is_toplevel (GTK_WIDGET (widget)))
{
GTK_WIDGET_CLASS (gtk_window_parent_class)->show (widget);
return;
}
if (priv->title_box)
gtk_widget_show_all (priv->title_box);
_gtk_widget_set_visible_flag (widget, TRUE);
need_resize = _gtk_widget_get_alloc_needed (widget) || !gtk_widget_get_realized (widget);
@@ -4897,6 +4990,9 @@ gtk_window_map (GtkWidget *widget)
!gtk_widget_get_mapped (child))
gtk_widget_map (child);
if (!gtk_widget_get_mapped (priv->title_box))
gtk_widget_map (priv->title_box);
gdk_window = gtk_widget_get_window (widget);
if (priv->maximize_initially)
@@ -5051,6 +5147,9 @@ gtk_window_unmap (GtkWidget *widget)
priv->above_initially = (state & GDK_WINDOW_STATE_ABOVE) != 0;
priv->below_initially = (state & GDK_WINDOW_STATE_BELOW) != 0;
if (priv->title_box)
gtk_widget_unmap (priv->title_box);
child = gtk_bin_get_child (&(window->bin));
if (child)
gtk_widget_unmap (child);
@@ -5225,6 +5324,7 @@ gtk_window_realize (GtkWidget *widget)
attributes.height = allocation.height;
attributes.event_mask = gtk_widget_get_events (widget);
attributes.event_mask |= (GDK_EXPOSURE_MASK |
GDK_BUTTON_PRESS_MASK |
GDK_KEY_PRESS_MASK |
GDK_KEY_RELEASE_MASK |
GDK_ENTER_NOTIFY_MASK |
@@ -5549,20 +5649,39 @@ gtk_window_size_allocate (GtkWidget *widget,
GtkAllocation *allocation)
{
GtkWindow *window = GTK_WINDOW (widget);
GtkWindowPrivate *priv = window->priv;
GtkAllocation child_allocation;
GtkWidget *child;
guint border_width;
_gtk_window_set_allocation (window, allocation);
border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
if (priv->title_box && gtk_widget_get_visual(priv->title_box))
{
child_allocation.x = priv->title_border.left + priv->window_border.left;
child_allocation.y = priv->title_border.top + priv->window_border.top;
child_allocation.width =
MAX (1, (gint) allocation->width -
priv->title_border.left - priv->title_border.right -
priv->window_border.left - priv->window_border.right);
child_allocation.height = priv->title_height;
gtk_widget_size_allocate (priv->title_box, &child_allocation);
}
child = gtk_bin_get_child (&(window->bin));
if (child && gtk_widget_get_visible (child))
{
border_width = gtk_container_get_border_width (GTK_CONTAINER (window));
child_allocation.x = border_width;
child_allocation.y = border_width;
child_allocation.width = MAX (1, allocation->width - border_width * 2);
child_allocation.height = MAX (1, allocation->height - border_width * 2);
child_allocation.x = border_width + priv->window_border.left;
child_allocation.y =
border_width + priv->window_border.top + priv->title_height +
priv->title_border.top + priv->title_border.bottom;
child_allocation.width =
MAX (1, (gint)allocation->width - child_allocation.x * 2);
child_allocation.height =
MAX (1, (gint)allocation->height -
child_allocation.y - border_width - priv->window_border.bottom);
gtk_widget_size_allocate (child, &child_allocation);
}
@@ -5896,6 +6015,7 @@ gtk_window_get_resize_grip_area (GtkWindow *window,
GdkRectangle *rect)
{
GtkWidget *widget = GTK_WIDGET (window);
GtkWindowPrivate *priv = window->priv;
GtkAllocation allocation;
gint grip_width;
gint grip_height;
@@ -5920,10 +6040,12 @@ gtk_window_get_resize_grip_area (GtkWindow *window,
rect->width = grip_width;
rect->height = grip_height;
rect->y = allocation.y + allocation.height - grip_height;
rect->y = allocation.y + allocation.height -
grip_height - priv->window_border.bottom;
if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
rect->x = allocation.x + allocation.width - grip_width;
rect->x = allocation.x + allocation.width -
grip_width - priv->window_border.right;
else
rect->x = allocation.x;
@@ -6074,6 +6196,12 @@ gtk_window_button_press_event (GtkWidget *widget,
{
GtkWindowPrivate *priv = GTK_WINDOW (widget)->priv;
GdkWindowEdge edge;
GtkAllocation allocation;
int border_width;
gtk_widget_get_allocation (priv->title_box, &allocation);
border_width =
gtk_container_get_border_width (GTK_CONTAINER (priv->title_box));
if (event->window == priv->grip_window)
{
@@ -6088,6 +6216,20 @@ gtk_window_button_press_event (GtkWidget *widget,
return TRUE;
}
else if (allocation.x - border_width <= event->x &&
event->x < allocation.x + border_width + allocation.width &&
allocation.y - border_width <= event->y &&
event->y < allocation.y + border_width + allocation.height)
{
gdk_window_begin_move_drag_for_device (gtk_widget_get_window(widget),
gdk_event_get_device((GdkEvent *) event),
event->button,
event->x_root,
event->y_root,
event->time);
return TRUE;
}
return FALSE;
}
@@ -6253,6 +6395,49 @@ gtk_window_check_resize (GtkContainer *container)
gtk_window_move_resize (GTK_WINDOW (container));
}
static void
gtk_window_forall (GtkContainer *container,
gboolean include_internals,
GtkCallback callback,
gpointer callback_data)
{
GtkWindow *window = GTK_WINDOW (container);
GtkWindowPrivate *priv = window->priv;
GtkWidget *child;
child = gtk_bin_get_child (GTK_BIN (container));
if (child)
(* callback) (child, callback_data);
if (priv->title_box)
(* callback) (priv->title_box, callback_data);
}
static void
gtk_window_remove (GtkContainer *container,
GtkWidget *child)
{
GtkWindow *window = GTK_WINDOW (container);
GtkWindowPrivate *priv = window->priv;
gboolean widget_was_visible;
/* Modified from gtk_bin_remove() to work with the decoration widgets. */
widget_was_visible = gtk_widget_get_visible (child);
gtk_widget_unparent (child);
if (gtk_bin_get_child (GTK_BIN (container)) == child)
_gtk_bin_set_child (GTK_BIN (container), NULL);
else if (priv->title_box == child)
priv->title_box = NULL;
/* queue resize regardless of gtk_widget_get_visible (container),
* since that's what is needed by toplevels, which derive from GtkBin.
*/
if (widget_was_visible)
gtk_widget_queue_resize (GTK_WIDGET (container));
}
static gboolean
gtk_window_focus (GtkWidget *widget,
GtkDirectionType direction)
@@ -6430,6 +6615,122 @@ gtk_window_real_set_focus (GtkWindow *window,
}
}
static void
gtk_window_get_preferred_width (GtkWidget *widget,
gint *minimum_size,
gint *natural_size)
{
GtkWindow *window;
GtkWidget *child;
GtkWindowPrivate *priv;
GtkStyleContext *context;
GtkStateFlags state;
guint border_width;
gint title_min = 0, title_nat = 0;
gint child_min = 0, child_nat = 0;
window = GTK_WINDOW (widget);
priv = window->priv;
child = gtk_bin_get_child (GTK_BIN (window));
border_width =
2 * gtk_container_get_border_width (GTK_CONTAINER (window));
if (priv->title_box)
{
gtk_widget_get_preferred_width (priv->title_box,
&title_min, &title_nat);
context = gtk_widget_get_style_context (widget);
state = gtk_style_context_get_state (context);
gtk_style_context_save (context);
gtk_style_context_add_class (context, "titlebar");
gtk_style_context_get_border (context, state, &priv->title_border);
gtk_style_context_restore (context);
gtk_style_context_save (context);
gtk_style_context_add_class (context, "window-border");
gtk_style_context_get_border (context, state, &priv->window_border);
gtk_style_context_restore (context);
border_width +=
priv->title_border.left + priv->title_border.right +
priv->window_border.left + priv->window_border.right;
title_min += border_width;
title_nat += border_width;
}
if (child && gtk_widget_get_visible (child))
{
gtk_widget_get_preferred_width (child, &child_min, &child_nat);
child_min += border_width;
child_nat += border_width;
}
*minimum_size = MAX (title_min, child_min);
*natural_size = MAX (title_nat, child_nat);
}
static void
gtk_window_get_preferred_height (GtkWidget *widget,
gint *minimum_size,
gint *natural_size)
{
GtkWindow *window;
GtkWindowPrivate *priv;
GtkWidget *child;
GtkStyleContext *context;
GtkStateFlags state;
guint border_width;
int title_min;
window = GTK_WINDOW (widget);
priv = window->priv;
child = gtk_bin_get_child (GTK_BIN (window));
*minimum_size = 0;
*natural_size = 0;
if (priv->title_box)
{
gtk_widget_get_preferred_height (priv->title_box,
&title_min, &priv->title_height);
context = gtk_widget_get_style_context (widget);
state = gtk_style_context_get_state (context);
gtk_style_context_save (context);
gtk_style_context_add_class (context, "titlebar");
gtk_style_context_get_border (context, state, &priv->title_border);
gtk_style_context_restore (context);
gtk_style_context_save (context);
gtk_style_context_add_class (context, "window-border");
gtk_style_context_get_border (context, state, &priv->window_border);
gtk_style_context_restore (context);
border_width =
2 * gtk_container_get_border_width (GTK_CONTAINER (window)) +
priv->title_border.top + priv->title_border.bottom +
priv->window_border.top + priv->window_border.bottom;
*minimum_size = border_width + title_min;
*natural_size = border_width + priv->title_height;
}
if (child && gtk_widget_get_visible (child))
{
gint child_min, child_nat;
gtk_widget_get_preferred_height (child, &child_min, &child_nat);
*minimum_size += child_min;
*natural_size += child_nat;
}
}
/**
* _gtk_window_unset_focus_and_default:
* @window: a #GtkWindow
@@ -7562,15 +7863,63 @@ gtk_window_draw (GtkWidget *widget,
GtkWindowPrivate *priv = GTK_WINDOW (widget)->priv;
GtkStyleContext *context;
gboolean ret = FALSE;
GtkAllocation allocation;
context = gtk_widget_get_style_context (widget);
if (!gtk_widget_get_app_paintable (widget) &&
gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
{
gtk_render_background (context, cr, 0, 0,
gtk_widget_get_allocated_width (widget),
gtk_widget_get_allocated_height (widget));
gtk_style_context_save (context);
gtk_style_context_add_class (context, GTK_STYLE_CLASS_BACKGROUND);
if (priv->title_box)
{
gtk_style_context_add_class (context, "window-border");
gtk_widget_get_allocation (widget, &allocation);
gtk_render_background (context, cr,
priv->window_border.left,
priv->window_border.top,
allocation.width -
priv->window_border.left -
priv->window_border.right,
allocation.height -
priv->window_border.top -
priv->window_border.bottom);
gtk_render_frame (context, cr,
0, 0, allocation.width, allocation.height);
}
else
{
gtk_widget_get_allocation (widget, &allocation);
gtk_render_background (context, cr,
0, 0, allocation.width, allocation.height);
}
gtk_style_context_restore (context);
}
if (priv->title_box)
{
gtk_style_context_save (context);
gtk_style_context_add_class (context, "titlebar");
gtk_widget_get_allocation (priv->title_box, &allocation);
gtk_render_background (context, cr,
allocation.x - priv->title_border.left,
allocation.y - priv->title_border.top,
allocation.width + priv->title_border.left + priv->title_border.right,
allocation.height + priv->title_border.top + priv->title_border.left);
gtk_render_frame (context, cr,
allocation.x - priv->title_border.left,
allocation.y - priv->title_border.top,
allocation.width + priv->title_border.left + priv->title_border.right,
allocation.height + priv->title_border.top + priv->title_border.left);
gtk_style_context_restore (context);
}
if (GTK_WIDGET_CLASS (gtk_window_parent_class)->draw)

BIN
theme.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB