wxGrid is a nightmare... 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
user_bob
Earned a small fee
Earned a small fee
Posts: 21
Joined: Fri Jan 05, 2007 8:15 am

wxGrid is a nightmare...

Post by user_bob » Tue Jun 05, 2007 4:55 pm

I need something to display data like so:

[subject ]|[ project ]|[ status ]|[ icon ]
----------------------------------------------------------------------------------
string | combobox | checkmark pic | thumbnail icon
string | combobox | checkmark pic | thumbnail icon
string | combobox | checkmark pic | thumbnail icon
string | combobox | checkmark pic | thumbnail icon
string | combobox | checkmark pic | thumbnail icon
string | combobox | checkmark pic | thumbnail icon
string | combobox | checkmark pic | thumbnail icon
...

I need this control to be able to be sorted by subject, status, project, when you click on the headers. I understand this requires to use of a derived wxGridTableBase class to keep all the data for the wxGrid class. I having difficulty using wxGrid and finding little or no documentation or examples on how to accomplish this fairly common design. Any help would be appreciated. I don't think wxListCtrl is an option because it cannot display images nor can it be customized to handle comboboxes.

I guess the thing holding me up at this point, is that I can't figure out how to set an Image or Combobox value from my wxGridTableBase derived class to the wxGrid class. It seems like I need to override some function but there is no documentation for wxGridTableBase.

clyde729
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Mon May 29, 2006 10:50 pm
Location: Jena, Germany

Post by clyde729 » Tue Jun 05, 2007 9:14 pm

Please have a look at the samples of wxWidgets. The "grid" sample should give you an introduction on how to use wxGrid and wxGridTableBase. To your problem: You will have to set different editors ("wxGridCellEditor") and renderers ("wxGridCellRenderer") for each column. You can do this by using the "wxGridAttr" class.

There were some similar topics not long time ago. Please have a look at the threads here and here, to get an idea of the whole thing. Also search the forum for wxGrid. I think you will get some hits :wink:

Last, don't forget to look at the wiki page of wxGrid.
OS: Windows XP Home, Compiler: MingW, Version: wxWidgets 2.8.0, IDE: wx-Devcpp

user_bob
Earned a small fee
Earned a small fee
Posts: 21
Joined: Fri Jan 05, 2007 8:15 am

Post by user_bob » Wed Jun 06, 2007 10:50 pm

Clyde thanks for you help. I've made I little progress just looking at the wxGrid source. I have one more problem.

how come when I draw a gif image into a cell in a wxGrid, the background is grey? how do I fix this?

Code: Select all

// ------------------------- MyGridCellRenderer ---------------------------//

class MyGridCellRenderer : public wxGridCellStringRenderer
{
public:
   virtual void Draw(wxGrid& grid,
                     wxGridCellAttr& attr,
                     wxDC& dc,
                     const wxRect& rect,
                     int row, int col,
                     bool isSelected)
   {
      //wxGridCellStringRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);

      wxImage img;
      wxInitAllImageHandlers();

      if(img.LoadFile(wxT("pulmonary.gif"), wxBITMAP_TYPE_GIF))
      {
         dc.DrawBitmap(img, rect.x, rect.y, true);
      }
      else
         wxLogMessage(wxT("bitmap error"));
   }
};





Attachments
snapshot1.jpg

clyde729
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Mon May 29, 2006 10:50 pm
Location: Jena, Germany

Post by clyde729 » Wed Jun 06, 2007 11:33 pm

O.k., first: Call "wxInitAllImageHandlers()" in your App::OnInit on startup, not again and again while drawing. In addition, it's better to load the image only once and hold it as a member variable.

To your question. You will have to fill the background first, before drawing the image. So insert the lines:

Code: Select all

 
  dc.SetBrush(wxBrush(wxColour(*wxBLACK)));
  dc.DrawRectangle( rect.GetX(), rect.GetY(), rect.GetWidth(), rect.GetHeight());
just before loading and drawing the image. In this case the background should become black.
OS: Windows XP Home, Compiler: MingW, Version: wxWidgets 2.8.0, IDE: wx-Devcpp

Post Reply