Forum: wxRuby Help with relative address

755ce658078632d051cc60c811613066?d=identicon&s=25 Ann Maybury (Guest)
on 2011-04-30 19:00
(Received via mailing list)
I am using the wiki tutorial to try to learn wxruby.   I have programmed
the
windows  interface many times.  The first two samples ran fine, but now
I am
on the icon frame and need a little help.  I am aware that I could
replace
the offending path with a hard coded one, but do not want to do that.

I have 50 years programming experience but no experience with ruby or
other
modern scripting languages.

The code found near the bottom of

 http://wxruby.rubyforge.org/wiki/wiki.pl?Frames_%28Part_1%29

is copied below.


My current OS is
Windows XP
service pack 3
version 2002
AMD processor
2.50 gig of ram.

When I run the RubyMine debugger I get the following error.
Uncaught exception: Icon file does not exist: ./icons/wxwin.ico

If I search my machine I find the file at

C:\Ruby192\lib\ruby\gems\1.9.1\gems\wxruby-ruby19-2.0.1-x86-mingw32\samples\bigdemo\icons


How do I change the line highlighted in red  below so that ruby will
find
this file?

---------------------------------Code
--------------------------------------
require "rubygems"
require "wx"

ID_ICON = 1000



class IconFrame < Wx::Frame
    def initialize
        super(nil, -1, "Changing Icons!", Wx::DEFAULT_POSITION,
Wx::Size.new(225,150))
        panel = Wx::Panel.new(self, -1)
        iconNames = ["wxwin", "mondrian", "ogl", "smiley"]
        icons = Wx::RadioBox.new(panel, ID_ICON, "&Icons",
Wx::Point.new(20,5),
                                 Wx::DEFAULT_SIZE, iconNames, 1,
Wx::RA_SPECIFY_COLS)
        evt_radiobox(ID_ICON) {|event| on_change_icon(event)}
        if Wx::PLATFORM == "WXMSW"
            Wx::Icon.new("./icons/wxwin.ico", Wx::BITMAP_TYPE_ICO)
        else
            Wx::Icon.new("./icons/wxwin16x16.xpm", Wx::BITMAP_TYPE_XPM)
        end


        show(true) #true is the default value, so it may be left off
    end

    def on_change_icon(event)
        if Wx::RUBY_PLATFORM == "WXMSW"
            case event.get_int   #(get_int is deprecated)
                when 0
                    set_icon(Wx::Icon.new("./icons/wxwin.ico"))
                when 1
                    set_icon(Wx::Icon.new("./icons/mondrian.ico"))
                when 2
                    set_icon(Wx::Icon.new("./icons/ogl.ico"))
                when 3
                    set_icon(Wx::Icon.new("./icons/smiley.ico"))
            end
        else
            case event.get_int
                when 0
                    set_icon(Wx::Icon.new("./icons/wxwin16x16.xpm"))
                when 1
                    set_icon(Wx::Icon.new("./icons/mondrian.xpm"))
                when 2
                    set_icon(Wx::Icon.new("./icons/ogl.xpm"))
                when 3
                    set_icon(Wx::Icon.new("./icons/smiley.xpm"))
            end
        end
    end
end

class MinimalApp < Wx::App

    def on_init
        IconFrame.new
    end

end

MinimalApp.new.main_loop
----------------------end of
code------------------------------------------------




--
Ann Maybury
ann.maybury@gmail.com
06f6780c99d4a8dd71f2b474082ea9ce?d=identicon&s=25 Alex Fenton (Guest)
on 2011-05-01 12:12
(Received via mailing list)
Hi Ann


On 30/04/11 17:17, Ann Maybury wrote:
> I am using the wiki tutorial to try to learn wxruby.   I have
> programmed the windows  interface many times.  The first two samples
> ran fine, but now I am on the icon frame and need a little help.  I am
> aware that I could replace the offending path with a hard coded one,
> but do not want to do that.

It's looking for the icon file relative to the current Ruby working
directory (same as it would if you used standard File.open from Ruby).
You mentioned that you're running in RedMine and it looks like it's not
setting the working directory to the one in which the script is saved.

There are a couple of ways round this:

1) Use Dir.chdir at the start of your script to switch Ruby's working
directory.

Dir.chdir(File.dirname(__FILE__))

But obviously this affects other things

> How do I change the line highlighted in red  below so that ruby will
> find this file?
>

...

> Wx::Icon.new("./icons/wxwin.ico", Wx::BITMAP_TYPE_ICO)


2) If you only want to change the one line, you could look up the
location relative to the script, something like

Wx::Icon.new( File.join( File.dirname(__FILE__), 'icons', 'wxwin.ico') )


In general, File.dirname(__FILE__) is a bit cumbersome, but proposals to
introduce __DIR__ into Ruby core don't seem to have been concluded:

http://redmine.ruby-lang.org/issues/3346

cheers
alex
This topic is locked and can not be replied to.