xref: /haiku/src/tests/kits/interface/WidthBufferTest.cpp (revision 83b1a68c52ba3e0e8796282759f694b7fdddf06d)
1 /*
2 ** Copyright 2008, Stefano Ceccherini (stefano.ceccherini@gmail.com).
3 ** All rights reserved.
4 ** Distributed under the terms of the Haiku License.
5 */
6 
7 #include <Application.h>
8 #include <Font.h>
9 
10 #include <stdio.h>
11 #include <stdlib.h>
12 
13 #include <WidthBuffer.h>
14 
15 
16 class App : public BApplication {
17 public:
18 	App();
19 	~App();
20 	virtual void ReadyToRun();
21 
22 private:
23 	_BWidthBuffer_ *fWidthBuffer;
24 	thread_id fThread;
25 
26 	int32 TesterFunc();
27 	static int32 _thread(void *data);
28 
29 };
30 
31 
32 int main()
33 {
34 	App app;
35 	app.Run();
36 	return 0;
37 }
38 
39 
40 App::App()
41 	:BApplication("application/x-vnd-WidthBufferTest")
42 {
43 	fWidthBuffer = new _BWidthBuffer_;
44 	fThread = spawn_thread(App::_thread, "widthbuffer tester",
45 				B_NORMAL_PRIORITY, this);
46 }
47 
48 
49 void
50 App::ReadyToRun()
51 {
52 	resume_thread(fThread);
53 }
54 
55 
56 App::~App()
57 {
58 	status_t status;
59 	wait_for_thread(fThread, &status);
60 	delete fWidthBuffer;
61 }
62 
63 
64 int32
65 App::TesterFunc()
66 {
67 	FILE *file = fopen("/boot/beos/etc/termcap", "r");
68 	if (file != NULL) {
69 		char buffer[512];
70 		while (fgets(buffer, 512, file)) {
71 			float width = fWidthBuffer->StringWidth(buffer, 0,
72 							strlen(buffer), be_plain_font);
73 			printf("string: %s, width: %f\n", buffer, width);
74 		}
75 		fclose(file);
76 	}
77 
78 	PostMessage(B_QUIT_REQUESTED);
79 
80 	return 0;
81 }
82 
83 
84 int32
85 App::_thread(void *data)
86 {
87 	return static_cast<App *>(data)->TesterFunc();
88 }
89 
90