~ubuntu-branches/ubuntu/saucy/cairo-dock-plug-ins/saucy

« back to all changes in this revision

Viewing changes to keyboard-indicator/src/applet-draw.c

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2009-08-26 21:07:39 UTC
  • Revision ID: james.westby@ubuntu.com-20090826210739-gyjuuqezrzuluao4
Tags: upstream-2.0.8.1
ImportĀ upstreamĀ versionĀ 2.0.8.1

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
/******************************************************************************
 
21
 
 
22
This file is a part of the cairo-dock program, 
 
23
released under the terms of the GNU General Public License.
 
24
 
 
25
Written by Fabrice Rey (for any bug report, please mail me to fabounet@users.berlios.de)
 
26
 
 
27
******************************************************************************/
 
28
 
 
29
#include <stdlib.h>
 
30
#include <math.h>
 
31
#include <string.h>
 
32
 
 
33
#include "applet-struct.h"
 
34
#include "applet-draw.h"
 
35
 
 
36
 
 
37
void cd_xkbd_update_icon (const gchar *cGroupName, const gchar *cShortGroupName, const gchar *cIndicatorName, gboolean bRedrawSurface)
 
38
{
 
39
        //g_print ("%s (%s;%s;%d)\n", __func__, cGroupName, cShortGroupName, bRedrawSurface);
 
40
        
 
41
        if (bRedrawSurface)
 
42
        {
 
43
                //\__________________ On sauvegarde l'ancienne surface/texture.
 
44
                if (myData.pOldSurface != NULL)
 
45
                        cairo_surface_destroy (myData.pOldSurface);
 
46
                if (myData.iOldTexture != 0)
 
47
                        _cairo_dock_delete_texture (myData.iOldTexture);
 
48
                myData.pOldSurface = myData.pCurrentSurface;
 
49
                myData.iOldTexture = myData.iCurrentTexture;
 
50
                myData.iOldTextWidth = myData.iCurrentTextWidth;
 
51
                myData.iOldTextHeight = myData.iCurrentTextHeight;
 
52
                
 
53
                //\__________________ On cree la nouvelle surface (la taille du texte peut avoir change).
 
54
                int iWidth, iHeight;
 
55
                CD_APPLET_GET_MY_ICON_EXTENT (&iWidth, &iHeight);
 
56
                double fMaxScale = cairo_dock_get_max_scale (myContainer);
 
57
                double fTextXOffset, fTextYOffset;
 
58
                myData.pCurrentSurface = cairo_dock_create_surface_from_text_full (cShortGroupName,
 
59
                        myDrawContext,
 
60
                        &myConfig.textDescription,
 
61
                        fMaxScale,
 
62
                        iWidth,
 
63
                        &myData.iCurrentTextWidth, &myData.iCurrentTextHeight, &fTextXOffset, &fTextYOffset);
 
64
                myData.iCurrentTextWidth *= fMaxScale;
 
65
                myData.iCurrentTextHeight *= fMaxScale;
 
66
                if (g_bUseOpenGL)
 
67
                {
 
68
                        myData.iCurrentTexture = cairo_dock_create_texture_from_surface (myData.pCurrentSurface);
 
69
                }
 
70
                
 
71
                //\__________________ On lance une transition entre ancienne et nouvelle surface/texture, ou on dessine direct.
 
72
                if (myConfig.iTransitionDuration != 0 && myData.pOldSurface != NULL)
 
73
                {
 
74
                        CD_APPLET_SET_TRANSITION_ON_MY_ICON (cd_xkbd_render_step_cairo,
 
75
                                cd_xkbd_render_step_opengl,
 
76
                                g_bUseOpenGL,  // bFastPace : vite si opengl, lent si cairo.
 
77
                                myConfig.iTransitionDuration,
 
78
                                TRUE);  // bRemoveWhenFinished
 
79
                }
 
80
                else
 
81
                {
 
82
                        if (CD_APPLET_MY_CONTAINER_IS_OPENGL)
 
83
                        {
 
84
                                CD_APPLET_START_DRAWING_MY_ICON_OR_RETURN ();
 
85
                                cd_xkbd_render_step_opengl (myApplet);  
 
86
                                CD_APPLET_FINISH_DRAWING_MY_ICON;
 
87
                        }
 
88
                        else
 
89
                        {
 
90
                                cairo_dock_erase_cairo_context (myDrawContext);
 
91
                                cd_xkbd_render_step_cairo (myApplet);
 
92
                                CD_APPLET_UPDATE_REFLECT_ON_MY_ICON;
 
93
                        }
 
94
                        CD_APPLET_REDRAW_MY_ICON;
 
95
                }
 
96
                
 
97
                //\__________________ On met a jour le reste.
 
98
                CD_APPLET_SET_NAME_FOR_MY_ICON (cGroupName);
 
99
                CD_APPLET_SET_QUICK_INFO_ON_MY_ICON (cIndicatorName);
 
100
        }
 
101
        else
 
102
        {
 
103
                CD_APPLET_SET_QUICK_INFO_ON_MY_ICON (cIndicatorName);
 
104
                CD_APPLET_REDRAW_MY_ICON;
 
105
        }
 
106
}
 
107
 
 
108
 
 
109
gboolean cd_xkbd_render_step_opengl (CairoDockModuleInstance *myApplet)
 
110
{
 
111
        double f = CD_APPLET_GET_TRANSITION_FRACTION ();
 
112
        //g_print ("%s (%.2f)\n", __func__, f);
 
113
        
 
114
        int iWidth, iHeight;
 
115
        CD_APPLET_GET_MY_ICON_EXTENT (&iWidth, &iHeight);
 
116
        
 
117
        cairo_dock_set_perspective_view (iWidth, iHeight);
 
118
        glScalef (1., -1., 1.);
 
119
 
 
120
        double fTheta = - 45. + f * 90.;  // -45 -> 45
 
121
        glTranslatef (0., 0., - iWidth * sqrt(2)/2 * cos (fTheta/180.*G_PI));  // pour faire tenir le cube dans la fenetre.
 
122
        glEnable (GL_DEPTH_TEST);
 
123
        _cairo_dock_enable_texture ();
 
124
        _cairo_dock_set_blend_alpha ();
 
125
        _cairo_dock_set_alpha (1.);
 
126
        
 
127
        // fond
 
128
        if (myData.iBackgroundTexture != 0)
 
129
                cairo_dock_apply_texture_at_size (myData.iBackgroundTexture, iWidth, iHeight);
 
130
        
 
131
        // image precedente.
 
132
        if (fTheta < 25 && myData.iOldTexture != 0)  // inutile de dessiner si elle est derriere l'image courante, par l'effet de perspective (en fait 22.5, mais bizarrement ca a l'air un peu trop tot).
 
133
        {
 
134
                glPushMatrix ();
 
135
                glRotatef (45. + fTheta, 0., 1., 0.);  // 0 -> 90
 
136
                glTranslatef (0., 0., (myData.iCurrentTextWidth ? myData.iCurrentTextWidth : iWidth)/2);
 
137
                cairo_dock_apply_texture_at_size (myData.iOldTexture, myData.iOldTextWidth, MIN (iHeight, myData.iOldTextHeight));
 
138
                glPopMatrix ();
 
139
        }
 
140
        
 
141
        // image courante a 90deg.
 
142
        glRotatef (45. + fTheta, 0., 1., 0.);  // 0 -> 90
 
143
        glTranslatef (- (myData.iOldTextWidth ? myData.iOldTextWidth : iWidth)/2, 0., 0.);
 
144
        
 
145
        glRotatef (-90., 0., 1., 0.);
 
146
        cairo_dock_apply_texture_at_size (myData.iCurrentTexture, myData.iCurrentTextWidth, MIN (iHeight, myData.iCurrentTextHeight));
 
147
        
 
148
        glDisable (GL_DEPTH_TEST);
 
149
        _cairo_dock_disable_texture ();
 
150
        
 
151
        if (myDock)
 
152
        {
 
153
                cairo_dock_set_ortho_view (iWidth, iHeight);
 
154
        }
 
155
        
 
156
        return TRUE;
 
157
}
 
158
 
 
159
 
 
160
gboolean cd_xkbd_render_step_cairo (CairoDockModuleInstance *myApplet)
 
161
{
 
162
        double f = CD_APPLET_GET_TRANSITION_FRACTION ();
 
163
        
 
164
        //g_print ("%s (%.2f)\n", __func__, f);
 
165
        int iWidth, iHeight;
 
166
        CD_APPLET_GET_MY_ICON_EXTENT (&iWidth, &iHeight);
 
167
        
 
168
        if (myData.pBackgroundSurface != NULL)
 
169
        {
 
170
                cairo_set_source_surface (
 
171
                        myDrawContext,
 
172
                        myData.pBackgroundSurface,
 
173
                        0.,
 
174
                        0.);
 
175
                cairo_paint (myDrawContext);
 
176
                cairo_dock_set_icon_surface_full (myDrawContext, myData.pBackgroundSurface, 1., 1., myIcon, myContainer);
 
177
        }
 
178
        
 
179
        double dx, dy, fScale=0;
 
180
        if (myData.pOldSurface != NULL && 1-f > .01)
 
181
        {
 
182
                dx = (iWidth - myData.iOldTextWidth)/2;
 
183
                dy = (iHeight - myData.iOldTextHeight)/2;
 
184
                if (dy < 0)  // peut arriver si une police de la taille de l'icone n'existe pas.
 
185
                {
 
186
                        dy = 0;
 
187
                        fScale = 1.*iHeight / myData.iOldTextHeight;
 
188
                        cairo_save (myDrawContext);
 
189
                        cairo_scale (myDrawContext, 1., fScale);  // a priori la difference n'est pas enorme, on laisse donc remplit en largeur.
 
190
                }
 
191
                cairo_set_source_surface (
 
192
                        myDrawContext,
 
193
                        myData.pOldSurface,
 
194
                        dx,
 
195
                        dy);
 
196
                cairo_paint_with_alpha (myDrawContext, 1-f);
 
197
                if (fScale != 0)
 
198
                        cairo_restore (myDrawContext);
 
199
        }
 
200
        if (myData.pCurrentSurface != NULL)
 
201
        {
 
202
                dx = (iWidth - myData.iCurrentTextWidth)/2;
 
203
                dy = (iHeight - myData.iCurrentTextHeight)/2;
 
204
                if (dy < 0)
 
205
                {
 
206
                        dy = 0;
 
207
                        fScale = 1.*iHeight / myData.iCurrentTextHeight;
 
208
                        cairo_save (myDrawContext);
 
209
                        cairo_scale (myDrawContext, 1., fScale);
 
210
                }
 
211
                cairo_set_source_surface (
 
212
                        myDrawContext,
 
213
                        myData.pCurrentSurface,
 
214
                        dx,
 
215
                        dy);
 
216
                cairo_paint_with_alpha (myDrawContext, f);
 
217
                if (fScale != 0)
 
218
                        cairo_restore (myDrawContext);
 
219
        }
 
220
        
 
221
        return TRUE;
 
222
}