add more spacing
[personal-kdebase.git] / workspace / kwin / effects / test / howto.cpp
bloba4150f993a1e4c1d95ee85aaaf9c4abdc479bd77
1 /*****************************************************************
2 KWin - the KDE window manager
3 This file is part of the KDE project.
5 Copyright (C) 2006 Lubos Lunak <l.lunak@kde.org>
7 You can Freely distribute this program under the GNU General Public
8 License. See the file "COPYING" for the exact licensing terms.
9 ******************************************************************/
13 Files howto.cpp and howto.h implement HowtoEffect, a commented demo compositing
14 effect that fades out and again in a window after it has been activated.
16 Please see file howto.h first.
20 // Include the class definition.
21 #include "howto.h"
23 namespace KWin
26 // This macro creates entry function for the plugin. First argument is name, second is class name.
27 KWIN_EFFECT( howto, HowtoEffect )
29 // A pre-paint function that tells the compositing code how this effect will affect
30 // the painting. During every painting pass this function is called first, before
31 // the actual painting function.
32 // Arguments:
33 // w - the window that will be painted
34 // mask - a mask of flags controlling the painting, setting or resetting flags changes
35 // how the painting will be done
36 // region - the region of the screen that needs to be painted, support for modifying it
37 // is not fully implemented yet, do not use
38 // time - time in milliseconds since the last paint, useful for animations
39 void HowtoEffect::prePaintWindow( EffectWindow* w, WindowPrePaintData& data, int time )
41 // Is this window the one that is going to be faded out and in again?
42 if( w == fade_window )
44 // Simply add the time to the total progress. The value of progress will be used
45 // to determine how far in effect is.
46 progress += time;
47 // If progress is < 1000 (milliseconds), the effect is still in progress.
48 if( progress < 1000 ) // complete change in 1000ms
50 // Since the effect will make the window translucent, explicitly change
51 // the flags so that the window will be painted only as translucent.
52 // Use a helper that also takes care of changing the clipping rectangle.
53 data.setTranslucent();
55 else
57 // If progress has reached 1000 (milliseconds), it means the effect is done
58 // and there is no window to fade anymore.
59 fade_window = NULL;
62 // Call the next effect (or the actual window painting code if this is the last effect).
63 // Effects are chained and they all modify something if needed and then call the next one.
64 effects->prePaintWindow( w, data, time );
67 // The function that handles the actual painting. Some simple modifications are possible
68 // by only changing the painting data. More complicated effects would do some painting
69 // or transformations directly.
70 // Arguments:
71 // w - the window that will be painted
72 // mask - a mask of flags controlling the painting
73 // region - the region of the screen that needs to be painted, if mask includes the TRANSFORMED
74 // then special care needs to be taken, because the region may be infiniteRegion(), meaning
75 // everything needs to be painted
76 // data - painting data that can be modified to do some simple transformations
77 void HowtoEffect::paintWindow( EffectWindow* w, int mask, QRegion region, WindowPaintData& data )
79 // Is this the window to be faded out and in again?
80 if( w == fade_window )
82 // This effect, after a window has been activated, fades it out to only 50% transparency
83 // and then fades it in again to be fully opaque (assuming it's otherwise a fully opaque
84 // window, otherwise the transparencies will be added).
85 if( progress <= 500 )
87 // For the first 500 milliseconds (progress being 0 to 500), the window is faded out.
88 // progress == 0 -> opacity *= 1
89 // progress == 500 -> opacity *= 0.5
90 // Note that the division is floating-point division to avoid integer rounding down.
91 // Note that data.opacity is not set but multiplied - this allows chaining of effects,
92 // for example if another effect always changes opacity of some window types.
93 data.opacity *= 1 - 0.5 * ( progress / 500.0 );
95 else
97 // For the second 500 milliseconds (progress being 500 to 1000), the window is
98 // faded in again.
99 // progress == 500 -> opacity *= 0.5
100 // progress == 1000 -> opacity *= 1
101 data.opacity *= 0.5 + 0.5 * ( progress - 500 ) / 500.0;
104 // Call the next effect.
105 effects->paintWindow( w, mask, region, data );
108 // The function that is called after the painting pass is finished. When an animation is going on,
109 // it can add repaints of some areas so that the next painting pass has to repaint them again.
110 void HowtoEffect::postPaintWindow( EffectWindow* w )
112 // Is this the window to be faded out and in again?
113 if( w == fade_window )
115 // Trigger repaint of the whole window, this will cause it to be repainted the next painting pass.
116 w->addRepaintFull(); // trigger next animation repaint
118 // Call the next effect.
119 effects->postPaintWindow( w );
122 // This function is called when a new window becomes active.
123 void HowtoEffect::windowActivated( EffectWindow* c )
125 // Set the window to be faded (or NULL if no window is active).
126 fade_window = c;
127 if( fade_window != NULL )
129 // If there is a window to be faded, reset the progress to zero.
130 progress = 0;
131 // And add repaint to the window so that it needs to be repainted.
132 c->addRepaintFull();
136 // This function is called when a window is closed.
137 void HowtoEffect::windowClosed( EffectWindow* c )
139 // If the window to be faded out and in is closed, just reset the pointer.
140 // This effect then will do nothing and just call the next effect.
141 if( fade_window == c )
142 fade_window = NULL;
145 // That's all. Now only the matching .desktop file is needed.
148 } // namespace