📄 nfgtable.cc
字号:
// return the string extent virtual wxSize GetBestSize(wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, int row, int col); virtual wxGridCellRenderer *Clone() const { return new ColoredStringRenderer; }protected: // set the text colours before drawing void SetTextColoursAndFont(wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, bool isSelected); // calc the string extent for given string/font wxSize DoGetBestSize(wxGridCellAttr& attr, wxDC& dc, const wxString& text);};void ColoredStringRenderer::SetTextColoursAndFont(wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, bool isSelected){ dc.SetBackgroundMode( wxTRANSPARENT ); // TODO some special colours for attr.IsReadOnly() case? if ( isSelected ) { dc.SetTextBackground( grid.GetSelectionBackground() ); dc.SetTextForeground( grid.GetSelectionForeground() ); } else { dc.SetTextBackground( attr.GetBackgroundColour() ); dc.SetTextForeground( attr.GetTextColour() ); } dc.SetFont( attr.GetFont() );}wxSize ColoredStringRenderer::DoGetBestSize(wxGridCellAttr& attr, wxDC& dc, const wxString& text){ wxCoord x = 0, y = 0; dc.SetFont(attr.GetFont()); dc.GetTextExtent(text, &x, &y); return wxSize(x, y);}wxSize ColoredStringRenderer::GetBestSize(wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, int row, int col){ return DoGetBestSize(attr, dc, grid.GetCellValue(row, col));}void ColoredStringRenderer::Draw(wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, const wxRect& rectCell, int row, int col, bool isSelected){ wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); // now we only have to draw the text SetTextColoursAndFont(grid, attr, dc, isSelected); wxRect rect = rectCell; rect.Inflate(-1); wxCoord x, y; grid.DrawTextRectangle(dc, wxString("("), rect); dc.GetTextExtent("(", &x, &y); rect.x += x; wxString text = grid.GetCellValue(row, col); dc.SetTextForeground(*wxRED); for (unsigned int i = 0; i < text.Length(); i++) { if (text[i] == ',') { wxColour color = dc.GetTextForeground(); dc.SetTextForeground(*wxBLACK); grid.DrawTextRectangle(dc, wxString(","), rect); dc.GetTextExtent(",", &x, &y); rect.x += x; if (color == *wxRED) { dc.SetTextForeground(*wxBLUE); } else { dc.SetTextForeground(*wxRED); } } else { grid.DrawTextRectangle(dc, wxString(text[i]), rect); dc.GetTextExtent(text[i], &x, &y); rect.x += x; } } dc.SetTextForeground(*wxBLACK); grid.DrawTextRectangle(dc, wxString(")"), rect); }//======================================================================// class NfgTable: Member functions//======================================================================BEGIN_EVENT_TABLE(NfgTable, wxPanel) EVT_GRID_SELECT_CELL(NfgTable::OnCellSelect) EVT_GRID_CELL_LEFT_DCLICK(NfgTable::OnLeftDoubleClick) EVT_GRID_LABEL_LEFT_CLICK(NfgTable::OnLabelLeftClick)END_EVENT_TABLE()NfgTable::NfgTable(Nfg &p_nfg, wxWindow *p_parent) : wxPanel(p_parent, -1), m_nfg(p_nfg), m_parent(p_parent), m_editable(true), m_cursorMoving(false), m_rowPlayer(1), m_colPlayer(2), m_support(m_nfg), m_profile(0), m_showProb(0), m_showDom(0), m_showValue(0){ SetAutoLayout(true); m_grid = new wxGrid(this, -1, wxDefaultPosition, wxDefaultSize); m_grid->SetTable(new NfgGridTable(this, &m_nfg), true); m_grid->SetGridCursor(0, 0); m_grid->SetEditable(false); m_grid->SetDefaultCellFont(m_settings.GetDataFont()); m_grid->SetLabelFont(m_settings.GetLabelFont()); m_grid->SetDefaultCellAlignment(wxALIGN_CENTER, wxALIGN_CENTER); m_grid->DisableDragRowSize(); m_grid->DisableDragColSize(); m_grid->AutoSizeRows(); m_grid->AutoSizeColumns(); m_grid->AdjustScrollbars(); wxBoxSizer *topSizer = new wxBoxSizer(wxHORIZONTAL); topSizer->Add(m_grid, 1, wxALL | wxEXPAND | wxALIGN_RIGHT, 5); SetSizer(topSizer); topSizer->Fit(this); topSizer->SetSizeHints(this); Layout(); Show(true);}void NfgTable::SetContingency(const gArray<int> &p_profile){ m_grid->SetGridCursor(p_profile[GetRowPlayer()] - 1, p_profile[GetColPlayer()] - 1); RefreshTable();}gArray<int> NfgTable::GetContingency(void) const{ return ((NfgShow *) m_parent)->GetContingency();}void NfgTable::SetPlayers(int p_rowPlayer, int p_colPlayer){ m_grid->BeginBatch(); m_rowPlayer = p_rowPlayer; m_colPlayer = p_colPlayer; int stratRows = m_grid->GetRows() - m_showProb - m_showDom - m_showValue; int stratCols = m_grid->GetCols() - m_showProb - m_showDom - m_showValue; if (m_support.NumStrats(p_rowPlayer) < stratRows) { m_grid->DeleteRows(0, stratRows - m_support.NumStrats(p_rowPlayer)); } else if (m_support.NumStrats(p_rowPlayer) > stratRows) { m_grid->InsertRows(0, m_support.NumStrats(p_rowPlayer) - stratRows); } if (m_support.NumStrats(p_colPlayer) < stratCols) { m_grid->DeleteCols(0, stratCols - m_support.NumStrats(p_colPlayer)); } else if (m_support.NumStrats(p_colPlayer) > stratCols) { m_grid->InsertCols(0, m_support.NumStrats(p_colPlayer) - stratCols); } ((NfgShow *) m_parent)->SetStrategy(m_rowPlayer, 1); ((NfgShow *) m_parent)->SetStrategy(m_colPlayer, 1); m_grid->AutoSizeRows(); m_grid->AutoSizeColumns(); m_grid->EndBatch(); m_grid->AdjustScrollbars(); RefreshTable();}void NfgTable::SetStrategy(int p_player, int p_strategy){ if (!m_cursorMoving) { // prevents reentry if (p_player == GetRowPlayer()) { m_grid->SetGridCursor(p_strategy - 1, m_grid->GetCursorColumn()); } else if (p_player == GetColPlayer()) { m_grid->SetGridCursor(m_grid->GetCursorRow(), p_strategy - 1); } }}void NfgTable::ToggleProbs(void){ m_showProb = 1 - m_showProb; if (m_showProb) { m_grid->AppendCols(); m_grid->AppendRows(); } else { m_grid->DeleteCols(); m_grid->DeleteRows(); } m_grid->AutoSizeRows(); m_grid->AutoSizeColumns(); m_grid->AdjustScrollbars(); m_grid->Refresh();}void NfgTable::ToggleDominance(void){ m_showDom = 1 - m_showDom; if (m_showDom) { m_grid->AppendCols(); m_grid->AppendRows(); } else { m_grid->DeleteCols(); m_grid->DeleteRows(); } m_grid->AutoSizeRows(); m_grid->AutoSizeColumns(); m_grid->AdjustScrollbars(); m_grid->Refresh();}void NfgTable::ToggleValues(void){ m_showValue = 1 - m_showValue; if (m_showValue) { m_grid->AppendCols(); m_grid->AppendRows(); } else { m_grid->DeleteCols(); m_grid->DeleteRows(); } m_grid->AutoSizeRows(); m_grid->AutoSizeColumns(); m_grid->AdjustScrollbars(); m_grid->Refresh();}void NfgTable::SetDataFont(const wxFont &p_font){ m_settings.SetDataFont(p_font); m_settings.SaveSettings(); m_grid->SetDefaultCellFont(p_font); m_grid->AutoSizeRows(); m_grid->AutoSizeColumns();}void NfgTable::SetLabelFont(const wxFont &p_font) { m_settings.SetLabelFont(p_font); m_settings.SaveSettings(); m_grid->SetLabelFont(p_font); m_grid->AutoSizeRows(); m_grid->AutoSizeColumns();}void NfgTable::SetOutcomeValues(bool p_values){ m_settings.SetOutcomeValues(p_values); m_settings.SaveSettings(); m_grid->AutoSizeRows(); m_grid->AutoSizeColumns();}void NfgTable::OnCellSelect(wxGridEvent &p_event){ if (p_event.GetRow() >= m_support.NumStrats(GetRowPlayer()) || p_event.GetCol() >= m_support.NumStrats(GetColPlayer())) { p_event.Veto(); } else { m_cursorMoving = true; // this prevents re-entry ((NfgShow *) m_parent)->SetStrategy(GetRowPlayer(), p_event.GetRow() + 1); ((NfgShow *) m_parent)->SetStrategy(GetColPlayer(), p_event.GetCol() + 1); m_cursorMoving = false; // now continue with the default behavior (i.e., highlight the new cell) p_event.Skip(); }}void NfgTable::OnLeftDoubleClick(wxGridEvent &p_event){ if (m_editable && p_event.GetRow() < m_support.NumStrats(GetRowPlayer()) && p_event.GetCol() < m_support.NumStrats(GetColPlayer())) { wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, NFG_EDIT_CONTINGENCY); m_parent->AddPendingEvent(event); }}void NfgTable::OnLabelLeftClick(wxGridEvent &p_event){ // for the moment, just veto it p_event.Veto();}void NfgTable::SetSupport(const NFSupport &p_support){ m_support = p_support; SetPlayers(m_rowPlayer, m_colPlayer); RefreshTable();}void NfgTable::SetProfile(const MixedSolution &p_solution){ if (m_profile) { delete m_profile; } m_profile = new MixedSolution(p_solution); RefreshTable();}void NfgTable::ClearProfile(void){ if (m_profile) { delete m_profile; m_profile = 0; RefreshTable(); }}void NfgTable::RefreshTable(void){ m_grid->ForceRefresh(); m_grid->AutoSizeRows(); m_grid->AutoSizeColumns();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -