~shnatsel/+junk/cairo-compmgr

« back to all changes in this revision

Viewing changes to plugins/vala-test/ccm-vala-window-plugin.vala

  • Committer: Sergey "Shnatsel" Davidoff
  • Date: 2012-03-04 22:53:22 UTC
  • Revision ID: shnatsel@gmail.com-20120304225322-q2hz82j51yxv1qqw
fixed up build dependencies

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4 -*- */
 
2
/*
 
3
 * cairo-compmgr
 
4
 * Copyright (C) Nicolas Bruguier 2007-2010 <gandalfn@club-internet.fr>
 
5
 * 
 
6
 * cairo-compmgr is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Lesser General Public
 
8
 * License as published by the Free Software Foundation; either
 
9
 * version 2.1 of the License, or (at your option) any later version.
 
10
 * 
 
11
 * cairo-compmgr is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * Lesser General Public License for more details.
 
15
 * 
 
16
 * You should have received a copy of the GNU Lesser General Public
 
17
 * License along with cairo-compmgr.  If not, write to:
 
18
 *      The Free Software Foundation, Inc.,
 
19
 *      51 Franklin Street, Fifth Floor
 
20
 *      Boston, MA  02110-1301, USA.
 
21
 */
 
22
 
 
23
using GLib;
 
24
using Cairo;
 
25
using CCM;
 
26
 
 
27
namespace CCM
 
28
{
 
29
    enum Options
 
30
    {
 
31
        ENABLED,
 
32
        N
 
33
    }
 
34
 
 
35
    class ValaWindowOptions : PluginOptions
 
36
    {
 
37
        public bool enabled = false;
 
38
 
 
39
        override void
 
40
        changed(CCM.Config config)
 
41
        {
 
42
            try
 
43
            {
 
44
                enabled = config.get_boolean ();
 
45
            }
 
46
            catch (GLib.Error err)
 
47
            {
 
48
                CCM.log("%s", err);
 
49
            }
 
50
        }
 
51
    }
 
52
 
 
53
    private class ValaWindowPlugin : CCM.Plugin, CCM.WindowPlugin
 
54
    {
 
55
        const string[] options_key = {
 
56
            "enabled"
 
57
        };
 
58
 
 
59
        weak CCM.Window window;
 
60
 
 
61
        uint counter = 0;
 
62
 
 
63
        class construct
 
64
        {
 
65
            type_options = typeof (ValaWindowOptions);
 
66
        }
 
67
 
 
68
        ~ValaWindowPlugin ()
 
69
        {
 
70
            options_unload ();
 
71
        }
 
72
 
 
73
        void
 
74
        option_changed (int index)
 
75
        {
 
76
            if (!((ValaWindowOptions) get_option ()).enabled)
 
77
                window.get_screen ().damage_all ();
 
78
        }
 
79
 
 
80
        /**
 
81
         * Implement load_options window plugin interface
 
82
         **/
 
83
        void
 
84
        window_load_options (CCM.Window window)
 
85
        {
 
86
            this.window = window;
 
87
 
 
88
            options_load ("vala-window-plugin", options_key, 
 
89
                          (PluginOptionsChangedFunc)option_changed);
 
90
 
 
91
            /* Chain call to next plugin */
 
92
            ((CCM.WindowPlugin) parent).window_load_options (window);
 
93
        }
 
94
 
 
95
        /**
 
96
         * Implement paint window plugin interface
 
97
         **/
 
98
        bool 
 
99
        window_paint (CCM.Window window, Cairo.Context ctx,
 
100
                      Cairo.Surface surface, bool y_invert)
 
101
        {
 
102
            bool ret = false;
 
103
 
 
104
            /* Chain call to next plugin */
 
105
            ret = ((CCM.WindowPlugin) parent).window_paint (window, ctx, 
 
106
                                                            surface, y_invert);
 
107
 
 
108
            /* Paint damaged area */
 
109
            if (((ValaWindowOptions) get_option ()).enabled)
 
110
            {
 
111
                weak CCM.Region damaged = window.damaged;
 
112
 
 
113
                if (damaged != null)
 
114
                {
 
115
                    unowned Cairo.Rectangle[] rectangles;
 
116
 
 
117
                    damaged.get_rectangles (out rectangles);
 
118
 
 
119
                    switch (counter)
 
120
                    {
 
121
                        case 0:
 
122
                            ctx.set_source_rgba (1, 0, 0, 0.5);
 
123
                            break;
 
124
                        case 1:
 
125
                            ctx.set_source_rgba (0, 1, 0, 0.5);
 
126
                            break;
 
127
                        case 2:
 
128
                            ctx.set_source_rgba (0, 0, 1, 0.5);
 
129
                            break;
 
130
                        default:
 
131
                            break;
 
132
                    }
 
133
                    if (++counter > 2)
 
134
                        counter = 0;
 
135
 
 
136
                    foreach (Cairo.Rectangle rectangle in rectangles)
 
137
                    {
 
138
                        ctx.rectangle (rectangle.x, rectangle.y,
 
139
                                       rectangle.width, rectangle.height);
 
140
                        ctx.fill ();
 
141
                    }
 
142
                    rectangles_free (rectangles);
 
143
                }
 
144
            }
 
145
 
 
146
            return ret;
 
147
        }
 
148
    }
 
149
}
 
150
 
 
151
/**
 
152
 * Init plugin
 
153
 **/
 
154
[ModuleInit]
 
155
public Type
 
156
ccm_vala_window_plugin_get_plugin_type (TypeModule module)
 
157
{
 
158
    return typeof (ValaWindowPlugin);
 
159
}