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