1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2008 by Eran Ifrah
// file name : openwindowspanel.cpp
//
// -------------------------------------------------------------------------
// A
// _____ _ _ _ _
// / __ \ | | | | (_) |
// | / \/ ___ __| | ___| | _| |_ ___
// | | / _ \ / _ |/ _ \ | | | __/ _ )
// | \__/\ (_) | (_| | __/ |___| | || __/
// \____/\___/ \__,_|\___\_____/_|\__\___|
//
// F i l e
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#include <wx/xrc/xmlres.h>
#include "globals.h"
#include <wx/clntdata.h>
#include "frame.h"
#include "event_notifier.h"
#include "manager.h"
#include "openwindowspanel.h"
#include "cl_config.h"
wxDECLARE_EVENT(wxEVT_TAB_LISTBOX_ISDIRTY, wxNotifyEvent);
wxDEFINE_EVENT(wxEVT_TAB_LISTBOX_ISDIRTY, wxNotifyEvent);
BEGIN_EVENT_TABLE(OpenWindowsPanel, OpenWindowsPanelBase)
EVT_MENU(XRCID("wxID_CLOSE_SELECTED"), OpenWindowsPanel::OnCloseSelectedFiles)
EVT_MENU(XRCID("wxID_SAVE_SELECTED"), OpenWindowsPanel::OnSaveSelectedFiles)
END_EVENT_TABLE()
OpenWindowsPanel::OpenWindowsPanel( wxWindow* parent, const wxString &caption )
: OpenWindowsPanelBase( parent )
, m_caption(caption)
, m_rclickMenu(wxXmlResource::Get()->LoadMenu(wxT("editor_tab_right_click")))
, m_mutliMenu(wxXmlResource::Get()->LoadMenu(wxT("tabs_multi_sels_menu")))
{
MSWSetNativeTheme(m_fileList);
clConfig cfg;
m_toolbarTabs->ToggleTool(XRCID("TabsSortTool"), cfg.Read("TabsPaneSortAlphabetically", true));
EventNotifier::Get()->Connect(wxEVT_ACTIVE_EDITOR_CHANGED, wxCommandEventHandler(OpenWindowsPanel::OnActiveEditorChanged), NULL, this);
EventNotifier::Get()->Connect(wxEVT_EDITOR_CLOSING, wxCommandEventHandler(OpenWindowsPanel::OnEditorClosing), NULL, this);
EventNotifier::Get()->Connect(wxEVT_ALL_EDITORS_CLOSED, wxCommandEventHandler(OpenWindowsPanel::OnAllEditorsClosed), NULL, this);
wxTheApp->Connect(wxEVT_COMMAND_AUINOTEBOOK_END_DRAG, wxAuiNotebookEventHandler(OpenWindowsPanel::OnDragEnded), NULL, this);
Connect(wxEVT_TAB_LISTBOX_ISDIRTY, wxCommandEventHandler(OpenWindowsPanel::OnListBoxIsDirty), NULL, this);
m_themeHelper = new ThemeHandlerHelper(this);
}
OpenWindowsPanel::~OpenWindowsPanel()
{
wxDELETE(m_themeHelper);
if (m_rclickMenu) {
delete m_rclickMenu;
m_rclickMenu = NULL;
}
if(m_mutliMenu) {
delete m_mutliMenu;
m_mutliMenu = NULL;
}
// clear list now, or wxGTK seems to crash on exit
m_fileList->Clear();
EventNotifier::Get()->Disconnect(wxEVT_ACTIVE_EDITOR_CHANGED, wxCommandEventHandler(OpenWindowsPanel::OnActiveEditorChanged), NULL, this);
EventNotifier::Get()->Disconnect(wxEVT_EDITOR_CLOSING, wxCommandEventHandler(OpenWindowsPanel::OnEditorClosing), NULL, this);
EventNotifier::Get()->Disconnect(wxEVT_ALL_EDITORS_CLOSED, wxCommandEventHandler(OpenWindowsPanel::OnAllEditorsClosed), NULL, this);
wxTheApp->Disconnect(wxEVT_COMMAND_AUINOTEBOOK_END_DRAG, wxAuiNotebookEventHandler(OpenWindowsPanel::OnDragEnded), NULL, this);
Disconnect(wxEVT_TAB_LISTBOX_ISDIRTY, wxCommandEventHandler(OpenWindowsPanel::OnListBoxIsDirty), NULL, this);
}
int OpenWindowsPanel::EditorItem(const LEditor *editor)
{
if (editor) {
wxString path = editor->GetFileName().GetFullPath();
for (unsigned i = 0; i < m_fileList->GetCount(); i++) {
MyStringClientData *data = dynamic_cast<MyStringClientData *>(m_fileList->GetClientObject(i));
if (data->GetData() == path)
return i;
}
}
return wxNOT_FOUND;
}
void OpenWindowsPanel::DoOpenSelectedItem(int item)
{
MyStringClientData *data = dynamic_cast<MyStringClientData *>(m_fileList->GetClientObject(item));
if(data) {
LEditor *editor = dynamic_cast<LEditor*>(clMainFrame::Get()->GetMainBook()->FindPage(data->GetData()));
if( editor && editor->GetFileName().FileExists() == false ) {
// "Untitled" document, this means that 'OpenFile' will probably fail for it
clMainFrame::Get()->GetMainBook()->SelectPage(editor);
} else {
clMainFrame::Get()->GetMainBook()->OpenFile(data->GetData(), wxEmptyString);
editor->DelayedSetActive(); // At least in wxGTK, this won't work synchronously
}
}
}
void OpenWindowsPanel::DoCloseSelectedItem(int item)
{
DoOpenSelectedItem(item); // make sure the editor is selected in MainBook
wxCommandEvent e(wxEVT_COMMAND_MENU_SELECTED, XRCID("close_file"));
ProcessEvent(e);
}
void OpenWindowsPanel::OnKeyDown( wxKeyEvent& event )
{
switch (event.GetKeyCode()) {
case WXK_RETURN:
case WXK_NUMPAD_ENTER:
case WXK_SPACE:
if (DoGetSingleSelection() != wxNOT_FOUND) {
DoOpenSelectedItem(DoGetSingleSelection());
}
break;
case WXK_DELETE:
case WXK_NUMPAD_DELETE: {
wxCommandEvent dummyEvent;
OnCloseSelectedFiles(dummyEvent);
m_fileList->SetFocus();
}
break;
default:
event.Skip();
break;
}
}
void OpenWindowsPanel::OnItemDClicked( wxCommandEvent& event )
{
DoOpenSelectedItem(event.GetSelection());
}
void OpenWindowsPanel::OnRightDown( wxMouseEvent& event )
{
wxUnusedVar(event);
wxArrayInt sels;
m_fileList->GetSelections(sels);
if(sels.GetCount() == 1) {
DoOpenSelectedItem(sels.Item(0));
PopupMenu(m_rclickMenu);
} else {
PopupMenu(m_mutliMenu);
}
}
void OpenWindowsPanel::OnChar(wxKeyEvent& event)
{
OnKeyDown(event);
}
void OpenWindowsPanel::OnActiveEditorChanged(wxCommandEvent& e)
{
e.Skip();
// We may get here when an editor has just been closed (and so a different one become active).
// So do nothing immediately, otherwise GetMainBook()->GetAllEditors() may still return the stale value
wxNotifyEvent event(wxEVT_TAB_LISTBOX_ISDIRTY);
wxPostEvent(this, event);
}
void OpenWindowsPanel::OnListBoxIsDirty(wxCommandEvent& e) // Called from OnActiveEditorChanged(), after a delay
{
LEditor *editor = clMainFrame::Get()->GetMainBook()->GetActiveEditor();
if(!editor)
return;
int i = EditorItem(editor);
if (i != wxNOT_FOUND) {
DoSelectItem(editor);
return;
}
if (i == wxNOT_FOUND) {
wxToolBarToolBase* sorttool = m_toolbarTabs->FindById(XRCID("TabsSortTool"));
if (sorttool && sorttool->IsToggled()) {
SortAlphabetically();
} else {
SortByEditorOrder();
}
}
DoSelectItem(editor);
}
void OpenWindowsPanel::OnAllEditorsClosed(wxCommandEvent& e)
{
e.Skip();
m_fileList->Clear();
}
void OpenWindowsPanel::OnEditorClosing(wxCommandEvent& e)
{
e.Skip();
LEditor *editor = dynamic_cast<LEditor*>((IEditor*) e.GetClientData());
if(!editor)
return;
int i = EditorItem(editor);
if (i != wxNOT_FOUND) {
m_fileList->Delete(i);
}
}
void OpenWindowsPanel::OnCloseSelectedFiles(wxCommandEvent& e)
{
wxUnusedVar(e);
wxArrayInt sels;
m_fileList->GetSelections(sels);
if(sels.IsEmpty())
return;
wxArrayString files;
for(size_t i=0; i<sels.GetCount(); i++) {
MyStringClientData *data = dynamic_cast<MyStringClientData *>(m_fileList->GetClientObject(sels.Item(i)));
if(data) {
files.Add(data->GetData());
}
}
for(size_t i=0; i<sels.GetCount(); i++) {
DoCloseItem(files.Item(i));
}
}
void OpenWindowsPanel::OnSaveSelectedFiles(wxCommandEvent& e)
{
wxUnusedVar(e);
wxArrayInt sels;
m_fileList->GetSelections(sels);
if(sels.IsEmpty())
return;
for(int i=0; i<(int)sels.GetCount(); i++) {
DoSaveItem(sels.Item(i));
}
}
void OpenWindowsPanel::DoSaveItem(int item)
{
MyStringClientData *data = dynamic_cast<MyStringClientData *>(m_fileList->GetClientObject(item));
if(data) {
LEditor * editor = clMainFrame::Get()->GetMainBook()->FindEditor(data->GetData());
if(editor) {
editor->SaveFile();
}
}
}
void OpenWindowsPanel::DoCloseItem(const wxString &filename)
{
clMainFrame::Get()->GetMainBook()->CloseEditor(filename);
}
void OpenWindowsPanel::DoClearSelections()
{
wxArrayInt sels;
m_fileList->GetSelections(sels);
if(sels.IsEmpty())
return;
for(size_t i=0; i<sels.GetCount(); i++)
m_fileList->SetSelection(sels.Item(i), false);
}
void OpenWindowsPanel::DoSelectItem(int item)
{
DoClearSelections();
m_fileList->SetSelection(item, true);
m_fileList->EnsureVisible(item);
}
void OpenWindowsPanel::DoSelectItem(const LEditor* editor)
{
DoClearSelections();
wxToolBarToolBase* sorttool = m_toolbarTabs->FindById(XRCID("TabsSortTool"));
if (sorttool && !sorttool->IsToggled()) {
// If we're sorting-by-editor-order, in wxAuiNotebook we can't rely on m_fileList's order
std::vector<LEditor*> editors;
clMainFrame::Get()->GetMainBook()->GetAllEditors(editors, MainBook::kGetAll_IncludeDetached|MainBook::kGetAll_RetainOrder);
for (size_t n = 0; n < editors.size(); ++n) {
if (editors.at(n)->GetFileName().GetFullPath() == editor->GetFileName().GetFullPath()) {
DoSelectItem(n);
return;
}
wxASSERT("Passed an editor which doesn't exist");
}
} else {
int i = EditorItem(editor);
if (i != wxNOT_FOUND) {
DoSelectItem(i);
}
}
}
int OpenWindowsPanel::DoGetSingleSelection()
{
wxArrayInt sels;
m_fileList->GetSelections(sels);
if(sels.IsEmpty())
return wxNOT_FOUND;
return sels.Item(0);
}
void OpenWindowsPanel::OnDragEnded(wxAuiNotebookEvent& event)
{
// We only care about editor re-ordering if we sort by editor order
wxToolBarToolBase* sorttool = m_toolbarTabs->FindById(XRCID("TabsSortTool"));
if (sorttool && !sorttool->IsToggled()) {
// First find which editor is being dragged. We'll need it to identify which tab to select afterwards
// and there's no point asking wxAuiNotebook or GetActiveEditor(), which return the wrong one.
LEditor* editor = NULL;
int sel = DoGetSingleSelection();
if (sel != wxNOT_FOUND) {
MyStringClientData* data = dynamic_cast<MyStringClientData *>(m_fileList->GetClientObject(sel));
if(data) {
editor = clMainFrame::Get()->GetMainBook()->FindEditor(data->GetData());
}
}
SortByEditorOrder();
if (editor) {
DoSelectItem(editor);
}
}
}
void OpenWindowsPanel::OnSortItems(wxCommandEvent& event)
{
if (event.IsChecked()) {
SortAlphabetically();
} else {
SortByEditorOrder();
}
LEditor* editor = clMainFrame::Get()->GetMainBook()->GetActiveEditor();
DoSelectItem(editor);
clConfig::Get().Write("TabsPaneSortAlphabetically", event.IsChecked());
}
void OpenWindowsPanel::OnSortItemsUpdateUI(wxUpdateUIEvent& event)
{
event.Enable(m_fileList->GetCount() > 0);
}
void OpenWindowsPanel::SortAlphabetically()
{
m_fileList->Clear();
std::vector<LEditor*> editors;
clMainFrame::Get()->GetMainBook()->GetAllEditors(editors, MainBook::kGetAll_IncludeDetached|MainBook::kGetAll_RetainOrder);
for (size_t n = 0; n < editors.size(); ++n) {
LEditor* editor = editors.at(n);
wxString name = editor->GetFileName().GetFullName();
MyStringClientData* data = new MyStringClientData(editor->GetFileName().GetFullPath());
bool inserted(false);
for (size_t i = 0; i < m_fileList->GetCount(); ++i) {
if (name.Cmp(m_fileList->GetString(i)) < 0) {
m_fileList->Insert(name, i, data);
inserted = true;
break;
}
}
if (!inserted) {
m_fileList->Append(name, data);
}
}
}
void OpenWindowsPanel::SortByEditorOrder()
{
std::vector<LEditor*> editors;
clMainFrame::Get()->GetMainBook()->GetAllEditors(editors, MainBook::kGetAll_IncludeDetached|MainBook::kGetAll_RetainOrder);
m_fileList->Clear();
for (size_t n = 0; n < editors.size(); ++n) {
LEditor* editor = editors.at(n);
wxString name = editor->GetFileName().GetFullName();
MyStringClientData* data = new MyStringClientData(editor->GetFileName().GetFullPath());
m_fileList->Append(name, data);
}
}
|