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