1 /*
2  * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3  *           (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com>
4  *
5  * This file is part of lsp-plugins
6  * Created on: 9 июл. 2017 г.
7  *
8  * lsp-plugins is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * any later version.
12  *
13  * lsp-plugins is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with lsp-plugins. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #include <ui/tk/tk.h>
23 
24 namespace lsp
25 {
26     namespace tk
27     {
28         const w_class_t LSPSeparator::metadata = { "LSPSeparator", &LSPWidget::metadata };
29 
LSPSeparator(LSPDisplay * dpy,bool horizontal)30         LSPSeparator::LSPSeparator(LSPDisplay *dpy, bool horizontal): LSPWidget(dpy),
31             sColor(this)
32         {
33             nSize           = -1;
34             nBorder         = 2;
35             nLineWidth      = 1;
36             nPadding        = 0;
37             enOrientation   = (horizontal) ? O_HORIZONTAL : O_VERTICAL;
38             pClass          = &metadata;
39         }
40 
~LSPSeparator()41         LSPSeparator::~LSPSeparator()
42         {
43         }
44 
init()45         status_t LSPSeparator::init()
46         {
47             status_t result = LSPWidget::init();
48             if (result != STATUS_OK)
49                 return result;
50 
51             init_color(C_LABEL_TEXT, &sColor);
52 
53             return STATUS_OK;
54         }
55 
set_size(ssize_t value)56         void LSPSeparator::set_size(ssize_t value)
57         {
58             nSize       = value;
59             query_resize();
60         }
61 
set_border(size_t value)62         void LSPSeparator::set_border(size_t value)
63         {
64             nBorder     = value;
65             query_resize();
66         }
67 
set_padding(size_t value)68         void LSPSeparator::set_padding(size_t value)
69         {
70             nPadding    = value;
71             query_resize();
72         }
73 
set_line_width(size_t value)74         void LSPSeparator::set_line_width(size_t value)
75         {
76             nLineWidth  = value;
77             query_resize();
78         }
79 
set_horizontal(bool value)80         void LSPSeparator::set_horizontal(bool value)
81         {
82             set_orientation((value) ? O_HORIZONTAL : O_VERTICAL);
83         }
84 
set_vertical(bool value)85         void LSPSeparator::set_vertical(bool value)
86         {
87             set_orientation((value) ? O_VERTICAL : O_HORIZONTAL);
88         }
89 
set_orientation(orientation_t value)90         void LSPSeparator::set_orientation(orientation_t value)
91         {
92             if (enOrientation == value)
93                 return;
94 
95             enOrientation = value;
96             query_resize();
97         }
98 
render(ISurface * s,bool force)99         void LSPSeparator::render(ISurface *s, bool force)
100         {
101             // Prepare palette
102             Color bg_color(sBgColor);
103             Color color(sColor);
104             color.scale_lightness(brightness());
105 
106             // Draw background
107             s->fill_rect(sSize.nLeft, sSize.nTop, sSize.nWidth, sSize.nHeight, bg_color);
108 
109             // Draw separator
110             ssize_t width   = sSize.nWidth - nBorder * 2;
111             ssize_t height  = sSize.nHeight - nBorder * 2;
112 
113             if (enOrientation == O_HORIZONTAL)
114             {
115                 ssize_t length  = (nSize >= 0) ? nSize : width - nPadding * 2;
116                 if (length < ssize_t(nLineWidth))
117                     length      = nLineWidth;
118                 s->fill_rect(sSize.nLeft + ((sSize.nWidth - length) >> 1), sSize.nTop + ((sSize.nHeight - nLineWidth) >> 1), length, nLineWidth, color);
119             }
120             else
121             {
122                 ssize_t length  = (nSize >= 0) ? nSize : height - nPadding * 2;
123                 if (length < ssize_t(nLineWidth))
124                     length      = nLineWidth;
125                 s->fill_rect(sSize.nLeft + ((sSize.nWidth - nLineWidth) >> 1), sSize.nTop + ((sSize.nHeight - length) >> 1), nLineWidth, length, color);
126             }
127         }
128 
size_request(size_request_t * r)129         void LSPSeparator::size_request(size_request_t *r)
130         {
131             r->nMaxWidth    = -1;
132             r->nMaxHeight   = -1;
133 
134             if (enOrientation == O_HORIZONTAL)
135             {
136                 r->nMinWidth    = (nBorder + nPadding)*2 + nLineWidth;
137                 r->nMinHeight   = nBorder*2 + nLineWidth;
138                 if (nSize > 0)
139                     r->nMinWidth   += nSize;
140             }
141             else
142             {
143                 r->nMinWidth    = nBorder*2 + nLineWidth;
144                 r->nMinHeight   = (nBorder + nPadding)*2 + nLineWidth;
145                 if (nSize > 0)
146                     r->nMinHeight  += nSize;
147             };
148         }
149 
150     } /* namespace tk */
151 } /* namespace lsp */
152