FilePane and Close All but this context menu item on tabs

This commit is contained in:
Tim Warren 2015-06-05 16:50:52 -04:00
parent 1fa24545d2
commit 619db418ff
9 changed files with 232 additions and 56 deletions

View File

@ -259,25 +259,19 @@ bool EditPane::FileWritable()
*/ */
void EditPane::BindEvents() void EditPane::BindEvents()
{ {
Bind(wxEVT_STC_MARGINCLICK, &EditPane::OnMarginClick, this, wxID_ANY); // Code folding event handler
Bind(wxEVT_STC_CHARADDED, &EditPane::OnCharAdded, this, wxID_ANY); this->Bind(wxEVT_STC_MARGINCLICK, [=](wxStyledTextEvent& event) {
} if (event.GetMargin() == MARGIN_FOLD)
{
/** int lineClick = this->LineFromPosition (event.GetPosition());
* Code folding event handler int levelClick = this->GetFoldLevel (lineClick);
* if ((levelClick & wxSTC_FOLDLEVELHEADERFLAG) > 0) {
* @param wxStyledTextEvent& event this->ToggleFold (lineClick);
* @return void }
*/
void EditPane::OnMarginClick(wxStyledTextEvent& event)
{
if (event.GetMargin() == MARGIN_FOLD) {
int lineClick = this->LineFromPosition (event.GetPosition());
int levelClick = this->GetFoldLevel (lineClick);
if ((levelClick & wxSTC_FOLDLEVELHEADERFLAG) > 0) {
this->ToggleFold (lineClick);
} }
} }, wxID_ANY);
this->Bind(wxEVT_STC_CHARADDED, &EditPane::OnCharAdded, this, wxID_ANY);
} }
/** /**

View File

@ -30,7 +30,6 @@ private:
bool FileReadable(); bool FileReadable();
bool FileWritable(); bool FileWritable();
void BindEvents(); void BindEvents();
void OnMarginClick(wxStyledTextEvent &event);
void OnCharAdded(wxStyledTextEvent &event); void OnCharAdded(wxStyledTextEvent &event);
void SetTheme(string theme_name); void SetTheme(string theme_name);
void _ApplyTheme(JsonValue &lexer_map); void _ApplyTheme(JsonValue &lexer_map);

94
src/widgets/FilePane.cpp Normal file
View File

@ -0,0 +1,94 @@
#include "widget.h"
extern MainFrame *Glob_main_frame;
enum CustomFilePaneEventIds {
myFP_ID_OPEN = (wxID_HIGHEST * 2),
myFP_ID_RENAME,
myFP_ID_DELETE
};
/**
* Constructor
* @param parent
* @param id
* @param dir
* @param pos
* @param size
* @param style
* @param filter
*/
FilePane::FilePane(
wxWindow* parent,
const wxWindowID id,
const wxString &dir,
const wxPoint &pos,
const wxSize &size,
long style,
const wxString &filter
) : wxGenericDirCtrl(parent, id, dir, pos, size, style, filter)
{
this->CreateContextMenu();
this->BindEvents();
this->SetDefaultPath(wxStandardPaths::Get().GetDocumentsDir());
this->SetPath(this->GetDefaultPath());
}
/**
* Destructor
*/
FilePane::~FilePane()
{
delete this->contextMenu;
}
/**
* Bind event handlers
*
* @return void
*/
void FilePane::BindEvents()
{
// Open files on double-click or enter
this->Bind(wxEVT_DIRCTRL_FILEACTIVATED, [=](wxTreeEvent&) {
wxLogDebug("File activated event fired");
this->OpenSelectedFiles();
}, wxID_ANY);
// Context Menu
this->Bind(wxEVT_TREE_ITEM_RIGHT_CLICK, [=](wxTreeEvent&) {
this->PopupMenu(this->contextMenu);
}, wxID_ANY);
// Open file(s)
this->Bind(wxEVT_MENU, [=](wxCommandEvent&) {
this->OpenSelectedFiles();
}, myFP_ID_OPEN);
}
/**
* Create the filePane Context Menu
*
* @return void
*/
void FilePane::CreateContextMenu()
{
this->contextMenu = new wxMenu();
this->contextMenu->Append(myFP_ID_OPEN, "&Open Ctrl+Shift+O", "Open the selected file(s)");
//this->contextMenu->Append(myFP_ID_RENAME, "&Rename Ctrl+Shift+R", "Rename the selected file");
//this->contextMenu->Append(myFP_ID_DELETE, "Delete", "Delete the selected file");
}
/**
* Open the files that are currently selected in the file pane in the current
* editor window
*
* @return void
*/
void FilePane::OpenSelectedFiles()
{
wxArrayString paths;
this->GetPaths(paths);
Glob_main_frame->OpenFiles(paths);
}

24
src/widgets/FilePane.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef TYRO_FILE_PANE_H
#define TYRO_FILE_PANE_H
class FilePane : public wxGenericDirCtrl {
public:
FilePane(
wxWindow *parent,
const wxWindowID id = wxID_ANY,
const wxString &dir = wxDirDialogDefaultFolderStr,
const wxPoint &pos = wxDefaultPosition,
const wxSize &size = wxDefaultSize,
long style = wxDIRCTRL_EDIT_LABELS | wxDIRCTRL_MULTIPLE,
const wxString &filter = wxEmptyString
);
~FilePane();
private:
wxMenu *contextMenu = nullptr;
void BindEvents();
void CreateContextMenu();
void OpenSelectedFiles();
};
#endif /* TYRO_FILE_PANE_H */

View File

@ -8,6 +8,7 @@ extern TyroMenu *Glob_menu_bar;
extern PrefPane *Glob_pref_pane; extern PrefPane *Glob_pref_pane;
extern wxStatusBar *Glob_status_bar; extern wxStatusBar *Glob_status_bar;
static TabContainer *notebook = nullptr; static TabContainer *notebook = nullptr;
static FilePane *filePane = nullptr;
// Frame icon // Frame icon
#include "../../resources/xpm/tyro.xpm" #include "../../resources/xpm/tyro.xpm"
@ -21,6 +22,8 @@ MainFrame::MainFrame(wxFrame *frame, const wxString &title)
// Create the tab container // Create the tab container
notebook = new TabContainer(this); notebook = new TabContainer(this);
filePane = new FilePane(this);
// Set the frame icon // Set the frame icon
wxIcon app_icon(tyro_icon); wxIcon app_icon(tyro_icon);
this->SetIcon(app_icon); this->SetIcon(app_icon);
@ -45,6 +48,7 @@ MainFrame::~MainFrame()
wxLogDebug("Main Frame Destructor Called."); wxLogDebug("Main Frame Destructor Called.");
delete notebook; delete notebook;
delete toolBar; delete toolBar;
delete filePane;
wxDELETE(this->findDlg); wxDELETE(this->findDlg);
wxDELETE(this->findData); wxDELETE(this->findData);
@ -76,6 +80,16 @@ void MainFrame::DoLayout()
.Resizable(true); .Resizable(true);
this->manager->AddPane(toolBar, toolBarPaneInfo); this->manager->AddPane(toolBar, toolBarPaneInfo);
wxAuiPaneInfo filePaneInfo;
filePaneInfo.Left()
.MinSize(225, 550)
.TopDockable(false)
.BottomDockable(false)
.RightDockable(true)
.LeftDockable(true)
.Resizable(true);
this->manager->AddPane(filePane, filePaneInfo);
wxAuiPaneInfo notebookPaneInfo; wxAuiPaneInfo notebookPaneInfo;
notebookPaneInfo.CenterPane(); notebookPaneInfo.CenterPane();
this->manager->AddPane(notebook, notebookPaneInfo); this->manager->AddPane(notebook, notebookPaneInfo);
@ -162,6 +176,7 @@ void MainFrame::BindEvents()
Bind(wxEVT_MENU, &MainFrame::OnSave, this, wxID_SAVE); Bind(wxEVT_MENU, &MainFrame::OnSave, this, wxID_SAVE);
Bind(wxEVT_MENU, &MainFrame::OnSaveAs, this, wxID_SAVEAS); Bind(wxEVT_MENU, &MainFrame::OnSaveAs, this, wxID_SAVEAS);
Bind(wxEVT_MENU, &MainFrame::OnCloseTab, this, wxID_CLOSE); Bind(wxEVT_MENU, &MainFrame::OnCloseTab, this, wxID_CLOSE);
Bind(wxEVT_MENU, &TabContainer::OnCloseAllButThis, notebook, myID_CLOSE_ALL_BUT_THIS);
Bind(wxEVT_MENU, &TabContainer::OnCloseAll, notebook, myID_CLOSE_ALL); Bind(wxEVT_MENU, &TabContainer::OnCloseAll, notebook, myID_CLOSE_ALL);
Bind(wxEVT_MENU, &MainFrame::OnQuit, this, wxID_EXIT); Bind(wxEVT_MENU, &MainFrame::OnQuit, this, wxID_EXIT);

View File

@ -15,6 +15,13 @@ public:
wxSizer *sizer = new wxBoxSizer(wxVERTICAL); wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
// Font size will have to wait until wxWidgets 3.0.3, where font widgets
// will actually work
/*wxFontPickerCtrl *fontPicker = new wxFontPickerCtrl(this, wxID_ANY);
wxSizer *fontSizer = new wxBoxSizer(wxHORIZONTAL);
fontSizer->Add(fontPicker, wxSizerFlags().Border());
sizer->Add(fontSizer, wxSizerFlags().Border());*/
sizer->Add(showLineNumbers, wxSizerFlags().Border()); sizer->Add(showLineNumbers, wxSizerFlags().Border());
sizer->Add(showIndentGuides, wxSizerFlags().Border()); sizer->Add(showIndentGuides, wxSizerFlags().Border());
sizer->Add(showCodeFolding, wxSizerFlags().Border()); sizer->Add(showCodeFolding, wxSizerFlags().Border());
@ -25,9 +32,24 @@ public:
// On supported platforms // On supported platforms
if (wxPreferencesEditor::ShouldApplyChangesImmediately()) if (wxPreferencesEditor::ShouldApplyChangesImmediately())
{ {
showLineNumbers->Bind(wxEVT_CHECKBOX, &GeneralPrefPanePage::ToggleShowLineNumbers, this, myID_PREFS_LINE_NUMBERS); showLineNumbers->Bind(wxEVT_CHECKBOX, [=] (wxCommandEvent &event) {
showIndentGuides->Bind(wxEVT_CHECKBOX, &GeneralPrefPanePage::ToggleShowIndentGuides, this, myID_PREFS_IDENT_GUIDES); Glob_config->Write("show_line_numbers", event.IsChecked());
showCodeFolding->Bind(wxEVT_CHECKBOX, &GeneralPrefPanePage::ToggleShowCodeFolding, this, myID_PREFS_CODE_FOLDING); this->frame->OnPrefsChanged(event);
Glob_config->Flush();
}, myID_PREFS_LINE_NUMBERS);
showIndentGuides->Bind(wxEVT_CHECKBOX, [=] (wxCommandEvent &event) {
Glob_config->Write("show_indent_guides", event.IsChecked());
this->frame->OnPrefsChanged(event);
Glob_config->Flush();
}, myID_PREFS_IDENT_GUIDES);
showCodeFolding->Bind(wxEVT_CHECKBOX, [=] (wxCommandEvent &event) {
Glob_config->Write("show_code_folding", event.IsChecked());
this->frame->OnPrefsChanged(event);
Glob_config->Flush();
}, myID_PREFS_CODE_FOLDING);
} }
} }
@ -74,31 +96,9 @@ public:
private: private:
MainFrame *frame; MainFrame *frame;
wxCheckBox *showLineNumbers; wxCheckBox *showLineNumbers = nullptr;
wxCheckBox *showIndentGuides; wxCheckBox *showIndentGuides = nullptr;
wxCheckBox *showCodeFolding; wxCheckBox *showCodeFolding = nullptr;
void ToggleShowLineNumbers(wxCommandEvent &event)
{
Glob_config->Write("show_line_numbers", event.IsChecked());
this->frame->OnPrefsChanged(event);
Glob_config->Flush();
}
void ToggleShowIndentGuides(wxCommandEvent &event)
{
Glob_config->Write("show_indent_guides", event.IsChecked());
this->frame->OnPrefsChanged(event);
Glob_config->Flush();
}
void ToggleShowCodeFolding(wxCommandEvent &event)
{
Glob_config->Write("show_code_folding", event.IsChecked());
this->frame->OnPrefsChanged(event);
Glob_config->Flush();
}
}; };
class GeneralPrefPane: public wxStockPreferencesPage { class GeneralPrefPane: public wxStockPreferencesPage {

View File

@ -6,6 +6,8 @@
extern TyroMenu *Glob_menu_bar; extern TyroMenu *Glob_menu_bar;
extern wxStatusBar *Glob_status_bar; extern wxStatusBar *Glob_status_bar;
static vector<EditPane *> editors;
static unsigned long untitled_document_count = 0; static unsigned long untitled_document_count = 0;
/** /**
@ -27,10 +29,10 @@ TabContainer::TabContainer(
{ {
this->parent = (MainFrame *) parent; this->parent = (MainFrame *) parent;
Bind(wxEVT_AUINOTEBOOK_PAGE_CLOSE, &TabContainer::OnClose, this, wxID_ANY); this->Bind(wxEVT_AUINOTEBOOK_PAGE_CLOSE, &TabContainer::OnClose, this, wxID_ANY);
Bind(wxEVT_AUINOTEBOOK_PAGE_CLOSED, &TabContainer::OnClosed, this, wxID_ANY); this->Bind(wxEVT_AUINOTEBOOK_PAGE_CLOSED, &TabContainer::OnClosed, this, wxID_ANY);
Bind(wxEVT_AUINOTEBOOK_TAB_RIGHT_DOWN, &TabContainer::OnTabContextMenu, this, wxID_ANY); this->Bind(wxEVT_AUINOTEBOOK_TAB_RIGHT_DOWN, &TabContainer::OnTabContextMenu, this, wxID_ANY);
Bind(wxEVT_AUINOTEBOOK_PAGE_CHANGED, &TabContainer::OnTabSwitch, this, wxID_ANY); this->Bind(wxEVT_AUINOTEBOOK_PAGE_CHANGED, &TabContainer::OnTabSwitch, this, wxID_ANY);
} }
/** /**
@ -41,6 +43,16 @@ TabContainer::~TabContainer()
wxLogDebug("TabContainer destructor called"); wxLogDebug("TabContainer destructor called");
} }
/**
* Create a new editor instance for a new tab
*
* @return EditPane*
*/
EditPane* TabContainer::NewEditor()
{
return new EditPane(this);
}
/** /**
* Add a new blank document * Add a new blank document
* *
@ -54,9 +66,7 @@ void TabContainer::AddTab()
caption.Printf("Untitled %lu", untitled_document_count); caption.Printf("Untitled %lu", untitled_document_count);
EditPane *editor = new EditPane(this); this->AddPage(this->NewEditor(), caption, true);
this->AddPage(editor, caption, true);
} }
/** /**
@ -76,7 +86,7 @@ void TabContainer::AddTab(wxString filePath)
} }
wxString caption= fileName.GetFullName(); wxString caption= fileName.GetFullName();
EditPane *editor = new EditPane(this); EditPane *editor = this->NewEditor();
if (editor->Load(filePath)) if (editor->Load(filePath))
{ {
@ -150,7 +160,7 @@ void TabContainer::OnClose(wxAuiNotebookEvent &event)
return event.Veto(); return event.Veto();
} }
} }
}; }
} }
/** /**
@ -180,6 +190,7 @@ void TabContainer::OnTabContextMenu(wxAuiNotebookEvent &WXUNUSED(event))
wxMenu *contextMenu = new wxMenu(); wxMenu *contextMenu = new wxMenu();
contextMenu->Append(wxID_CLOSE, "&Close\tCtrl+W", "Close the current tab"); contextMenu->Append(wxID_CLOSE, "&Close\tCtrl+W", "Close the current tab");
contextMenu->Append(myID_CLOSE_ALL, "C&lose All\tShift+Ctrl+W", "Close all open documents."); contextMenu->Append(myID_CLOSE_ALL, "C&lose All\tShift+Ctrl+W", "Close all open documents.");
contextMenu->Append(myID_CLOSE_ALL_BUT_THIS, "Close All but this\tCtrl+Shift+Alt+W", "Close all open documents, except the one selected");
this->PopupMenu(contextMenu); this->PopupMenu(contextMenu);
} }
@ -195,6 +206,40 @@ void TabContainer::OnCloseAll(wxCommandEvent &WXUNUSED(event))
this->parent->EnableEditControls(false); this->parent->EnableEditControls(false);
} }
/**
* Close all tabs but the one selected
*
* @param wxCommandEvent& event
* @return void
*/
void TabContainer::OnCloseAllButThis(wxCommandEvent &WXUNUSED(event))
{
auto ind = this->GetSelection();
wxString curr_tooltip = this->GetPageToolTip(ind);
wxString curr_caption = this->GetPageText(ind);
size_t page_count = this->GetPageCount();
this->Freeze();
// Do the loop in this order, as the indices change
// when tabs are removed
size_t i = page_count;
while(i > 0)
{
i--;
// Identify the current tab by the caption and tooltip
// If they don't match, remove the tab
if (
curr_tooltip == this->GetPageToolTip(i) &&
curr_caption == this->GetPageText(i)
) continue;
this->RemovePage(i);
}
this->Thaw();
}
/** /**
* Update menu state when switching tabs * Update menu state when switching tabs
* *

View File

@ -28,8 +28,10 @@ public:
EditPane *GetCurrentEditor(); EditPane *GetCurrentEditor();
EditPane *GetEditor(size_t page_idx); EditPane *GetEditor(size_t page_idx);
void OnCloseAll(wxCommandEvent &event); void OnCloseAll(wxCommandEvent &event);
void OnCloseAllButThis(wxCommandEvent &event);
private: private:
MainFrame *parent = nullptr; MainFrame *parent = nullptr;
EditPane *NewEditor();
void OnTabSwitch(wxAuiNotebookEvent &event); void OnTabSwitch(wxAuiNotebookEvent &event);
void OnClose(wxAuiNotebookEvent &event); void OnClose(wxAuiNotebookEvent &event);
void OnClosed(wxAuiNotebookEvent &event); void OnClosed(wxAuiNotebookEvent &event);

View File

@ -13,6 +13,8 @@
#include <wx/aui/aui.h> #include <wx/aui/aui.h>
#include <wx/stc/stc.h> #include <wx/stc/stc.h>
#include <wx/preferences.h> #include <wx/preferences.h>
#include <wx/dirctrl.h>
#include <wx/fontpicker.h>
// Tyro includes // Tyro includes
#include "TyroMenu.h" #include "TyroMenu.h"
@ -20,6 +22,7 @@
#include "TabContainer.h" #include "TabContainer.h"
#include "MainFrame.h" #include "MainFrame.h"
#include "PrefPane.h" #include "PrefPane.h"
#include "FilePane.h"
#endif /* TYRO_WIDGET_H */ #endif /* TYRO_WIDGET_H */