Sunday, May 22, 2011

Getting back into Ruby

My bad. I've been doing lots of stuff with Ruby on and off over the last few years. I haven't written much here. I'll try to change that soon.

I'm re-acquainting myself with github. It looks like the last time I made a commit there was 2 years ago. I'm clearing that space now and refreshing it.  I will post an update here when I have dropped some new scripts there.

Saturday, April 3, 2010

Saving window position with FXRegistry and using those settings at startup

In my last post I described my discovery of where the FXRegistry settings are saved in the Windows Registry.  I used the 'imageviewer.rb' sample/example program code to help me figure out how to read/write these settings.

I started off by simply using the x, y, width, height settings in my test app.  I found that the width and height settings were getting saved and retrieved correctly because the window size (when the app started) always matched what it was when it last closed. (good)  However, the window always started in the middle of the screen. (not so good)  Why is that?

Many frustrating hours later of searching and tinkering, I eventually thought to start playing with the "show( PLACEMENT_SCREEN )" line of code in the application.  That was it!

If you read the FXTopWindow API doc, you'll see a description of the Initial window placement options.  In particular, I found these three interesting:
  1. PLACEMENT_SCREEN - place it centered on the screen
  2. PLACEMENT_VISIBLE - place window to be fully visible
  3. PLACEMENT_DEFAULT - place it at the default size and location
So, I replaced the 'show' command option in imageviewer.rb and here's what I noticed. (BTW, if you try this with the same sample program make sure you  quit the program using CTRL+Q. Otherwise the settings won't get saved!)

The imageviewer code includes PLACEMENT_SCREEN, and regardless of what 'position' settings you quit the app with, it will always open in the centre of the screen - just as advertised.

I changed the line to use PLACEMENT_DEFAULT, and the window now opened exactly where I left it when it closed.  Yay!  That's exactly what I was expecting the code to do but it wasn't.  Anywhere in the primary or secondary monitor or even with the window app partially off the screen completely - it keeps appearing exactly where it last was.

Then my 'tester' brain kicked in (because I'm a tester by trade, not a programmer) and I wondered if there was a way to prevent a user from exiting the app off-screen (i.e. where you can't see most of the app window at all) and having it open in the same spot, partially hidden off screen?  It seemed like a lot of icky code to check the x & y coordinates and enforce some kind of 'good' placement settings.  And it gets ickier when you consider that every monitor has different resolution settings, and multiple monitors and all that bother.  blah.

So I tried PLACEMENT_VISIBLE.  This one was neat.  When I exited the app anywhere on screen, it opened the next time exactly where I left it. (good)  Then I tried exiting the app with the window partially or mostly hidden off screen, and when I start it the next time it pops on screen close to where it was, except I can see the whole window -- i.e. it's "visible".  Therefore, working as advertised! (good!)

I moved the app to my second monitor and exited the app and it appeared in the second monitor too.  The difference between this setting and PLACEMENT_DEFAULT is that with 'default' I can save the coordinates off-screen and that's where the window will next appear, but with 'visible' the window always appears in a way where you can see the whole app UI.

Cool!  Problem solved.  I don't have to write and maintain a whole bunch of complex code to idiot-proof a minor, undocumented feature of my app.  =)

The only thing left is to hopefully update the imageviewer.rb code so that it includes a different show/placement line so that it uses the x/y position settings it saves.  I'll forward this note to Lyle to see what he thinks.

Cheers!

FXRegistry and the Windows Registry

Someone pointed me to the FXRegistry class when I was faced with the problem of trying to save/retrieve settings from the FXRuby app I'm writing.

I read the FXRuby API docs but they didn't tell me much.  An internet search brought me to the Fox-Toolkit.org documentation page, and while more informative, I still didn't have clear examples of how to do this.

Turns out the FXRuby User's Guide Examples Page includes a program (imageviewer.rb) that uses FXRegistry to save/retrieve some settings.  That was beginning to help but I still had lots of questions. I found another example in chapter 12 of the FXRuby book ("FXRuby: Create Lean and Mean GUIs with Ruby" by Lyle Johnson). Section 12.3 talks about resizing layouts and I didn't care for that, but I did care about the fact that it saves and reads the settings from the registry.  The code for splitter.rb is available online. (NOTE: this splitter.rb app is different from the splitter.rb app you find in the FXRuby examples page and directory on your hard drive.)

What did I learn from all this?  Settings are magically saved somewhere, but no one could tell me exactly where in MS Windows. (I'm currently running/testing this on Windows XP.)  So I opened up 'regedit', ran the FXRuby apps and looked for the saved settings.

Here's what I found.

The 'splitter.rb' app saved the settings in: \HKEY_CURRENT_USER\Software\FoxDefault\Application\Settings

The 'imageviewer.rb' app saved the settings in: \HKEY_CURRENT_USER\Software\FoxTest\ImageViewer\...

Okay. So, I copied the registry commands from imageviewer.rb to my test app and ran it.  Where did the settings get saved?  Turns out they were saved in the same location as the splitter.rb.  Why? How do I change that?  How do I control where the settings go?

I noticed that there were other settings under the 'FoxTest' key in the Registry.  I ran a lot of sample code that I found on the net, and several of them must have written to the registry.

I stared back and forth at the code for the 3 apps (mine, splitter and imageviewer) until it hit me - I found the difference and the answer I was looking for!

If you look at the bottom of the imageviewer code, you will see the following line:

  application = FXApp.new("ImageViewer", "FoxTest")

When I look at the splitter code, it looks slightly different:

  FXApp.new do |app|
    SplitterExample.new(app)
    app.create
    app.run
  end

My source code looked like:

  application = FXApp.new

Aha! There it is!

The imageviewer code specifies the application name in the 'FXApp.new' creation line and that is used in the FXRegistry key names.  The splitter code and my code didn't specify any names so it gets saved to "FoxDefault\Application".

So, I changed my code to now read:

  application = FXApp.new( "TestApp", "FoxTest")

And I found the settings saved in: \HKEY_CURRENT_USER\Software\FoxTest\TestApp\

Yay!  I now know where the FXRegistry settings are saved in Windows Registry and how to change the location.  Cool.

Tuesday, March 30, 2010

FXRuby 'FRAME' type examples

I created a good working UI using foxGUIb 1.0.0 for an app that I've wanted to create for a while now.  The problem I have with the source code generated is that it's hard to customise.  When something doesn't look quite right, I'm competing with several layers for clarity and what I see in the generated source code doesn't match what I see built with the tool.

I gave up trying to make it work for me and decided to just start from scratch with plain FXRuby.  I'm using the foxGUIb layout as my 'spec' or template to build the app using simpler code.  The problem now, of course, is that I don't know FXRuby and am learning it as I go.  Google is my friend, coupled with the many examples available from the FXRuby docs.

While working with FXGroupBox, I was trying to figure out what the different frameStyle types did. e.g. FRAME_GROOVE, FRAME_LINE,  FRAME_RAISED, FRAME_RIDGE,  FRAME_SUNKEN, FRAME_THICK, and FRAME_NORMAL

Google turned up this example from the "FXRuby: Create Lean and Mean GUIs with Ruby" book.  I love online examples! =)

Can you find the error in the code?

Tuesday, March 2, 2010

Enabling checkboxes in FXRuby

While playing around with FXRuby, I tried to create a form window where you could have an "Include All?" check box at the top that automatically enables/disables all the other options (check boxes) below.

This turned out to be non-obvious to code for a non-programmer like me. None of the FXRuby examples include code that does something like this. Google didn't help and neither did the FXRuby book. (Although in all fairness, I only skimmed through the book and didn't read the whole thing.)

I posted a request for some help to the FXRuby forum and a kind soul wrote up an example to do just that. Rather than include the sample code in an email, he posted it online at: http://pastebin.com/rPKJLC9r

I'd never heard of Pastebin.com until now. Seems like a cool place to drop random chunks of code that you can have others look over and help you get working. I posted the link here so I can remember where to find it next time.

Oh, and the FXRuby code example was perfect. Just what I needed. The sample code was an over-simplified version of what I needed to do but it was enough to help me understand the relationship between the objects and commands.

Wednesday, February 24, 2010

Creating UI's with Ruby

I began with Ruby years ago by writing scripts to help me test a web app. I used WATIR for that. I will likely write more about Watir another time.

It took me a while to figure out how to write and structure the scripts using whatever examples I found online. It wasn't until I read Brian Marick's "Everyday Scripting with Ruby" that I really understood how to structure a ruby program. Excellent book by the way - I highly recommend it!

Over the years I accumulated quite the army of command-line programs/scripts and one thing I've wanted to do was figure out how to put a pretty UI/front-end on some of these scripts.

Apparently with Ruby, you have a few different options - TK, FXRuby, Shoes, etc.

I decided to settle on FXRuby since it's the only one that happens to be supported out-of-the-box with a clean Ruby install. (Watch, I bet they'll remove it from a future distribution/release now that I've said that.)

FXRuby is kind of neat. The main home page has some good documentation and examples to help get you started.

I was able to easily create some simple apps, but then it gets kind of tough. Working with FXRuby reminds me of the time I took a course in Japanese, some 20 years ago. Nice language. I picked up the alphabets, sentence structure and syntaxes and some of the vocabulary. I got the basics but just couldn't piece together big sentences/conversations. It just got too complicated for me. I suppose if I'd gone to Japan or had more opportunity to practice that it would have gotten easier, but I didn't and it didn't.

FXRuby's like that. Looks simple on the surface, but make no mistake, you need to know how to program to piece together a working app! The examples are helpful, but what I want to do is somehow always a bit more complicated than the available examples. Sigh. If only it were easier to pull together a UI for such a friendly programming language.

I discovered foxGUIb a few days ago. That tool really helped me figure out the relationships between the various layout styles and elements to bring together a UI. Unfortunately, I don't think I can use the code it generated, so I now have a pretty prototype with no working code underneath.

Oh well. Here's hoping that with practice it will get easier to do this sort of thing. I've browsed all of the available GUI toolkits and they're all the same to me. They start off with easy, Ruby'ishly-simple examples that try to hook you - then "WHAM!", insert programming jargon, hooks, classes and structures and "voila! there you go." Easy as 1, 2, googolplex.

Ruby I like. All of the available toolkits to help you build UI's still seem very programmerish to me. Not cool.

5 Chapters of O’Reilly’s Ruby Best Practices – Free!

Came across a tweet today:
zeljkofilipin 5 Chapters of O’Reilly’s #Ruby Best Practices – Free! http://bit.ly/aFhTcE

Of course, I followed the link and discovered the RubyInside web site... and that yes, O'Reilly has made the first 5 chapters of 'Ruby Best Practices' available in PDF form online for free.

I browsed them. Boring. Way to suck the life and passion out of a fun and cool programming language.

I've collected a bunch of Ruby ebooks in PDF and CHM (Windows help file) format in my travels over the last few years. I haven't read a single one of them. Or rather, the benefit (for me) is in helping me do a quick electronic search through the text to see if there's something that will help me with whatever current programming problem I'm working on.

I have a few books (sorry environmentalists, but I still prefer paper) that I use for reference and have a few that I still plan to read, but in general I've learnt programming by doing it. Which is to say that I don't really know how to program so much as do creative and well-documented hacking. ;)

I've got more to learn, but only as much as I need to learn to complete whatever projects I'm working on. I'm not a programmer by trade so I will defer to the experts when I need to get some real work done. God bless online forums! =)

Cheers!