forked from eranif/codelite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvnblameeditor.cpp
169 lines (137 loc) · 5.5 KB
/
svnblameeditor.cpp
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
#include "svnblameeditor.h"
#include <wx/menu.h>
#include <wx/xrc/xmlres.h>
#include "drawingutils.h"
#include <map>
#include <wx/tokenzr.h>
#include <wx/font.h>
#include <wx/settings.h>
BEGIN_EVENT_TABLE(SvnBlameEditor, wxStyledTextCtrl)
EVT_CONTEXT_MENU(SvnBlameEditor::OnContextMenu)
END_EVENT_TABLE()
SvnBlameEditor::SvnBlameEditor(wxWindow *win)
: wxStyledTextCtrl(win)
{
Initialize();
}
SvnBlameEditor::~SvnBlameEditor()
{
}
void SvnBlameEditor::Initialize()
{
// Initialize some styles
StyleClearAll();
SetLexer(wxSTC_LEX_NULL);
wxFont defFont = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
wxFont font(defFont.GetPointSize(), wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
for (int i=0; i<=wxSTC_STYLE_DEFAULT; i++) {
StyleSetBackground(i, wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
StyleSetForeground(i, *wxBLACK);
StyleSetFont(i, font);
}
SetMarginType (0, wxSTC_MARGIN_TEXT );
SetMarginType (1, wxSTC_MARGIN_NUMBER);
SetMarginWidth(1, 4 + 5*TextWidth(wxSTC_STYLE_LINENUMBER, wxT("9")));
SetMarginWidth(2, 0);
SetMarginWidth(3, 0);
SetMarginWidth(4, 0);
SetTabWidth(4);
// Define some colors to use
StyleSetBackground(1, DrawingUtils::LightColour(wxT("GREEN"), 7.0));
StyleSetBackground(2, DrawingUtils::LightColour(wxT("BLUE"), 7.0));
StyleSetBackground(3, DrawingUtils::LightColour(wxT("ORANGE"), 7.0));
StyleSetBackground(4, DrawingUtils::LightColour(wxT("YELLOW"), 7.0));
StyleSetBackground(5, DrawingUtils::LightColour(wxT("PURPLE"), 7.0));
StyleSetBackground(6, DrawingUtils::LightColour(wxT("RED"), 7.0));
StyleSetBackground(7, DrawingUtils::LightColour(wxT("BROWN"), 7.0));
StyleSetBackground(8, DrawingUtils::LightColour(wxT("LIGHT GREY"), 7.0));
StyleSetBackground(9, DrawingUtils::LightColour(wxT("SIENNA"), 7.0));
StyleSetBackground(10, wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT));
StyleSetForeground(10, wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT));
}
void SvnBlameEditor::SetText(const wxString& text)
{
// Define some colors
int xx, yy;
int marginWidth(0);
int s_style(1);
std::map<wxString, int> authorsColorsMap;
wxFont defFont = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
wxFont font(defFont.GetPointSize(), wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
wxArrayString lines = wxStringTokenize(text, wxT("\n"), wxTOKEN_RET_DELIMS);
for (size_t i=0; i<lines.GetCount(); i++) {
wxString revision;
wxString author;
wxString text;
wxString line = lines.Item(i);
line.Trim(false);
revision = line.BeforeFirst(wxT(' '));
revision.Trim().Trim(false);
line = line.AfterFirst(wxT(' '));
line.Trim(false);
author = line.BeforeFirst(wxT(' '));
author.Trim().Trim(false);
int style;
std::map<wxString, int>::iterator iter = authorsColorsMap.find(author);
if (iter != authorsColorsMap.end()) {
// Create new random color and use it
style = (*iter).second;
} else {
style = s_style;
s_style ++;
if (s_style > 9)
s_style = 1;
authorsColorsMap[author] = style;
}
line = line.AfterFirst(wxT(' '));
wxString marginText = wxString::Format(wxT("% 8s: %s"), revision.c_str(), author.c_str());
wxWindow::GetTextExtent(marginText, &xx, &yy, NULL, NULL, &font);
marginWidth = wxMax(marginWidth, xx);
AppendText(line);
MarginSetText((int)i, marginText);
MarginSetStyle((int)i, style);
// Keep the revision on in array
BlameLineInfo info;
info.revision = revision;
info.style = style;
m_lineInfo.push_back(info);
}
SetMarginWidth(0, marginWidth);
SetReadOnly(true);
}
void SvnBlameEditor::OnContextMenu(wxContextMenuEvent& event)
{
wxPoint pt = event.GetPosition();
wxPoint clientPt = ScreenToClient(pt);
int margin = GetMarginWidth(0); // get the margin width
if ( clientPt.x < margin ) {
GotoPos( PositionFromPoint(clientPt) );
// Margin context menu
wxMenu menu;
menu.Append( XRCID("svn_highlight_revision"), _("Highlight this revision"), _("Highlight this revision"), false);
menu.Connect(XRCID("svn_highlight_revision"), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(SvnBlameEditor::OnHighlightRevision), NULL, this);
PopupMenu(&menu);
} else {
wxStyledTextCtrl::OnContextMenu(event);
}
}
void SvnBlameEditor::OnHighlightRevision(wxCommandEvent& event)
{
int line = GetCurrentLine();
if(line < (int)m_lineInfo.size() && line >= 0) {
BlameLineInfo info = m_lineInfo.at(line);
wxString revision = info.revision;
// Loop over the lines and adjust the styles
for(size_t i=0; i<m_lineInfo.size(); i++) {
BlameLineInfo lineInfo = m_lineInfo.at(i);
if(lineInfo.revision == revision) {
// Highlight this line
MarginSetStyle((int)i, 10);
} else {
// restore old settings
MarginSetStyle((int)i, lineInfo.style);
}
}
Colourise(0, -1);
}
}