Mehr, Assaph (Assaph) wrote:> I''m having serious troubles resizing a dialog with a ListCtrl. The
ctrl
> will not resize horizontally no matter what I''m doing.
I''ve attached a
> sample code below. Any help will be greatly appreciated, as I really
> like wxRuby :)
Wow. Interesting case. It seems that wxWidgets lacks a table control
that automatically grows individual columns to fill available space.
I think I came up with something that may be close enough to what you
wanted. At least, it works pretty well on my Linux box. My revision of
your sample appears below.
First, I extracted out a FileList class, to keep things simpler. In a
real app, I would move some or all of fillListCtrl into this new class.
Next, I removed the extra sizer you were using, since it wasn''t
helping.
The ListCtrl was already automatically growing and shrinking--you just
couldn''t tell because the individual columns each had a fixed size.
After that, I removed the onSize handler in the frame, and added an
on_size handler in FileList. That''s where I experimented.
In the on_size handler, I adjust the last column to be as wide as
possible, given the new width of the control, and the widths of the
other columns.
Unfortunately, there doesn''t seem to be any way to know exactly how
wide
the column can be, because if you take up the whole width of the
ListCtrl, it inserts a horizontal scroll bar, which I didn''t want. I
couldn''t find a way to discover the width of a scroll bar, so in this
sample I just hard-coded 30 pixels of padding.
In a real app, I would generally allow users to resize columns, and
would remember their widths (except the one column that resizes itself).
That way, if I guess wrong about some of the column widths, it''s ok
because the user can easily fix it (once). Alternatively, you could also
resize more than one column, splitting the available space between them
according to whatever rule you wanted.
> Another thing I''ve noticed - this seems to be ignored in the
example
> below:
> @fileList.set_column_width(0, LIST_AUTOSIZE_USEHEADER)
For me, it seemed to work correctly. I changed the column header names
to make it more obvious whether it is working or not.
Thanks for your questions, and for trying out wxRuby. I hope it works
for you, and don''t hesitate to ask more questions.
Kevin
# BEGIN CODE
require ''wxruby''
include Wx
class FileList < ListCtrl
def initialize(parent)
super(parent, -1,
DEFAULT_POSITION, Size.new(250, 200),
LC_REPORT | LC_SINGLE_SEL)
evt_size() {|event| onSize(event) }
end
def onSize(event)
event.skip()
available_width = get_client_size.get_width - 20
taken_width = get_column_width(0) + get_column_width(1)
set_column_width(2, available_width - taken_width)
end
end
class MainFrame < Frame
def initialize ## {{{
# init size, ionstance variables and super {{{
super(nil, -1, "test frame")
#}}}
# main layout {{{
@dlgSizer = BoxSizer.new(VERTICAL)
# File List {{{
@fileList = FileList.new(self)
fillListCtrl()
@dlgSizer.add(@fileList, 1, GROW|EXPAND)
# }}}
@dlgSizer.set_size_hints(self)
set_sizer(@dlgSizer)
# end main layout }}}
## global events {{{
evt_close {onClose}
# }}}
end ## }}}
def onClose ## {{{
destroy()
end ## }}}
def fillListCtrl #{{{
@fileList.clear_all
@fileList.insert_column(0, "Longer Name", LIST_FORMAT_LEFT)
@fileList.insert_column(1, "2", LIST_FORMAT_LEFT)
@fileList.insert_column(2, "col 3", LIST_FORMAT_LEFT, 1000)
item = ListItem.new
item.set_column(0)
20.times { |i|
idx = @fileList.insert_item(item)
@fileList.set_item(idx, 0, i.to_s * i)
@fileList.set_item(idx, 1, i.to_s * i)
@fileList.set_item(idx, 2, i.to_s * i)
}
@fileList.set_column_width(0, LIST_AUTOSIZE_USEHEADER)
@fileList.set_column_width(1, LIST_AUTOSIZE_USEHEADER)
@fileList.set_column_width(2, LIST_AUTOSIZE_USEHEADER)
end #}}}
end
class TestApp < App #{{{
def on_init
@frm = MainFrame.new
set_top_window(@frm)
@frm.centre(BOTH)
@frm.show(true)
end
end #}}}
TestApp.new.main_loop