summaryrefslogtreecommitdiffstats
path: root/source/widgets/separator.c
blob: 295ba95fb5d61435156cf451f33f8bf8183ec8be (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
/**
 *   MIT/X11 License
 *   Modified  (c) 2016 Qball Cow <qball@gmpclient.org>
 *
 *   Permission is hereby granted, free of charge, to any person obtaining
 *   a copy of this software and associated documentation files (the
 *   "Software"), to deal in the Software without restriction, including
 *   without limitation the rights to use, copy, modify, merge, publish,
 *   distribute, sublicense, and/or sell copies of the Software, and to
 *   permit persons to whom the Software is furnished to do so, subject to
 *   the following conditions:
 *
 *   The above copyright notice and this permission notice shall be
 *   included in all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 *   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 *   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 *   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 *   TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 *   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <config.h>
#include <xkbcommon/xkbcommon.h>
#include <glib.h>
#include <string.h>
#include "widgets/widget.h"
#include "widgets/widget-internal.h"
#include "widgets/separator.h"
#include "x11-helper.h"
#include "settings.h"
#include "theme.h"

/**
 * @param sp The separator widget handle.
 * @param style_str String representation of the style.
 *
 * Sets the line style based on the string style_str
 */
void separator_set_line_style_from_string ( separator *sp, const char *style_str );

/**
 * @param sp The separator widget handle.
 * @param style The new style.
 *
 * Sets the line style.
 */
void separator_set_line_style ( separator *sp, separator_line_style style );

/**
 * Internal structure for the separator.
 */
struct _separator
{
    widget               widget;
    separator_type       type;
    separator_line_style line_style;
};

/** Configuration value for separator style indicating no line */
const char *const _separator_style_none = "none";
/** Configuration value for separator style indicating dashed line. */
const char *const _separator_style_dash = "dash";
static void separator_draw ( widget *, cairo_t * );
static void separator_free ( widget * );

separator *separator_create ( const char *name, separator_type type, short sw )
{
    separator *sb = g_malloc0 ( sizeof ( separator ) );
    sb->type = type;
    sb->widget.name = g_strdup ( name );
    sb->widget.x = 0;
    sb->widget.y = 0;
    if ( sb->type == S_HORIZONTAL ) {
        sb->widget.w = 1;
        sb->widget.h = MAX ( 1, sw );
    }
    else {
        sb->widget.h = 1;
        sb->widget.w = MAX ( 1, sw );
    }

    sb->widget.draw = separator_draw;
    sb->widget.free = separator_free;

    // Enabled by default
    sb->widget.enabled = TRUE;

    const char *line_style = rofi_theme_get_string ( "@scrollbar", sb->widget.name, NULL, "line-style", "solid");
    separator_set_line_style_from_string ( sb, line_style );
    return sb;
}

static void separator_free ( widget *wid )
{
    separator *sb = (separator *) wid;
    g_free ( sb );
}

void separator_set_line_style ( separator *sp, separator_line_style style )
{
    if ( sp ) {
        sp->line_style = style;
        widget_need_redraw ( WIDGET ( sp ) );
    }
}
void separator_set_line_style_from_string ( separator *sp, const char *style_str )
{
    if ( !sp  ) {
        return;
    }
    separator_line_style style = S_LINE_SOLID;
    if ( g_strcmp0 ( style_str, _separator_style_none ) == 0 ) {
        style = S_LINE_NONE;
    }
    else if ( g_strcmp0 ( style_str, _separator_style_dash ) == 0 ) {
        style = S_LINE_DASH;
    }
    separator_set_line_style ( sp, style );
}

static void separator_draw ( widget *wid, cairo_t *draw )
{
    separator *sep = (separator *) wid;
    if ( sep->line_style == S_LINE_NONE ) {
        // Nothing to draw.
        return;
    }
    color_separator ( draw );
    rofi_theme_get_color ( "@separator", wid->name, NULL, "foreground", draw );
    if ( sep->line_style == S_LINE_DASH ) {
        const double dashes[1] = { 4 };
        cairo_set_dash ( draw, dashes, 1, 0.0 );
    }
    if ( sep->type == S_HORIZONTAL ) {
        cairo_set_line_width ( draw, wid->h );
        double half = wid->h / 2.0;
        cairo_move_to ( draw, wid->x, wid->y + half );
        cairo_line_to ( draw, wid->x + wid->w, wid->y + half );
    }
    else {
        cairo_set_line_width ( draw, wid->w );
        double half = wid->w / 2.0;
        cairo_move_to ( draw, wid->x + half, wid->y );
        cairo_line_to ( draw, wid->x + half, wid->y + wid->h );
    }
    cairo_stroke ( draw );
}