Sizers API again...

Another problem with the API: selecting individual list items. Sizers
need to behave more like lists. Consider deleting an individual sizer
item from a sizer with an event handler:

. . . sizer . . . . . . . . . . . . . . . .
+----------------------------------------+.

      This is a panel with a textctrl |.
<DEL> and a delete button |.
      Enter stuff: [_foo____________] |.

+----------------------------------------+.
+----------------------------------------+.

      (more panels) |.
<DEL> |.
      Enter stuff: [________________] |.

+----------------------------------------+.
. . . . . . . . . . . . . . . . . . . . . .

Suppose each panel represents an item in a list L such that the item is
L[i]. You want to update attributes of L[i] based on events.

  def OnTextEnter(self, evt):
    ctl=evt.GetEventObject()
    # find the index of the control in the sizer
    sizeritems=self.sizer.GetChildren()
    pos=-1
    for item in sizeritems:
      pos=pos+1
      if item.GetWindow()==ctl:
        break

    answers[pos]=ctl.GetValue()

If the GetChildren/GetWindow API looks unfamiliar to you, that's because
it's undocumented. I discovered it by looking at dir(sizer) and then
dir(sizeritem). (wxSizerItems themselves are also undocumented.) I
shudder to think how hard this would be without the sizeritem API, but
even armed with that knowledge, it's way too hard.

By comparison here's what I tried first. This is obviously incorrect but
I think you can see how I got there mentally:

  def OnTextEnter(self, evt):
    ctl=evt.GetEventObject()
    # find the index of the control in the sizer
    pos=self.sizer.GetPosition(ctl)
    answers[pos]=ctl.GetValue()

In short, it's a pain in the ass to find out where an item is in the
sizer! Eventually I'm going to want to be able to change the order of the
panels in the sizer; I try not to think too hard about how I'm going to do
that :-).

If we're discussing a new Sizers API, how about adding more list-like
behavior to it so it's trivial to find out where an item is in the sizer.

···

__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.

Cory Dodt wrote:

Another problem with the API: selecting individual list items. Sizers
need to behave more like lists. Consider deleting an individual sizer
item from a sizer with an event handler:

Why are you putting an event handler on a sizer? Sizers should only handle positioning. (The reason that the sizeritem interface isn't documented is because it's not intended for external use. It's an implementation detail that applications generally shouldn't care about.)

In short, it's a pain in the ass to find out where an item is in the
sizer! Eventually I'm going to want to be able to change the order of the
panels in the sizer; I try not to think too hard about how I'm going to do
that :-).

Here's where you'll really run into problems doing things your way. But if you use sizers as they're intended, then there's no such problem. I'd recommend maintaining a separate list of subpanels in your parent window, and attaching your event handlers there (if not simply putting the event handlers on the subpanels themselves). Then it won't matter what order panels are in the sizer, and you can even move them dynamically without needing to worry about event handlers. Let the sizer do just sizing/positioning, and your life will be much simpler.

Jeff Shannon
Technician/Programmer
Credit International