]> www.fi.muni.cz Git - evince.git/blob - shell/ev-presentation-timer.c
Small cleanup
[evince.git] / shell / ev-presentation-timer.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /* this file is part of evince, a gnome document viewer
3  *
4  *
5  *  Author:
6  *    Lukas Bezdicka <255993@mail.muni.cz>
7  *
8  * Evince is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * Evince is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <cairo.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30 #include <math.h>
31
32 #include "ev-presentation-timer.h"
33
34 struct _EvPresentationTimerPrivate
35 {
36         gint       time;
37         gint       remaining;
38         guint      page;
39         guint      pages;
40         gboolean   running;
41 };
42
43 #define EV_PRESENTATION_TIMER_GET_PRIVATE(object) \
44         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_PRESENTATION_TIMER, EvPresentationTimerPrivate))
45
46 G_DEFINE_TYPE (EvPresentationTimer, ev_presentation_timer, GTK_TYPE_DRAWING_AREA);
47
48 static gdouble
49 ev_presentation_timer_progress (gdouble time, gdouble remaining)
50 {
51         return remaining/(time*60);
52 }
53
54 static gboolean
55 ev_presentation_timer_draw(GtkWidget *timer, cairo_t *cr)
56 {
57         EvPresentationTimer *ev_timer = EV_PRESENTATION_TIMER(timer);
58         GtkAllocation allocation;
59         gtk_widget_get_allocation (timer, &allocation);
60         cairo_set_source_rgb (cr, 0, 0, 0);
61         cairo_set_line_width (cr, 5);
62         guint pos = (allocation.width/ev_timer->priv->pages)*ev_timer->priv->page;
63         cairo_move_to (cr,pos,2);
64         cairo_line_to (cr,pos,allocation.height);
65         cairo_stroke (cr);
66         if(ev_timer->priv->running && ev_timer->priv->time > 0 && ev_timer->priv->remaining > 0)
67         {
68                 gdouble progress = ev_presentation_timer_progress (ev_timer->priv->time,
69                                                                    ev_timer->priv->remaining)*(allocation.width);
70                 cairo_rectangle (cr, allocation.width-progress,
71                                  10,
72                                  (allocation.width-(allocation.width-progress))-10,
73                                  allocation.height-5);
74                 cairo_stroke_preserve (cr);
75                 cairo_fill(cr);
76         }
77         return FALSE;
78 }
79
80 static gboolean
81 timeout_cb (gpointer data)
82 {
83         EvPresentationTimer *ev_timer = EV_PRESENTATION_TIMER(data);
84         ev_timer->priv->remaining--;
85
86         if(time >= 0 && ev_timer->priv->remaining >= 0)
87         {
88                 gtk_widget_queue_draw(GTK_WIDGET(ev_timer));
89         } else
90                 ev_timer->priv->running = FALSE;
91         return ev_timer->priv->running;
92 }
93
94 void
95 ev_presentation_timer_set_pages (EvPresentationTimer *ev_timer, guint pages)
96 {
97         ev_timer->priv->pages = pages -1;
98 }
99
100 void
101 ev_presentation_timer_set_page (EvPresentationTimer *ev_timer, guint page)
102 {
103         if (page >= ev_timer->priv->pages)
104         {
105                 page = ev_timer->priv->pages;
106                 ev_timer->priv->running=FALSE;
107         }
108         ev_timer->priv->page = page;
109         gtk_widget_queue_draw(GTK_WIDGET(ev_timer));
110 }
111
112 static void
113 ev_presentation_timer_init (EvPresentationTimer *ev_timer)
114 {
115         ev_timer->priv = EV_PRESENTATION_TIMER_GET_PRIVATE (ev_timer);
116         ev_timer->priv->page = 0;
117         ev_timer->priv->pages = 0;
118         ev_timer->priv->remaining = 0;
119         ev_timer->priv->time = 0;
120         ev_timer->priv->running = FALSE;
121 }
122
123 void
124 ev_presentation_timer_start (EvPresentationTimer *ev_timer)
125 {
126         if (ev_timer->priv->running == FALSE)
127         {
128                 ev_timer->priv->remaining = (ev_timer->priv->time)*60;
129                 ev_timer->priv->running = TRUE;
130                 g_timeout_add_seconds (1, timeout_cb, ev_timer);
131         }
132 }
133
134 void
135 ev_presentation_timer_stop (EvPresentationTimer *ev_timer)
136 {
137         ev_timer->priv->remaining = 0;
138 }
139
140 void
141 ev_presentation_timer_set_time (EvPresentationTimer *ev_timer,
142                                 gint time)
143 {
144         if(ev_timer->priv->running)
145                 ev_timer->priv->remaining = ((ev_timer->priv->remaining)/(ev_timer->priv->time)*time);
146
147         ev_timer->priv->time = (time < -1)? -1:time;
148 }
149
150 static void
151 ev_presentation_timer_class_init (EvPresentationTimerClass *klass)
152 {
153         GObjectClass    *object_class = G_OBJECT_CLASS (klass);
154         GtkWidgetClass  *widget_class = GTK_WIDGET_CLASS (klass);
155         g_type_class_add_private (object_class, sizeof (EvPresentationTimerPrivate));
156         widget_class->draw = ev_presentation_timer_draw;
157         /*object_class->finalize = ev_presentation_timer_finalize;*/
158 }
159
160 GtkWidget *
161 ev_presentation_timer_new (void)
162 {
163         EvPresentationTimer     *ev_timer;
164
165         ev_timer = g_object_new (EV_TYPE_PRESENTATION_TIMER, NULL);
166         return GTK_WIDGET (ev_timer);
167 }