~ubuntu-branches/ubuntu/raring/codeblocks/raring-proposed

« back to all changes in this revision

Viewing changes to src/plugins/codecompletion/classbrowserbuilderthread.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cosme Domínguez Díaz
  • Date: 2010-08-09 04:38:38 UTC
  • mfrom: (1.1.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20100809043838-a59ygguym4eg0jgw
Tags: 10.05-0ubuntu1
* New upstream release. Closes (LP: #322350)
 - Switch to dpkg-source 3.0 (quilt) format
 - Remove unneeded README.source
 - Add debian/get-source-orig script that removes all
   Windows prebuilt binaries
* Bump Standards-Version to 3.9.1
 - Stop shipping *.la files
* debian/control
 - Add cdbs package as Build-Depend
 - Add libbz2-dev and zlib1g-dev packages as
   Build-Depends (needed by libhelp_plugin.so)
 - Remove dpatch package of Build-Depends
 - Add codeblocks-contrib-debug package
 - Split architecture-independent files of codeblocks
   package in codeblocks-common package
* debian/rules
 - Switch to CDBS rules system
 - Add parallel build support
 - Add a call to debian/get-source-orig script
 - Use lzma compression (saves 23,5 MB of free space)
* debian/patches
 - Refresh 01_codeblocks_plugin_path
 - Add 02_no_Makefiles_in_debian_dir to remove any link
   in codeblocks build system to deleted Makefiles of debian directory
 - Drop 02_ftbfs_gcc44 and 03_ftbfs_glib221 (merged in upstream)
* debian/watch
 - Update to use the new host (berlios.de)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
3
 
 * http://www.gnu.org/licenses/gpl-3.0.html
4
 
 *
5
 
 * $Revision$
6
 
 * $Id$
7
 
 * $HeadURL$
8
 
 */
9
 
 
10
 
#include <sdk.h>
11
 
#include "classbrowserbuilderthread.h"
12
 
#include <globals.h>
13
 
#include <manager.h>
14
 
#include <projectmanager.h>
15
 
#include <cbproject.h>
16
 
 
17
 
#include <wx/settings.h>
18
 
#include <wx/utils.h>
19
 
 
20
 
#include <algorithm>
21
 
 
22
 
namespace compatibility { typedef TernaryCondTypedef<wxMinimumVersion<2,5>::eval, wxTreeItemIdValue, long int>::eval tree_cookie_t; };
23
 
 
24
 
ClassBrowserBuilderThread::ClassBrowserBuilderThread(wxSemaphore& sem, ClassBrowserBuilderThread** threadVar)
25
 
    : wxThread(wxTHREAD_JOINABLE),
26
 
    m_Semaphore(sem),
27
 
    m_pParser(0),
28
 
    m_pTreeTop(0),
29
 
    m_pTreeBottom(0),
30
 
    m_pUserData(0),
31
 
    m_Options(),
32
 
    m_pTokens(0),
33
 
    m_ppThreadVar(threadVar)
34
 
{
35
 
    //ctor
36
 
}
37
 
 
38
 
ClassBrowserBuilderThread::~ClassBrowserBuilderThread()
39
 
{
40
 
    //dtor
41
 
}
42
 
 
43
 
void ClassBrowserBuilderThread::Init(Parser* parser,
44
 
                                    wxTreeCtrl* treeTop,
45
 
                                    wxTreeCtrl* treeBottom,
46
 
                                    const wxString& active_filename,
47
 
                                    void* user_data, // active project
48
 
                                    const BrowserOptions& options,
49
 
                                    TokensTree* pTokens,
50
 
                                    bool build_tree)
51
 
{
52
 
    wxMutexLocker lock(m_BuildMutex);
53
 
    m_pParser = parser;
54
 
    m_pTreeTop = treeTop;
55
 
    m_pTreeBottom = treeBottom;
56
 
    m_ActiveFilename = active_filename;
57
 
    m_pUserData = user_data;
58
 
    m_Options = options;
59
 
    m_pTokens = pTokens;
60
 
 
61
 
    m_CurrentFileSet.clear();
62
 
    m_CurrentTokenSet.clear();
63
 
 
64
 
    TokensTree* tree = m_pParser->GetTokens();
65
 
    // fill filter set for current-file-filter
66
 
    if (m_Options.displayFilter == bdfFile && !m_ActiveFilename.IsEmpty())
67
 
    {
68
 
        // m_ActiveFilename is the full filename up to the extension dot. No extension though.
69
 
        // get all filenames' indices matching our mask
70
 
        tree->m_FilenamesMap.FindMatches(m_ActiveFilename, m_CurrentFileSet, true, true);
71
 
    }
72
 
 
73
 
    if(m_Options.displayFilter == bdfProject && (user_data != 0))
74
 
    {
75
 
        cbProject* prj = (cbProject*)user_data;
76
 
        for(int i = 0; i < prj->GetFilesCount(); i++)
77
 
        {
78
 
            ProjectFile* curfile = prj->GetFile(i);
79
 
            if(!curfile)
80
 
                continue;
81
 
            wxString filename = curfile->file.GetFullPath();
82
 
            size_t fileIdx = tree->m_FilenamesMap.GetItemNo(filename);
83
 
            if(fileIdx)
84
 
            {
85
 
                m_CurrentFileSet.insert(fileIdx);
86
 
            }
87
 
        }
88
 
    }
89
 
 
90
 
    if (!m_CurrentFileSet.empty())
91
 
    {
92
 
        m_CurrentTokenSet.clear();
93
 
        m_CurrentGlobalTokensSet.clear();
94
 
        for(TokenFilesSet::iterator it = m_CurrentFileSet.begin();it != m_CurrentFileSet.end(); it++)
95
 
        {
96
 
            TokenIdxSet* curset = &(tree->m_FilesMap[*it]);
97
 
            for(TokenIdxSet::iterator it2 = curset->begin(); it2 != curset->end(); it2++)
98
 
            {
99
 
                Token* curtoken = tree->at(*it2);
100
 
                if(curtoken)
101
 
                {
102
 
                    m_CurrentTokenSet.insert(*it2);
103
 
                    if(curtoken->m_ParentIndex == -1)
104
 
                        m_CurrentGlobalTokensSet.insert(*it2);
105
 
                }
106
 
            }
107
 
        }
108
 
    }
109
 
 
110
 
    if (build_tree)
111
 
    {
112
 
        BuildTree();
113
 
    }
114
 
}
115
 
 
116
 
void* ClassBrowserBuilderThread::Entry()
117
 
{
118
 
    while (!TestDestroy() && !Manager::IsAppShuttingDown())
119
 
    {
120
 
        // wait until the classbrowser signals
121
 
        m_Semaphore.Wait();
122
 
//        Manager::Get()->GetLogManager()->DebugLog(F(_T(" - - - - - -")));
123
 
 
124
 
        if (TestDestroy() || Manager::IsAppShuttingDown())
125
 
            break;
126
 
 
127
 
        if(platform::gtk)
128
 
        {
129
 
            // this code (PART 1/2) seems to be good on linux
130
 
            // because of it the libcairo crash on dualcore processors
131
 
            // is gone, but on windows it has very bad influence,
132
 
            // henceforth the ifdef guard
133
 
            // the questions remains if it is the correct solution
134
 
            if(!::wxIsMainThread())
135
 
            {
136
 
                ::wxMutexGuiEnter();
137
 
            }
138
 
        }
139
 
 
140
 
        BuildTree();
141
 
 
142
 
        if (TestDestroy() || Manager::IsAppShuttingDown())
143
 
        {
144
 
            if(platform::gtk)
145
 
            {
146
 
                if(!::wxIsMainThread())
147
 
                {
148
 
                    ::wxMutexGuiLeave();
149
 
                }
150
 
            }
151
 
            break;
152
 
        }
153
 
 
154
 
                m_pTreeTop->Freeze();
155
 
        ExpandNamespaces(m_pTreeTop->GetRootItem());
156
 
            m_pTreeTop->Thaw();
157
 
 
158
 
        if(platform::gtk)
159
 
        {
160
 
            // this code (PART 2/2) seems to be good on linux
161
 
            // because of it the libcairo crash on dualcore processors
162
 
            // is gone, but on windows it has very bad influence,
163
 
            // henceforth the ifdef guard
164
 
            // the questions remains if it is the correct solution
165
 
            if(!::wxIsMainThread())
166
 
            {
167
 
                ::wxMutexGuiLeave();
168
 
            }
169
 
        }
170
 
    }
171
 
 
172
 
    m_pParser = 0;
173
 
    m_pTreeTop = 0;
174
 
    m_pTreeBottom = 0;
175
 
 
176
 
    /*if (m_ppThreadVar)
177
 
        *m_ppThreadVar = 0;*/
178
 
    return 0;
179
 
}
180
 
 
181
 
void ClassBrowserBuilderThread::ExpandNamespaces(wxTreeItemId node)
182
 
{
183
 
        if (!m_Options.expandNS || !node.IsOk())
184
 
                return;
185
 
 
186
 
        wxTreeItemIdValue enumerationCookie;
187
 
    wxTreeItemId existing = m_pTreeTop->GetFirstChild(node, enumerationCookie);
188
 
    while (existing.IsOk())
189
 
    {
190
 
                CBTreeData* data = (CBTreeData*)m_pTreeTop->GetItemData(existing);
191
 
                if (data && data->m_pToken && data->m_pToken->m_TokenKind == tkNamespace)
192
 
                {
193
 
//                      Manager::Get()->GetLogManager()->DebugLog(F(_T("Auto-expanding: ") + data->m_pToken->m_Name));
194
 
                        m_pTreeTop->Expand(existing);
195
 
                        ExpandNamespaces(existing); // recurse
196
 
                }
197
 
 
198
 
        existing = m_pTreeTop->GetNextSibling(existing);
199
 
        }
200
 
}
201
 
 
202
 
void ClassBrowserBuilderThread::BuildTree()
203
 
{
204
 
    if (Manager::IsAppShuttingDown())
205
 
        return;
206
 
//    wxMutexLocker lock(m_BuildMutex);
207
 
 
208
 
    m_pTreeTop->SetImageList(m_pParser->GetImageList());
209
 
    m_pTreeBottom->SetImageList(m_pParser->GetImageList());
210
 
 
211
 
    wxTreeItemId root = m_pTreeTop->GetRootItem();
212
 
    if (!root.IsOk())
213
 
    {
214
 
        root = m_pTreeTop->AddRoot(_("Symbols"), PARSER_IMG_SYMBOLS_FOLDER, PARSER_IMG_SYMBOLS_FOLDER, new CBTreeData(sfRoot));
215
 
        m_pTreeTop->SetItemHasChildren(root);
216
 
    }
217
 
 
218
 
    m_pTreeTop->Hide();
219
 
    m_pTreeBottom->Hide();
220
 
    m_pTreeTop->Freeze();
221
 
    m_pTreeBottom->Freeze();
222
 
 
223
 
        RemoveInvalidNodes(m_pTreeTop, root);
224
 
        RemoveInvalidNodes(m_pTreeBottom, m_pTreeBottom->GetRootItem());
225
 
 
226
 
    if (!TestDestroy() && !Manager::IsAppShuttingDown())
227
 
    {
228
 
        // the tree is completely dynamic: it is populated when a node expands/collapses.
229
 
        // so, by expanding the root node, we already instruct it to fill the top level :)
230
 
        //
231
 
        // this technique makes it really fast to draw (we only draw what's expanded) and
232
 
        // has very minimum memory overhead since it contains as few items as possible.
233
 
        // plus, it doesn't flicker because we 're not emptying it and re-creating it each time ;)
234
 
        m_pTreeTop->Expand(root);
235
 
 
236
 
        if(platform::gtk)
237
 
        {
238
 
            // seems like the "expand" event comes too late in wxGTK,
239
 
            // so make it happen now
240
 
            ExpandItem(root);
241
 
        }
242
 
    }
243
 
 
244
 
    m_pTreeBottom->Thaw();
245
 
    m_pTreeTop->Thaw();
246
 
    m_pTreeBottom->Show();
247
 
    m_pTreeTop->Show();
248
 
 
249
 
    SelectNode(m_pTreeTop->GetSelection()); // refresh selection
250
 
}
251
 
 
252
 
#if 1
253
 
void ClassBrowserBuilderThread::RemoveInvalidNodes(wxTreeCtrl* tree, wxTreeItemId parent)
254
 
{
255
 
    if (TestDestroy() || Manager::IsAppShuttingDown() || (!(parent.IsOk())))
256
 
        return;
257
 
 
258
 
    // recursively enters all existing nodes and deletes the node if the token it references
259
 
    // is invalid (i.e. m_pTokens->at() != token_in_data)
260
 
 
261
 
    // we 'll loop backwards so we can delete nodes without problems
262
 
    wxTreeItemId existing = tree->GetLastChild(parent);
263
 
    wxTreeItemId root = tree->GetRootItem();
264
 
    while (parent.IsOk() && existing.IsOk())
265
 
    {
266
 
        bool removeCurrent = false;
267
 
        bool hasChildren = tree->ItemHasChildren(existing);
268
 
        CBTreeData* data = (CBTreeData*)(tree->GetItemData(existing));
269
 
 
270
 
        if (tree == m_pTreeBottom)
271
 
            removeCurrent = true;
272
 
        else if (data && data->m_pToken)
273
 
        {
274
 
            if (m_pTokens->at(data->m_TokenIndex) != data->m_pToken ||
275
 
                (data->m_Ticket && data->m_Ticket != data->m_pToken->GetTicket()) ||
276
 
                !TokenMatchesFilter(data->m_pToken))
277
 
            {
278
 
                removeCurrent = true;
279
 
            }
280
 
        }
281
 
        if(removeCurrent)
282
 
        {
283
 
            if(hasChildren)
284
 
                tree->DeleteChildren(existing);
285
 
            wxTreeItemId next = tree->GetPrevSibling(existing);
286
 
            if(!next.IsOk() && parent.IsOk() && tree == m_pTreeTop && tree->GetChildrenCount(parent, false) == 1 )
287
 
            {
288
 
                CollapseItem(parent);
289
 
                // tree->SetItemHasChildren(parent, false);
290
 
            }
291
 
            else
292
 
            {
293
 
                tree->Delete(existing);
294
 
                existing = next;
295
 
                continue;
296
 
            }
297
 
        }
298
 
        else
299
 
        {
300
 
            RemoveInvalidNodes(tree, existing); // recurse
301
 
        }
302
 
        if(existing.IsOk())
303
 
            existing = tree->GetPrevSibling(existing);
304
 
    }
305
 
}
306
 
#else
307
 
void ClassBrowserBuilderThread::RemoveInvalidNodes(wxTreeCtrl* tree, wxTreeItemId parent)
308
 
{
309
 
    if (TestDestroy() || Manager::IsAppShuttingDown() || (!(parent.IsOk())))
310
 
        return;
311
 
 
312
 
    // recursively enters all existing nodes and deletes the node if the token it references
313
 
    // is invalid (i.e. m_pTokens->at() != token_in_data)
314
 
 
315
 
    // we 'll loop backwards so we can delete nodes without problems
316
 
    wxTreeItemId existing = tree->GetLastChild(parent);
317
 
    while (existing.IsOk())
318
 
    {
319
 
        // recurse
320
 
        if (tree->ItemHasChildren(existing))
321
 
            RemoveInvalidNodes(tree, existing);
322
 
 
323
 
        CBTreeData* data = (CBTreeData*)tree->GetItemData(existing);
324
 
        if (data && data->m_pToken)
325
 
        {
326
 
            if (m_pTokens->at(data->m_TokenIndex) != data->m_pToken ||
327
 
                data->m_TokenKind != data->m_pToken->m_TokenKind || // need to compare kinds: the token index might have been reused...
328
 
                data->m_TokenName != data->m_pToken->m_Name || // same for the token name
329
 
                !TokenMatchesFilter(data->m_pToken))
330
 
            {
331
 
                // keep parent and set flag if this is the last child of parent
332
 
                wxTreeItemId parent = tree->GetItemParent(existing);
333
 
                bool isLastChild = tree->GetChildrenCount(parent) == 1;
334
 
                // we have to do this in two steps: first collapse and then set haschildren to false
335
 
                if (isLastChild && parent.IsOk() && tree == m_pTreeTop)
336
 
                {
337
 
                    CollapseItem(parent);
338
 
                    return;
339
 
                }
340
 
                                else
341
 
                                {
342
 
//                      Manager::Get()->GetLogManager()->DebugLog(F(_T("Item %s is invalid"), tree->GetItemText(existing).c_str()));
343
 
                                        wxTreeItemId next = tree->GetPrevSibling(existing);
344
 
                                        tree->Delete(existing);
345
 
                                        existing = next;
346
 
                                }
347
 
 
348
 
                // if this was the last child of its parent, collapse the parent
349
 
//                if (isLastChild)
350
 
//                    tree->SetItemHasChildren(parent, false);
351
 
 
352
 
                continue;
353
 
            }
354
 
        }
355
 
 
356
 
        existing = tree->GetPrevSibling(existing);
357
 
    }
358
 
 
359
 
//    if (parent != tree->GetRootItem() && tree->GetChildrenCount(parent) == 0)
360
 
//        tree->Delete(parent);
361
 
}
362
 
#endif
363
 
wxTreeItemId ClassBrowserBuilderThread::AddNodeIfNotThere(wxTreeCtrl* tree, wxTreeItemId parent, const wxString& name, int imgIndex, CBTreeData* data, bool sorted)
364
 
{
365
 
    sorted = sorted & tree == m_pTreeTop && data; // sorting only for the top tree
366
 
    SpecialFolder new_type = data->m_SpecialFolder;
367
 
    bool newIsNamespace = data->m_TokenKind == tkNamespace;
368
 
 
369
 
    compatibility::tree_cookie_t cookie = 0;
370
 
 
371
 
    wxTreeItemId insert_after; // node to insert after; we 'll be looping all children so we might as well sort at the same time
372
 
    wxTreeItemId existing = tree->GetFirstChild(parent, cookie);
373
 
    while (existing)
374
 
    {
375
 
        wxString itemText = tree->GetItemText(existing);
376
 
        if (itemText == name)
377
 
        {
378
 
            // update the existing node's image indices and user-data.
379
 
            // it's not possible to have the same token name more than once
380
 
            // under the same namespace anyway. if we do, there's a bug in the parser :(
381
 
            tree->SetItemImage(existing, imgIndex, wxTreeItemIcon_Normal);
382
 
            tree->SetItemImage(existing, imgIndex, wxTreeItemIcon_Selected);
383
 
            delete tree->GetItemData(existing); // make Valgrind happy
384
 
            tree->SetItemData(existing, data);
385
 
 
386
 
            return existing;
387
 
        }
388
 
 
389
 
        if (sorted)
390
 
        {
391
 
            CBTreeData* existing_data = (CBTreeData*)tree->GetItemData(existing);
392
 
            if (existing_data)
393
 
            {
394
 
                SpecialFolder existing_type = existing_data->m_SpecialFolder;
395
 
 
396
 
                // first go special folders
397
 
                if ((existing_type & (sfGFuncs | sfGVars | sfPreproc | sfTypedef)) &&
398
 
                    !(new_type & (sfGFuncs | sfGVars | sfPreproc | sfTypedef)))
399
 
                {
400
 
                    insert_after = existing;
401
 
                }
402
 
                // then go namespaces, alphabetically
403
 
                else if (newIsNamespace &&
404
 
                        existing_data->m_TokenKind == tkNamespace &&
405
 
                        name.CompareTo(itemText, wxString::ignoreCase) >= 0)
406
 
                {
407
 
                    insert_after = existing;
408
 
                }
409
 
                // then everything else, alphabetically
410
 
                else if (!newIsNamespace &&
411
 
                        (existing_data->m_TokenKind == tkNamespace ||
412
 
                        name.CompareTo(itemText, wxString::ignoreCase) >= 0))
413
 
                {
414
 
                    insert_after = existing;
415
 
                }
416
 
            }
417
 
        }
418
 
        existing = tree->GetNextChild(parent, cookie);
419
 
    }
420
 
 
421
 
    if (sorted)
422
 
        existing = tree->InsertItem(parent, insert_after, name, imgIndex, imgIndex, data);
423
 
    else
424
 
        existing = tree->AppendItem(parent, name, imgIndex, imgIndex, data);
425
 
    return existing;
426
 
}
427
 
 
428
 
bool ClassBrowserBuilderThread::AddChildrenOf(wxTreeCtrl* tree, wxTreeItemId parent, int parentTokenIdx, int tokenKindMask)
429
 
{
430
 
    if (TestDestroy() || Manager::IsAppShuttingDown())
431
 
        return false;
432
 
 
433
 
    Token* parentToken = 0;
434
 
    TokenIdxSet::iterator it;
435
 
    TokenIdxSet::iterator it_end;
436
 
 
437
 
    if (parentTokenIdx == -1)
438
 
    {
439
 
        if(m_Options.displayFilter == bdfWorkspace)
440
 
        {
441
 
            it = m_pTokens->m_GlobalNameSpace.begin();
442
 
            it_end = m_pTokens->m_GlobalNameSpace.end();
443
 
        }
444
 
        else
445
 
        {
446
 
            it = m_CurrentGlobalTokensSet.begin();
447
 
            it_end = m_CurrentGlobalTokensSet.end();
448
 
        }
449
 
    }
450
 
    else
451
 
    {
452
 
        parentToken = m_pTokens->at(parentTokenIdx);
453
 
        if (!parentToken)
454
 
        {
455
 
//            Manager::Get()->GetLogManager()->DebugLog(F(_T("Token not found?!?")));
456
 
            return false;
457
 
        }
458
 
        it = parentToken->m_Children.begin();
459
 
        it_end = parentToken->m_Children.end();
460
 
    }
461
 
 
462
 
    return AddNodes(tree, parent, it, it_end, tokenKindMask);
463
 
}
464
 
 
465
 
bool ClassBrowserBuilderThread::AddAncestorsOf(wxTreeCtrl* tree, wxTreeItemId parent, int tokenIdx)
466
 
{
467
 
    if (TestDestroy() || Manager::IsAppShuttingDown())
468
 
        return false;
469
 
 
470
 
    Token* token = m_pTokens->at(tokenIdx);
471
 
    if (!token)
472
 
        return false;
473
 
 
474
 
    return AddNodes(tree, parent, token->m_DirectAncestors.begin(), token->m_DirectAncestors.end(), tkClass | tkTypedef, true);
475
 
}
476
 
 
477
 
bool ClassBrowserBuilderThread::AddDescendantsOf(wxTreeCtrl* tree, wxTreeItemId parent, int tokenIdx, bool allowInheritance)
478
 
{
479
 
    if (TestDestroy() || Manager::IsAppShuttingDown())
480
 
        return false;
481
 
 
482
 
    Token* token = m_pTokens->at(tokenIdx);
483
 
    if (!token)
484
 
        return false;
485
 
 
486
 
    bool inh = m_Options.showInheritance;
487
 
    m_Options.showInheritance = allowInheritance;
488
 
 
489
 
    bool ret = AddNodes(tree, parent, token->m_Descendants.begin(), token->m_Descendants.end(), tkClass | tkTypedef, true);
490
 
 
491
 
    m_Options.showInheritance = inh;
492
 
    return ret;
493
 
}
494
 
 
495
 
bool ClassBrowserBuilderThread::AddNodes(wxTreeCtrl* tree, wxTreeItemId parent, TokenIdxSet::iterator start, TokenIdxSet::iterator end, int tokenKindMask, bool allowGlobals)
496
 
{
497
 
    int count = 0;
498
 
    set<unsigned long, less<unsigned long> > tickets;
499
 
 
500
 
    // Build temporary list of Token tickets - if the token's ticket is present
501
 
    // among the parent node's children, it's a dupe, and we'll skip it.
502
 
    if(parent.IsOk() && tree == m_pTreeTop)
503
 
    {
504
 
        wxTreeItemIdValue cookie;
505
 
        wxTreeItemId curchild = tree->GetFirstChild(parent,cookie);
506
 
        while(curchild.IsOk())
507
 
        {
508
 
            CBTreeData* data = (CBTreeData*)(tree->GetItemData(curchild));
509
 
            curchild = tree->GetNextSibling(curchild);
510
 
            if(data && data->m_Ticket)
511
 
                tickets.insert(data->m_Ticket);
512
 
        }
513
 
    }
514
 
 
515
 
    for ( ; start != end; ++start)
516
 
    {
517
 
        Token* token = m_pTokens->at(*start);
518
 
        if (token &&
519
 
            (token->m_TokenKind & tokenKindMask) &&
520
 
            (allowGlobals || token->m_IsLocal) &&
521
 
            TokenMatchesFilter(token))
522
 
        {
523
 
            if(tree == m_pTreeTop && tickets.find(token->GetTicket()) != tickets.end())
524
 
                continue; // dupe
525
 
            ++count;
526
 
            int img = m_pParser->GetTokenKindImage(token);
527
 
 
528
 
            wxString str = token->m_Name;
529
 
            if (token->m_TokenKind == tkFunction || token->m_TokenKind == tkConstructor || token->m_TokenKind == tkDestructor)
530
 
                str << token->m_Args;
531
 
            if (!token->m_ActualType.IsEmpty())
532
 
                 str = str + _T(" : ") + token->m_ActualType;
533
 
 
534
 
            if (tree == m_pTreeTop)
535
 
            {
536
 
                wxTreeItemId child = AddNodeIfNotThere(tree, parent, str, img, new CBTreeData(sfToken, token, tokenKindMask));
537
 
                // mark as expanding if it is a container
538
 
                if (token->m_TokenKind == tkClass)
539
 
                    tree->SetItemHasChildren(child, m_Options.showInheritance || TokenContainsChildrenOfKind(token, tkClass | tkNamespace | tkEnum));
540
 
                else if (token->m_TokenKind & (tkNamespace | tkEnum))
541
 
                    tree->SetItemHasChildren(child, TokenContainsChildrenOfKind(token, tkClass | tkNamespace | tkEnum));
542
 
            }
543
 
            else // the bottom tree needs no checks
544
 
                tree->AppendItem(parent, str, img, img, new CBTreeData(sfToken, token));
545
 
        }
546
 
    }
547
 
//    Manager::Get()->GetLogManager()->DebugLog(F(_T("Sorting...")));
548
 
    if (tree == m_pTreeBottom) // only sort alphabetically the bottom tree
549
 
        tree->SortChildren(parent);
550
 
//    Manager::Get()->GetLogManager()->DebugLog(F(_T("Added %d nodes"), count));
551
 
    return count != 0;
552
 
}
553
 
 
554
 
bool ClassBrowserBuilderThread::TokenMatchesFilter(Token* token)
555
 
{
556
 
    if (token->m_IsTemp)
557
 
        return false;
558
 
 
559
 
    if (m_Options.displayFilter == bdfWorkspace)
560
 
        return true;
561
 
 
562
 
    if (m_Options.displayFilter == bdfFile && !m_CurrentTokenSet.empty())
563
 
    {
564
 
        if (m_CurrentTokenSet.find(token->GetSelf()) != m_CurrentTokenSet.end())
565
 
            return true;
566
 
 
567
 
        // we got to check all children of this token (recursively)
568
 
        // to see if any of them matches the filter...
569
 
        for (TokenIdxSet::iterator it = token->m_Children.begin(); it != token->m_Children.end(); ++it)
570
 
        {
571
 
            if (TokenMatchesFilter(m_pTokens->at(*it)))
572
 
                return true;
573
 
        }
574
 
    }
575
 
    else if (m_Options.displayFilter == bdfProject && m_pUserData)
576
 
    {
577
 
        return token->m_pUserData == m_pUserData;
578
 
    }
579
 
 
580
 
    return false;
581
 
}
582
 
 
583
 
bool ClassBrowserBuilderThread::TokenContainsChildrenOfKind(Token* token, int kind)
584
 
{
585
 
    if (!token)
586
 
        return false;
587
 
    TokensTree* tt = token->GetTree();
588
 
    for (TokenIdxSet::iterator it = token->m_Children.begin(); it != token->m_Children.end(); ++it)
589
 
    {
590
 
        Token* child = tt->at(*it);
591
 
        if (child->m_TokenKind & kind)
592
 
            return true;
593
 
    }
594
 
    return false;
595
 
}
596
 
 
597
 
void ClassBrowserBuilderThread::SelectNode(wxTreeItemId node)
598
 
{
599
 
    if (TestDestroy() || Manager::IsAppShuttingDown() || (!(node.IsOk())))
600
 
        return;
601
 
 
602
 
    m_pTreeBottom->Freeze();
603
 
    wxTreeItemId root = m_pTreeBottom->GetRootItem();
604
 
    if (!root)
605
 
        root = m_pTreeBottom->AddRoot(_T("Members")); // not visible, so don't translate
606
 
    else
607
 
        m_pTreeBottom->DeleteChildren(root);
608
 
    CBTreeData* data = (CBTreeData*)m_pTreeTop->GetItemData(node);
609
 
    if (data)
610
 
    {
611
 
        switch (data->m_SpecialFolder)
612
 
        {
613
 
            case sfGFuncs: AddChildrenOf(m_pTreeBottom, root, -1, tkFunction); break;
614
 
            case sfGVars: AddChildrenOf(m_pTreeBottom, root, -1, tkVariable); break;
615
 
            case sfPreproc: AddChildrenOf(m_pTreeBottom, root, -1, tkPreprocessor); break;
616
 
            case sfTypedef: AddChildrenOf(m_pTreeBottom, root, -1, tkTypedef); break;
617
 
            case sfToken:
618
 
            {
619
 
                // add all children, except containers
620
 
                AddChildrenOf(m_pTreeBottom, root, data->m_pToken->GetSelf(), ~(tkNamespace | tkClass | tkEnum));
621
 
                break;
622
 
            }
623
 
            default: break;
624
 
        }
625
 
    }
626
 
    m_pTreeBottom->Thaw();
627
 
}
628
 
 
629
 
// checks if there are respective children and colors the nodes
630
 
bool ClassBrowserBuilderThread::CreateSpecialFolders(wxTreeCtrl* tree, wxTreeItemId parent)
631
 
{
632
 
    bool hasGF = false;
633
 
    bool hasGV = false;
634
 
    bool hasGP = false;
635
 
    bool hasTD = false;
636
 
 
637
 
    // loop all tokens in global namespace and see if we have matches
638
 
    TokensTree* tt = m_pParser->GetTokens();
639
 
    for (TokenIdxSet::iterator it = tt->m_GlobalNameSpace.begin(); it != tt->m_GlobalNameSpace.end(); ++it)
640
 
    {
641
 
        Token* token = tt->at(*it);
642
 
        if (token && token->m_IsLocal && TokenMatchesFilter(token))
643
 
        {
644
 
            if (!hasGF && token->m_TokenKind == tkFunction)
645
 
                hasGF = true;
646
 
            else if (!hasGV && token->m_TokenKind == tkVariable)
647
 
                hasGV = true;
648
 
            else if (!hasGP && token->m_TokenKind == tkPreprocessor)
649
 
                hasGP = true;
650
 
            else if (!hasTD && token->m_TokenKind == tkTypedef)
651
 
                hasTD = true;
652
 
        }
653
 
 
654
 
        if (hasGF && hasGV && hasGP && hasTD)
655
 
            break; // we have everything, stop iterating...
656
 
    }
657
 
 
658
 
    wxTreeItemId gfuncs = AddNodeIfNotThere(m_pTreeTop, parent, _("Global functions"), PARSER_IMG_OTHERS_FOLDER, new CBTreeData(sfGFuncs, 0, tkFunction, -1));
659
 
    wxTreeItemId tdef = AddNodeIfNotThere(m_pTreeTop, parent, _("Global typedefs"), PARSER_IMG_TYPEDEF_FOLDER, new CBTreeData(sfTypedef, 0, tkTypedef, -1));
660
 
    wxTreeItemId gvars = AddNodeIfNotThere(m_pTreeTop, parent, _("Global variables"), PARSER_IMG_OTHERS_FOLDER, new CBTreeData(sfGVars, 0, tkVariable, -1));
661
 
    wxTreeItemId preproc = AddNodeIfNotThere(m_pTreeTop, parent, _("Preprocessor symbols"), PARSER_IMG_PREPROC_FOLDER, new CBTreeData(sfPreproc, 0, tkPreprocessor, -1));
662
 
 
663
 
    wxColour black = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
664
 
    wxColour grey = wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT);
665
 
 
666
 
    tree->SetItemTextColour(gfuncs, hasGF ? black : grey);
667
 
    tree->SetItemTextColour(gvars, hasGV ? black : grey);
668
 
    tree->SetItemTextColour(preproc, hasGP ? black : grey);
669
 
    tree->SetItemTextColour(tdef, hasTD ? black : grey);
670
 
 
671
 
    return hasGF || hasGV || hasGP || hasTD;
672
 
}
673
 
 
674
 
void ClassBrowserBuilderThread::ExpandItem(wxTreeItemId item)
675
 
{
676
 
    if (TestDestroy() || Manager::IsAppShuttingDown())
677
 
        return;
678
 
 
679
 
    wxMutexLocker lock(m_BuildMutex);
680
 
    CBTreeData* data = (CBTreeData*)m_pTreeTop->GetItemData(item);
681
 
    if (data)
682
 
    {
683
 
        switch (data->m_SpecialFolder)
684
 
        {
685
 
            case sfRoot:
686
 
            {
687
 
                CreateSpecialFolders(m_pTreeTop, item);
688
 
                AddChildrenOf(m_pTreeTop, item, -1, ~(tkFunction | tkVariable | tkPreprocessor | tkTypedef));
689
 
                break;
690
 
            }
691
 
            case sfBase: AddAncestorsOf(m_pTreeTop, item, data->m_pToken->GetSelf()); break;
692
 
            case sfDerived: AddDescendantsOf(m_pTreeTop, item, data->m_pToken->GetSelf(), false); break;
693
 
            case sfToken:
694
 
            {
695
 
                int kind = 0;
696
 
                switch (data->m_pToken->m_TokenKind)
697
 
                {
698
 
                    case tkClass:
699
 
                    {
700
 
                        // add base and derived classes folders
701
 
                        if (m_Options.showInheritance)
702
 
                        {
703
 
                            wxTreeItemId base = AddNodeIfNotThere(m_pTreeTop, item, _("Base classes"), PARSER_IMG_CLASS_FOLDER, new CBTreeData(sfBase, data->m_pToken, tkClass, data->m_pToken->GetSelf()));
704
 
                            if (!data->m_pToken->m_DirectAncestors.empty())
705
 
                                m_pTreeTop->SetItemHasChildren(base);
706
 
                            wxTreeItemId derived = AddNodeIfNotThere(m_pTreeTop, item, _("Derived classes"), PARSER_IMG_CLASS_FOLDER, new CBTreeData(sfDerived, data->m_pToken, tkClass, data->m_pToken->GetSelf()));
707
 
                            if (!data->m_pToken->m_Descendants.empty())
708
 
                                m_pTreeTop->SetItemHasChildren(derived);
709
 
                        }
710
 
                        kind = tkClass | tkEnum;
711
 
                        break;
712
 
                    }
713
 
                    case tkNamespace:
714
 
                        kind = tkNamespace | tkClass | tkEnum;
715
 
                        break;
716
 
                    default:
717
 
                        break;
718
 
                }
719
 
                if (kind != 0)
720
 
                    AddChildrenOf(m_pTreeTop, item, data->m_pToken->GetSelf(), kind);
721
 
                break;
722
 
            }
723
 
            default: break;
724
 
        }
725
 
    }
726
 
//    Manager::Get()->GetLogManager()->DebugLog(F(_("E: %d items"), m_pTreeTop->GetCount()));
727
 
}
728
 
 
729
 
void ClassBrowserBuilderThread::CollapseItem(wxTreeItemId item)
730
 
{
731
 
    if (TestDestroy() || Manager::IsAppShuttingDown())
732
 
        return;
733
 
 
734
 
    wxMutexLocker lock(m_BuildMutex);
735
 
#ifndef __WXGTK__
736
 
    m_pTreeTop->CollapseAndReset(item); // this freezes gtk
737
 
#else
738
 
    m_pTreeTop->DeleteChildren(item);
739
 
#endif
740
 
    m_pTreeTop->SetItemHasChildren(item);
741
 
//    Manager::Get()->GetLogManager()->DebugLog(F(_("C: %d items"), m_pTreeTop->GetCount()));
742
 
}
743
 
 
744
 
void ClassBrowserBuilderThread::SelectItem(wxTreeItemId item)
745
 
{
746
 
    if (TestDestroy() || Manager::IsAppShuttingDown())
747
 
        return;
748
 
 
749
 
    wxMutexLocker lock(m_BuildMutex);
750
 
    SelectNode(item);
751
 
//    Manager::Get()->GetLogManager()->DebugLog(F(_T("Select ") + m_pTreeTop->GetItemText(item)));
752
 
}
 
1
/*
 
2
 * This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
 
3
 * http://www.gnu.org/licenses/gpl-3.0.html
 
4
 *
 
5
 * $Revision$
 
6
 * $Id$
 
7
 * $HeadURL$
 
8
 */
 
9
 
 
10
#include <sdk.h>
 
11
#include "classbrowserbuilderthread.h"
 
12
#include <globals.h>
 
13
#include <manager.h>
 
14
#include <projectmanager.h>
 
15
#include <cbproject.h>
 
16
 
 
17
#include <wx/settings.h>
 
18
#include <wx/utils.h>
 
19
#ifdef buildtree_measuring
 
20
    #include <wx/stopwatch.h>
 
21
#endif
 
22
 
 
23
#include <algorithm>
 
24
 
 
25
 
 
26
namespace compatibility { typedef TernaryCondTypedef<wxMinimumVersion<2,5>::eval, wxTreeItemIdValue, long int>::eval tree_cookie_t; };
 
27
 
 
28
IMPLEMENT_DYNAMIC_CLASS(CBTreeCtrl, wxTreeCtrl)
 
29
 
 
30
CBTreeCtrl::CBTreeCtrl()
 
31
{
 
32
   Compare = &CBNoCompare;
 
33
}
 
34
 
 
35
CBTreeCtrl::CBTreeCtrl(wxWindow *parent, const wxWindowID id,const wxPoint& pos, const wxSize& size,long style)
 
36
                       :wxTreeCtrl(parent, id, pos, size, style)
 
37
{
 
38
   Compare = &CBNoCompare;
 
39
}
 
40
 
 
41
void CBTreeCtrl::SetCompareFunction(const BrowserSortType type)
 
42
{
 
43
    switch (type)
 
44
    {
 
45
        case bstAlphabet:
 
46
            Compare = &CBAlphabetCompare;
 
47
            break;
 
48
        case bstKind:
 
49
            Compare = &CBKindCompare;
 
50
            break;
 
51
        case bstScope:
 
52
            Compare = &CBScopeCompare;
 
53
            break;
 
54
        case bstNone:
 
55
        default:
 
56
            Compare = &CBNoCompare;
 
57
            break;
 
58
    }
 
59
 
 
60
}
 
61
 
 
62
int CBTreeCtrl::OnCompareItems(const wxTreeItemId& item1, const wxTreeItemId& item2)
 
63
{
 
64
    return Compare((CBTreeData*)GetItemData(item1), (CBTreeData*)GetItemData(item2));
 
65
 
 
66
}
 
67
 
 
68
int CBTreeCtrl::CBAlphabetCompare (CBTreeData* lhs, CBTreeData* rhs)
 
69
{
 
70
    if (!lhs || !rhs)
 
71
        return 1;
 
72
    if (lhs->m_SpecialFolder != sfToken || rhs->m_SpecialFolder != sfToken)
 
73
        return -1;
 
74
    if (!lhs->m_pToken || !rhs->m_pToken)
 
75
        return 1;
 
76
    return wxStricmp(lhs->m_pToken->m_Name, rhs->m_pToken->m_Name);
 
77
}
 
78
 
 
79
int CBTreeCtrl::CBKindCompare(CBTreeData* lhs, CBTreeData* rhs)
 
80
{
 
81
    if (!lhs || !rhs)
 
82
        return 1;
 
83
    if (lhs->m_SpecialFolder != sfToken || rhs->m_SpecialFolder != sfToken)
 
84
        return -1;
 
85
    if (lhs->m_TokenKind == rhs->m_TokenKind)
 
86
        return CBAlphabetCompare(lhs, rhs);
 
87
 
 
88
    return lhs->m_TokenKind - rhs->m_TokenKind;
 
89
}
 
90
 
 
91
int CBTreeCtrl::CBScopeCompare(CBTreeData* lhs, CBTreeData* rhs)
 
92
{
 
93
    if (!lhs || !rhs)
 
94
        return 1;
 
95
    if (lhs->m_SpecialFolder != sfToken || rhs->m_SpecialFolder != sfToken)
 
96
        return -1;
 
97
 
 
98
    if (lhs->m_pToken->m_Scope == rhs->m_pToken->m_Scope)
 
99
        return CBKindCompare(lhs, rhs);
 
100
 
 
101
    return rhs->m_pToken->m_Scope - lhs->m_pToken->m_Scope;
 
102
}
 
103
 
 
104
int CBTreeCtrl::CBNoCompare(CBTreeData* lhs, CBTreeData* rhs)
 
105
{
 
106
    return 0;
 
107
}
 
108
 
 
109
// This does not really do what it says !
 
110
// It only removes doubles, if they are neighbours, so the tree should be sorted !!
 
111
// The last one (after sorting) remains.
 
112
void CBTreeCtrl::RemoveDoubles(const wxTreeItemId& parent)
 
113
{
 
114
    if (Manager::IsAppShuttingDown() || (!(parent.IsOk())))
 
115
        return;
 
116
 
 
117
#ifdef buildtree_measuring
 
118
    wxStopWatch sw;
 
119
#endif
 
120
    // we 'll loop backwards so we can delete nodes without problems
 
121
    wxTreeItemId existing = GetLastChild(parent);
 
122
    while (parent.IsOk() && existing.IsOk())
 
123
    {
 
124
        wxTreeItemId prevItem = GetPrevSibling(existing);
 
125
        if (!prevItem.IsOk())
 
126
            break;
 
127
        CBTreeData* dataExisting = (CBTreeData*)(GetItemData(existing));
 
128
        CBTreeData* dataPrev = (CBTreeData*)(GetItemData(prevItem));
 
129
        if (dataExisting &&
 
130
           dataPrev &&
 
131
           dataExisting->m_SpecialFolder == sfToken &&
 
132
           dataPrev->m_SpecialFolder == sfToken &&
 
133
           dataExisting->m_pToken &&
 
134
           dataPrev->m_pToken &&
 
135
           (dataExisting->m_pToken->DisplayName() == dataPrev->m_pToken->DisplayName()))
 
136
        {
 
137
            Delete(prevItem);
 
138
        }
 
139
        else if (existing.IsOk())
 
140
            existing = GetPrevSibling(existing);
 
141
    }
 
142
#ifdef buildtree_measuring
 
143
    Manager::Get()->GetLogManager()->DebugLog(F(_T("RemoveDoubles took : %ld"), sw.Time()));
 
144
#endif
 
145
}
 
146
 
 
147
// ClassBrowserBuilderThread
 
148
ClassBrowserBuilderThread::ClassBrowserBuilderThread(wxSemaphore& sem, ClassBrowserBuilderThread** threadVar)
 
149
    : wxThread(wxTHREAD_JOINABLE),
 
150
    m_Semaphore(sem),
 
151
    m_pParser(0),
 
152
    m_pTreeTop(0),
 
153
    m_pTreeBottom(0),
 
154
    m_pUserData(0),
 
155
    m_Options(),
 
156
    m_pTokensTree(0),
 
157
    m_ppThreadVar(threadVar)
 
158
{
 
159
    //ctor
 
160
}
 
161
 
 
162
ClassBrowserBuilderThread::~ClassBrowserBuilderThread()
 
163
{
 
164
    //dtor
 
165
}
 
166
 
 
167
void ClassBrowserBuilderThread::Init(Parser* parser,
 
168
                                    CBTreeCtrl* treeTop,
 
169
                                    CBTreeCtrl* treeBottom,
 
170
                                    const wxString& active_filename,
 
171
                                    void* user_data, // active project
 
172
                                    const BrowserOptions& options,
 
173
                                    TokensTree* pTokensTree,
 
174
                                    bool build_tree)
 
175
{
 
176
    wxMutexLocker lock(m_BuildMutex);
 
177
    m_pParser        = parser;
 
178
    m_pTreeTop       = treeTop;
 
179
    m_pTreeBottom    = treeBottom;
 
180
    m_ActiveFilename = active_filename;
 
181
    m_pUserData      = user_data;
 
182
    m_Options        = options;
 
183
    m_pTokensTree    = pTokensTree;
 
184
 
 
185
    m_CurrentFileSet.clear();
 
186
    m_CurrentTokenSet.clear();
 
187
 
 
188
    TokensTree* tree = m_pParser->GetTokens();
 
189
    // fill filter set for current-file-filter
 
190
    if (m_Options.displayFilter == bdfFile && !m_ActiveFilename.IsEmpty())
 
191
    {
 
192
        // m_ActiveFilename is the full filename up to the extension dot. No extension though.
 
193
        // get all filenames' indices matching our mask
 
194
        tree->m_FilenamesMap.FindMatches(m_ActiveFilename, m_CurrentFileSet, true, true);
 
195
    }
 
196
 
 
197
    if (m_Options.displayFilter == bdfProject && (user_data != 0))
 
198
    {
 
199
        cbProject* prj = (cbProject*)user_data;
 
200
        for (int i = 0; i < prj->GetFilesCount(); i++)
 
201
        {
 
202
            ProjectFile* curfile = prj->GetFile(i);
 
203
            if (!curfile)
 
204
                continue;
 
205
 
 
206
            wxString filename = curfile->file.GetFullPath();
 
207
            size_t fileIdx = tree->m_FilenamesMap.GetItemNo(filename);
 
208
            if (fileIdx)
 
209
            {
 
210
                m_CurrentFileSet.insert(fileIdx);
 
211
            }
 
212
        }
 
213
    }
 
214
 
 
215
    if (!m_CurrentFileSet.empty())
 
216
    {
 
217
        m_CurrentTokenSet.clear();
 
218
        m_CurrentGlobalTokensSet.clear();
 
219
        for (TokenFilesSet::iterator it = m_CurrentFileSet.begin();it != m_CurrentFileSet.end(); it++)
 
220
        {
 
221
            TokenIdxSet* curset = &(tree->m_FilesMap[*it]);
 
222
            for (TokenIdxSet::iterator it2 = curset->begin(); it2 != curset->end(); it2++)
 
223
            {
 
224
                Token* curtoken = tree->at(*it2);
 
225
                if (curtoken)
 
226
                {
 
227
                    m_CurrentTokenSet.insert(*it2);
 
228
                    if (curtoken->m_ParentIndex == -1)
 
229
                        m_CurrentGlobalTokensSet.insert(*it2);
 
230
                }
 
231
            }
 
232
        }
 
233
    }
 
234
 
 
235
    if (build_tree)
 
236
    {
 
237
        BuildTree(false);
 
238
    }
 
239
}
 
240
 
 
241
void* ClassBrowserBuilderThread::Entry()
 
242
{
 
243
    while (!TestDestroy() && !Manager::IsAppShuttingDown())
 
244
    {
 
245
        // wait until the classbrowser signals
 
246
        m_Semaphore.Wait();
 
247
//        Manager::Get()->GetLogManager()->DebugLog(F(_T(" - - - - - -")));
 
248
 
 
249
        if (TestDestroy() || Manager::IsAppShuttingDown())
 
250
            break;
 
251
 
 
252
        if (platform::gtk)
 
253
        {
 
254
            // this code (PART 1/2) seems to be good on linux
 
255
            // because of it the libcairo crash on dualcore processors
 
256
            // is gone, but on windows it has very bad influence,
 
257
            // henceforth the ifdef guard
 
258
            // the questions remains if it is the correct solution
 
259
            if (!::wxIsMainThread())
 
260
                ::wxMutexGuiEnter();
 
261
        }
 
262
 
 
263
        BuildTree();
 
264
 
 
265
        if (platform::gtk)
 
266
        {
 
267
            // this code (PART 2/2) seems to be good on linux
 
268
            // because of it the libcairo crash on dualcore processors
 
269
            // is gone, but on windows it has very bad influence,
 
270
            // henceforth the ifdef guard
 
271
            // the questions remains if it is the correct solution
 
272
            if (!::wxIsMainThread())
 
273
                ::wxMutexGuiLeave();
 
274
        }
 
275
    }
 
276
 
 
277
    m_pParser = 0;
 
278
    m_pTreeTop = 0;
 
279
    m_pTreeBottom = 0;
 
280
 
 
281
    return 0;
 
282
}
 
283
 
 
284
void ClassBrowserBuilderThread::ExpandNamespaces(wxTreeItemId node)
 
285
{
 
286
    if ((!::wxIsMainThread() && TestDestroy()) || Manager::IsAppShuttingDown())
 
287
        return;
 
288
 
 
289
    if (!m_Options.expandNS || !node.IsOk())
 
290
        return;
 
291
 
 
292
    wxTreeItemIdValue enumerationCookie;
 
293
    wxTreeItemId existing = m_pTreeTop->GetFirstChild(node, enumerationCookie);
 
294
    while (existing.IsOk())
 
295
    {
 
296
        CBTreeData* data = (CBTreeData*)m_pTreeTop->GetItemData(existing);
 
297
        if (data && data->m_pToken && data->m_pToken->m_TokenKind == tkNamespace)
 
298
        {
 
299
//            Manager::Get()->GetLogManager()->DebugLog(F(_T("Auto-expanding: ") + data->m_pToken->m_Name));
 
300
            m_pTreeTop->Expand(existing);
 
301
            ExpandNamespaces(existing); // recurse
 
302
        }
 
303
 
 
304
        existing = m_pTreeTop->GetNextSibling(existing);
 
305
    }
 
306
}
 
307
 
 
308
void ClassBrowserBuilderThread::BuildTree(bool useLock)
 
309
{
 
310
    if ((!::wxIsMainThread() && TestDestroy()) || Manager::IsAppShuttingDown())
 
311
        return;
 
312
 
 
313
#ifdef buildtree_measuring
 
314
    wxStopWatch sw;
 
315
    wxStopWatch sw_total;
 
316
#endif
 
317
    m_pTreeTop->SetImageList(m_pParser->GetImageList());
 
318
    m_pTreeBottom->SetImageList(m_pParser->GetImageList());
 
319
 
 
320
    wxTreeItemId root = m_pTreeTop->GetRootItem();
 
321
    if (!root.IsOk())
 
322
    {
 
323
        root = m_pTreeTop->AddRoot(_("Symbols"), PARSER_IMG_SYMBOLS_FOLDER, PARSER_IMG_SYMBOLS_FOLDER, new CBTreeData(sfRoot));
 
324
        m_pTreeTop->SetItemHasChildren(root);
 
325
    }
 
326
 
 
327
 
 
328
    m_pTreeTop->SetCompareFunction(m_Options.sortType);
 
329
    m_pTreeBottom->SetCompareFunction(m_Options.sortType);
 
330
 
 
331
    m_ExpandedVect.clear();
 
332
    SaveExpandedItems(m_pTreeTop, root, 0);
 
333
#ifdef buildtree_measuring
 
334
    Manager::Get()->GetLogManager()->DebugLog(F(_T("Saving expanded items took : %ld ms"),sw.Time()));
 
335
    sw.Start();
 
336
#endif
 
337
 
 
338
    SaveSelectedItem();
 
339
#ifdef buildtree_measuring
 
340
    Manager::Get()->GetLogManager()->DebugLog(F(_T("Saving selected items took : %ld ms"),sw.Time()));
 
341
    sw.Start();
 
342
#endif
 
343
 
 
344
    if (m_Options.treeMembers)
 
345
    {
 
346
        m_pTreeBottom->Hide();
 
347
        m_pTreeBottom->Freeze();
 
348
    }
 
349
    m_pTreeTop->Hide();
 
350
    m_pTreeTop->Freeze();
 
351
 
 
352
#ifdef buildtree_measuring
 
353
    Manager::Get()->GetLogManager()->DebugLog(F(_T("Hiding and freezing trees took : %ld ms"),sw.Time()));
 
354
    sw.Start();
 
355
#endif
 
356
    RemoveInvalidNodes(m_pTreeTop, root);
 
357
#ifdef buildtree_measuring
 
358
    Manager::Get()->GetLogManager()->DebugLog(F(_T("Removing invalid nodes (top tree) took : %ld ms"),sw.Time()));
 
359
    sw.Start();
 
360
#endif
 
361
    if (m_Options.treeMembers)
 
362
    {
 
363
        RemoveInvalidNodes(m_pTreeBottom, m_pTreeBottom->GetRootItem());
 
364
#ifdef buildtree_measuring
 
365
    Manager::Get()->GetLogManager()->DebugLog(F(_T("Removing invalid nodes (bottom tree) took : %ld ms"),sw.Time()));
 
366
    sw.Start();
 
367
#endif
 
368
    }
 
369
 
 
370
    if ((!::wxIsMainThread() && TestDestroy()) || Manager::IsAppShuttingDown())
 
371
        return;
 
372
#ifdef buildtree_measuring
 
373
    Manager::Get()->GetLogManager()->DebugLog(F(_T("TestDestroy() took : %ld ms"),sw.Time()));
 
374
    sw.Start();
 
375
#endif
 
376
 
 
377
    // the tree is completely dynamic: it is populated when a node expands/collapses.
 
378
    // so, by expanding the root node, we already instruct it to fill the top level :)
 
379
    //
 
380
    // this technique makes it really fast to draw (we only draw what's expanded) and
 
381
    // has very minimum memory overhead since it contains as few items as possible.
 
382
    // plus, it doesn't flicker because we 're not emptying it and re-creating it each time ;)
 
383
 
 
384
    CollapseItem(root, useLock);
 
385
#ifdef buildtree_measuring
 
386
    Manager::Get()->GetLogManager()->DebugLog(F(_T("Collapsing root item took : %ld ms"),sw.Time()));
 
387
    sw.Start();
 
388
#endif
 
389
    // Bottleneck: Takes ~4 secs on C::B workspace:
 
390
    m_pTreeTop->Expand(root);
 
391
#ifdef buildtree_measuring
 
392
    Manager::Get()->GetLogManager()->DebugLog(F(_T("Expanding root item took : %ld ms"),sw.Time()));
 
393
    sw.Start();
 
394
#endif
 
395
 
 
396
    // seems like the "expand" event comes too late in wxGTK, so make it happen now
 
397
    if (platform::gtk)
 
398
        ExpandItem(root);
 
399
#ifdef buildtree_measuring
 
400
    Manager::Get()->GetLogManager()->DebugLog(F(_T("Expanding root item (gtk only) took : %ld ms"),sw.Time()));
 
401
    sw.Start();
 
402
#endif
 
403
    ExpandSavedItems(m_pTreeTop, root, 0);
 
404
#ifdef buildtree_measuring
 
405
    Manager::Get()->GetLogManager()->DebugLog(F(_T("Expanding saved items took : %ld ms"),sw.Time()));
 
406
    sw.Start();
 
407
#endif
 
408
    // Bottleneck: Takes ~4 secs on C::B workspace:
 
409
    SelectSavedItem();
 
410
#ifdef buildtree_measuring
 
411
    Manager::Get()->GetLogManager()->DebugLog(F(_T("Selecting saved item took : %ld ms"),sw.Time()));
 
412
    sw.Start();
 
413
#endif
 
414
 
 
415
    if (m_Options.treeMembers)
 
416
    {
 
417
        m_pTreeBottom->Thaw();
 
418
#ifdef buildtree_measuring
 
419
    Manager::Get()->GetLogManager()->DebugLog(F(_T("Thaw bottom tree took : %ld ms"),sw.Time()));
 
420
    sw.Start();
 
421
#endif
 
422
        m_pTreeBottom->Show();
 
423
#ifdef buildtree_measuring
 
424
    Manager::Get()->GetLogManager()->DebugLog(F(_T("Showing bottom tree took : %ld ms"),sw.Time()));
 
425
    sw.Start();
 
426
#endif
 
427
    }
 
428
 
 
429
    ExpandNamespaces(m_pTreeTop->GetRootItem());
 
430
#ifdef buildtree_measuring
 
431
    Manager::Get()->GetLogManager()->DebugLog(F(_T("Expanding namespaces took : %ld ms"),sw.Time()));
 
432
    sw.Start();
 
433
#endif
 
434
 
 
435
    m_pTreeTop->Thaw();
 
436
#ifdef buildtree_measuring
 
437
    Manager::Get()->GetLogManager()->DebugLog(F(_T("Thaw top tree took : %ld ms"),sw.Time()));
 
438
    sw.Start();
 
439
#endif
 
440
    // Bottleneck: Takes ~4 secs on C::B workspace:
 
441
    m_pTreeTop->Show();
 
442
#ifdef buildtree_measuring
 
443
    Manager::Get()->GetLogManager()->DebugLog(F(_T("Show top tree took : %ld ms"),sw.Time()));
 
444
    Manager::Get()->GetLogManager()->DebugLog(F(_T("BuildTree took : %ld ms in total"),sw_total.Time()));
 
445
#endif
 
446
}
 
447
 
 
448
#if 1
 
449
void ClassBrowserBuilderThread::RemoveInvalidNodes(CBTreeCtrl* tree, wxTreeItemId parent)
 
450
{
 
451
    if ((!::wxIsMainThread() && TestDestroy()) || Manager::IsAppShuttingDown() || (!(parent.IsOk())))
 
452
        return;
 
453
 
 
454
    // recursively enters all existing nodes and deletes the node if the token it references
 
455
    // is invalid (i.e. m_pTokensTree->at() != token_in_data)
 
456
 
 
457
    // we 'll loop backwards so we can delete nodes without problems
 
458
    wxTreeItemId existing = tree->GetLastChild(parent);
 
459
    while (parent.IsOk() && existing.IsOk())
 
460
    {
 
461
        bool removeCurrent = false;
 
462
        bool hasChildren = tree->ItemHasChildren(existing);
 
463
        CBTreeData* data = (CBTreeData*)(tree->GetItemData(existing));
 
464
 
 
465
        if (tree == m_pTreeBottom)
 
466
        {
 
467
            removeCurrent = true;
 
468
        }
 
469
        else if (data && data->m_pToken)
 
470
        {
 
471
 
 
472
            if (m_pTokensTree->at(data->m_TokenIndex) != data->m_pToken ||
 
473
                (data->m_Ticket && data->m_Ticket != data->m_pToken->GetTicket()) ||
 
474
                !TokenMatchesFilter(data->m_pToken))
 
475
            {
 
476
                removeCurrent = true;
 
477
            }
 
478
        }
 
479
 
 
480
        if (removeCurrent)
 
481
        {
 
482
            if (hasChildren)
 
483
                tree->DeleteChildren(existing);
 
484
 
 
485
            wxTreeItemId next = tree->GetPrevSibling(existing);
 
486
            if (!next.IsOk() && parent.IsOk() && tree == m_pTreeTop && tree->GetChildrenCount(parent, false) == 1 )
 
487
            {
 
488
                CollapseItem(parent);
 
489
                // tree->SetItemHasChildren(parent, false);
 
490
                // existing is the last item an gets deleted in CollapseItem and at least on 64-bit linux it can
 
491
                // lead to a crash, because we use it again some lines later, but m_pItem is not 0 in some rare cases,
 
492
                // and therefore IsOk returns true !!
 
493
                // so we leave the function here
 
494
                return;
 
495
            }
 
496
            else
 
497
            {
 
498
                tree->Delete(existing);
 
499
                existing = next;
 
500
                continue;
 
501
            }
 
502
        }
 
503
        else
 
504
        {
 
505
            RemoveInvalidNodes(tree, existing); // recurse
 
506
        }
 
507
        if (existing.IsOk())
 
508
            existing = tree->GetPrevSibling(existing);
 
509
    }
 
510
}
 
511
#else
 
512
void ClassBrowserBuilderThread::RemoveInvalidNodes(CBTreeCtrl* tree, wxTreeItemId parent)
 
513
{
 
514
    if ((!::wxIsMainThread() && TestDestroy()) || Manager::IsAppShuttingDown() || (!(parent.IsOk())))
 
515
        return;
 
516
 
 
517
    // recursively enters all existing nodes and deletes the node if the token it references
 
518
    // is invalid (i.e. m_pTokensTree->at() != token_in_data)
 
519
 
 
520
    // we 'll loop backwards so we can delete nodes without problems
 
521
    wxTreeItemId existing = tree->GetLastChild(parent);
 
522
    while (existing.IsOk())
 
523
    {
 
524
        // recurse
 
525
        if (tree->ItemHasChildren(existing))
 
526
            RemoveInvalidNodes(tree, existing);
 
527
 
 
528
        CBTreeData* data = (CBTreeData*)tree->GetItemData(existing);
 
529
        if (data && data->m_pToken)
 
530
        {
 
531
            if (m_pTokensTree->at(data->m_TokenIndex) != data->m_pToken ||
 
532
                data->m_TokenKind != data->m_pToken->m_TokenKind || // need to compare kinds: the token index might have been reused...
 
533
                data->m_TokenName != data->m_pToken->m_Name || // same for the token name
 
534
                !TokenMatchesFilter(data->m_pToken))
 
535
            {
 
536
                // keep parent and set flag if this is the last child of parent
 
537
                wxTreeItemId parent = tree->GetItemParent(existing);
 
538
                bool isLastChild = tree->GetChildrenCount(parent) == 1;
 
539
                // we have to do this in two steps: first collapse and then set haschildren to false
 
540
                if (isLastChild && parent.IsOk() && tree == m_pTreeTop)
 
541
                {
 
542
                    CollapseItem(parent);
 
543
                    return;
 
544
                }
 
545
                else
 
546
                {
 
547
//                    Manager::Get()->GetLogManager()->DebugLog(F(_T("Item %s is invalid"), tree->GetItemText(existing).c_str()));
 
548
                    wxTreeItemId next = tree->GetPrevSibling(existing);
 
549
                    tree->Delete(existing);
 
550
                    existing = next;
 
551
                }
 
552
 
 
553
                // if this was the last child of its parent, collapse the parent
 
554
//                if (isLastChild)
 
555
//                    tree->SetItemHasChildren(parent, false);
 
556
 
 
557
                continue;
 
558
            }
 
559
        }
 
560
 
 
561
        existing = tree->GetPrevSibling(existing);
 
562
    }
 
563
}
 
564
#endif
 
565
 
 
566
wxTreeItemId ClassBrowserBuilderThread::AddNodeIfNotThere(CBTreeCtrl* tree, wxTreeItemId parent, const wxString& name, int imgIndex, CBTreeData* data)
 
567
{
 
568
    compatibility::tree_cookie_t cookie = 0;
 
569
 
 
570
    wxTreeItemId existing = tree->GetFirstChild(parent, cookie);
 
571
    while (existing)
 
572
    {
 
573
        wxString itemText = tree->GetItemText(existing);
 
574
        if (itemText == name)
 
575
        {
 
576
            // update the existing node's image indices and user-data.
 
577
            // it's not possible to have the same token name more than once
 
578
            // under the same namespace anyway. if we do, there's a bug in the parser :(
 
579
            tree->SetItemImage(existing, imgIndex, wxTreeItemIcon_Normal);
 
580
            tree->SetItemImage(existing, imgIndex, wxTreeItemIcon_Selected);
 
581
            delete tree->GetItemData(existing); // make Valgrind happy
 
582
            tree->SetItemData(existing, data);
 
583
 
 
584
            return existing;
 
585
        }
 
586
        existing = tree->GetNextChild(parent, cookie);
 
587
    }
 
588
        return tree->AppendItem(parent, name, imgIndex, imgIndex, data);
 
589
}
 
590
 
 
591
bool ClassBrowserBuilderThread::AddChildrenOf(CBTreeCtrl* tree, wxTreeItemId parent, int parentTokenIdx, short int tokenKindMask, int tokenScopeMask)
 
592
{
 
593
    if ((!::wxIsMainThread() && TestDestroy()) || Manager::IsAppShuttingDown())
 
594
        return false;
 
595
 
 
596
    Token* parentToken = 0;
 
597
    TokenIdxSet* tokens = 0;
 
598
 
 
599
    if (parentTokenIdx == -1)
 
600
    {
 
601
        if (m_Options.displayFilter >= bdfWorkspace)
 
602
            tokens = &m_pTokensTree->m_GlobalNameSpace;
 
603
        else
 
604
            tokens = &m_CurrentGlobalTokensSet;
 
605
    }
 
606
    else
 
607
    {
 
608
        parentToken = m_pTokensTree->at(parentTokenIdx);
 
609
        if (!parentToken)
 
610
        {
 
611
//            Manager::Get()->GetLogManager()->DebugLog(F(_T("Token not found?!?")));
 
612
            return false;
 
613
        }
 
614
        tokens = &parentToken->m_Children;
 
615
    }
 
616
 
 
617
    return AddNodes(tree, parent, *tokens, tokenKindMask, tokenScopeMask, m_Options.displayFilter == bdfEverything);
 
618
}
 
619
 
 
620
bool ClassBrowserBuilderThread::AddAncestorsOf(CBTreeCtrl* tree, wxTreeItemId parent, int tokenIdx)
 
621
{
 
622
    if ((!::wxIsMainThread() && TestDestroy()) || Manager::IsAppShuttingDown())
 
623
        return false;
 
624
 
 
625
    Token* token = m_pTokensTree->at(tokenIdx);
 
626
    if (!token)
 
627
        return false;
 
628
 
 
629
    return AddNodes(tree, parent, token->m_DirectAncestors, tkClass | tkTypedef, 0, true);
 
630
}
 
631
 
 
632
bool ClassBrowserBuilderThread::AddDescendantsOf(CBTreeCtrl* tree, wxTreeItemId parent, int tokenIdx, bool allowInheritance)
 
633
{
 
634
    if ((!::wxIsMainThread() && TestDestroy()) || Manager::IsAppShuttingDown())
 
635
        return false;
 
636
 
 
637
    Token* token = m_pTokensTree->at(tokenIdx);
 
638
    if (!token)
 
639
        return false;
 
640
 
 
641
    bool inh = m_Options.showInheritance;
 
642
    m_Options.showInheritance = allowInheritance;
 
643
 
 
644
    bool ret = AddNodes(tree, parent, token->m_Descendants, tkClass | tkTypedef, 0, true);
 
645
 
 
646
    m_Options.showInheritance = inh;
 
647
    return ret;
 
648
}
 
649
 
 
650
bool ClassBrowserBuilderThread::AddNodes(CBTreeCtrl* tree, wxTreeItemId parent, const TokenIdxSet& tokens, short int tokenKindMask, int tokenScopeMask, bool allowGlobals)
 
651
{
 
652
    int count = 0;
 
653
    set<unsigned long, less<unsigned long> > tickets;
 
654
 
 
655
    // Build temporary list of Token tickets - if the token's ticket is present
 
656
    // among the parent node's children, it's a dupe, and we'll skip it.
 
657
    if (parent.IsOk() && tree == m_pTreeTop)
 
658
    {
 
659
        wxTreeItemIdValue cookie;
 
660
        wxTreeItemId curchild = tree->GetFirstChild(parent,cookie);
 
661
        while (curchild.IsOk())
 
662
        {
 
663
            CBTreeData* data = (CBTreeData*)(tree->GetItemData(curchild));
 
664
            curchild = tree->GetNextSibling(curchild);
 
665
            if (data && data->m_Ticket)
 
666
                tickets.insert(data->m_Ticket);
 
667
        }
 
668
    }
 
669
 
 
670
    TokenIdxSet::iterator start = tokens.begin();
 
671
    TokenIdxSet::iterator end = tokens.end();
 
672
 
 
673
    for ( ; start != end; ++start)
 
674
    {
 
675
        Token* token = m_pTokensTree->at(*start);
 
676
        if (token &&
 
677
            (token->m_TokenKind & tokenKindMask) &&
 
678
            (tokenScopeMask == 0 || token->m_Scope == tokenScopeMask) &&
 
679
            (allowGlobals || token->m_IsLocal ||
 
680
            TokenMatchesFilter(token)))
 
681
        {
 
682
            if (tree == m_pTreeTop && tickets.find(token->GetTicket()) != tickets.end())
 
683
                continue; // dupe
 
684
            ++count;
 
685
            int img = m_pParser->GetTokenKindImage(token);
 
686
 
 
687
            wxString str = token->m_Name;
 
688
            if (   (token->m_TokenKind == tkFunction)
 
689
                || (token->m_TokenKind == tkConstructor)
 
690
                || (token->m_TokenKind == tkDestructor)
 
691
                || (token->m_TokenKind == tkMacro)
 
692
                || (token->m_TokenKind == tkClass) )
 
693
                str << token->m_Args;
 
694
            // modification suggested by ollydbg in http://forums.codeblocks.org/index.php/topic,10242.msg70865.html#msg70865:
 
695
//            if (!token->m_ActualType.IsEmpty())
 
696
//                 str = str + _T(" : ") + token->m_ActualType;
 
697
            if (!token->m_Type.IsEmpty())
 
698
                 str = str + _T(" : ") + token->m_Type;
 
699
 
 
700
            wxTreeItemId child = tree->AppendItem(parent, str, img, img, new CBTreeData(sfToken, token, tokenKindMask));
 
701
 
 
702
            // mark as expanding if it is a container
 
703
            int kind = tkClass | tkNamespace | tkEnum;
 
704
            if (token->m_TokenKind == tkClass)
 
705
            {
 
706
                if (!m_Options.treeMembers)
 
707
                    kind |= tkTypedef | tkFunction | tkVariable | tkEnum | tkMacro;
 
708
                tree->SetItemHasChildren(child, m_Options.showInheritance || TokenContainsChildrenOfKind(token, kind));
 
709
            }
 
710
            else if (token->m_TokenKind & (tkNamespace | tkEnum))
 
711
            {
 
712
                if (!m_Options.treeMembers)
 
713
                    kind |= tkTypedef | tkFunction | tkVariable | tkEnumerator | tkMacro;
 
714
                tree->SetItemHasChildren(child, TokenContainsChildrenOfKind(token, kind));
 
715
            }
 
716
        }
 
717
    }
 
718
 
 
719
    tree->SortChildren(parent);
 
720
//    tree->RemoveDoubles(parent);
 
721
#ifdef buildtree_measuring
 
722
    Manager::Get()->GetLogManager()->DebugLog(F(_T("Added %d nodes"), count));
 
723
#endif
 
724
    return count != 0;
 
725
}
 
726
 
 
727
bool ClassBrowserBuilderThread::TokenMatchesFilter(Token* token)
 
728
{
 
729
    if (token->m_IsTemp)
 
730
        return false;
 
731
 
 
732
    if (    m_Options.displayFilter == bdfEverything
 
733
        || (m_Options.displayFilter == bdfWorkspace && token->m_IsLocal) )
 
734
        return true;
 
735
 
 
736
    if (m_Options.displayFilter == bdfFile && !m_CurrentTokenSet.empty())
 
737
    {
 
738
        if (m_CurrentTokenSet.find(token->GetSelf()) != m_CurrentTokenSet.end())
 
739
            return true;
 
740
 
 
741
        // we got to check all children of this token (recursively)
 
742
        // to see if any of them matches the filter...
 
743
        for (TokenIdxSet::iterator it = token->m_Children.begin(); it != token->m_Children.end(); ++it)
 
744
        {
 
745
            if (TokenMatchesFilter(m_pTokensTree->at(*it)))
 
746
                return true;
 
747
        }
 
748
    }
 
749
    else if (m_Options.displayFilter == bdfProject && m_pUserData)
 
750
    {
 
751
        return token->m_pUserData == m_pUserData;
 
752
    }
 
753
 
 
754
    return false;
 
755
}
 
756
 
 
757
bool ClassBrowserBuilderThread::TokenContainsChildrenOfKind(Token* token, int kind)
 
758
{
 
759
    if (!token)
 
760
        return false;
 
761
    TokensTree* tt = token->GetTree();
 
762
    for (TokenIdxSet::iterator it = token->m_Children.begin(); it != token->m_Children.end(); ++it)
 
763
    {
 
764
        Token* child = tt->at(*it);
 
765
        if (child->m_TokenKind & kind)
 
766
            return true;
 
767
    }
 
768
    return false;
 
769
}
 
770
 
 
771
void ClassBrowserBuilderThread::AddMembersOf(CBTreeCtrl* tree, wxTreeItemId node)
 
772
{
 
773
   if ((!::wxIsMainThread() && TestDestroy()) || Manager::IsAppShuttingDown() || !node.IsOk())
 
774
        return;
 
775
 
 
776
    CBTreeData* data = (CBTreeData*)m_pTreeTop->GetItemData(node);
 
777
 
 
778
    bool bottom = (tree == m_pTreeBottom);
 
779
    if (bottom)
 
780
    {
 
781
#ifdef buildtree_measuring
 
782
        wxStopWatch sw;
 
783
#endif
 
784
        tree->Freeze();
 
785
#ifdef buildtree_measuring
 
786
        Manager::Get()->GetLogManager()->DebugLog(F(_T("tree->Freeze() took : %ld ms"),sw.Time()));
 
787
        sw.Start();
 
788
#endif
 
789
        tree->DeleteAllItems();
 
790
#ifdef buildtree_measuring
 
791
        Manager::Get()->GetLogManager()->DebugLog(F(_T("tree->DeleteAllItems() took : %ld ms"),sw.Time()));
 
792
        sw.Start();
 
793
#endif
 
794
        node = tree->AddRoot(_T("Members")); // not visible, so don't translate
 
795
#ifdef buildtree_measuring
 
796
        Manager::Get()->GetLogManager()->DebugLog(F(_T("tree->AddRoot() took : %ld ms"),sw.Time()));
 
797
#endif
 
798
    }
 
799
 
 
800
    wxTreeItemId firstItem;
 
801
    bool haveFirstItem = false;
 
802
    if (data)
 
803
    {
 
804
        switch (data->m_SpecialFolder)
 
805
        {
 
806
            case sfGFuncs  : AddChildrenOf(tree, node, -1, tkFunction,     false); break;
 
807
            case sfGVars   : AddChildrenOf(tree, node, -1, tkVariable,     false); break;
 
808
            case sfPreproc : AddChildrenOf(tree, node, -1, tkPreprocessor, false); break;
 
809
            case sfTypedef : AddChildrenOf(tree, node, -1, tkTypedef,      false); break;
 
810
            case sfMacro   : AddChildrenOf(tree, node, -1, tkMacro,        false); break;
 
811
            case sfToken:
 
812
            {
 
813
                if (bottom)
 
814
                {
 
815
                    if (m_Options.sortType == bstKind && !(data->m_pToken->m_TokenKind & tkEnum))
 
816
                    {
 
817
                        wxTreeItemId rootCtorDtor = tree->AppendItem(node, _("Ctors & Dtors"), PARSER_IMG_CLASS_FOLDER);
 
818
                        wxTreeItemId rootFuncs    = tree->AppendItem(node, _("Functions"), PARSER_IMG_FUNCS_FOLDER);
 
819
                        wxTreeItemId rootVars     = tree->AppendItem(node, _("Variables"), PARSER_IMG_VARS_FOLDER);
 
820
                        wxTreeItemId rootMacro    = tree->AppendItem(node, _("Macros"), PARSER_IMG_MACRO_FOLDER);
 
821
                        wxTreeItemId rootOthers   = tree->AppendItem(node, _("Others"), PARSER_IMG_OTHERS_FOLDER);
 
822
 
 
823
                        AddChildrenOf(tree, rootCtorDtor, data->m_pToken->GetSelf(), tkConstructor | tkDestructor);
 
824
                        AddChildrenOf(tree, rootFuncs,    data->m_pToken->GetSelf(), tkFunction);
 
825
                        AddChildrenOf(tree, rootVars,     data->m_pToken->GetSelf(), tkVariable);
 
826
                        AddChildrenOf(tree, rootMacro,    data->m_pToken->GetSelf(), tkMacro);
 
827
                        AddChildrenOf(tree, rootOthers,   data->m_pToken->GetSelf(), ~(tkNamespace | tkClass | tkEnum | tkAnyFunction | tkVariable | tkMacro));
 
828
 
 
829
                        firstItem = rootCtorDtor;
 
830
                    }
 
831
                    else if (m_Options.sortType == bstScope && data->m_pToken->m_TokenKind & tkClass)
 
832
                    {
 
833
                        wxTreeItemId rootPublic    = tree->AppendItem(node, _("Public"), PARSER_IMG_CLASS_FOLDER);
 
834
                        wxTreeItemId rootProtected = tree->AppendItem(node, _("Protected"), PARSER_IMG_FUNCS_FOLDER);
 
835
                        wxTreeItemId rootPrivate   = tree->AppendItem(node, _("Private"), PARSER_IMG_VARS_FOLDER);
 
836
 
 
837
                        AddChildrenOf(tree, rootPublic,    data->m_pToken->GetSelf(), ~(tkNamespace | tkClass | tkEnum), tsPublic);
 
838
                        AddChildrenOf(tree, rootProtected, data->m_pToken->GetSelf(), ~(tkNamespace | tkClass | tkEnum), tsProtected);
 
839
                        AddChildrenOf(tree, rootPrivate,   data->m_pToken->GetSelf(), ~(tkNamespace | tkClass | tkEnum), tsPrivate);
 
840
 
 
841
                        firstItem = rootPublic;
 
842
                    }
 
843
                    else
 
844
                    {
 
845
                        AddChildrenOf(tree, node, data->m_pToken->GetSelf(), ~(tkNamespace | tkClass | tkEnum));
 
846
                        break;
 
847
                    }
 
848
 
 
849
                    wxTreeItemId existing = tree->GetLastChild(tree->GetRootItem());
 
850
                    while (existing.IsOk())
 
851
                    {
 
852
                        wxTreeItemId next = tree->GetPrevSibling(existing);
 
853
 
 
854
                        if (tree->GetChildrenCount(existing) > 0)
 
855
                        {
 
856
                            tree->SetItemBold(existing, true);
 
857
                            // make existing the firstItem, because the former firstItem might get deleted
 
858
                            // in the else-clause, if it has no children, what can lead to a crash
 
859
                            firstItem = existing;
 
860
                            // needed, if no child remains, because firstItem IsOk() returns true anyway
 
861
                            // in some cases.
 
862
                            haveFirstItem = true;
 
863
                        }
 
864
                        else
 
865
                        {
 
866
                            tree->Delete(existing);
 
867
                            existing = next;
 
868
                            continue;
 
869
                        }
 
870
                        existing = tree->GetPrevSibling(existing);
 
871
                    }
 
872
                }
 
873
                else
 
874
                    AddChildrenOf(tree, node, data->m_pToken->GetSelf(), ~(tkNamespace | tkClass | tkEnum));
 
875
 
 
876
                // add all children, except containers
 
877
                // AddChildrenOf(tree, node, data->m_pToken->GetSelf(), ~(tkNamespace | tkClass | tkEnum));
 
878
                break;
 
879
            }
 
880
            default: break;
 
881
        }
 
882
    }
 
883
 
 
884
    if (bottom)
 
885
    {
 
886
        tree->ExpandAll();
 
887
        if (haveFirstItem && firstItem.IsOk())
 
888
        {
 
889
            tree->ScrollTo(firstItem);
 
890
            tree->EnsureVisible(firstItem);
 
891
        }
 
892
        tree->Thaw();
 
893
    }
 
894
}
 
895
 
 
896
// checks if there are respective children and colors the nodes
 
897
bool ClassBrowserBuilderThread::CreateSpecialFolders(CBTreeCtrl* tree, wxTreeItemId parent)
 
898
{
 
899
    bool hasGF = false;
 
900
    bool hasGV = false;
 
901
    bool hasGP = false;
 
902
    bool hasTD = false;
 
903
    bool hasGM = false;
 
904
 
 
905
    // loop all tokens in global namespace and see if we have matches
 
906
    TokensTree* tt = m_pParser->GetTokens();
 
907
    for (TokenIdxSet::iterator it = tt->m_GlobalNameSpace.begin(); it != tt->m_GlobalNameSpace.end(); ++it)
 
908
    {
 
909
        Token* token = tt->at(*it);
 
910
        if (token && token->m_IsLocal && TokenMatchesFilter(token))
 
911
        {
 
912
            if      (!hasGF && token->m_TokenKind == tkFunction)
 
913
                hasGF = true;
 
914
            else if (!hasGM && token->m_TokenKind == tkMacro)
 
915
                hasGM = true;
 
916
            else if (!hasGV && token->m_TokenKind == tkVariable)
 
917
                hasGV = true;
 
918
            else if (!hasGP && token->m_TokenKind == tkPreprocessor)
 
919
                hasGP = true;
 
920
            else if (!hasTD && token->m_TokenKind == tkTypedef)
 
921
                hasTD = true;
 
922
        }
 
923
 
 
924
        if (hasGF && hasGV && hasGP && hasTD && hasGM)
 
925
            break; // we have everything, stop iterating...
 
926
    }
 
927
 
 
928
    wxTreeItemId gfuncs  = AddNodeIfNotThere(m_pTreeTop, parent, _("Global functions"),     PARSER_IMG_FUNCS_FOLDER,   new CBTreeData(sfGFuncs, 0, tkFunction, -1));
 
929
    wxTreeItemId tdef    = AddNodeIfNotThere(m_pTreeTop, parent, _("Global typedefs"),      PARSER_IMG_TYPEDEF_FOLDER, new CBTreeData(sfTypedef, 0, tkTypedef, -1));
 
930
    wxTreeItemId gvars   = AddNodeIfNotThere(m_pTreeTop, parent, _("Global variables"),     PARSER_IMG_VARS_FOLDER,    new CBTreeData(sfGVars, 0, tkVariable, -1));
 
931
    wxTreeItemId preproc = AddNodeIfNotThere(m_pTreeTop, parent, _("Preprocessor symbols"), PARSER_IMG_PREPROC_FOLDER, new CBTreeData(sfPreproc, 0, tkPreprocessor, -1));
 
932
    wxTreeItemId gmacro  = AddNodeIfNotThere(m_pTreeTop, parent, _("Global macros"),        PARSER_IMG_MACRO_FOLDER,   new CBTreeData(sfMacro, 0, tkMacro, -1));
 
933
 
 
934
    bool bottom = m_Options.treeMembers;
 
935
    m_pTreeTop->SetItemHasChildren(gfuncs,  !bottom && hasGF);
 
936
    m_pTreeTop->SetItemHasChildren(tdef,    !bottom && hasTD);
 
937
    m_pTreeTop->SetItemHasChildren(gvars,   !bottom && hasGV);
 
938
    m_pTreeTop->SetItemHasChildren(preproc, !bottom && hasGP);
 
939
    m_pTreeTop->SetItemHasChildren(gmacro,  !bottom && hasGM);
 
940
 
 
941
    wxColour black = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
 
942
    wxColour grey  = wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT);
 
943
 
 
944
    tree->SetItemTextColour(gfuncs,  hasGF ? black : grey);
 
945
    tree->SetItemTextColour(gvars,   hasGV ? black : grey);
 
946
    tree->SetItemTextColour(preproc, hasGP ? black : grey);
 
947
    tree->SetItemTextColour(tdef,    hasTD ? black : grey);
 
948
    tree->SetItemTextColour(gmacro,  hasGM ? black : grey);
 
949
 
 
950
    return hasGF || hasGV || hasGP || hasTD || hasGM;
 
951
}
 
952
 
 
953
void ClassBrowserBuilderThread::ExpandItem(wxTreeItemId item)
 
954
{
 
955
    if ((!::wxIsMainThread() && TestDestroy()) || Manager::IsAppShuttingDown())
 
956
        return;
 
957
 
 
958
#ifdef buildtree_measuring
 
959
    wxStopWatch sw;
 
960
#endif
 
961
//    wxMutexLocker lock(m_BuildMutex);
 
962
    CBTreeData* data = (CBTreeData*)m_pTreeTop->GetItemData(item);
 
963
    if (data)
 
964
    {
 
965
        switch (data->m_SpecialFolder)
 
966
        {
 
967
            case sfRoot:
 
968
            {
 
969
                CreateSpecialFolders(m_pTreeTop, item);
 
970
                if( ! (m_Options.displayFilter == bdfFile  && m_ActiveFilename.IsEmpty()))
 
971
                    AddChildrenOf(m_pTreeTop, item, -1, ~(tkFunction | tkVariable | tkPreprocessor | tkTypedef | tkMacro));
 
972
                break;
 
973
            }
 
974
            case sfBase: AddAncestorsOf(m_pTreeTop, item, data->m_pToken->GetSelf()); break;
 
975
            case sfDerived: AddDescendantsOf(m_pTreeTop, item, data->m_pToken->GetSelf(), false); break;
 
976
            case sfToken:
 
977
            {
 
978
                short int kind = 0;
 
979
                switch (data->m_pToken->m_TokenKind)
 
980
                {
 
981
                    case tkClass:
 
982
                    {
 
983
                        // add base and derived classes folders
 
984
                        if (m_Options.showInheritance)
 
985
                        {
 
986
                            wxTreeItemId base = m_pTreeTop->AppendItem(item, _("Base classes"), PARSER_IMG_CLASS_FOLDER, PARSER_IMG_CLASS_FOLDER, new CBTreeData(sfBase, data->m_pToken, tkClass, data->m_pToken->GetSelf()));
 
987
                            if (!data->m_pToken->m_DirectAncestors.empty())
 
988
                                m_pTreeTop->SetItemHasChildren(base);
 
989
                            wxTreeItemId derived = m_pTreeTop->AppendItem(item, _("Derived classes"), PARSER_IMG_CLASS_FOLDER, PARSER_IMG_CLASS_FOLDER, new CBTreeData(sfDerived, data->m_pToken, tkClass, data->m_pToken->GetSelf()));
 
990
                            if (!data->m_pToken->m_Descendants.empty())
 
991
                                m_pTreeTop->SetItemHasChildren(derived);
 
992
                        }
 
993
                        kind = tkClass | tkEnum;
 
994
                        break;
 
995
                    }
 
996
                    case tkNamespace:
 
997
                        kind = tkNamespace | tkClass | tkEnum;
 
998
                        break;
 
999
                    default:
 
1000
                        break;
 
1001
                }
 
1002
                if (kind != 0)
 
1003
                    AddChildrenOf(m_pTreeTop, item, data->m_pToken->GetSelf(), kind);
 
1004
                break;
 
1005
            }
 
1006
            default: break;
 
1007
        }
 
1008
    }
 
1009
    if (m_pParser && !m_Options.treeMembers)
 
1010
    {
 
1011
        AddMembersOf(m_pTreeTop, item);
 
1012
    }
 
1013
#ifdef buildtree_measuring
 
1014
    Manager::Get()->GetLogManager()->DebugLog(F(_T("ExpandItems (internally) took : %ld ms"),sw.Time()));
 
1015
#endif
 
1016
//    Manager::Get()->GetLogManager()->DebugLog(F(_("E: %d items"), m_pTreeTop->GetCount()));
 
1017
}
 
1018
 
 
1019
void ClassBrowserBuilderThread::CollapseItem(wxTreeItemId item, bool useLock)
 
1020
{
 
1021
    if ((!::wxIsMainThread() && TestDestroy()) || Manager::IsAppShuttingDown())
 
1022
        return;
 
1023
 
 
1024
    if (useLock)
 
1025
    wxMutexLocker lock(m_BuildMutex);
 
1026
#ifndef __WXGTK__
 
1027
    m_pTreeTop->CollapseAndReset(item); // this freezes gtk
 
1028
#else
 
1029
    m_pTreeTop->DeleteChildren(item);
 
1030
#endif
 
1031
    m_pTreeTop->SetItemHasChildren(item);
 
1032
//    Manager::Get()->GetLogManager()->DebugLog(F(_("C: %d items"), m_pTreeTop->GetCount()));
 
1033
}
 
1034
 
 
1035
void ClassBrowserBuilderThread::SelectItem(wxTreeItemId item)
 
1036
{
 
1037
    if ((!::wxIsMainThread() && TestDestroy()) || Manager::IsAppShuttingDown())
 
1038
        return;
 
1039
#ifdef buildtree_measuring
 
1040
    wxStopWatch sw;
 
1041
#endif
 
1042
    wxMutexLocker lock(m_BuildMutex);
 
1043
 
 
1044
    CBTreeCtrl* tree = (m_Options.treeMembers) ? m_pTreeBottom : m_pTreeTop;
 
1045
    if ( !(m_Options.displayFilter == bdfFile && m_ActiveFilename.IsEmpty()))
 
1046
        AddMembersOf(tree, item);
 
1047
//    Manager::Get()->GetLogManager()->DebugLog(F(_T("Select ") + m_pTreeTop->GetItemText(item)));
 
1048
#ifdef buildtree_measuring
 
1049
    Manager::Get()->GetLogManager()->DebugLog(F(_T("SelectItem (internally) took : %ld ms"),sw.Time()));
 
1050
#endif
 
1051
}
 
1052
 
 
1053
void ClassBrowserBuilderThread::SaveExpandedItems(CBTreeCtrl* tree, wxTreeItemId parent, int level)
 
1054
{
 
1055
    if ((!::wxIsMainThread() && TestDestroy()) || Manager::IsAppShuttingDown())
 
1056
        return;
 
1057
 
 
1058
    wxTreeItemIdValue cookie;
 
1059
    wxTreeItemId existing = tree->GetFirstChild(parent, cookie);
 
1060
    while (existing.IsOk())
 
1061
    {
 
1062
        CBTreeData* data = (CBTreeData*)(tree->GetItemData(existing));
 
1063
        if (tree->GetChildrenCount(existing,false) > 0)
 
1064
        {
 
1065
            m_ExpandedVect.push_back(CBExpandedItemData(data, level));
 
1066
 
 
1067
            SaveExpandedItems(tree, existing, level + 1);
 
1068
        }
 
1069
 
 
1070
        existing = tree->GetNextSibling(existing);
 
1071
    }
 
1072
}
 
1073
 
 
1074
void ClassBrowserBuilderThread::ExpandSavedItems(CBTreeCtrl* tree, wxTreeItemId parent, int level)
 
1075
{
 
1076
    if ((!::wxIsMainThread() && TestDestroy()) || Manager::IsAppShuttingDown())
 
1077
        return;
 
1078
 
 
1079
    wxTreeItemIdValue cookie;
 
1080
    wxTreeItemId existing = tree->GetFirstChild(parent, cookie);
 
1081
    while (existing.IsOk() && !m_ExpandedVect.empty())
 
1082
    {
 
1083
        CBTreeData* data = (CBTreeData*)(tree->GetItemData(existing));
 
1084
        CBExpandedItemData saved = m_ExpandedVect.front();
 
1085
 
 
1086
        if (level == saved.GetLevel() &&
 
1087
            wxStrcmp(data->m_TokenName, saved.GetData().m_TokenName) == 0 &&
 
1088
            data->m_TokenKind == saved.GetData().m_TokenKind &&
 
1089
            data->m_SpecialFolder == saved.GetData().m_SpecialFolder)
 
1090
        {
 
1091
            tree->Expand(existing);
 
1092
            //ExpandItem(existing);
 
1093
 
 
1094
            m_ExpandedVect.pop_front();
 
1095
 
 
1096
            if (m_ExpandedVect.empty())
 
1097
                return;
 
1098
 
 
1099
            saved = m_ExpandedVect.front(); //next saved
 
1100
            if (saved.GetLevel() < level)
 
1101
                return;
 
1102
 
 
1103
            if (saved.GetLevel() > level)
 
1104
                ExpandSavedItems(tree, existing, saved.GetLevel());
 
1105
        }
 
1106
 
 
1107
        existing = tree->GetNextSibling(existing);
 
1108
    }
 
1109
 
 
1110
    // remove non-existing by now saved items
 
1111
    while (!m_ExpandedVect.empty() && m_ExpandedVect.front().GetLevel() > level)
 
1112
        m_ExpandedVect.pop_front();
 
1113
}
 
1114
 
 
1115
void ClassBrowserBuilderThread::SaveSelectedItem()
 
1116
{
 
1117
    if ((!::wxIsMainThread() && TestDestroy()) || Manager::IsAppShuttingDown())
 
1118
        return;
 
1119
 
 
1120
    m_SelectedPath.clear();
 
1121
 
 
1122
    wxTreeItemId item = m_pTreeTop->GetSelection();
 
1123
    while (item.IsOk() && item != m_pTreeTop->GetRootItem())
 
1124
    {
 
1125
        CBTreeData* data = (CBTreeData*)m_pTreeTop->GetItemData(item);
 
1126
        m_SelectedPath.push_front(*data);
 
1127
 
 
1128
        item = m_pTreeTop->GetItemParent(item);
 
1129
    }
 
1130
}
 
1131
 
 
1132
void ClassBrowserBuilderThread::SelectSavedItem()
 
1133
{
 
1134
    if ((!::wxIsMainThread() && TestDestroy()) || Manager::IsAppShuttingDown())
 
1135
        return;
 
1136
 
 
1137
    wxTreeItemId parent = m_pTreeTop->GetRootItem();
 
1138
 
 
1139
    wxTreeItemIdValue cookie;
 
1140
    wxTreeItemId item = m_pTreeTop->GetFirstChild(parent, cookie);
 
1141
 
 
1142
    while (!m_SelectedPath.empty() && item.IsOk())
 
1143
    {
 
1144
        CBTreeData* data = (CBTreeData*)m_pTreeTop->GetItemData(item);
 
1145
        CBTreeData* saved = &m_SelectedPath.front();
 
1146
 
 
1147
        if (data->m_SpecialFolder == saved->m_SpecialFolder &&
 
1148
            wxStrcmp(data->m_TokenName, saved->m_TokenName) == 0 &&
 
1149
            data->m_TokenKind == saved->m_TokenKind)
 
1150
        {
 
1151
            wxTreeItemIdValue cookie;
 
1152
            parent = item;
 
1153
            item = m_pTreeTop->GetFirstChild(item, cookie);
 
1154
            m_SelectedPath.pop_front();
 
1155
        }
 
1156
        else
 
1157
            item = m_pTreeTop->GetNextSibling(item);
 
1158
    }
 
1159
 
 
1160
    m_pTreeTop->SelectItem(parent, true);
 
1161
    m_pTreeTop->EnsureVisible(parent);
 
1162
    m_SelectedPath.clear();
 
1163
}