Hi, Thanks to the newly added event handlers for Wx::Grid in the 0.6.0 release, I can now provide a much slicker intreface to my "data widgets" which heavily rely on that control. However, I haven''t quite figured out how to programmatically scroll to the last row of a populated grid (actually, the first empty row). So say that I have a grid with 200 rows only 20 of which are visible at any given time. If say 80 of these rows are currently populated with some data and the top 20 are being viewed. Now, I have to provide "Add" functionality via a button which when clicked, the grid should scroll to the 81st row so that it comes in the view (say towards the top). How do I go about doing that? I tried grid.set_scroll_y( ) but it did not seem to work. Then I tried grid.move_page_down() which worked, but I could not figure out how to know when to stop. Any ideas? -- shanko
Hi Shanko, wxGrid inherits from wxScrolledWindow, so try the "scroll" method. Nick Shashank Date wrote:> Hi, > > Thanks to the newly added event handlers for Wx::Grid in the 0.6.0 > release, I can now provide a much slicker intreface to my "data widgets" > which heavily rely on that control. > However, I haven''t quite figured out how to programmatically scroll to > the last row of a populated grid (actually, the first empty row). So > say that I have a grid with 200 rows only 20 of which are visible at any > given time. If say 80 of these rows are currently populated with some > data and the top 20 are being viewed. Now, I have to provide "Add" > functionality via a button which when clicked, the grid should scroll to > the 81st row so that it comes in the view (say towards the top). How do > I go about doing that? > > I tried grid.set_scroll_y( ) but it did not seem to work. Then I tried > grid.move_page_down() which worked, but I could not figure out how to > know when to stop. > > Any ideas? > -- shanko > _______________________________________________ > wxruby-users mailing list > wxruby-users@rubyforge.org > http://rubyforge.org/mailman/listinfo/wxruby-users > > >
Nick wrote:> Hi Shanko, > > wxGrid inherits from wxScrolledWindow, so try the "scroll" method.Thanks, Nick. I will work in that direction. Like I said earlier, I haven''t figured out how to know how much to scroll. Of course, I can always find out how many rows are populated , multiply that by the size in pixels of each row, convert the pixels into scroll units and then scroll those many units. But that is too much of work ;-) I was wondering if there is a smarter way of doing this. Thanks again, -- shanko
On Fri, 2004-12-10 at 06:59 -0600, Shashank Date wrote:> Thanks, Nick. I will work in that direction. Like I said earlier, I > haven''t figured out how to know how much to scroll. > Of course, I can always find out how many rows are populated , multiply > that by the size in pixels of each row, convert the pixels into scroll > units and then scroll those many units. But that is too much of work ;-)And it may not work. At least the way I read the wxGrid docs, each row could be a different size, so you would have to loop through, adding up each row''s height. Another crude approach might be to call move_cursor_down once for each row you want to scroll by. But hopefully you won''t need do do that, because...> I was wondering if there is a smarter way of doing this.Just looking at the wxWindows docs (not at wxRuby itself), it looks like you want wxGrid::CellToRect. Given a cell (row/col), it should return the logical X/Y position, which you could pass to scroll(x,y) [inherited from ScrolledWindow], or set_scroll_y (if it works), or at worst, to know when to stop calling move_page_down. Kevin
Hi, Kevin Smith wrote:>Another crude approach might be to call move_cursor_down once for each >row you want to scroll by. >That approach, although crude, worked like a charm. Since none of the grids I am having are larger than few hundred rows, it is OK for now.>But hopefully you won''t need do do that, >because... > > > >>I was wondering if there is a smarter way of doing this. >> >> > >Just looking at the wxWindows docs (not at wxRuby itself), it looks like >you want wxGrid::CellToRect. Given a cell (row/col), it should return >the logical X/Y position, which you could pass to scroll(x,y) [inherited >from ScrolledWindow], or set_scroll_y (if it works), or at worst, to >know when to stop calling move_page_down. > >I could not get it to work. I could be missing something. See example below (modified grid sample with the non-working code commented out). I am on Win XP (Home) using the latest one-click installer and wxRuby 0.6.0 Thanks, Kevin.>Kevin > >-- shanko ################################################################# require ''wxruby'' class MyFrame < Wx::Frame def initialize(parent, id = -1, title = "MyFrame", pos = Wx::DEFAULT_POSITION, size = Wx::DEFAULT_SIZE, style = Wx::DEFAULT_FRAME_STYLE) super(parent, id, title, pos, size, style) create_status_bar() set_status_text(Wx::VERSION_STRING) panel = Wx::Panel.new(self) btn = Wx::Button.new(panel, :btn.id, "Add", Wx::Point.new(10, 300),Wx::Size.new(50,30)) evt_button(:btn.id){|event| on_button_add(event)} make_grid(panel) populate_grid() evt_grid_cell_left_click() do |evt| set_status_text("#{evt.get_row} x #{evt.get_col} is clicked") evt.skip end evt_grid_cell_right_click() do |evt| set_status_text("#{evt.get_row} x #{evt.get_col} is right clicked") evt.skip end evt_grid_cell_left_dclick() do |evt| set_status_text("#{evt.get_row} x #{evt.get_col} is double clicked") evt.skip end evt_grid_cell_right_dclick() do |evt| set_status_text("#{evt.get_row} x #{evt.get_col} is right double clicked") evt.skip end evt_grid_label_left_click() do |evt| set_status_text("#{evt.get_row} x #{evt.get_col} label is clicked") evt.skip end evt_grid_label_right_click() do |evt| set_status_text("#{evt.get_row} x #{evt.get_col} label is right clicked") evt.skip end evt_grid_label_left_dclick() do |evt| set_status_text("#{evt.get_row} x #{evt.get_col} labelis double clicked") evt.skip end evt_grid_label_right_dclick() do |evt| set_status_text("#{evt.get_row} x #{evt.get_col} label is right double clicked") evt.skip end evt_grid_select_cell() do |evt| set_status_text("#{evt.get_row} x #{evt.get_col} cell is selected") evt.skip end evt_grid_row_size do |evt| set_status_text("Row #{evt.get_row_or_col} size changed") evt.skip end evt_grid_col_size do |evt| set_status_text("Column #{evt.get_row_or_col} size changed") evt.skip end evt_grid_editor_shown do |evt| set_status_text("Begin editing") evt.skip end evt_grid_editor_hidden do |evt| set_status_text("End editing") evt.skip end evt_grid_range_select do |evt| top = evt.get_top_left_coords bottom = evt.get_bottom_right_coords set_status_text("[ #{top[0].to_s} x #{top[1].to_s} ] to [ #{bottom[0].to_s} x #{bottom[1].to_s} ] is selected") end evt_grid_editor_created do |evt| set_status_text("Control #{evt.get_control} created") evt.skip end evt_grid_cell_change() do |evt| set_status_text("Cell #{evt.get_row} x #{evt.get_col} has changed") end end def on_button_add(event) @grid.set_grid_cursor(0,0) @grid.get_number_rows.times{|i| val = @grid.get_cell_value(i,0) @grid.move_cursor_down(false) if val.to_s.length > 0 } # rect=@grid.cell_to_rect(10,1) # x,y = rect.x, rect.y # @grid.scroll(x,y) # # row = @grid.y_to_row(y) # col = @grid.x_to_col(x) # # Wx::message_box("(#{x},#{y}) (#{row},#{col})","(x,y) (row,col)") rescue Wx::message_box($!.to_s,"add") end def make_grid(panel) # Create a wxGrid object @grid = Wx::Grid.new(panel, :grid.id, Wx::Point.new(10,10), Wx::Size.new(925,275)) # Then we call CreateGrid to set the dimensions of the grid # (11 rows and 12 columns in this example) @grid.create_grid( 51, 12 ) # We can set the sizes of individual rows and columns # in pixels, and the label value string @grid.set_row_size( 1, 40 ) @grid.set_row_size( 9, 30 ) @grid.set_row_label_value( 0, "Row1" ) @grid.set_row_label_alignment(Wx::ALIGN_CENTRE, Wx::ALIGN_CENTRE) @grid.set_col_size( 0, 120 ) @grid.set_col_label_value( 0, "Col1" ) @grid.set_col_label_alignment(Wx::ALIGN_CENTRE, Wx::ALIGN_CENTRE) # And set grid cell contents as strings @grid.set_cell_value( 0, 0, "wxGrid is good" ) # We can specify that some cells are read-only @grid.set_cell_value( 0, 3, "This is read-only" ) @grid.set_read_only( 0, 3 ) # Colours can be specified for grid cell contents @grid.set_cell_value(3, 3, "white on black") @grid.set_cell_text_colour(3, 3, Wx::WHITE) @grid.set_cell_background_colour(3, 3, Wx::BLACK) # We can specify the some cells will store numeric # values rather than strings. Here we set grid column 5 # to hold floating point values displayed with width # of 6 and precision of 2 @grid.set_col_format_float(5, 6, 2) @grid.set_cell_value(0, 6, "3.1415") end def populate_grid() %w{one two three four five six seven eight nine ten eleven twelve}.each_with_index do|val,i| @grid.set_cell_value( i, 0, val) end end end class MyApp < Wx::App def on_init frame = MyFrame.new(nil, -1, "Grid Sample", Wx::Point.new(10, 100), Wx::Size.new(1000,400)) frame.show(TRUE) set_top_window(frame) end end app = MyApp.new app.main_loop() -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/wxruby-users/attachments/20041211/7ede6462/attachment-0001.htm
On Sat, 11 Dec 2004 22:38:46 -0600, Shashank Date <sdate@everestkc.net> wrote:> I could not get it to work. I could be missing > something. See example below (modified grid sample > with the non-working code commented out). > > I am on Win XP (Home) using the latest one-click installer and wxRuby > 0.6.0[snip]> def on_button_add(event) > > @grid.set_grid_cursor(0,0) > > @grid.get_number_rows.times{|i| > val = @grid.get_cell_value(i,0) > @grid.move_cursor_down(false) if val.to_s.length > 0 > } > > # rect=@grid.cell_to_rect(10,1) > # x,y = rect.x, rect.y > # @grid.scroll(x,y) > # > # row = @grid.y_to_row(y) > # col = @grid.x_to_col(x) > #It shoud be simply: @grid.move_cursor_down_block(false) # move to last cell that has content @grid.move_cursor_down(false) # move one row down If you know the cell you want to move to (and not just the last cell with content) use: @grid.set_grid_cursor row, col @grid.scroll col, row Notice that the parameters to #scroll and #set_grid_cursor are inverted. Both are zero-based. I didn''t see #scroll in the docs, but it recieves the number of of the col and row, not the screen parameters. It will position itself so that the cell is at the bottom of the screen. You can also use; @grid.make_cell_visible col, row which will place the visible cell 3 rows from the bottom. Works for me on WinXP, wxRuby 0.6. Cheers, Assaph
Hi Assaph,> >It shoud be simply: > @grid.move_cursor_down_block(false) # move to last cell that has content > @grid.move_cursor_down(false) # move one row down > >That worked perfectly!>If you know the cell you want to move to (and not just the last cell >with content) use: > @grid.set_grid_cursor row, col > @grid.scroll col, row > >Oh, and I was passing (x,y) values to scroll instead of (col,row). That worked too ...>I didn''t see #scroll in the docs, but it recieves the number of of the >col and row, not the screen parameters. It will position itself so >that the cell is at the bottom of the screen. > >I always refer to the wxWindows documentation if I don''t find it in wxRuby docs. I guess the wxWindows docs has this wrong then ... or is it one of those places where wxRuby is different from wxWindows?>You can also use; > @grid.make_cell_visible col, row > >Ah ha ! That worked too ...>which will place the visible cell 3 rows from the bottom. >Works for me on WinXP, wxRuby 0.6. > >Thanks Assaph for offering so many ways to do so many different things.>Cheers, >Assaph > >-- shanko
Apparently Analagous Threads
- [903] branches/wxruby2/wxwidgets_282/samples: Added #!/usr/bin/env ruby to the grid sample.
- [875] trunk/wxruby2/samples: Add expanded grid sample
- wxGrid Question
- [861] trunk/wxruby2/doc/textile/grid.txtl: Added missing methods listing section
- wxGrid Event handling work around?