File: ctlTree.cpp

package info (click to toggle)
pgadmin3 1.20.0~beta2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 73,704 kB
  • ctags: 18,591
  • sloc: cpp: 193,786; ansic: 18,736; sh: 5,154; pascal: 1,120; yacc: 927; makefile: 516; lex: 421; xml: 126; perl: 40
file content (346 lines) | stat: -rw-r--r-- 8,222 bytes parent folder | download
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// ctlTree.cpp - wxTreeCtrl containing pgObjects
//
//////////////////////////////////////////////////////////////////////////

// wxWindows headers
#include <wx/wx.h>

// App headers
#include "pgAdmin3.h"
#include "ctl/ctlTree.h"

#include "schema/pgObject.h"
#include "schema/pgCollection.h"
#include "schema/pgServer.h"

BEGIN_EVENT_TABLE(ctlTree, wxTreeCtrl)
	EVT_CHAR(ctlTree::OnChar)
END_EVENT_TABLE()


wxTreeItemId ctlTree::FindItem(const wxTreeItemId &idParent, const wxString &prefixOrig)
{
	// match is case insensitive as this is more convenient to the user: having
	// to press Shift-letter to go to the item starting with a capital letter
	// would be too bothersome
	wxString prefix = prefixOrig.Lower();

	// determine the starting point: we shouldn't take the current item (this
	// allows to switch between two items starting with the same letter just by
	// pressing it) but we shouldn't jump to the next one if the user is
	// continuing to type as otherwise he might easily skip the item he wanted
	wxTreeItemId id = idParent;

	if ( prefix.length() == 1 )
	{
		wxCookieType cookie;
		if (HasChildren(id))
			id = GetFirstChild(id, cookie);
		else
		{
			// Try a sibling of this or ancestor instead
			wxTreeItemId p = id;
			wxTreeItemId toFind;
			do
			{
				toFind = GetNextSibling(p);
				p = GetItemParent(p);
			}
			while (p.IsOk() && !toFind.IsOk());
			id = toFind;
		}
	}

	// look for the item starting with the given prefix after it
	while ( id.IsOk() &&
	        ( ( GetItemText(id) == wxT("Dummy") && !GetItemData(id) ) ||
	          !GetItemText(id).Lower().StartsWith(prefix) ))
	{
		wxCookieType cookie;
		if ( HasChildren(id) )
			id = GetFirstChild(id, cookie);
		else
		{
			// Try a sibling of this or ancestor instead
			wxTreeItemId p = id;
			wxTreeItemId toFind;
			do
			{
				toFind = GetNextSibling(p);
				p = GetItemParent(p);
			}
			while (p.IsOk() && !toFind.IsOk());
			id = toFind;
		}
	}

	// if we haven't found anything...
	if ( !id.IsOk() )
	{
		// ... wrap to the beginning
		id = GetRootItem();
		if ( HasFlag(wxTR_HIDE_ROOT) )
		{
			wxCookieType cookie;
			// can't select virtual root
			if ( HasChildren(id) )
				id = GetFirstChild(id, cookie);
			else
			{
				// Try a sibling of this or ancestor instead
				wxTreeItemId p = id;
				wxTreeItemId toFind;
				do
				{
					toFind = GetNextSibling(p);
					p = GetItemParent(p);
				}
				while (p.IsOk() && !toFind.IsOk());
				id = toFind;
			}
		}

		// and try all the items (stop when we get to the one we started from)
		while ( id.IsOk() && id != idParent &&
		        (( GetItemText(id) == wxT("Dummy") && !GetItemData(id) ) ||
		         !GetItemText(id).Lower().StartsWith(prefix) ))
		{
			wxCookieType cookie;
			if ( HasChildren(id) )
				id = GetFirstChild(id, cookie);
			else
			{
				// Try a sibling of this or ancestor instead
				wxTreeItemId p = id;
				wxTreeItemId toFind;
				do
				{
					toFind = GetNextSibling(p);
					p = GetItemParent(p);
				}
				while (p.IsOk() && !toFind.IsOk());
				id = toFind;
			}
		}
		// If we haven't found the item, id.IsOk() will be false, as per
		// documentation
	}

	return id;
}

void ctlTree::OnChar(wxKeyEvent &event)
{
	int keyCode = event.GetKeyCode();
	if ( !event.HasModifiers() &&
	        ((keyCode >= '0' && keyCode <= '9') ||
	         (keyCode >= 'a' && keyCode <= 'z') ||
	         (keyCode >= 'A' && keyCode <= 'Z')))
	{
		wxTreeItemId currItem = GetSelection();

		if (!currItem.IsOk())
			return;

		wxTreeItemId matchItem = FindItem(currItem, m_findPrefix + (wxChar)keyCode);

		if ( matchItem.IsOk() )
		{
			EnsureVisible(matchItem);
			SelectItem(matchItem);

			m_findPrefix += (wxChar)keyCode;

			// also start the timer to reset the current prefix if the user
			// doesn't press any more alnum keys soon -- we wouldn't want
			// to use this prefix for a new item search
			if ( !m_findTimer )
			{
				m_findTimer = new ctlTreeFindTimer(this);
			}

			m_findTimer->Start(ctlTreeFindTimer::CTLTREE_DELAY, wxTIMER_ONE_SHOT);
			return;
		}
	}
	else
	{
		event.Skip(true);
	}
}

ctlTree::ctlTree(wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, long style)
	: wxTreeCtrl(parent, id, pos, size, style), m_findTimer(NULL)
{
}

ctlTree::~ctlTree()
{
	if ( m_findTimer )
		delete m_findTimer;
	m_findTimer = NULL;
}


void ctlTree::RemoveDummyChild(pgObject *obj)
{
	wxCookieType cookie;
	wxTreeItemId childItem = GetFirstChild(obj->GetId(), cookie);
	if (childItem && !GetItemData(childItem))
	{
		// The child was a dummy item, which will be replaced by the following ShowTreeDetail by true items
		Delete(childItem);
	}
}


pgObject *ctlTree::GetObject(wxTreeItemId id)
{
	if (id)
		return (pgObject *)GetItemData(id);
	return 0;
}


pgCollection *ctlTree::GetParentCollection(wxTreeItemId id)
{
	pgCollection *coll = (pgCollection *)GetParentObject(id);
	if (coll && coll->IsCollection())
		return coll;
	return 0;
}


void ctlTree::SetItemImage(const wxTreeItemId &item, int image, wxTreeItemIcon which)
{
	wxTreeCtrl::SetItemImage(item, image, which);

	wxTreeItemData *data = GetItemData(item);

	// Set the item colour
	if (data)
	{
		if (((pgObject *)data)->GetMetaType() == PGM_SERVER)
		{
			if (!((pgServer *)data)->GetColour().IsEmpty())
				SetItemBackgroundColour(item, wxColour(((pgServer *)data)->GetColour()));
		}
		else if (((pgObject *)data)->GetServer())
		{
			if (!((pgObject *)data)->GetServer()->GetColour().IsEmpty())
				SetItemBackgroundColour(item, wxColour(((pgObject *)data)->GetServer()->GetColour()));
		}
	}
}

wxTreeItemId ctlTree::AppendItem(const wxTreeItemId &parent, const wxString &text, int image, int selImage, wxTreeItemData *data)
{
	wxTreeItemId itm = wxTreeCtrl::AppendItem(parent, text, image, selImage, data);

	// Set the item colour
	if (data)
	{
		if (((pgObject *)data)->GetMetaType() == PGM_SERVER)
		{
			if (!((pgServer *)data)->GetColour().IsEmpty())
				SetItemBackgroundColour(itm, wxColour(((pgServer *)data)->GetColour()));
		}
		else if (((pgObject *)data)->GetServer())
		{
			if (!((pgObject *)data)->GetServer()->GetColour().IsEmpty())
				SetItemBackgroundColour(itm, wxColour(((pgObject *)data)->GetServer()->GetColour()));
		}
	}

	return itm;
}

wxTreeItemId ctlTree::AppendObject(pgObject *parent, pgObject *object)
{
	wxString label;
	wxTreeItemId item;

	if (object->IsCollection())
		label = object->GetTypeName();
	else
		label = object->GetDisplayName();
	item = AppendItem(parent->GetId(), label, object->GetIconId(), -1, object);
	if (object->IsCollection())
		object->ShowTreeDetail(this);
	else if (object->WantDummyChild())
		AppendItem(object->GetId(), wxT("Dummy"));

	return item;
}


pgCollection *ctlTree::AppendCollection(pgObject *parent, pgaFactory &factory)
{
	pgCollection *collection = factory.CreateCollection(parent);
	AppendObject(parent, collection);
	return collection;
}


pgObject *ctlTree::FindObject(pgaFactory &factory, wxTreeItemId parent)
{
	wxCookieType cookie;
	wxTreeItemId item = GetFirstChild(parent, cookie);
	while (item)
	{
		pgObject *obj = (pgObject *)GetItemData(item);
		if (obj && obj->IsCreatedBy(factory))
			return obj;
		item = GetNextChild(parent, cookie);
	}
	return 0;
}


pgCollection *ctlTree::FindCollection(pgaFactory &factory, wxTreeItemId parent)
{
	pgaFactory *cf = factory.GetCollectionFactory();
	if (!cf)
		return 0;

	pgCollection *collection = (pgCollection *)FindObject(*cf, parent);

	if (!collection || !collection->IsCollection())
		return 0;
	return collection;
}


//////////////////////

treeObjectIterator::treeObjectIterator(ctlTree *brow, pgObject *obj)
{
	browser = brow;
	object = obj;
}


pgObject *treeObjectIterator::GetNextObject()
{
	if (!object || !browser)
		return 0;

	if (!lastItem)
		lastItem = browser->GetFirstChild(object->GetId(), cookie);
	else
		lastItem = browser->GetNextChild(object->GetId(), cookie);

	if (lastItem)
		return browser->GetObject(lastItem);
	else
		object = 0;

	return 0;
}