xref: /haiku/src/tests/servers/app/bitmap_bounds/main.cpp (revision b55a57da7173b9af0432bd3e148d03f06161d036)
1 // main.cpp
2 
3 #include <stdio.h>
4 
5 #include <Application.h>
6 #include <Bitmap.h>
7 #include <View.h>
8 #include <Window.h>
9 
10 class TestView : public BView {
11 
12  public:
13 					TestView(BRect frame, const char* name,
14 							 uint32 resizeFlags, uint32 flags);
15 
16 	virtual	void	Draw(BRect updateRect);
17 
18  private:
19 	BBitmap*		fBitmap;
20 };
21 
22 //#define LEFT_OFFSET 0
23 //#define TOP_OFFSET 0
24 #define LEFT_OFFSET 30
25 #define TOP_OFFSET 30
26 
27 // constructor
28 TestView::TestView(BRect frame, const char* name,
29 				   uint32 resizeFlags, uint32 flags)
30 	: BView(frame, name, resizeFlags, flags),
31 
32 	  fBitmap(new BBitmap(BRect(0 + LEFT_OFFSET,
33 	  							0 + TOP_OFFSET,
34 	  							99 + LEFT_OFFSET,
35 	  							99 + TOP_OFFSET),
36 	  					  B_BITMAP_CLEAR_TO_WHITE,
37 	  					  B_RGB32))
38 {
39 	SetViewColor(216, 216, 216);
40 
41 	uint8* bits = (uint8*)fBitmap->Bits();
42 	uint32 width = fBitmap->Bounds().IntegerWidth() + 1;
43 	uint32 height = fBitmap->Bounds().IntegerHeight() + 1;
44 	uint32 bpr = fBitmap->BytesPerRow();
45 
46 	// top row -> red
47 	uint8* b = bits;
48 	for (uint32 x = 0; x < width; x++) {
49 		b[0] = 0;
50 		b[1] = 0;
51 		b += 4;
52 	}
53 
54 	// left/right edge pixels -> red
55 	b = bits + bpr;
56 	for (uint32 y = 1; y < height - 1; y++) {
57 		b[0] = 0;
58 		b[1] = 0;
59 		b[(width - 1) * 4 + 0] = 0;
60 		b[(width - 1) * 4 + 1] = 0;
61 		b += bpr;
62 	}
63 
64 	// bottom row -> red
65 	for (uint32 x = 0; x < width; x++) {
66 		b[0] = 0;
67 		b[1] = 0;
68 		b += 4;
69 	}
70 }
71 
72 // Draw
73 void
74 TestView::Draw(BRect updateRect)
75 {
76 //	DrawBitmap(BBitmap*, BRect source, BRect destination);
77 //
78 //  BeBook:
79 //  If a source rectangle is given, only that part of the
80 //  bitmap image is drawn. Otherwise, the entire bitmap
81 //  is placed in the view. The source rectangle is stated
82 //  in the internal coordinates of the BBitmap object.
83 
84 // Test 1:
85 	// if the above was true, then we should see the left
86 	// top area of the bitmap...
87 //	BRect view(0, 0, 50, 50);
88 //	BRect bitmap = view.OffsetByCopy(fBitmap->Bounds().LeftTop());
89 //
90 //	DrawBitmap(fBitmap, bitmap, view);
91 
92 // Test 2:
93 	// if the above was true, we should simply see the entire
94 	// bitmap at the left top corner of the view
95 	BRect bitmap = fBitmap->Bounds();
96 	BRect view = bitmap.OffsetToCopy(B_ORIGIN);
97 
98 	DrawBitmap(fBitmap, bitmap, view);
99 }
100 
101 
102 // main
103 int
104 main(int argc, char** argv)
105 {
106 	BApplication app("application/x.vnd-Haiku.BitmapBounds");
107 
108 	BRect frame(50.0, 50.0, 300.0, 250.0);
109 	BWindow* window = new BWindow(frame, "Bitmap Bounds", B_TITLED_WINDOW,
110 		B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE);
111 
112 	BView* view = new TestView(window->Bounds(), "test",
113 		B_FOLLOW_ALL, B_WILL_DRAW);
114 	window->AddChild(view);
115 
116 	window->Show();
117 
118 	app.Run();
119 
120 	return 0;
121 }
122