Still Porting Code...

Hello NG,

    In porting the code from C++ to Python, I'm facing a difficulty. It
is not easy to explain, so pardon me if I am a little bit confusing.

Basically, I have a class called FoldWindowItem. In C++, the __init__ function
of this class accepts a generic wxWindow OR a separator (a simple wxWindow
with black/custom colour). In C++, considering the generic wxWindow case,
you can call this class in this way:

wxFoldWindowItem(wxWindow *wnd, int flags = wxFPB_ALIGN_WIDTH, int ySpacing
= wxFPB_DEFAULT_YSPACING,
                     int leftSpacing = wxFPB_DEFAULT_LEFTSPACING, int rightSpacing
= wxFPB_DEFAULT_RIGHTSPACING)
        : _wnd(wnd)
        , _type(WINDOW)
        , _flags(flags)
        , _leftSpacing(leftSpacing)
        , _rightSpacing(rightSpacing)
        , _ySpacing(ySpacing)
        , _lineWidth(0)
        , _lineY(0)
    {

The generic wxWindow can be whatever you want (a button, a sizer, a listctrl,
and so on). The problem is that, in Python, I have to initialize it in some
way (I don't know if I can initialize it in a generic way). At the moment
I do the following:

class FoldWindowItem(wx.Window):

In this way, however, I can only initialize the class as a wxWindow, and
not as a button, listctrl, and so on.
To be clearer (I hope), the C++ class acts like the sizer behavior (in my
opinion). You can add whatever you want to a sizer (and this "whatever"
is a generic wxWindow, transformed in C++ to the object you want, like a
button, a listctrl and so on). In Python, I don't know how to handle this
situation.

Could anyone please enlight me? Sorry, probably is a stupid question, but
I was not able to find any solution...

Thank you a lot.

Andrea.

andrea_gavana@tin.it writes:

> Hello NG,
>
> The generic wxWindow can be whatever you want (a button, a sizer, a listctrl,
> and so on). The problem is that, in Python, I have to initialize it in some
> way (I don't know if I can initialize it in a generic way). At the moment
> I do the following:
>
> class FoldWindowItem(wx.Window):
>
This is not initialization, this is the class definition, saying that
FoldWindowItem should inherit from wx.Window (which, by the way,
the c++ version doesn't do).

What you could do is check the type at runtime:

class FoldWindowItem:
    def __init__(self, window_or_spacer, ...):
       if isinstance(window_of_spacer,wx.Window):
           do something interesting
       else:
           do something else

or something to that effect.

···

--
  Pierre Hjälm

This is not initialization, this is the class definition, saying that
FoldWindowItem should inherit from wx.Window (which, by the way,
the c++ version doesn't do).

How do you know that? It seemed to me that the C++ version inherit from
a wxWindow:

class wxFoldWindowItem
{
private:
    wxWindow *_wnd;
    int _type, _flags;
    int _leftSpacing,
        _rightSpacing,
        _ySpacing;
    int _lineWidth, _lineY;
    wxColour _sepLineColour;

public:
    enum
    {
        WINDOW = 0,
        SEPARATOR
    };

    // wxWindow constructor. This initialises the class as a wxWindow type
    wxFoldWindowItem(wxWindow *wnd, int flags = wxFPB_ALIGN_WIDTH, int ySpacing
= wxFPB_DEFAULT_YSPACING,
                     int leftSpacing = wxFPB_DEFAULT_LEFTSPACING, int rightSpacing
= wxFPB_DEFAULT_RIGHTSPACING)
        : _wnd(wnd)
        , _type(WINDOW)
        , _flags(flags)
        , _leftSpacing(leftSpacing)
        , _rightSpacing(rightSpacing)
        , _ySpacing(ySpacing)
        , _lineWidth(0)
        , _lineY(0)
    {
    };

But, knowing that I am not a C++ guru, I am surely wrong. Following your
suggestion:

class FoldWindowItem:
   def __init__(self, window_or_spacer, ...):
      if isinstance(window_of_spacer,wx.Window):
          do something interesting
      else:
          do something else

First Way:

class FoldWindowItem:

    def __init__(self, parent, window=None, **kw):
            wx.Window.__init__(self, parent, wx.ID_ANY)

TypeError: unbound method __init__() must be called with Window instance
as first argument (got FoldWindowItem instance instead)

Second Way:

class FoldWindowItem:

    def __init__(self, parent, window=None, **kw):
        # NO wxWindow or other thinsg here...

AttributeError: FoldWindowItem instance has no attribute 'GetSize'

I am not trying to distinguish between a wxWindow and a spacer. This is
the first step. The problem is that you can pass whatever you want to this
class (wxButton, wxListCtrl and so on). In C++ (it seems to me) this situation
is treated using always a wxWindow... but at this moment I am more confused
than before.

Thanks to you all for your suggestions.

Andrea.

andrea_gavana@tin.it writes:

> >This is not initialization, this is the class definition, saying that
> >FoldWindowItem should inherit from wx.Window (which, by the way,
> >the c++ version doesn't do).
>
> How do you know that? It seemed to me that the C++ version inherit from
> a wxWindow:
>
In c++, inheritance looks like

class sublass::superclass

if my memory doesn't fail me (no, I'm certainly no c++ expert).

> class wxFoldWindowItem
> {
> private:
> wxWindow *_wnd;
> int _type, _flags;
> int _leftSpacing,
> _rightSpacing,
> _ySpacing;
> int _lineWidth, _lineY;
> wxColour _sepLineColour;
>
> public:
> enum
> {
> WINDOW = 0,
> SEPARATOR
> };
>
> // wxWindow constructor. This initialises the class as a wxWindow type
> wxFoldWindowItem(wxWindow *wnd, int flags = wxFPB_ALIGN_WIDTH, int ySpacing
> = wxFPB_DEFAULT_YSPACING,
> int leftSpacing = wxFPB_DEFAULT_LEFTSPACING, int rightSpacing
> = wxFPB_DEFAULT_RIGHTSPACING)
> : _wnd(wnd)
> , _type(WINDOW)
> , _flags(flags)
> , _leftSpacing(leftSpacing)
> , _rightSpacing(rightSpacing)
> , _ySpacing(ySpacing)
> , _lineWidth(0)
> , _lineY(0)
> {
> };
>
What that says is that it uses different constructors of *wxFoldWindowItem*
if passed different arguments. Not that is inherits differently.

>
> But, knowing that I am not a C++ guru, I am surely wrong. Following your
> suggestion:
>
> >class FoldWindowItem:
> > def __init__(self, window_or_spacer, ...):
> > if isinstance(window_of_spacer,wx.Window):
> > do something interesting
> > else:
> > do something else
> >
>
> First Way:
>
> class FoldWindowItem:
>
> def __init__(self, parent, window=None, **kw):
> wx.Window.__init__(self, parent, wx.ID_ANY)
>
> TypeError: unbound method __init__() must be called with Window instance
> as first argument (got FoldWindowItem instance instead)
>
> Second Way:
>
> class FoldWindowItem:
>
> def __init__(self, parent, window=None, **kw):
> # NO wxWindow or other thinsg here...
>
> AttributeError: FoldWindowItem instance has no attribute 'GetSize'
>
> I am not trying to distinguish between a wxWindow and a spacer. This is
> the first step. The problem is that you can pass whatever you want to this
> class (wxButton, wxListCtrl and so on). In C++ (it seems to me) this situation
> is treated using always a wxWindow... but at this moment I am more confused
> than before.
>
Yes, everything above is a subclass of wx.Window.

Since wxFoldWindowItem is _not_ subclassed from wx.Window (as I said
above), you shouldn't try to call wx.Window.__init__.

Ok, here follows a try:

WINDOW, SEPARATOR = 0, 1
class FoldWindowItem:
    def __init__(self, parent, win_or_sep, flags_or_color, ySpacing, leftSpacing, rightSpacing):
        if isinstance(win_or_sep, wx.Window):
            self._wnd = win_or_sep
            self._flags = flags_or_color
            self._type = WINDOW
            self._lineY = 0
        else:
            self._wnd = None
            self._flags = wx.FPB_ALIGN_WIDTH
            self._sepLineColour = flags_or_color
            self._type = SEPARATOR
            self._lineY = y
        self._leftSpacing = leftSpacing
        self._rightSpacing = rightSpacing
        self._ySpacing = ySpacing

    # and then all the other methods...

Where does GetSize come in?

I'm not sure this is the best solution, but it should work (although
it's completely untested :wink:

···

--
  Pierre Hjälm

Hello Pierre & NG,

In c++, inheritance looks like

class sublass::superclass
if my memory doesn't fail me (no, I'm certainly no c++ expert).

Sorry for my ignorance in C++... I'm becoming a little bit crazy in order
to convert this code... The possibilities are:

1) The C++ is not very well written;
2) My ability to convert C++ code into Python is very limited (highly probable)
3) The Python code does not behave like the C++ code (which means that my
translation is correct, but in wxPython I have to treat some situations
in a different way).

What that says is that it uses different constructors of *wxFoldWindowItem*
if passed different arguments. Not that is inherits differently.

Yes, everything above is a subclass of wx.Window.

Since wxFoldWindowItem is _not_ subclassed from wx.Window (as I said
above), you shouldn't try to call wx.Window.__init__.

Ok, here follows a try:

WINDOW, SEPARATOR = 0, 1
class FoldWindowItem:
   def __init__(self, parent, win_or_sep, flags_or_color, ySpacing, leftSpacing,
rightSpacing):
       if isinstance(win_or_sep, wx.Window):
           self._wnd = win_or_sep
           self._flags = flags_or_color
           self._type = WINDOW
           self._lineY = 0
       else:
           self._wnd = None
           self._flags = wx.FPB_ALIGN_WIDTH
           self._sepLineColour = flags_or_color
           self._type = SEPARATOR
           self._lineY = y
       self._leftSpacing = leftSpacing
       self._rightSpacing = rightSpacing
       self._ySpacing = ySpacing

   # and then all the other methods...

Ok, I got your point. Thanks for this!

Where does GetSize come in?

This function is not a method of the class FoldWindoItem; is only a property
of self._wnd (that can be a wxButton, a wxWindow, a wxListCtrl and so on).

Knowing that I am not an expert, does anyone know what this syntax means
in C++:

#if 0
    if(foldrect.GetHeight() < 23)
        foldrect.SetHeight(0);
    else
        foldrect.SetHeight(foldrect.GetHeight() - 22);
#endif

I don't know what the # if 0 ... # endif means... why there is a # before?!?!?!??

Thank you a lot for every suggestions.

Andrea.

#if 0
    if(foldrect.GetHeight() < 23)
        foldrect.SetHeight(0);
    else
        foldrect.SetHeight(foldrect.GetHeight() - 22);
#endif

It is a macro preproccessor conditional directive and "0" here is a condition
(which never holds true). As a result you do not have to care about
this piece of code.

Jirka

To further clarify, it means the same thing as if it was Python code "if
0:...". Zero is false, the statements in the block can never be run. In the
case of a compiled language like C++, using a preprocessor directive can
shrink the output code size although most modern C++ compilers would do it
automatically anyways.

Matt

···

On Tue, 22 Mar 2005 14:47:33 +0100, Jirka Mikulasek wrote:

#if 0
if(foldrect.GetHeight() < 23)
foldrect.SetHeight(0);
else
foldrect.SetHeight(foldrect.GetHeight() - 22);
#endif

It is a macro preproccessor conditional directive and "0" here is a
condition (which never holds true). As a result you do not have to
care about this piece of code.

Jirka

If MY memory serves me correctly, C++ inheritance looks like this...

class subclass : (public|private|protected) superclass1 {, superclass 2,
superclass3} { /* declaration goes here */ }

I USED to be a C++ expert but it has been years since I touched it (and I
don't miss it). I moved on to Java, Matlab, and now Python.

--Matt

···

On Tue, 22 Mar 2005 13:57:01 +0100, Pierre Hjälm wrote:

andrea_gavana@tin.it writes:

This is not initialization, this is the class definition,
saying that
FoldWindowItem should inherit from wx.Window (which, by the way,
the c++ version doesn't do).

How do you know that? It seemed to me that the C++ version
inherit from
a wxWindow:

In c++, inheritance looks like

class sublass::superclass

if my memory doesn't fail me (no, I'm certainly no c++ expert).

Matthew Zaleski <mzaleski@spamcop.net> writes:

···

On Tue, 22 Mar 2005 13:57:01 +0100, Pierre Hjälm wrote:

> > andrea_gavana@tin.it writes:
> >
> >>> This is not initialization, this is the class definition,
> >>> saying that
> >>> FoldWindowItem should inherit from wx.Window (which, by the way,
> >>> the c++ version doesn't do).
> >>>
> >> How do you know that? It seemed to me that the C++ version
> >> inherit from
> >> a wxWindow:
> >>
> > In c++, inheritance looks like
> >
> > class sublass::superclass
> >
> > if my memory doesn't fail me (no, I'm certainly no c++ expert).
> >
>
> If MY memory serves me correctly, C++ inheritance looks like this...
>
> class subclass : (public|private|protected) superclass1 {, superclass 2,
> superclass3} { /* declaration goes here */ }
>
> I USED to be a C++ expert but it has been years since I touched it (and I
> don't miss it). I moved on to Java, Matlab, and now Python.
>
I admit my shortcomings when it comes to c++, but I at least got it
right enough to prove my point :slight_smile:

On the other hand, I managed to convert OGL to python, so my c++
knowledge can't be all that bad, although I'm most certainly better
at reading it than writing it (not that I would want to).

--
  Pierre Hjälm