From 68dfaf0f9d49a713685e93133b37c249535b62d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Thu, 5 Sep 2013 13:26:15 +0200 Subject: [PATCH] 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. --- src/apps/haiku-depot/Jamfile | 34 ++++-- .../haiku-depot/textview/TextDocumentTest.cpp | 75 +++++++++++++ .../haiku-depot/textview/TextDocumentTest.h | 21 ++++ .../haiku-depot/textview/TextDocumentView.cpp | 105 ++++++++++++++++++ .../haiku-depot/textview/TextDocumentView.h | 41 +++++++ 5 files changed, 265 insertions(+), 11 deletions(-) create mode 100644 src/apps/haiku-depot/textview/TextDocumentTest.cpp create mode 100644 src/apps/haiku-depot/textview/TextDocumentTest.h create mode 100644 src/apps/haiku-depot/textview/TextDocumentView.cpp create mode 100644 src/apps/haiku-depot/textview/TextDocumentView.h diff --git a/src/apps/haiku-depot/Jamfile b/src/apps/haiku-depot/Jamfile index 22335f5c30..13b48f1db4 100644 --- a/src/apps/haiku-depot/Jamfile +++ b/src/apps/haiku-depot/Jamfile @@ -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++) +; diff --git a/src/apps/haiku-depot/textview/TextDocumentTest.cpp b/src/apps/haiku-depot/textview/TextDocumentTest.cpp new file mode 100644 index 0000000000..292b19b1e7 --- /dev/null +++ b/src/apps/haiku-depot/textview/TextDocumentTest.cpp @@ -0,0 +1,75 @@ +/* + * Copyright 2013, Stephan Aßmus . + * All rights reserved. Distributed under the terms of the MIT License. + */ + +#include "TextDocumentTest.h" + +#include + +#include +#include + +#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; +} + + diff --git a/src/apps/haiku-depot/textview/TextDocumentTest.h b/src/apps/haiku-depot/textview/TextDocumentTest.h new file mode 100644 index 0000000000..1fd05c1014 --- /dev/null +++ b/src/apps/haiku-depot/textview/TextDocumentTest.h @@ -0,0 +1,21 @@ +/* + * Copyright 2013, Stephan Aßmus . + * All rights reserved. Distributed under the terms of the MIT License. + */ +#ifndef TEXT_DOCUMENT_TEST_H +#define TEXT_DOCUMENT_TEST_H + + +#include + + +class TextDocumentTest : public BApplication { +public: + TextDocumentTest(); + virtual ~TextDocumentTest(); + + virtual void ReadyToRun(); +}; + + +#endif // TEXT_DOCUMENT_TEST_H diff --git a/src/apps/haiku-depot/textview/TextDocumentView.cpp b/src/apps/haiku-depot/textview/TextDocumentView.cpp new file mode 100644 index 0000000000..c344f10efe --- /dev/null +++ b/src/apps/haiku-depot/textview/TextDocumentView.cpp @@ -0,0 +1,105 @@ +/* + * Copyright 2013, Stephan Aßmus . + * 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(); +} + diff --git a/src/apps/haiku-depot/textview/TextDocumentView.h b/src/apps/haiku-depot/textview/TextDocumentView.h new file mode 100644 index 0000000000..0e7f57c4d6 --- /dev/null +++ b/src/apps/haiku-depot/textview/TextDocumentView.h @@ -0,0 +1,41 @@ +/* + * Copyright 2013, Stephan Aßmus . + * All rights reserved. Distributed under the terms of the MIT License. + */ +#ifndef TEXT_DOCUMENT_VIEW_H +#define TEXT_DOCUMENT_VIEW_H + +#include +#include + +#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