From b87d73ec861c1c5c48729092743f8d0cc864559c Mon Sep 17 00:00:00 2001 From: Tim Warren Date: Tue, 12 May 2015 16:30:22 -0400 Subject: [PATCH] Tweak widget display for more apparent speed on tab opening/closing, toggle menus based on the current tab, and make globals more obvious --- src/TyroApp.cpp | 24 +++++++++-------- src/settings/LangConfig.cpp | 40 +++++++++++++++++++++++++---- src/settings/LangConfig.h | 2 ++ src/widgets/EditPane.cpp | 11 ++++++++ src/widgets/EditPane.h | 2 +- src/widgets/MainFrame.cpp | 50 ++++++++++++------------------------ src/widgets/TabContainer.cpp | 34 +++++++++++++++++++----- src/widgets/TyroMenu.cpp | 42 +++++++++++++++++++++++++++++- src/widgets/TyroMenu.h | 2 ++ src/widgets/widget.h | 11 ++------ src/wx_common.h | 5 ++++ 11 files changed, 157 insertions(+), 66 deletions(-) diff --git a/src/TyroApp.cpp b/src/TyroApp.cpp index c1a6f30..3bdeac5 100644 --- a/src/TyroApp.cpp +++ b/src/TyroApp.cpp @@ -6,12 +6,12 @@ #include "widgets/widget.h" #include -#include + // Some global stuff -wxConfigBase *Config; -TyroMenu *mbar; -MainFrame *main_frame; +wxConfigBase *Glob_config; +TyroMenu *Glob_menu_bar; +MainFrame *Glob_main_frame; /** * Class with main method @@ -29,15 +29,17 @@ public: this->SetAppName(APP_NAME); this->SetVendorName(APP_VENDOR); - Config = wxConfigBase::Get(); - mbar = new TyroMenu(); - main_frame = new MainFrame(0L, APP_NAME); + // Initialize globals + Glob_config = wxConfigBase::Get(); + Glob_menu_bar = new TyroMenu(); + Glob_main_frame = new MainFrame(0L, APP_NAME); - SetTopWindow(main_frame); + SetTopWindow(Glob_main_frame); - main_frame->Layout(); - main_frame->CenterOnScreen(); - main_frame->Show(true); + // Setup Main Window + Glob_main_frame->Layout(); + Glob_main_frame->CenterOnScreen(); + Glob_main_frame->Show(true); return true; } diff --git a/src/settings/LangConfig.cpp b/src/settings/LangConfig.cpp index 331cf44..5aa2db1 100644 --- a/src/settings/LangConfig.cpp +++ b/src/settings/LangConfig.cpp @@ -11,6 +11,16 @@ LangConfig::LangConfig() { this->LoadJson(languages_json); this->lang = ""; + + // "cache" reverse map of languages to their keys + JsonValue langList = this->GetRoot(); + JsonValue::iterator it; + + for (it = langList.begin(); it != langList.end(); ++it) + { + JsonValue langObj = *it; + reverseMap[langObj.get("name", JsonValue()).asString()] = it.key().asString(); + } } LangConfig::~LangConfig() @@ -99,22 +109,42 @@ void LangConfig::SetLang(string lang) this->lang = lang; } +/** + * Get the current language key + */ string LangConfig::GetLang() { return this->lang; } +/** + * Get the name attribute of the currently selected language + * + * @return string + */ +string LangConfig::GetCurrentLangName() +{ + return this->GetRoot() + .get(this->lang, JsonValue()) + .get("name", JsonValue()) + .asString(); +} + +/** + * Gets the list of languages available + * + * @return StringMap + */ StringMap LangConfig::GetLangList() { - JsonValue langList = this->GetRoot(); - JsonValue::iterator it; + StringMap revList = this->reverseMap; + StringMap::iterator it; StringMap outputList; - for (it = langList.begin(); it != langList.end(); ++it) + for (it = revList.begin(); it != revList.end(); ++it) { - JsonValue langObj = *it; - outputList[it.key().asString()] = langObj.get("name", JsonValue()).asString(); + outputList[it->second] = it->first; } return outputList; diff --git a/src/settings/LangConfig.h b/src/settings/LangConfig.h index f6b1a67..b538870 100644 --- a/src/settings/LangConfig.h +++ b/src/settings/LangConfig.h @@ -14,8 +14,10 @@ public: JsonValue GetKeywordList(string lang="none"); JsonValue GetLexerMap(string lang="none"); StringMap GetLangList(); + string GetCurrentLangName(); private: string lang; + StringMap reverseMap; }; #endif diff --git a/src/widgets/EditPane.cpp b/src/widgets/EditPane.cpp index 8a1e8d5..e969ce0 100644 --- a/src/widgets/EditPane.cpp +++ b/src/widgets/EditPane.cpp @@ -417,3 +417,14 @@ void EditPane::_ApplyTheme(JsonValue &lexer_map) } } } + +/** + * Get the display name of the currently selected + * language used for highlighting + * + * @return string + */ +string EditPane::GetCurrentLang() +{ + return lang_config->GetCurrentLangName(); +} diff --git a/src/widgets/EditPane.h b/src/widgets/EditPane.h index 5af6c11..6b2f117 100644 --- a/src/widgets/EditPane.h +++ b/src/widgets/EditPane.h @@ -20,6 +20,7 @@ public: bool SaveFile(); bool SaveFile(const wxString &filename); void ApplyTheme(string lang, string theme=""); + string GetCurrentLang(); private: StringConstMap lexerMap; StringConstMap::iterator lexerMapIt; @@ -28,7 +29,6 @@ private: enum myMargins { MARGIN_FOLD, - MARGIN_SYMBOL, MARGIN_LINE_NUMBERS }; bool FileReadable(); diff --git a/src/widgets/MainFrame.cpp b/src/widgets/MainFrame.cpp index f84863d..27093ab 100644 --- a/src/widgets/MainFrame.cpp +++ b/src/widgets/MainFrame.cpp @@ -4,7 +4,7 @@ #include "widget.h" // Nasty globals -extern TyroMenu *mbar; +extern TyroMenu *Glob_menu_bar; static TabContainer *notebook; /** @@ -13,24 +13,18 @@ static TabContainer *notebook; MainFrame::MainFrame(wxFrame *frame, const wxString &title) : wxFrame(frame, -1, title) { - #include "../../resources/xpm/tyro.xpm" - findReplaceData = new wxFindReplaceData(wxFR_DOWN); + // Set the frame icon +#include "../../resources/xpm/tyro.xpm" wxIcon app_icon(tyro_icon); this->SetIcon(app_icon); // Apply the menu bar to the current frame -#ifdef __WXMAC__ - wxMenuBar::MacSetCommonMenuBar(mbar); -#endif // __WXMAC__ - SetMenuBar(mbar); + this->SetMenuBar(Glob_menu_bar); this->SetupStatusBar(); - // Create the tab container - notebook = new TabContainer(this); - this->DoLayout(); this->BindEvents(); } @@ -53,6 +47,9 @@ MainFrame::~MainFrame() */ void MainFrame::DoLayout() { + // Create the tab container + notebook = new TabContainer(this); + this->manager = new wxAuiManager(this); this->SetupToolbar(); @@ -81,9 +78,7 @@ void MainFrame::DoLayout() */ void MainFrame::SetupStatusBar() { - CreateStatusBar(2); - SetStatusText(_(""), 0); - SetStatusText(_(""), 1); + CreateStatusBar(3); } /** @@ -94,7 +89,7 @@ void MainFrame::SetupStatusBar() void MainFrame::SetupToolbar() { // Icon files -#ifdef __WXMAC__ +#ifndef __WXGTK__ #include "../../resources/xpm/32/new.xpm" #include "../../resources/xpm/32/open.xpm" #include "../../resources/xpm/32/save.xpm" @@ -108,23 +103,7 @@ void MainFrame::SetupToolbar() wxBitmap copy_icon(copy); wxBitmap cut_icon(cut); wxBitmap paste_icon(paste); -#endif -#ifdef __WXMSW__ - #include "../../resources/xpm/24/new.xpm" - #include "../../resources/xpm/24/open.xpm" - #include "../../resources/xpm/24/save.xpm" - #include "../../resources/xpm/24/cut.xpm" - #include "../../resources/xpm/24/copy.xpm" - #include "../../resources/xpm/24/paste.xpm" - - wxBitmap new_file_icon(new_file); - wxBitmap open_file_icon(open); - wxBitmap save_file_icon(save); - wxBitmap copy_icon(copy); - wxBitmap cut_icon(cut); - wxBitmap paste_icon(paste); -#endif -#ifdef __WXGTK__ +#else wxBitmap new_file_icon = wxArtProvider::GetBitmap(wxART_NEW, wxART_TOOLBAR); wxBitmap open_file_icon = wxArtProvider::GetBitmap(wxART_FILE_OPEN, wxART_TOOLBAR); wxBitmap save_file_icon = wxArtProvider::GetBitmap(wxART_FILE_SAVE, wxART_TOOLBAR); @@ -139,7 +118,6 @@ void MainFrame::SetupToolbar() toolBar->AddTool(wxID_OPEN, "Open", open_file_icon, "Open file"); toolBar->AddTool(wxID_SAVE, "Save", save_file_icon, "Save file"); - toolBar->AddSeparator(); toolBar->AddTool(wxID_COPY, "Copy", copy_icon, "Copy"); @@ -225,10 +203,12 @@ void MainFrame::OnOpen(wxCommandEvent &WXUNUSED(event)) listcount = filelist.GetCount(); // Open a new tab for each file + notebook->Hide(); for (int i = 0; i < listcount; i++) { notebook->AddTab(filelist[i]); } + notebook->Show(); this->EnableEditControls(true); } @@ -242,6 +222,7 @@ void MainFrame::OnCloseTab(wxCommandEvent &WXUNUSED(event)) { int current_tab = notebook->GetSelection(); + notebook->Hide(); notebook->DeletePage(current_tab); if (notebook->GetPageCount() == 0) @@ -249,6 +230,7 @@ void MainFrame::OnCloseTab(wxCommandEvent &WXUNUSED(event)) this->EnableEditControls(false); } + notebook->Show(); this->manager->Update(); } @@ -259,8 +241,10 @@ void MainFrame::OnCloseTab(wxCommandEvent &WXUNUSED(event)) */ void MainFrame::OnCloseAll(wxCommandEvent &WXUNUSED(event)) { + notebook->Hide(); notebook->DeleteAllPages(); this->EnableEditControls(false); + notebook->Show(); } /** @@ -598,7 +582,7 @@ void MainFrame::OnToggleLineEndings(wxCommandEvent &event) void MainFrame::EnableEditControls(bool enable) { // Update menu items - mbar->EnableEditControls(enable); + Glob_menu_bar->EnableEditControls(enable); // Toggle toolbar items this->toolBar->EnableTool(wxID_SAVE, enable); diff --git a/src/widgets/TabContainer.cpp b/src/widgets/TabContainer.cpp index 4c0843e..962fe6e 100644 --- a/src/widgets/TabContainer.cpp +++ b/src/widgets/TabContainer.cpp @@ -4,9 +4,19 @@ #include "widget.h" -extern MainFrame *main_frame; +extern MainFrame *Glob_main_frame; +extern TyroMenu *Glob_menu_bar; static unsigned long untitled_document_count = 0; +/** + * Constructor + * + * @param wxWindow* parent + * @param wxWindowID id + * @param const wxPoint& pos + * @param const wxSize& size + * @param long style + */ TabContainer::TabContainer( wxWindow* parent, wxWindowID id, @@ -21,6 +31,9 @@ TabContainer::TabContainer( Bind(wxEVT_AUINOTEBOOK_PAGE_CHANGED, &TabContainer::OnTabSwitch, this, wxID_ANY); } +/** + * Destructor + */ TabContainer::~TabContainer() { wxLogDebug("TabContainer destructor called"); @@ -63,6 +76,9 @@ void TabContainer::AddTab(wxString filePath) this->SetPageToolTip(this->GetPageIndex(this->GetCurrentPage()), fileName.GetFullPath()); + // Select the appropriate language in the language menu + Glob_menu_bar->SetCurrentLanguage(editor->GetCurrentLang()); + return; } @@ -139,7 +155,7 @@ void TabContainer::OnClosed(wxAuiNotebookEvent &WXUNUSED(event)) { if (this->GetPageCount() == 0) { - main_frame->EnableEditControls(false); + Glob_main_frame->EnableEditControls(false); } } @@ -166,7 +182,7 @@ void TabContainer::OnTabContextMenu(wxAuiNotebookEvent &WXUNUSED(event)) void TabContainer::OnCloseAll(wxCommandEvent &WXUNUSED(event)) { this->DeleteAllPages(); - main_frame->EnableEditControls(false); + Glob_main_frame->EnableEditControls(false); } /** @@ -175,9 +191,15 @@ void TabContainer::OnCloseAll(wxCommandEvent &WXUNUSED(event)) * @param wxAuiNotebookEvent& event * @return void */ -void TabContainer::OnTabSwitch(wxAuiNotebookEvent &WXUNUSED(event)) +void TabContainer::OnTabSwitch(wxAuiNotebookEvent &event) { - //EditPane *editor = this->GetEditor(event.GetSelection()); + EditPane *editor = this->GetEditor(event.GetSelection()); - // @TODO: Update menu item checkboxes based on the state of the current editor + // Update view menu options + Glob_menu_bar->SetIdChecked(myID_VIEW_WHITESPACE, (editor->GetViewWhiteSpace() == wxSTC_WS_VISIBLEALWAYS)); + Glob_menu_bar->SetIdChecked(myID_VIEW_LINE_ENDINGS, editor->GetViewEOL()); + Glob_menu_bar->SetIdChecked(myID_LINE_WRAP, (editor->GetWrapMode() == wxSTC_WRAP_WORD)); + + // Update language menu selection + Glob_menu_bar->SetCurrentLanguage(editor->GetCurrentLang()); } \ No newline at end of file diff --git a/src/widgets/TyroMenu.cpp b/src/widgets/TyroMenu.cpp index 76cf61a..7e5ae8c 100644 --- a/src/widgets/TyroMenu.cpp +++ b/src/widgets/TyroMenu.cpp @@ -27,6 +27,7 @@ TyroMenu::TyroMenu() TyroMenu::~TyroMenu() { wxLogDebug("TyroMenu Destructor Called."); + delete lang_config; } void TyroMenu::SetupMainMenus() @@ -71,9 +72,11 @@ void TyroMenu::SetupLangMenu() StringMap::iterator last = langs.end(); + langMenu->Append(wxID_ANY, "Plain Text", "Don't highlight file", wxITEM_CHECK); + for (it = langs.begin(); it != last; ++it) { - langMenu->Append(wxID_ANY, it->second, "Hightlight file as " + it->second, wxITEM_CHECK); + langMenu->Append(wxID_ANY, it->second, "Highlight file as " + it->second, wxITEM_CHECK); } } @@ -90,6 +93,18 @@ void TyroMenu::EnableEditControls(bool enable) this->EnableEntireMenu(myLANG_MENU, this->langMenu, enable); } +/** + * Check the menu item associated with the specified id + * + * @param int id + * @param bool checked + * @return void + */ +void TyroMenu::SetIdChecked(int id, bool checked) +{ + this->FindItem(id)->Check(checked); +} + /** * Enable/disable all the items in the menu, for environments * that don't properly support disabling the menu by the parent label (like Ubuntu's Unity) @@ -112,5 +127,30 @@ void TyroMenu::EnableEntireMenu(size_t menuId, wxMenu *menu, bool enable) { wxMenuItem *current = *iter; current->Enable(enable); + + // Uncheck all the items + if (current->IsCheckable()) + { + current->Check(false); + } } +} + +/** + * Change the language used for highlighting + * + * @param string lang + * @return void + */ +void TyroMenu::SetCurrentLanguage(string lang) +{ + if (lang == "") + { + lang = "Plain Text"; + } + + // Clear checks in the current menu + this->EnableEntireMenu(myLANG_MENU, langMenu, true); + + this->Check(this->FindMenuItem("&Language", lang), true); } \ No newline at end of file diff --git a/src/widgets/TyroMenu.h b/src/widgets/TyroMenu.h index fd86659..d069a73 100644 --- a/src/widgets/TyroMenu.h +++ b/src/widgets/TyroMenu.h @@ -11,6 +11,8 @@ public: TyroMenu(); ~TyroMenu(); void EnableEditControls(bool enable); + void SetIdChecked(int id, bool checked); + void SetCurrentLanguage(string lang); private: wxMenu *fileMenu; wxMenu *editMenu; diff --git a/src/widgets/widget.h b/src/widgets/widget.h index 7c24e3c..44b135c 100644 --- a/src/widgets/widget.h +++ b/src/widgets/widget.h @@ -1,10 +1,6 @@ -/* - * File: widget.h - * Author: twarren - * - * Created on May 7, 2015, 2:35 PM +/** + * Common header for widgets */ - #ifndef TYRO_WIDGET_H #define TYRO_WIDGET_H @@ -12,9 +8,6 @@ #include "../wx_common.h" // Base widgets -#include -#include -#include #include #include #include diff --git a/src/wx_common.h b/src/wx_common.h index 601f57f..49da2fa 100644 --- a/src/wx_common.h +++ b/src/wx_common.h @@ -14,10 +14,15 @@ #include #endif +// Common helpers/functionality +#include +#include +#include #include #include #include +// Tyro-specific variables #include "definitions.h" #endif /* WX_COMMON_H */