1 // This module implements the QsciStyle class.
2 //
3 // Copyright (c) 2021 Riverbank Computing Limited <info@riverbankcomputing.com>
4 //
5 // This file is part of QScintilla.
6 //
7 // This file may be used under the terms of the GNU General Public License
8 // version 3.0 as published by the Free Software Foundation and appearing in
9 // the file LICENSE included in the packaging of this file.  Please review the
10 // following information to ensure the GNU General Public License version 3.0
11 // requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 //
13 // If you do not wish to use this file under the terms of the GPL version 3.0
14 // then you may purchase a commercial license.  For more information contact
15 // info@riverbankcomputing.com.
16 //
17 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 
20 
21 #include "Qsci/qscistyle.h"
22 
23 #include <qapplication.h>
24 
25 #include "Qsci/qsciscintillabase.h"
26 
27 
28 // A ctor.
QsciStyle(int style)29 QsciStyle::QsciStyle(int style)
30 {
31     init(style);
32 
33     QPalette pal = QApplication::palette();
34     setColor(pal.text().color());
35     setPaper(pal.base().color());
36 
37     setFont(QApplication::font());
38     setEolFill(false);
39 }
40 
41 
42 // A ctor.
QsciStyle(int style,const QString & description,const QColor & color,const QColor & paper,const QFont & font,bool eolFill)43 QsciStyle::QsciStyle(int style, const QString &description,
44         const QColor &color, const QColor &paper, const QFont &font,
45         bool eolFill)
46 {
47     init(style);
48 
49     setDescription(description);
50 
51     setColor(color);
52     setPaper(paper);
53 
54     setFont(font);
55     setEolFill(eolFill);
56 }
57 
58 
59 // Initialisation common to all ctors.
init(int style)60 void QsciStyle::init(int style)
61 {
62     // The next style number to allocate.  The initial values corresponds to
63     // the amount of space that Scintilla initially creates for styles.
64     static int next_style_nr = 63;
65 
66     // See if a new style should be allocated.  Note that we allow styles to be
67     // passed in that are bigger than STYLE_MAX because the styles used for
68     // annotations are allowed to be.
69     if (style < 0)
70     {
71         // Note that we don't deal with the situation where the newly allocated
72         // style number has already been used explicitly.
73         if (next_style_nr > QsciScintillaBase::STYLE_LASTPREDEFINED)
74             style = next_style_nr--;
75     }
76 
77     style_nr = style;
78 
79     // Initialise the minor attributes.
80     setTextCase(QsciStyle::OriginalCase);
81     setVisible(true);
82     setChangeable(true);
83     setHotspot(false);
84 }
85 
86 
87 // Apply the style to a particular editor.
apply(QsciScintillaBase * sci) const88 void QsciStyle::apply(QsciScintillaBase *sci) const
89 {
90     // Don't do anything if the style is invalid.
91     if (style_nr < 0)
92         return;
93 
94     sci->SendScintilla(QsciScintillaBase::SCI_STYLESETFORE, style_nr,
95             style_color);
96     sci->SendScintilla(QsciScintillaBase::SCI_STYLESETBACK, style_nr,
97             style_paper);
98     sci->SendScintilla(QsciScintillaBase::SCI_STYLESETFONT, style_nr,
99             style_font.family().toLatin1().data());
100     sci->SendScintilla(QsciScintillaBase::SCI_STYLESETSIZEFRACTIONAL, style_nr,
101             long(style_font.pointSizeF() * QsciScintillaBase::SC_FONT_SIZE_MULTIPLIER));
102 
103     // Pass the Qt weight via the back door.
104     sci->SendScintilla(QsciScintillaBase::SCI_STYLESETWEIGHT, style_nr,
105             -style_font.weight());
106 
107     sci->SendScintilla(QsciScintillaBase::SCI_STYLESETITALIC, style_nr,
108             style_font.italic());
109     sci->SendScintilla(QsciScintillaBase::SCI_STYLESETUNDERLINE, style_nr,
110             style_font.underline());
111     sci->SendScintilla(QsciScintillaBase::SCI_STYLESETEOLFILLED, style_nr,
112             style_eol_fill);
113     sci->SendScintilla(QsciScintillaBase::SCI_STYLESETCASE, style_nr,
114             (long)style_case);
115     sci->SendScintilla(QsciScintillaBase::SCI_STYLESETVISIBLE, style_nr,
116             style_visible);
117     sci->SendScintilla(QsciScintillaBase::SCI_STYLESETCHANGEABLE, style_nr,
118             style_changeable);
119     sci->SendScintilla(QsciScintillaBase::SCI_STYLESETHOTSPOT, style_nr,
120             style_hotspot);
121 }
122 
123 
124 // Set the color attribute.
setColor(const QColor & color)125 void QsciStyle::setColor(const QColor &color)
126 {
127     style_color = color;
128 }
129 
130 
131 // Set the paper attribute.
setPaper(const QColor & paper)132 void QsciStyle::setPaper(const QColor &paper)
133 {
134     style_paper = paper;
135 }
136 
137 
138 // Set the font attribute.
setFont(const QFont & font)139 void QsciStyle::setFont(const QFont &font)
140 {
141     style_font = font;
142 }
143 
144 
145 // Set the eol fill attribute.
setEolFill(bool eolFill)146 void QsciStyle::setEolFill(bool eolFill)
147 {
148     style_eol_fill = eolFill;
149 }
150 
151 
152 // Set the text case attribute.
setTextCase(QsciStyle::TextCase text_case)153 void QsciStyle::setTextCase(QsciStyle::TextCase text_case)
154 {
155     style_case = text_case;
156 }
157 
158 
159 // Set the visible attribute.
setVisible(bool visible)160 void QsciStyle::setVisible(bool visible)
161 {
162     style_visible = visible;
163 }
164 
165 
166 // Set the changeable attribute.
setChangeable(bool changeable)167 void QsciStyle::setChangeable(bool changeable)
168 {
169     style_changeable = changeable;
170 }
171 
172 
173 // Set the hotspot attribute.
setHotspot(bool hotspot)174 void QsciStyle::setHotspot(bool hotspot)
175 {
176     style_hotspot = hotspot;
177 }
178 
179 
180 // Refresh the style.  Note that since we had to add apply() then this can't do
181 // anything useful so we leave it as a no-op.
refresh()182 void QsciStyle::refresh()
183 {
184 }
185