2015-04-02 18:00:50 -04:00
|
|
|
/**
|
|
|
|
* Wrapper around wxAuiNotebook
|
|
|
|
*/
|
|
|
|
|
2015-05-07 17:05:27 -04:00
|
|
|
#include "widget.h"
|
2015-04-02 18:00:50 -04:00
|
|
|
|
2015-05-12 16:30:22 -04:00
|
|
|
extern TyroMenu *Glob_menu_bar;
|
2015-06-03 17:06:55 -04:00
|
|
|
extern wxStatusBar *Glob_status_bar;
|
2015-06-05 16:50:52 -04:00
|
|
|
|
|
|
|
static vector<EditPane *> editors;
|
2015-04-02 18:00:50 -04:00
|
|
|
static unsigned long untitled_document_count = 0;
|
|
|
|
|
2015-05-12 16:30:22 -04:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param wxWindow* parent
|
|
|
|
* @param wxWindowID id
|
|
|
|
* @param const wxPoint& pos
|
|
|
|
* @param const wxSize& size
|
|
|
|
* @param long style
|
|
|
|
*/
|
2015-04-02 18:00:50 -04:00
|
|
|
TabContainer::TabContainer(
|
|
|
|
wxWindow* parent,
|
|
|
|
wxWindowID id,
|
|
|
|
const wxPoint& pos,
|
|
|
|
const wxSize& size,
|
|
|
|
long style
|
|
|
|
) : wxAuiNotebook(parent, id, pos, size, style)
|
2015-04-24 16:55:10 -04:00
|
|
|
{
|
2015-05-15 16:55:18 -04:00
|
|
|
this->parent = (MainFrame *) parent;
|
|
|
|
|
2015-06-05 16:50:52 -04:00
|
|
|
this->Bind(wxEVT_AUINOTEBOOK_PAGE_CLOSE, &TabContainer::OnClose, this, wxID_ANY);
|
|
|
|
this->Bind(wxEVT_AUINOTEBOOK_PAGE_CLOSED, &TabContainer::OnClosed, this, wxID_ANY);
|
|
|
|
this->Bind(wxEVT_AUINOTEBOOK_TAB_RIGHT_DOWN, &TabContainer::OnTabContextMenu, this, wxID_ANY);
|
|
|
|
this->Bind(wxEVT_AUINOTEBOOK_PAGE_CHANGED, &TabContainer::OnTabSwitch, this, wxID_ANY);
|
2015-04-02 18:00:50 -04:00
|
|
|
}
|
|
|
|
|
2015-05-12 16:30:22 -04:00
|
|
|
/**
|
|
|
|
* Destructor
|
|
|
|
*/
|
2015-05-07 17:05:27 -04:00
|
|
|
TabContainer::~TabContainer()
|
|
|
|
{
|
|
|
|
wxLogDebug("TabContainer destructor called");
|
|
|
|
}
|
2015-04-02 18:00:50 -04:00
|
|
|
|
2015-06-05 16:50:52 -04:00
|
|
|
/**
|
|
|
|
* Create a new editor instance for a new tab
|
|
|
|
*
|
|
|
|
* @return EditPane*
|
|
|
|
*/
|
|
|
|
EditPane* TabContainer::NewEditor()
|
|
|
|
{
|
|
|
|
return new EditPane(this);
|
|
|
|
}
|
|
|
|
|
2015-04-21 17:06:21 -04:00
|
|
|
/**
|
|
|
|
* Add a new blank document
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2015-04-02 18:00:50 -04:00
|
|
|
void TabContainer::AddTab()
|
|
|
|
{
|
|
|
|
untitled_document_count++;
|
2015-04-13 09:30:56 -04:00
|
|
|
|
2015-04-02 18:00:50 -04:00
|
|
|
wxString caption;
|
2015-04-10 15:11:15 -04:00
|
|
|
|
2015-04-02 18:00:50 -04:00
|
|
|
caption.Printf("Untitled %lu", untitled_document_count);
|
2015-04-10 15:11:15 -04:00
|
|
|
|
2015-06-05 16:50:52 -04:00
|
|
|
this->AddPage(this->NewEditor(), caption, true);
|
2015-04-02 18:00:50 -04:00
|
|
|
}
|
|
|
|
|
2015-04-21 17:06:21 -04:00
|
|
|
/**
|
|
|
|
* Open an existing document
|
|
|
|
*
|
|
|
|
* @param wxString filePath
|
|
|
|
* @return void
|
|
|
|
*/
|
2015-04-02 18:00:50 -04:00
|
|
|
void TabContainer::AddTab(wxString filePath)
|
2015-05-15 16:55:18 -04:00
|
|
|
{
|
2015-04-10 15:11:15 -04:00
|
|
|
wxFileName fileName(filePath);
|
|
|
|
|
2015-05-15 16:55:18 -04:00
|
|
|
if ( ! (fileName.IsOk() && fileName.FileExists()))
|
|
|
|
{
|
|
|
|
wxLogDebug("Invalid file name.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-10 15:11:15 -04:00
|
|
|
wxString caption= fileName.GetFullName();
|
2015-06-05 16:50:52 -04:00
|
|
|
EditPane *editor = this->NewEditor();
|
2015-04-02 18:00:50 -04:00
|
|
|
|
2015-04-17 16:55:48 -04:00
|
|
|
if (editor->Load(filePath))
|
2015-04-10 15:11:15 -04:00
|
|
|
{
|
2015-04-15 12:17:25 -04:00
|
|
|
this->AddPage(editor, caption, true);
|
2015-04-17 16:55:48 -04:00
|
|
|
|
2015-04-24 16:55:10 -04:00
|
|
|
this->SetPageToolTip(this->GetPageIndex(this->GetCurrentPage()), fileName.GetFullPath());
|
|
|
|
|
2015-05-12 16:30:22 -04:00
|
|
|
// Select the appropriate language in the language menu
|
|
|
|
Glob_menu_bar->SetCurrentLanguage(editor->GetCurrentLang());
|
|
|
|
|
2015-04-17 16:55:48 -04:00
|
|
|
return;
|
2015-04-10 15:11:15 -04:00
|
|
|
}
|
2015-04-17 16:55:48 -04:00
|
|
|
|
|
|
|
wxLogDebug("Failed to load file!?");
|
2015-04-09 11:45:19 -04:00
|
|
|
}
|
|
|
|
|
2015-04-21 17:06:21 -04:00
|
|
|
/**
|
|
|
|
* Get the EditPane control in the current tab
|
|
|
|
*
|
|
|
|
* @return *EditPane
|
|
|
|
*/
|
2015-04-09 13:27:30 -04:00
|
|
|
EditPane *TabContainer::GetCurrentEditor()
|
2015-04-09 11:45:19 -04:00
|
|
|
{
|
2015-04-09 13:27:30 -04:00
|
|
|
return (EditPane *) this->GetCurrentPage();
|
2015-05-07 17:05:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the EditPane control in the specified tab
|
|
|
|
*
|
|
|
|
* @param size_t page_idx
|
|
|
|
* @return *EditPane
|
|
|
|
*/
|
|
|
|
EditPane *TabContainer::GetEditor(size_t page_idx)
|
|
|
|
{
|
|
|
|
return (EditPane *) this->GetPage(page_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event handler for file saving checks on tab close
|
|
|
|
*
|
|
|
|
* @param wxAuiNotebookEvent& event
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
void TabContainer::OnClose(wxAuiNotebookEvent &event)
|
|
|
|
{
|
|
|
|
int current_tab = this->GetSelection();
|
|
|
|
EditPane *editor = this->GetCurrentEditor();
|
|
|
|
|
|
|
|
// Sanity check
|
|
|
|
if (current_tab == -1) return;
|
|
|
|
|
|
|
|
if (editor->IsModified())
|
|
|
|
{
|
|
|
|
int Msgbox_Choice = wxMessageBox(
|
|
|
|
"File has not been saved, save file before closing?",
|
|
|
|
"Modified File",
|
|
|
|
wxYES_NO | wxCANCEL | wxICON_QUESTION,
|
|
|
|
this
|
|
|
|
);
|
|
|
|
|
|
|
|
if (Msgbox_Choice == wxCANCEL)
|
|
|
|
{
|
|
|
|
return event.Veto();
|
|
|
|
}
|
|
|
|
else if (Msgbox_Choice == wxYES)
|
|
|
|
{
|
|
|
|
editor->SaveFile();
|
|
|
|
if (editor->IsModified())
|
|
|
|
{
|
|
|
|
wxMessageBox(TYRO_SAVE_ERROR, TYRO_SAVE_ERROR_CAPTION, wxOK | wxICON_EXCLAMATION);
|
|
|
|
return event.Veto();
|
|
|
|
}
|
|
|
|
}
|
2015-06-05 16:50:52 -04:00
|
|
|
}
|
2015-05-07 17:05:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event handler triggered after a tab is closed
|
|
|
|
*
|
|
|
|
* @param WXUNUSED
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
void TabContainer::OnClosed(wxAuiNotebookEvent &WXUNUSED(event))
|
|
|
|
{
|
|
|
|
if (this->GetPageCount() == 0)
|
|
|
|
{
|
2015-05-15 16:55:18 -04:00
|
|
|
this->parent->EnableEditControls(false);
|
2015-06-03 17:06:55 -04:00
|
|
|
Glob_status_bar->SetStatusText("", STATUS_CURSOR_LOCATION);
|
|
|
|
Glob_status_bar->SetStatusText("", STATUS_CURRENT_LANGUAGE);
|
2015-05-07 17:05:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays a context menu on the current tab
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
void TabContainer::OnTabContextMenu(wxAuiNotebookEvent &WXUNUSED(event))
|
|
|
|
{
|
|
|
|
// Create Menu
|
|
|
|
wxMenu *contextMenu = new wxMenu();
|
|
|
|
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.");
|
2015-06-05 16:50:52 -04:00
|
|
|
contextMenu->Append(myID_CLOSE_ALL_BUT_THIS, "Close All but this\tCtrl+Shift+Alt+W", "Close all open documents, except the one selected");
|
2015-05-07 17:05:27 -04:00
|
|
|
|
|
|
|
this->PopupMenu(contextMenu);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close all the open tabs
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
void TabContainer::OnCloseAll(wxCommandEvent &WXUNUSED(event))
|
|
|
|
{
|
|
|
|
this->DeleteAllPages();
|
2015-05-15 16:55:18 -04:00
|
|
|
this->parent->EnableEditControls(false);
|
2015-05-07 17:05:27 -04:00
|
|
|
}
|
|
|
|
|
2015-06-05 16:50:52 -04:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
|
2015-05-07 17:05:27 -04:00
|
|
|
/**
|
|
|
|
* Update menu state when switching tabs
|
|
|
|
*
|
|
|
|
* @param wxAuiNotebookEvent& event
|
|
|
|
* @return void
|
|
|
|
*/
|
2015-05-12 16:30:22 -04:00
|
|
|
void TabContainer::OnTabSwitch(wxAuiNotebookEvent &event)
|
2015-05-07 17:05:27 -04:00
|
|
|
{
|
2015-05-12 16:30:22 -04:00
|
|
|
EditPane *editor = this->GetEditor(event.GetSelection());
|
|
|
|
|
|
|
|
// 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));
|
2015-05-07 17:05:27 -04:00
|
|
|
|
2015-05-12 16:30:22 -04:00
|
|
|
// Update language menu selection
|
|
|
|
Glob_menu_bar->SetCurrentLanguage(editor->GetCurrentLang());
|
2015-06-03 17:06:55 -04:00
|
|
|
|
|
|
|
// Update status bar
|
|
|
|
Glob_status_bar->SetStatusText(editor->GetCurrentLang(), STATUS_CURRENT_LANGUAGE);
|
2015-04-16 17:03:27 -04:00
|
|
|
}
|