| 1 |
#include "DAPVariableListCtrl.hpp" |
| 2 |
|
| 3 |
#include "globals.h" |
| 4 |
#include "macros.h" |
| 5 |
|
| 6 |
#include <wx/menu.h> |
| 7 |
#include <wx/xrc/xmlres.h> |
| 8 |
|
| 9 |
#if defined(__WXMSW__) || defined(__WXMAC__) |
| 10 |
#define BORDER_STYLE wxBORDER_SIMPLE |
| 11 |
#else |
| 12 |
#define BORDER_STYLE wxBORDER_THEME |
| 13 |
#endif |
| 14 |
|
| 15 |
DAPVariableListCtrl::DAPVariableListCtrl(wxWindow* parent, dap::Client* client, dap::EvaluateContext dapContext, |
| 16 |
wxWindowID id, const wxPoint& pos, const wxSize& size) |
| 17 |
: clThemedTreeCtrl(parent, id, pos, size, BORDER_STYLE | wxTR_HIDE_ROOT | wxTR_MULTIPLE) |
| 18 |
, m_client(client) |
| 19 |
, m_dapContext(dapContext) |
| 20 |
{ |
| 21 |
SetShowHeader(false); |
| 22 |
AddHeader(_("Expression")); |
| 23 |
AddHeader(_("Value")); |
| 24 |
AddHeader(_("Type")); |
| 25 |
AddRoot(_("Variables")); |
| 26 |
|
| 27 |
Bind(wxEVT_TREE_ITEM_EXPANDING, &DAPVariableListCtrl::OnItemExpanding, this); |
| 28 |
Bind(wxEVT_TREE_ITEM_MENU, &DAPVariableListCtrl::OnMenu, this); |
| 29 |
} |
| 30 |
|
| 31 |
DAPVariableListCtrl::~DAPVariableListCtrl() {} |
| 32 |
|
| 33 |
void DAPVariableListCtrl::OnItemExpanding(wxTreeEvent& event) |
| 34 |
{ |
| 35 |
event.Skip(); |
| 36 |
auto item = event.GetItem(); |
| 37 |
CHECK_ITEM_RET(item); |
| 38 |
CHECK_COND_RET(ItemHasChildren(item)); |
| 39 |
|
| 40 |
wxTreeItemIdValue cookie; |
| 41 |
auto child = GetFirstChild(item, cookie); |
| 42 |
CHECK_ITEM_RET(child); |
| 43 |
|
| 44 |
wxString text = GetItemText(child); |
| 45 |
if(text != "<dummy>") { |
| 46 |
// already evaluated |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
// remove the fake children |
| 51 |
DeleteChildren(item); |
| 52 |
|
| 53 |
auto cd = GetItemData(item); |
| 54 |
CHECK_COND_RET(cd->refId != wxNOT_FOUND); |
| 55 |
|
| 56 |
m_client->GetChildrenVariables(cd->refId, m_dapContext); |
| 57 |
m_pending_items.insert({ cd->refId, item }); |
| 58 |
} |
| 59 |
|
| 60 |
void DAPVariableListCtrl::OnMenu(wxTreeEvent& event) |
| 61 |
{ |
| 62 |
auto item = event.GetItem(); |
| 63 |
CHECK_ITEM_RET(item); |
| 64 |
|
| 65 |
wxMenu menu; |
| 66 |
menu.Append(XRCID("dap_copy_var_value"), _("Copy")); |
| 67 |
menu.Bind( |
| 68 |
wxEVT_MENU, |
| 69 |
[this, item](wxCommandEvent& e) { |
| 70 |
wxUnusedVar(e); |
| 71 |
auto cd = GetItemData(item); |
| 72 |
CHECK_PTR_RET(cd); |
| 73 |
::CopyToClipboard(cd->value); |
| 74 |
}, |
| 75 |
XRCID("dap_copy_var_value")); |
| 76 |
PopupMenu(&menu); |
| 77 |
} |
| 78 |
|
| 79 |
DAPVariableListCtrlItemData* DAPVariableListCtrl::GetItemData(const wxTreeItemId& item) |
| 80 |
{ |
| 81 |
if(!item) { |
| 82 |
return nullptr; |
| 83 |
} |
| 84 |
|
| 85 |
auto ptr = clThemedTreeCtrl::GetItemData(item); |
| 86 |
CHECK_PTR_RET_NULL(ptr); |
| 87 |
|
| 88 |
return dynamic_cast<DAPVariableListCtrlItemData*>(ptr); |
| 89 |
} |
| 90 |
|
| 91 |
void DAPVariableListCtrl::AddWatch(const wxString& expression, const wxString& value, const wxString& type, int varRef) |
| 92 |
{ |
| 93 |
wxString root_text; |
| 94 |
auto root = GetRootItem(); |
| 95 |
auto item = AppendItem(root, expression, -1, -1, new DAPVariableListCtrlItemData(varRef, value)); |
| 96 |
SetItemText(item, value, 1); |
| 97 |
SetItemText(item, type, 2); |
| 98 |
if(varRef > 0) { |
| 99 |
AppendItem(item, "<dummy>"); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
void DAPVariableListCtrl::UpdateChildren(int varId, dap::VariablesResponse* response) |
| 104 |
{ |
| 105 |
if(m_pending_items.count(varId) == 0) { |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
wxTreeItemId item = m_pending_items[varId]; |
| 110 |
m_pending_items.erase(varId); |
| 111 |
|
| 112 |
// update the tree |
| 113 |
for(auto var : response->variables) { |
| 114 |
auto child = |
| 115 |
AppendItem(item, var.name, -1, -1, new DAPVariableListCtrlItemData(var.variablesReference, var.value)); |
| 116 |
SetItemText(child, var.value, 1); |
| 117 |
SetItemText(child, var.type, 2); |
| 118 |
|
| 119 |
if(var.variablesReference > 0) { |
| 120 |
AppendItem(child, "<dummy>"); |
| 121 |
} |
| 122 |
} |
| 123 |
Expand(item); |
| 124 |
} |