gtkoffscreenwindow.c 9.78 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
13
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Cody Russell's avatar
Cody Russell committed
14 15 16 17 18
 *
 * Authors: Cody Russell <crussell@canonical.com>
 *          Alexander Larsson <alexl@redhat.com>
 */

19 20
#include "config.h"

21
#include "gtkoffscreenwindow.h"
Emmanuele Bassi's avatar
Emmanuele Bassi committed
22
#include "gtkwidgetprivate.h"
23
#include "gtkcontainerprivate.h"
24
#include "gtkprivate.h"
25

26
/**
27
 * SECTION:gtkoffscreenwindow
28
 * @short_description: A toplevel to manage offscreen rendering of child widgets
29
 * @title: GtkOffscreenWindow
30
 *
31
 * GtkOffscreenWindow is strictly intended to be used for obtaining
32
 * snapshots of widgets that are not part of a normal widget hierarchy.
33 34 35
 * Since #GtkOffscreenWindow is a toplevel widget you cannot obtain
 * snapshots of a full window with it since you cannot pack a toplevel
 * widget in another toplevel.
36
 *
Cody Russell's avatar
Cody Russell committed
37
 * The idea is to take a widget and manually set the state of it,
38
 * add it to a GtkOffscreenWindow and then retrieve the snapshot
39
 * as a #cairo_surface_t or #GdkPixbuf.
40
 *
41
 * GtkOffscreenWindow derives from #GtkWindow only as an implementation
42 43 44
 * detail.  Applications should not use any API specific to #GtkWindow
 * to operate on this object.  It should be treated as a #GtkBin that
 * has no parent widget.
45
 *
46
 * When contained offscreen widgets are redrawn, GtkOffscreenWindow
47
 * will emit a #GtkWidget::damage-event signal.
48 49
 */

50 51 52
G_DEFINE_TYPE (GtkOffscreenWindow, gtk_offscreen_window, GTK_TYPE_WINDOW);

static void
53 54 55
gtk_offscreen_window_get_preferred_width (GtkWidget *widget,
					  gint      *minimum,
					  gint      *natural)
56 57
{
  GtkBin *bin = GTK_BIN (widget);
Javier Jardón's avatar
Javier Jardón committed
58
  GtkWidget *child;
59
  gint border_width;
60
  gint default_width;
61 62 63

  border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));

64 65
  *minimum = border_width * 2;
  *natural = border_width * 2;
66

Javier Jardón's avatar
Javier Jardón committed
67
  child = gtk_bin_get_child (bin);
68 69

  if (child != NULL && gtk_widget_get_visible (child))
70
    {
71
      gint child_min, child_nat;
72

73
      gtk_widget_get_preferred_width (child, &child_min, &child_nat);
74

75 76
      *minimum += child_min;
      *natural += child_nat;
77 78 79
    }

  gtk_window_get_default_size (GTK_WINDOW (widget),
80
                               &default_width, NULL);
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
  *minimum = MAX (*minimum, default_width);
  *natural = MAX (*natural, default_width);
}

static void
gtk_offscreen_window_get_preferred_height (GtkWidget *widget,
					   gint      *minimum,
					   gint      *natural)
{
  GtkBin *bin = GTK_BIN (widget);
  GtkWidget *child;
  gint border_width;
  gint default_height;

  border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));

  *minimum = border_width * 2;
  *natural = border_width * 2;

  child = gtk_bin_get_child (bin);

  if (child != NULL && gtk_widget_get_visible (child))
    {
      gint child_min, child_nat;

      gtk_widget_get_preferred_height (child, &child_min, &child_nat);

      *minimum += child_min;
      *natural += child_nat;
    }

  gtk_window_get_default_size (GTK_WINDOW (widget),
                               NULL, &default_height);

  *minimum = MAX (*minimum, default_height);
  *natural = MAX (*natural, default_height);
118 119 120 121 122 123 124
}

static void
gtk_offscreen_window_size_allocate (GtkWidget *widget,
                                    GtkAllocation *allocation)
{
  GtkBin *bin = GTK_BIN (widget);
Javier Jardón's avatar
Javier Jardón committed
125
  GtkWidget *child;
126 127
  gint border_width;

128
  gtk_widget_set_allocation (widget, allocation);
129 130 131

  border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));

132
  if (gtk_widget_get_realized (widget))
133
    gdk_window_move_resize (gtk_widget_get_window (widget),
134 135 136 137 138
                            allocation->x,
                            allocation->y,
                            allocation->width,
                            allocation->height);

Javier Jardón's avatar
Javier Jardón committed
139
  child = gtk_bin_get_child (bin);
140 141

  if (child != NULL && gtk_widget_get_visible (child))
142 143 144 145 146 147 148 149
    {
      GtkAllocation  child_alloc;

      child_alloc.x = border_width;
      child_alloc.y = border_width;
      child_alloc.width = allocation->width - 2 * border_width;
      child_alloc.height = allocation->height - 2 * border_width;

Javier Jardón's avatar
Javier Jardón committed
150
      gtk_widget_size_allocate (child, &child_alloc);
151 152 153 154 155 156 157 158
    }

  gtk_widget_queue_draw (widget);
}

static void
gtk_offscreen_window_realize (GtkWidget *widget)
{
159
  GtkAllocation allocation;
160
  GtkBin *bin;
Javier Jardón's avatar
Javier Jardón committed
161
  GtkWidget *child;
162
  GdkWindow *window;
163 164 165 166 167
  GdkWindowAttr attributes;
  gint attributes_mask;

  bin = GTK_BIN (widget);

168
  gtk_widget_set_realized (widget, TRUE);
169

170 171 172 173 174 175
  gtk_widget_get_allocation (widget, &allocation);

  attributes.x = allocation.x;
  attributes.y = allocation.y;
  attributes.width = allocation.width;
  attributes.height = allocation.height;
176 177 178 179 180
  attributes.window_type = GDK_WINDOW_OFFSCREEN;
  attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.wclass = GDK_INPUT_OUTPUT;

181
  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
182

183 184 185
  window = gdk_window_new (gtk_widget_get_parent_window (widget),
                           &attributes, attributes_mask);
  gtk_widget_set_window (widget, window);
186
  gtk_widget_register_window (widget, window);
187

Javier Jardón's avatar
Javier Jardón committed
188 189
  child = gtk_bin_get_child (bin);
  if (child)
190
    gtk_widget_set_parent_window (child, window);
191 192 193 194 195 196 197 198
}

static void
gtk_offscreen_window_resize (GtkWidget *widget)
{
  GtkAllocation allocation = { 0, 0 };
  GtkRequisition requisition;

199
  gtk_widget_get_preferred_size (widget, &requisition, NULL);
200 201 202 203 204 205 206 207 208 209 210 211

  allocation.width  = requisition.width;
  allocation.height = requisition.height;
  gtk_widget_size_allocate (widget, &allocation);
}

static void
move_focus (GtkWidget       *widget,
            GtkDirectionType dir)
{
  gtk_widget_child_focus (widget, dir);

212
  if (!gtk_container_get_focus_child (GTK_CONTAINER (widget)))
213 214 215 216 217 218 219 220
    gtk_window_set_focus (GTK_WINDOW (widget), NULL);
}

static void
gtk_offscreen_window_show (GtkWidget *widget)
{
  gboolean need_resize;

221
  _gtk_widget_set_visible_flag (widget, TRUE);
222

223
  need_resize = _gtk_widget_get_alloc_needed (widget) || !gtk_widget_get_realized (widget);
224 225 226 227 228 229 230 231 232 233 234 235 236 237

  if (need_resize)
    gtk_offscreen_window_resize (widget);

  gtk_widget_map (widget);

  /* Try to make sure that we have some focused widget */
  if (!gtk_window_get_focus (GTK_WINDOW (widget)))
    move_focus (widget, GTK_DIR_TAB_FORWARD);
}

static void
gtk_offscreen_window_hide (GtkWidget *widget)
{
238
  _gtk_widget_set_visible_flag (widget, FALSE);
239 240 241 242 243 244 245 246
  gtk_widget_unmap (widget);
}

static void
gtk_offscreen_window_check_resize (GtkContainer *container)
{
  GtkWidget *widget = GTK_WIDGET (container);

247
  if (gtk_widget_get_visible (widget))
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
    gtk_offscreen_window_resize (widget);
}

static void
gtk_offscreen_window_class_init (GtkOffscreenWindowClass *class)
{
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;

  widget_class = GTK_WIDGET_CLASS (class);
  container_class = GTK_CONTAINER_CLASS (class);

  widget_class->realize = gtk_offscreen_window_realize;
  widget_class->show = gtk_offscreen_window_show;
  widget_class->hide = gtk_offscreen_window_hide;
263 264
  widget_class->get_preferred_width = gtk_offscreen_window_get_preferred_width;
  widget_class->get_preferred_height = gtk_offscreen_window_get_preferred_height;
265 266 267 268 269 270
  widget_class->size_allocate = gtk_offscreen_window_size_allocate;

  container_class->check_resize = gtk_offscreen_window_check_resize;
}

static void
Cody Russell's avatar
Cody Russell committed
271
gtk_offscreen_window_init (GtkOffscreenWindow *window)
272 273 274
{
}

275
/* --- functions --- */
276 277 278 279
/**
 * gtk_offscreen_window_new:
 *
 * Creates a toplevel container widget that is used to retrieve
280
 * snapshots of widgets without showing them on the screen.
281
 *
282
 * Returns: A pointer to a #GtkWidget
283 284
 *
 * Since: 2.20
285
 */
286 287 288 289 290 291
GtkWidget *
gtk_offscreen_window_new (void)
{
  return g_object_new (gtk_offscreen_window_get_type (), NULL);
}

292
/**
293
 * gtk_offscreen_window_get_surface:
294
 * @offscreen: the #GtkOffscreenWindow contained widget.
295 296
 *
 * Retrieves a snapshot of the contained widget in the form of
297
 * a #cairo_surface_t.  If you need to keep this around over window
298 299
 * resizes then you should add a reference to it.
 *
300
 * Returns: (nullable) (transfer none): A #cairo_surface_t pointer to the offscreen
301
 *     surface, or %NULL.
302 303
 *
 * Since: 2.20
304
 */
305 306
cairo_surface_t *
gtk_offscreen_window_get_surface (GtkOffscreenWindow *offscreen)
307 308 309
{
  g_return_val_if_fail (GTK_IS_OFFSCREEN_WINDOW (offscreen), NULL);

310
  return gdk_offscreen_window_get_surface (gtk_widget_get_window (GTK_WIDGET (offscreen)));
311 312 313 314
}

/**
 * gtk_offscreen_window_get_pixbuf:
315
 * @offscreen: the #GtkOffscreenWindow contained widget.
316 317 318 319 320 321
 *
 * Retrieves a snapshot of the contained widget in the form of
 * a #GdkPixbuf.  This is a new pixbuf with a reference count of 1,
 * and the application should unreference it once it is no longer
 * needed.
 *
322
 * Returns: (nullable) (transfer full): A #GdkPixbuf pointer, or %NULL.
323 324
 *
 * Since: 2.20
325
 */
326 327 328
GdkPixbuf *
gtk_offscreen_window_get_pixbuf (GtkOffscreenWindow *offscreen)
{
329
  cairo_surface_t *surface;
330
  GdkPixbuf *pixbuf = NULL;
331
  GdkWindow *window;
332 333 334

  g_return_val_if_fail (GTK_IS_OFFSCREEN_WINDOW (offscreen), NULL);

335 336
  window = gtk_widget_get_window (GTK_WIDGET (offscreen));
  surface = gdk_offscreen_window_get_surface (window);
337

338
  if (surface != NULL)
339
    {
340 341
      pixbuf = gdk_pixbuf_get_from_surface (surface,
                                            0, 0,
342 343
                                            gdk_window_get_width (window),
                                            gdk_window_get_height (window));
344 345 346 347
    }

  return pixbuf;
}