| 1 |
#include "DAPTextView.h" |
| 2 |
|
| 3 |
#include "ColoursAndFontsManager.h" |
| 4 |
#include "bookmark_manager.h" |
| 5 |
#include "editor_config.h" |
| 6 |
#include "event_notifier.h" |
| 7 |
#include "globals.h" |
| 8 |
#include "imanager.h" |
| 9 |
|
| 10 |
namespace |
| 11 |
{ |
| 12 |
constexpr int NUMBER_MARGIN_ID = 0; |
| 13 |
constexpr int EDIT_TRACKER_MARGIN_ID = 1; |
| 14 |
constexpr int SYMBOLS_MARGIN_ID = 2; |
| 15 |
constexpr int SYMBOLS_MARGIN_SEP_ID = 3; |
| 16 |
constexpr int FOLD_MARGIN_ID = 4; |
| 17 |
} // namespace |
| 18 |
|
| 19 |
DAPTextView::DAPTextView(wxWindow* parent) |
| 20 |
: DAPTextViewBase(parent) |
| 21 |
{ |
| 22 |
MSWSetWindowDarkTheme(this); |
| 23 |
EventNotifier::Get()->Bind(wxEVT_SYS_COLOURS_CHANGED, &DAPTextView::OnColourChanged, this); |
| 24 |
m_stcTextView->Bind(wxEVT_STC_MARGINCLICK, &DAPTextView::OnMarginClick, this); |
| 25 |
|
| 26 |
m_stcTextView->SetEditable(false); |
| 27 |
ApplyTheme(); |
| 28 |
|
| 29 |
// define the debugger marker |
| 30 |
auto options = EditorConfigST::Get()->GetOptions(); |
| 31 |
if(options->HasOption(OptionsConfig::Opt_Mark_Debugger_Line)) { |
| 32 |
m_stcTextView->MarkerDefine(smt_indicator, wxSTC_MARK_BACKGROUND, wxNullColour, |
| 33 |
options->GetDebuggerMarkerLine()); |
| 34 |
m_stcTextView->MarkerSetAlpha(smt_indicator, 50); |
| 35 |
|
| 36 |
} else { |
| 37 |
m_stcTextView->MarkerDefine(smt_indicator, wxSTC_MARK_SHORTARROW); |
| 38 |
wxColour debuggerMarkerColour(136, 170, 0); |
| 39 |
m_stcTextView->MarkerSetBackground(smt_indicator, debuggerMarkerColour); |
| 40 |
m_stcTextView->MarkerSetForeground(smt_indicator, debuggerMarkerColour.ChangeLightness(50)); |
| 41 |
} |
| 42 |
|
| 43 |
// margins |
| 44 |
// line numbers |
| 45 |
m_stcTextView->SetMarginType(NUMBER_MARGIN_ID, wxSTC_MARGIN_NUMBER); |
| 46 |
|
| 47 |
// symbol margin |
| 48 |
m_stcTextView->SetMarginType(SYMBOLS_MARGIN_ID, wxSTC_MARGIN_SYMBOL); |
| 49 |
m_stcTextView->SetMarginWidth(SYMBOLS_MARGIN_ID, FromDIP(16)); |
| 50 |
|
| 51 |
// breakpoints |
| 52 |
m_stcTextView->MarkerDefine(smt_breakpoint, wxSTC_MARK_CIRCLE); |
| 53 |
m_stcTextView->MarkerSetBackground(smt_breakpoint, "RED"); |
| 54 |
m_stcTextView->MarkerSetAlpha(smt_breakpoint, 30); |
| 55 |
} |
| 56 |
|
| 57 |
DAPTextView::~DAPTextView() |
| 58 |
{ |
| 59 |
EventNotifier::Get()->Unbind(wxEVT_SYS_COLOURS_CHANGED, &DAPTextView::OnColourChanged, this); |
| 60 |
m_stcTextView->Unbind(wxEVT_STC_MARGINCLICK, &DAPTextView::OnMarginClick, this); |
| 61 |
} |
| 62 |
|
| 63 |
void DAPTextView::OnColourChanged(clCommandEvent& event) |
| 64 |
{ |
| 65 |
event.Skip(); |
| 66 |
ApplyTheme(); |
| 67 |
} |
| 68 |
|
| 69 |
void DAPTextView::ApplyTheme() |
| 70 |
{ |
| 71 |
wxString file_name = "file.text"; |
| 72 |
if(!m_current_source.path.empty()) { |
| 73 |
file_name = wxFileName(m_current_source.path).GetFullName(); |
| 74 |
} else if(!m_mimeType.empty()) { |
| 75 |
// try the mime type |
| 76 |
if(m_mimeType == "text/x-lldb.disassembly") { |
| 77 |
file_name = "file.asm"; // assembly |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
auto lexer = ColoursAndFontsManager::Get().GetLexerForFile(file_name); |
| 82 |
lexer->Apply(m_stcTextView); |
| 83 |
} |
| 84 |
|
| 85 |
void DAPTextView::ClearMarker(wxStyledTextCtrl* ctrl) { ctrl->MarkerDeleteAll(smt_indicator); } |
| 86 |
|
| 87 |
void DAPTextView::SetMarker(wxStyledTextCtrl* ctrl, int line_number) |
| 88 |
{ |
| 89 |
ctrl->MarkerDeleteAll(smt_indicator); |
| 90 |
ctrl->MarkerAdd(line_number, smt_indicator); |
| 91 |
int caretPos = ctrl->PositionFromLine(line_number); |
| 92 |
ctrl->SetSelection(caretPos, caretPos); |
| 93 |
ctrl->SetCurrentPos(caretPos); |
| 94 |
ctrl->EnsureCaretVisible(); |
| 95 |
} |
| 96 |
|
| 97 |
void DAPTextView::Clear() |
| 98 |
{ |
| 99 |
m_stcTextView->MarkerDeleteAll(smt_indicator); |
| 100 |
m_stcTextView->ClearAll(); |
| 101 |
m_filepath.clear(); |
| 102 |
} |
| 103 |
|
| 104 |
void DAPTextView::SetText(const dap::Source& source, const wxString& text, const wxString& path, |
| 105 |
const wxString& mimeType) |
| 106 |
{ |
| 107 |
m_stcTextView->SetEditable(true); |
| 108 |
m_stcTextView->SetText(text); |
| 109 |
m_stcTextView->SetEditable(false); |
| 110 |
m_current_source = source; |
| 111 |
m_mimeType = mimeType; |
| 112 |
SetFilePath(path); |
| 113 |
UpdateLineNumbersMargin(); |
| 114 |
ApplyTheme(); |
| 115 |
} |
| 116 |
|
| 117 |
void DAPTextView::LoadFile(const dap::Source& source, const wxString& filepath) |
| 118 |
{ |
| 119 |
m_stcTextView->SetEditable(true); |
| 120 |
bool ok = m_stcTextView->LoadFile(filepath); |
| 121 |
m_stcTextView->SetEditable(false); |
| 122 |
m_mimeType.clear(); |
| 123 |
|
| 124 |
if(ok) { |
| 125 |
SetFilePath(filepath); |
| 126 |
m_current_source = source; |
| 127 |
} else { |
| 128 |
Clear(); |
| 129 |
} |
| 130 |
|
| 131 |
UpdateLineNumbersMargin(); |
| 132 |
ApplyTheme(); |
| 133 |
} |
| 134 |
|
| 135 |
void DAPTextView::SetFilePath(const wxString& filepath) |
| 136 |
{ |
| 137 |
m_filepath = filepath; |
| 138 |
auto book = clGetManager()->GetMainNotebook(); |
| 139 |
int where = book->FindPage(this); |
| 140 |
if(where == wxNOT_FOUND) { |
| 141 |
return; |
| 142 |
} |
| 143 |
book->SetPageText(where, "[dap]: " + m_filepath); |
| 144 |
} |
| 145 |
|
| 146 |
void DAPTextView::UpdateLineNumbersMargin() |
| 147 |
{ |
| 148 |
int newLineCount = m_stcTextView->GetLineCount(); |
| 149 |
int newWidthCount = log10(newLineCount) + 2; |
| 150 |
int size = FromDIP(newWidthCount * m_stcTextView->TextWidth(wxSTC_STYLE_LINENUMBER, "X")); |
| 151 |
m_stcTextView->SetMarginWidth(NUMBER_MARGIN_ID, size); |
| 152 |
} |
| 153 |
|
| 154 |
void DAPTextView::OnMarginClick(wxStyledTextEvent& event) |
| 155 |
{ |
| 156 |
int nLine = m_stcTextView->LineFromPosition(event.GetPosition()); |
| 157 |
|
| 158 |
switch(event.GetMargin()) { |
| 159 |
case SYMBOLS_MARGIN_ID: |
| 160 |
if(HasBreakpointMarker(nLine)) { |
| 161 |
DeleteBreakpointMarkers(nLine); |
| 162 |
} else { |
| 163 |
SetBreakpointMarker(nLine, wxEmptyString); |
| 164 |
} |
| 165 |
break; |
| 166 |
default: |
| 167 |
break; |
| 168 |
} |
| 169 |
|
| 170 |
// TODO: let the plugin know that it needs to apply breakpoints for this source |
| 171 |
} |
| 172 |
|
| 173 |
bool DAPTextView::HasBreakpointMarker(int line_number) |
| 174 |
{ |
| 175 |
int markers_bit_mask = m_stcTextView->MarkerGet(line_number); |
| 176 |
int mask = (1 << smt_breakpoint); |
| 177 |
return markers_bit_mask & mask; |
| 178 |
} |
| 179 |
|
| 180 |
size_t DAPTextView::GetBreakpointMarkers(std::vector<int>* lines) |
| 181 |
{ |
| 182 |
int mask = (1 << smt_breakpoint); |
| 183 |
int line = m_stcTextView->MarkerNext(0, mask); |
| 184 |
while(line != wxNOT_FOUND) { |
| 185 |
lines->push_back(line); |
| 186 |
line = m_stcTextView->MarkerNext(line + 1, mask); |
| 187 |
} |
| 188 |
return lines->size(); |
| 189 |
} |
| 190 |
|
| 191 |
void DAPTextView::DeleteBreakpointMarkers(int line_number) |
| 192 |
{ |
| 193 |
// get a list of lines to work on |
| 194 |
std::vector<int> lines; |
| 195 |
if(line_number == wxNOT_FOUND) { |
| 196 |
GetBreakpointMarkers(&lines); |
| 197 |
} else { |
| 198 |
lines.push_back(line_number); |
| 199 |
} |
| 200 |
|
| 201 |
for(int line : lines) { |
| 202 |
m_stcTextView->MarkerDelete(line, smt_breakpoint); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
void DAPTextView::SetBreakpointMarker(int line_number, const wxString& tooltip) |
| 207 |
{ |
| 208 |
if(HasBreakpointMarker(line_number)) { |
| 209 |
return; |
| 210 |
} |
| 211 |
m_stcTextView->MarkerAdd(line_number, smt_breakpoint); |
| 212 |
} |