3 * sushivision copyright (C) 2006-2007 Monty <monty@xiph.org>
5 * sushivision is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * sushivision is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with sushivision; see the file COPYING. If not, write to the
17 * Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <gtk/gtkmain.h>
32 static GtkWidgetClass
*parent_class
= NULL
;
37 static void _sv_spinner_draw(GtkWidget
*wg
, cairo_surface_t
*s
,int n
){
38 cairo_t
*c
= cairo_create(s
);
39 int w
= cairo_image_surface_get_width(s
);
40 int h
= cairo_image_surface_get_height(s
);
42 double alpha
= (n
>=0?1.:0.);
46 GdkColor
*bg
= &wg
->style
->bg
[GTK_STATE_NORMAL
];
47 double shade_r
=bg
->red
/65535.;
48 double shade_g
=bg
->green
/65535.;
49 double shade_b
=bg
->blue
/65535.;
50 cairo_set_source_rgb (c
, shade_r
,shade_g
,shade_b
);
54 double x
= w
/2 - 7*SPA
/2 + i
*SPA
+.5;
57 cairo_set_source_rgba (c
, 0,0,.5,1.);
58 cairo_arc(c
,x
,y
,DIA
*.5,0,M_PI
*2.);
61 cairo_set_source_rgba (c
, 1.,1.,1.,alpha
);
62 cairo_arc(c
,x
,y
,DIA
*.5,0,M_PI
*2.);
71 static void _sv_spinner_init (_sv_spinner_t
*p
){
72 // instance initialization
75 static void _sv_spinner_destroy (GtkObject
*object
){
77 if (GTK_OBJECT_CLASS (parent_class
)->destroy
)
78 (*GTK_OBJECT_CLASS (parent_class
)->destroy
) (object
);
80 GtkWidget
*widget
= GTK_WIDGET(object
);
81 _sv_spinner_t
*p
= SPINNER (widget
);
82 // free local resources
90 cairo_surface_destroy(p
->b
[i
]);
96 static gint
_sv_spinner_expose (GtkWidget
*widget
,
97 GdkEventExpose
*event
){
98 if (GTK_WIDGET_REALIZED (widget
)){
99 _sv_spinner_t
*sp
= SPINNER (widget
);
100 int frame
= (sp
->busy
?sp
->busy_count
+1:0);
103 if(sp
->b
&& sp
->b
[frame
]){
104 cairo_set_source_surface(sp
->wc
,
113 static void _sv_spinner_size_request (GtkWidget
*widget
,
114 GtkRequisition
*requisition
){
115 requisition
->width
= SPA
*7 + DIA
+ 2;
116 requisition
->height
= DIA
+ 2;
119 static void _sv_spinner_realize (GtkWidget
*widget
){
120 GdkWindowAttr attributes
;
121 gint attributes_mask
;
123 GTK_WIDGET_SET_FLAGS (widget
, GTK_REALIZED
);
124 GTK_WIDGET_SET_FLAGS (widget
, GTK_CAN_FOCUS
);
126 attributes
.x
= widget
->allocation
.x
;
127 attributes
.y
= widget
->allocation
.y
;
128 attributes
.width
= widget
->allocation
.width
;
129 attributes
.height
= widget
->allocation
.height
;
130 attributes
.wclass
= GDK_INPUT_OUTPUT
;
131 attributes
.window_type
= GDK_WINDOW_CHILD
;
132 attributes
.event_mask
=
133 gtk_widget_get_events (widget
) |
136 attributes
.visual
= gtk_widget_get_visual (widget
);
137 attributes
.colormap
= gtk_widget_get_colormap (widget
);
138 attributes_mask
= GDK_WA_X
| GDK_WA_Y
| GDK_WA_VISUAL
| GDK_WA_COLORMAP
;
139 widget
->window
= gdk_window_new (widget
->parent
->window
,
140 &attributes
, attributes_mask
);
141 gtk_style_attach (widget
->style
, widget
->window
);
142 gtk_style_set_background (widget
->style
, widget
->window
, GTK_STATE_NORMAL
);
143 gdk_window_set_user_data (widget
->window
, widget
);
144 gtk_widget_set_double_buffered (widget
, FALSE
);
147 static void _sv_spinner_size_allocate (GtkWidget
*widget
,
148 GtkAllocation
*allocation
){
149 _sv_spinner_t
*p
= SPINNER (widget
);
152 if (GTK_WIDGET_REALIZED (widget
)){
155 cairo_destroy(p
->wc
);
160 cairo_surface_destroy(p
->b
[i
]);
165 gdk_window_move_resize (widget
->window
, allocation
->x
, allocation
->y
,
166 allocation
->width
, allocation
->height
);
168 p
->wc
= gdk_cairo_create(widget
->window
);
171 p
->b
[i
] = cairo_image_surface_create(CAIRO_FORMAT_RGB24
,
174 _sv_spinner_draw(widget
, p
->b
[i
],i
-1);
178 widget
->allocation
= *allocation
;
181 static void _sv_spinner_class_init (_sv_spinner_class_t
* class) {
182 GtkObjectClass
*object_class
;
183 GtkWidgetClass
*widget_class
;
184 object_class
= (GtkObjectClass
*) class;
185 widget_class
= (GtkWidgetClass
*) class;
186 parent_class
= gtk_type_class (GTK_TYPE_WIDGET
);
188 object_class
->destroy
= _sv_spinner_destroy
;
189 widget_class
->realize
= _sv_spinner_realize
;
190 widget_class
->expose_event
= _sv_spinner_expose
;
191 widget_class
->size_request
= _sv_spinner_size_request
;
192 widget_class
->size_allocate
= _sv_spinner_size_allocate
;
196 GType
_sv_spinner_get_type (void){
198 static GType spinner_type
= 0;
202 static const GTypeInfo spinner_info
= {
203 sizeof (_sv_spinner_class_t
),
206 (GClassInitFunc
) _sv_spinner_class_init
,
209 sizeof (_sv_spinner_t
),
211 (GInstanceInitFunc
) _sv_spinner_init
,
215 spinner_type
= g_type_register_static (GTK_TYPE_WIDGET
, "Spinner",
222 _sv_spinner_t
*_sv_spinner_new (){
223 GtkWidget
*g
= GTK_WIDGET (g_object_new (SPINNER_TYPE
, NULL
));
224 _sv_spinner_t
*p
= SPINNER (g
);
228 void _sv_spinner_set_busy(_sv_spinner_t
*p
){
234 gettimeofday(&now
,NULL
);
239 _sv_spinner_expose(GTK_WIDGET(p
),NULL
); // do it now
242 test
= (now
.tv_sec
- p
->last
.tv_sec
)*1000 + (now
.tv_usec
- p
->last
.tv_usec
)/1000;
250 _sv_spinner_expose(GTK_WIDGET(p
),NULL
); // do it now
255 void _sv_spinner_set_idle(_sv_spinner_t
*p
){
258 _sv_spinner_expose(GTK_WIDGET(p
),NULL
); // do it now