aboutsummaryrefslogtreecommitdiffhomepage
path: root/gui/win32/widgetrender.c
blob: b3a6cca46dabd85ff029a37656a0b3ad28c9203c (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 * MPlayer GUI for Win32
 * Copyright (C) 2003 Sascha Sommer <saschasommer@freenet.de>
 * Copyright (C) 2006 Erik Augustson <erik_27can@yahoo.com>
 * Copyright (C) 2006 Gianluigi Tiesi <sherpya@netfarm.it>
 *
 * This file is part of MPlayer.
 *
 * MPlayer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * MPlayer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MPlayer; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <stdio.h>
#include <ctype.h>
#include <windows.h>
#include <interface.h>
#include "gui.h"

extern char *codecname;
#define MAX_LABELSIZE 250

static void render(int bitsperpixel, image *dst, image *src, int x, int y, int sx, int sy, int sw, int sh, int transparent)
{
    int i;
    int bpp = bitsperpixel / 8;
    int offset = (dst->width * bpp * y) + (x * bpp);
    int soffset = (src->width * bpp * sy) + (sx * bpp);

    for(i=0; i<sh; i++)
    {
        int c;
        for(c=0; c < (sw * bpp); c += bpp)
        {
            if(bpp == 2)
            {
                if(!transparent || (((src->data + soffset + (i * src->width * bpp) + c)[0] != 0x1f)
                    && ((src->data + soffset + (i * src->width * bpp) + c)[1] != 0x7c)))
                    memcpy(dst->data + offset + c, src->data + soffset + (i * src->width * bpp) + c, bpp);
            }
            else if(bpp > 2)
            {
                if(!transparent || *((unsigned int *) (src->data + soffset + (i * src->width * bpp) + c)) != 0x00ff00ff)
                    memcpy(dst->data + offset + c, src->data + soffset + (i * src->width * bpp) + c, bpp);
            }
        }
        offset += (dst->width * bpp);
    }
}

static image *find_background(skin_t *skin, widget *item)
{
    unsigned int i;
    for (i=0; i < skin->windowcount; i++)
        if(skin->windows[i]->type == item->window)
            return skin->windows[i]->base->bitmap[0];
    return NULL;
}

/******************************************************************/
/*                      FONT related functions                    */
/******************************************************************/

/* returns the pos of s2 inside s1 or -1 if  s1 doesn't contain s2 */
static int strpos(char *s1, const char* s2)
{
    unsigned int i, x;
    for (i=0; i < strlen(s1); i++)
    {
        if(s1[i] == s2[0])
        {
            if(strlen(s1 + i) >= strlen(s2))
            {
                for (x=0; x <strlen(s2); x++)
                    if(s1[i + x] != s2[x]) break;
                if(x == strlen(s2)) return i;
            }
        }
    }
    return -1;
}

/* replaces all occurences of what in dest with format */
static void stringreplace(char *dest, const char *what, const char *format, ... )
{
    char tmp[MAX_LABELSIZE];
    int offset=0;
    va_list va;
    va_start(va, format);
    vsnprintf(tmp, MAX_LABELSIZE, format, va);
    va_end(va);
    /* no search string == replace the entire string */
    if(!what)
    {
        memcpy(dest, tmp, strlen(tmp));
        dest[strlen(tmp)] = 0;
        return;
    }
    while((offset = strpos(dest, what)) != -1)
    {
        memmove(dest + offset + strlen(tmp), dest + offset + strlen(what), strlen(dest + offset + strlen(what)) + 1);
        memcpy(dest + offset, tmp, strlen(tmp));
    }
}

/* replaces the chars with special meaning with the associated data from the player info struct */
static char *generatetextfromlabel(widget *item)
{
    char *text = malloc(MAX_LABELSIZE);
    char tmp[MAX_LABELSIZE];
    unsigned int i;
    if(!item)
    {
        free(text);
        return NULL;
    }
    strcpy(text, item->label);
    if(item->type == tySlabel) return text;
    stringreplace(text, "$1", "%.2i:%.2i:%.2i", guiIntfStruct.TimeSec / 3600,
                 (guiIntfStruct.TimeSec / 60) % 60, guiIntfStruct.TimeSec % 60);
    stringreplace(text, "$2", "%.4i:%.2i", guiIntfStruct.TimeSec / 60, guiIntfStruct.TimeSec % 60);
    stringreplace(text, "$3", "%.2i", guiIntfStruct.TimeSec / 3600);
    stringreplace(text, "$4", "%.2i", (guiIntfStruct.TimeSec / 60) % 60);
    stringreplace(text, "$5", "%.2i", guiIntfStruct.TimeSec % 60);
    stringreplace(text, "$6", "%.2i:%.2i:%.2i", guiIntfStruct.LengthInSec / 3600,
                 (guiIntfStruct.LengthInSec / 60) % 60, guiIntfStruct.LengthInSec % 60);
    stringreplace(text, "$7", "%.4i:%.2i", guiIntfStruct.LengthInSec / 60, guiIntfStruct.LengthInSec % 60);
    stringreplace(text, "$8", "%i:%.2i:%.2i", guiIntfStruct.TimeSec / 3600,
                 (guiIntfStruct.TimeSec / 60) % 60, guiIntfStruct.TimeSec % 60);
    stringreplace(text, "$v", "%3.2f", guiIntfStruct.Volume);
    stringreplace(text, "$V", "%3.1f", guiIntfStruct.Volume);
    stringreplace(text, "$b", "%3.2f", guiIntfStruct.Balance);
    stringreplace(text, "$B", "%3.1f", guiIntfStruct.Balance);
    stringreplace(text, "$t", "%.2i", guiIntfStruct.Track);
    stringreplace(text, "$o", "%s", guiIntfStruct.Filename);
    stringreplace(text, "$x", "%i", guiIntfStruct.MovieWidth);
    stringreplace(text, "$y", "%i", guiIntfStruct.MovieHeight);
    stringreplace(text, "$C", "%s", guiIntfStruct.sh_video ? codecname : "");
    stringreplace(text, "$$", "$");

    if(!strcmp(text, "$p") || !strcmp(text, "$s") || !strcmp(text, "$e"))
    {
        if(guiIntfStruct.Playing == 0) stringreplace(text, NULL, "s");
        else if(guiIntfStruct.Playing == 1) stringreplace(text, NULL, "p");
        else if(guiIntfStruct.Playing == 2) stringreplace(text, NULL, "e");
    }

    if(guiIntfStruct.AudioType == 0) stringreplace(text, "$a", "n");
    else if(guiIntfStruct.AudioType == 1) stringreplace(text, "$a", "m");
    else stringreplace(text, "$a", "t");

    if(guiIntfStruct.StreamType == 0)
        stringreplace(text, "$T", "f");
#ifdef USE_DVDREAD
    else if(guiIntfStruct.StreamType == STREAMTYPE_DVD || guiIntfStruct.StreamType == STREAMTYPE_DVDNAV)
        stringreplace(text, "$T", "d");
#endif
    else stringreplace(text, "$T", "u");

    if(guiIntfStruct.Filename)
    {
        for (i=0; i<strlen(guiIntfStruct.Filename); i++)
            tmp[i] = tolower(guiIntfStruct.Filename[i]);
        stringreplace(text, "$f", tmp);

        for (i=0; i<strlen(guiIntfStruct.Filename); i++)
            tmp[i] = toupper(guiIntfStruct.Filename[i]);
        stringreplace(text, "$F", tmp);
    }

    return text;
}

/* cuts text to buflen scrolling from right to left */
static void scrolltext(char *text, unsigned int buflen, float *value)
{
    char *buffer = (char *) malloc(buflen + 1);
    unsigned int x,i;
    if(*value < buflen) x = 0;
    else x = *value - buflen;
    memset(buffer, ' ', buflen);
    for (i = (*value>=buflen) ? 0 : buflen - *value; i<buflen; i++)
    {
        if(x < strlen(text))
            buffer[i] = text[x];
        x++;
    }
    buffer[buflen] = 0;
    *value += 1.0f;
    if(*value >= strlen(text) + buflen) *value = 0.0f;
    strcpy(text, buffer);
    free(buffer);
}

/* updates all dlabels and slabels */
void renderinfobox(skin_t *skin, window_priv_t *priv)
{
    unsigned int i;
    if (!priv) return;

    /* repaint the area behind the text*/
    /* we have to do this for all labels here, because they may overlap in buggy skins ;( */

    for (i=0; i<skin->widgetcount; i++)
        if((skin->widgets[i]->type == tyDlabel) || (skin->widgets[i]->type == tySlabel))
        {
            if(skin->widgets[i]->window == priv->type)
                render(skin->desktopbpp,
                       &priv->img,
                       find_background(skin, skin->widgets[i]),
                       skin->widgets[i]->x,
                       skin->widgets[i]->y,
                       skin->widgets[i]->x,
                       skin->widgets[i]->y,
                       skin->widgets[i]->length,
                       skin->widgets[i]->font->chars[0]->height,
                       1);
        }

    /* load all slabels and dlabels */
    for (i=0; i<skin->widgetcount; i++)
    {
        widget *item = skin->widgets[i];
        if(item->window != priv->type) continue;
        if((i == skin->widgetcount) || (item->type == tyDlabel) || (item->type == tySlabel))
        {
            char *text = generatetextfromlabel(item);
            unsigned int current, c;
            int offset = 0;
            unsigned int textlen;
            if(!text) continue;
            textlen = strlen(text);

            /* render(win, win->background, gui->skin->widgets[i]->x, gui->skin->widgets[i]->y,
                      gui->skin->widgets[i]->x, gui->skin->widgets[i]->y,
                      gui->skin->widgets[i]->length, gui->skin->widgets[i]->font->chars[0]->height,1); */

            /* calculate text size */
            for (current=0; current<textlen; current++)
            {
                for (c=0; c<item->font->charcount; c++)
                    if(item->font->chars[c]->c == text[current])
                    {
                        offset += item->font->chars[c]->width;
                        break;
                    }
            }

            /* labels can be scrolled if they are to big */
            if((item->type == tyDlabel) && (item->length < offset))
            {
                int tomuch = (offset - item->length) / (offset /textlen);
                scrolltext(text, textlen - tomuch - 1, &skin->widgets[i]->value);
                textlen = strlen(text);
            }

            /* align the text */
            if(item->align == 1)
                offset = (item->length-offset) / 2;
            else if(item->align == 2)
                offset = item->length-offset;
            else
                offset = 0;

            if(offset < 0) offset = 0;

            /* render the text */
            for (current=0; current<textlen; current++)
            {
                for (c=0; c<item->font->charcount; c++)
                {
                    char_t *cchar = item->font->chars[c];
                    if(cchar->c == *(text + current))
                    {
                        render(skin->desktopbpp,
                               &priv->img,
                               item->font->image,
                               item->x + offset,
                               item->y,
                               cchar->x,
                               cchar->y,
                               (cchar->width + offset > item->length) ? item->length - offset : cchar->width,
                               cchar->height,
                               1);
                        offset += cchar->width;
                    break;
                    }
                }
            }
            free(text);
        }
    }
}

/******************************************************************/
/*                   WIDGET related functions                     */
/******************************************************************/

void renderwidget(skin_t *skin, image *dest, widget *item, int state)
{
    image *img = NULL;
    int height;
    int y;

    if(!dest) return;
    if((item->type == tyButton) || (item->type == tyHpotmeter) || (item->type == tyPotmeter))
        img = item->bitmap[0];

    if(!img) return;

    y = item->y;
    if(item->type == tyPotmeter)
    {
        height = img->height / item->phases;
        y =  height * (int)(item->value * item->phases / 100);
        if(y > img->height-height)
            y = img->height - height;
    }
    else
    {
        height = img->height / 3;
        y = state * height;
    }

    /* redraw background */
    if(item->type == tyButton)
        render(skin->desktopbpp, dest, find_background(skin,item), item->x, item->y, item->x, item->y, img->width, height, 1);

    if((item->type == tyHpotmeter) || (item->type == tyPotmeter))
    {
        /* repaint the area behind the slider */
        render(skin->desktopbpp, dest, find_background(skin, item), item->wx, item->wy, item->wx, item->wy, item->wwidth, item->height, 1);
        item->x = item->value * (item->wwidth-item->width) / 100 + item->wx;
        if((item->x + item->width) > (item->wx + item->wwidth))
            item->x = item->wx + item->wwidth - item->width;
        if(item->x < item->wx)
            item->x = item->wx;
        /* workaround for blue */
        if(item->type == tyHpotmeter)
            height = (item->height < img->height / 3) ? item->height : img->height / 3;
    }
    render(skin->desktopbpp, dest, img, item->x, item->y, 0, y, img->width, height, 1);
}