summaryrefslogtreecommitdiff
path: root/LiteEditor/localstable.cpp
blob: 4e78db738300541419dc654a0d795f5b921c1854 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include "localstable.h"
#include <wx/wupdlock.h>
#include "debuggerconfigtool.h"
#include "globals.h"
#include "debuggermanager.h"
#include "manager.h"
#include "new_quick_watch_dlg.h"
#include <set>

#define LOCAL_NAME_COL      0
#define LOCAL_TYPE_COL      1
#define LOCAL_VALUE_COL     2
#define LOCAL_KIND_COL      3

static const wxString sKindLocalVariable   (wxT("Local Variable"));
static const wxString sKindFunctionArgument(wxT("Function Argument"));

LocalsTable::LocalsTable(wxWindow *parent)
		: LocalsTableBase(parent)
{
	m_listTable->InsertColumn(LOCAL_NAME_COL, wxT("Name"));
	m_listTable->InsertColumn(LOCAL_TYPE_COL, wxT("Type"));
	m_listTable->InsertColumn(LOCAL_VALUE_COL, wxT("Value"));
	m_listTable->InsertColumn(LOCAL_KIND_COL, wxT("Kind"));

	m_listTable->SetColumnWidth(LOCAL_NAME_COL, 200);
	m_listTable->SetColumnWidth(LOCAL_TYPE_COL, 200);
	m_listTable->SetColumnWidth(LOCAL_VALUE_COL, 200);
	m_listTable->SetColumnWidth(LOCAL_KIND_COL, 200);
}

LocalsTable::~LocalsTable()
{
}

void LocalsTable::OnItemActivated(wxListEvent& event)
{
	if ( m_choiceExpand->GetSelection() == 1 ) {
		DoShowDetails( event.m_itemIndex );
	}
}

void LocalsTable::OnItemSelected(wxListEvent& event)
{
	if ( m_choiceExpand->GetSelection() == 0 ) {
		DoShowDetails( event.m_itemIndex );
	}
	event.Skip();
}

void LocalsTable::UpdateLocals(const LocalVariables& locals)
{
	bool evaluatingLocals = true;
	IDebugger *dbgr = DebuggerMgr::Get().GetActiveDebugger();
	if( dbgr && dbgr->GetDebuggerInformation().resolveLocals == false) {
		evaluatingLocals = false;
	}

	LocalVariables vars = locals;
	// locate all items that were modified
	// this feature is disabled when 'Locals' resolving is enabled
	// due to the async nature of the debugger
	if( !evaluatingLocals ) {
		for(size_t i=0; i<vars.size(); i++){
			LocalVariable &var = vars.at(i);
			wxString      oldValue;

			// try to locate this variable in the table
			long idx = DoGetIdxByVar(var, sKindLocalVariable);
			if ( idx != wxNOT_FOUND ) {
				oldValue = GetColumnText(m_listTable, idx, LOCAL_VALUE_COL);
				var.updated = (oldValue != var.value);
			}
		}

	}

	wxWindowUpdateLocker locker ( this );
	Clear();

	for(size_t i=0; i<vars.size(); i++) {
		LocalVariable &var = vars.at(i);
		long idx = AppendListCtrlRow(m_listTable);
		SetColumnText(m_listTable, idx, LOCAL_NAME_COL,  var.name  );
		SetColumnText(m_listTable, idx, LOCAL_TYPE_COL,  var.type  );
		SetColumnText(m_listTable, idx, LOCAL_KIND_COL,  sKindLocalVariable );
		// If this variable has an "inline" value, dont display the row data
		if ( !DoShowInline(var, idx) ) {
			SetColumnText(m_listTable, idx, LOCAL_VALUE_COL, var.value );
			if ( var.updated && evaluatingLocals == false ) {
				m_listTable->SetItemTextColour(idx, wxT("RED"));
			}
		}
	}
}


void LocalsTable::UpdateFuncArgs(const LocalVariables& args)
{
	bool evaluatingLocals = true;
	IDebugger *dbgr = DebuggerMgr::Get().GetActiveDebugger();
	if( dbgr && dbgr->GetDebuggerInformation().resolveLocals == false) {
		evaluatingLocals = false;
	}
	LocalVariables vars = args;

	// locate all items that were modified
	// this feature is disabled when 'Locals' resolving is enabled
	// due to the async nature of the debugger
	if( !evaluatingLocals ) {
		for(size_t i=0; i<vars.size(); i++){
			LocalVariable &var = vars.at(i);
			wxString      oldValue;

			// try to locate this variable in the table
			long idx = DoGetIdxByVar(var, sKindFunctionArgument);
			if ( idx != wxNOT_FOUND ) {
				oldValue = GetColumnText(m_listTable, idx, LOCAL_VALUE_COL);
				var.updated = (oldValue != var.value);
			}
		}
	}

	wxWindowUpdateLocker locker ( this );

	// Delete all function arguments from the table
	for(int i=0; i<m_listTable->GetItemCount(); i++){
		if(GetColumnText(m_listTable, i, LOCAL_KIND_COL) == sKindFunctionArgument) {
			m_listTable->DeleteItem(i);
		}
	}

	for(size_t i=0; i<vars.size(); i++) {
		LocalVariable &var = vars.at(i);
		long idx = AppendListCtrlRow(m_listTable);
		SetColumnText(m_listTable, idx, LOCAL_NAME_COL,  var.name  );
		SetColumnText(m_listTable, idx, LOCAL_TYPE_COL,  var.type  );
		SetColumnText(m_listTable, idx, LOCAL_KIND_COL,  sKindFunctionArgument );

		if ( !DoShowInline(var, idx) ) {
			SetColumnText(m_listTable, idx, LOCAL_VALUE_COL, var.value );
			if ( var.updated && evaluatingLocals == false ) {
				m_listTable->SetItemTextColour(idx, wxT("RED"));
			}
		}
	}
}

void LocalsTable::DoShowDetails(long item)
{
	IDebugger *dbgr = DebuggerMgr::Get().GetActiveDebugger();
	long sel = m_listTable->GetFirstSelected();
	if( sel != wxNOT_FOUND ){
		wxString name = GetColumnText(m_listTable, sel, LOCAL_NAME_COL);
		if( dbgr && dbgr->IsRunning() && ManagerST::Get()->DbgCanInteract() ) {

			if ( ManagerST::Get()->GetDebuggerTip()->IsShown() ) {
				ManagerST::Get()->GetDebuggerTip()->HideDialog();
			}

			dbgr->CreateVariableObject(name, DBG_USERR_LOCALS);
		}
	}
}

long LocalsTable::DoGetIdxByName(const wxString& name)
{
	for( int i=0; i<m_listTable->GetItemCount(); i++){
		if(GetColumnText(m_listTable, i, LOCAL_NAME_COL) == name) {
			return i;
		}
	}
	return wxNOT_FOUND;
}

void LocalsTable::Clear()
{
	m_listTable->DeleteAllItems();
	m_expression2Idx.clear();
}

void LocalsTable::Initialize()
{
	// Read the debugger defined commands
	DebuggerSettingsData data;
	DebuggerConfigTool::Get()->ReadObject(wxT("DebuggerCommands"), &data);
	m_dbgCmds = data.GetCmds();
}

wxString LocalsTable::GetRealType(const wxString& gdbType)
{
	wxString realType ( gdbType );
	realType.Replace(wxT("*"), wxT(""));
	realType.Replace(wxT("const"), wxT(""));
	realType.Replace(wxT("&"), wxT(""));

	realType.Trim().Trim(false);
	return realType;
}

long LocalsTable::DoGetIdxByVar(const LocalVariable& var, const wxString& kind)
{
	for( int i=0; i<m_listTable->GetItemCount(); i++){
		if( GetColumnText(m_listTable, i, LOCAL_NAME_COL) == var.name &&
			GetColumnText(m_listTable, i, LOCAL_KIND_COL) == kind)
		{
			return i;
		}
	}
	return wxNOT_FOUND;
}

bool LocalsTable::DoShowInline(const LocalVariable& var, long item)
{
	IDebugger *dbgr = DebuggerMgr::Get().GetActiveDebugger();
	if( dbgr && dbgr->GetDebuggerInformation().resolveLocals == false) {
		return false;
	}

	wxString realType = GetRealType( var.type );
	for(size_t i=0; i<m_dbgCmds.size(); i++) {
		DebuggerCmdData dcd = m_dbgCmds.at(i);
		if(dcd.GetName() == realType) {
			// Create variable object for this variable
			// and display the content
			wxString expression = dcd.GetCommand();
			expression.Replace(wxT("$(Variable)"), var.name);
			if( dbgr && dbgr->IsRunning() && ManagerST::Get()->DbgCanInteract() ) {
				dbgr->CreateVariableObject(expression, DBG_USERR_LOCALS_INLINE);
				m_expression2Idx[expression] = item;
				return true;
			}
		}
	}
	return false;
}

void LocalsTable::UpdateInline(const DebuggerEvent& event)
{
	wxString key = event.m_expression;
	std::map<wxString, long>::iterator iter = m_expression2Idx.find(key);
	if(iter != m_expression2Idx.end()){
		long idx = iter->second;
		SetColumnText(m_listTable, idx, LOCAL_VALUE_COL, event.m_evaluated);
	}

	IDebugger *dbgr = DebuggerMgr::Get().GetActiveDebugger();
	if(dbgr && dbgr->IsRunning() && ManagerST::Get()->DbgCanInteract()) {
		dbgr->DeleteVariableObject(event.m_variableObject.gdbId);
	}
}