Tweak widget display for more apparent speed on tab opening/closing, toggle menus based on the current tab, and make globals more obvious

This commit is contained in:
Tim Warren 2015-05-12 16:30:22 -04:00
parent ccb3de679a
commit b87d73ec86
11 changed files with 157 additions and 66 deletions

View File

@ -6,12 +6,12 @@
#include "widgets/widget.h" #include "widgets/widget.h"
#include <wx/app.h> #include <wx/app.h>
#include <wx/debug.h>
// Some global stuff // Some global stuff
wxConfigBase *Config; wxConfigBase *Glob_config;
TyroMenu *mbar; TyroMenu *Glob_menu_bar;
MainFrame *main_frame; MainFrame *Glob_main_frame;
/** /**
* Class with main method * Class with main method
@ -29,15 +29,17 @@ public:
this->SetAppName(APP_NAME); this->SetAppName(APP_NAME);
this->SetVendorName(APP_VENDOR); this->SetVendorName(APP_VENDOR);
Config = wxConfigBase::Get(); // Initialize globals
mbar = new TyroMenu(); Glob_config = wxConfigBase::Get();
main_frame = new MainFrame(0L, APP_NAME); Glob_menu_bar = new TyroMenu();
Glob_main_frame = new MainFrame(0L, APP_NAME);
SetTopWindow(main_frame); SetTopWindow(Glob_main_frame);
main_frame->Layout(); // Setup Main Window
main_frame->CenterOnScreen(); Glob_main_frame->Layout();
main_frame->Show(true); Glob_main_frame->CenterOnScreen();
Glob_main_frame->Show(true);
return true; return true;
} }

View File

@ -11,6 +11,16 @@ LangConfig::LangConfig()
{ {
this->LoadJson(languages_json); this->LoadJson(languages_json);
this->lang = ""; 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() LangConfig::~LangConfig()
@ -99,22 +109,42 @@ void LangConfig::SetLang(string lang)
this->lang = lang; this->lang = lang;
} }
/**
* Get the current language key
*/
string LangConfig::GetLang() string LangConfig::GetLang()
{ {
return this->lang; 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() StringMap LangConfig::GetLangList()
{ {
JsonValue langList = this->GetRoot(); StringMap revList = this->reverseMap;
JsonValue::iterator it; StringMap::iterator it;
StringMap outputList; StringMap outputList;
for (it = langList.begin(); it != langList.end(); ++it) for (it = revList.begin(); it != revList.end(); ++it)
{ {
JsonValue langObj = *it; outputList[it->second] = it->first;
outputList[it.key().asString()] = langObj.get("name", JsonValue()).asString();
} }
return outputList; return outputList;

View File

@ -14,8 +14,10 @@ public:
JsonValue GetKeywordList(string lang="none"); JsonValue GetKeywordList(string lang="none");
JsonValue GetLexerMap(string lang="none"); JsonValue GetLexerMap(string lang="none");
StringMap GetLangList(); StringMap GetLangList();
string GetCurrentLangName();
private: private:
string lang; string lang;
StringMap reverseMap;
}; };
#endif #endif

View File

@ -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();
}

View File

@ -20,6 +20,7 @@ public:
bool SaveFile(); bool SaveFile();
bool SaveFile(const wxString &filename); bool SaveFile(const wxString &filename);
void ApplyTheme(string lang, string theme=""); void ApplyTheme(string lang, string theme="");
string GetCurrentLang();
private: private:
StringConstMap lexerMap; StringConstMap lexerMap;
StringConstMap::iterator lexerMapIt; StringConstMap::iterator lexerMapIt;
@ -28,7 +29,6 @@ private:
enum myMargins enum myMargins
{ {
MARGIN_FOLD, MARGIN_FOLD,
MARGIN_SYMBOL,
MARGIN_LINE_NUMBERS MARGIN_LINE_NUMBERS
}; };
bool FileReadable(); bool FileReadable();

View File

@ -4,7 +4,7 @@
#include "widget.h" #include "widget.h"
// Nasty globals // Nasty globals
extern TyroMenu *mbar; extern TyroMenu *Glob_menu_bar;
static TabContainer *notebook; static TabContainer *notebook;
/** /**
@ -13,24 +13,18 @@ static TabContainer *notebook;
MainFrame::MainFrame(wxFrame *frame, const wxString &title) MainFrame::MainFrame(wxFrame *frame, const wxString &title)
: wxFrame(frame, -1, title) : wxFrame(frame, -1, title)
{ {
#include "../../resources/xpm/tyro.xpm"
findReplaceData = new wxFindReplaceData(wxFR_DOWN); findReplaceData = new wxFindReplaceData(wxFR_DOWN);
// Set the frame icon
#include "../../resources/xpm/tyro.xpm"
wxIcon app_icon(tyro_icon); wxIcon app_icon(tyro_icon);
this->SetIcon(app_icon); this->SetIcon(app_icon);
// Apply the menu bar to the current frame // Apply the menu bar to the current frame
#ifdef __WXMAC__ this->SetMenuBar(Glob_menu_bar);
wxMenuBar::MacSetCommonMenuBar(mbar);
#endif // __WXMAC__
SetMenuBar(mbar);
this->SetupStatusBar(); this->SetupStatusBar();
// Create the tab container
notebook = new TabContainer(this);
this->DoLayout(); this->DoLayout();
this->BindEvents(); this->BindEvents();
} }
@ -53,6 +47,9 @@ MainFrame::~MainFrame()
*/ */
void MainFrame::DoLayout() void MainFrame::DoLayout()
{ {
// Create the tab container
notebook = new TabContainer(this);
this->manager = new wxAuiManager(this); this->manager = new wxAuiManager(this);
this->SetupToolbar(); this->SetupToolbar();
@ -81,9 +78,7 @@ void MainFrame::DoLayout()
*/ */
void MainFrame::SetupStatusBar() void MainFrame::SetupStatusBar()
{ {
CreateStatusBar(2); CreateStatusBar(3);
SetStatusText(_(""), 0);
SetStatusText(_(""), 1);
} }
/** /**
@ -94,7 +89,7 @@ void MainFrame::SetupStatusBar()
void MainFrame::SetupToolbar() void MainFrame::SetupToolbar()
{ {
// Icon files // Icon files
#ifdef __WXMAC__ #ifndef __WXGTK__
#include "../../resources/xpm/32/new.xpm" #include "../../resources/xpm/32/new.xpm"
#include "../../resources/xpm/32/open.xpm" #include "../../resources/xpm/32/open.xpm"
#include "../../resources/xpm/32/save.xpm" #include "../../resources/xpm/32/save.xpm"
@ -108,23 +103,7 @@ void MainFrame::SetupToolbar()
wxBitmap copy_icon(copy); wxBitmap copy_icon(copy);
wxBitmap cut_icon(cut); wxBitmap cut_icon(cut);
wxBitmap paste_icon(paste); wxBitmap paste_icon(paste);
#endif #else
#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__
wxBitmap new_file_icon = wxArtProvider::GetBitmap(wxART_NEW, wxART_TOOLBAR); wxBitmap new_file_icon = wxArtProvider::GetBitmap(wxART_NEW, wxART_TOOLBAR);
wxBitmap open_file_icon = wxArtProvider::GetBitmap(wxART_FILE_OPEN, wxART_TOOLBAR); wxBitmap open_file_icon = wxArtProvider::GetBitmap(wxART_FILE_OPEN, wxART_TOOLBAR);
wxBitmap save_file_icon = wxArtProvider::GetBitmap(wxART_FILE_SAVE, 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_OPEN, "Open", open_file_icon, "Open file");
toolBar->AddTool(wxID_SAVE, "Save", save_file_icon, "Save file"); toolBar->AddTool(wxID_SAVE, "Save", save_file_icon, "Save file");
toolBar->AddSeparator(); toolBar->AddSeparator();
toolBar->AddTool(wxID_COPY, "Copy", copy_icon, "Copy"); toolBar->AddTool(wxID_COPY, "Copy", copy_icon, "Copy");
@ -225,10 +203,12 @@ void MainFrame::OnOpen(wxCommandEvent &WXUNUSED(event))
listcount = filelist.GetCount(); listcount = filelist.GetCount();
// Open a new tab for each file // Open a new tab for each file
notebook->Hide();
for (int i = 0; i < listcount; i++) for (int i = 0; i < listcount; i++)
{ {
notebook->AddTab(filelist[i]); notebook->AddTab(filelist[i]);
} }
notebook->Show();
this->EnableEditControls(true); this->EnableEditControls(true);
} }
@ -242,6 +222,7 @@ void MainFrame::OnCloseTab(wxCommandEvent &WXUNUSED(event))
{ {
int current_tab = notebook->GetSelection(); int current_tab = notebook->GetSelection();
notebook->Hide();
notebook->DeletePage(current_tab); notebook->DeletePage(current_tab);
if (notebook->GetPageCount() == 0) if (notebook->GetPageCount() == 0)
@ -249,6 +230,7 @@ void MainFrame::OnCloseTab(wxCommandEvent &WXUNUSED(event))
this->EnableEditControls(false); this->EnableEditControls(false);
} }
notebook->Show();
this->manager->Update(); this->manager->Update();
} }
@ -259,8 +241,10 @@ void MainFrame::OnCloseTab(wxCommandEvent &WXUNUSED(event))
*/ */
void MainFrame::OnCloseAll(wxCommandEvent &WXUNUSED(event)) void MainFrame::OnCloseAll(wxCommandEvent &WXUNUSED(event))
{ {
notebook->Hide();
notebook->DeleteAllPages(); notebook->DeleteAllPages();
this->EnableEditControls(false); this->EnableEditControls(false);
notebook->Show();
} }
/** /**
@ -598,7 +582,7 @@ void MainFrame::OnToggleLineEndings(wxCommandEvent &event)
void MainFrame::EnableEditControls(bool enable) void MainFrame::EnableEditControls(bool enable)
{ {
// Update menu items // Update menu items
mbar->EnableEditControls(enable); Glob_menu_bar->EnableEditControls(enable);
// Toggle toolbar items // Toggle toolbar items
this->toolBar->EnableTool(wxID_SAVE, enable); this->toolBar->EnableTool(wxID_SAVE, enable);

View File

@ -4,9 +4,19 @@
#include "widget.h" #include "widget.h"
extern MainFrame *main_frame; extern MainFrame *Glob_main_frame;
extern TyroMenu *Glob_menu_bar;
static unsigned long untitled_document_count = 0; 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( TabContainer::TabContainer(
wxWindow* parent, wxWindow* parent,
wxWindowID id, wxWindowID id,
@ -21,6 +31,9 @@ TabContainer::TabContainer(
Bind(wxEVT_AUINOTEBOOK_PAGE_CHANGED, &TabContainer::OnTabSwitch, this, wxID_ANY); Bind(wxEVT_AUINOTEBOOK_PAGE_CHANGED, &TabContainer::OnTabSwitch, this, wxID_ANY);
} }
/**
* Destructor
*/
TabContainer::~TabContainer() TabContainer::~TabContainer()
{ {
wxLogDebug("TabContainer destructor called"); wxLogDebug("TabContainer destructor called");
@ -63,6 +76,9 @@ void TabContainer::AddTab(wxString filePath)
this->SetPageToolTip(this->GetPageIndex(this->GetCurrentPage()), fileName.GetFullPath()); this->SetPageToolTip(this->GetPageIndex(this->GetCurrentPage()), fileName.GetFullPath());
// Select the appropriate language in the language menu
Glob_menu_bar->SetCurrentLanguage(editor->GetCurrentLang());
return; return;
} }
@ -139,7 +155,7 @@ void TabContainer::OnClosed(wxAuiNotebookEvent &WXUNUSED(event))
{ {
if (this->GetPageCount() == 0) 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)) void TabContainer::OnCloseAll(wxCommandEvent &WXUNUSED(event))
{ {
this->DeleteAllPages(); this->DeleteAllPages();
main_frame->EnableEditControls(false); Glob_main_frame->EnableEditControls(false);
} }
/** /**
@ -175,9 +191,15 @@ void TabContainer::OnCloseAll(wxCommandEvent &WXUNUSED(event))
* @param wxAuiNotebookEvent& event * @param wxAuiNotebookEvent& event
* @return void * @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());
} }

View File

@ -27,6 +27,7 @@ TyroMenu::TyroMenu()
TyroMenu::~TyroMenu() TyroMenu::~TyroMenu()
{ {
wxLogDebug("TyroMenu Destructor Called."); wxLogDebug("TyroMenu Destructor Called.");
delete lang_config;
} }
void TyroMenu::SetupMainMenus() void TyroMenu::SetupMainMenus()
@ -71,9 +72,11 @@ void TyroMenu::SetupLangMenu()
StringMap::iterator last = langs.end(); StringMap::iterator last = langs.end();
langMenu->Append(wxID_ANY, "Plain Text", "Don't highlight file", wxITEM_CHECK);
for (it = langs.begin(); it != last; ++it) 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); 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 * 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) * 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; wxMenuItem *current = *iter;
current->Enable(enable); 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);
}

View File

@ -11,6 +11,8 @@ public:
TyroMenu(); TyroMenu();
~TyroMenu(); ~TyroMenu();
void EnableEditControls(bool enable); void EnableEditControls(bool enable);
void SetIdChecked(int id, bool checked);
void SetCurrentLanguage(string lang);
private: private:
wxMenu *fileMenu; wxMenu *fileMenu;
wxMenu *editMenu; wxMenu *editMenu;

View File

@ -1,10 +1,6 @@
/* /**
* File: widget.h * Common header for widgets
* Author: twarren
*
* Created on May 7, 2015, 2:35 PM
*/ */
#ifndef TYRO_WIDGET_H #ifndef TYRO_WIDGET_H
#define TYRO_WIDGET_H #define TYRO_WIDGET_H
@ -12,9 +8,6 @@
#include "../wx_common.h" #include "../wx_common.h"
// Base widgets // Base widgets
#include <wx/cmdline.h>
#include <wx/config.h>
#include <wx/filename.h>
#include <wx/aboutdlg.h> #include <wx/aboutdlg.h>
#include <wx/fdrepdlg.h> #include <wx/fdrepdlg.h>
#include <wx/aui/aui.h> #include <wx/aui/aui.h>

View File

@ -14,10 +14,15 @@
#include <wx/wx.h> #include <wx/wx.h>
#endif #endif
// Common helpers/functionality
#include <wx/debug.h>
#include <wx/cmdline.h>
#include <wx/config.h>
#include <wx/stdpaths.h> #include <wx/stdpaths.h>
#include <wx/filename.h> #include <wx/filename.h>
#include <wx/artprov.h> #include <wx/artprov.h>
// Tyro-specific variables
#include "definitions.h" #include "definitions.h"
#endif /* WX_COMMON_H */ #endif /* WX_COMMON_H */