xref: /haiku/src/tests/kits/interface/layout/widget_layout_test/tests/TextViewTest.cpp (revision 6152a5bc58737936fcfd39eb4aa0459db1c5fea8)
1 /*
2  * Copyright 2008, Stephan Aßmus <superstippi@gmx.de>.
3  * Copyright 2007, Ingo Weinhold <bonefish@cs.tu-berlin.de>.
4  * All rights reserved. Distributed under the terms of the MIT License.
5  */
6 
7 #include "TextViewTest.h"
8 
9 #include <TextView.h>
10 
11 #include "CheckBox.h"
12 #include "GroupView.h"
13 #include "TestView.h"
14 
15 
16 // messages
17 enum {
18 	MSG_UPDATE_INSETS			= 'upin',
19 	MSG_UPDATE_TEXT				= 'uptx',
20 	MSG_UPDATE_FONT				= 'upfn',
21 };
22 
23 
24 static const char* kNormalText = "Some text to get something on the screen.\n"
25 	"There is even a line break to see that as well.";
26 static const char* kAlternativeText = "This is some different Text to "
27 	"check out what happens when the text changes.";
28 
29 
TextViewTest()30 TextViewTest::TextViewTest()
31 	: Test("TextView", NULL),
32 
33 	  fTextView(new BTextView("text view")),
34 
35 //	  fTextView(new BTextView(BRect(0, 0, 49, 49), "name",
36 //	  	BRect(0, 0, 49, 49), B_FOLLOW_NONE, B_WILL_DRAW | B_PULSE_NEEDED
37 //	  		| B_SUPPORTS_LAYOUT)),
38 
39 
40 	  fUseInsetsCheckBox(NULL),
41 	  fTextCheckBox(NULL),
42 	  fFontCheckBox(NULL)
43 {
44 	SetView(fTextView);
45 	fTextView->SetText(kNormalText);
46 }
47 
48 
49 Test*
CreateTest()50 TextViewTest::CreateTest()
51 {
52 	return new TextViewTest;
53 }
54 
55 
56 void
ActivateTest(View * controls)57 TextViewTest::ActivateTest(View* controls)
58 {
59 	GroupView* group = new GroupView(B_VERTICAL);
60 	group->SetFrame(controls->Bounds());
61 	group->SetSpacing(0, 4);
62 	controls->AddChild(group);
63 
64 	// insets
65 	fUseInsetsCheckBox = new LabeledCheckBox("Use text rect insets",
66 		new BMessage(MSG_UPDATE_INSETS), this);
67 	group->AddChild(fUseInsetsCheckBox);
68 
69 	// text
70 	fTextCheckBox = new LabeledCheckBox("Use alternative text",
71 		new BMessage(MSG_UPDATE_TEXT), this);
72 	group->AddChild(fTextCheckBox);
73 
74 	// font
75 	fFontCheckBox = new LabeledCheckBox("Use large font",
76 		new BMessage(MSG_UPDATE_FONT), this);
77 	group->AddChild(fFontCheckBox);
78 
79 	// glue
80 	group->AddChild(new Glue());
81 }
82 
83 
84 void
DectivateTest()85 TextViewTest::DectivateTest()
86 {
87 }
88 
89 
90 void
MessageReceived(BMessage * message)91 TextViewTest::MessageReceived(BMessage* message)
92 {
93 	switch (message->what) {
94 		case MSG_UPDATE_INSETS:
95 			_UpdateInsets();
96 			break;
97 		case MSG_UPDATE_TEXT:
98 			_UpdateText();
99 			break;
100 		case MSG_UPDATE_FONT:
101 			_UpdateFont();
102 			break;
103 		default:
104 			Test::MessageReceived(message);
105 			break;
106 	}
107 }
108 
109 
110 // #pragma mark - private
111 
112 
113 void
_UpdateInsets()114 TextViewTest::_UpdateInsets()
115 {
116 	if (fUseInsetsCheckBox == NULL)
117 		return;
118 
119 	if (fUseInsetsCheckBox->IsSelected())
120 		fTextView->SetInsets(10, 10, 10, 10);
121 	else
122 		fTextView->SetInsets(0, 0, 0, 0);
123 }
124 
125 
126 void
_UpdateText()127 TextViewTest::_UpdateText()
128 {
129 	if (fTextCheckBox == NULL)
130 		return;
131 
132 	if (fTextCheckBox->IsSelected())
133 		fTextView->SetText(kAlternativeText);
134 	else
135 		fTextView->SetText(kNormalText);
136 }
137 
138 
139 void
_UpdateFont()140 TextViewTest::_UpdateFont()
141 {
142 	if (fFontCheckBox == NULL)
143 		return;
144 
145 	BFont font(be_plain_font);
146 	if (fFontCheckBox->IsSelected()) {
147 		font.SetSize(ceilf(font.Size() * 1.5));
148 		fTextView->SetFontAndColor(&font);
149 	} else {
150 		fTextView->SetFontAndColor(&font);
151 	}
152 }
153 
154 
155 
156