xref: /haiku/src/apps/mandelbrot/Mandelbrot.cpp (revision 89755088d790ff4fe36f8aa77dacb2bd15507108)
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 	menuBar->AddItem(menu);
76 
77 	item->SetMarked(true);
78 	menu->SetRadioMode(true);
79 
80 	AddChild(menuBar);
81 	float barHeight = menuBar->Bounds().Height();
82 
83 	fView = new TShowBit(BRect(0, barHeight + 1, 188 - B_V_SCROLL_BAR_WIDTH,
84 		188 - B_H_SCROLL_BAR_HEIGHT), B_FOLLOW_ALL, B_WILL_DRAW);
85 	BScrollView	*scrollView = new BScrollView("scroll view", fView,
86 		B_FOLLOW_ALL, B_WILL_DRAW, true, true, B_NO_BORDER);
87 	AddChild(scrollView);
88 
89 	SetSizeLimits(80, size_x + 13, 80 + 20, size_y + barHeight + 1 + 13);
90 	ResizeTo(size_x + 13, size_y + barHeight + 1 + 13);
91 	SetPulseRate(150000);
92 	UpdateScrollBars();
93 }
94 
95 
96 TMainWindow::~TMainWindow()
97 {
98 }
99 
100 
101 void
102 TMainWindow::UpdateScrollBars()
103 {
104 	BScrollView*	scrollview;
105 	BScrollBar*		scrollbar;
106 	BRect			visible_extent;
107 	BRect			total_extent;
108 	BRect			bound;
109 	BRect			my_bounds;
110 	long			max;
111 
112 	Lock();
113 	if ((scrollview = (BScrollView*)FindView("scroll view"))) {
114 		bound.Set(0, 0, size_x, size_y);
115 		my_bounds = Bounds();
116 
117 		visible_extent = bound & my_bounds;
118 		total_extent = bound | my_bounds;
119 
120 		scrollbar = scrollview->ScrollBar(B_HORIZONTAL);
121 		max = (long) (bound.Width() - my_bounds.Width());
122 		if (max < 0)
123 			max = 0;
124 		scrollbar->SetRange(0, max);
125 		scrollbar->SetProportion(visible_extent.Width() / total_extent.Width());
126 
127 		scrollbar = scrollview->ScrollBar(B_VERTICAL);
128 		max = (long) (bound.Height() - my_bounds.Height());
129 		if (max < 0)
130 			max = 0;
131 		scrollbar->SetRange(0, max);
132 		scrollbar->SetProportion(visible_extent.Height() / total_extent.Height());
133 	}
134 	Unlock();
135 }
136 
137 
138 void
139 TMainWindow::FrameResized(float, float)
140 {
141 	UpdateScrollBars();
142 }
143 
144 
145 bool
146 TMainWindow::QuitRequested()
147 {
148 	if (fView->busy) {
149 		fView->exit_now = true;
150 		PostMessage(B_QUIT_REQUESTED);
151 		return false;
152 	}
153 
154 	be_app->PostMessage(B_QUIT_REQUESTED);
155 	return true;
156 }
157 
158 
159 void
160 TMainWindow::MessageReceived(BMessage* message)
161 {
162 	switch (message->what) {
163 		case P1:
164 		case P2:
165 		case P3:
166 		case P4:
167 			fView->set_palette(message->what - P1);
168 			break;
169 
170 		case 128:
171 		case 256:
172 		case 384:
173 		case 512:
174 		case 768:
175 		case 1024:
176 			fView->set_iter(message->what);
177 			break;
178 
179 		default:
180 			BWindow::MessageReceived(message);
181 			break;
182 	}
183 }
184 
185 
186 //	#pragma mark -
187 
188 
189 int
190 main(int, char**)
191 {
192 	BApplication* app = new BApplication("application/x-vnd.Be-MAND");
193 
194 	BWindow* window = new TMainWindow(BRect(100, 100, 288, 288), "Mandelbrot",
195 		B_DOCUMENT_WINDOW, B_WILL_ACCEPT_FIRST_CLICK);
196 	window->Show();
197 
198 	app->Run();
199 	delete app;
200 	return 0;
201 }
202