summaryrefslogtreecommitdiffstats
path: root/apps/plugins/text_viewer/tv_preferences.c
blob: ad4b0aa12baba299e94737bd41e24068815cdfca (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 Gilles Roux
 *               2003 Garrett Derner
 *               2010 Yoshihisa Uchida
 *
 * This program 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.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "plugin.h"
#include "tv_preferences.h"

static struct tv_preferences prefs;
static bool is_initialized = false;
static int listner_count = 0;

#define TV_MAX_LISTNERS 4
static void (*listners[TV_MAX_LISTNERS])(const struct tv_preferences *oldp);

static void tv_notify_change_preferences(const struct tv_preferences *oldp,
                                         const struct tv_preferences *newp)
{
    int i;

    /*
     * the following items do not check.
     *   - alignment
     *   - scroll_mode
     *   - page_mode
     *   - font
     *   - autoscroll_speed
     */
    if ((oldp == NULL)                                             ||
        (oldp->word_mode            != newp->word_mode)            ||
        (oldp->line_mode            != newp->line_mode)            ||
        (oldp->windows              != newp->windows)              ||
        (oldp->horizontal_scrollbar != newp->horizontal_scrollbar) ||
        (oldp->vertical_scrollbar   != newp->vertical_scrollbar)   ||
        (oldp->encoding             != newp->encoding)             ||
#ifdef HAVE_LCD_BITMAP
        (oldp->header_mode          != newp->header_mode)          ||
        (oldp->footer_mode          != newp->footer_mode)          ||
        (rb->strcmp(oldp->font_name, newp->font_name))             ||
#endif
        (rb->strcmp(oldp->file_name, newp->file_name)))
    {
        for (i = listner_count - 1; i >= 0; i--)
            listners[i](oldp);
    }
}

const struct tv_preferences *tv_get_preferences(void)
{
    return &prefs;
}

void tv_set_preferences(const struct tv_preferences *new_prefs)
{
    struct tv_preferences *oldp = NULL;
    struct tv_preferences old_prefs;

    if (!is_initialized)
        is_initialized = true;
    else
    {
        old_prefs = prefs;
        oldp      = &old_prefs;
    }
    rb->memcpy(&prefs, new_prefs, sizeof(struct tv_preferences));
    tv_notify_change_preferences(oldp, &prefs);
}

void tv_copy_preferences(struct tv_preferences *copy_prefs)
{
    rb->memcpy(copy_prefs, &prefs, sizeof(struct tv_preferences));
}

void tv_set_default_preferences(struct tv_preferences *p)
{
    p->word_mode = WRAP;
    p->line_mode = NORMAL;
    p->windows = 1;
    p->alignment = LEFT;
    p->scroll_mode = PAGE;
    p->page_mode = NO_OVERLAP;
    p->horizontal_scrollbar = SB_OFF;
    p->vertical_scrollbar = SB_OFF;
    rb->memset(p->font_name, 0, MAX_PATH);
#ifdef HAVE_LCD_BITMAP
    p->header_mode = HD_BOTH;
    p->footer_mode = FT_BOTH;
    rb->strlcpy(p->font_name, rb->global_settings->font_file, MAX_PATH);
    p->font = rb->font_get(FONT_UI);
#else
    p->header_mode = HD_NONE;
    p->footer_mode = FT_NONE;
#endif
    p->autoscroll_speed = 1;
    /* Set codepage to system default */
    p->encoding = rb->global_settings->default_codepage;
    p->file_name[0] = '\0';
}

void tv_add_preferences_change_listner(void (*listner)(const struct tv_preferences *oldp))
{
    if (listner_count < TV_MAX_LISTNERS)
        listners[listner_count++] = listner;
}