Module: Context::Gtk::Widget

Included in:
MainWindow
Defined in:
lib/Context/Gtk/Widget.rb

Overview

This is a set of routines for translating the requests from Context to the specific widgit set.

Note: If you wish your widget to be able to add and removed

other widgets (i.e. if it can act as a container), then
please define the following two methods on your object.

gtkAddWidget(widget) -- simply adds the passed widget to
                        the correct Gtk container.  Normally
                        this can be implemented using add().
                        However, for some things like tables
                        you will have to use other methods.
gtkRemoveWidget(widget) -- removes the passed widget from
                           the correct Gtk container.  Again
                           you will normally implement this
                           with remove().

The following also need to be defined if your widget is
not derived from a Gtk:Widget.

show_all() -- Displays the widgets.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#gtkWidgetMainWindowObject

Returns the value of attribute gtkWidgetMainWindow.



31
32
33
# File 'lib/Context/Gtk/Widget.rb', line 31

def gtkWidgetMainWindow
  @gtkWidgetMainWindow
end

Instance Method Details

#addToThisWidget(widget) ⇒ Object

Use this widget as a container for the passed widget. Calls gtkAddWidget, which you must define on the object. Normally this will simply call add() in the correct container. Also calls show_all, which you must define on the object (if it isn’t already).



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/Context/Gtk/Widget.rb', line 80

def addToThisWidget(widget)
 		    if !widget.class.ancestors.include?(Gtk::Window)
  		    widget.gtkWidgetMainWindow = @gtkWidgetMainWindow
  			gtkAddWidget(widget)
  			if !isInTests?
          		show_all
              end
  	    else
  	        widget.isAMainWindow
  	        widget.set_transient_for(@gtkWidgetMainWindow)
  	        if !isInTests?
      		    widget.show_all
      		end
  	    end
end

#afterWidgetIsAdded(&block) ⇒ Object

Set a closure to be run when the widget has been added. The block must accept the container widget as a parameter.



114
115
116
# File 'lib/Context/Gtk/Widget.rb', line 114

def afterWidgetIsAdded(&block)
    @gtkWidgetAddedToCallback = block
end

#afterWidgetIsRemoved(&block) ⇒ Object

Set a closure to be run when the widget has been removed. The block must accept the container widget as a parameter.



121
122
123
# File 'lib/Context/Gtk/Widget.rb', line 121

def afterWidgetIsRemoved(&block)
    @gtkWidgetRemovedFromCallback = block
end

#expandWidgetHeightObject

When adding the widget, expand it to take up all the allocated vertical height.



53
54
55
# File 'lib/Context/Gtk/Widget.rb', line 53

def expandWidgetHeight
 @gtkWidgetExpandHeight = true
end

#expandWidgetHeight?Boolean

Returns true when the the widget should take up all the allocated vertical height.

Returns:

  • (Boolean)


59
60
61
# File 'lib/Context/Gtk/Widget.rb', line 59

def expandWidgetHeight?
    @gtkWidgetExpandHeight
end

#expandWidgetWidthObject

When adding the widget, expand it to take up all the allocated horizontal width.



65
66
67
# File 'lib/Context/Gtk/Widget.rb', line 65

def expandWidgetWidth
 @gtkWidgetExpandWidth = true
end

#expandWidgetWidth?Boolean

Returns true when the the widget should take up all the allocated horizontal width.

Returns:

  • (Boolean)


71
72
73
# File 'lib/Context/Gtk/Widget.rb', line 71

def expandWidgetWidth?
    @gtkWidgetExpandWidth
end

#gtkAddWidget(widget) ⇒ Object

Default method for adding a widget. Simply logs an warning. It does not add the widget.



149
150
151
152
153
154
# File 'lib/Context/Gtk/Widget.rb', line 149

def gtkAddWidget(widget)
    Context::Log::warning("Context::Widget", 
                          "Attempted to add a widget " +
                          "to #{self.class} which doesn't define " +
                          "gtkAddWidget(). Ignoring addition.")
end

#gtkRemoveWidget(widget) ⇒ Object



156
157
158
159
160
161
# File 'lib/Context/Gtk/Widget.rb', line 156

def gtkRemoveWidget(widget)
    Context::Log::warning("Context::Widget", 
                          "Attempted to remove a widget " +
                          "from #{self.class} which doesn't define " +
                          "gtkRemoveWidget(). Ignoring removal.")
end

#isAMainWindowObject

Declares that this widget is a main Window Normally, this will get set for you if the widget you are adding is derived from Gtk::Window. But you can set it explicitly for certain effects if you know what you are doing.



47
48
49
# File 'lib/Context/Gtk/Widget.rb', line 47

def isAMainWindow
    @gtkWidgetMainWindow = self
end

#isInTests?Boolean

Helper method for testing. If this method is redefined to return true, then the items will not be shown on the screen.

Returns:

  • (Boolean)


143
144
145
# File 'lib/Context/Gtk/Widget.rb', line 143

def isInTests?
    false
end

#removeFromThisWidget(widget) ⇒ Object

Remove the passed widget from this object. Calls gtkRemoveWidget, which you must define on the object. Normally this will simply call remove(). Also calls show_all, which you must define on the object (if it isn’t already).



101
102
103
104
105
106
107
108
109
# File 'lib/Context/Gtk/Widget.rb', line 101

def removeFromThisWidget(widget)
    widget.gtkWidgetMainWindow = nil
 if !widget.class.ancestors.include?(Gtk::Window)   
        gtkRemoveWidget(widget)
        if !isInTests?
            show_all
        end
    end
end

#setupWidgetObject



34
35
36
37
38
39
40
41
# File 'lib/Context/Gtk/Widget.rb', line 34

def setupWidget
    @gtkWidgetMainWindow = nil
    # Packing hints for the container
    @gtkWidgetExpandHeight = false
    @gtkWidgetExpandWidth = false
       @gtkWidgetAddedToCallback = nil
       @gtkWidgetRemovedFromCallback = nil
end

#widgetWasAddedTo(widget) ⇒ Object

This method is called after the widget has been successfully added to another widget



127
128
129
130
131
# File 'lib/Context/Gtk/Widget.rb', line 127

def widgetWasAddedTo(widget)
          if !@gtkWidgetAddedToCallback.nil?
              @gtkWidgetAddedToCallback.call(widget)
          end
end

#widgetWasRemovedFrom(widget) ⇒ Object

This method is called after the widget has been successfully removed from another widget



135
136
137
138
139
# File 'lib/Context/Gtk/Widget.rb', line 135

def widgetWasRemovedFrom(widget)
          if !@gtkWidgetRemovedFromCallback.nil?
              @gtkWidgetRemovedFromCallback.call(widget)
          end
end