Wrapping C++ Code

Is there a way to use C++ code manipulating wx objects from wxPython?

I wrote a function in C++ that does an interpolated scale of a wxImage.
The visual improvement over the built-in scaling function is dramatic
enough that it's a requirement. However, I'm moving my work to python,
and the python translation of that function is a magnitude or two
slower.

I presume that since I'm dealing with a wxImage, it will not suffice to
simply follow the normal docs on extending python. However, wxImage is
the only wx class I use, and the rest of the code involves looping over
C arrays. Is there a way to do this? I'd appreciate any pushes in the
right direction.

Thanks!

···

--
Brian

Hi, Brian. I have actually done about the same thing and here's how I
made it accessibly from python: (it's kind of ugly)

-get the wxPython source
-put your c++ code under
wxPythonSrc-<version>/wxPython/contrib/<your-module-name>
-download and build wxSWIG using the instructions at
http://www.wxpython.org/BUILD.unix.txt
-create a swig wrapper for your class using the other contribs as an
example (mostly just a copy of your .h file with some extras)
-run wxSWIG on your .i file to create the module c++ code and some
python code (make sure to use the right flags with wxSWIG: -c++ -shadow
-python -keyword -dnone -I./src -D__WXGTK__ or -D__WXMSW__ etc)
-modify setup.py to build your extension using the code from the other
extensions as an example (search for BUILD_DLLWIDGET)

assuming you manage all that, you should be able to compile and install
wxPython and run "from wxPython import <yourmodulename>"

it looks like a big mess (actually it is a big mess) but once it
"clicks" it is not that hard. I'm certainly happy to help where I can
and this list is also quite helpful.

Out of personal curiosity, are you able to release your code? I am not
exactly a skilled coder so my solution to the image scaling problem was
to use the gdk-pixbuf library but I am always looking for something
simpler/faster/better so I would love to check it out. If not, I
understand.

Good luck,

Mark Roach

···

On Tue, 2003-06-10 at 18:17, Brian Victor wrote:

Is there a way to use C++ code manipulating wx objects from wxPython?

I wrote a function in C++ that does an interpolated scale of a wxImage.
The visual improvement over the built-in scaling function is dramatic
enough that it's a requirement. However, I'm moving my work to python,
and the python translation of that function is a magnitude or two
slower.

Roach, Mark R. wrote:

···

On Tue, 2003-06-10 at 18:17, Brian Victor wrote:

Is there a way to use C++ code manipulating wx objects from wxPython?

I wrote a function in C++ that does an interpolated scale of a wxImage.
The visual improvement over the built-in scaling function is dramatic
enough that it's a requirement. However, I'm moving my work to python,
and the python translation of that function is a magnitude or two
slower.

Hi, Brian. I have actually done about the same thing and here's how I
made it accessibly from python: (it's kind of ugly)

-get the wxPython source
-put your c++ code under
wxPythonSrc-<version>/wxPython/contrib/<your-module-name>
-download and build wxSWIG using the instructions at
http://www.wxpython.org/BUILD.unix.txt
-create a swig wrapper for your class using the other contribs as an
example (mostly just a copy of your .h file with some extras)
-run wxSWIG on your .i file to create the module c++ code and some
python code (make sure to use the right flags with wxSWIG: -c++ -shadow
-python -keyword -dnone -I./src -D__WXGTK__ or -D__WXMSW__ etc)
-modify setup.py to build your extension using the code from the other
extensions as an example (search for BUILD_DLLWIDGET)

assuming you manage all that, you should be able to compile and install
wxPython and run "from wxPython import <yourmodulename>"

it looks like a big mess (actually it is a big mess) but once it
"clicks" it is not that hard. I'm certainly happy to help where I can
and this list is also quite helpful.

Documenting this and any other tips you have figured out from hard experience in the wiki would be nice. Of course I know all about this stuff but after having been doing every day for many years it's hard to document stuff that I don't have to think about anymore, it's all automatic now...

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

Hi, Brian. I have actually done about the same thing and here's how I
made it accessibly from python: (it's kind of ugly)

Thanks for the reply, Mark. I got at least partway there, but I'm
winding up with a linker error. The only interesting thing I put into
the .i file is:

wxImage BWAARescale(wxImage &in, int x, int y);

I copied the DLL part of setup.py and changed these lines:
    location = 'contrib/bwaa'
    swig_files = ['bwaa.i']

    copy_file(opj(location, 'bwaa.py'), PKGDIR, update=1, verbose=0)

    ext = Extension('bwaa.cpp', [
                                '%s/bwaa.cpp' % location,
                             ] + swig_sources,

I compiled wxswig from CVS (2.4 branch), and ran it on the .i file to
produce a very spartan bwaa.py file.

I'm not sure what this line was supposed to do, but whatever it is, it
didn't do it :wink:

-python -keyword -dnone -I./src -D__WXGTK__ or -D__WXMSW__ etc)

python -keyword -dnone -I. -D__WXGTK__
Unknown option: -k

In a perfect world, I wouldn't need to relink wxPython to add a function
like this. I'd rather have a separate module that I could link to
wxPython. But I'll take it one step at a time.

As far as sharing my code, I certainly can. I should warn you that it's
not terribly optimized, and it's designed to only scale 1-bit images.
And, obviously, it's in C++. Let me know if you still want it.

···

On Tue, Jun 10, 2003 at 08:28:57PM -0400, Roach, Mark R. wrote:

--
Brian

Brian Victor wrote:

I compiled wxswig from CVS (2.4 branch), and ran it on the .i file to
produce a very spartan bwaa.py file.

I'm not sure what this line was supposed to do, but whatever it is, it
didn't do it :wink:

-python -keyword -dnone -I./src -D__WXGTK__ or -D__WXMSW__ etc)

python -keyword -dnone -I. -D__WXGTK__
Unknown option: -k

Mark's directions had split this command line in two. The whole thing is something like this, all on one line:

./wxSWIG/wxswig -c++ -shadow -python -keyword -dnone -I./src -D__WXGTK__ -Isrc -c -o src/gtk/windows.cpp src/windows.i

But if you are modifying setup.py to include your own Extension then you should be able to do it automatically with:

python setup.py USE_SWIG=1 build

In a perfect world, I wouldn't need to relink wxPython to add a function
like this. I'd rather have a separate module that I could link to
wxPython. But I'll take it one step at a time.

I'm thinking over a few ideas on how to allow this...

···

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

Sure thing, I know it took me a while to figure it all out, I still
haven't quite figured out how to keep my code from having to go in a msw
or gtk directory :slight_smile:

-Mark

···

On Wed, 2003-06-11 at 14:23, Robin Dunn wrote:

Documenting this and any other tips you have figured out from hard
experience in the wiki would be nice. Of course I know all about this
stuff but after having been doing every day for many years it's hard to
document stuff that I don't have to think about anymore, it's all
automatic now...

Ahh, didn't notice that, whoops :wink:

-Mark

···

On Wed, 2003-06-11 at 21:33, Robin Dunn wrote:

Brian Victor wrote:
>
> I compiled wxswig from CVS (2.4 branch), and ran it on the .i file to
> produce a very spartan bwaa.py file.
>
> I'm not sure what this line was supposed to do, but whatever it is, it
> didn't do it :wink:
>
>
>>-python -keyword -dnone -I./src -D__WXGTK__ or -D__WXMSW__ etc)
>
>
> python -keyword -dnone -I. -D__WXGTK__
> Unknown option: -k

Mark's directions had split this command line in two. The whole thing
is something like this, all on one line:

./wxSWIG/wxswig -c++ -shadow -python -keyword -dnone -I./src -D__WXGTK__
-Isrc -c -o src/gtk/windows.cpp src/windows.i

Roach, Mark R. wrote:

···

On Wed, 2003-06-11 at 14:23, Robin Dunn wrote:

Documenting this and any other tips you have figured out from hard experience in the wiki would be nice. Of course I know all about this stuff but after having been doing every day for many years it's hard to document stuff that I don't have to think about anymore, it's all automatic now...

Sure thing, I know it took me a while to figure it all out, I still
haven't quite figured out how to keep my code from having to go in a msw
or gtk directory :slight_smile:

Use "" instead of GENDIR in the call to run_swig.

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