xref: /haiku/src/tests/kits/interface/TextViewTestManual.cpp (revision 83b1a68c52ba3e0e8796282759f694b7fdddf06d)
1 /*
2  * Copyright 2009, Oliver Tappe, zooey@hirschkaefer.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <Application.h>
8 #include <Button.h>
9 #include <ControlLook.h>
10 #include <GroupLayout.h>
11 #include <GroupLayoutBuilder.h>
12 #include <ScrollView.h>
13 #include <String.h>
14 #include <TextControl.h>
15 #include <TextView.h>
16 #include <Window.h>
17 
18 #include <stdio.h>
19 
20 
21 const static uint32 kMsgAlignLeft = 'alle';
22 const static uint32 kMsgAlignCenter = 'alce';
23 const static uint32 kMsgAlignRight = 'alri';
24 
25 
26 class Window : public BWindow {
27 	public:
28 		Window();
29 
30 		virtual bool QuitRequested();
31 		virtual void MessageReceived(BMessage *message);
32 
33 	private:
34 		BTextControl* fTextControl;
35 		BTextView* fTextView;
36 };
37 
38 
39 //	#pragma mark -
40 
41 
42 Window::Window()
43 	: BWindow(BRect(100, 100, 800, 500), "TextView-Test",
44 			B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS)
45 {
46 	fTextControl = new BTextControl("text-contr-O",
47 		"a single line of text - (c) Conglom-O", NULL);
48 
49 	BTextControl* disabled = new BTextControl("disabled",
50 		"I'm disabled: you can't edit me", NULL);
51 	disabled->SetEnabled(false);
52 	BTextControl* invalid = new BTextControl("invalid",
53 		"I'm invalid: my border is red", NULL);
54 	invalid->MarkAsInvalid(true);
55 
56 	fTextView = new BTextView("text-O");
57 	BScrollView* scrollView = new BScrollView("scroll-O", fTextView, 0, true,
58 		true, B_FANCY_BORDER);
59 
60 	SetLayout(new BGroupLayout(B_HORIZONTAL));
61 	AddChild(BGroupLayoutBuilder(B_VERTICAL, 10)
62 		.Add(fTextControl)
63 		.Add(disabled)
64 		.Add(invalid)
65 		.Add(scrollView)
66 		.Add(BGroupLayoutBuilder(B_HORIZONTAL, 10)
67 			.Add(new BButton("Align Left", new BMessage(kMsgAlignLeft)))
68 			.AddGlue()
69 			.Add(new BButton("Align Center", new BMessage(kMsgAlignCenter)))
70 			.AddGlue()
71 			.Add(new BButton("Align Right", new BMessage(kMsgAlignRight)))
72 		)
73 		.SetInsets(5, 5, 5, 5)
74 	);
75 
76 	// generate some lines of content
77 	const int32 kLineCount = 10;
78 	const int32 kLineNoSize = 6;
79 	BString line = ": just some text here - nothing special to see\n";
80 	BString format = BString("%*d") << line;
81 	BString content;
82 	int32 lineLength = line.Length() + kLineNoSize;
83 	int32 contentLength = lineLength * kLineCount;
84 	char* currLine = content.LockBuffer(contentLength);
85 	if (currLine) {
86 		int32 lineNo = 0;
87 		for ( ; lineNo < kLineCount; currLine += lineLength)
88 			sprintf(currLine, format.String(), kLineNoSize, lineNo++);
89 		content.UnlockBuffer(contentLength);
90 	}
91 	fTextView->SetInsets(2,2,2,2);
92 	fTextView->SetText(content.String());
93 }
94 
95 
96 bool
97 Window::QuitRequested()
98 {
99 	be_app->PostMessage(B_QUIT_REQUESTED);
100 	return true;
101 }
102 
103 
104 void
105 Window::MessageReceived(BMessage *message)
106 {
107 	switch (message->what) {
108 		case kMsgAlignLeft:
109 			fTextControl->SetAlignment(B_ALIGN_LEFT, B_ALIGN_LEFT);
110 			fTextView->SetAlignment(B_ALIGN_LEFT);
111 			break;
112 
113 		case kMsgAlignCenter:
114 			fTextControl->SetAlignment(B_ALIGN_LEFT, B_ALIGN_CENTER);
115 			fTextView->SetAlignment(B_ALIGN_CENTER);
116 			break;
117 
118 		case kMsgAlignRight:
119 			fTextControl->SetAlignment(B_ALIGN_LEFT, B_ALIGN_RIGHT);
120 			fTextView->SetAlignment(B_ALIGN_RIGHT);
121 			break;
122 
123 		default:
124 			BWindow::MessageReceived(message);
125 			break;
126 	}
127 }
128 
129 
130 //	#pragma mark -
131 
132 
133 class Application : public BApplication {
134 	public:
135 		Application();
136 
137 		virtual void ReadyToRun(void);
138 };
139 
140 
141 Application::Application()
142 	: BApplication("application/x-vnd.haiku-test")
143 {
144 }
145 
146 
147 void
148 Application::ReadyToRun(void)
149 {
150 	BWindow *window = new Window();
151 	window->Show();
152 }
153 
154 
155 //	#pragma mark -
156 
157 
158 int
159 main(int argc, char **argv)
160 {
161 	Application app;
162 
163 	app.Run();
164 	return 0;
165 }
166 
167