File: open_resource_dialog.cpp

package info (click to toggle)
codelite 6.1.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 48,992 kB
  • ctags: 43,502
  • sloc: cpp: 334,263; ansic: 18,441; xml: 4,713; yacc: 2,653; lex: 2,449; python: 1,188; sh: 385; makefile: 40
file content (512 lines) | stat: -rw-r--r-- 18,224 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2014 The CodeLite Team
// file name            : open_resource_dialog.cpp
//
// -------------------------------------------------------------------------
// A
//              _____           _      _     _ _
//             /  __ \         | |    | |   (_) |
//             | /  \/ ___   __| | ___| |    _| |_ ___
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
//              \____/\___/ \__,_|\___\_____/_|\__\___|
//
//                                                  F i l e
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

#include "open_resource_dialog.h"
#include "bitmap_loader.h"
#include <wx/imaglist.h>
#include <wx/xrc/xmlres.h>
#include "globals.h"
#include "window_locker.h"
#include "editor_config.h"
#include "ieditor.h"
#include "ctags_manager.h"
#include <wx/filefn.h>
#include "project.h"
#include "workspace.h"
#include <wx/wupdlock.h>
#include "imanager.h"
#include "windowattrmanager.h"
#include <vector>
#include <codelite_events.h>

BEGIN_EVENT_TABLE(OpenResourceDialog, OpenResourceDialogBase)
    EVT_TIMER(XRCID("OR_TIMER"), OpenResourceDialog::OnTimer)
END_EVENT_TABLE()

OpenResourceDialog::OpenResourceDialog( wxWindow* parent, IManager *manager)
    : OpenResourceDialogBase( parent )
    , m_manager(manager)
    , m_needRefresh(false)
{
    Hide();
    // Create an image list
    wxImageList *li = new wxImageList(16, 16, true);
    BitmapLoader *bmpLoader = m_manager->GetStdIcons();

    m_tagImgMap[wxT("class")]              = li->Add(bmpLoader->LoadBitmap(wxT("cc/16/class")));
    m_tagImgMap[wxT("struct")]             = li->Add(bmpLoader->LoadBitmap(wxT("cc/16/struct")));
    m_tagImgMap[wxT("namespace")]          = li->Add(bmpLoader->LoadBitmap(wxT("cc/16/namespace")));
    m_tagImgMap[wxT("typedef")]            = li->Add(bmpLoader->LoadBitmap(wxT("cc/16/typedef")));
    m_tagImgMap[wxT("member_private")]     = li->Add(bmpLoader->LoadBitmap(wxT("cc/16/member_private")));
    m_tagImgMap[wxT("member_public")]      = li->Add(bmpLoader->LoadBitmap(wxT("cc/16/member_public")));
    m_tagImgMap[wxT("member_protected")]   = li->Add(bmpLoader->LoadBitmap(wxT("cc/16/member_protected")));
    m_tagImgMap[wxT("function_private")]   = li->Add(bmpLoader->LoadBitmap(wxT("cc/16/function_private")));
    m_tagImgMap[wxT("function_public")]    = li->Add(bmpLoader->LoadBitmap(wxT("cc/16/function_public")));
    m_tagImgMap[wxT("function_protected")] = li->Add(bmpLoader->LoadBitmap(wxT("cc/16/function_protected")));
    m_tagImgMap[wxT("enum")]               = li->Add(bmpLoader->LoadBitmap(wxT("cc/16/enum")));
    m_tagImgMap[wxT("enumerator")]         = li->Add(bmpLoader->LoadBitmap(wxT("cc/16/enumerator")));
    m_tagImgMap[wxT("cpp")]                = li->Add(bmpLoader->LoadBitmap(wxT("mime/16/cpp")));
    m_tagImgMap[wxT("h")]                  = li->Add(bmpLoader->LoadBitmap(wxT("mime/16/h")));
    m_tagImgMap[wxT("text")]               = li->Add(bmpLoader->LoadBitmap(wxT("mime/16/text")));
    m_tagImgMap[wxT("c")]                  = li->Add(bmpLoader->LoadBitmap(wxT("mime/16/c")));
    m_tagImgMap[wxT("wxfb")]               = li->Add(bmpLoader->LoadBitmap(wxT("mime/16/wxfb")));
    m_tagImgMap[wxT("wxcp")]               = li->Add(bmpLoader->LoadBitmap(wxT("mime/16/wxcp")));

    m_listOptions->AssignImageList(li, wxIMAGE_LIST_SMALL);

    m_timer = new wxTimer(this, XRCID("OR_TIMER"));
    MSWSetNativeTheme(m_listOptions);
    m_listOptions->InsertColumn(0, wxT("Name"));
    m_listOptions->InsertColumn(1, wxT("Full path"));

    m_textCtrlResourceName->SetFocus();
    SetLabel(_("Open resource..."));

    WindowAttrManager::Load(this, wxT("OpenResourceDialog"), m_manager->GetConfigTool());

    //load all files from the workspace
    if ( m_manager->IsWorkspaceOpen() ) {
        wxArrayString projects;
        m_manager->GetWorkspace()->GetProjectList( projects );

        for ( size_t i=0; i<projects.GetCount(); i++ ) {
            std::vector<wxFileName> fileNames;
            wxString errmsg;
            ProjectPtr p = m_manager->GetWorkspace()->FindProjectByName(projects.Item(i), errmsg);

            if ( p ) {
                p->GetFiles(fileNames, true);

                //convert std::vector to wxArrayString
                for ( std::vector<wxFileName>::iterator it = fileNames.begin(); it != fileNames.end(); it ++ ) {
                    wxString name = it->GetFullName().MakeLower();
                    m_files.insert(std::make_pair(name, it->GetFullPath()) );
                }
            }
        }
    }
    m_listOptions->Connect( wxEVT_COMMAND_LIST_ITEM_ACTIVATED, wxListEventHandler( OpenResourceDialog::OnItemActivated ), NULL, this );
    m_listOptions->Connect( wxEVT_COMMAND_LIST_ITEM_SELECTED,  wxListEventHandler( OpenResourceDialog::OnItemSelected ), NULL, this );
}

OpenResourceDialog::~OpenResourceDialog()
{
    m_timer->Stop();
    delete m_timer;
    WindowAttrManager::Save(this, wxT("OpenResourceDialog"), m_manager->GetConfigTool());
}

void OpenResourceDialog::OnText( wxCommandEvent& event )
{
    event.Skip();
    m_timer->Stop();
    m_timer->Start(200, true);

    wxString filter = m_textCtrlResourceName->GetValue();
    filter.Trim().Trim(false);

    if(filter.IsEmpty()) {
        // The filter content is cleared, delete all entries
        Clear();
        m_needRefresh = false;

    } else {
        m_needRefresh = true;

    }
}

void OpenResourceDialog::OnUsePartialMatching( wxCommandEvent& event )
{
    wxUnusedVar(event);
    DoPopulateList();
    m_textCtrlResourceName->SetFocus();
}

void OpenResourceDialog::OnEnter(wxCommandEvent& event)
{
    wxUnusedVar(event);
    int sel = m_listOptions->GetFirstSelected();
    if (sel != wxNOT_FOUND) {
        OpenResourceDialogItemData *data = (OpenResourceDialogItemData *) m_listOptions->GetItemData(sel);
        if (data) {
            m_selection = *data;
            EndModal(wxID_OK);
        }
    }
}

void OpenResourceDialog::OnItemActivated(wxListEvent& event)
{
    int sel = event.m_itemIndex;
    if (sel != wxNOT_FOUND) {
        OpenResourceDialogItemData *data = (OpenResourceDialogItemData *) m_listOptions->GetItemData(sel);
        if (data) {
            m_selection = *data;
            EndModal(wxID_OK);
        }
    }
}

void OpenResourceDialog::DoPopulateList()
{
    wxString name = m_textCtrlResourceName->GetValue();
    name.Trim().Trim(false);
    if(name.IsEmpty())
        return;

    Clear();

    wxWindowUpdateLocker locker(m_listOptions);

    // First add the workspace files
    
    // Prepare the user filter
    m_userFilters.Clear();
    m_userFilters = ::wxStringTokenize(name, " \t", wxTOKEN_STRTOK);
    for(size_t i=0; i<m_userFilters.GetCount(); ++i) {
        m_userFilters.Item(i).MakeLower();
    }
    
    DoPopulateWorkspaceFile();
    DoPopulateTags();

    if(m_listOptions->GetItemCount()) {
        m_listOptions->SetColumnWidth(0, wxLIST_AUTOSIZE);
        m_listOptions->SetColumnWidth(1, wxLIST_AUTOSIZE);
    } else {
        m_listOptions->SetColumnWidth(0, wxLIST_AUTOSIZE_USEHEADER);
        m_listOptions->SetColumnWidth(1, wxLIST_AUTOSIZE_USEHEADER);
    }
}

void OpenResourceDialog::DoPopulateTags()
{
    bool gotExactMatch(false);

    // Next, add the tags
    TagEntryPtrVector_t tags;
    if ( m_userFilters.IsEmpty() )
        return;
    
    m_manager->GetTagsManager()->GetTagsByPartialName(m_userFilters.Item(0), tags);

    for (size_t i=0; i<tags.size(); i++) {
        TagEntryPtr tag = tags.at(i);
        
        // Filter out non relevanting entries
        if ( !m_filters.IsEmpty() && m_filters.Index(tag->GetKind()) == wxNOT_FOUND )
            continue;
        
        if ( !MatchesFilter( tag->GetName()) )
            continue;
            
        wxString name(tag->GetName());

        // keep the fullpath
        int index(0);
        wxString fullname;
        if(tag->GetKind() == wxT("function") || tag->GetKind() == wxT("prototype")) {
            fullname = wxString::Format(wxT("%s::%s%s"), tag->GetScope().c_str(), tag->GetName().c_str(), tag->GetSignature().c_str());
            index = DoAppendLine(tag->GetName(),
                                 fullname,
                                 (tag->GetKind() == wxT("function")),
                                 new OpenResourceDialogItemData(tag->GetFile(), tag->GetLine(), tag->GetPattern(), tag->GetName(), tag->GetScope()),
                                 DoGetTagImgId(tag));
        } else {

            fullname = wxString::Format(wxT("%s::%s"), tag->GetScope().c_str(), tag->GetName().c_str());
            index = DoAppendLine(tag->GetName(),
                                 fullname,
                                 false,
                                 new OpenResourceDialogItemData(tag->GetFile(), tag->GetLine(), tag->GetPattern(), tag->GetName(), tag->GetScope()),
                                 DoGetTagImgId(tag));

        }
        
        if ((m_userFilters.GetCount() == 1) && (m_userFilters.Item(0).CmpNoCase(name) == 0) && !gotExactMatch) {
            gotExactMatch = true;
            DoSelectItem(index);
        }
    }

    if (!gotExactMatch && m_listOptions->GetItemCount()) {
        DoSelectItem(0);
    }
}

void OpenResourceDialog::DoPopulateWorkspaceFile()
{
    // do we need to include files?
    if ( !m_filters.IsEmpty() && m_filters.Index(TagEntry::KIND_FILE) == wxNOT_FOUND)
        return;
    
    if ( !m_userFilters.IsEmpty() ) {

        std::multimap<wxString, wxString>::iterator iter  = m_files.begin();
        for(; iter != m_files.end(); iter++) {
            
            if ( !MatchesFilter( iter->first ) )
                continue;
            
            wxFileName fn(iter->second);
            FileExtManager::FileType type = FileExtManager::GetType(fn.GetFullName());
            int imgId = m_tagImgMap[wxT("text")];
            switch(type) {
            case FileExtManager::TypeSourceC:
                imgId = m_tagImgMap[wxT("c")];
                break;

            case FileExtManager::TypeSourceCpp:
                imgId = m_tagImgMap[wxT("cpp")];
                break;
            case FileExtManager::TypeHeader:
                imgId = m_tagImgMap[wxT("h")];
                break;
            case FileExtManager::TypeFormbuilder:
                imgId = m_tagImgMap[wxT("wxfb")];
                break;
            case FileExtManager::TypeWxCrafter:
                imgId = m_tagImgMap[wxT("wxcp")];
                break;
            default:
                break;
            }
            DoAppendLine(fn.GetFullName(),
                         fn.GetFullPath(),
                         false,
                         new OpenResourceDialogItemData(fn.GetFullPath(), -1, wxT(""), fn.GetFullName(), wxT("")),
                         imgId);
        }
    }
}

void OpenResourceDialog::Clear()
{
    // list control does not own the client data, we need to free it ourselves
    for(int i=0; i<m_listOptions->GetItemCount(); i++) {
        OpenResourceDialogItemData *data = (OpenResourceDialogItemData *) m_listOptions->GetItemData(i);
        if (data) {
            delete data;
        }
    }
    m_listOptions->DeleteAllItems();
    m_userFilters.Clear();
}

void OpenResourceDialog::OpenSelection(const OpenResourceDialogItemData& selection, IManager* manager)
{
    // send event to the plugins to see if they want
    // to open this file
    wxString file_path = selection.m_file;
    if (SendCmdEvent(wxEVT_TREE_ITEM_FILE_ACTIVATED, &file_path)) {
        return;
    }
    
    if ( manager && manager->OpenFile(selection.m_file, wxEmptyString, selection.m_line) ) {
        IEditor *editor = manager->GetActiveEditor();
        if ( editor && !selection.m_name.IsEmpty() && !selection.m_pattern.IsEmpty()) {
            editor->FindAndSelectV(selection.m_pattern, selection.m_name);
        }
    }
}

void OpenResourceDialog::OnKeyDown(wxKeyEvent& event)
{
    if (event.GetKeyCode() == WXK_DOWN && m_listOptions->GetItemCount()> 0) {
        //up key
        int cursel = m_listOptions->GetFirstSelected();
        if (cursel != wxNOT_FOUND) {
            //there is a selection in the listbox
            cursel++;
            if (cursel >= (int)m_listOptions->GetItemCount()) {
                //already at last item, cant scroll anymore
                return;
            }
            DoSelectItem(cursel);

        } else {
            //no selection is made
            DoSelectItem(0);
        }
        return;

    } else if (event.GetKeyCode() == WXK_UP && m_listOptions->GetItemCount() > 0) {
        //up key
        int cursel = m_listOptions->GetFirstSelected();
        if (cursel != wxNOT_FOUND) {
            //there is a selection in the listbox
            cursel--;
            if (cursel < 0) {
                //already at first item, cant scroll anymore
                return;
            }
            DoSelectItem(cursel);

        } else {
            //no selection is made
            DoSelectItem(0);
        }
        return;

    } else
        event.Skip();
}

void OpenResourceDialog::OnOK(wxCommandEvent& event)
{
    event.Skip();
}

void OpenResourceDialog::OnOKUI(wxUpdateUIEvent& event)
{
    event.Enable(m_selection.IsOk());
}


bool OpenResourceDialogItemData::IsOk() const
{
    return m_file.IsEmpty() == false;
}

void OpenResourceDialog::DoSelectItem(int selection, bool makeFirst)
{
    // Unselect current item first
    int currentSelection = m_listOptions->GetFirstSelected();
    if(currentSelection != wxNOT_FOUND) {
        m_listOptions->Select(currentSelection, false);
    }

    m_listOptions->Select(selection);
    if(makeFirst)
        m_listOptions->EnsureVisible(selection);

    // display the full name at the bottom static text control
    OpenResourceDialogItemData *data = (OpenResourceDialogItemData *) m_listOptions->GetItemData(selection);
    m_selection = *data;
}

void OpenResourceDialog::OnItemSelected(wxListEvent& event)
{
    event.Skip();
    if(event.m_itemIndex != wxNOT_FOUND) {
        // display the full name at the bottom static text control
        OpenResourceDialogItemData *data = (OpenResourceDialogItemData *) m_listOptions->GetItemData(event.m_itemIndex );
        m_selection = *data;
    }
}

int OpenResourceDialog::DoAppendLine(const wxString& name, const wxString& fullname, bool boldFont, OpenResourceDialogItemData* clientData, int imgId)
{
    int index = AppendListCtrlRow(m_listOptions);
    SetColumnText(m_listOptions, index, 0, name, imgId);
    SetColumnText(m_listOptions, index, 1, fullname);

    m_listOptions->SetItemPtrData(index, (wxUIntPtr)(clientData));

    // Mark implementations with bold font
    wxFont font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
    if(boldFont)
        font.SetWeight(wxFONTWEIGHT_BOLD);
    m_listOptions->SetItemFont(index, font);
    return index;
}

void OpenResourceDialog::OnTimer(wxTimerEvent& event)
{
    if(m_needRefresh)
        DoPopulateList();

    m_needRefresh = false;
}

int OpenResourceDialog::DoGetTagImgId(TagEntryPtr tag)
{
    wxString kind   = tag->GetKind();
    wxString access = tag->GetAccess();
    int imgId = m_tagImgMap[wxT("text")];
    if (kind == wxT("class"))
        imgId = m_tagImgMap[wxT("class")];

    if (kind == wxT("struct"))
        imgId = m_tagImgMap[wxT("struct")];

    if (kind == wxT("namespace"))
        imgId = m_tagImgMap[wxT("namespace")];

    if (kind == wxT("variable"))
        imgId = m_tagImgMap[wxT("member_public")];

    if (kind == wxT("typedef"))
        imgId = m_tagImgMap[wxT("typedef")];

    if (kind == wxT("member") && access.Contains(wxT("private")))
        imgId = m_tagImgMap[wxT("member_private")];

    if (kind == wxT("member") && access.Contains(wxT("public")))
        imgId = m_tagImgMap[wxT("member_public")];

    if (kind == wxT("member") && access.Contains(wxT("protected")))
        imgId = m_tagImgMap[wxT("member_protected")];

    if (kind == wxT("member"))
        imgId = m_tagImgMap[wxT("member_public")];

    if ((kind == wxT("function") || kind == wxT("prototype")) && access.Contains(wxT("private")))
        imgId = m_tagImgMap[wxT("function_private")];

    if ((kind == wxT("function") || kind == wxT("prototype")) && (access.Contains(wxT("public")) || access.IsEmpty()))
        imgId = m_tagImgMap[wxT("function_public")];

    if ((kind == wxT("function") || kind == wxT("prototype")) && access.Contains(wxT("protected")))
        imgId = m_tagImgMap[wxT("function_protected")];

    if (kind == wxT("macro"))
        imgId = m_tagImgMap[wxT("typedef")];

    if (kind == wxT("enum"))
        imgId = m_tagImgMap[wxT("enum")];

    if (kind == wxT("enumerator"))
        imgId = m_tagImgMap[wxT("enumerator")];

    return imgId;
}

bool OpenResourceDialog::MatchesFilter(const wxString& name)
{
    wxString tmpname = name;
    tmpname.MakeLower();
    
    if ( m_userFilters.IsEmpty() )
        return false;
    
    
    for(size_t i=0; i<m_userFilters.GetCount(); ++i) {
        if ( !tmpname.Contains(m_userFilters.Item(i)) ) 
            return false;
    }
    return true;
}