| 1 |
// FileTreeCtrl.cpp : implementation file |
|---|
| 2 |
// |
|---|
| 3 |
|
|---|
| 4 |
#include "stdafx.h" |
|---|
| 5 |
#include "Elephant.h" |
|---|
| 6 |
#include "FileTreeCtrl.h" |
|---|
| 7 |
|
|---|
| 8 |
#ifdef _DEBUG |
|---|
| 9 |
#define new DEBUG_NEW |
|---|
| 10 |
#undef THIS_FILE |
|---|
| 11 |
static char THIS_FILE[] = __FILE__; |
|---|
| 12 |
#endif |
|---|
| 13 |
|
|---|
| 14 |
///////////////////////////////////////////////////////////////////////////// |
|---|
| 15 |
// CFileTreeCtrl |
|---|
| 16 |
|
|---|
| 17 |
CFileTreeCtrl::CFileTreeCtrl() |
|---|
| 18 |
{ |
|---|
| 19 |
} |
|---|
| 20 |
|
|---|
| 21 |
CFileTreeCtrl::~CFileTreeCtrl() |
|---|
| 22 |
{ |
|---|
| 23 |
} |
|---|
| 24 |
|
|---|
| 25 |
BEGIN_MESSAGE_MAP(CFileTreeCtrl, CXTTreeCtrl) |
|---|
| 26 |
ON_WM_RBUTTONDOWN() |
|---|
| 27 |
ON_WM_LBUTTONDBLCLK() |
|---|
| 28 |
ON_WM_KEYUP() |
|---|
| 29 |
//{{AFX_MSG_MAP(CFileTreeCtrl) |
|---|
| 30 |
ON_WM_CREATE() |
|---|
| 31 |
ON_NOTIFY_REFLECT(TVN_ENDLABELEDIT, OnEndlabeledit) |
|---|
| 32 |
//}}AFX_MSG_MAP |
|---|
| 33 |
//ON_WM_DROPFILES() |
|---|
| 34 |
|
|---|
| 35 |
END_MESSAGE_MAP() |
|---|
| 36 |
|
|---|
| 37 |
///////////////////////////////////////////////////////////////////////////// |
|---|
| 38 |
// CFileTreeCtrl message handlers |
|---|
| 39 |
|
|---|
| 40 |
void CFileTreeCtrl::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) |
|---|
| 41 |
{ |
|---|
| 42 |
|
|---|
| 43 |
if ( nChar == VK_DELETE ) |
|---|
| 44 |
{ |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
CMainFrame* mf = static_cast<CMainFrame*>(Globals::theApp.m_pMainWnd ); |
|---|
| 48 |
if ( mf ) mf->OnFiletreeRemovefile(); |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
else if (nChar == VK_RETURN ) |
|---|
| 52 |
{ |
|---|
| 53 |
|
|---|
| 54 |
OnLButtonDblClk(0,CPoint(0,0 ) ); |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
} |
|---|
| 59 |
else if ( nChar == VK_ESCAPE ) |
|---|
| 60 |
{ |
|---|
| 61 |
|
|---|
| 62 |
CMainFrame* m = (CMainFrame*)Globals::theApp.m_pMainWnd; |
|---|
| 63 |
if ( m ){ |
|---|
| 64 |
|
|---|
| 65 |
CElephantView* v = m->GetActiveView(); |
|---|
| 66 |
if ( v ) |
|---|
| 67 |
{ |
|---|
| 68 |
CScintillaCtrl& rCtrl = v->GetCtrl(); |
|---|
| 69 |
rCtrl.SetFocus(); |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
return; |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
BOOL CFileTreeCtrl::PreCreateWindow(CREATESTRUCT& cs) |
|---|
| 84 |
{ |
|---|
| 85 |
if ( !CXTTreeCtrl::PreCreateWindow( cs ) ) |
|---|
| 86 |
return FALSE; |
|---|
| 87 |
|
|---|
| 88 |
cs.style |= TVS_HASBUTTONS|TVS_HASLINES|TVS_LINESATROOT|TVS_EDITLABELS; |
|---|
| 89 |
|
|---|
| 90 |
if (!xtAfxData.bXPMode) |
|---|
| 91 |
{ |
|---|
| 92 |
cs.dwExStyle |= WS_EX_CLIENTEDGE; |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
return TRUE; |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
int CFileTreeCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) |
|---|
| 100 |
{ |
|---|
| 101 |
if (CXTTreeCtrl::OnCreate(lpCreateStruct) == -1) |
|---|
| 102 |
return -1; |
|---|
| 103 |
|
|---|
| 104 |
if ( !m_imageList.Create( 16, 16, ILC_COLOR24 | ILC_MASK, 1, 1 ) ) |
|---|
| 105 |
{ |
|---|
| 106 |
TRACE0("Failed to create image list.\n"); |
|---|
| 107 |
return -1; |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
m_bitmap.LoadBitmap( IDB_IMGLIST_VIEW ); |
|---|
| 111 |
m_imageList.Add( &m_bitmap, RGB( 0x00,0xff,0x00 ) ); |
|---|
| 112 |
|
|---|
| 113 |
SetImageList( &m_imageList, TVSIL_NORMAL ); |
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
return 0; |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
void CFileTreeCtrl::OnEndlabeledit(NMHDR* pNMHDR, LRESULT* pResult) |
|---|
| 121 |
{ |
|---|
| 122 |
TV_DISPINFO* pTVDispInfo = (TV_DISPINFO*)pNMHDR; |
|---|
| 123 |
|
|---|
| 124 |
if ( pTVDispInfo->item.pszText ) |
|---|
| 125 |
{ |
|---|
| 126 |
SetItemText( pTVDispInfo->item.hItem, |
|---|
| 127 |
pTVDispInfo->item.pszText ); |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
*pResult = 0; |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
void CFileTreeCtrl::OnRButtonDown(UINT nFlags, CPoint point) |
|---|
| 135 |
{ |
|---|
| 136 |
|
|---|
| 137 |
CPoint pt = point; |
|---|
| 138 |
ClientToScreen(&pt); |
|---|
| 139 |
|
|---|
| 140 |
CXTMenu menu; |
|---|
| 141 |
VERIFY(menu.LoadMenu(IDR_FILETREE_MENU)); |
|---|
| 142 |
|
|---|
| 143 |
CWnd* pWndPopupOwner = this; |
|---|
| 144 |
while (pWndPopupOwner->GetStyle() & WS_CHILD) |
|---|
| 145 |
pWndPopupOwner = pWndPopupOwner->GetParent(); |
|---|
| 146 |
|
|---|
| 147 |
CXTMenu* pPopup = menu.GetSubMenu(0); |
|---|
| 148 |
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, |
|---|
| 149 |
pt.x, pt.y, pWndPopupOwner); |
|---|
| 150 |
|
|---|
| 151 |
} |
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
void CFileTreeCtrl::OnLButtonDblClk(UINT nFlags, CPoint point) |
|---|
| 155 |
{ |
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
HTREEITEM hCurSel = GetNextItem(TVI_ROOT, TVGN_CARET); |
|---|
| 159 |
//string sel = GetSelTreeCtrlItem(this); |
|---|
| 160 |
|
|---|
| 161 |
char* path = (char*)this->GetItemData(hCurSel); |
|---|
| 162 |
|
|---|
| 163 |
//AfxMessageBox(path); |
|---|
| 164 |
|
|---|
| 165 |
if ( Globals::currentProject && path != NULL ) |
|---|
| 166 |
{ |
|---|
| 167 |
//ElephantMessageBox(Globals::currentProject->PathForFile(sel).c_str()); |
|---|
| 168 |
Globals::theApp.OpenFile(path); |
|---|
| 169 |
} |
|---|
| 170 |
} |
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
bool CFileTreeCtrl::IsFolder(HTREEITEM item ) |
|---|
| 174 |
{ |
|---|
| 175 |
int image , selImage = 0; |
|---|
| 176 |
this->GetItemImage(item,image,selImage); |
|---|
| 177 |
//ephantMessageBox(ITOA(image)); |
|---|
| 178 |
|
|---|
| 179 |
return image == 2; |
|---|
| 180 |
} |
|---|
| 181 |
void CFileTreeCtrl::RemoveFolder() |
|---|
| 182 |
|
|---|
| 183 |
{ |
|---|
| 184 |
HTREEITEM item = NULL; |
|---|
| 185 |
string pname = Globals::currentProject->name; |
|---|
| 186 |
|
|---|
| 187 |
if ( ( item = GetNextItem(TVI_ROOT, TVGN_CARET) ) != NULL && GetSelTreeCtrlItem(this) != "Work Desktop" && GetSelTreeCtrlItem(this) != pname) |
|---|
| 188 |
{ |
|---|
| 189 |
if ( ItemHasChildren(item)) |
|---|
| 190 |
{ |
|---|
| 191 |
Globals::theApp.SetStatusBarText("Remove folder FAILED. Sorry the folder you selected has files underneath it, please ensure the selected folder is empty."); |
|---|
| 192 |
|
|---|
| 193 |
} |
|---|
| 194 |
else { |
|---|
| 195 |
Globals::theApp.SetStatusBarText("Remove folder SUCCEEDED : removed folder [ " + GetSelTreeCtrlItem(this) + " ] ."); |
|---|
| 196 |
project->RemoveFolder(GetSelFolderPath() ); |
|---|
| 197 |
DeleteItem(item); |
|---|
| 198 |
} |
|---|
| 199 |
|
|---|
| 200 |
} |
|---|
| 201 |
else Globals::theApp.SetStatusBarText("Remove folder FAILED. Sorry what you have selected is not a folder."); |
|---|
| 202 |
|
|---|
| 203 |
} |
|---|
| 204 |
void CFileTreeCtrl::AddFile(const string& path, bool create,bool useDefaultDir) |
|---|
| 205 |
{ |
|---|
| 206 |
if ( !Globals::currentProject ) return; |
|---|
| 207 |
string pname = Globals::currentProject->name; |
|---|
| 208 |
|
|---|
| 209 |
HTREEITEM hCurSel = NULL; |
|---|
| 210 |
|
|---|
| 211 |
// If its not a folder, well just add it to the first listed folder available |
|---|
| 212 |
if ( useDefaultDir ) |
|---|
| 213 |
{ |
|---|
| 214 |
vector<string> folders; |
|---|
| 215 |
Split(Globals::currentProject->GetCache("ProjectFolders"),",",folders ); |
|---|
| 216 |
if ( !folders.size() ) return; |
|---|
| 217 |
hCurSel = projectMap[folders[0] ]; |
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
} |
|---|
| 221 |
else hCurSel = GetNextItem(TVI_ROOT, TVGN_CARET); |
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
string folder = GetSelFolderPath(hCurSel); |
|---|
| 225 |
|
|---|
| 226 |
CString x = GetItemText(hCurSel); |
|---|
| 227 |
string folderName = x.GetBuffer(0); |
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
//ElephantMessageBox(folder.c_str() ); |
|---|
| 231 |
//AfxMessageBox(path.c_str()); |
|---|
| 232 |
string file = GetRelativePath(project->path , StripDirPath(path)) + StripFileName(path); |
|---|
| 233 |
//ElephantMessageBox(file.c_str()); |
|---|
| 234 |
// ElephantMessageBox(folderName.c_str()); |
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
Globals::currentProject->AddFile(file,folderName ); |
|---|
| 241 |
HTREEITEM parent = projectMap[folderName]; |
|---|
| 242 |
|
|---|
| 243 |
int img = 0; |
|---|
| 244 |
if ( FileExtension(file) != "d") img = -1; |
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
HTREEITEM addedItem = InsertItem(StripFileName(path).c_str(),img,img,parent); |
|---|
| 248 |
this->SetItemData(addedItem, (DWORD) strdup(path.c_str())); |
|---|
| 249 |
|
|---|
| 250 |
if ( create ) |
|---|
| 251 |
{ |
|---|
| 252 |
ofstream out(file.c_str()); |
|---|
| 253 |
out << ""; |
|---|
| 254 |
out.close(); |
|---|
| 255 |
} |
|---|
| 256 |
Globals::theApp.OpenFile(file); |
|---|
| 257 |
Expand(parent,TVE_EXPAND); |
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
} |
|---|
| 262 |
|
|---|
| 263 |
string CFileTreeCtrl::GetSelFolderPath(HTREEITEM hCurSel ) |
|---|
| 264 |
{ |
|---|
| 265 |
string path; |
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
if ( hCurSel == NULL ) |
|---|
| 269 |
{ |
|---|
| 270 |
hCurSel = GetNextItem(TVI_ROOT, TVGN_CARET); |
|---|
| 271 |
} |
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 |
path = GetItemText(hCurSel).GetBuffer(0); |
|---|
| 275 |
if ( ! IsFolder(hCurSel) ) path = ""; |
|---|
| 276 |
//ElephantMessageBox(path.c_str()); |
|---|
| 277 |
HTREEITEM sel = NULL; |
|---|
| 278 |
while ( ( sel = GetParentItem(hCurSel) ) != NULL ) |
|---|
| 279 |
{ |
|---|
| 280 |
if ( GetParentItem(sel) != NULL ) { |
|---|
| 281 |
|
|---|
| 282 |
string temp = GetItemText(sel).GetBuffer(0); |
|---|
| 283 |
path = temp + "//" + path; |
|---|
| 284 |
hCurSel = sel; |
|---|
| 285 |
} |
|---|
| 286 |
else hCurSel = sel; |
|---|
| 287 |
} |
|---|
| 288 |
|
|---|
| 289 |
if ( path[path.length() - 1 ] == '/' ) |
|---|
| 290 |
{ |
|---|
| 291 |
char buffer[100]; |
|---|
| 292 |
sprintf(buffer,"C : %c", path[path.length() - 1]); |
|---|
| 293 |
// ElephantMessageBox(buffer); |
|---|
| 294 |
|
|---|
| 295 |
path = path.substr(0,path.length() - 2 ); |
|---|
| 296 |
} |
|---|
| 297 |
|
|---|
| 298 |
// ElephantMessageBox(path.c_str()); |
|---|
| 299 |
return path; |
|---|
| 300 |
} |
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 |
void CFileTreeCtrl::RemoveFile() |
|---|
| 304 |
{ |
|---|
| 305 |
if ( !Globals::currentProject ) return; |
|---|
| 306 |
string pname = Globals::currentProject->name; |
|---|
| 307 |
HTREEITEM item = NULL; |
|---|
| 308 |
if ( ( item = GetNextItem(TVI_ROOT, TVGN_CARET) ) != NULL && GetSelTreeCtrlItem(this) != "Work Desktop" && GetSelTreeCtrlItem(this) != pname) |
|---|
| 309 |
{ |
|---|
| 310 |
if ( ItemHasChildren(item)) |
|---|
| 311 |
{ |
|---|
| 312 |
Globals::theApp.SetStatusBarText("Remove File FAILED : Sorry this looks like a directory."); |
|---|
| 313 |
//ElephantMessageBox("This looks like a directory."); |
|---|
| 314 |
} |
|---|
| 315 |
|
|---|
| 316 |
else { |
|---|
| 317 |
|
|---|
| 318 |
|
|---|
| 319 |
|
|---|
| 320 |
string path = Globals::currentProject->PathForFile(GetItemText(item).GetBuffer(0)); |
|---|
| 321 |
if ( Globals::currentProject->IsBuildTarget(path)) |
|---|
| 322 |
{ |
|---|
| 323 |
ElephantMessageBox("Did you know Elephant maintains the build target file so you dont have to? Unfortunately that means you cannot remove this file sorry!"); |
|---|
| 324 |
return; |
|---|
| 325 |
} |
|---|
| 326 |
project->RemoveFile(GetItemText(item).GetBuffer(0)); |
|---|
| 327 |
//AfxMessageBox(path.c_str()); |
|---|
| 328 |
Globals::theApp.RemoveImportToCurrentProject(path); |
|---|
| 329 |
DeleteItem(item); |
|---|
| 330 |
|
|---|
| 331 |
vector<string> myFiles; |
|---|
| 332 |
Split(Globals::currentProject->GetCache("files"),",",myFiles); |
|---|
| 333 |
string reloadPath = Globals::currentProject->path + "\\" + myFiles[0]; |
|---|
| 334 |
//AfxMessageBox(reloadPath.c_str()); |
|---|
| 335 |
CMainFrame* mf = static_cast<CMainFrame*>(Globals::theApp.m_pMainWnd ); |
|---|
| 336 |
mf->ReloadNamedFile(CString(reloadPath.c_str() ) ); |
|---|
| 337 |
} |
|---|
| 338 |
} |
|---|
| 339 |
|
|---|
| 340 |
} |
|---|