[wxPython] Using wxPython type in another extension

wxPython users,

Let me start by thanking Robin Dunn for this terrific project! I'm a
long time lurker in wxWindows world, but never bothered actually using
it. Even though 90%+ of my work is in C++, I hate writing GUIs in C++
given most GUIs use bizarre designs (but mostly because I hate writing
GUIs ;-). Robin has certainly chipped away a large chunk of my now
crumbling self resolve to stay far away from writing GUIs.

I'm currently using Python to prototype some new x-ray optics modeling
tools, and wxPython looks like my way out of Tkinter. I also love the
fact that I can simply translate all my existing VTK** scripts from
Tcl to Python.

** Visualization Toolkit http://www.kitware.com/vtk.html

The question that I have is the following -- is there an established
mechanism for writing an C++ extension that depends on and uses types,
eg., wxWindow or wxDC, from wxPython extension?

Note that these extensions are not necessarily SWIG'd, and in fact,
probably will never be.

If I were to move to Python/wxPython in the future, I'll have to deal
with the issue of plotting/charting/etc, and there is nothing even
remotely satisfactory out there for my needs. The ones that are usually
talked about all have significant issues, but I'm hoping that'll change
over time. BLT provides some of the features, but back to Tkinter for
that. I have lots of code using PLPlot, and it already has Python
bindings, and so I wrote up a simple driver, based on code by Frank
Huebner, and now it works very well with wxWindows/C++. The driver
needs the address of the DC it needs to write into, and the problem
I'm running into is how to pass the address to the extension module.
I also have an wxWindows driver for Gist (the graphics package of
Yorick, and also has python bindings), and have similar problems there.

The current hack I'm using is disgusting -- I'm basically extracting the
string representation of the pointer value and passing that to the driver
which re-creates the pointer and casts it to the correct type. Something
like the following:

    def plplot_set_wxdc(pl, dc):
      import re
      pat = re.compile(r'[^_]*_([0-9a-z]*)_.*')
      dc_address = pat.sub('0x\\1', dc.this)
      # Now pass the DC address via undocumented hack.
      pl.plsfnam(dc_address)

or, perhaps:

    import pl
    # Other things.
    pl.plP_esc(pl.PLESC_SET_WXDC, dc)

Help, guidance, etc more than welcome.

Regards,
Mumit

···

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/mailman/listinfo/wxpython-users

[...]
The question that I have is the following -- is there an established
mechanism for writing an C++ extension that depends on and uses types,
eg., wxWindow or wxDC, from wxPython extension?

Note that these extensions are not necessarily SWIG'd, and in fact,
probably will never be.

[...]
The driver
needs the address of the DC it needs to write into, and the problem
I'm running into is how to pass the address to the extension module.

I'm afraid that currently the only solution is to do as you have done,
extract the address out of the obj.this attribute.

We might be able to come up with some mechanism that would be little safer.
Perhaps adding a GetPtr method to the wxPython classes that returns a
CObject to be passed to your extension module methods. This way you won't
be wide open to just any string giving you bogus pointers, you can verify
that the type of object you recieve is a CObject, and then you can pull a
void* out of it to be used however you need. It's still not real safe, but
better...

Another possibility is to use CObject to export some C functions from the
wxPython extension module to others, (which is what CObject was originally
created for.) Namely a pointer to the SWIG_GetPtrObj function could be
exported and you could use it to convert a PyObject* to a pointer to the C++
object. It also uses the SWIG type registry to verify that it is of the
expected type, or a subclass.

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com
http://wxPython.org Java give you jitters?
http://wxPROs.com Relax with wxPython!

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/mailman/listinfo/wxpython-users

Robin,

Thanks for the quick response, and confirming my suspicions in this
regard.

I'll continue my current hack for now, and then I'll revisit the
possible solutions you mention (adding a GetPtr to get a CObject, or
possibility of exporting C linkage functions); the safety issue in my
hack is actually a non-issue since my internal casting code checks
the string rep of dc.this for the correct typename.

Regards,
Mumit

···

On Thu, 16 Nov 2000, Robin Dunn wrote:

I'm afraid that currently the only solution is to do as you have done,
extract the address out of the obj.this attribute.

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/mailman/listinfo/wxpython-users