~ubuntu-branches/ubuntu/lucid/codelite/lucid

« back to all changes in this revision

Viewing changes to LiteEditor/new_item_dlg.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2009-01-12 15:46:55 UTC
  • Revision ID: james.westby@ubuntu.com-20090112154655-sdynrljcb6u167yw
Tags: upstream-1.0.2674+dfsg
ImportĀ upstreamĀ versionĀ 1.0.2674+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//////////////////////////////////////////////////////////////////////////////
 
2
//////////////////////////////////////////////////////////////////////////////
 
3
//
 
4
// copyright            : (C) 2008 by Eran Ifrah
 
5
// file name            : new_item_dlg.cpp
 
6
//
 
7
// -------------------------------------------------------------------------
 
8
// A
 
9
//              _____           _      _     _ _
 
10
//             /  __ \         | |    | |   (_) |
 
11
//             | /  \/ ___   __| | ___| |    _| |_ ___
 
12
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
 
13
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
 
14
//              \____/\___/ \__,_|\___\_____/_|\__\___|
 
15
//
 
16
//                                                  F i l e
 
17
//
 
18
//    This program is free software; you can redistribute it and/or modify
 
19
//    it under the terms of the GNU General Public License as published by
 
20
//    the Free Software Foundation; either version 2 of the License, or
 
21
//    (at your option) any later version.
 
22
//
 
23
//////////////////////////////////////////////////////////////////////////////
 
24
//////////////////////////////////////////////////////////////////////////////
 
25
///////////////////////////////////////////////////////////////////////////
 
26
// C++ code generated with wxFormBuilder (version Feb  1 2007)
 
27
// http://www.wxformbuilder.org/
 
28
//
 
29
// PLEASE DO "NOT" EDIT THIS FILE!
 
30
///////////////////////////////////////////////////////////////////////////
 
31
 
 
32
#include "wx/wxprec.h"
 
33
 
 
34
#ifdef __BORLANDC__
 
35
#pragma hdrstop
 
36
#endif //__BORLANDC__
 
37
 
 
38
#ifndef WX_PRECOMP
 
39
#include <wx/wx.h>
 
40
#endif //WX_PRECOMP
 
41
 
 
42
#include "new_item_dlg.h"
 
43
#include "windowattrmanager.h"
 
44
#include "globals.h"
 
45
#include "wx/xrc/xmlres.h"
 
46
#include "wx/textctrl.h"
 
47
#include "dirsaver.h"
 
48
#include "macros.h"
 
49
#include <wx/imaglist.h>
 
50
 
 
51
static const wxString FileTypeCpp = wxT("C++ Source File (.cpp)");
 
52
static const wxString FileTypeC = wxT("C Source File (.c)");
 
53
static const wxString FileTypeHeader = wxT("Header File (.h)");
 
54
static const wxString FileTypeAny = wxT("Any File");
 
55
 
 
56
///////////////////////////////////////////////////////////////////////////
 
57
 
 
58
BEGIN_EVENT_TABLE(NewItemDlg, NewItemBaseDlg)
 
59
        EVT_CHAR_HOOK(NewItemDlg::OnCharHook)
 
60
END_EVENT_TABLE()
 
61
 
 
62
NewItemDlg::NewItemDlg( wxWindow* parent, wxString cwd)
 
63
                : NewItemBaseDlg(parent)
 
64
{
 
65
        m_cwd = cwd;
 
66
        m_fileType->InsertColumn(0, _("File Type"));
 
67
        m_fileType->SetColumnWidth(0, 300);
 
68
 
 
69
        // Initialise images map
 
70
        wxImageList *images = new wxImageList(16, 16, true);
 
71
        images->Add(wxXmlResource::Get()->LoadBitmap(_T("page_white_c")));                      //0
 
72
        images->Add(wxXmlResource::Get()->LoadBitmap(_T("page_white_cplusplus")));      //1
 
73
        images->Add(wxXmlResource::Get()->LoadBitmap(_T("page_white_h")));                      //2
 
74
        images->Add(wxXmlResource::Get()->LoadBitmap(_T("page_white_text")));           //3
 
75
 
 
76
        m_fileType->AssignImageList( images,  wxIMAGE_LIST_SMALL );
 
77
 
 
78
        //-----------------------------------
 
79
        // Populate the list:
 
80
        m_fileTypeValue = FileTypeCpp;
 
81
 
 
82
        long row = AppendListCtrlRow(m_fileType);
 
83
        SetColumnText(m_fileType, row, 0, FileTypeCpp, 1);
 
84
 
 
85
        row = AppendListCtrlRow(m_fileType);
 
86
        SetColumnText(m_fileType, row, 0, FileTypeC, 0);
 
87
 
 
88
        row = AppendListCtrlRow(m_fileType);
 
89
        SetColumnText(m_fileType, row, 0, FileTypeHeader, 2);
 
90
 
 
91
        row = AppendListCtrlRow(m_fileType);
 
92
        SetColumnText(m_fileType, row, 0, FileTypeAny, 3);
 
93
        m_fileTypeValue = FileTypeAny;
 
94
 
 
95
        //select the last item
 
96
        m_fileType->SetItemState(row, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
 
97
        m_fileType->SetItemState(row, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
 
98
 
 
99
        m_location->SetValue(m_cwd);
 
100
        m_fileName->SetFocus();
 
101
 
 
102
        // Attach events
 
103
        ConnectEvents();
 
104
        WindowAttrManager::Load(this, wxT("NewItemDlg"), NULL);
 
105
}
 
106
 
 
107
void NewItemDlg::ConnectEvents()
 
108
{
 
109
        m_cancel->Connect(m_cancel->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(NewItemDlg::OnClick), NULL, this);
 
110
        m_okButton->Connect(m_okButton->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(NewItemDlg::OnClick), NULL, this);
 
111
        m_browseBtn->Connect(m_browseBtn->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(NewItemDlg::OnClick), NULL, this);
 
112
        m_fileType->Connect(m_fileType->GetId(), wxEVT_COMMAND_LIST_ITEM_SELECTED, wxListEventHandler(NewItemDlg::OnListItemSelected), NULL, this);
 
113
}
 
114
 
 
115
 
 
116
void NewItemDlg::OnClick(wxCommandEvent &event)
 
117
{
 
118
        int id = event.GetId();
 
119
        if ( id == m_okButton->GetId() )
 
120
        {
 
121
                DoCreateFile();
 
122
        }
 
123
        else if ( id == m_cancel->GetId() )
 
124
        {
 
125
                EndModal(wxID_CANCEL);
 
126
        }
 
127
        else if ( id == m_browseBtn->GetId() )
 
128
        {
 
129
                DirSaver ds;
 
130
                wxDirDialog *dlg = new wxDirDialog(this, wxT("Location:"),  m_cwd);
 
131
                if (dlg->ShowModal() == wxID_OK)
 
132
                {
 
133
                        m_location->SetValue(dlg->GetPath());
 
134
                }
 
135
                dlg->Destroy();
 
136
        }
 
137
}
 
138
 
 
139
void NewItemDlg::DoCreateFile()
 
140
{
 
141
        wxString errMsg;
 
142
        if ( !Validate(errMsg) )
 
143
        {
 
144
                wxMessageBox(errMsg, wxT("CodeLite"), wxICON_INFORMATION | wxOK);
 
145
                return;
 
146
        }
 
147
 
 
148
        // Construct the file name
 
149
        wxString fileName(m_fileName->GetValue());
 
150
        TrimString(fileName);
 
151
 
 
152
        if (m_fileTypeValue == FileTypeAny)
 
153
        {
 
154
                m_newFileName = wxFileName(m_location->GetValue(),fileName);
 
155
        }
 
156
        else if ( m_fileTypeValue == FileTypeC )
 
157
        {
 
158
                // If user already provided suffix, dont add another one on top of it
 
159
                if (fileName.Find(wxT(".")) == wxNOT_FOUND )
 
160
                {
 
161
                        m_newFileName = wxFileName(m_location->GetValue(),fileName, wxT("c"));
 
162
                }
 
163
                else
 
164
                {
 
165
                        m_newFileName = wxFileName(m_location->GetValue(),fileName);
 
166
                }
 
167
        }
 
168
        else if ( m_fileTypeValue == FileTypeCpp )
 
169
        {
 
170
                // If user already provided suffix, dont add another one on top of it
 
171
                if (fileName.Find(wxT(".")) == wxNOT_FOUND )
 
172
                {
 
173
                        m_newFileName = wxFileName(m_location->GetValue(),fileName, wxT("cpp"));
 
174
                }
 
175
                else
 
176
                {
 
177
                        m_newFileName = wxFileName(m_location->GetValue(),fileName);
 
178
                }
 
179
        }
 
180
        else if ( m_fileTypeValue == FileTypeHeader )
 
181
        {
 
182
                // If user already provided suffix, dont add another one on top of it
 
183
                if (fileName.Find(wxT(".")) == wxNOT_FOUND )
 
184
                {
 
185
                        m_newFileName = wxFileName(m_location->GetValue(),fileName, wxT("h"));
 
186
                }
 
187
                else
 
188
                {
 
189
                        m_newFileName = wxFileName(m_location->GetValue(),fileName);
 
190
                }
 
191
        }
 
192
        EndModal(wxID_OK);
 
193
}
 
194
 
 
195
bool NewItemDlg::Validate(wxString &errMsg)
 
196
{
 
197
        // make sure we have file name & path set up correctly
 
198
        wxFileName fn(m_location->GetValue() + wxFileName::GetPathSeparator());
 
199
 
 
200
        if ( m_location->GetValue().Trim().IsEmpty() )
 
201
        {
 
202
                errMsg = wxT("Missing location");
 
203
                return false;
 
204
        }
 
205
 
 
206
        if ( !fn.DirExists() )
 
207
        {
 
208
                errMsg = wxT("Directory: ");
 
209
                errMsg << fn.GetPath() << wxT(" does not exist");
 
210
                return false;
 
211
        }
 
212
 
 
213
        fn = wxFileName(m_location->GetValue(), m_fileName->GetValue());
 
214
        if ( fn.FileExists() )
 
215
        {
 
216
                errMsg = wxT("A file with that name already exist, please choose different name");
 
217
                return false;
 
218
        }
 
219
 
 
220
        if ( m_fileName->GetValue().Trim().IsEmpty() )
 
221
        {
 
222
                errMsg = wxT("Missing file name");
 
223
                return false;
 
224
        }
 
225
 
 
226
        return true;
 
227
}
 
228
 
 
229
void NewItemDlg::OnListItemSelected(wxListEvent &event)
 
230
{
 
231
        m_fileTypeValue = event.GetText();
 
232
}
 
233
 
 
234
void NewItemDlg::OnCharHook(wxKeyEvent &event)
 
235
{
 
236
        if (event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER)
 
237
        {
 
238
                DoCreateFile();
 
239
        }
 
240
        event.Skip();
 
241
}
 
242
 
 
243
NewItemDlg::~NewItemDlg()
 
244
{
 
245
        WindowAttrManager::Save(this, wxT("NewItemDlg"), NULL);
 
246
}