0

Summary: I do not know how to set the main window dimensions so that no scroll bars are used around the wxGrid without changing the default widths of the columns.

I want to write a simple application where user can see a limited number of numbers, and she can make correction of some of them and save the new content. The application should be a kind of minimalistic. The number of rows and cells is small enough to fit in a window without the need for scroll bars. Here is a simplified code:

wxGrid *grid = new wxGrid(this, wxID_ANY, wxDefaultPosition, wxDefaultSize);
grid->CreateGrid(21, 12);

// Setting the column labels and filling the grid with some numbers.

grid->Fit();

There will be some button below the grid. For now, the only sizer contains the only controll -- the grid:

wxBoxSizer *topSizer = new wxBoxSizer(wxVERTICAL);
topSizer->Add(grid, 1, wxEXPAND);
SetSizer(topSizer);

The code is placed into the constructor of my AppFrame class. Finally, I call the following:

topSizer->Fit(this);
topSizer->SetSizeHints(this);

It does almost exactly what I need, but some columns have longer labels and the Fit changes the widths of the columns. It is not nice visually. What should I call instead of Fit to get the same effect but without changing the uniform column widths?

Update: Actually, I did not noticed earlier, that also grid->Fit(); was called in my code (added now to the above example). After removing it, the grid does not set internally its dimensions and the window is extremely small (even the upper left corner window is not fully visible in height and ony a bit more that that on width. Then I have added also SetColSize for each column (to the same place where I set the format for the columns):

for (int i = 0; i < 12; ++i)
{
    grid->SetColSize(i, WXGRID_DEFAULT_COL_WIDTH);
    grid->SetColFormatFloat(i, -1, 3);
}

This did not help and the window is smaller than wanted. So, question should probably be reworded: How the filled grid can capture its own dimensions to be reported to the outer wrapper?

Update 2: It still does not work. The full code of the constructor. (I know, somewhere between the chair and the keyboard... :) I personally consider i a bit dirty code (at least for the magic numbers) -- possibly cleaned up later:

AppFrame::AppFrame() :
    wxFrame(NULL, wxID_ANY, "Month rates of Euro")
{
    InitDefaultRates();

    wxGrid *grid = new wxGrid(this, wxID_ANY, wxDefaultPosition, wxDefaultSize);

    // Fixed grid for 12 months (columns) and the years 2000 to 2020 (rows).
    //
    grid->CreateGrid(21, 12);

    grid->BeginBatch();

    // Set the column labes (in Czech -- to lazy to translate).
    //
    grid->SetColLabelValue(0,  "leden");
    grid->SetColLabelValue(1,  "únor");
    grid->SetColLabelValue(2,  "březen");
    grid->SetColLabelValue(3,  "duben");
    grid->SetColLabelValue(4,  "květen");
    grid->SetColLabelValue(5,  "červen");
    grid->SetColLabelValue(6,  "červenec");
    grid->SetColLabelValue(7,  "srpen");
    grid->SetColLabelValue(8,  "září");
    grid->SetColLabelValue(9,  "říjen");
    grid->SetColLabelValue(10, "listopad");
    grid->SetColLabelValue(11, "prosinec");

    // Float with 3 decimal places for each of the columns.
    //
    for (int i = 0; i < 12; ++i)
    {
        grid->SetColFormatFloat(i, -1, 3);
    }

    // Filling with the default values calculated from 
    // http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.zip.
    // Index zero is for the year 2000.
    //
    for (int year = 0; year < 21; ++year)
    {
        ostringstream oss;
        oss << 2000 + year;
        grid->SetRowLabelValue(year, oss.str().c_str());

        for (int month = 0; month < 12; ++month)
        {
            double default_value = default_rates[year][month];
            double value = default_value;

            oss.str("");
            oss << setprecision(3) << fixed << value;

            // Problem with decimal point separator. Replace the locale
            // comma by dot.
            //
            wxString s(oss.str().c_str());
            string::size_type pos = s.find_first_of('.');
            if (pos != string::npos)
                s[pos] = ',';

            grid->SetCellValue(year, month, s);

            if (value == 0.0)
                grid->SetCellTextColour(year, month, *wxLIGHT_GREY);
            else if (value == default_value)
                grid->SetCellTextColour(year, month, *wxBLUE);
        }
    }

    // Setting the initial size of the grid.
    //
    grid->SetInitialSize(grid->GetBestSize());

    // Vertical size will be the top one.
    //
    wxBoxSizer *topSizer = new wxBoxSizer(wxVERTICAL);
    topSizer->Add(grid, 1, wxEXPAND);
    SetSizer(topSizer);

    // I do not understand this (could be a problem). From wxBook p. 196.
    //
    topSizer->Fit(this);
    topSizer->SetSizeHints(this);

    // Allow redraw.
    //
    grid->EndBatch();

    // Set cursor to the current month.
    //
    int year = wxDateTime::GetCurrentYear() - 2000;
    int month = wxDateTime::GetCurrentMonth();
    grid->GoToCell(year, month);
}

Thanks for your time and experience, Petr

pepr
  • 20,112
  • 15
  • 76
  • 139

1 Answers1

2

Here is a complete working program:

#include "wx/app.h"
#include "wx/frame.h"
#include "wx/grid.h"
#include "wx/datetime.h"

class MyFrame : public wxFrame
{
public:
    MyFrame()
        : wxFrame(NULL, wxID_ANY, "Chronological data")
    {
        wxGrid *grid = new wxGrid(this, wxID_ANY);
        grid->CreateGrid(21, 12);

        for ( wxDateTime::Month m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) )
        {
            grid->SetColLabelValue(m, wxDateTime::GetMonthName(m));
            for ( int y = 0; y < 21; y++ )
            grid->SetCellValue(y, m, wxString::Format("%.2f", (float)rand() / RAND_MAX));
        }

        grid->InvalidateBestSize();
        SetClientSize(grid->GetBestSize());
    }
};

class MyApp : public wxApp
{
public:
    virtual bool OnInit()
    {
        (new MyFrame)->Show();
        return true;
    }
};

wxIMPLEMENT_APP(MyApp);

Notice that for me "September" doesn't fit into the default column width, either call AutoSizeColumns() (but you don't like this) or just increase the column width uniformly.

VZ.
  • 21,740
  • 3
  • 39
  • 42
  • It still does not work. I am using the latest wxWidget (https://github.com/wxWidgets/wxWidgets.git from yesterday), and Visual Studio 2012. I have updated the question -- full code of the `AppFrame` constructor. – pepr Nov 09 '12 at 07:47
  • The `grid->GetBestSize()` (the `wxWindowBase::GetBestSize`) ended prematurely. The `m_windowSizer` is NULL and the `m_bestSizeCache`contains `{x=82 y=32}`. This is the result that was also returned. – pepr Nov 09 '12 at 08:21
  • But what is the logic behind? Should not be the *best size* updated when the number of rows and columns is set -- as a part of `CreateGrid`. Or should not it be at list invalidated at least once? Or when the best size was made valid (the 82x32)? What is the reason for making it valid? Should that be the separate question in SO? – pepr Nov 09 '12 at 22:23
  • `wxGrid` doesn't invalidate its best size when the values of its cells are set. It could be a viewed as a bug but OTOH there would be problems with doing it too, so I'm not sure if it would be a good idea. – VZ. Nov 11 '12 at 12:48