summaryrefslogtreecommitdiff
path: root/src/effects.c
blob: b5d7150bcb7f610cabd57adb422e99fe263c84b4 (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
/* Metacity animation effects */

/* 
 * Copyright (C) 2001 Anders Carlsson, Havoc Pennington
 * 
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#include "effects.h"
#include "display.h"

typedef struct
{
  MetaScreen *screen;
  
  GC gc;

  int step;
  int steps;
  
  double current_x, current_y;
  double current_width, current_height;
  
  double delta_x, delta_y;
  double delta_width, delta_height;
} BoxAnimationContext;

static gboolean
effects_draw_box_animation_timeout (BoxAnimationContext *context)
{
  if (context->step == 0)
    {
      /* It's our first time, grab the X server */
      meta_display_grab (context->screen->display);
    }
  else
    {
      /* Restore the previously drawn background */
      XDrawRectangle (context->screen->display->xdisplay,
                      context->screen->xroot,
                      context->gc,
                      context->current_x, context->current_y,
                      context->current_width, context->current_height);
    }

  /* Return if we're done */
  if (context->step == context->steps)
    {
      meta_display_ungrab (context->screen->display);
      XFreeGC (context->screen->display->xdisplay,
               context->gc);
      g_free (context);
      return FALSE;
    }

  context->current_x += context->delta_x;
  context->current_y += context->delta_y;
  context->current_width += context->delta_width;
  context->current_height += context->delta_height;
	
  /* Draw the rectangle */
  XDrawRectangle (context->screen->display->xdisplay,
                  context->screen->xroot,
                  context->gc,
                  context->current_x, context->current_y,
                  context->current_width, context->current_height);  

  context->step += 1;

  return TRUE;
}

void
meta_effects_draw_box_animation (MetaScreen *screen,
                                 MetaRectangle *initial_rect,
                                 MetaRectangle *destination_rect,
                                 int steps,
                                 int delay)
{
  BoxAnimationContext *context;
  XGCValues gc_values;

  /* Create the animation context */
  context = g_new (BoxAnimationContext, 1);
	
  gc_values.subwindow_mode = IncludeInferiors;
  gc_values.function = GXinvert;
	
  /* Create a gc for the root window */
  context->screen = screen;
  context->gc = XCreateGC (screen->display->xdisplay,
                           screen->xroot,
                           GCSubwindowMode | GCFunction,
                           &gc_values);
  context->step = 0;
  context->steps = steps;
  context->delta_x = (destination_rect->x - initial_rect->x) / (double)steps;
  context->delta_y = (destination_rect->y - initial_rect->y) / (double)steps;
  context->delta_width = (destination_rect->width - initial_rect->width) / (double)steps;
  context->delta_height = (destination_rect->height - initial_rect->height) / (double)steps;
  
  context->current_x = initial_rect->x;
  context->current_y = initial_rect->y;
  context->current_width = initial_rect->width;
  context->current_height = initial_rect->height;
  
  /* Add the timeout */
  g_timeout_add (delay,
                 (GSourceFunc)effects_draw_box_animation_timeout,
                 context);
}