1/* 2 * Copyright 2007, Michael Pfeiffer, laplace@users.sourceforge.net. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5#include "CenteredViewContainer.h" 6 7CenteredViewContainer::CenteredViewContainer(BView *target, BRect frame, const char* name, uint32 resizingMode) 8 : BView(frame, name, resizingMode, B_WILL_DRAW | B_FRAME_EVENTS) 9 , fTarget(target) 10{ 11 SetViewColor(B_TRANSPARENT_COLOR); 12 // to avoid flickering 13 AddChild(fTarget); 14 _CenterTarget(frame.Width(), frame.Height()); 15} 16 17CenteredViewContainer::~CenteredViewContainer() 18{ 19} 20 21void 22CenteredViewContainer::Draw(BRect updateRect) 23{ 24 FillRect(updateRect); 25} 26 27void 28CenteredViewContainer::FrameResized(float width, float height) 29{ 30 BView::FrameResized(width, height); 31 _CenterTarget(width, height); 32} 33 34void CenteredViewContainer::_CenterTarget(float width, float height) 35{ 36 float size = width < height ? width : height; 37 float left = floor((width - size) / 2); 38 float top = floor((height - size) / 2); 39 fTarget->MoveTo(left, top); 40 fTarget->ResizeTo(size, size); 41 fTarget->FrameResized(size, size); 42 // in BeOS R5 BView::FrameResized is not (always) called automatically 43 // after ResizeTo() 44} 45