Debian Codegraph @

DebugAdapterClient/DAPWatchesView.cpp

raw
1 #include "DAPWatchesView.h"
2
3 #include "DebugAdapterClient.hpp"
4 #include "globals.h"
5 #include "ieditor.h"
6 #include "imanager.h"
7 #include "macros.h"
8
9 DAPWatchesView::DAPWatchesView(wxWindow* parent, DebugAdapterClient* plugin, clModuleLogger& log)
10 : DAPWatchesViewBase(parent)
11 , m_plugin(plugin)
12 , LOG(log)
13 {
14 m_list = new DAPVariableListCtrl(this, &m_plugin->GetClient(), dap::EvaluateContext::WATCH);
15 GetSizer()->Add(m_list, wxSizerFlags(1).Expand());
16
17 clBitmapList* bitmaps = new clBitmapList;
18 m_toolbar->AddTool(XRCID("dap-watch-new"), _("New"), bitmaps->Add("file_new"));
19 m_toolbar->AddTool(XRCID("dap-watch-delete"), _("Delete"), bitmaps->Add("file_close"));
20 m_toolbar->AddStretchableSpace();
21 m_toolbar->AddTool(XRCID("dap-watch-delete-all"), _("Delete All"), bitmaps->Add("clear"));
22 m_toolbar->AssignBitmaps(bitmaps);
23 m_toolbar->Realize();
24
25 m_toolbar->Bind(wxEVT_TOOL, &DAPWatchesView::OnNewWatch, this, XRCID("dap-watch-new"));
26 m_toolbar->Bind(wxEVT_TOOL, &DAPWatchesView::OnDeleteWatch, this, XRCID("dap-watch-delete"));
27 m_toolbar->Bind(wxEVT_TOOL, &DAPWatchesView::OnDeleteAll, this, XRCID("dap-watch-delete-all"));
28 m_toolbar->Bind(wxEVT_UPDATE_UI, &DAPWatchesView::OnDeleteAllUI, this, XRCID("dap-watch-delete-all"));
29 m_toolbar->Bind(wxEVT_UPDATE_UI, &DAPWatchesView::OnDeleteWatchUI, this, XRCID("dap-watch-delete"));
30 }
31
32 DAPWatchesView::~DAPWatchesView() {}
33
34 void DAPWatchesView::Update(int current_frame_id)
35 {
36 // update the watches
37 m_list->Begin();
38
39 wxTreeItemIdValue cookie;
40 auto item = m_list->GetFirstChild(m_list->GetRootItem(), cookie);
41 wxArrayString words;
42
43 // two loops, to avoid manipulating the tree while we are adding entries
44 while(item.IsOk()) {
45 wxString word = m_list->GetItemText(item);
46 words.Add(word);
47 item = m_list->GetNextChild(m_list->GetRootItem(), cookie);
48 }
49
50 m_list->DeleteChildren(m_list->GetRootItem());
51 for(const auto& word : words) {
52 m_plugin->GetClient().EvaluateExpression(
53 word, current_frame_id, dap::EvaluateContext::WATCH,
54 [this, word](bool success, const wxString& result, const wxString& type, int variablesReference) {
55 if(!success) {
56 m_list->AddWatch(word, wxEmptyString, wxEmptyString, 0);
57 } else {
58 m_list->AddWatch(word, result, type, variablesReference);
59 }
60 });
61 }
62 m_list->Commit();
63 }
64
65 void DAPWatchesView::OnNewWatch(wxCommandEvent& event)
66 {
67 wxUnusedVar(event);
68 auto editor = clGetManager()->GetActiveEditor();
69 CHECK_PTR_RET(editor);
70 CHECK_COND_RET(m_plugin->GetCurrentFrameId() != wxNOT_FOUND);
71
72 wxString selected_text = editor->GetSelection();
73 wxString expression = ::clGetTextFromUser(_("Add watch"), _("Expression:"), selected_text);
74 if(expression.empty()) {
75 return;
76 }
77
78 m_list->AppendItem(m_list->GetRootItem(), expression);
79 Update(m_plugin->GetCurrentFrameId());
80 }
81
82 void DAPWatchesView::OnDeleteWatch(wxCommandEvent& event)
83 {
84 wxArrayTreeItemIds items;
85 m_list->GetSelections(items);
86 m_list->Begin();
87 for(auto item : items) {
88 m_list->Delete(item);
89 }
90 m_list->Commit();
91 }
92
93 void DAPWatchesView::OnDeleteAll(wxCommandEvent& event)
94 {
95 m_list->Begin();
96 m_list->DeleteChildren(m_list->GetRootItem());
97 m_list->Commit();
98 }
99
100 void DAPWatchesView::UpdateChildren(int varId, dap::VariablesResponse* response)
101 {
102 if(!m_list) {
103 return;
104 }
105 m_list->UpdateChildren(varId, response);
106 }
107
108 void DAPWatchesView::OnDeleteAllUI(wxUpdateUIEvent& event)
109 {
110 wxTreeItemIdValue cookie;
111 auto item = m_list->GetFirstChild(m_list->GetRootItem(), cookie);
112 event.Enable(item.IsOk());
113 }
114
115 void DAPWatchesView::OnDeleteWatchUI(wxUpdateUIEvent& event)
116 {
117 wxArrayTreeItemIds items;
118 m_list->GetSelections(items);
119 event.Enable(!items.empty());
120 }