Module: SimpleLayout::Base
- Defined in:
- lib/simple_layout.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#add_component(w, container, layout_opt = nil) ⇒ Object
add a widget to container (and/or become a new container as well).
-
#area_first(resize = true, shrink = true, &block) ⇒ Object
for HPaned and VPaned container.
- #area_second(resize = true, shrink = true, &block) ⇒ Object
-
#component(name) ⇒ Object
get component with given name.
-
#component_children(name) ⇒ Object
return children array of a component or group.
-
#expose_components ⇒ Object
expose the components as instance variables.
-
#factory_menu_bar(name, options = {}, &block) ⇒ Object
menu stuff.
- #factory_menu_item(name, options = {}, &block) ⇒ Object
- #grid(left, top, *args, &block) ⇒ Object
-
#grid_flx(left, right, top, bottom, *args, &block) ⇒ Object
for Table container.
-
#group(name) {|cnt| ... } ⇒ Object
group the children.
-
#layout_component(w, options = {}, &block) ⇒ Object
layout the new UI component (container or widget).
-
#page(text = nil, &block) ⇒ Object
for Notebook container.
-
#register_auto_events ⇒ Object
register automatic event handlers.
-
#with_attr(options = {}, &block) ⇒ Object
create a “with block” for setup common attributes.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object (private)
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
# File 'lib/simple_layout.rb', line 456 def method_missing(sym, *args, &block) if sym.to_s =~ /^(.+)_in_(.+)$/ maps = self.class.layout_class_maps inner, outter = $1, $2 if maps[inner] && maps[outter] if args.last.is_a?(Hash) = {} = args.pop if args.last.is_a?(Hash) # default args pass to inner component, execpt: # :layout pass to outter :layout # :inner_layout pass to inner :layout # :outter_args pass to outter args outter_args, outter_layout_opt, [:layout] = .delete(:outter_args), .delete(:layout), .delete(:inner_layout) outter_args = (outter_args ? [outter_args] : []) unless outter_args.is_a?(Array) outter_args << {} unless outter_args.last.is_a?(Hash) outter_args.last[:layout] ||= outter_layout_opt args.push # push back inner options end inner_proc = Proc.new do create_component(maps[inner], args, block) end return create_component(maps[outter], outter_args || [], inner_proc) end end simple_layout_method_missing_alias(sym, *args, &block) end |
Class Method Details
.included(base) ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/simple_layout.rb', line 102 def Base.included(base) base.extend(ExtClassMethod) base.layout_class_maps.each do |k, v| define_method(k) do |*args, &block| create_component(v, args, block) end end end |
Instance Method Details
#add_component(w, container, layout_opt = nil) ⇒ Object
add a widget to container (and/or become a new container as well). do not call this function directly unless knowing what you are doing
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/simple_layout.rb', line 137 def add_component(w, container, layout_opt = nil) if @pass_on_stack.last.nil? || @pass_on_stack.last[0] == false if container.is_a?(Gtk::Box) layout_opt ||= [false, false, 0] pack_method = 'pack_start' if layout_opt.first.is_a?(Symbol) pack_method = 'pack_end' if layout_opt.shift == :end end container.send(pack_method, w, *layout_opt) elsif container.is_a?(Gtk::Fixed) || container.is_a?(Gtk::Layout) layout_opt ||= [0, 0] container.put w, *layout_opt elsif container.is_a?(Gtk::MenuShell) container.append w elsif container.is_a?(Gtk::Toolbar) container.insert(container.n_items, w) elsif container.is_a?(Gtk::MenuToolButton) container. = w elsif container.is_a?(Gtk::Table) # should use #grid or #grid_flx to add a child to Table elsif container.is_a?(Gtk::Notebook) # should use #page to add a child to Notebook elsif container.is_a?(Gtk::Paned) # should use #area_first or #area_second to add child to Paned elsif container.is_a?(Gtk::Container) layout_opt ||= [] container.add(w, *layout_opt) end else fun_name, args = *(@pass_on_stack.last[1]) container.send(fun_name, w, *args) end end |
#area_first(resize = true, shrink = true, &block) ⇒ Object
for HPaned and VPaned container
212 213 214 |
# File 'lib/simple_layout.rb', line 212 def area_first(resize = true, shrink = true, &block) container_pass_on(Gtk::Paned, 'pack1', resize, shrink, block) end |
#area_second(resize = true, shrink = true, &block) ⇒ Object
215 216 217 |
# File 'lib/simple_layout.rb', line 215 def area_second(resize = true, shrink = true, &block) container_pass_on(Gtk::Paned, 'pack2', resize, shrink, block) end |
#component(name) ⇒ Object
get component with given name
183 184 185 |
# File 'lib/simple_layout.rb', line 183 def component(name) @components[name] end |
#component_children(name) ⇒ Object
return children array of a component or group
188 189 190 191 |
# File 'lib/simple_layout.rb', line 188 def component_children(name) @component_children ||= {} @component_children[name] end |
#expose_components ⇒ Object
expose the components as instance variables
125 126 127 128 129 130 131 132 133 |
# File 'lib/simple_layout.rb', line 125 def expose_components() @components.each_key do |k| unless self.respond_to?(k.to_s, true) self.instance_eval("def #{k.to_s}; component(:#{k.to_s}) end") else raise LayoutError, "#{k} is conflit with method, please redifine component id" end end end |
#factory_menu_bar(name, options = {}, &block) ⇒ Object
menu stuff
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/simple_layout.rb', line 235 def (name, = {}, &block) cb = Proc.new do |id, w| id = id.gsub('_', '') if id.is_a?(String) m = "menu_#{name}_on_active" self.send(m, id, Gtk::ItemFactory.(w), w) if self.respond_to?(m) end @item_factory_stack ||= [] @item_factory_stack.push [cb, [], []] block.call(name) if block [:id] ||= name.to_sym _, _, items = @item_factory_stack.pop accel_group = Gtk::AccelGroup.new add_accel_group(accel_group) fact = Gtk::ItemFactory.new(Gtk::ItemFactory::TYPE_MENU_BAR, "<#{name}>", accel_group) fact.create_items(items) # process item attributes items.each do |x| # TODO: ... end layout_component(fact.("<#{name}>"), ) end |
#factory_menu_item(name, options = {}, &block) ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/simple_layout.rb', line 258 def (name, = {}, &block) cb, stack, items = @item_factory_stack.last branch = false [:type] ||= :Item case name when /^[-]+$/ [:type] = :Separator when /^<[-]+>$/ [:type] = :Tearoff when /^>>(.+)>>$/ name = $1 branch = true [:type] = :LastBranch when /^<(.+)>$/ name = $1 branch = true [:type] = :Branch end image = .delete(:image) if image.is_a?(String) [:type] = :ImageItem image = Gdk::Pixbuf.new(image) elsif image.is_a?(Gdk::Pixbuf) [:type] = :ImageItem elsif image [:type] = :StockItem end item = [ "#{stack.last}/#{name}", "<#{[:type].to_s}>", [:accel], image, cb, [:id] || name ] items << item if branch stack.push "#{stack.last}/#{name}" block.call(name) if block stack.pop if branch end item end |
#grid(left, top, *args, &block) ⇒ Object
229 230 231 232 |
# File 'lib/simple_layout.rb', line 229 def grid(left, top, *args, &block) args.push block container_pass_on(Gtk::Table, 'attach', left, left + 1, top, top + 1, *args) end |
#grid_flx(left, right, top, bottom, *args, &block) ⇒ Object
for Table container
225 226 227 228 |
# File 'lib/simple_layout.rb', line 225 def grid_flx(left, right, top, bottom, *args, &block) args.push block container_pass_on(Gtk::Table, 'attach', left, right, top, bottom, *args) end |
#group(name) {|cnt| ... } ⇒ Object
group the children
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/simple_layout.rb', line 194 def group(name) cnt, misc = @containers.last gs = (name ? [name].flatten : []) gs.each{|g| @component_children[g] ||= [] } m = { :groups => gs, :virtual => true, :sibling => misc[:sibling], :insp => misc[:insp], :layout => misc[:layout], :options => misc[:options], :name => nil, } @containers.push [cnt, m] yield cnt if block_given? @containers.pop end |
#layout_component(w, options = {}, &block) ⇒ Object
layout the new UI component (container or widget)
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/simple_layout.rb', line 304 def layout_component(w, = {}, &block) @containers ||= [] @pass_on_stack ||= [] @components ||= {} @common_attribute ||= [] @component_children ||= {} add_singleton_event_map(w) # so that you can use: w.on_clicked{|*args| ... } name = .delete(:id) group_name = .delete(:gid) || name layout_opt = .delete(:layout) keep_top_cnt = .delete(:keep_top_container) .each do |k, v| if v.is_a?(Array) w.send(k.to_s, *v) if w.respond_to?(k.to_s) else w.send(k.to_s + '=', v) if w.respond_to?(k.to_s + '=') end end @components[name] = w if name gs = (group_name ? [group_name].flatten : []) gs.each{|g| @component_children[g] ||= [] } misc = nil if @containers.size > 0 container, misc = @containers.last misc[:groups].each{ |g| @component_children[g].push w } misc[:sibling] += 1 end unless container and container.is_a? Gtk::ScrolledWindow insp_evb = make_inspect_evb(misc, w, name, layout_opt, ) end if block # if given block, it's a container as well m = { :groups => gs, :sibling => 0, :insp => insp_evb, :name => name, :layout => layout_opt, :options => , } @containers.push [w, m] @pass_on_stack.push [false, nil] @common_attribute.push({}) block.call(w) if block @common_attribute.pop @pass_on_stack.pop @containers.pop end if @containers.size > 0 add_component(insp_evb || w, container, layout_opt) # add myself to parent else add_component(insp_evb || w, self, layout_opt) unless keep_top_cnt # add top container to host @components[:self] = self # add host as ':self' end w end |
#page(text = nil, &block) ⇒ Object
for Notebook container
220 221 222 |
# File 'lib/simple_layout.rb', line 220 def page(text = nil, &block) container_pass_on(Gtk::Notebook, 'append_page', Gtk::Label.new(text), block) end |
#register_auto_events ⇒ Object
register automatic event handlers
114 115 116 117 118 119 120 121 122 |
# File 'lib/simple_layout.rb', line 114 def register_auto_events() self.methods.each do |name| if name =~ /^(.+)_on_(.+)$/ w, evt = $1, $2 w = component(w.to_sym) w.signal_connect(evt) do |*args| self.send(name, *args) end if w end end end |
#with_attr(options = {}, &block) ⇒ Object
create a “with block” for setup common attributes
172 173 174 175 176 177 178 179 180 |
# File 'lib/simple_layout.rb', line 172 def with_attr( = {}, &block) if block @common_attribute ||= [] @common_attribute.push cnt, _ = @containers.last block.call(cnt) @common_attribute.pop end end |