How to set wxGrid cell size to the smallest?

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
Eman
Experienced Solver
Experienced Solver
Posts: 68
Joined: Sat Aug 31, 2019 2:11 pm

How to set wxGrid cell size to the smallest?

Post by Eman »

Hi
I have 40X40 wxGrid, and I want to display it without scrolling:

Code: Select all

                wxGrid *gridTable; // table
                width =40;
                height =40;
		gridTable = new wxGrid(panel,-1,wxPoint( 0, 0 ), wxSize( 850, 850 ));
		gridTable->SetRowMinimalAcceptableHeight(1);
		gridTable->SetColMinimalAcceptableWidth(1);
		gridTable->SetDefaultRowSize(1,true);
		gridTable->SetDefaultColSize(1,true);
		gridTable->SetRowLabelSize(0);
		gridTable->SetColLabelSize(0);
		gridTable->CreateGrid(height, width );
But, this gives me scrolling in the vertical side
The image in the attachment.
Attachments
eee.png
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to set wxGrid cell size to the smallest?

Post by doublemax »

I don't get any scrollbars with this code (added to the MyFrame constructor in the "minimal" sample):

Code: Select all

wxPanel *panel = new wxPanel(this, wxID_ANY);

wxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
wxGrid *grid = new wxGrid(panel,  wxID_ANY);
grid->SetDefaultColSize(16);
grid->SetDefaultRowSize(16);
grid->HideColLabels();
grid->HideRowLabels();
grid->CreateGrid(40, 40);

mainSizer->Add(grid, 1, wxEXPAND);
panel->SetSizer(mainSizer);

mainSizer->Fit(this);
Use the source, Luke!
Eman
Experienced Solver
Experienced Solver
Posts: 68
Joined: Sat Aug 31, 2019 2:11 pm

Re: How to set wxGrid cell size to the smallest?

Post by Eman »

Many thanks....
These three lines fix the problem:

Code: Select all

 wxSize size = gridTable->GetBestSize();
 SetMinClientSize(size);
 Fit();
fixed.png
fixed.png (22.99 KiB) Viewed 2159 times
However;
The title of the window "Benchmark Maps Visulaizer" not shown.
Also, the legend and controls in the right side disappears when adding these 3 lines
My frame size is wxSize(900,660) and my wxGrid size is wxSize( 660, 660 )
GUI.png
GUI.png (42.46 KiB) Viewed 2159 times
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to set wxGrid cell size to the smallest?

Post by doublemax »

Yes, that code only works if there is only the grid in the frame. Try the edited code. (Replace these 3 lines with mainSizer->Fit(this);
Use the source, Luke!
Eman
Experienced Solver
Experienced Solver
Posts: 68
Joined: Sat Aug 31, 2019 2:11 pm

Re: How to set wxGrid cell size to the smallest?

Post by Eman »

it does not work. It might be because my mainSizer contains nested sizers and one of them is the sizer that contains wxGrid
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to set wxGrid cell size to the smallest?

Post by doublemax »

Can you post the whole code?
Use the source, Luke!
Eman
Experienced Solver
Experienced Solver
Posts: 68
Joined: Sat Aug 31, 2019 2:11 pm

Re: How to set wxGrid cell size to the smallest?

Post by Eman »

main.cpp

Code: Select all

#include <wx/wx.h>
#include "GUI.h"


// application class
class BenchmarkMapsApp : public wxApp
{
public:
	// function called at the application initialization
	virtual bool OnInit();
};

IMPLEMENT_APP(BenchmarkMapsApp);

bool BenchmarkMapsApp::OnInit()
{
	int height = 40;
	int width = 40;
	GUI *gui = new GUI(height,width,wxT("Benchmark Maps Visulaizer"));
    gui->Show(true);
	
    return true;
}


GUI.h

Code: Select all

#include <wx/wx.h>
#include <vector>
#include <random>
#include <wx/grid.h>
#include <wx/bitmap.h>
#include "wx/rawbmp.h"
using namespace std;
typedef wxNativePixelData PixelData;

#ifndef GUI_H
#define GUI_H

class GUI : public wxFrame
{
private:
	wxPanel *panel; // the main panel
	// horizontal box for (1)map and information and (2)controls and results
	wxBoxSizer *mapInfoAndControlsResultsHbox; 
	
	wxGrid *gridTable; // table
	
	wxBoxSizer *vboxMap; // vertical box where we Add gridBitmap or gridTable
	wxBoxSizer *vboxMapAndInformation;
	wxBoxSizer *vboxControlsAndResults;
	
	// simulator parameters
	int height; // rows
	int width; // columns
	int start;
	int goal;
	
	wxArrayString complexityIndexStrings; 
	wxChoice *complexityIndexList;
	
	wxWindowID choosingComplexityIndexLabelId=900;
	wxWindowID complexityIndexListId = 1000; 
	
	wxWindowID enteringFileNameLabelId = 1830;
	wxWindowID fileNameInputTextCtrlId = 1835;
	wxTextCtrl *fileNameInputTextCtrl;

	wxWindowID readFileNameButtonId=9100; // read file name button
	wxWindowID resetButtonId=9500; // reset button
	
	
public:
	GUI(int h, int w, const wxString& title);
	~GUI();
	vector<int> get_integers_from( const string& );
	void OnButtonClicked(wxCommandEvent& event);
	void OnChoice(wxCommandEvent& event);
	void createMap();
	void showSimulatorParameters();
};
#endif // GUI_H
 

GUI.cpp

Code: Select all

#include "GUI.h"
#include<iostream>
#include <fstream>
#include <sstream>
#include <string>
GUI::GUI(int h, int w,const wxString& title)
        : wxFrame(NULL, -1, title, wxPoint(-1, -1), wxSize(900,660))
{
  panel = new wxPanel(this, -1);
  vboxMap = new wxBoxSizer(wxVERTICAL);
  vboxMapAndInformation = new wxBoxSizer(wxVERTICAL);
  vboxControlsAndResults = new wxBoxSizer(wxVERTICAL);

  mapInfoAndControlsResultsHbox = new wxBoxSizer(wxHORIZONTAL);
  wxBoxSizer *vboxControls = new wxBoxSizer(wxVERTICAL);
  wxBoxSizer *hboxForEnviro = new wxBoxSizer(wxHORIZONTAL);
  wxBoxSizer *hboxForFileName = new wxBoxSizer(wxHORIZONTAL);
  

  height = h; // height is rows
  width = w; // width is columns
  
  start = 0;
  goal = 0;
  
 
  createMap();

  
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

  wxStaticText *choosingComplexityIndexLabel = new wxStaticText(panel,choosingComplexityIndexLabelId, "complexity index (CI) ");

  complexityIndexStrings.Add(wxT("---"));
  complexityIndexStrings.Add(wxT("Low"));
  complexityIndexStrings.Add(wxT("Medium"));
  complexityIndexStrings.Add(wxT("High"));
 
  complexityIndexList = new wxChoice(panel,complexityIndexListId,wxDefaultPosition,wxDefaultSize,complexityIndexStrings);
  complexityIndexList->Connect(complexityIndexListId,wxEVT_COMMAND_CHOICE_SELECTED,wxCommandEventHandler(GUI::OnChoice),NULL,this);
  complexityIndexList->SetSelection(0); // default selected ---
  
  wxStaticText *enterFileNameLabel = new wxStaticText(panel,enteringFileNameLabelId, "Enter file name: ");
  fileNameInputTextCtrl = new wxTextCtrl(panel,fileNameInputTextCtrlId,"",wxDefaultPosition, wxSize(100,25));
  
  wxButton *readFileNameButton = new wxButton(panel,readFileNameButtonId, wxT("Read File"));
  readFileNameButton->Connect(readFileNameButtonId, wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(GUI::OnButtonClicked),NULL,this);

 
  wxButton *resetButton = new wxButton(panel,resetButtonId, wxT("Reset"));
  resetButton->Connect(resetButtonId, wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(GUI::OnButtonClicked),NULL,this);
  
  wxBoxSizer *vboxMapInfo = new wxBoxSizer(wxVERTICAL);
  
  wxStaticText *mapInfoRedLabel = new wxStaticText(panel,100,"     ");
  mapInfoRedLabel->SetBackgroundColour(*wxRED);
  wxStaticText *mapInfoStartLabel = new wxStaticText(panel,100," Start");
  wxBoxSizer *hboxStartLegend = new wxBoxSizer(wxHORIZONTAL);
  hboxStartLegend->Add(mapInfoRedLabel);
  hboxStartLegend->Add(mapInfoStartLabel);
  
  wxStaticText *mapInfoGreenLabel = new wxStaticText(panel,100,"     ");
  mapInfoGreenLabel->SetBackgroundColour(*wxGREEN);
  wxStaticText *mapInfoGoalLabel = new wxStaticText(panel,100," Goal");
  wxBoxSizer *hboxGoalLegend = new wxBoxSizer(wxHORIZONTAL);
  hboxGoalLegend->Add(mapInfoGreenLabel);
  hboxGoalLegend->Add(mapInfoGoalLabel);
  
  wxStaticText *mapInfoCyanLabel = new wxStaticText(panel,100,"  ");
  mapInfoCyanLabel->SetBackgroundColour(*wxCYAN);
  wxStaticText *mapInfoPathLabel = new wxStaticText(panel,100," Path ");

  wxBoxSizer *hboxAlgorithmsLegend = new wxBoxSizer(wxHORIZONTAL);
  hboxAlgorithmsLegend->Add(mapInfoCyanLabel);
  hboxAlgorithmsLegend->Add(mapInfoPathLabel);
  
  
  vboxMapInfo->Add(hboxStartLegend);
  vboxMapInfo->Add(hboxGoalLegend);
  vboxMapInfo->Add(hboxAlgorithmsLegend);
  
  showSimulatorParameters();
  
  //Sizer for Environemnt size label and list
  hboxForEnviro->Add(choosingComplexityIndexLabel,0,wxALIGN_CENTER);
  hboxForEnviro->Add(complexityIndexList);
  
  hboxForFileName->Add(enterFileNameLabel);
  hboxForFileName->Add(fileNameInputTextCtrl);

  
  vboxControls->Add(hboxForEnviro);
  vboxControls->Add(hboxForFileName);
  vboxControls->Add(readFileNameButton);
  vboxControls->Add(resetButton);
  vboxControlsAndResults->Add(vboxControls);
  vboxControlsAndResults->Add(vboxMapInfo);
  
  vboxMapAndInformation->Add(vboxMap,1,wxALL,10);//10 is the space around box
  //vboxMapAndInformation->Add(vboxMapInfo,1,wxALL,10);//10 is the space around box
  
  mapInfoAndControlsResultsHbox->Add(vboxMapAndInformation);
  mapInfoAndControlsResultsHbox->Add(vboxControlsAndResults,0,wxALL,10);//10 is the space around box
  
  panel->SetBackgroundColour("#40E0D0");
  panel->SetSizer(mapInfoAndControlsResultsHbox);
  
  Centre();
		
}

void GUI::createMap()
{	
		gridTable = new wxGrid(panel,-1,wxPoint( 0, 0 ), wxSize( 660, 660 ));
		gridTable->SetRowMinimalAcceptableHeight(1);
		gridTable->SetColMinimalAcceptableWidth(1);
		gridTable->SetDefaultRowSize(16);
		gridTable->SetDefaultColSize(16);
		gridTable->SetRowLabelSize(0);
		gridTable->SetColLabelSize(0);
		gridTable->CreateGrid(height, width );
		
		//gridTable->AutoSize();
		
		// re-put the new gridTable to the sizer hierarchy
		vboxMap->Add(gridTable,0,wxRESERVE_SPACE_EVEN_IF_HIDDEN);
}
void GUI::showSimulatorParameters()
{

}
GUI::~GUI() 
{
}

// handles clicking  buttons
void GUI::OnButtonClicked(wxCommandEvent& event)
{
}

void GUI::OnChoice(wxCommandEvent& event)
{
}

User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to set wxGrid cell size to the smallest?

Post by doublemax »

Don't set a mininum size for the Grid:

Code: Select all

gridTable = new wxGrid(panel, -1);
And add these lines at the end of the GUI constructor:

Code: Select all

  SetMinClientSize( mapInfoAndControlsResultsHbox->CalcMin() );
  mapInfoAndControlsResultsHbox->Fit(this);
Use the source, Luke!
Eman
Experienced Solver
Experienced Solver
Posts: 68
Joined: Sat Aug 31, 2019 2:11 pm

Re: How to set wxGrid cell size to the smallest?

Post by Eman »

Many thanks. It works.
Post Reply