HaikuDepot: New TextDocumentView and test app.

* TextDocumentView still only shows the first paragraph
   of a TextDocument, but this time using the new
   ParagraphLayout. A class for layouting all paragraphs
   of a TextDocument is up next.
 * A simple test app shows that TextSpans with different
   CharacterStyles already work in one Paragraph. The test
   is nowhere extensive and does not test for bugs in
   corner cases.
This commit is contained in:
Stephan Aßmus
2013-09-05 13:26:15 +02:00
parent 24523b867b
commit 68dfaf0f9d
5 changed files with 265 additions and 11 deletions
+23 -11
View File
@@ -12,6 +12,21 @@ for sourceDir in $(sourceDirs) {
SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src apps haiku-depot $(sourceDir) ] ;
}
local textDocumentSources =
CharacterStyle.cpp
CharacterStyleData.cpp
Paragraph.cpp
ParagraphLayout.cpp
ParagraphStyle.cpp
ParagraphStyleData.cpp
TextDocument.cpp
TextDocumentView.cpp
TextLayout.cpp
TextSpan.cpp
TextStyle.cpp
TextView.cpp
;
Application HaikuDepot :
App.cpp
BitmapButton.cpp
@@ -27,17 +42,7 @@ Application HaikuDepot :
support.cpp
# textview stuff
CharacterStyle.cpp
CharacterStyleData.cpp
Paragraph.cpp
ParagraphLayout.cpp
ParagraphStyle.cpp
ParagraphStyleData.cpp
TextDocument.cpp
TextLayout.cpp
TextSpan.cpp
TextStyle.cpp
TextView.cpp
$(textDocumentSources)
: be translation libcolumnlistview.a libshared.a $(TARGET_LIBSUPC++)
$(HAIKU_LOCALE_LIBS)
@@ -55,3 +60,10 @@ DoCatalogs HaikuDepot :
PackageListView.cpp
PackageManager.cpp
;
Application TextDocumentTest :
TextDocumentTest.cpp
$(textDocumentSources)
: be translation libshared.a $(TARGET_LIBSUPC++)
;
@@ -0,0 +1,75 @@
/*
* Copyright 2013, Stephan Aßmus <[email protected]>.
* All rights reserved. Distributed under the terms of the MIT License.
*/
#include "TextDocumentTest.h"
#include <stdio.h>
#include <LayoutBuilder.h>
#include <Window.h>
#include "TextDocumentView.h"
TextDocumentTest::TextDocumentTest()
:
BApplication("application/x-vnd.Haiku-TextDocumentTest")
{
}
TextDocumentTest::~TextDocumentTest()
{
}
void
TextDocumentTest::ReadyToRun()
{
BRect frame(50.0, 50.0, 749.0, 549.0);
BWindow* window = new BWindow(frame, "Text document test",
B_TITLED_WINDOW, B_QUIT_ON_WINDOW_CLOSE | B_AUTO_UPDATE_SIZE_LIMITS);
TextDocumentView* documentView = new TextDocumentView("text document view");
BLayoutBuilder::Group<>(window, B_VERTICAL)
.Add(documentView)
;
ParagraphStyle paragraphStyle;
CharacterStyle regularStyle;
CharacterStyle boldStyle(regularStyle);
boldStyle.SetFont(BFont(be_bold_font));
CharacterStyle bigStyle(regularStyle);
bigStyle.SetFontSize(24);
bigStyle.SetForegroundColor(255, 50, 50);
Paragraph paragraph(paragraphStyle);
paragraph.Append(TextSpan("This is a ", regularStyle));
paragraph.Append(TextSpan("test", bigStyle));
paragraph.Append(TextSpan(" to see if ", regularStyle));
paragraph.Append(TextSpan("different", boldStyle));
paragraph.Append(TextSpan(" character styles already work.", regularStyle));
TextDocumentRef document(new TextDocument(), true);
document->Append(paragraph);
documentView->SetTextDocument(document);
window->Show();
}
int
main(int argc, char* argv[])
{
TextDocumentTest().Run();
return 0;
}
@@ -0,0 +1,21 @@
/*
* Copyright 2013, Stephan Aßmus <[email protected]>.
* All rights reserved. Distributed under the terms of the MIT License.
*/
#ifndef TEXT_DOCUMENT_TEST_H
#define TEXT_DOCUMENT_TEST_H
#include <Application.h>
class TextDocumentTest : public BApplication {
public:
TextDocumentTest();
virtual ~TextDocumentTest();
virtual void ReadyToRun();
};
#endif // TEXT_DOCUMENT_TEST_H
@@ -0,0 +1,105 @@
/*
* Copyright 2013, Stephan Aßmus <[email protected]>.
* All rights reserved. Distributed under the terms of the MIT License.
*/
#include "TextDocumentView.h"
TextDocumentView::TextDocumentView(const char* name)
:
BView(name, B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE | B_FRAME_EVENTS)
{
fParagraphLayout.SetWidth(Bounds().Width());
SetViewColor(B_TRANSPARENT_COLOR);
// SetLowColor(ui_color(B_PANEL_BACKGROUND_COLOR));
SetLowColor(255, 255, 255, 255);
}
TextDocumentView::~TextDocumentView()
{
}
void
TextDocumentView::Draw(BRect updateRect)
{
FillRect(updateRect, B_SOLID_LOW);
fParagraphLayout.SetWidth(Bounds().Width());
fParagraphLayout.Draw(this, B_ORIGIN);
}
void
TextDocumentView::AttachedToWindow()
{
BView* parent = Parent();
if (parent != NULL)
SetLowColor(parent->ViewColor());
}
void
TextDocumentView::FrameResized(float width, float height)
{
fParagraphLayout.SetWidth(width);
}
BSize
TextDocumentView::MinSize()
{
return BSize(50.0f, 0.0f);
}
BSize
TextDocumentView::MaxSize()
{
return BSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED);
}
BSize
TextDocumentView::PreferredSize()
{
return BSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED);
}
bool
TextDocumentView::HasHeightForWidth()
{
return true;
}
void
TextDocumentView::GetHeightForWidth(float width, float* min, float* max,
float* preferred)
{
ParagraphLayout layout(fParagraphLayout);
layout.SetWidth(width);
float height = layout.Height() + 1;
if (min != NULL)
*min = height;
if (max != NULL)
*max = height;
if (preferred != NULL)
*preferred = height;
}
void
TextDocumentView::SetTextDocument(const TextDocumentRef& document)
{
fTextDocument = document;
fParagraphLayout.SetParagraph(fTextDocument->ParagraphAt(0));
InvalidateLayout();
}
@@ -0,0 +1,41 @@
/*
* Copyright 2013, Stephan Aßmus <[email protected]>.
* All rights reserved. Distributed under the terms of the MIT License.
*/
#ifndef TEXT_DOCUMENT_VIEW_H
#define TEXT_DOCUMENT_VIEW_H
#include <String.h>
#include <View.h>
#include "ParagraphLayout.h"
#include "TextDocument.h"
class TextDocumentView : public BView {
public:
TextDocumentView(const char* name = NULL);
virtual ~TextDocumentView();
virtual void Draw(BRect updateRect);
virtual void AttachedToWindow();
virtual void FrameResized(float width, float height);
virtual BSize MinSize();
virtual BSize MaxSize();
virtual BSize PreferredSize();
virtual bool HasHeightForWidth();
virtual void GetHeightForWidth(float width, float* min,
float* max, float* preferred);
void SetTextDocument(
const TextDocumentRef& document);
private:
TextDocumentRef fTextDocument;
ParagraphLayout fParagraphLayout;
};
#endif // TEXT_DOCUMENT_VIEW_H