File: EclipseThemeImporterBase.cpp

package info (click to toggle)
codelite 6.1.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 48,992 kB
  • ctags: 43,502
  • sloc: cpp: 334,263; ansic: 18,441; xml: 4,713; yacc: 2,653; lex: 2,449; python: 1,188; sh: 385; makefile: 40
file content (176 lines) | stat: -rw-r--r-- 6,483 bytes parent folder | download
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "EclipseThemeImporterBase.h"
#include "drawingutils.h"
#include "xmlutils.h"
#include <wx/colour.h>

EclipseThemeImporterBase::EclipseThemeImporterBase()
{
}

EclipseThemeImporterBase::~EclipseThemeImporterBase()
{
}

bool EclipseThemeImporterBase::GetProperty(const wxString& name, EclipseThemeImporterBase::Property& prop) const
{
    prop.colour = "";
    prop.isBold = false;
    prop.isItalic = false;
    if(!m_doc.IsOk())
        return false;

    wxXmlNode* child = m_doc.GetRoot()->GetChildren();
    while(child) {
        if(child->GetName() == name) {
            prop.colour = child->GetAttribute("color");
            prop.isBold = child->GetAttribute("bold", "false") == "true";
            prop.isItalic = child->GetAttribute("italic", "false") == "true";
            return true;
        }
        child = child->GetNext();
    }
    return false;
}

bool EclipseThemeImporterBase::Load(const wxFileName& eclipseXml)
{
    return m_doc.Load(eclipseXml.GetFullPath());
}

bool EclipseThemeImporterBase::IsDarkTheme() const
{
    // load the theme background colour
    EclipseThemeImporterBase::Property p;
    if(!GetProperty("background", p))
        return false;

    // test the colour
    return DrawingUtils::IsDark(p.colour);
}

wxString EclipseThemeImporterBase::GetName() const
{
    if(!IsValid())
        return "";
    return m_doc.GetRoot()->GetAttribute("name");
}

wxString EclipseThemeImporterBase::GetOutputFile(const wxString& language) const
{
    wxString name = GetName();
    name.MakeLower();

    // Normalize the file name
    name.Replace(" ", "_");
    name.Replace("::", "_");
    name.Replace("(", "_");
    name.Replace(")", "_");
    name.Replace(":", "_");
    name.Replace(",", "_");
    name.Replace(".", "_");
    name.Replace(";", "_");

    wxString xmlFileName;
    xmlFileName << "lexer_" << language.Lower() << "_" << name << ".xml";
    return xmlFileName;
}

void EclipseThemeImporterBase::AddProperty(wxXmlNode* properties,
                                           const wxString& id,
                                           const wxString& name,
                                           const wxString& colour,
                                           const wxString& bgColour,
                                           bool bold,
                                           bool italic)
{
    wxASSERT(!colour.IsEmpty());
    wxASSERT(!bgColour.IsEmpty());

    wxXmlNode* property = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, "Property");
    properties->AddChild(property);

    property->AddAttribute("Id", id);
    property->AddAttribute("Name", name);
    property->AddAttribute("Bold", bold ? "yes" : "no");
    property->AddAttribute("Italic", italic ? "yes" : "no");
    property->AddAttribute("Face", "");
    property->AddAttribute("Colour", colour);
    property->AddAttribute("BgColour", bgColour);
    property->AddAttribute("Underline", "no");
    property->AddAttribute("EolFilled", "no");
    property->AddAttribute("Alpha", "50");
    property->AddAttribute("Size", "11");
}

void EclipseThemeImporterBase::AddBaseProperties(wxXmlNode* node, const wxString& lang, const wxString& id)
{
    node->AddAttribute("Name", lang);
    node->AddAttribute("Theme", GetName());
    node->AddAttribute("IsActive", "No");
    node->AddAttribute("UseCustomTextSelFgColour", "Yes");
    node->AddAttribute("StylingWithinPreProcessor", "yes");
    node->AddAttribute("Id", id); // C++ lexer id is 3

    // Set keywords
    {
        wxXmlNode* keywords = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, "KeyWords0");
        node->AddChild(keywords);
        XmlUtils::SetNodeContent(keywords, GetKeywords0());
    }
    {
        wxXmlNode* keywords = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, "KeyWords1");
        node->AddChild(keywords);
        XmlUtils::SetNodeContent(keywords, GetKeywords1());
    }
    {
        wxXmlNode* keywords = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, "KeyWords2");
        node->AddChild(keywords);
        XmlUtils::SetNodeContent(keywords, GetKeywords2());
    }
    {
        wxXmlNode* keywords = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, "KeyWords3");
        node->AddChild(keywords);
        XmlUtils::SetNodeContent(keywords, GetKeywords3());
    }
    {
        wxXmlNode* keywords = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, "KeyWords4");
        node->AddChild(keywords);
        XmlUtils::SetNodeContent(keywords, GetKeywords4());
    }
    wxXmlNode* extensions = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, "Extensions");
    node->AddChild(extensions);
    XmlUtils::SetNodeContent(extensions, GetFileExtensions());
}

void EclipseThemeImporterBase::AddCommonProperties(wxXmlNode* propertiesNode)
{
    // Set the brace match based on the background colour
    Property background, foreground, selectionBackground, selectionForeground;

    GetProperty("background", background);
    GetProperty("foreground", foreground);
    GetProperty("selectionForeground", selectionForeground);
    GetProperty("selectionBackground", selectionBackground);
    
    wxString whitespaceColour;
    if(IsDarkTheme()) {
        // dark theme
        // Whitespace should be a bit lighether
        whitespaceColour = wxColour(background.colour).ChangeLightness(150).GetAsString(wxC2S_HTML_SYNTAX);
        AddProperty(propertiesNode, "34", "Brace match", "yellow", background.colour, true);
        AddProperty(propertiesNode, "35", "Brace bad match", "red", background.colour, true);
        AddProperty(propertiesNode, "37", "Indent Guide", whitespaceColour, background.colour);

    } else {
        // light theme
        whitespaceColour = wxColour(background.colour).ChangeLightness(50).GetAsString(wxC2S_HTML_SYNTAX);
        AddProperty(propertiesNode, "34", "Brace match", "black", "cyan", true);
        AddProperty(propertiesNode, "35", "Brace bad match", "black", "red", true);
        AddProperty(propertiesNode, "37", "Indent Guide", whitespaceColour, background.colour);
    }
    AddProperty(propertiesNode, "-1", "Fold Margin", background.colour, background.colour);
    AddProperty(propertiesNode, "-2", "Text Selection", selectionForeground.colour, selectionBackground.colour);
    AddProperty(propertiesNode, "-3", "Caret Colour", IsDarkTheme() ? "white" : "black", background.colour);
    AddProperty(propertiesNode, "-4", "Whitespace", whitespaceColour, background.colour);
    AddProperty(propertiesNode, "38", "Calltip", foreground.colour, background.colour);
}