Thanks Roy. Fixing the methods in TextCtrl was one of the top reasons I
wanted to get the typemaps.i working. Nice also to have an example of
how to discard a return value. Another method that should now be easy to
port is TextCtrl#HitTest, but I can''t try it b/c it''s only
implemented
on MSW & GTK.
I have a few OS X bugs with these typemaps now:
* the first return value of position_to_xy is always false, even if a
valid position is given. The correct x & y values are returned - so this
below, adapted from your textctrl.rb report() method works correctly on OS X
result, x, y = position_to_xy(get_insertion_point)
if x.zero? and y.zero?
report << "<invalid location>\n"
else
report << x.to_s << ", " << y.to_s <<
"\n"
end
Perhaps it would be better to dump the boolean return value of
position_to_xy altogether, return a two-element array, and just return
Ruby nil if an invalid position is passed?
* the return value of get_last_position is always an opaque SWIG type
SWIG::TYPE_p_wxTextPos:0x3a807c
It should be seen and converted as a long. I tried including
wx/textctrl.h, which includes a typedef for wxTextPos. I tried adding a
typemap
%typemap(directorout) wxTextPos {
$1 = LONG2NUM((long)$1);
}
but neither seemed to help
cheers
alex
Roy Sutton wrote:> Attached are patches to make the textctrl.rb sample work fully. Note
> the new %directorargout typemap I added to fix wxWindows calling into
> SWIG. I don''t know if this can fix all such problems or not. Let
me
> know what you think.
>
> I also rubified the sample a little bit more. Also, note that I fix
> the << operator so the function can be called. I hope I did this
right.
>
> Roy
> ------------------------------------------------------------------------
>
>
> C:\RubyDev>diff -b -u wxruby2_old/samples/text/textctrl.rb
wxruby2/samples/text/textctrl.rb
> --- wxruby2_old/samples/text/textctrl.rb 2005-11-28 19:07:04.000000000
-0500
> +++ wxruby2/samples/text/textctrl.rb 2006-04-26 23:46:39.539263000 -0400
> @@ -12,21 +12,22 @@
> super(parent, -1, text, Wx::DEFAULT_POSITION, Wx::DEFAULT_SIZE, STYLE)
> end
>
> - # more ruby-ish
> - alias :<< :append_text
> -
> # run through a few useful methods of textctrl and report the results
> # as a string
> - def report()
> + def report
> report = ''''
> - report << ''Insertion Point: '' <<
get_insertion_point.to_s() << "\n"
> + report << ''Insertion Point: '' <<
get_insertion_point.to_s << "\n"
> report << ''First Line Text: '' <<
get_line_text(0) << "\n"
> - report << ''Final Position: '' <<
get_last_position().to_s() << "\n"
> - report << ''String Selection: '' <<
get_string_selection().inspect << "\n"
> - # not available
> - report << ''Selection: '' << "Not
Implemented\n"
> - # available, but method signature is wrong (3 for 1) - needs SWIG
hints
> - report << ''Position to XY: '' <<
"Not Implemented\n"
> + report << ''Final Position: '' <<
get_last_position.to_s << "\n"
> + report << ''String Selection: '' <<
get_string_selection.inspect << "\n"
> + report << ''Selection: '' <<
get_selection.join('' to '') << "\n"
> + report << ''Position to XY: ''
> + result, x, y = position_to_xy(get_insertion_point);
> + if result
> + report << x.to_s << ", " << y.to_s
<< "\n"
> + else
> + report << "<invalid location>\n"
> + end
> return report
> end
> end
> @@ -45,7 +46,7 @@
> sizer = Wx::BoxSizer.new(Wx::VERTICAL)
>
> @textctrl = InformativeTextCtrl.new(self, '''')
> - populate_textctrl()
> + populate_textctrl
> sizer.add(@textctrl, 2, Wx::GROW|Wx::ALL, 2)
>
> button = Wx::Button.new(self, -1, ''Get Info'')
> @@ -58,7 +59,7 @@
> self.set_sizer( sizer )
> end
>
> - def populate_textctrl()
> + def populate_textctrl
> @textctrl << "This is some plain text\n"
> @textctrl << "Text with green letters and yellow
background\n"
> @textctrl << "This is some more plain text"
> @@ -68,31 +69,31 @@
> @textctrl.set_style(24, 69, attr)
> end
>
> - def construct_menus()
> - menu_bar = Wx::MenuBar.new()
> + def construct_menus
> + menu_bar = Wx::MenuBar.new
>
> - menu_file = Wx::Menu.new()
> + menu_file = Wx::Menu.new
> menu_file.append(Wx::ID_EXIT, "E&xit\tAlt-X", "Quit
this program")
> menu_bar.append(menu_file, "&File")
> - evt_menu(Wx::ID_EXIT) { on_quit() }
> + evt_menu(Wx::ID_EXIT) { on_quit }
>
> - menu_help = Wx::Menu.new()
> + menu_help = Wx::Menu.new
> menu_help.append(Wx::ID_ABOUT, "&About...\tF1",
"Show about dialog")
> - evt_menu(Wx::ID_ABOUT) { on_about() }
> + evt_menu(Wx::ID_ABOUT) { on_about }
> menu_bar.append(menu_help, "&Help")
>
> set_menu_bar(menu_bar)
> end
>
> def on_click(e)
> - @log.set_value( @textctrl.report() )
> + @log.set_value( @textctrl.report )
> end
>
> - def on_quit()
> + def on_quit
> close(TRUE)
> end
>
> - def on_about()
> + def on_about
> msg = sprintf("This is the About dialog of the textctrl
sample.\n" \
> "Welcome to %s", Wx::VERSION_STRING)
> message_box(msg, "About Minimal",
Wx::OK|Wx::ICON_INFORMATION, self)
> @@ -100,7 +101,7 @@
> end
>
> class RbApp < Wx::App
> - def on_init()
> + def on_init
> frame = TextCtrlFrame.new("TextCtrl demonstration",
> Wx::Point.new(50, 50),
> Wx::Size.new(450, 340) )
> @@ -109,4 +110,4 @@
> end
> end
>
> -RbApp.new().main_loop()
> +RbApp.new.main_loop
>
> ------------------------------------------------------------------------
>
>
> C:\RubyDev>diff -b -u wxruby2_old/swig/classes/TextCtrl.i
wxruby2/swig/classes/TextCtrl.i
> --- wxruby2_old/swig/classes/TextCtrl.i 2005-08-20 19:47:08.000000000 -0400
> +++ wxruby2/swig/classes/TextCtrl.i 2006-04-27 00:29:05.710477400 -0400
> @@ -26,10 +26,20 @@
>
> %}
>
> -%init %{
> - extern VALUE mWxTextCtrl;
> - rb_define_method(mWxTextCtrl, "<<",
VALUEFUNC(op_append), 1);
> -%}
> +%apply long *OUTPUT { long* from , long* to };
> +%apply long *OUTPUT { long* x , long* y };
> +
> +%typemap(directorargout) ( long* from, long* to ) {
> + if((TYPE(result) == T_ARRAY) && (RARRAY(result)->len >=
2))
> + {
> + *$1 = NUM2LONG(rb_ary_entry(result,0));
> + *$2 = NUM2LONG(rb_ary_entry(result,1));
> + }
> + else
> + {
> + *$1 = 0; *$2 = 0;
> + }
> +}
>
> %import "include/wxObject.h"
> %import "include/wxEvtHandler.h"
> @@ -37,3 +47,7 @@
> %import "include/wxControl.h"
>
> %include "include/wxTextCtrl.h"
> +
> +%init %{
> + rb_define_method(cWxTextCtrl.klass, "<<",
VALUEFUNC(op_append), 1);
> +%}
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> wxruby-users mailing list
> wxruby-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/wxruby-users