Matrix Grid Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
computerquip
Experienced Solver
Experienced Solver
Posts: 72
Joined: Fri Feb 20, 2009 7:13 pm
Location: $(#wx)\src

Matrix Grid

Post by computerquip » Fri Feb 20, 2009 8:42 pm

Due to lack of experience, I'd thought I'd make a GUI version of my Matrix Grid. Simply decrypts and encrypts a word by a grid via x then y or y then x depending on user input. Similar to a kids assignment to line up the rows and columns to find the password of the paper.

I've run into a few problems of course. This is the constructor of the frame :

Code: Select all

    wxGrid* grid = new wxGrid(this, -1);
    wxBoxSizer* s_layout = new wxBoxSizer(wxHORIZONTAL);
  //{
        s_layout->Add(grid, 1, wxEXPAND, 0);

        wxBoxSizer* s_buttons = new wxBoxSizer(wxVERTICAL);
      //{
            s_buttons->Add(new wxButton(this, SET_Settings, _T("Apply")), 1, wxLEFT | wxRIGHT | wxBOTTOM, 30);

            s_buttons->Add(new wxStaticText(this, -1, _T("X-Axis:")), 0, wxALIGN_CENTER_HORIZONTAL, 0);
            wxTextCtrl* xAxis = new wxTextCtrl(this, -1); //Declared in private section of class in header. wxTextCtrl* xAxis
            s_buttons->Add(xAxis, 0, wxALIGN_CENTER_HORIZONTAL, 0);

            s_buttons->Add(new wxStaticText(this, -1, _T("Y-Axis:")), 0, wxALIGN_CENTER_HORIZONTAL|wxTOP, 30);
            wxTextCtrl* yAxis = new wxTextCtrl(this, -1); //Declared in private section of class in header wxTextCtrl* yAxis
            s_buttons->Add(yAxis, 0, wxALIGN_CENTER_HORIZONTAL, 0);
      //}
        s_layout->Add(s_buttons, 0, wxALIGN_CENTER_HORIZONTAL, 0);
  //}
    SetSizer(s_layout);
This creates a button with an "Apply" label and the id of SET_Settings which is assigned to the function MatrixGridFrame::OnApply.

Here's the OnApply function:

Code: Select all

grid->CreateGrid( wxAtoi(xAxis->GetValue()), wxAtoi(yAxis->GetValue()) );
POINT: When I press the apply button, the letters for the grid and rows are messed up and glitches. It also doesn't work twice which I think I can fix by clearing the grid and remaking it or inserting or deleting cells. But is this a glitch caused by the grid or me?

JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway » Fri Feb 20, 2009 9:37 pm

Hi,

wxGrid::CreateGrid is really meant to be called only once.
It looks like you want the grid to re-dimension itself every time the user clicks your button.

To do that, call CreateGrid with 0 rows and columns (I think that works) right after calling the constructor.
Then in your button handler:

Code: Select all

grid->BeginBatch();  // suspend screen updates...
grid->DeleteRows(0, grid->GetNumberRows());
grid->DeleteCols(0, grid->GetNumberCols());
grid->AppendRows(wxAtoi(xAxis->GetValue())); // as per your post, but I think it could be yAxis for rows...
grid->AppendCols(wxAtoi(yAxis->GetValue()));
grid->EndBatch();  // update the display

Hope that helps,

Jim
OS: Vista SP1, wxWidgets 2.8.7.

computerquip
Experienced Solver
Experienced Solver
Posts: 72
Joined: Fri Feb 20, 2009 7:13 pm
Location: $(#wx)\src

Post by computerquip » Sat Feb 21, 2009 5:09 am

Thanks! I think I found a better way..

Code: Select all

    grid = new wxGrid(this, -1);
    grid->CreateGrid( 0,0 );
    wxBoxSizer* s_under = new wxBoxSizer(wxVERTICAL);
  //{
        wxBoxSizer* s_layout = new wxBoxSizer(wxHORIZONTAL);
      //{
            s_layout->Add(grid, 1, wxEXPAND, 0);
            wxBoxSizer* s_buttons = new wxBoxSizer(wxVERTICAL);
          //{
                s_buttons->Add(new wxButton(this, SET_Settings, _T("Apply")), 1, wxLEFT | wxRIGHT | wxBOTTOM, 30);

                s_buttons->Add(new wxStaticText(this, -1, _T("X-Axis:")), 0, wxALIGN_CENTER_HORIZONTAL, 0);
                xAxis = new wxTextCtrl(this, -1);
                s_buttons->Add(xAxis, 0, wxALIGN_CENTER_HORIZONTAL, 0);

                s_buttons->Add(new wxStaticText(this, -1, _T("Y-Axis:")), 0, wxALIGN_CENTER_HORIZONTAL|wxTOP, 30);
                yAxis = new wxTextCtrl(this, -1);
                s_buttons->Add(yAxis, 0, wxALIGN_CENTER_HORIZONTAL, 0);

                s_buttons->Add(new wxButton(this, GET_Output, _T("Copy Output")), 1, wxLEFT | wxRIGHT | wxTOP, 30);
          //}
            s_layout->Add(s_buttons, 0, wxALIGN_CENTER_HORIZONTAL, 0);
      //}
        s_under->Add(s_layout, 1, wxEXPAND, 0);
        //Create a new listbox to output and add the output of the decryption / encryption.
        answerBox = new wxListCtrl(this, -1, wxDefaultPosition, wxDefaultSize, wxLC_LIST);
        answerBox->InsertItem(0, _T("My name is bobz"));
        s_under->Add(answerBox, 0, wxGROW | wxALIGN_BOTTOM, 0);
  //}
    SetSizer(s_under);
}
(I hope I'm using the sizer correctly.) The above declares the grid and makes it a (0,0) graph.

Code: Select all

FIX: Get more effecient.
    int nRows = ( wxAtoi( yAxis->GetValue() ) - grid->GetNumberRows() );
    if (nRows >= 0)
    {
        grid->InsertRows(grid->GetViewHeight(), nRows);
    }
    else
    {
        grid->DeleteRows(grid->GetViewHeight(), abs(nRows) );
    }

    int nCols = ( wxAtoi( xAxis->GetValue() ) - grid->GetNumberCols() );
    if (nCols >= 0)
    {
        grid->InsertCols(grid->GetViewWidth(), nCols);
    }
    else
    {
        grid->DeleteCols(grid->GetViewWidth(), abs(nCols) );
    }
EDIT: LOL, I just now looked up BeginBatch and EndBatch and now I understand what you were trying to do. >.> Sorry for not paying attention.

Post Reply