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
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2014 Eran Ifrah
// file name : unredobase.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 "unredobase.h"
#include "clToolBar.h"
#include "macros.h"
#include <algorithm>
#include <wx/app.h>
#include <wx/menu.h>
#include <wx/xrc/xmlres.h>
const int FIRST_MENU_ID = 10000;
wxString GetBestLabel(CLCommand::Ptr_t command)
{
wxString label, text;
if(command) {
if(!command->GetUserLabel().empty()) {
label = command->GetUserLabel();
} else {
label = command->GetName();
text = command->GetText();
size_t len = text.Len();
if(len) {
text.Replace("\r\n", "\\n"); // Otherwise newlines result in a multiline display!
text.Replace("\n", "\\n");
// Truncate long pastes
if(len > 70) {
wxString shorter = text.Left(34);
shorter << " ... " << text.Right(34);
text = shorter;
}
label << " \"" << text << "\"";
}
}
}
return label;
}
CommandProcessorBase::CommandProcessorBase()
: m_currentCommand(-1)
{
Bind(wxEVT_TOOL_DROPDOWN, &CommandProcessorBase::OnTBUnRedo, this, wxID_UNDO);
Bind(wxEVT_TOOL_DROPDOWN, &CommandProcessorBase::OnTBUnRedo, this, wxID_REDO);
}
CommandProcessorBase::~CommandProcessorBase()
{
Clear();
Unbind(wxEVT_TOOL_DROPDOWN, &CommandProcessorBase::OnTBUnRedo, this, wxID_UNDO);
Unbind(wxEVT_TOOL_DROPDOWN, &CommandProcessorBase::OnTBUnRedo, this, wxID_REDO);
}
void CommandProcessorBase::ProcessOpenCommand()
{
CLCommand::Ptr_t command = GetOpenCommand();
wxCHECK_RET(command, "Trying to process a non-existing or non-open command");
command->SetName(GetBestLabel(command)); // Update the item's label
CloseOpenCommand();
}
CLCommand::Ptr_t CommandProcessorBase::GetOpenCommand()
{
CLCommand::Ptr_t command(NULL);
size_t size = GetCommands().size();
if(size && GetCommands().at(size - 1)->IsOpen()) {
command = GetCommands().at(size - 1);
}
return command;
}
void CommandProcessorBase::CloseOpenCommand()
{
CLCommand::Ptr_t command = GetOpenCommand();
wxCHECK_RET(command, "Trying to close to a non-existent or already-closed command");
command->Close();
}
void CommandProcessorBase::IncrementCurrentCommand()
{
wxCHECK_RET((m_currentCommand + 1) < (int)GetCommands().size(), "Can't increment the current command");
++m_currentCommand;
}
void CommandProcessorBase::DecrementCurrentCommand()
{
wxCHECK_RET(m_currentCommand > -1, "Can't decrement the current command");
// Close any open command. That makes sense anyway if we're undoing and, if we don't, the command doesn't get a
// sensible label when it's displayed in the dropdown menu
if(GetOpenCommand()) {
ProcessOpenCommand();
}
--m_currentCommand;
}
void CommandProcessorBase::SetUserLabel(const wxString& label)
{
if(GetOpenCommand()) {
ProcessOpenCommand(); // We need to make it non-pending, otherwise it may change after the label is set; which
// probably won't be what the user expects
}
CLCommand::Ptr_t command = GetActiveCommand();
if(command) {
command->SetUserLabel(label);
}
}
void CommandProcessorBase::OnTBUnRedo(wxCommandEvent& event)
{
PopulateUnRedoMenu(dynamic_cast<clToolBar*>(event.GetEventObject()), event.GetId());
}
void CommandProcessorBase::PopulateUnRedoMenu(clToolBar* tb, wxWindowID toolId)
{
CHECK_PTR_RET(tb);
wxMenu menu;
DoPopulateUnRedoMenu(menu, (toolId == wxID_UNDO));
if(toolId == wxID_UNDO) {
menu.Bind(wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(CommandProcessorBase::OnUndoDropdownItem), this);
tb->ShowMenuForButton(toolId, &menu);
menu.Unbind(wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(CommandProcessorBase::OnUndoDropdownItem), this);
} else {
menu.Bind(wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(CommandProcessorBase::OnRedoDropdownItem), this);
tb->ShowMenuForButton(toolId, &menu);
menu.Unbind(wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(CommandProcessorBase::OnRedoDropdownItem), this);
}
}
void CommandProcessorBase::DoPopulateUnRedoMenu(wxMenu& menu, bool undoing)
{
wxString prefix(undoing ? _("Undo ") : _("Redo "));
int id = FIRST_MENU_ID;
int count = 0;
if(undoing) {
if(GetCommands().size() > 0) {
for(CLCommand::Vec_t::const_reverse_iterator iter = GetCommands().rbegin() + GetNextUndoCommand();
iter != GetCommands().rend(); ++iter) {
CLCommand::Ptr_t command = *iter;
if(command) {
wxString label;
if(!command->GetUserLabel().empty()) {
if(command->GetName().Contains(":")) {
label = command->GetName().BeforeFirst(':') + ": ";
}
label << command->GetUserLabel();
} else {
if(command == GetOpenCommand()) {
label = GetBestLabel(
command); // If the command's still open, there won't otherwise be a name string
} else {
label = command->GetName();
}
}
menu.Append(id++, wxString::Format("%i ", ++count) + prefix + label);
}
}
}
} else {
for(CLCommand::Vec_t::const_iterator iter = GetCommands().begin() + GetCurrentCommand() + 1;
iter != GetCommands().end(); ++iter) {
CLCommand::Ptr_t command = *iter;
if(command) {
wxString label;
if(!command->GetUserLabel().empty()) {
if(command->GetName().Contains(":")) {
label = command->GetName().BeforeFirst(':') + ": ";
}
label << command->GetUserLabel();
} else {
label = command->GetName();
}
menu.Append(id++, wxString::Format("%i ", ++count) + prefix + label);
}
}
}
}
void CommandProcessorBase::PrepareLabelledStatesMenu(wxMenu* editmenu)
{
// First remove any current labelled-state submenu, which will almost certainly hold stale data
wxMenuItem* menuitem = editmenu->FindItem(XRCID("goto_labelled_state"));
if(menuitem) {
editmenu->Delete(menuitem);
}
// Find the item we want to insert _after_
size_t pos;
menuitem = editmenu->FindChildItem(XRCID("label_current_state"), &pos);
wxCHECK_RET(menuitem && (int)pos > wxNOT_FOUND, "Failed to find the 'label_current_state' item");
int kludge = 0;
if(pos == 2) { // For some reason, the insertion point of the CL edit menu is otherwise off-by-1. The wxC frame one
// isn't :/
kludge = 1;
}
// Get any labelled undo/redo states into the submenu. If none, abort.
wxMenu* submenu = new wxMenu;
PopulateLabelledStatesMenu(submenu);
if(submenu->GetMenuItemCount()) {
editmenu->Insert(pos + 1 + kludge, XRCID("goto_labelled_state"), _("Undo/Redo to a pre&viously labelled state"),
submenu);
} else {
delete submenu;
}
}
void CommandProcessorBase::PopulateLabelledStatesMenu(wxMenu* menu)
{
wxCHECK_RET(menu, "NULL menu");
for(size_t n = menu->GetMenuItemCount(); n > 0; --n) { // We need to delete any items left over from a previous call
wxMenuItem* item = menu->FindItemByPosition(n - 1);
if(item) {
menu->Delete(item);
}
}
if(!GetInitialCommand()->GetUserLabel().empty()) { // First any initial label
menu->Append(FIRST_MENU_ID - 1,
GetInitialCommand()->GetUserLabel()); // (Ab)use an out-of-range id to avoid confusion
}
int id = FIRST_MENU_ID;
for(CLCommand::Vec_t::const_iterator iter = GetCommands().begin(); iter != GetCommands().end(); ++iter) {
CLCommand::Ptr_t command = *iter;
if(command && !command->GetUserLabel().empty()) {
menu->Append(id, command->GetUserLabel());
}
++id; // It's easier in the handler if the event id is the GetCommands() index, rather than a menu index
}
menu->Bind(wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(CommandProcessorBase::OnLabelledStatesMenuItem),
this);
}
void CommandProcessorBase::UnBindLabelledStatesMenu(wxMenu* menu)
{
wxCHECK_RET(menu, "NULL menu");
CallAfter(&CommandProcessorBase::DoUnBindLabelledStatesMenu,
menu); // We can't Unbind yet: the event won't have been caught
}
void CommandProcessorBase::DoUnBindLabelledStatesMenu(wxMenu* menu)
{
wxCHECK_RET(menu, "NULL menu");
menu->Unbind(wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(CommandProcessorBase::OnLabelledStatesMenuItem),
this);
}
void CommandProcessorBase::OnLabelledStatesMenuItem(wxCommandEvent& event)
{
if(GetOpenCommand()) {
ProcessOpenCommand();
}
int index = event.GetId() - FIRST_MENU_ID; // NB index will be -1 if the selection is the initial-command's label.
// This will always mean *un*dos (or nothing)
wxCHECK_RET(index < (int)GetCommands().size(), "An ID that overruns the command-list");
if(index < GetCurrentCommand()) {
const int count = GetCurrentCommand() - index;
for(int n = 0; n < count; ++n) {
if(DoUndo()) {
DecrementCurrentCommand();
}
}
} else {
const int count = index - GetCurrentCommand();
for(int n = 0; n < count; ++n) {
if(DoRedo()) {
IncrementCurrentCommand();
}
}
}
}
void CommandProcessorBase::OnUndoDropdownItem(wxCommandEvent& event)
{
if(GetOpenCommand()) {
ProcessOpenCommand();
}
const int count = event.GetId() - FIRST_MENU_ID + 1;
for(int n = 0; n < count; ++n) {
if(DoUndo()) {
DecrementCurrentCommand();
}
}
}
void CommandProcessorBase::OnRedoDropdownItem(wxCommandEvent& event)
{
const int count = event.GetId() - FIRST_MENU_ID + 1;
for(int n = 0; n < count; ++n) {
if(DoRedo()) {
IncrementCurrentCommand();
}
}
}
CLCommand::Ptr_t CommandProcessorBase::GetActiveCommand() const
{
CLCommand::Ptr_t command(NULL);
if(GetCurrentCommand() == -1) {
command = GetInitialCommand();
} else if(GetCommands().size() > 0) {
command = GetCommands().at(GetCurrentCommand());
}
return command;
}
|