summaryrefslogtreecommitdiff
path: root/deck.c
blob: b5f7536842cc3a6bf36e325a573aa6c56bc0c091 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "deck.h"

#define MARGIN		18
#define RATIO		1.414
#define N_SLIDES	30

struct Deck
{
    DeckChangeNotify	notify;	/* Called on slide add/delete/reorder */
    gpointer		data;

    int			view_width;
    int			thumb_width;
};

Deck *
deck_new (DeckChangeNotify notify,
	  gpointer data)
{
    Deck *deck = g_new0 (Deck, 1);

    deck->notify = notify;
    deck->data = data;
    
    return deck;
}

void
deck_set_main_width (Deck *deck, int view_width)
{
    deck->view_width = view_width;
}

static int
get_slide_height (int view_width)
{
    return (view_width - 2 * MARGIN) / RATIO;
}

int
deck_get_main_slide_height (Deck *deck)
{
    return get_slide_height (deck->view_width);
}

int
deck_get_thumb_slide_height (Deck *deck)
{
    return get_slide_height (deck->thumb_width);
}

static void
deck_paint (Deck *deck,
	    cairo_t *cr,
	    int	orig_x,
	    int orig_y,
	    int width,
	    gboolean is_thumbnail)
{
    int height;
    int i;
    
    height = get_slide_height (width);

    width -= 2 * MARGIN;
    
    for (i = 0; i < N_SLIDES; ++i)
    {
	double x, y;

	x = orig_x + MARGIN;
	y = orig_y + MARGIN + i * (MARGIN + height);

	cairo_save (cr);

	cairo_set_source_rgba (cr, 0, 0, 0, 0.6);
	cairo_translate (cr, 2, 2);
	cairo_rectangle (cr, x, y, width, height);
	cairo_fill (cr);
	cairo_restore (cr);
	
	cairo_set_source_rgba (cr, 1, 1, 1, 1);
	cairo_rectangle (cr, x, y, width, height);
	cairo_fill (cr);

	cairo_rectangle (cr, x + 0.5, y + 0.5, width - 1, height - 1);
	
	cairo_set_source_rgba (cr, 0, 0, 0, 1);
	cairo_set_line_width (cr, 1);
	cairo_stroke (cr);

	cairo_set_source_rgba (cr, (i + 1.0) / N_SLIDES, 0,
			       1 - (i + 1.0) / N_SLIDES, 1);

	if (is_thumbnail)
	{
	    cairo_rectangle (cr, x + width/2 - 4, y + height/2 - 4,
			     8, 8);
	}
	else
	{
	    cairo_rectangle (cr, x + width/2 - 10, y + height/2 - 10,
			     20, 20);
	}

	cairo_fill (cr);
    }
}

void
deck_paint_main (Deck          *deck,
		 cairo_t       *cr,
		 int		orig_x,
		 int		orig_y)
{
    deck_paint (deck, cr, orig_x, orig_y, deck->view_width, FALSE);
}

void
deck_paint_thumbs (Deck *deck,
		   cairo_t *cr,
		   int x,
		   int y)
{
    deck_paint (deck, cr, x, y, deck->thumb_width, TRUE);
}

/* Returns the slide the user is likely looking
 * at, given the viewport.
 */
int
deck_get_view_slide (Deck *deck,
		     GdkRectangle *viewport)
{
    int slide_height = deck_get_main_slide_height (deck);
    
    /* Compute the y-coordinate of the first visible top edge of a slide */
    return (viewport->y - MARGIN) / (MARGIN + slide_height) + 1;
}

/* Returns the y coordinate of the nth slide given
 * the viewport
 */
int
deck_get_main_slide_location (Deck *deck,
			      int nth_slide)
{
    int slide_height = deck_get_main_slide_height (deck);
    
    return nth_slide * (MARGIN + slide_height) + MARGIN;
}

int
deck_get_thumb_slide_location (Deck *deck,
			       int nth_slide)
{
    int slide_height = deck_get_thumb_slide_height (deck);

    return nth_slide * (MARGIN + slide_height) + MARGIN;
}

int
deck_get_main_height (Deck *deck)
{
    int slide_height = deck_get_main_slide_height (deck);
    
    return N_SLIDES * slide_height + (N_SLIDES + 1) * MARGIN;
}


void
deck_set_thumb_width (Deck *deck, int thumb_width)
{
    deck->thumb_width = thumb_width;
}

int
deck_get_thumb_height (Deck *deck)
{
    int thumb_height = deck_get_thumb_slide_height (deck);
    
    return N_SLIDES * thumb_height + (N_SLIDES + 1) * MARGIN;
}