31.1.09

Ten Steps in Shoes: step 2, Stack & Flow

Woow, I've been getting quite a lot of feedback on my last post. Thanks for the messages, I really appreciate it! It gets me even more enthusiastic about blogging and Shoes, I hope in my turn I get some other people to be Shoes enthusiasts. If I do, or you are already, consider blogging about it. For me it helps a lot in keeping focussed and to explore a bit further. I always love to read small articles and blogs on Ruby and Shoes, so if you happen to have one let me know!

If you read my previous post, you might have noticed a lack of layout structure in one of the examples. Some of the text was right behind the other, or even overlapped, and it all just looked like a big unsorted pile. This might be fine for dirty clothes or dishes, but our app aint dirty! But no worries, you can create order in this chaos by making neat columns and rows, by using stacks and flows!

To help a bit in explaining, I'll introduce you to something else first. Meet background, background is quite a cool, colourful fellow. You can use background within stacks and flows. One famous flow is the Shoesapp itself. So try this:

Shoes.app do
background black
para "Look ma, a black background!", :size => 30, :align => "center", :stroke => crimson
end

Let's drift a bit farther from our main objective and try a gradient with an angle:

background "#FF0".."#0FF", :angle => 60

Or an image!

background "justsomelocal.jpg", :border => "FF0".."0FF", :strokewidth => 10

You can even use an online picture and a border like so

background "http://poignantguide.net/ruby/i/the.foxes-4c.png"

Sometimes, using online images seem to cause some problems. Don't know why exactly, let's move on for now.

The Stack
A stack is something that allows you to put one thing above the other, simple as that. Stacks can make your shoes look sexy!

Let's create a stack containing four para's:

Shoes.app do
stack do
para "heya"
para "we are stacked"
para "stacked you see?"
para "different lines"
end
end

Let's create put all the para's in their own stacks. One is blue, so he needs a friend:

Shoes.app do
stack do
background fuchsia
para "hi again"
end

stack do
background cornflowerblue
para "we are in stacks"
para "we are blue together"
end

stack do
background peru
para "each our own"
end

stack do
background gold
para "with our colours"
end
end

Remember some of the style options in the previous post? Well, you can use many of them exactly in the same way.

Shoes.app do
background black
stack :width => 100 do
background fuchsia
para "we are not wide"
end

stack :width => 200, :height => 150, :scroll => true do
background cornflowerblue
para "we are in stacks"
para "the stack is high"
para "content exeeds height"
para "bla"
para "blabla"
para "but we have a scrollbar"
end

stack :top => 250, :left => 150 do
background gold
para "some margin for errors"
end
end

The Flow
A flow is like a stack, but horizontal. But, unlike a stack, a flow has a limit. Why? Because there are no horizontal scrollbars, those suck anyway! So try to imagine your flow like a typewriter, going back and down when you have reached the end.

Now wait a minute, just think about it. Remember those para's from step 1? Remember how the para text was placed behind the others until it wrapped around? It's a flow! The shoes.app is a flow! So you already know the basic flow behaviour a bit. So lets just skip the talk and try a combination of stack and flow!

Shoes.app :width => 400, :height => 140 do
flow do
stack :width => '50%' do
background khaki
para "first stack"
para "stacks rock"
para "khakis are only cool in shoes"
end

stack :width => '40%' do
background peru
para "second stack"
end

para "i'm no stack but just in a flow, but did not fit", :stroke => blue
end
end

See, Shoes.app is a flow, so we can use the same style options for it, like setting the height! This example should demonstrate how stacks and flows differ from each other, but can work together. If only the people of the world were like stacks and flows!

27.1.09

10 Steps in Shoes, step 1: Para's

Phew, this has been one of those weeks in which you need to put everyone and everything on hold. But now is not a time to stand still, instead we are going to start the Shoes travel by taking the first step in Shoes. It's going to be a short walk though, just 10 steps to be exact. There are 10 basic concepts in Shoes and we will take a look at the basic possibilities of each one as we talk the talk and learn to walk the walk. We will keep it short, so you have no excuses not to walk with us, let's go!

Wear your Shoes
To start taking steps, you first need to have Shoes. So make sure you have Ruby and Shoes installed.

Tie your Shoes
Before we can start using anything in Shoes, we need to create a Shoes app.
Open a file in your favorite editor, mine being vi and call it step1.rb. Notice the .rb extension, that's for Ruby, not Shoes. But how does Ruby know we want to run a Shoes app? Well just write "Shoes.app do" in the first line of your Shoes code. See, Shoes is easy! How to end a Shoes app? Any guesses? Yes you are right, write "end"!

Here's what your Shoes app looks like:
Shoes.app do

end
Para's
Now that you know how to start coding a Shoes app, let's take the first step in Shoes! Any programming starts with a first program to salute the world, so you need to know how to write some text, or a paragraph if you have much to say. Lets write a short paragraph, in fact lets not say paragraph but say para instead.
Shoes.app do
para "Hello World!"
end
Save your file and open it with Shoes by running "shoes step1.rb" or just opening an app from the Shoes GUI. You will get something like this:

Your shoes application screen just gets filled with the characters you provided to para. Para works quite like writing on a piece of paper, you can write in diferent sizes, in different styles, on different locations and in different colors and when you have reached the end, you start again at the following line. Let's try it. Reopen your file in your editor and add the following line:
para "Hello World, are you there?", :size => 48
Save, run and take a look.
What we did here is use the size style option. Note the comma and the semicolon. Style options are key-value pairs of a hash, hence we use => to say that key size has value 48. This is just like the way we use hashes in Ruby. First I thought the biggest size of text in shoes is 48 and it could not grow beyond 48. This is not true however. You can have really huge letter sizes, I'm not sure if there is a maximum. There is some weird behaviour if you use really, really large values though.

Instead of using a para with a certain size value, you can use any of the following:

title (same as para with size of 34)
subtitle (same as para with size of 26)
tagline (same as para with size of 18)
caption (same as para with size of 14)
para (default size is 12)
inscription (same as para with size of 10)

Colour your shoes!
Even though the eighties have long gone and nobody knows what happend to King, we still want to colour our shoes! Don't click that link if you keep some youth sentiment about that moment you saw your first ever music clip on tv, you have been warned! Better humn that song and use the Shoes equivalent to spraycans to be cool like so:
Shoes.app do
para "Hello World!", :size => 48, :stroke => dodgerblue
subtitle "Why are you not answering?", :stroke => red
tagline "Hmm wait, what to say in case it does answer?", :fill => "#FF0000"
inscription "...yes quite some responsibility to be the one who talks to the world", :fill => darkkhaki
end
What do you think this app will look like? Save and run it! As you might have noticed you can use style options for colors too. Colors can be specified by their name or their hexadecimal Red-Green-Blue value. Here's a list for color names and values. We also used some alternatives to para's.

The Fonts is cool
Heeeyy, use cool Fonts and the girls will be kissing with a snap of the fingers! But that was in a time of innocence and the coolest technology around was a motorcycle. For people who have grown up and want more then just kissing, there is Shoes. The happy days are over, but Shoes has many Fonts.

Shoes.app do
para "heeeeyyyy":font => "Helvetica, Arial"
end

These Shoes are made for walking
I guess you get it by now. There is much more you can do with para and it's siblings. Almost everything is related to style.
You should be able to work it out for yourself, just look at the shoes help on styles. Not all styles work for everything, so note what it says after "For".
Here's a small example of various possibilities.

Shoes.app do
para "Centered text!", :size => 14, :align => "center", :stroke => firebrick
subtitle "justify", :justify => true, :stroke => hotpink, :fill => black
para "top and left", :top => 150, :left => 222, :stroke => mediumspringgreen
tagline "oblique", :emphasis => "oblique", :stroke => peru
inscription "displace", :displace_left => 20, :top => 180
para "more left less top", :top => 250, :left => 280, :stroke => olive
title "strikethrough and strikecolor", :strikethrough => "single", :strikecolor => crimson
caption "leading", :leading => 10
para "undercolor", :underline => "single", :undercolor => darkorchid, :top => 210
tagline "Variant Weight", :variant => "smallcaps", :weight => "heavy", :stroke => gold
end

A few last tips: use alt + ? for help and alt + . to reload your Shoes app in the Shoes GUI. Use alt + / for the Shoes console to see debug messages. Use alt + p to package your app.
I suggest you play with the various options for a bit, and get ready for Step 2!

18.1.09

GUI toolkit extravaganza wrap-up

Folks, I have decided to conclude the GUI toolkit extravaganza. There were two more toolkits to follow, monkeybars and qtruby. But alas, I am not going to check those out any time soon. I just don't feel very curious about these toolkits anymore and want to do some more exploring of Shoes. But I'll take those two toolkits along in the following overview.

The big question is off course, which one is the best? It all depends on what you need and what effort you are willing to invest (ooh how cliché!). When it comes to GUI toolkits for Ruby, there are three basic categories: system-dependant, system-independent and the category for those that won't fit elsewhere. Let's take a brief look at each category:

System-dependant: ruby-gnome2 and qtruby
These are toolkits specifically written to support a certain system, a specific window manager for example. These toolkits offer a big load of functions, sometimes not even restricted to graphics. For example, ruby-gnome2 has some audio functionality as well. The amount of available functionality and the fact that you need to understand the given window manager as well, makes these toolkits a bit complex and the learning curve steep. I would only consider to learn and use these if I really needed to use some system specific functionality that other toolkits don't provide. Otherwise I'd walk on and find something more simple which I could use in more situations.

If you need to do some qt stuff here's some info:
a book
korundrun
: bindings for KDE and qt api's
KDE techbase

System-independent: fxruby and wxruby
Usually these toolkits are a layer cake of wrappers and libraries. Both are supported by many systems and allow you to code an app that looks the same on different systems. There is not much difference between these two toolkits. Fx is older which is bad because it just looks a bit dated, but on the other hand there is a decent amount of documentation, including a book on fxruby. Wx is a bit younger and looks better. One other difference is that wx is truly native, so all widgets are in the same style your window manager is. Fx looks mostly the same on different platforms. Both toolkits provide all standard GUI functions.

If you are looking for a toolkit to make mainstream GUI's I'd go for one of these. Walk on if you want something different, something that's more suited for beginners or something more fun.
Link
The category for those that won't fit elsewhere: Monkeybars & Shoes
Monkeybars:
This toolkit is for rubyists who evolved from Javabut still want to code swing but this time using Ruby code. So if you learned Swing in a past life and don't want to learn a new GUI language again, I guess this is for you! Here's some background info:
project page
Linkscreencastic screencast

Shoes:
My personal choice and recommendation. If you don't have very specific wishes you should try shoes and see if it fits! Why? Because it's the easiest and most enjoyable. If you later find that you need something which Shoes can't do you can always choose to learn something else.
To learn Shoes you only need to take 10 steps, so stay tuned as we take 10 steps with Shoes in the following postings!

13.1.09

GUI toolkit extravaganza part IV: Shoes

Happy new year and all that. Yes I know, I haven't posted on my blog for ages. There's a perfectly good reason for that you know, see it was fricking freezing over here and my blog froze as well. Now the thaw has set in, time to get back at the GUI toolkit extravaganza. You do remember this GUI toolkit journey we started way back in 2008 right?...right? Well ok I guess I can't blame you, after all it has been a while and there probably were some parties...

Once upon a time, a curious ruby learner wondered what he had to do to get his ruby programs do some GUI work. The learner quickly found himself in a scary forest of GUI toolkits for Ruby. Not knowing which one to choose and frightened by the aspect of learning the wrong toolkit he decided to take a peek at each one and write down the main differences. After each of his travels he would share his journeys with GUI toolkit extravaganza episodes.

Originally the following toolkits were on the menu:

Ruby-Gnome2
FXRuby
WXRuby
Ruby Cocoa
Shoes
Monkeybars
QtRuby

Next up was to be Ruby Cocoa, but alas this is a Ruby GUI toolit for MacOS/X only and there's no apples here only bananas...

Noticed my somewhat crazy writing style with an flair of eccentricity and impressions of genius have you? Yes my sweet children, you are correct when you think this must have something to do with Shoes! You were in fact still in the scary GUI toolkit forest and now you have been kidnapped by some catbunny!

Shoes
So what's Shoes? It's crazy, it's fun it's genius! It's small and simple but powerfull. It's somewhat webbie and easy to learn. And it's ooh shiny! And most of all it's quite fun and hassle free.

ease of installation
You can get binaries for varius operating systems, source code is available and compiling is easy.

documentation
There's plenty of documentation on Shoes, and for a change it's great fun to read it! Here's what I have found so far:

Nobody Knows Shoes, the superb manual from _why, the author of Shoes in hardcopy, html or pdf.
Online shoes documentation (online as in included in the app)

The Shoes webpage includes many documentation and tutorials as well.


supported platforms
There are binaries for Linux, Mac and Windows and sources are available as well. Ubuntu has included a slightly older version in their repositories.

complexity
Shoes has been the easiest of all to learn and use. There are no complex constructs or difficult syntax issues.

functionality
Shoes offers a simple but effective toolkit. There are some things that are just not there, things like window tabs or OpenGL stuff for example. But Shoes is not a GUI toolkit in the classic sense.
Don't think too quickly you cannot do this or that because it lacks something. There's lots and lots you can do with Shoes you just need to think a bit about Human-Computer interaction and use your creativity. Yes, creativity is what Shoes is all about. If you don't understand what I'm talking about, just check Nobody Knows Shoes and the Shoe-box.

performance
No complaints here, these shoes are suited for running.

looks
Shoes apps look wonderfull. Take a look at the shoebox to get some impressions!

You can find Shoes at:
http://www.shoooes.net/

24.12.08

Christmas: relief, salvation and commercialism

Christmas eve has finally arrived! To some, Christmas brings emotions of relief or salvation. To others, it's just one of those days filled with obligatory social calls and commercialism.

The days that precede Christmas have been as hectic as they possibly can. One one side I had to make some preparations for the social necessities. And on the other side there was the commercial aspect, in Christmas times known as 'desperately trying to find something to give to someone while moving to endless black crowds carrying baby buggies with loss of orientation and decency'.

If only had nothing else to do, I could fully indulge myself and study the strange mental state people get in while on a Christmas shopping spree...But, I had to attend also to more enlightened things such as coding my Shoes course assignments. The course is great and lots of fun. This is not just getting some exercises done. There is a great social aspect where you can interact with other coders and see solutions from other people, things you would have never thought of yourself. Also, having a skilled teacher who carries a contagious enthusiasm providing some excellent exercises helps to decrease the learning curve and keeps it fun! Unfortunately, the better must sometimes give way to the good...
But, if you are interested in learning some GUI programming, there is good news for you! The 3rd batch of the shoes programming course at Rubylearning has been announced, and it starts on 14 February. That's no holidays to bother you!

Throwing myself at the Christmas shopping crowd did pay off at the very end. The right presents were obtained, and to further add to the joy, I finally got hold of my own very ASUS EEE PC! After a disastrous adventure on eBay, countless disappointments in the shops, and a somewhat obsessive compulsive venture to the ugliest city of my country, I am pleased to announce: ladies and gentlemen: I got em. Apparently the very last, ASUS EEE PC 1000HD, and specially for Christmas: the white version! Unfortunately, _no_ shop in .nl sells the Linux version. Apparently consumers prefer to pay for a Windows license and the dreadful OEM CD instead of a nice 40GB SSD. Well, if the noise will start to bother, I guess I'll visit jkk mobile and put the SSD in myself. It's a relieve to find that ubuntu eee runs flawlessly and everything works like a charm. All adjustments have been made to resume my ruby coding and blogging as well.

The holiday's have not arrived for me yet though. Starting tomorrow I have the social calls to make and right after that I'll travel to Berlin for the 25C3. But there is one thing that's different in this busy week as opposed to the previous: an EEE with a full sized keyboard! I get to do some work while I'm wasting my time, finally salvation!

Marry Christmas!

11.12.08

GUI toolkit extravaganza part III: WxRuby

In the previous part of the GUI toolkit extravaganza we looked at the FXRuby toolkit. This time we will spend some time to look at the Yin of FXRuby's Yang, the wxRuby toolkit.

wxRuby
wxRuby is a library that enables Ruby code to use the wxWidgets library. wxWidgets is a cross-platform GUI library written in C++. wxRuby has arrived at version 2 so we will look at that one. Not only the name sounds like FXRuby but also it's features seem alike. Let's find out if there are any differences!

ease of installation
xwruby2 is available as a Ruby gem. This makes the installation process very easy. According to the documentation, all you have to do is: sudo gem install wxruby2. There are also binary and source packages are available. On my Ubuntu Hardy Heron system, this did not do the trick however. Apperently there is something else you need to to apart from installing the gem, and this is poorly documented. You can read more about the problems installing wxruby2 in my previous post.

documentation
There seems to be quite a decent ammount of documentation for wxruby2. The project website provides the complete API reference and a Tutorial. As far as I'm aware there are no books about wxruby2, nor have I seen it featured in a book. The project's website hosts two mailinglists.

supported platforms
Wxruby has been created to provide a cross platform GUI toolkit, so it runs on multiple platforms. Systems running Linux, UNIX, Windows or Os/X are all supported by wxruby.

complexity
Take a look at the following code sample. We create a main window and place a button on it. We use PLACEMENT_SCREEN to center it. Piece of cake!

#!/usr/bin/env ruby
#!/usr/bin/env ruby
require 'wx'
Wx::App.run do
frame = Wx::Frame.new(nil, :title => "wxRuby window")
button = Wx::Button.new(frame, :label => 'Button')
evt_button(button) { puts 'WXRuby' }
frame.show
end

functionality
wxruby2 provides a standard set of graphical functions. The focus lies on cross-platform availability and the usage of native window designs. So on a GNOME system, you get GTK look and feel, on and on Mac OS/X you get aqua and on Windows you get the well known theme from Redmond. I am curious though as how much of windows, aqua and gtk functionality is supported...Unfortunately, finding that out would take more time then I have at the moment.

performance
There was some flickering going on at the top of the window sometimes. But I have noticed that on Fxruby windows as well since I upgraded my system to Ubuntu Intrepid. It does not seem like it's a problem caused by any of these two kits. Otherwise this performs just good.

looks
There's not much difference to be seen in the example provided here and the one previously provided in parts I of the GUI toolkit extravaganza. There are some minimal details around the buttons and edges. Reason is that it uses native graphics, so on my system it uses GTK in the end. Instead of adding yet another picture of a window with one button with only miniscule differences. Here's a picture from one of the sample programs, in this case,



You can find the wxruby2 toolkit at:
http://wxruby.rubyforge.org

Next up for the GUI toolkit extravaganza part IV is Shoes!

9.12.08

wxruby: reading error messages about libraries

As you might have noticed, some time has passed since I wrote about fxruby. Wxruby was to follow a few days after it. However I had been experiencing problems getting it to work. With only a few days to go before the Shoes class starts I wanted to put an end to this misery.

First thing people do nowadays when problem solving is to Google for solutions, so did I. Searching for the errors I got did return some mailing list postings from people with the same problem, but no useful answers. As you might have experienced yourself, searching for these kinds of things can quickly take hours without you realizing it. It is not only time consuming, but in the spirit of despair you might just actually try some really stupid recommendation from a person who's most likely just be guessing himself. Actually trying some of their suggestions can add even more problems and make the whole so complex you'll eventually just give up...but then that's not very Ninja, is it?

After installing the wx package I thought I'd only need to install the wxruby gem and be done with it.

sudo gem install wxruby

Unfortunately not so...
ruby -rubygems ./wxtest.rb
/var/lib/gems/1.8/gems/wxruby-1.9.9-x86-linux/lib/wxruby2.so: libwx_gtk2u_stc-2.8.so.0: cannot open shared object file: No such file or directory - /var/lib/gems/1.8/gems/wxruby-1.9.9-x86-linux/lib/wxruby2.so (LoadError)
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:27:in `require'
from /var/lib/gems/1.8/gems/wxruby-1.9.9-x86-linux/lib/wx.rb:12
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:32:in `gem_original_require'
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:32:in `require'
from ./wxtest.rb:3
Obviously it had some problems loading the wxruby2.so binary library. This file exists in the specified directory along with wx.rb. The permissions to wxruby2.so were set to readonly, this did not seem right for a binary. Adding execute permissions did not help however.

The problem might have been caused by one of the other errors. One of them seemed to originate from wx.rb. Wx.rb can be found in the same directory as wxruby2.so. This is the file you should require in your ruby code. Basically it loads both the wxruby2.so and ruby extensions to core wx classes. There was something about line 12. Using vim's :set nu command, the apparent culprit was displayed on my screen:
11 # load the binary library
12 require 'wxruby2'
13
14 # alias the module
15 Wx = Wxruby2
There could not be anything wrong about that.

There were however three errors in custom_require.rb. The first impression was, judging from the directory, that this was a generic function used by or for rubygems. Obviously I was not very convinced that this might be the cause of the problem. But it would not hurt to take a look.

First look was at the code near line number 27:
26 def require(path) # :nodoc:
27 gem_original_require path
28 rescue LoadError => load_error
29 if load_error.message =~ /\A[Nn]o such file to load -- #{Regexp.escape path}\z/
Hmm, this code wants a path to some gem thingy, else it throws an error. Not the error I got however. While this seemed illogical at that moment, sometimes one must keep problems simple and not add other problems along the way. The main point was this code wanted some path.

Two other errors were caused at something near line 32.
32 gem_original_require path
33 else
34 raise load_error
No more clues were necessary. The suspect was some unknown path variable. Since wxruby was installed as a gem. My code needed to load rubygems itself. According to the installation manual there's three ways to do this:

* Run ruby with the -rubygems argument: eg ruby -rubygems my_wxruby_script.rb
* Set the RUBYOPT environment variable to -rubygems
* Add require 'rubygems' at the top of your wxruby program.

I had tried running my code with the -rubygems argument allready. And I did use the require 'rubygems' in my code. I did not set the RUBYOPT however, so I removed the require line and set this variable with RUBYOPT="-rubygems".

The same errors lit the screen after I ran my code again. A sense of defeat and disillusion filled my mind, what could be the problem? I had tried countless solutions, analyzed code, read documentation and yet I was no step further. Having no more ideas, I stared blankly at the screen, looking at the errors...

/var/lib/gems/1.8/gems/wxruby-1.9.9-x86-linux/lib/wxruby2.so: libwx_gtk2u_stc-2.8.so.0: cannot open shared object file: No such file or directory - /var/lib/gems/1.8/gems/wxruby-1.9.9-x86-linux/lib/wxruby2.so (LoadError)

Staring back was a half lit reflection of myself. I saw a person who was about to give up as I started to think about skipping this chapter of the GUI Toolkit extravaganza. At least I had Ruby-GTK2 and FXruby...GTK?...What's that gtk doing in my error message!

A quick query in my package management tool found that I did not have any libwx_gtk* libraries installed, why would I. The documentation certainly did not write about needing this library! I did encounter it while searching for ruby & wx libraries, but never made the connection...

Anyway, there were some dependencies and here's what was installed:

libwxgtk2.6-0
libwxgtk2.6-dev
libwxbase2.6-0
libwxbase2.6-dev
libwxgtk2.8-0
libwxgtk2.8-dev
libwxbase2.8-0
libwxbase2.8-dev

After this I could run the examples that come with wxruby. Looking back at the errors I wonder, did you overlook this one in the error messages as well?

Comming up: Part III of the GUI toolkit extravaganza, featuring wxruby!