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