~skiter23/codeblocks/Stable

« back to all changes in this revision

Viewing changes to plugins/codecompletion/insertclassmethoddlg.cpp

  • Committer: skiter
  • Date: 2008-04-13 02:17:50 UTC
  • Revision ID: skiter@anonymous-20080413021750-idptbpxvdui6ms6y
Initial import

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: 4909 $
 
6
 * $Id: insertclassmethoddlg.cpp 4909 2008-02-27 13:15:26Z mortenmacfly $
 
7
 * $HeadURL: svn://svn.berlios.de/codeblocks/tags/8.02/src/plugins/codecompletion/insertclassmethoddlg.cpp $
 
8
 */
 
9
 
 
10
#include "sdk.h"
 
11
#ifndef CB_PRECOMP
 
12
#include <wx/checkbox.h>
 
13
#include <wx/checklst.h>
 
14
#include <wx/intl.h>
 
15
#include <wx/listbox.h>
 
16
#include <wx/radiobox.h>
 
17
#include <wx/xrc/xmlres.h>
 
18
#include "globals.h"
 
19
#include "manager.h"
 
20
#include "logmanager.h"
 
21
#endif
 
22
#include "parser/parser.h"
 
23
#include "insertclassmethoddlg.h"
 
24
 
 
25
BEGIN_EVENT_TABLE(InsertClassMethodDlg, wxDialog)
 
26
    EVT_LISTBOX(XRCID("lstClasses"), InsertClassMethodDlg::OnClassesChange)
 
27
    EVT_RADIOBOX(XRCID("rbCode"), InsertClassMethodDlg::OnCodeChange)
 
28
    EVT_CHECKBOX(XRCID("chkPrivate"), InsertClassMethodDlg::OnFilterChange)
 
29
    EVT_CHECKBOX(XRCID("chkProtected"), InsertClassMethodDlg::OnFilterChange)
 
30
    EVT_CHECKBOX(XRCID("chkPublic"), InsertClassMethodDlg::OnFilterChange)
 
31
END_EVENT_TABLE()
 
32
 
 
33
InsertClassMethodDlg::InsertClassMethodDlg(wxWindow* parent, Parser* parser, const wxString& filename)
 
34
    : m_pParser(parser),
 
35
    m_Decl(true),
 
36
    m_Filename(filename)
 
37
{
 
38
        //ctor
 
39
        wxXmlResource::Get()->LoadDialog(this, parent, _T("dlgInsertClassMethod"));
 
40
    XRCCTRL(*this, "rbCode", wxRadioBox)->SetSelection(0);
 
41
    FillClasses();
 
42
}
 
43
 
 
44
InsertClassMethodDlg::~InsertClassMethodDlg()
 
45
{
 
46
        //dtor
 
47
}
 
48
 
 
49
wxArrayString InsertClassMethodDlg::GetCode() const
 
50
{
 
51
    wxArrayString array;
 
52
    const wxCheckListBox* clb = XRCCTRL(*this, "chklstMethods", wxCheckListBox);
 
53
 
 
54
    for (int i = 0; i < (int)clb->GetCount(); ++i)
 
55
    {
 
56
        if (clb->IsChecked(i))
 
57
        {
 
58
            wxString str;
 
59
            if (XRCCTRL(*this, "chkAddDoc", wxCheckBox)->IsChecked())
 
60
            {
 
61
                // add doc block
 
62
                str << _T("/** @brief (one liner)\n  *\n  * (documentation goes here)\n  */\n");
 
63
            }
 
64
            str << clb->GetString(i);
 
65
            str.Replace(_T("&&"), _T("&"));
 
66
            array.Add(str + (m_Decl ? _T(";\n") : _T("\n{\n\n}\n\n")));
 
67
        }
 
68
    }
 
69
 
 
70
    return array;
 
71
} // end of GetCode
 
72
 
 
73
void InsertClassMethodDlg::FillClasses()
 
74
{
 
75
    if (!m_pParser || !m_pParser->Done())
 
76
        return;
 
77
 
 
78
    wxListBox* lb = XRCCTRL(*this, "lstClasses", wxListBox);
 
79
    lb->Freeze();
 
80
    lb->Clear();
 
81
    TokensTree* tree = m_pParser->GetTokens();
 
82
    for (size_t i = 0; i < tree->size(); ++i)
 
83
    {
 
84
        Token* token = tree->at(i);
 
85
        //Manager::Get()->GetLogManager()->DebugLog("m_Filename=%s, token=%s", m_Filename.c_str(), token->m_Filename.c_str());
 
86
        if (token && (token->m_TokenKind & (tkClass | tkTypedef))) //&&
 
87
            //token->m_Filename == UnixFilename(m_Filename))
 
88
            // TODO: check against file's pair too
 
89
            lb->Append(token->m_Name, token);
 
90
    }
 
91
    lb->Thaw();
 
92
    FillMethods();
 
93
}
 
94
 
 
95
void InsertClassMethodDlg::FillMethods()
 
96
{
 
97
    if (!m_pParser || !m_pParser->Done())
 
98
        return;
 
99
 
 
100
    wxListBox* lb = XRCCTRL(*this, "lstClasses", wxListBox);
 
101
    wxCheckListBox* clb = XRCCTRL(*this, "chklstMethods", wxCheckListBox);
 
102
    clb->Clear();
 
103
 
 
104
    if (lb->GetSelection() == -1)
 
105
        return;
 
106
 
 
107
    bool includePrivate = XRCCTRL(*this, "chkPrivate", wxCheckBox)->IsChecked();
 
108
    bool includeProtected = XRCCTRL(*this, "chkProtected", wxCheckBox)->IsChecked();
 
109
    bool includePublic = XRCCTRL(*this, "chkPublic", wxCheckBox)->IsChecked();
 
110
 
 
111
    Token* parentToken = reinterpret_cast<Token*>(lb->GetClientData(lb->GetSelection()));
 
112
 
 
113
    clb->Freeze();
 
114
    DoFillMethodsFor(clb,
 
115
                    parentToken,
 
116
                    parentToken ? parentToken->m_Name + _T("::") : _T(""),
 
117
                    includePrivate,
 
118
                    includeProtected,
 
119
                    includePublic);
 
120
    clb->Thaw();
 
121
}
 
122
 
 
123
void InsertClassMethodDlg::DoFillMethodsFor(wxCheckListBox* clb,
 
124
                                            Token* parentToken,
 
125
                                            const wxString& ns,
 
126
                                            bool includePrivate,
 
127
                                            bool includeProtected,
 
128
                                            bool includePublic)
 
129
{
 
130
    if (!parentToken)
 
131
        return;
 
132
    TokensTree* tree = parentToken->GetTree();
 
133
    if (!tree)
 
134
        return;
 
135
    //Manager::Get()->GetLogManager()->DebugLog("Fill methods for %s", parentToken->m_DisplayName.c_str());
 
136
 
 
137
    // loop ascending the inheritance tree
 
138
 
 
139
    for (TokenIdxSet::iterator it = parentToken->m_Children.begin(); it != parentToken->m_Children.end(); ++it)
 
140
    {
 
141
        int idx = *it;
 
142
        Token* token = tree->at(idx);
 
143
        if (!token)
 
144
            continue;
 
145
 
 
146
        //Manager::Get()->GetLogManager()->DebugLog("Evaluating %s", token->m_DisplayName.c_str());
 
147
        bool valid = token->m_TokenKind & (tkFunction | tkConstructor | tkDestructor) &&
 
148
                ((includePrivate && token->m_Scope == tsPrivate) ||
 
149
                (includeProtected && token->m_Scope == tsProtected) ||
 
150
                (includePublic && token->m_Scope == tsPublic));
 
151
        if (valid)
 
152
        {
 
153
            //Manager::Get()->GetLogManager()->DebugLog("Adding %s", token->m_DisplayName.c_str());
 
154
            // BUG IN WXWIDGETS: wxCheckListBox::Append(string, data) crashes...
 
155
            //                   wxCheckListBox::Append(string) does not...
 
156
            wxString str;
 
157
            str << token->m_Type << _T(" ") << ns << token->m_Name << token->m_Args;
 
158
            str.Replace(_T("&"), _T("&&"));
 
159
            if (clb->FindString(str) == wxNOT_FOUND)
 
160
                clb->Append(str);
 
161
        }
 
162
    }
 
163
 
 
164
    // inheritance
 
165
    for (TokenIdxSet::iterator it = parentToken->m_DirectAncestors.begin();it!=parentToken->m_DirectAncestors.end(); ++it)
 
166
    {
 
167
        int idx = *it;
 
168
        Token* token = tree->at(idx);
 
169
        if (!token)
 
170
            continue;
 
171
        DoFillMethodsFor(clb, token, ns, includePrivate, includeProtected, includePublic);
 
172
    }
 
173
} // end of DoFillMethodsFor
 
174
 
 
175
// events
 
176
 
 
177
void InsertClassMethodDlg::OnClassesChange(wxCommandEvent& /*event*/)
 
178
{
 
179
    FillMethods();
 
180
}
 
181
 
 
182
void InsertClassMethodDlg::OnCodeChange(wxCommandEvent& /*event*/)
 
183
{
 
184
    m_Decl = XRCCTRL(*this, "rbCode", wxRadioBox)->GetSelection() == 0;
 
185
}
 
186
 
 
187
void InsertClassMethodDlg::OnFilterChange(wxCommandEvent& /*event*/)
 
188
{
 
189
    FillMethods();
 
190
}