xref: /haiku/src/tests/kits/interface/TruncateString.cpp (revision e33d4e74c244cab2668c1229f1016d015ea0dcec)
1 #include <stdio.h>
2 #include <SupportDefs.h>
3 
4 #include <Application.h>
5 #include <String.h>
6 #include <View.h>
7 #include <Window.h>
8 
9 
10 class TruncateView : public BView {
11 public:
TruncateView(BRect frame)12 	TruncateView(BRect frame)
13 		:	BView(frame, "TruncateView", B_FOLLOW_ALL, B_WILL_DRAW)
14 	{
15 	}
16 
Draw(BRect updateRect)17 	void Draw(BRect updateRect)
18 	{
19 		const float kSpacing = 20.0;
20 		const float kTopOffset = kSpacing + 5;
21 		const uint32 kTruncateModes[]
22 			= { B_TRUNCATE_BEGINNING, B_TRUNCATE_MIDDLE, B_TRUNCATE_END };
23 		const uint32 kTruncateModeCount
24 			= sizeof(kTruncateModes) / sizeof(kTruncateModes[0]);
25 
26 		BString theString("ö&ä|ü-é#");
27 		BPoint point(10, kTopOffset);
28 		BString truncated;
29 		for (float length = 5; length < 65; length += 3) {
30 			SetHighColor(255, 0, 0);
31 			StrokeRect(BRect(point.x, point.y - kSpacing, point.x + length,
32 				point.y + kSpacing * kTruncateModeCount));
33 			SetHighColor(0, 0, 0);
34 
35 			for (uint32 i = 0; i < kTruncateModeCount; i++) {
36 				truncated = theString;
37 				TruncateString(&truncated, kTruncateModes[i], length);
38 				DrawString(truncated.String(), point);
39 				point.y += kSpacing;
40 			}
41 
42 			point.x += length + kSpacing;
43 			point.y = kTopOffset;
44 		}
45 	}
46 };
47 
48 
49 int
main(int argc,char * argv[])50 main(int argc, char *argv[])
51 {
52 	BApplication app("application/x-vnd.Haiku-TruncateString");
53 
54 	BRect frame(100, 200, 1200, 300);
55 	BWindow *window = new BWindow(frame, "TruncateString", B_TITLED_WINDOW,
56 		B_QUIT_ON_WINDOW_CLOSE);
57 
58 	TruncateView *view = new TruncateView(frame.OffsetToSelf(0, 0));
59 	window->AddChild(view);
60 
61 	window->Show();
62 	app.Run();
63 	return 0;
64 }
65