Ruby UI

From Schmid.wiki
Jump to: navigation, search

Recently, the tk GUI toolkit described in the Ruby book isn't included in the win32 Ruby installation. To install it, you need to install a huge Tcl/tk package (ActiveTcl). All the more reason to use wxWidgets!

Contents

Installation

gem install wxruby

References

Message Box Example

require 'wx'
include Wx

$TEXT_INSTALL = <<EOF
This is the Quickschmearch Firefox installation program.
Do you want to install Quickschmearch in Firefox?
EOF

class InstallDialog < App
    def on_init
        Frame.new(nil, -1, "Quickschmearch Installation")
        dialog = MessageDialog.new(nil,
                                   $TEXT_INSTALL,
                                   "Quickschmearch Installation",
                                   OK|CANCEL|ICON_QUESTION)

        if dialog.show_modal() != ID_OK then exit end
        ...
        exit
    end
end

InstallDialog.new.main_loop

Create Links Example

This example uses SysInternals junction to create directory links for MAME. Note that puts is replaced by a message box in the scope of MyApp.

require 'wx'
include Wx

class MyApp < App
    def puts(message, style = ICON_INFORMATION)
        d = MessageDialog.new(nil, message, "Message box", style|OK) 
        d.show_modal()
    end

    def create_links(mame_path)
        ["cfg", "ini", "snap", "nvram"].each do |path|
            name = "#{mame_path}\\#{path}"
            raise("'#{name}' already exists!") if FileTest::exists?(name)
            unless system("junction \"#{name}\" #{path}")
                raise("Junction failed. Is junction.exe in your executable path?")
            end
        end
    end

    def on_init()
        dir_dialog = DirDialog.new(nil, "Select MAME directory", "C:\\")

        case dir_dialog.show_modal()
        when ID_OK
            begin create_links(dir_dialog.get_path)
            rescue RuntimeError => e
                puts("Error creating links: #{e}", ICON_ERROR)
                exit
            end
            puts("MAME links junctioned successfully.")
            exit
        when ID_CANCEL
            exit
        end
    end
end

MyApp.new.main_loop()

Personal tools