From e0d8b1203d05b54e50d504470c362d1f7a7490a3 Mon Sep 17 00:00:00 2001 From: Tim Warren Date: Fri, 10 Apr 2015 15:11:15 -0400 Subject: [PATCH] Lexers can now be loaded --- Makefile | 2 +- src/common.h | 1 + src/definitions.h | 4 ++++ src/widgets/EditPane.cpp | 31 ++++++++++++++++++++++++++++++- src/widgets/EditPane.h | 7 ++++++- src/widgets/EditPaneDefinitions.h | 19 ------------------- src/widgets/MainFrame.cpp | 5 ++++- src/widgets/MainFrame.h | 1 + src/widgets/TabContainer.cpp | 22 +++++++++++++++------- src/widgets/TabContainer.h | 2 ++ src/wx_common.h | 3 +++ 11 files changed, 67 insertions(+), 30 deletions(-) delete mode 100644 src/widgets/EditPaneDefinitions.h diff --git a/Makefile b/Makefile index 532ddad..7eff014 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ PROGRAM_SRC = $(wildcard src/*.cpp src/widgets/*.cpp) PROGRAM = build/Tyro PROGRAM_OBJECTS = $(patsubst %.cpp,%.o, $(PROGRAM_SRC)) -BASE_FLAGS = -DSCI_LEXER -std=gnu++11 +BASE_FLAGS = -DSCI_LEXER -std=c++11 LDLIBS = $(TARGET) $(shell wx-config --libs base core aui stc adv) -lssh2 WX_CXXFLAGS = $(shell wx-config --cxxflags) $(BASE_FLAGS) diff --git a/src/common.h b/src/common.h index 6db2aee..efc447c 100644 --- a/src/common.h +++ b/src/common.h @@ -6,6 +6,7 @@ #include #include +#include #include #include #include diff --git a/src/definitions.h b/src/definitions.h index c0beff0..f4d1da9 100644 --- a/src/definitions.h +++ b/src/definitions.h @@ -5,6 +5,10 @@ #ifndef DEFINITIONS_H #define DEFINITIONS_H +// EditPane file extension to lexer mapping +typedef pair StringConstMapData; +typedef map StringConstMap; + const wxString TYRO_FILE_OPEN_WILDCARDS = _T("Bash (*.sh, *.bsh) |*.sh;*.bsh|") _T("Batch (*.bat, *.cmd, *.nt)|*.bat;*.cmd,*.nt|") diff --git a/src/widgets/EditPane.cpp b/src/widgets/EditPane.cpp index 813cd8b..39a10c6 100644 --- a/src/widgets/EditPane.cpp +++ b/src/widgets/EditPane.cpp @@ -5,7 +5,36 @@ EditPane::EditPane( const wxSize &size, long style ) : wxStyledTextCtrl (parent, id, pos, size, style) { - + StringConstMapData map_data[] = { + {"c", wxSTC_LEX_CPP}, + {"h", wxSTC_LEX_CPP}, + {"cpp", wxSTC_LEX_CPP}, + {"cxx", wxSTC_LEX_CPP}, + {"py", wxSTC_LEX_PYTHON}, + {"php", wxSTC_LEX_PHPSCRIPT} + }; + + lexer_map = StringConstMap( + map_data, + map_data + sizeof map_data / sizeof map_data[0] + ); } EditPane::~EditPane() {} + +/** + * Encapsulate lexer selection when opening a file + * + * @param wxString filePath + * @return bool + */ +bool EditPane::LoadAndHighlight(wxString filePath) +{ + wxFileName file(filePath); + wxString ext = file.GetExt(); + + //lexer_map_it = lexer_map.find((string) ext); + //this->SetLexer(lexer_map_it->second); + + return this->LoadFile(filePath); +} diff --git a/src/widgets/EditPane.h b/src/widgets/EditPane.h index 53f4e5d..cbcef8f 100644 --- a/src/widgets/EditPane.h +++ b/src/widgets/EditPane.h @@ -2,8 +2,10 @@ #define TYROEDIT_PANE_H #include "../wx_common.h" + +#include + #include "../../include/json/json.h" -#include "EditPaneDefinitions.h" class EditPane: public wxStyledTextCtrl { @@ -20,7 +22,10 @@ public: wxVSCROLL ); ~EditPane(); + bool LoadAndHighlight(wxString filePath); private: + StringConstMap lexer_map; + StringConstMap::iterator lexer_map_it; }; #endif // TYRODOC_FRAME_H diff --git a/src/widgets/EditPaneDefinitions.h b/src/widgets/EditPaneDefinitions.h deleted file mode 100644 index cc75391..0000000 --- a/src/widgets/EditPaneDefinitions.h +++ /dev/null @@ -1,19 +0,0 @@ - - -#ifndef EDITPANEDEFINITIONS_H -#define EDITPANEDEFINITIONS_H - -#include - -// EditPane file extension to lexer mapping -/*map TYROLEXER_MAPPING = { - {"c", wxSTC_LEX_CPP}, - {"h", wxSTC_LEX_CPP}, - {"cpp", wxSTC_LEX_CPP}, - {"cxx", wxSTC_LEX_CPP}, - {"py", wxSTC_LEX_PYTHON}, - {"php", wxSTC_LEX_PHPSCRIPT} -};*/ - -#endif /* EDITPANEDEFINITIONS_H */ - diff --git a/src/widgets/MainFrame.cpp b/src/widgets/MainFrame.cpp index 646bfea..0925c3e 100644 --- a/src/widgets/MainFrame.cpp +++ b/src/widgets/MainFrame.cpp @@ -107,6 +107,7 @@ void MainFrame::SetupMenu() editMenu->Append(wxID_CUT, _T("Cu&t\tCtrl+X"), _T("Cut selected text")); editMenu->Append(wxID_COPY, _T("&Copy\tCtrl+C"), _T("Copy selected text")); editMenu->Append(wxID_PASTE, _T("&Paste\tCtrl+V"), _T("Paste contents of clipboard")); + editMenu->Append(wxID_CLEAR, _T("&Delete\tDel")); editMenu->AppendSeparator(); editMenu->Append(wxID_SELECTALL, _T("Select All\tCtrl+A"), _T("Select all the text in the current document")); @@ -228,5 +229,7 @@ void MainFrame::OnAbout(wxCommandEvent &WXUNUSED(event)) info.SetDescription("Tyro, a text editor for all development"); info.SetCopyright(_T(" (C) 2015, Timothy J Warren")); - wxAboutBox(info, this); + wxGenericAboutDialog dlg; + dlg.Create(info, this); + dlg.ShowModal(); } diff --git a/src/widgets/MainFrame.h b/src/widgets/MainFrame.h index 3f1d91f..0483e02 100644 --- a/src/widgets/MainFrame.h +++ b/src/widgets/MainFrame.h @@ -9,6 +9,7 @@ #include "../TyroApp.h" #include +#include #include "TabContainer.h" diff --git a/src/widgets/TabContainer.cpp b/src/widgets/TabContainer.cpp index 94f9e18..0feffbb 100644 --- a/src/widgets/TabContainer.cpp +++ b/src/widgets/TabContainer.cpp @@ -14,7 +14,6 @@ TabContainer::TabContainer( long style ) : wxAuiNotebook(parent, id, pos, size, style) { - this->AddTab(); } TabContainer::~TabContainer() {} @@ -23,22 +22,31 @@ void TabContainer::AddTab() { untitled_document_count++; wxString caption; - + caption.Printf("Untitled %lu", untitled_document_count); - + EditPane *editor = new EditPane(this, wxID_ANY); - + this->AddPage(editor, caption); } void TabContainer::AddTab(wxString filePath) { - wxString caption=filePath; + wxFileName fileName(filePath); + + wxString caption= fileName.GetFullName(); EditPane *editor = new EditPane(this, wxID_ANY); - editor->LoadFile(filePath); + bool loaded_file = editor->LoadAndHighlight(filePath); - this->AddPage(editor, caption); + if (loaded_file) + { + this->AddPage(editor, caption); + } + else + { + // @TODO Add Error dialog here + } } EditPane *TabContainer::GetCurrentEditor() diff --git a/src/widgets/TabContainer.h b/src/widgets/TabContainer.h index 3e1c153..88b31e6 100644 --- a/src/widgets/TabContainer.h +++ b/src/widgets/TabContainer.h @@ -8,8 +8,10 @@ #include "../wx_common.h" #include +#include #include "EditPane.h" + static long tab_style = wxAUI_NB_TAB_MOVE | wxAUI_NB_SCROLL_BUTTONS | wxAUI_NB_WINDOWLIST_BUTTON | wxAUI_NB_CLOSE_ON_ALL_TABS | wxAUI_NB_MIDDLE_CLICK_CLOSE | wxAUI_NB_TOP; diff --git a/src/wx_common.h b/src/wx_common.h index fe8be63..220dfa3 100644 --- a/src/wx_common.h +++ b/src/wx_common.h @@ -14,6 +14,9 @@ #include #endif +#include +#include + #include "definitions.h" #endif /* WX_COMMON_H */