Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
syntax.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
4/* Authors:
5 * Rafael Siejakowski <rs@rs-math.net>
6 *
7 * Copyright (C) 2022 Authors
8 *
9 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
10 */
11
12#ifndef SEEN_UI_UI_SYNTAX_H
13#define SEEN_UI_UI_SYNTAX_H
14
15#include <memory>
16#include <optional>
17#include <vector>
18#include <gtkmm/textview.h>
19#include <glibmm.h>
20#include <glibmm/ustring.h>
21
22namespace Inkscape::UI::Syntax {
23
25struct Style
26{
27 std::optional<Glib::ustring> color;
28 std::optional<Glib::ustring> background;
29 uint8_t bold : 1;
30 uint8_t italic : 1;
31 uint8_t underline : 1;
32
34 : bold{false}
35 , italic{false}
36 , underline{false}
37 {}
38
39 bool isDefault() const { return !color && !background && !bold && !italic && !underline; }
40 Glib::ustring openingTag() const;
41 Glib::ustring closingTag() const;
42};
43
56
64{
65public:
66 XMLFormatter() = default;
68 : _style{styles}
69 {}
70
71 void setStyle(XMLStyles const &new_style) { _style = new_style; }
72 void setStyle(XMLStyles &&new_style) { _style = new_style; }
73
74 void openTag(char const *tag_name);
75 void addAttribute(char const *attribute_name, char const *attribute_value);
76 Glib::ustring finishTag(bool self_close = false);
77
78 Glib::ustring formatContent(char const* content, bool wrap_in_quotes = true) const;
79 Glib::ustring formatComment(char const* comment, bool wrap_in_comment_marks = true) const;
80 Glib::ustring formatProlog(char const* prolog) const { return _format(_style.prolog, prolog); }
81
82private:
83 Glib::ustring _format(Style const &style, Glib::ustring const &content) const;
84 Glib::ustring _format(Style const &style, char const *content) const
85 {
86 return style.openingTag() + Glib::Markup::escape_text(content) + style.closingTag();
87 }
88
90 Glib::ustring _wip;
91};
92
94XMLStyles build_xml_styles(const Glib::ustring& syntax_theme);
95
97enum class SyntaxMode
98{
99 PlainText,
100 InlineCss,
101 CssStyle,
104 JavaScript // JavaScript code
105};
106
109{
110public:
111 virtual ~TextEditView() = default;
112 virtual void setStyle(const Glib::ustring& theme) = 0;
113 virtual void setText(const Glib::ustring& text) = 0;
114 virtual Glib::ustring getText() const = 0;
115 virtual Gtk::TextView& getTextView() const = 0;
116
117 static std::unique_ptr<TextEditView> create(SyntaxMode mode);
118};
119
120} // namespace Inkscape::UI::Syntax
121
122#endif // SEEN_UI_UI_SYNTAX_H
123
124/*
125 Local Variables:
126 mode:c++
127 c-file-style:"stroustrup"
128 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
129 indent-tabs-mode:nil
130 fill-column:99
131 End:
132*/
133// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Base class for styled text editing widget.
Definition syntax.h:109
virtual Glib::ustring getText() const =0
virtual void setStyle(const Glib::ustring &theme)=0
virtual void setText(const Glib::ustring &text)=0
virtual Gtk::TextView & getTextView() const =0
A formatter for XML syntax, based on Pango markup.
Definition syntax.h:64
Glib::ustring _format(Style const &style, Glib::ustring const &content) const
Definition syntax.cpp:32
void openTag(char const *tag_name)
Open a new XML tag with the given tag name.
Definition syntax.cpp:78
Glib::ustring formatComment(char const *comment, bool wrap_in_comment_marks=true) const
Definition syntax.cpp:120
Glib::ustring formatProlog(char const *prolog) const
Definition syntax.h:80
XMLFormatter(XMLStyles &&styles)
Definition syntax.h:67
void setStyle(XMLStyles const &new_style)
Definition syntax.h:71
Glib::ustring _format(Style const &style, char const *content) const
Definition syntax.h:84
Glib::ustring finishTag(bool self_close=false)
Definition syntax.cpp:109
void setStyle(XMLStyles &&new_style)
Definition syntax.h:72
void addAttribute(char const *attribute_name, char const *attribute_value)
Definition syntax.cpp:101
Glib::ustring formatContent(char const *content, bool wrap_in_quotes=true) const
Definition syntax.cpp:114
XMLStyles build_xml_styles(const Glib::ustring &syntax_theme)
Build XML styles from a GTKSourceView syntax color theme.
Definition syntax.cpp:129
SyntaxMode
Syntax highlighting mode (language).
Definition syntax.h:98
@ CssStyle
File-scope CSS (contents of a CSS file or a <style> tag).
@ PlainText
Plain text (no highlighting).
@ SvgPolyPoints
Contents of the 'points' attribute of <polyline> or <polygon>.
@ SvgPathData
Contents of the 'd' attribute of the SVG <path> element.
@ InlineCss
Inline CSS (contents of a style="..." attribute).
int mode
The style of a single element in a (Pango markup)-enabled widget.
Definition syntax.h:26
bool isDefault() const
Definition syntax.h:39
std::optional< Glib::ustring > background
Definition syntax.h:28
Glib::ustring openingTag() const
Get the opening tag of the Pango markup for this style.
Definition syntax.cpp:38
std::optional< Glib::ustring > color
Definition syntax.h:27
Glib::ustring closingTag() const
Get the closing tag of Pango markup for this style.
Definition syntax.cpp:67
The styles used for simple XML syntax highlighting.
Definition syntax.h:46
std::unique_ptr< Toolbar >(* create)()
Definition toolbars.cpp:56