Post by Robin DunnYou can use wxSystemSettings::GetMetric to find the
width of the scrollbars
Thanks, this is one piece of the puzzle I was missing.
After some thought and much experimentation, I have a
solution that works for me on MSW. I have a feeling it
will need revision to account for other uses, but I
might as well post what I've done here in case it
helps someone else.
The problem to be solved was limiting the size of a
wxFrame containing a wxScrolledWindow. This is what I
came up with:
In the wxFrame subclass, I overrode two methods:
void MainWindow::Fit( void )
{
// Set the maximum size to something (hopefully)
impossibly large so our
// resize isn't limited.
SetSizeHints( -1, -1, 10000, 10000 );
// Scan the children, but ignore the first one,
since it is the menu and
// it imposes an extra long length.
int width = 0, height = 0, maxWidthDiff = 0,
maxHeightDiff = 0;
for( wxWindowList::Node* node = GetChildren(
).GetFirst( )->GetNext( );
node; node = node->GetNext( ) ) {
wxSize size = node->GetData( )->GetSize( );
if( size.GetWidth( ) > width ) {
width = size.GetWidth( );
maxWidthDiff = node->GetData(
)->GetMaxWidth( ) - width;
}
if( size.GetHeight( ) > height ) {
height = size.GetHeight( );
maxHeightDiff = node->GetData(
)->GetMaxHeight( ) - height;
}
}
SetClientSize( wxSize( width, height ) );
// Re-load the actual size including title bar,
menu, etc.
GetSize( &width, &height );
int desktopX, desktopY, desktopWidth,
desktopHeight;
wxClientDisplayRect( &desktopX, &desktopY,
&desktopWidth,
&desktopHeight );
if( width > desktopWidth ) width = desktopWidth;
if( height > desktopHeight ) height =
desktopHeight;
SetSize( wxSize( width, height ) );
SetSizeHints( -1, -1, width + maxWidthDiff, height
+ maxHeightDiff );
int windowX, windowY;
GetPosition( &windowX, &windowY );
if( windowX + width > desktopX + desktopWidth ) {
windowX = desktopX + desktopWidth - width;
}
if( windowY + height > desktopY + desktopHeight )
{
windowY = desktopY + desktopHeight - height;
}
Move( windowX, windowY );
}
void MainWindow::SetSizeHints( int minW, int minH, int
maxW, int maxH,
int incW, int incH )
{
bool setMaxW = maxW == -1;
bool setMaxH = maxH == -1;
if( setMaxW || setMaxH ) {
if( setMaxW ) {
maxW = GetSize( ).GetWidth( );
}
if( setMaxH ) {
maxH = GetSize( ).GetHeight( );
}
for( wxWindowList::Node* node = GetChildren(
).GetFirst( )->GetNext( );
node; node = node->GetNext( ) ) {
wxSize size = node->GetData( )->GetSize(
);
if( setMaxW ) {
maxW += node->GetData( )->GetMaxWidth(
) - size.GetWidth( );
}
if( setMaxH ) {
maxH += node->GetData(
)->GetMaxHeight( ) - size.GetHeight( );
}
}
}
wxFrame::SetSizeHints( minW, minH, maxW, maxH,
incW, incH );
}
And then, in my wxScrolledWindow subclass, I used the
following to set the client size:
void showSome( int width, int
height )
{
_width = width;
_height = height;
SetVirtualSize( width, height );
SetScrollbars( 1, 1, width, height );
SetSizeHints( -1, -1, width, height );
SetClientSize( width, height );
GetParent( )->Fit( );
}
Finally, the real trick was adding a handler for the
resizing event in the wxScrolledWindow:
void handleSize( wxSizeEvent&
event )
{
if( _width && _height ) {
// Handle a bunch of special cases
involving scroll bars appearing
// or disappearing.
int width, height;
if( GetSize( ).GetWidth( ) >= _width &&
GetSize( ).GetHeight( ) >= _height
) {
SetScrollbar( wxVERTICAL, 0, 0, 0,
FALSE );
SetScrollbar( wxHORIZONTAL, 0, 0, 0,
FALSE );
}
if( GetClientSize( ).GetWidth( ) < _width
) {
height = _height +
wxSystemSettings::GetMetric( wxSYS_HSCROLL_Y );
} else {
height = _height;
}
if( GetClientSize( ).GetHeight( ) <
_height ) {
width = _width +
wxSystemSettings::GetMetric( wxSYS_VSCROLL_X );
} else {
width = _width;
}
SetSizeHints( -1, -1, width, height );
GetParent( )->SetSizeHints( -1, -1 );
}
event.Skip( );
}
Phew! There really should be an easier way to do this,
but looking at what I've done, I'm not exactly sure
how the changes would be incorporated into the
library. Anyway, hope this helps someone!
Kris
__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
---------------------------------------------------------------------
Please read http://www.wxwindows.org/mlhowto.htm before posting.
To unsubscribe, e-mail: wx-users-***@lists.wxwindows.org
For additional commands, e-mail: wx-users-***@lists.wxwindows.org