[fxruby-users] Add a tab to a tabbok dynamically?
Jeroen van der Zijp
jeroen at fox-toolkit.org
Tue Oct 3 09:40:29 EDT 2006
On Tuesday 03 October 2006 05:52, Philippe Lang wrote:
> I cannot figure out how to add a tab to a tabbook dynamically, while the program is running.
> Does anyone know how to force the tab to appear? I tried calling recalc, repaint, layout or forceRefresh, but no success... Below is a small example.
> P.S. Windows XP SP2, Ruby 1.8.4, FXRuby-1.6.2-ruby184.exe
> require 'fox16'
> include Fox
> class MyWindow < FXMainWindow
> def initialize(app)
> super(app, "Window", nil, nil, DECOR_ALL, 0, 0, 600, 350)
> # Menu bar stretched along the top of the main window
> menubar = FXMenuBar.new(self, LAYOUT_SIDE_TOP|LAYOUT_FILL_X)
> # File menu
> filemenu = FXMenuPane.new(self)
> FXMenuTitle.new(menubar, "&File", nil, filemenu)
> FXMenuCommand.new(filemenu, "&Quit\tCtl-Q\tQuit the application",
> nil, app, FXApp::ID_QUIT)
> # Tab menu
> tabmenu = FXMenuPane.new(self)
> FXMenuTitle.new(menubar, "Tab", nil, tabmenu)
> FXMenuCommand.new(tabmenu, "Add a tab").connect(SEL_COMMAND, method(:onAddTab))
> # Tabbook
> @tabbook = FXTabBook.new(self, nil, 0,
> # First tab
> FXTabItem.new(@tabbook, "Tab 1", nil)
> def onAddTab(sender, sel, data)
> puts "A tab is added..."
> FXTabItem.new(@tabbook, "Another tab", nil)
> def create
> if __FILE__ == $0
> application = FXApp.new("Attik System", "FXRuby Test")
If you add new widgets on the fly, don't forget to .create them. The
ones that exist at the time you call application.create will be recursively
created, new ones weren't part of the widget tree yet at that time.
You were right about recalc, although you should call it on the widget
you added the new children to [in this case, tabbook. The call to
recalc flags the tabbook's layout as "dirty", and when control returns
to the event loop it will perform a layout on the dialog.
In FOX, layouts are delayed, which means you can make many changes to
your widget tree, but incur the cost of the [expensive] layout only
a single time.
Its therefore very rare that you want to call layout directly; most of
the time recalc suffices. Recalc is cheap because all it does is set
dirty flags on a couple of widgets.
More information about the fxruby-users