Hi Doug,

Your method is 'smart' in that you could extend it to write out whole words 
only and maybe add some indication that all the text is not shown. I've seen 
code that does this - but again it is probably C++ code that I'm 
mis-remembering. It should be much easier in Perl anyway.

You can also achieve a 'dumb' write by using graphics methods alone:

$dc->DestroyClippingRegion;
$dc->SetClippingRegion(Wx::Region->new($rect));
$dc->DrawText($string, $rect->x, $rect->y );
$dc->DestroyClippingRegion;

Again, lifted from the wxWidgets C++ code.

The $dc gets passed to other cell renderers in the grid so always destroy 
clipping regions before you exit.


Regards

Mark




Doug Breshears wrote:
Thanks Mark for the nudge. Here is the new (Working method)

sub Draw {
   debug "Renderer:Draw\n";
 my( $self, $grid, $attr, $dc, $rect, $row, $col, $sel ) = ( shift, @_ );

 $self->SUPER::Draw( @_ );

 $dc->SetPen( wxBLACK_PEN );
 $dc->SetBrush( wxWHITE_BRUSH );
 $dc->SetFont(Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT ));
 my $dbvalue = $grid->GetCellValue( $row, $col );
 debug "Got Value of :$dbvalue:\n";
 my $textvalue = $self->get_text_from_id($dbvalue);
 debug "New Text is :$textvalue:\n";
 # Get the cell width so we can compare to the text.
 my $cellwidth = $rect->width;
 # Get the text properties (only interested in width ($w)
 my($w,$h,$d,$l) = $dc->GetTextExtent($textvalue);
 my $newtextval = $textvalue;

 # Loop while chopping off the text to get it down to a length
 # that will fit in the cell.
 while($w > $cellwidth){
     chop $newtextval;
       ($w,$h,$d,$l) = $dc->GetTextExtent($newtextval);
 }

 $dc->DrawText( $newtextval, $rect->x, $rect->y );
}


Doug Breshears wrote:
It's funny but no the demo did not cover this.

So, I use $dc->GetTextExtents($string) to get the size of the string, then compare it with $rect to see if it is going to overflow, then if it will what do I do? Just start chopping off one char in a loop and checking to see if it fits?

Thanks for the reply.
Doug Breshears

Mark Dootson wrote:
Hi,
I know very little about wxGrid, but have used wxDC and related objects a fair bit.

Therefore something jumps out from the code.

Using $grid->SetCellOverflow( $row, $col,0) should be used outside your subroutine to set the extents of the $rect passed in and it is your job in a custom renderer to draw the text within the bounds described by $rect.

use

$dc->GetTextExtents

to decide how to draw your text and how much of it to output.


As I say, I'm not familiar with wxGrid, so maybe it has some special properties, but if using a custom renderer in wxGrid is like any other custom rendering, I think all the work is down to you.

Is there not an example in Wx::Demo that does something exactly like this?

Cheers

Mark






Doug Breshears wrote:
Hi everybody,

I have a grid with a custom cell combo editor/renderer.
The Editor works fine, the renderer however is allowing the text to overflow the cell boundaries even when the SetCellOverflow is set to False.
   $grid->SetCellOverflow( $row, $col,0);
The other cells w/o custom renderer's work fine and properly cut the text off when it is to big for the cell.

Below is the "Draw" method for my custom renderer. It is pretty much a copy of the examples with extra combo box functionality thrown in. If anyone has a clue as to what is going on I would appreciate some assistance. (I have also set SetDefaultCellOverflow(FALSE) in the creation of the grid.)

sub Draw {
debug "Renderer:Draw\n";
my( $self, $grid, $attr, $dc, $rect, $row, $col, $sel ) = ( shift, @_ );
 $self->SUPER::Draw( @_ );
 $dc->SetPen( wxBLACK_PEN );
 $dc->SetBrush( wxWHITE_BRUSH );
 $dc->SetFont(Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT ));
 my $dbvalue = $grid->GetCellValue( $row, $col );
 $grid->SetCellOverflow( $row, $col,0);
 debug "Got Value of :$dbvalue:\n";
 my $textvalue = $self->get_text_from_id($dbvalue);
 debug "New Text is :$textvalue:\n";
 $dc->DrawText( $textvalue, $rect->x, $rect->y );
}




__________ NOD32 3055 (20080425) Information __________

This message was checked by NOD32 antivirus system.
http://www.eset.com








Reply via email to