xref: /haiku/src/apps/mandelbrot/Mandelbrot.cpp (revision 893988af824e65e49e55f517b157db8386e8002b)
1 /*
2 	Copyright 1993-1999, Be Incorporated.   All Rights Reserved.
3 	This file may be used under the terms of the Be Sample Code License.
4 */
5 
6 #include "tsb.h"
7 
8 #include <Debug.h>
9 
10 #include <Alert.h>
11 #include <Application.h>
12 #include <Bitmap.h>
13 #include <Menu.h>
14 #include <MenuBar.h>
15 #include <MenuItem.h>
16 #include <OS.h>
17 #include <Roster.h>
18 #include <ScrollView.h>
19 
20 #include <math.h>
21 #include <stdio.h>
22 
23 
24 /* Those are the menu item id's of the main window */
25 #define	P1		0x60
26 #define	P2		0x61
27 #define	P3		0x62
28 #define	P4		0x63
29 
30 class TMainWindow : public BWindow {
31 	public:
32 		TMainWindow(BRect bound, char* name, window_type type,
33 			long flags);
34 		virtual ~TMainWindow();
35 
36 		virtual void MessageReceived(BMessage* message);
37 		virtual void FrameResized(float width, float height);
38 		virtual bool QuitRequested();
39 
40 		void UpdateScrollBars();
41 
42 	private:
43 		TShowBit*	fView;
44 };
45 
46 
47 TMainWindow::TMainWindow(BRect bound, char* name, window_type type, long flags)
48 	: BWindow(bound, name, type, flags)
49 {
50 	BMenuBar* menuBar = new BMenuBar(BRect(0, 0, 1000, 15), "MB");
51 	BMenuItem* item;
52 	BMenu* menu;
53 
54 	menu = new BMenu("File");
55 	menu->AddItem(new BMenuItem("Quit", new BMessage(B_QUIT_REQUESTED), 'Q'));
56 	menuBar->AddItem(menu);
57 
58 	menu = new BMenu("Palette");
59 	menu->AddItem(new BMenuItem("Palette1", new BMessage(P1)));
60 	menu->AddItem(new BMenuItem("Palette2", new BMessage(P2)));
61 	menu->AddItem(item = new BMenuItem("Palette3", new BMessage(P3)));
62 	menu->AddItem(new BMenuItem("Palette4", new BMessage(P4)));
63 	menuBar->AddItem(menu);
64 
65 	item->SetMarked(true);
66 	menu->SetRadioMode(true);
67 
68 	menu = new BMenu("Iterations");
69 	menu->AddItem(new BMenuItem("128", new BMessage(128)));
70 	menu->AddItem(item = new BMenuItem("256", new BMessage(256)));
71 	menu->AddItem(new BMenuItem("384", new BMessage(384)));
72 	menu->AddItem(new BMenuItem("512", new BMessage(512)));
73 	menu->AddItem(new BMenuItem("768", new BMessage(768)));
74 	menu->AddItem(new BMenuItem("1024", new BMessage(1024)));
75 	menu->AddItem(new BMenuItem("2048", new BMessage(2048)));
76 	menu->AddItem(new BMenuItem("4096", new BMessage(4096)));
77 	menu->AddItem(new BMenuItem("8192", new BMessage(8192)));
78 	menuBar->AddItem(menu);
79 
80 	item->SetMarked(true);
81 	menu->SetRadioMode(true);
82 
83 	AddChild(menuBar);
84 	float barHeight = menuBar->Bounds().Height();
85 
86 	fView = new TShowBit(BRect(0, barHeight + 1, 188 - B_V_SCROLL_BAR_WIDTH,
87 		188 - B_H_SCROLL_BAR_HEIGHT), B_FOLLOW_ALL, B_WILL_DRAW);
88 	BScrollView	*scrollView = new BScrollView("scroll view", fView,
89 		B_FOLLOW_ALL, B_WILL_DRAW, true, true, B_NO_BORDER);
90 	AddChild(scrollView);
91 
92 	SetSizeLimits(80, size_x + 13, 80 + 20, size_y + barHeight + 1 + 13);
93 	ResizeTo(size_x + 13, size_y + barHeight + 1 + 13);
94 	SetPulseRate(150000);
95 	UpdateScrollBars();
96 }
97 
98 
99 TMainWindow::~TMainWindow()
100 {
101 }
102 
103 
104 void
105 TMainWindow::UpdateScrollBars()
106 {
107 	BScrollView*	scrollview;
108 	BScrollBar*		scrollbar;
109 	BRect			visible_extent;
110 	BRect			total_extent;
111 	BRect			bound;
112 	BRect			my_bounds;
113 	long			max;
114 
115 	Lock();
116 	if ((scrollview = (BScrollView*)FindView("scroll view"))) {
117 		bound.Set(0, 0, size_x, size_y);
118 		my_bounds = Bounds();
119 
120 		visible_extent = bound & my_bounds;
121 		total_extent = bound | my_bounds;
122 
123 		scrollbar = scrollview->ScrollBar(B_HORIZONTAL);
124 		max = (long) (bound.Width() - my_bounds.Width());
125 		if (max < 0)
126 			max = 0;
127 		scrollbar->SetRange(0, max);
128 		scrollbar->SetProportion(visible_extent.Width() / total_extent.Width());
129 
130 		scrollbar = scrollview->ScrollBar(B_VERTICAL);
131 		max = (long) (bound.Height() - my_bounds.Height());
132 		if (max < 0)
133 			max = 0;
134 		scrollbar->SetRange(0, max);
135 		scrollbar->SetProportion(visible_extent.Height() / total_extent.Height());
136 	}
137 	Unlock();
138 }
139 
140 
141 void
142 TMainWindow::FrameResized(float, float)
143 {
144 	UpdateScrollBars();
145 }
146 
147 
148 bool
149 TMainWindow::QuitRequested()
150 {
151 	if (fView->busy) {
152 		fView->exit_now = true;
153 		PostMessage(B_QUIT_REQUESTED);
154 		return false;
155 	}
156 
157 	be_app->PostMessage(B_QUIT_REQUESTED);
158 	return true;
159 }
160 
161 
162 void
163 TMainWindow::MessageReceived(BMessage* message)
164 {
165 	switch (message->what) {
166 		case P1:
167 		case P2:
168 		case P3:
169 		case P4:
170 			fView->set_palette(message->what - P1);
171 			break;
172 
173 		case 128:
174 		case 256:
175 		case 384:
176 		case 512:
177 		case 768:
178 		case 1024:
179 		case 2048:
180 		case 4096:
181 		case 8192:
182 			fView->set_iter(message->what);
183 			break;
184 
185 		default:
186 			BWindow::MessageReceived(message);
187 			break;
188 	}
189 }
190 
191 
192 //	#pragma mark -
193 
194 
195 int
196 main(int, char**)
197 {
198 	BApplication* app = new BApplication("application/x-vnd.Haiku-Mandelbrot");
199 
200 	BWindow* window = new TMainWindow(BRect(100, 100, 288, 288), "Mandelbrot",
201 		B_DOCUMENT_WINDOW, B_WILL_ACCEPT_FIRST_CLICK);
202 	window->Show();
203 
204 	app->Run();
205 	delete app;
206 	return 0;
207 }
208