File: new_item_dlg.cpp

package info (click to toggle)
codelite 12.0+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 95,112 kB
  • sloc: cpp: 424,040; ansic: 18,284; php: 9,569; lex: 4,186; yacc: 2,820; python: 2,294; sh: 312; makefile: 51; xml: 13
file content (218 lines) | stat: -rw-r--r-- 7,645 bytes parent folder | download | duplicates (2)
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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2008 by Eran Ifrah
// file name            : new_item_dlg.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.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// C++ code generated with wxFormBuilder (version Feb  1 2007)
// http://www.wxformbuilder.org/
//
// PLEASE DO "NOT" EDIT THIS FILE!
///////////////////////////////////////////////////////////////////////////

#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__

#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif //WX_PRECOMP

#include "new_item_dlg.h"
#include "pluginmanager.h"
#include "windowattrmanager.h"
#include "globals.h"
#include "wx/xrc/xmlres.h"
#include "wx/textctrl.h"
#include "dirsaver.h"
#include "macros.h"
#include <wx/imaglist.h>

static const wxString FileTypeCpp = wxT("C++ Source File (.cpp)");
static const wxString FileTypeC = wxT("C Source File (.c)");
static const wxString FileTypeHeader = wxT("Header File (.h)");
static const wxString FileTypeAny = wxT("Any File");

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

BEGIN_EVENT_TABLE(NewItemDlg, NewItemBaseDlg)
    EVT_CHAR_HOOK(NewItemDlg::OnCharHook)
END_EVENT_TABLE()

NewItemDlg::NewItemDlg( wxWindow* parent, wxString cwd)
    : NewItemBaseDlg(parent)
{
    m_cwd = cwd;
    m_fileType->InsertColumn(0, _("File Type"));
    m_fileType->SetColumnWidth(0, 300);

    // Initialise images map
    wxImageList *images = new wxImageList(16, 16, true);
    BitmapLoader *bmpLoader = PluginManager::Get()->GetStdIcons();
    images->Add(bmpLoader->LoadBitmap(wxT("mime/16/c")));     //0
    images->Add(bmpLoader->LoadBitmap(wxT("mime/16/cpp")));   //1
    images->Add(bmpLoader->LoadBitmap(wxT("mime/16/h")));     //2
    images->Add(bmpLoader->LoadBitmap(wxT("mime/16/text")));  //3

    m_fileType->AssignImageList( images,  wxIMAGE_LIST_SMALL );

    //-----------------------------------
    // Populate the list:
    m_fileTypeValue = FileTypeCpp;

    long row = AppendListCtrlRow(m_fileType);
    SetColumnText(m_fileType, row, 0, FileTypeCpp, 1);

    row = AppendListCtrlRow(m_fileType);
    SetColumnText(m_fileType, row, 0, FileTypeC, 0);

    row = AppendListCtrlRow(m_fileType);
    SetColumnText(m_fileType, row, 0, FileTypeHeader, 2);

    row = AppendListCtrlRow(m_fileType);
    SetColumnText(m_fileType, row, 0, FileTypeAny, 3);
    m_fileTypeValue = FileTypeAny;

    //select the last item
    m_fileType->SetItemState(row, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
    m_fileType->SetItemState(row, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);

    m_location->SetValue(m_cwd);
    m_fileName->SetFocus();

    // Attach events
    ConnectEvents();
    SetName("NewItemDlg");
    WindowAttrManager::Load(this);
}

void NewItemDlg::ConnectEvents()
{
    m_cancel->Connect(m_cancel->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(NewItemDlg::OnClick), NULL, this);
    m_okButton->Connect(m_okButton->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(NewItemDlg::OnClick), NULL, this);
    m_browseBtn->Connect(m_browseBtn->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(NewItemDlg::OnClick), NULL, this);
    m_fileType->Connect(m_fileType->GetId(), wxEVT_COMMAND_LIST_ITEM_SELECTED, wxListEventHandler(NewItemDlg::OnListItemSelected), NULL, this);
}


void NewItemDlg::OnClick(wxCommandEvent &event)
{
    int id = event.GetId();
    if ( id == m_okButton->GetId() ) {
        DoCreateFile();
    } else if ( id == m_cancel->GetId() ) {
        EndModal(wxID_CANCEL);
    } else if ( id == m_browseBtn->GetId() ) {
        DirSaver ds;
        wxDirDialog *dlg = new wxDirDialog(this, _("Location:"),  m_cwd);
        if (dlg->ShowModal() == wxID_OK) {
            m_location->SetValue(dlg->GetPath());
        }
        dlg->Destroy();
    }
}

void NewItemDlg::DoCreateFile()
{
    wxString errMsg;
    if ( !Validate(errMsg) ) {
        wxMessageBox(errMsg, _("CodeLite"), wxICON_INFORMATION | wxOK);
        return;
    }

    // Construct the file name
    wxString fileName(m_fileName->GetValue());
    TrimString(fileName);

    if (m_fileTypeValue == FileTypeAny) {
        m_newFileName = wxFileName(m_location->GetValue(), fileName);
        
    } else if ( m_fileTypeValue == FileTypeC ) {
        // If user already provided suffix, dont add another one on top of it
        if (fileName.Find(wxT(".")) == wxNOT_FOUND ) {
            m_newFileName = wxFileName(m_location->GetValue(), fileName, wxT("c"));
        } else {
            m_newFileName = wxFileName(m_location->GetValue(), fileName);
        }
        
    } else if ( m_fileTypeValue == FileTypeCpp ) {
        // If user already provided suffix, dont add another one on top of it
        if (fileName.Find(wxT(".")) == wxNOT_FOUND ) {
            m_newFileName = wxFileName(m_location->GetValue(), fileName, wxT("cpp"));
        } else {
            m_newFileName = wxFileName(m_location->GetValue(), fileName);
        }
        
    } else if ( m_fileTypeValue == FileTypeHeader ) {
        // If user already provided suffix, dont add another one on top of it
        if (fileName.Find(wxT(".")) == wxNOT_FOUND ) {
            m_newFileName = wxFileName(m_location->GetValue(), fileName, wxT("h"));
        } else {
            m_newFileName = wxFileName(m_location->GetValue(), fileName);
        }
    }
    EndModal(wxID_OK);
}

bool NewItemDlg::Validate(wxString &errMsg)
{
    // make sure we have file name & path set up correctly
    wxFileName fn(m_location->GetValue() + wxFileName::GetPathSeparator());

    if ( m_location->GetValue().Trim().IsEmpty() ) {
        errMsg = _("Missing location");
        return false;
    }

    fn = wxFileName(m_location->GetValue(), m_fileName->GetValue());
    if ( fn.FileExists() ) {
        errMsg = _("A file with that name already exists. Please choose a different name");
        return false;
    }

    if ( m_fileName->GetValue().Trim().IsEmpty() ) {
        errMsg = _("Missing file name");
        return false;
    }

    return true;
}

void NewItemDlg::OnListItemSelected(wxListEvent &event)
{
    m_fileTypeValue = event.GetText();
}

void NewItemDlg::OnCharHook(wxKeyEvent &event)
{
    if (event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER) {
        DoCreateFile();
    }
    event.Skip();
}

NewItemDlg::~NewItemDlg()
{
    
}