summaryrefslogtreecommitdiff
path: root/LiteEditor/navbar.cpp
blob: eae3ee1af50df60e0c1f9589414aecaaab5e1b6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2008 by Eran Ifrah
// file name            : navbar.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 <vector>
#include "editor_config.h"
#include "ctags_manager.h"
#include "cl_editor.h"
#include "manager.h"
#include "frame.h"
#include "navbar.h"
#include "event_notifier.h"
#include "globals.h"

NavBar::NavBar(wxWindow* parent)
    : NavBarControlBaseClass(parent)
{
#ifdef __WXMAC__
    m_scope->SetWindowVariant(wxWINDOW_VARIANT_SMALL);
    m_func->SetWindowVariant(wxWINDOW_VARIANT_SMALL);
#endif

    long sashPos = EditorConfigST::Get()->GetInteger(wxT("NavBarSashPos"));
    if(sashPos != wxNOT_FOUND) {
        m_splitter->SetSashPosition(sashPos);
    }

    EventNotifier::Get()->Connect(wxEVT_FILE_SAVED, clCommandEventHandler(NavBar::OnFileSaved), NULL, this);
    EventNotifier::Get()->Connect(
        wxEVT_ACTIVE_EDITOR_CHANGED, wxCommandEventHandler(NavBar::OnEditorChanged), NULL, this);
}

NavBar::~NavBar()
{
    EditorConfigST::Get()->SetInteger(wxT("NavBarSashPos"), m_splitter->GetSashPosition());
    EventNotifier::Get()->Disconnect(wxEVT_FILE_SAVED, clCommandEventHandler(NavBar::OnFileSaved), NULL, this);
    EventNotifier::Get()->Disconnect(
        wxEVT_ACTIVE_EDITOR_CHANGED, wxCommandEventHandler(NavBar::OnEditorChanged), NULL, this);
}

void NavBar::OnScope(wxCommandEvent& e)
{
    size_t sel = e.GetSelection();
    if(sel < m_scope->GetCount()) {
        m_tags.clear();
        m_func->Clear();
    }

    wxString scope = m_scope->GetString(sel);
    DoPopulateFunctions(DoGetCurFileName(), scope);
}

void NavBar::OnFunction(wxCommandEvent& e)
{
    LEditor* editor = clMainFrame::Get()->GetMainBook()->GetActiveEditor();
    if(!editor) return;

    size_t sel = e.GetSelection();
    if(sel >= m_tags.size()) return;

    wxString pattern = m_tags[sel]->GetPattern();
    wxString name = m_tags[sel]->GetName();
    editor->FindAndSelect(pattern, name);
    editor->SetActive();
}

void NavBar::DoShow(bool s)
{
    if(Show(s)) {
        GetParent()->GetSizer()->Layout();
    }
}

void NavBar::UpdateScope(TagEntryPtr tag)
{
    size_t sel = m_func->GetSelection();
    if(tag && sel < m_tags.size() && *m_tags[sel] == *tag) return;

    wxWindowUpdateLocker locker(this);

    m_tags.clear();
    m_scope->Clear();
    m_func->Clear();

    if(tag) {
        m_tags.push_back(tag);
        m_scope->AppendString(tag->GetScope());
        m_func->AppendString(tag->GetDisplayName());
        m_scope->SetSelection(0);
        m_func->SetSelection(0);

        DoPopulateTags(DoGetCurFileName());
    }
}

void NavBar::OnFileSaved(clCommandEvent& e)
{
    e.Skip();

    // sanity
    if(!ManagerST::Get()->IsWorkspaceOpen()) return;

    // get the active editor
    LEditor* editor = clMainFrame::Get()->GetMainBook()->GetActiveEditor();
    if(!editor) return;

    // is this the current file?
    wxFileName fn(e.GetString());
    if(fn != editor->GetFileName()) {
        return;
    }

    // The save was for the active file, re-popuplate the tags in the navigation bar
    DoPopulateTags(fn);
}

void NavBar::DoPopulateTags(const wxFileName& fn)
{
    std::vector<wxString> scopes;
    TagsManagerST::Get()->GetScopesFromFile(fn, scopes);

    m_scope->Freeze();

    wxString cursel = m_scope->GetStringSelection();
    m_scope->Clear();
    for(unsigned i = 0; i < scopes.size(); i++) {
        m_scope->AppendString(scopes[i]);
    }
    if(!cursel.IsEmpty()) {
        m_scope->SetStringSelection(cursel);
    }

    m_scope->Thaw();

    wxString scope = m_scope->GetStringSelection();
    if(scope.IsEmpty()) return;
    DoPopulateFunctions(fn, scope);
}

void NavBar::OnEditorChanged(wxCommandEvent& e)
{
    e.Skip();
    IEditor* editor = ::clGetManager()->GetActiveEditor();
    CHECK_PTR_RET(editor);

    const wxFileName& fn = editor->GetFileName();
    DoPopulateTags(fn);
}

void NavBar::DoPopulateFunctions(const wxFileName& fn, const wxString& scope)
{
    m_tags.clear();
    TagsManagerST::Get()->TagsFromFileAndScope(fn, scope, m_tags);

    m_func->Freeze();

    wxString func_cursel = m_func->GetStringSelection();
    m_func->Clear();
    for(size_t i = 0; i < m_tags.size(); i++) {
        m_func->AppendString(m_tags.at(i)->GetDisplayName());
    }
    if(!func_cursel.IsEmpty()) {
        m_func->SetStringSelection(func_cursel);
    }

    m_func->Thaw();
}

wxFileName NavBar::DoGetCurFileName() const
{
    LEditor* editor = clMainFrame::Get()->GetMainBook()->GetActiveEditor();
    if(!editor) return wxFileName();

    const wxFileName& fn = editor->GetFileName();
    return fn;
}