~cairo-dock-team/ubuntu/oneiric/cairo-dock/2.3.0-3

« back to all changes in this revision

Viewing changes to src/gldit/cairo-dock-emblem.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthieu Baerts (matttbe)
  • Date: 2010-08-09 23:26:12 UTC
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: james.westby@ubuntu.com-20100809232612-pocdxliaxjdetm37
Tags: upstream-2.2.0~0beta4
ImportĀ upstreamĀ versionĀ 2.2.0~0beta4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
* This file is a part of the Cairo-Dock project
 
3
*
 
4
* Copyright : (C) see the 'copyright' file.
 
5
* E-mail    : see the 'copyright' file.
 
6
*
 
7
* This program is free software; you can redistribute it and/or
 
8
* modify it under the terms of the GNU General Public License
 
9
* as published by the Free Software Foundation; either version 3
 
10
* of the License, or (at your option) any later version.
 
11
*
 
12
* This program is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
* GNU General Public License for more details.
 
16
* You should have received a copy of the GNU General Public License
 
17
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
*/
 
19
 
 
20
#include <math.h>
 
21
#include <string.h>
 
22
#include <stdlib.h>
 
23
#include <cairo.h>
 
24
 
 
25
#include "cairo-dock-icons.h"
 
26
#include "cairo-dock-draw.h"
 
27
#include "cairo-dock-draw-opengl.h"
 
28
#include "cairo-dock-backends-manager.h"
 
29
#include "cairo-dock-load.h"
 
30
#include "cairo-dock-log.h"
 
31
#include "cairo-dock-emblem.h"
 
32
 
 
33
extern CairoDockImageBuffer g_pIconBackgroundBuffer;
 
34
extern gboolean g_bUseOpenGL;
 
35
 
 
36
static double f = .5;
 
37
 
 
38
//merci a Necropotame et ChAnGFu !
 
39
 
 
40
CairoEmblem *cairo_dock_make_emblem (const gchar *cImageFile, Icon *pIcon, CairoContainer *pContainer)
 
41
{
 
42
        CairoEmblem *pEmblem = g_new0 (CairoEmblem, 1);
 
43
        pEmblem->fScale = f;
 
44
        
 
45
        //\___________ On calcule les dimensions de l'embleme.
 
46
        int w, h;
 
47
        cairo_dock_get_icon_extent (pIcon, pContainer, &w, &h);
 
48
        pEmblem->iWidth = f * w;
 
49
        pEmblem->iHeight = f * h;
 
50
        
 
51
        //\___________ On cree la surface/texture a cette taille.
 
52
        cairo_surface_t *pEmblemSurface = cairo_dock_create_surface_from_image_simple (cImageFile, pEmblem->iWidth, pEmblem->iHeight);
 
53
        
 
54
        if (g_bUseOpenGL && pEmblemSurface)
 
55
        {
 
56
                pEmblem->iTexture = cairo_dock_create_texture_from_surface (pEmblemSurface);
 
57
                cairo_surface_destroy (pEmblemSurface);
 
58
        }
 
59
        else
 
60
                pEmblem->pSurface = pEmblemSurface;
 
61
        
 
62
        return pEmblem;
 
63
}
 
64
 
 
65
CairoEmblem *cairo_dock_make_emblem_from_surface (cairo_surface_t *pSurface, int iSurfaceWidth, int iSurfaceHeight, Icon *pIcon, CairoContainer *pContainer)
 
66
{
 
67
        CairoEmblem *pEmblem = g_new0 (CairoEmblem, 1);
 
68
        pEmblem->fScale = f;
 
69
        
 
70
        int w, h;
 
71
        cairo_dock_get_icon_extent (pIcon, pContainer, &w, &h);
 
72
        pEmblem->iWidth = (iSurfaceWidth > 0 ? iSurfaceWidth : w);
 
73
        pEmblem->iHeight = (iSurfaceHeight > 0 ? iSurfaceHeight : h);
 
74
        pEmblem->pSurface = pSurface;
 
75
        return pEmblem;
 
76
}
 
77
 
 
78
CairoEmblem *cairo_dock_make_emblem_from_texture (GLuint iTexture, Icon *pIcon, CairoContainer *pContainer)
 
79
{
 
80
        CairoEmblem *pEmblem = g_new0 (CairoEmblem, 1);
 
81
        pEmblem->fScale = f;
 
82
        
 
83
        pEmblem->iTexture = iTexture;  // inutile de connaitre la taille de la texture.
 
84
        return pEmblem;
 
85
}
 
86
 
 
87
 
 
88
void cairo_dock_free_emblem (CairoEmblem *pEmblem)
 
89
{
 
90
        if (pEmblem == NULL)
 
91
                return;
 
92
        
 
93
        if (pEmblem->pSurface != NULL)
 
94
                cairo_surface_destroy (pEmblem->pSurface);
 
95
        if (pEmblem->iTexture != 0)
 
96
                _cairo_dock_delete_texture (pEmblem->iTexture);
 
97
        g_free (pEmblem);
 
98
}
 
99
 
 
100
 
 
101
void _cairo_dock_apply_emblem_texture (CairoEmblem *pEmblem, int w, int h)
 
102
{
 
103
        double a = pEmblem->fScale;
 
104
        double x, y;
 
105
        switch (pEmblem->iPosition)
 
106
        {
 
107
                case CAIRO_DOCK_EMBLEM_UPPER_RIGHT:
 
108
                        x = w/2 * (1 - a);
 
109
                        y = h/2 * (1 - a);
 
110
                break;
 
111
                case CAIRO_DOCK_EMBLEM_LOWER_RIGHT:
 
112
                        x = w/2 * (1 - a);
 
113
                        y = -h/2 * (1 - a);
 
114
                break;
 
115
                case CAIRO_DOCK_EMBLEM_UPPER_LEFT:
 
116
                        x = -(double)w/2 * (1 - a);
 
117
                        y = (double)h/2 * (1 - a);
 
118
                break;
 
119
                case CAIRO_DOCK_EMBLEM_LOWER_LEFT:
 
120
                default:
 
121
                        x = -w/2 * (1 - a);
 
122
                        y = -h/2 * (1 - a);
 
123
                break;
 
124
                case CAIRO_DOCK_EMBLEM_MIDDLE:
 
125
                        x = 0.;
 
126
                        y = 0.;
 
127
                break;
 
128
        }
 
129
        glBindTexture (GL_TEXTURE_2D, pEmblem->iTexture);
 
130
        _cairo_dock_apply_current_texture_at_size_with_offset (a*w, a*h, x, y);
 
131
}
 
132
 
 
133
void _cairo_dock_apply_emblem_surface (CairoEmblem *pEmblem, int w, int h, cairo_t *pCairoContext)
 
134
{
 
135
        double a = pEmblem->fScale;
 
136
        double zx = (double) a*w / pEmblem->iWidth;
 
137
        double zy = (double) a*h / pEmblem->iHeight;
 
138
        cairo_scale (pCairoContext, zx, zy);
 
139
        
 
140
        double x, y;
 
141
        switch (pEmblem->iPosition)
 
142
        {
 
143
                case CAIRO_DOCK_EMBLEM_UPPER_RIGHT:
 
144
                        x = w * (1 - a);
 
145
                        y = 0.;
 
146
                break;
 
147
                case CAIRO_DOCK_EMBLEM_LOWER_RIGHT:
 
148
                        x = w * (1 - a);
 
149
                        y = h * (1 - a);
 
150
                break;
 
151
                case CAIRO_DOCK_EMBLEM_UPPER_LEFT:
 
152
                        x = 0.;
 
153
                        y = 0.;
 
154
                break;
 
155
                case CAIRO_DOCK_EMBLEM_LOWER_LEFT:
 
156
                default:
 
157
                        x = 0.;
 
158
                        y = h * (1 - a);
 
159
                break;
 
160
                case CAIRO_DOCK_EMBLEM_MIDDLE:
 
161
                        x = w/2 * (1 - a);
 
162
                        y = h/2 * (1 - a);
 
163
                break;
 
164
        }
 
165
        cairo_set_source_surface (pCairoContext, pEmblem->pSurface, x/zx, y/zy);
 
166
        cairo_paint (pCairoContext);
 
167
}
 
168
void cairo_dock_draw_emblem_on_icon (CairoEmblem *pEmblem, Icon *pIcon, CairoContainer *pContainer)
 
169
{
 
170
        g_return_if_fail (pEmblem != NULL);
 
171
        
 
172
        int w, h;
 
173
        cairo_dock_get_icon_extent (pIcon, pContainer, &w, &h);
 
174
        
 
175
        if (pIcon->iIconTexture != 0 && pEmblem->iTexture != 0)  // dessin opengl : on dessine sur la texture de l'icone avec le mecanisme habituel.
 
176
        {
 
177
                if (! cairo_dock_begin_draw_icon (pIcon, pContainer, 1))
 
178
                        return ;
 
179
                
 
180
                _cairo_dock_enable_texture ();
 
181
                
 
182
                _cairo_dock_set_blend_source ();
 
183
                
 
184
                _cairo_dock_apply_texture_at_size (pIcon->iIconTexture, w, h);
 
185
                
 
186
                _cairo_dock_set_blend_alpha ();
 
187
                
 
188
                _cairo_dock_apply_emblem_texture (pEmblem, w, h);
 
189
                
 
190
                _cairo_dock_disable_texture ();
 
191
                
 
192
                cairo_dock_end_draw_icon (pIcon, pContainer);
 
193
        }
 
194
        else if (pIcon->pIconBuffer != NULL && pEmblem->pSurface != NULL)
 
195
        {
 
196
                cairo_t *pCairoContext = cairo_create (pIcon->pIconBuffer);
 
197
                g_return_if_fail (cairo_status (pCairoContext) == CAIRO_STATUS_SUCCESS);
 
198
                
 
199
                _cairo_dock_apply_emblem_surface (pEmblem, w, h, pCairoContext);
 
200
                
 
201
                cairo_paint (pCairoContext);
 
202
                
 
203
                cairo_destroy (pCairoContext);
 
204
        }
 
205
}