Exemplo n.º 1
0
void DataGrid::onKeyDown(wxKeyEvent &event)
{
	bool modified = false;
	if (event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER) {
		if (event.ControlDown()) {
			if (!IsCellEditControlEnabled()) {
				insertRowToDataUp(GetGridCursorRow());
				modified = true;
			}
		} else {
			insertRowToData(GetGridCursorRow());
			modified = true;
		}
	} else if (event.GetKeyCode() == WXK_DELETE) {
		if (IsSelection()) {
			wxArrayInt rows = GetSelectedRows();
			if (rows.Count() > 0) {
				modified = deleteRowsFromData(rows);
			} else {
				unsigned int i;
				int row, col;
				wxGridCellCoordsArray cells = GetSelectedCells();
				for (i = 0; i < cells.Count(); i++) {
					clearCellData(cells[i].GetRow(), cells[i].GetCol());
				}
				wxGridCellCoordsArray topLeft = GetSelectionBlockTopLeft();
				wxGridCellCoordsArray bottomRight = GetSelectionBlockBottomRight();
				for (i = 0; i < topLeft.Count(); i++) {
					for (row = topLeft[i].GetRow(); row <= bottomRight[i].GetRow(); row++) {
						for (col = topLeft[i].GetCol(); col <= bottomRight[i].GetCol(); col++) {
							clearCellData(row, col);
						}
					}
				}
				wxArrayInt cols = GetSelectedCols();
				for (i = 0; i < cols.Count(); i++) {
					for (row = 1; row < GetNumberRows(); row++) {
						clearCellData(row, cols[i]);
					}
				}
			}
			ClearSelection();
		} else {
			clearCellData(GetGridCursorRow(), GetGridCursorCol());
		}
	}
	wxGrid::OnKeyDown(event);
	if (modified) updateData(true);
}
Exemplo n.º 2
0
wex::grid::grid(const window_data& data)
  : wxGrid(
    data.parent(), 
    data.id(), 
    data.pos(), 
    data.size(), 
    data.style(), 
    data.name())
{
  SetDropTarget(new text_droptarget(this));
  m_use_drag_and_drop = true;

  Bind(wxEVT_MENU, [=](wxCommandEvent& event) {
    empty_selection();}, wxID_DELETE);
    
  Bind(wxEVT_MENU, [=](wxCommandEvent& event) {
    SelectAll();}, wxID_SELECTALL);
    
  Bind(wxEVT_MENU, [=](wxCommandEvent& event) {
    ClearSelection();}, ID_EDIT_SELECT_NONE);
    
  Bind(wxEVT_MENU, [=](wxCommandEvent& event) {
    copy_selected_cells_to_clipboard();}, wxID_COPY);
    
  Bind(wxEVT_MENU, [=](wxCommandEvent& event) {
    copy_selected_cells_to_clipboard();
    empty_selection();}, wxID_CUT);
    
  Bind(wxEVT_MENU, [=](wxCommandEvent& event) {
    paste_cells_from_clipboard();}, wxID_PASTE);

  Bind(wxEVT_FIND, [=](wxFindDialogEvent& event) {
    find_next(
      find_replace_data::get()->get_find_string(), 
      find_replace_data::get()->search_down());});
      
  Bind(wxEVT_FIND_NEXT, [=](wxFindDialogEvent& event) {
    find_next(
      find_replace_data::get()->get_find_string(), 
      find_replace_data::get()->search_down());});

  Bind(wxEVT_GRID_CELL_LEFT_CLICK, [=](wxGridEvent& event) {
    // Removed extra check for !IsEditable(),
    // drag/drop is different from editing, so allow that.
    if (!IsSelection())
    {
      event.Skip();
      return;
    }

    if (m_use_drag_and_drop)
    {
      // This is because drag/drop is not really supported by the wxGrid.
      // Even the wxEVT_GRID_CELL_BEGIN_DRAG does not seem to come in.
      // Therefore, we are really dragging if you click again in
      // your selection and move mouse and drop elsewhere.
      // So, if not clicked in the selection, do nothing, this was no drag.
      if (!IsInSelection(event.GetRow(), event.GetCol()))
      {
        event.Skip();
        return;
      }

      // Is it allowed to drag current selection??
      if (!is_allowed_drag_selection())
      {
        event.Skip();
        return;
      }

      // Start drag operation.
      wxTextDataObject textData(get_selected_cells_value());
      wxDropSource source(textData, this);
      wxDragResult result = source.DoDragDrop(wxDrag_DefaultMove);

      if (result != wxDragError &&
          result != wxDragNone &&
          result != wxDragCancel)
      {
        // The old contents is not deleted, as should be by moving.
        // To fix this, do not call Skip so selection remains active,
        // and call empty_selection.
        //  event.Skip();
        empty_selection();
        ClearSelection();
      }
      else
      {
        // Do not call Skip so selection remains active.
        // event.Skip();
      }
    }
    else
    {
      event.Skip();
    }
    });
  
  Bind(wxEVT_GRID_CELL_RIGHT_CLICK, [=](wxGridEvent& event) {
    int style = (IsEditable() ? wex::menu::DEFAULT: wex::menu::IS_READ_ONLY);
    if (IsSelection()) style |= wex::menu::IS_SELECTED;

    wex::menu menu(style);
    build_popup_menu(menu);
    PopupMenu(&menu);
    });
    
  Bind(wxEVT_GRID_SELECT_CELL, [=](wxGridEvent& event) {
    frame::statustext(
      std::to_string(1 + event.GetCol()) + "," + std::to_string(1 + event.GetRow()),
      "PaneInfo");
    event.Skip();});

  Bind(wxEVT_GRID_RANGE_SELECT, [=](wxGridRangeSelectEvent& event) {
    event.Skip();
    frame::statustext(std::to_string(GetSelectedCells().GetCount()),
      "PaneInfo");
    });
  
  Bind(wxEVT_SET_FOCUS, [=](wxFocusEvent& event) {
    wex::frame* frame = dynamic_cast<wex::frame*>(wxTheApp->GetTopWindow());
    if (frame != nullptr)
    {
      frame->set_find_focus(this);
    }
    event.Skip();});
}