* GetPreferredSize() now accepts NULL arguments for "width" and "height"

* use GetFontHeight() instead of: BFont font; GetFont(&font); font.GetHeight(...)
* fixed odd unarchiving of the alignment.
* cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@15090 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2005-11-23 17:28:18 +00:00
parent a8c4b88de4
commit f97761575a
2 changed files with 206 additions and 232 deletions
+49 -87
View File
@@ -1,107 +1,69 @@
//------------------------------------------------------------------------------ /*
// Copyright (c) 2001-2002, OpenBeOS * Copyright 2001-2005, Haiku Inc.
// * Distributed under the terms of the MIT License.
// Permission is hereby granted, free of charge, to any person obtaining a *
// copy of this software and associated documentation files (the "Software"), * Authors:
// to deal in the Software without restriction, including without limitation * Frans van Nispen ([email protected])
// 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: StringView.h
// Author: Frans van Nispen ([email protected])
// Description: BStringView draw a non-editable text string
//------------------------------------------------------------------------------
#ifndef _STRING_VIEW_H #ifndef _STRING_VIEW_H
#define _STRING_VIEW_H #define _STRING_VIEW_H
// Standard Includes -----------------------------------------------------------
// System Includes -------------------------------------------------------------
#include <BeBuild.h> #include <BeBuild.h>
#include <View.h> #include <View.h>
// Project Includes ------------------------------------------------------------
// Local Includes --------------------------------------------------------------
// Local Defines ---------------------------------------------------------------
// Globals ---------------------------------------------------------------------
// BStringView class -----------------------------------------------------------
class BStringView : public BView{ class BStringView : public BView{
public: public:
BStringView(BRect bounds, const char* name, const char* text, BStringView(BRect bounds, const char* name,
uint32 resizeFlags = B_FOLLOW_LEFT | B_FOLLOW_TOP, const char* text,
uint32 flags = B_WILL_DRAW); uint32 resizeFlags = B_FOLLOW_LEFT | B_FOLLOW_TOP,
BStringView(BMessage* data); uint32 flags = B_WILL_DRAW);
virtual ~BStringView(); BStringView(BMessage* data);
static BArchivable *Instantiate(BMessage* data); virtual ~BStringView();
virtual status_t Archive(BMessage* data, bool deep = true) const;
void SetText(const char* text); static BArchivable* Instantiate(BMessage* data);
const char *Text() const; virtual status_t Archive(BMessage* data, bool deep = true) const;
void SetAlignment(alignment flag);
alignment Alignment() const;
virtual void AttachedToWindow(); void SetText(const char* text);
virtual void Draw(BRect bounds); const char* Text() const;
void SetAlignment(alignment flag);
alignment Alignment() const;
virtual void MessageReceived(BMessage* msg); virtual void AttachedToWindow();
virtual void MouseDown(BPoint pt); virtual void Draw(BRect bounds);
virtual void MouseUp(BPoint pt);
virtual void MouseMoved(BPoint pt, uint32 code, const BMessage* msg);
virtual void DetachedFromWindow();
virtual void FrameMoved(BPoint new_position);
virtual void FrameResized(float new_width, float new_height);
virtual BHandler *ResolveSpecifier( BMessage* msg, int32 index, BMessage* specifier, virtual void MessageReceived(BMessage* message);
int32 form, const char* property); virtual void MouseDown(BPoint point);
virtual void MouseUp(BPoint point);
virtual void MouseMoved(BPoint point, uint32 transit,
const BMessage* dragMessage);
virtual void DetachedFromWindow();
virtual void FrameMoved(BPoint newPosition);
virtual void FrameResized(float newWidth, float newHeight);
virtual void ResizeToPreferred(); virtual BHandler* ResolveSpecifier(BMessage* msg, int32 index,
virtual void GetPreferredSize(float* width, float* height); BMessage* specifier, int32 form,
virtual void MakeFocus(bool state = true); const char* property);
virtual void AllAttached();
virtual void AllDetached();
virtual status_t GetSupportedSuites(BMessage* data);
virtual void ResizeToPreferred();
virtual void GetPreferredSize(float* _width, float* _height);
virtual void MakeFocus(bool state = true);
virtual void AllAttached();
virtual void AllDetached();
virtual status_t GetSupportedSuites(BMessage* data);
// Private or reserved --------------------------------------------------------- private:
virtual status_t Perform(perform_code d, void* arg); virtual status_t Perform(perform_code d, void* arg);
virtual void _ReservedStringView1();
virtual void _ReservedStringView2();
virtual void _ReservedStringView3();
private: BStringView &operator=(const BStringView&);
virtual void _ReservedStringView1();
virtual void _ReservedStringView2();
virtual void _ReservedStringView3();
BStringView &operator=(const BStringView&); char* fText;
alignment fAlign;
char* fText; uint32 _reserved[3];
alignment fAlign;
uint32 _reserved[3];
}; };
//------------------------------------------------------------------------------
#endif // _STRINGVIEW_H
/*
* $Log $
*
* $Id $
*
*/
#endif // _STRING_VIEW_H
+157 -145
View File
@@ -1,151 +1,139 @@
//------------------------------------------------------------------------------ /*
// Copyright (c) 2001-2002, OpenBeOS * Copyright 2001-2005, Haiku Inc.
// * Distributed under the terms of the MIT License.
// Permission is hereby granted, free of charge, to any person obtaining a *
// copy of this software and associated documentation files (the "Software"), * Authors:
// to deal in the Software without restriction, including without limitation * Frans van Nispen ([email protected])
// 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: /** BStringView draw a non-editable text string */
//
// 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: StringView.cpp
// Author: Frans van Nispen ([email protected])
// Description: BStringView draw a non-editable text string
//------------------------------------------------------------------------------
// Standard Includes -----------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// System Includes -------------------------------------------------------------
#include <Message.h> #include <Message.h>
#include <StringView.h> #include <StringView.h>
#include <View.h> #include <View.h>
#include <Window.h> #include <Window.h>
// Project Includes ------------------------------------------------------------ #include <stdio.h>
#include <stdlib.h>
// Local Includes -------------------------------------------------------------- #include <string.h>
// Local Defines ---------------------------------------------------------------
// Globals ---------------------------------------------------------------------
//------------------------------------------------------------------------------
BStringView::BStringView(BRect frame, const char* name, const char* text, BStringView::BStringView(BRect frame, const char* name, const char* text,
uint32 resizeMask, uint32 flags) uint32 resizeMask, uint32 flags)
: BView(frame, name, resizeMask, flags) : BView(frame, name, resizeMask, flags)
{ {
fText = text ? strdup(text) : NULL; fText = text ? strdup(text) : NULL;
fAlign = B_ALIGN_LEFT; fAlign = B_ALIGN_LEFT;
} }
//------------------------------------------------------------------------------
BStringView::BStringView(BMessage* data) BStringView::BStringView(BMessage* data)
: BView(data) : BView(data)
{ {
const char* text; int32 align;
if (data->FindInt32("_align", &align) == B_OK)
if (data->FindInt32("_align",(int32&)fAlign) != B_OK) { fAlign = (alignment)align;
else
fAlign = B_ALIGN_LEFT; fAlign = B_ALIGN_LEFT;
}
if (data->FindString("_text",&text) != B_OK) { const char* text;
if (data->FindString("_text", &text) != B_OK)
text = NULL; text = NULL;
}
SetText(text); SetText(text);
} }
//------------------------------------------------------------------------------
BArchivable* BStringView::Instantiate(BMessage* data)
BArchivable*
BStringView::Instantiate(BMessage* data)
{ {
if (!validate_instantiation(data,"BStringView")) { if (!validate_instantiation(data, "BStringView"))
return NULL; return NULL;
}
return new BStringView(data); return new BStringView(data);
} }
//------------------------------------------------------------------------------
status_t BStringView::Archive(BMessage* data, bool deep) const
status_t
BStringView::Archive(BMessage* data, bool deep) const
{ {
BView::Archive(data, deep); BView::Archive(data, deep);
if (fText) { if (fText)
data->AddString("_text",fText); data->AddString("_text", fText);
}
data->AddInt32("_align", fAlign); data->AddInt32("_align", fAlign);
return B_OK; return B_OK;
} }
//------------------------------------------------------------------------------
BStringView::~BStringView() BStringView::~BStringView()
{ {
free(fText); free(fText);
} }
//------------------------------------------------------------------------------
void BStringView::SetText(const char* text)
void
BStringView::SetText(const char* text)
{ {
free(fText); free(fText);
fText = text ? strdup(text) : NULL; fText = text ? strdup(text) : NULL;
Invalidate(); Invalidate();
} }
//------------------------------------------------------------------------------
const char* BStringView::Text() const
const char*
BStringView::Text() const
{ {
return fText; return fText;
} }
//------------------------------------------------------------------------------
void BStringView::SetAlignment(alignment flag)
void
BStringView::SetAlignment(alignment flag)
{ {
fAlign = flag; fAlign = flag;
Invalidate(); Invalidate();
} }
//------------------------------------------------------------------------------
alignment BStringView::Alignment() const
alignment
BStringView::Alignment() const
{ {
return fAlign; return fAlign;
} }
//------------------------------------------------------------------------------
void BStringView::AttachedToWindow()
void
BStringView::AttachedToWindow()
{ {
if (Parent()) { if (Parent())
SetViewColor(Parent()->ViewColor()); SetViewColor(Parent()->ViewColor());
}
} }
//------------------------------------------------------------------------------
void BStringView::Draw(BRect bounds)
void
BStringView::Draw(BRect bounds)
{ {
if (!fText) if (!fText)
return; return;
SetLowColor(ViewColor()); SetLowColor(ViewColor());
BFont font;
GetFont(&font); font_height fontHeight;
font_height fh; GetFontHeight(&fontHeight);
font.GetHeight(&fh);
float y = Bounds().bottom - ceil(fontHeight.descent);
float y = Bounds().bottom - ceil(fh.descent);
float x; float x;
switch (fAlign) { switch (fAlign) {
case B_ALIGN_RIGHT: case B_ALIGN_RIGHT:
x = Bounds().Width() - font.StringWidth(fText) - 2.0f; x = Bounds().Width() - StringWidth(fText) - 2.0f;
break; break;
case B_ALIGN_CENTER: case B_ALIGN_CENTER:
x = (Bounds().Width() - font.StringWidth(fText)) / 2.0f; x = (Bounds().Width() - StringWidth(fText)) / 2.0f;
break; break;
default: default:
@@ -153,115 +141,139 @@ void BStringView::Draw(BRect bounds)
break; break;
} }
DrawString(fText, BPoint(x,y)); DrawString(fText, BPoint(x, y));
} }
//------------------------------------------------------------------------------
void BStringView::ResizeToPreferred()
void
BStringView::ResizeToPreferred()
{ {
float w, h; float width, height;
GetPreferredSize(&w, &h); GetPreferredSize(&width, &height);
BView::ResizeTo(w, h);
BView::ResizeTo(width, height);
} }
//------------------------------------------------------------------------------
void BStringView::GetPreferredSize(float* width, float* height)
void
BStringView::GetPreferredSize(float* _width, float* _height)
{ {
if (!fText) { if (!fText) {
BView::GetPreferredSize(width, height); BView::GetPreferredSize(_width, _height);
return; return;
} }
BFont font; if (_width)
GetFont(&font); *_width = 4.0f + ceil(StringWidth(fText));
font_height fh;
font.GetHeight(&fh);
*height = ceil(fh.ascent + fh.descent + fh.leading) + 2.0f; if (_height) {
*width = 4.0f + ceil(font.StringWidth(fText)); font_height fontHeight;
GetFontHeight(&fontHeight);
*_height = ceil(fontHeight.ascent + fontHeight.descent + fontHeight.leading) + 2.0f;
}
} }
//------------------------------------------------------------------------------
void BStringView::MessageReceived(BMessage* msg)
void
BStringView::MessageReceived(BMessage* message)
{ {
BView::MessageReceived(msg); BView::MessageReceived(message);
} }
//------------------------------------------------------------------------------
void BStringView::MouseDown(BPoint pt)
void
BStringView::MouseDown(BPoint point)
{ {
BView::MouseDown(pt); BView::MouseDown(point);
} }
//------------------------------------------------------------------------------
void BStringView::MouseUp(BPoint pt)
void
BStringView::MouseUp(BPoint point)
{ {
BView::MouseUp(pt); BView::MouseUp(point);
} }
//------------------------------------------------------------------------------
void BStringView::MouseMoved(BPoint pt, uint32 code, const BMessage* msg)
void
BStringView::MouseMoved(BPoint point, uint32 transit, const BMessage* msg)
{ {
BView::MouseMoved(pt, code, msg); BView::MouseMoved(point, transit, msg);
} }
//------------------------------------------------------------------------------
void BStringView::DetachedFromWindow()
void
BStringView::DetachedFromWindow()
{ {
BView::DetachedFromWindow(); BView::DetachedFromWindow();
} }
//------------------------------------------------------------------------------
void BStringView::FrameMoved(BPoint newPosition)
void
BStringView::FrameMoved(BPoint newPosition)
{ {
BView::FrameMoved(newPosition); BView::FrameMoved(newPosition);
} }
//------------------------------------------------------------------------------
void BStringView::FrameResized(float newWidth, float newHeight)
void
BStringView::FrameResized(float newWidth, float newHeight)
{ {
BView::FrameResized(newWidth, newHeight); BView::FrameResized(newWidth, newHeight);
} }
//------------------------------------------------------------------------------
BHandler* BStringView::ResolveSpecifier(BMessage* msg, int32 index,
BMessage* specifier, int32 form, BHandler*
const char* property) BStringView::ResolveSpecifier(BMessage* msg, int32 index,
BMessage* specifier, int32 form, const char* property)
{ {
return NULL; return NULL;
} }
//------------------------------------------------------------------------------
void BStringView::MakeFocus(bool state)
void
BStringView::MakeFocus(bool state)
{ {
BView::MakeFocus(state); BView::MakeFocus(state);
} }
//------------------------------------------------------------------------------
void BStringView::AllAttached()
void
BStringView::AllAttached()
{ {
BView::AllAttached(); BView::AllAttached();
} }
//------------------------------------------------------------------------------
void BStringView::AllDetached()
void
BStringView::AllDetached()
{ {
BView::AllDetached(); BView::AllDetached();
} }
//------------------------------------------------------------------------------
status_t BStringView::GetSupportedSuites(BMessage* data)
status_t
BStringView::GetSupportedSuites(BMessage* data)
{ {
return B_OK; return B_OK;
} }
//------------------------------------------------------------------------------
status_t BStringView::Perform(perform_code d, void* arg)
status_t
BStringView::Perform(perform_code d, void* arg)
{ {
return B_ERROR; return B_ERROR;
} }
//------------------------------------------------------------------------------
void BStringView::_ReservedStringView1()
void BStringView::_ReservedStringView1() {}
void BStringView::_ReservedStringView2() {}
void BStringView::_ReservedStringView3() {}
BStringView&
BStringView::operator=(const BStringView&)
{ {
} // Assignment not allowed (private)
//------------------------------------------------------------------------------
void BStringView::_ReservedStringView2()
{
}
//------------------------------------------------------------------------------
void BStringView::_ReservedStringView3()
{
}
//------------------------------------------------------------------------------
BStringView& BStringView::operator=(const BStringView&)
{
// Assignment not allowed
return *this; return *this;
} }
//------------------------------------------------------------------------------