xref: /haiku/src/tests/servers/app/bitmap_drawing/main.cpp (revision 0fa7031a26bd1bc82cbeb4d8e531941cd5e86b1e)
1 // main.cpp
2 
3 #include <stdio.h>
4 #include <string.h>
5 
6 #include "Application.h"
7 #include "Bitmap.h"
8 #include "Button.h"
9 #include "Message.h"
10 #include "View.h"
11 #include "Window.h"
12 
13 #include "bitmap.h"
14 
15 enum {
16 	MSG_RESET	= 'rset',
17 };
18 
19 class TestView : public BView {
20 
21  public:
22 					TestView(BRect frame, const char* name,
23 							  uint32 resizeFlags, uint32 flags)
24 						: BView(frame, name, resizeFlags, flags),
25 						  fBitmap(new BBitmap(BRect(0, 0, kBitmapWidth - 1, kBitmapHeight -1),
26 						  					  0, kBitmapFormat)),
27 						  fBitmapRect(),
28 						  fTracking(TRACKING_NONE),
29 						  fLastMousePos(-1.0, -1.0)
30 					{
31 						SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
32 						SetLowColor(ViewColor());
33 						uint32 size = min_c((uint32)fBitmap->BitsLength(), sizeof(kBitmapBits));
34 						memcpy(fBitmap->Bits(), kBitmapBits, size);
35 						_ResetRect();
36 					}
37 
38 	virtual	void	MessageReceived(BMessage* message);
39 
40 	virtual	void	Draw(BRect updateRect);
41 
42 	virtual	void	MouseDown(BPoint where);
43 	virtual	void	MouseUp(BPoint where);
44 	virtual	void	MouseMoved(BPoint where, uint32 transit,
45 							   const BMessage* dragMessage);
46 
47  private:
48 			void	_ResetRect();
49 
50 	BBitmap*		fBitmap;
51 	BRect			fBitmapRect;
52 
53 	enum {
54 		TRACKING_NONE = 0,
55 
56 		TRACKING_LEFT,
57 		TRACKING_RIGHT,
58 		TRACKING_TOP,
59 		TRACKING_BOTTOM,
60 
61 		TRACKING_LEFT_TOP,
62 		TRACKING_RIGHT_TOP,
63 		TRACKING_LEFT_BOTTOM,
64 		TRACKING_RIGHT_BOTTOM,
65 
66 		TRACKING_ALL
67 	};
68 
69 	uint32			fTracking;
70 	BPoint			fLastMousePos;
71 };
72 
73 // MessageReceived
74 void
75 TestView::MessageReceived(BMessage* message)
76 {
77 	if (message->what == MSG_RESET) {
78 		BRect old = fBitmapRect;
79 		_ResetRect();
80 		Invalidate(old | fBitmapRect);
81 	} else
82 		BView::MessageReceived(message);
83 }
84 
85 // Draw
86 void
87 TestView::Draw(BRect updateRect)
88 {
89 	SetDrawingMode(B_OP_COPY);
90 	DrawBitmap(fBitmap, fBitmap->Bounds(), fBitmapRect);
91 
92 	// text
93 	SetDrawingMode(B_OP_OVER);
94 	SetHighColor(0, 0, 0, 255);
95 	const char* message = "Click and drag to move the bitmap!";
96 	DrawString(message, BPoint(20.0, 30.0));
97 }
98 
99 // MouseDown
100 void
101 TestView::MouseDown(BPoint where)
102 {
103 	fTracking = TRACKING_ALL;
104 	fLastMousePos = where;
105 }
106 
107 // MouseUp
108 void
109 TestView::MouseUp(BPoint where)
110 {
111 	fTracking = TRACKING_NONE;
112 }
113 
114 // MouseMoved
115 void
116 TestView::MouseMoved(BPoint where, uint32 transit,
117 					 const BMessage* dragMessage)
118 {
119 	if (fTracking > TRACKING_NONE) {
120 		switch (fTracking) {
121 			case TRACKING_ALL:
122 			default:
123 				BRect old = fBitmapRect;
124 				fBitmapRect.OffsetBy(where - fLastMousePos);
125 				fLastMousePos = where;
126 				Invalidate(old | fBitmapRect);
127 				break;
128 		}
129 	}
130 }
131 
132 // _ResetRect
133 void
134 TestView::_ResetRect()
135 {
136 	fBitmapRect = fBitmap->Bounds();
137 	fBitmapRect.OffsetBy(floorf((Bounds().Width() - fBitmapRect.Width()) / 2.0 + 0.5),
138 						 floorf((Bounds().Height() - fBitmapRect.Height()) / 2.0 + 0.5));
139 }
140 
141 // show_window
142 void
143 show_window(BRect frame, const char* name)
144 {
145 	BWindow* window = new BWindow(frame, name,
146 								  B_TITLED_WINDOW,
147 								  B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE);
148 
149 	BView* view = new TestView(window->Bounds(), "test", B_FOLLOW_ALL,
150 							   B_WILL_DRAW/* | B_FULL_UPDATE_ON_RESIZE*/);
151 
152 	window->AddChild(view);
153 	BRect b(0.0, 0.0, 60.0, 15.0);
154 	b.OffsetTo(5.0, view->Bounds().bottom - (b.Height() + 15.0));
155 	BButton* control = new BButton(b, "button", "Reset", new BMessage(MSG_RESET));
156 	view->AddChild(control);
157 	control->SetTarget(view);
158 
159 	window->Show();
160 }
161 
162 // main
163 int
164 main(int argc, char** argv)
165 {
166 	BApplication* app = new BApplication("application/x.vnd-Haiku.BitmapDrawing");
167 
168 	BRect frame(50.0, 50.0, 300.0, 250.0);
169 	show_window(frame, "Bitmap Drawing");
170 
171 	app->Run();
172 
173 	delete app;
174 	return 0;
175 }
176