LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-05-2006, 03:15 PM   #1
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 53
ruby/gtk2 drawingarea


Hello,

I'm trying to use a
Code:
GTK2::DrawingArea
in ruby but I can't even get started! The tutorial is flimsy and I really can't decide how to get started. What I want to do is create a drawingarea (which I can do) add it to the window (also, I can do) and then draw text to the drawingarea (no idea how to do this...).

Anyone have any ideas?

Last edited by Komakino; 07-05-2006 at 03:17 PM.
 
Old 07-05-2006, 09:42 PM   #2
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
To get you started:
http://ruby-gnome2.sourceforge.jp/hi...+API+Reference
http://ruby-gnome2.sourceforge.jp/hi...%3ADrawingArea
http://ruby-gnome2.sourceforge.jp/hi...%3A%3ADrawable
http://ruby-gnome2.sourceforge.jp/hi...go%3A%3ALayout
http://ruby-gnome2.sourceforge.jp/hi...o%3A%3AContext

I've made a short working example, but I haven't had a whole lot of experience with Pango, so your going to have to poke around yourself to get more info.

Code:
#!/usr/bin/env ruby

require 'gtk2'
Gtk.init

class DAWindow < Gtk::Window
	def initialize
		super
		title = "Blah"
		border_width = 4
		set_default_size(500, 400)
		
		@darea = Gtk::DrawingArea.new
		@darea.signal_connect('expose_event') do
   		  layout = Pango::Layout.new(@darea.pango_context)
   		  layout.text = "Hello, world!"
   		  @darea.window.draw_layout(@darea.style.fg_gc(@darea.state), 
   		                            50, 50, layout)
		end
		
		add(@darea)
		
		show_all

    end
end

dawindow = DAWindow.new
dawindow.signal_connect('delete_event') do
	Gtk.main_quit
	false
end



Gtk.main

Last edited by 95se; 07-05-2006 at 09:44 PM.
 
Old 07-06-2006, 09:08 AM   #3
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Original Poster
Rep: Reputation: 53
Quote:
Originally Posted by 95se
To get you started:
http://ruby-gnome2.sourceforge.jp/hi...+API+Reference
http://ruby-gnome2.sourceforge.jp/hi...%3ADrawingArea
http://ruby-gnome2.sourceforge.jp/hi...%3A%3ADrawable
http://ruby-gnome2.sourceforge.jp/hi...go%3A%3ALayout
http://ruby-gnome2.sourceforge.jp/hi...o%3A%3AContext

I've made a short working example, but I haven't had a whole lot of experience with Pango, so your going to have to poke around yourself to get more info.

Code:
#!/usr/bin/env ruby

require 'gtk2'
Gtk.init

class DAWindow < Gtk::Window
	def initialize
		super
		title = "Blah"
		border_width = 4
		set_default_size(500, 400)
		
		@darea = Gtk::DrawingArea.new
		@darea.signal_connect('expose_event') do
   		  layout = Pango::Layout.new(@darea.pango_context)
   		  layout.text = "Hello, world!"
   		  @darea.window.draw_layout(@darea.style.fg_gc(@darea.state), 
   		                            50, 50, layout)
		end
		
		add(@darea)
		
		show_all

    end
end

dawindow = DAWindow.new
dawindow.signal_connect('delete_event') do
	Gtk.main_quit
	false
end



Gtk.main
Thanks! I found the api reference already but couldn't make any use of it. What's with the @darea type variables?
 
Old 07-06-2006, 11:01 PM   #4
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
The '@' symbol means the variable is an attribute (attribute, @ttribute, get it? ) of whatever class it's declared in. Attributes live in an instance of the class (an object), and is alive as long as the object is alive. Without the @ symbol, the variable will be "dead" once the method it is declared in finishes, which is not what you necessarily want. In this example, it doesn't matter so much. Since GTK will maintain a pointer to the drawing area as long as the window is open, it isn't really needed, but if you ever wanted to expand, and refer to that variable in other instance methods of the class DAWindow, you'll be able to.

Edit: If you want to learn more about Ruby, you should check out Why's Poignant Guide. It's fun to read, and teaches you quite a bit about ruby as a language.

Last edited by 95se; 07-06-2006 at 11:04 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thoughts on Ruby as a first language dmsynck Programming 3 04-14-2006 12:36 AM
[funny study]try ruby llmmix Programming 1 12-23-2005 12:55 PM
diamonds are forever... what's up with Ruby??? zee_man78 Linux - Software 0 10-13-2005 04:48 PM
learn c++ before ruby? hottdogg Programming 13 06-17-2005 07:47 PM
Ruby help and advice Geeky_Kid Programming 5 12-31-2003 04:28 AM


All times are GMT -5. The time now is 07:56 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration