glcanvas and flexgridsizer

gao_bolin@voila.fr wrote:

Hi,

I have a problem using glcanvas embedded in a flexgridsizer. When the window is resized, the first glcanvas would not shrink. Here is the code:

When replacing glcanvas with a button, everything works fine. Any idea what is going on? I am using activepython 2.3.2 with wxpython 2.5.3.1.

There are several factors that sizers use to determine how to size the windows that they control. One of which is the window's "best size" which it reports by implementing a DoGetBestSize method. Since the GLCanvas doesn't implement that method it falls back to the default implementation in wxWindow, which is implemented like this pseudo code:

  if the window has a sizer
    return what the sizer thinks is the minimal size
  if the window uses layout contstraints
    return the constraints min size
  if the window has children
    return a size large enough to show all children
    at their current positions and sizes
  if the window has a set minimum size
    return the min size
  otherwise
    return the current size

Since none of the conditions apply to the GLCanvas then it is always telling the sizer that its current size is the best size. So when the sizer stretches it a bit because the layout changed then the new size becomes it's best size. And so on. When the frame shrinks then its sizer will ask the canvas again what it's best size is and it tells it the current size and so the sizer decides that it can't shrink it because it is already optimal. However if you look at the pseudo code above you can see that there is a very easy workaround, just tell the canvas windows that they have a min size:

import wx
print wx.VERSION

import wx.glcanvas
from OpenGL.GL import *

myApp = wx.App(0)
frame = wx.Frame(None, -1)
sizer = wx.FlexGridSizer(2,2,1,1)

c1 = wx.glcanvas.GLCanvas(frame, -1)
c2 = wx.glcanvas.GLCanvas(frame, -1)
c3 = wx.glcanvas.GLCanvas(frame, -1)
c4 = wx.glcanvas.GLCanvas(frame, -1)

c1.SetMinSize((200,100))
c2.SetMinSize((200,100))
c3.SetMinSize((200,100))
c4.SetMinSize((200,100))

sizer.Add(c1, 1, wx.EXPAND)
sizer.Add(c2, 1, wx.EXPAND)
sizer.Add(c3, 1, wx.EXPAND)
sizer.Add(c4, 1, wx.EXPAND)

sizer.AddGrowableCol(0)
sizer.AddGrowableCol(1)
sizer.AddGrowableRow(0)
sizer.AddGrowableRow(1)
frame.SetSizer(sizer)
frame.Show(True)
myApp.MainLoop()

ยทยทยท

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!