Adding widgets to glade generated interfaces via code

I have an interface that I created in Glade3. I’ve used
ruby-glade-create-template to create my initial code file. In the glade
template I made a horizontal box with one cell which I named gl_box.

I my code I’ve since created a Gtk::DrawingArea object which I would
like to pack into the gl_box. I’ve attached the code. The attempt I made
is at line 24.


#!/usr/bin/env ruby

This file is gererated by ruby-glade-create-template 1.1.4.

require ‘libglade2’
require ‘drawing_area’

class Interface02Glade < Gtk::Window
include GetText

attr :glade

def initialize(path_or_data, root = nil, domain = nil, localedir =
nil, flag = GladeXML::FILE)
super(“DRCAM”)
bindtextdomain(domain, localedir, nil, “UTF-8”)
@glade = GladeXML.new(path_or_data, root, domain, localedir, flag)
{|handler| method(handler)}
glconfig = create_glconfig

set_reallocate_redraws(true)
signal_connect("delete_event"){ Gtk.main_quit}

@cnc_drawing_area = CNCDrawingArea.new(glconfig)
@cnc_drawing_area.set_size_request(640,480)
#gl_box.pack_start(@cnc_drawing_area, true, true, 0)

end

def create_glconfig # Gdk::GLConfig.new([screen,]
attrib_list/mode)–Returns an OpenGL frame buffer configuration that
matches the specified configuration.
# Try double-buffered visual
glconfig = Gdk::GLConfig.new(Gdk::GLConfig::MODE_RGB|
Gdk::GLConfig::MODE_DEPTH|
Gdk::GLConfig::MODE_DOUBLE)

unless glconfig
  puts "*** Cannot find the double-buffered visual."
  puts "*** Trying single-buffered visual."

  # Try single-buffered visual
  glconfig = Gdk::GLConfig.new(Gdk::GL::MODE_RGB|
                               Gdk::GL::MODE_DEPTH)
  raise  "No appropriate OpenGL-capable visual found." unless

glconfig
end
glconfig
end

end

Main program

if FILE == $0

Set values as your own application.

PROG_PATH = “interface_layouts/interface-02.glade”
PROG_NAME = “YOUR_APPLICATION_NAME”
Interface02Glade.new(PROG_PATH, nil, PROG_NAME)
Gtk.main
end


What is the correct way to reference the widgets made via Glade3? Thanks
in advance for any help.

Mark Thompson wrote:

What is the correct way to reference the widgets made via Glade3? Thanks
in advance for any help.

Try @glade.get_widget(“yourwidgetname”)

This will return the GTk widget. Use <<, add, pack_start or pack_end
then to add widgets to your container.

Joachim G. wrote:

Try @glade.get_widget(“yourwidgetname”)

Joachim, thank you for your quick reply. That worked as far as I can
tell, but I still can’t see my opengl window. Perhaps I need to set some
properties of the window? Here’s how I implemented your suggestion.

@cnc_drawing_area = CNCDrawingArea.new(glconfig)
@cnc_drawing_area.set_size_request(640,480)
gl_box = @glade.get_widget("gl_box")
gl_box.pack_start(@cnc_drawing_area, true, true, 0)

Mark Thompson wrote:

Joachim G. wrote:

Try @glade.get_widget(“yourwidgetname”)

Joachim, thank you for your quick reply. That worked as far as I can
tell, but I still can’t see my opengl window. Perhaps I need to set some
properties of the window? Here’s how I implemented your suggestion.

@cnc_drawing_area = CNCDrawingArea.new(glconfig)
@cnc_drawing_area.set_size_request(640,480)
gl_box = @glade.get_widget("gl_box")
gl_box.pack_start(@cnc_drawing_area, true, true, 0)

That was it! .set_visible(true)

@cnc_drawing_area = CNCDrawingArea.new(glconfig)
@cnc_drawing_area.set_size_request(640,480)
@cnc_drawing_area.set_visible(true)
gl_box = @glade.get_widget("gl_box")
gl_box.pack_start(@cnc_drawing_area, true, true, 0)

Many thanks again Joachim!

I’m dying to know where you got this information, as it could be a great
resource for me as well…

Mark Thompson wrote:

That was it! .set_visible(true)

Or try show_all on your top level window

Mark Thompson wrote:

I’m dying to know where you got this information, as it could be a great
resource for me as well…

Meaning the “@glade.get_widget(“yourwidgetname”)” thing.

Hi,

it’s from the ruby-libglade docs at:

http://ruby-gnome2.sourceforge.jp/hiki.cgi?GladeXML#get_widget

Cheers, detlef

Am Mittwoch, den 05.03.2008, 17:49 +0100 schrieb Mark Thompson: