* Fixed BBox resizing when not attached.

* Less flickering when drawing the label: the area of the label is now
  clipped, so there is no need to fill the background again.
* Consumed the last reserved member for the bounding box of the label.
* More or less rewrote the header.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@15747 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2005-12-30 18:23:48 +00:00
parent be950af4df
commit f17cfabc82
2 changed files with 145 additions and 170 deletions

View File

@ -1,126 +1,78 @@
//------------------------------------------------------------------------------
// Copyright (c) 2001-2002, OpenBeOS
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// File Name: Box.h
// Author: Marc Flerackers (mflerackers@androme.be)
// Description: BBox objects group views together and draw a border
// around them.
//------------------------------------------------------------------------------
/*
* Copyright 2005, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _BOX_H
#define _BOX_H
// Standard Includes -----------------------------------------------------------
// System Includes -------------------------------------------------------------
#include <BeBuild.h>
#include <View.h>
// Project Includes ------------------------------------------------------------
// Local Includes --------------------------------------------------------------
// Local Defines ---------------------------------------------------------------
// Globals ---------------------------------------------------------------------
// BBox class ------------------------------------------------------------------
class BBox : public BView {
public:
BBox(BRect frame,
const char *name = NULL,
uint32 resizingMode = B_FOLLOW_LEFT | B_FOLLOW_TOP,
uint32 flags = B_WILL_DRAW | B_FRAME_EVENTS |
B_NAVIGABLE_JUMP,
border_style border = B_FANCY_BORDER);
virtual ~BBox();
/* Archiving */
BBox(BMessage *archive);
static BArchivable *Instantiate(BMessage *archive);
virtual status_t Archive(BMessage *archive, bool deep = true) const;
virtual void SetBorder(border_style border);
border_style Border() const;
void SetLabel(const char *string);
status_t SetLabel(BView *viewLabel);
const char *Label() const;
BView *LabelView() const;
virtual void Draw(BRect updateRect);
virtual void AttachedToWindow();
virtual void DetachedFromWindow();
virtual void AllAttached();
virtual void AllDetached();
virtual void FrameResized(float width, float height);
virtual void MessageReceived(BMessage *message);
virtual void MouseDown(BPoint point);
virtual void MouseUp(BPoint point);
virtual void WindowActivated(bool active);
virtual void MouseMoved(BPoint point, uint32 transit,
const BMessage *message);
virtual void FrameMoved(BPoint newLocation);
virtual BHandler *ResolveSpecifier(BMessage *message,
int32 index,
BMessage *specifier,
int32 what,
const char *property);
virtual void ResizeToPreferred();
virtual void GetPreferredSize(float *width, float *height);
virtual void MakeFocus(bool focused = true);
virtual status_t GetSupportedSuites(BMessage *message);
virtual status_t Perform(perform_code d, void *arg);
private:
virtual void _ReservedBox1();
virtual void _ReservedBox2();
BBox &operator=(const BBox &);
void InitObject(BMessage *data = NULL);
void DrawPlain();
void DrawFancy();
void ClearAnyLabel();
char *fLabel;
BRect fBounds;
border_style fStyle;
BView *fLabelView;
uint32 _reserved[1];
};
//------------------------------------------------------------------------------
#endif // _BOX_H
/*
* $Log $
*
* $Id $
*
*/
class BBox : public BView {
public:
BBox(BRect frame, const char *name = NULL,
uint32 resizingMode = B_FOLLOW_LEFT | B_FOLLOW_TOP,
uint32 flags = B_WILL_DRAW | B_FRAME_EVENTS
| B_NAVIGABLE_JUMP,
border_style border = B_FANCY_BORDER);
virtual ~BBox();
/* Archiving */
BBox(BMessage* archive);
static BArchivable* Instantiate(BMessage* archive);
virtual status_t Archive(BMessage* archive, bool deep = true) const;
virtual void SetBorder(border_style border);
border_style Border() const;
void SetLabel(const char* string);
status_t SetLabel(BView* viewLabel);
const char* Label() const;
BView* LabelView() const;
virtual void Draw(BRect updateRect);
virtual void AttachedToWindow();
virtual void DetachedFromWindow();
virtual void AllAttached();
virtual void AllDetached();
virtual void FrameResized(float width, float height);
virtual void MessageReceived(BMessage* message);
virtual void MouseDown(BPoint point);
virtual void MouseUp(BPoint point);
virtual void WindowActivated(bool active);
virtual void MouseMoved(BPoint point, uint32 transit,
const BMessage* dragMessage);
virtual void FrameMoved(BPoint newLocation);
virtual BHandler* ResolveSpecifier(BMessage* message,
int32 index, BMessage* specifier,
int32 what, const char* property);
virtual void ResizeToPreferred();
virtual void GetPreferredSize(float* _width, float* _height);
virtual void MakeFocus(bool focused = true);
virtual status_t GetSupportedSuites(BMessage* message);
virtual status_t Perform(perform_code d, void* arg);
private:
virtual void _ReservedBox1();
virtual void _ReservedBox2();
BBox &operator=(const BBox &);
void _InitObject(BMessage* data = NULL);
void _DrawPlain(BRect labelBox);
void _DrawFancy(BRect labelBox);
void _ClearLabel();
char* fLabel;
BRect fBounds;
border_style fStyle;
BView* fLabelView;
BRect* fLabelBox;
};
#endif // _BOX_H

View File

@ -6,38 +6,35 @@
* Marc Flerackers (mflerackers@androme.be)
* Stephan Aßmus <superstippi@gmx.de>
* DarkWyrm <bpmagic@columbus.rr.com>
* Axel Dörfler, axeld@pinc-software.de
*/
#include <Box.h>
#include <Message.h>
#include <Region.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Box.h>
#include <Message.h>
BBox::BBox(BRect frame, const char *name, uint32 resizingMode, uint32 flags,
border_style border)
: BView(frame, name, resizingMode, flags | B_FRAME_EVENTS),
border_style border)
: BView(frame, name, resizingMode, flags | B_FRAME_EVENTS),
fStyle(border)
{
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
SetLowColor(ui_color(B_PANEL_BACKGROUND_COLOR));
InitObject();
}
BBox::~BBox()
{
ClearAnyLabel();
_InitObject();
}
BBox::BBox(BMessage *archive)
: BView(archive)
: BView(archive)
{
InitObject(archive);
_InitObject(archive);
const char *string;
@ -57,13 +54,19 @@ BBox::BBox(BMessage *archive)
}
BBox::~BBox()
{
_ClearLabel();
}
BArchivable *
BBox::Instantiate(BMessage *archive)
{
if (validate_instantiation(archive, "BBox"))
return new BBox(archive);
else
return NULL;
return NULL;
}
@ -107,17 +110,18 @@ BBox::Border() const
void
BBox::SetLabel(const char *string)
{
ClearAnyLabel();
_ClearLabel();
if (string) {
// Update fBounds
fBounds = Bounds();
font_height fh;
GetFontHeight(&fh);
fBounds.top = (float)ceil((fh.ascent + fh.descent) / 2.0f);
font_height fontHeight;
GetFontHeight(&fontHeight);
fLabel = strdup(string);
// leave 6 pixels of the frame, and have a gap of 4 pixels between
// the frame and the text on both sides
fLabelBox = new BRect(6.0f, 0, StringWidth(string) + 14.0f,
ceilf(fontHeight.ascent + fontHeight.descent));
}
if (Window())
@ -128,14 +132,9 @@ BBox::SetLabel(const char *string)
status_t
BBox::SetLabel(BView *viewLabel)
{
ClearAnyLabel();
_ClearLabel();
if (viewLabel) {
// Update fBounds
fBounds = Bounds();
fBounds.top = ceilf(viewLabel->Bounds().Height() / 2.0f);
fLabelView = viewLabel;
fLabelView->MoveTo(10.0f, 0.0f);
AddChild(fLabelView, ChildAt(0));
@ -165,41 +164,61 @@ BBox::LabelView() const
void
BBox::Draw(BRect updateRect)
{
PushState();
BRect labelBox = BRect(0, 0, 0, 0);
if (fLabel != NULL) {
labelBox = *fLabelBox;
BRegion update(updateRect);
update.Exclude(labelBox);
ConstrainClippingRegion(&update);
} else if (fLabelView != NULL)
labelBox = fLabelView->Bounds();
switch (fStyle) {
case B_FANCY_BORDER:
DrawFancy();
_DrawFancy(labelBox);
break;
case B_PLAIN_BORDER:
DrawPlain();
_DrawPlain(labelBox);
break;
default:
break;
}
if (fLabel) {
font_height fh;
GetFontHeight(&fh);
if (fLabel) {
ConstrainClippingRegion(NULL);
font_height fontHeight;
GetFontHeight(&fontHeight);
/*
SetHighColor(ViewColor());
FillRect(BRect(6.0f, 1.0f, 12.0f + StringWidth(fLabel),
(float)ceil(fh.ascent + fh.descent))/*, B_SOLID_LOW*/);
(float)ceil(fh.ascent + fh.descent)), B_SOLID_LOW);
*/
SetHighColor(0, 0, 0);
DrawString(fLabel, BPoint(10.0f, (float)ceil(fh.ascent - fh.descent)
+ 1.0f));
DrawString(fLabel, BPoint(10.0f,
ceilf(fontHeight.ascent - fontHeight.descent) + 1.0f));
}
PopState();
}
void BBox::AttachedToWindow()
void
BBox::AttachedToWindow()
{
if (Parent()) {
SetViewColor(Parent()->ViewColor());
SetLowColor(Parent()->ViewColor());
}
// The box could have been resized in the mean time
fBounds = Bounds();
}
@ -313,8 +332,8 @@ BBox::FrameMoved(BPoint newLocation)
BHandler *
BBox::ResolveSpecifier(BMessage *message, int32 index,
BMessage *specifier, int32 what,
const char *property)
BMessage *specifier, int32 what,
const char *property)
{
return BView::ResolveSpecifier(message, index, specifier, what, property);
}
@ -414,7 +433,7 @@ BBox::operator=(const BBox &)
void
BBox::InitObject(BMessage *data)
BBox::_InitObject(BMessage *data)
{
fLabel = NULL;
fBounds = Bounds();
@ -436,9 +455,10 @@ BBox::InitObject(BMessage *data)
void
BBox::DrawPlain()
BBox::_DrawPlain(BRect labelBox)
{
BRect r = fBounds;
BRect r = Bounds();
r.top += labelBox.Height() / 2.0f;
rgb_color light = tint_color(ViewColor(), B_LIGHTEN_MAX_TINT);
rgb_color shadow = tint_color(ViewColor(), B_DARKEN_3_TINT);
@ -457,9 +477,10 @@ BBox::DrawPlain()
void
BBox::DrawFancy()
BBox::_DrawFancy(BRect labelBox)
{
BRect r = fBounds;
BRect r = Bounds();
r.top += labelBox.Height() / 2.0f;
rgb_color light = tint_color(ViewColor(), B_LIGHTEN_MAX_TINT);
rgb_color shadow = tint_color(ViewColor(), B_DARKEN_3_TINT);
@ -489,13 +510,15 @@ BBox::DrawFancy()
void
BBox::ClearAnyLabel()
BBox::_ClearLabel()
{
fBounds.top = 0;
if (fLabel) {
delete fLabelBox;
free(fLabel);
fLabel = NULL;
fLabelBox = NULL;
} else if (fLabelView) {
fLabelView->RemoveSelf();
delete fLabelView;