1 /* 2 * Copyright 2014 Stephan Aßmus <superstippi@gmx.de> 3 * All rights reserved. Distributed under the terms of the MIT license. 4 */ 5 6 7 #include "harness.h" 8 9 10 #include <Application.h> 11 #include <Bitmap.h> 12 #include <LayoutBuilder.h> 13 #include <Message.h> 14 #include <PopUpMenu.h> 15 #include <ScrollView.h> 16 17 18 Test::Test(const char* name) 19 : 20 fName(name) 21 { 22 } 23 24 25 Test::~Test() 26 { 27 } 28 29 30 // #pragma mark - TestView 31 32 33 class TestView : public BView { 34 public: 35 TestView(); 36 virtual ~TestView(); 37 38 virtual void Draw(BRect updateRect); 39 40 void SetTest(Test* test); 41 42 private: 43 Test* fTest; 44 }; 45 46 47 TestView::TestView() 48 : 49 BView(NULL, B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE), 50 fTest(NULL) 51 { 52 } 53 54 55 TestView::~TestView() 56 { 57 } 58 59 60 void 61 TestView::Draw(BRect updateRect) 62 { 63 if (fTest != NULL) 64 fTest->Draw(this, updateRect); 65 } 66 67 68 void 69 TestView::SetTest(Test* test) 70 { 71 fTest = test; 72 Invalidate(); 73 } 74 75 76 // #pragma mark - TestWindow 77 78 79 enum { 80 MSG_SELECT_TEST = 'stst' 81 }; 82 83 84 TestWindow::TestWindow(const char* title) 85 : 86 BWindow(BRect(50.0, 50.0, 450.0, 250.0), title, 87 B_DOCUMENT_WINDOW, B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE 88 | B_AUTO_UPDATE_SIZE_LIMITS) 89 { 90 fTestView = new TestView(); 91 92 BScrollView* scrollView = new BScrollView("scroll", fTestView, 0, true, 93 true); 94 95 fTestSelectionField = new BMenuField("test selection", 96 "Select test:", new BPopUpMenu("select")); 97 98 BLayoutBuilder::Group<>(this, B_VERTICAL, 0.0f) 99 .AddGroup(B_HORIZONTAL) 100 .Add(fTestSelectionField) 101 .AddGlue() 102 .SetInsets(B_USE_DEFAULT_SPACING) 103 .End() 104 .Add(scrollView) 105 ; 106 } 107 108 109 TestWindow::~TestWindow() 110 { 111 for (int32 i = fTests.CountItems() - 1; i >= 0; i++) 112 delete (Test*)fTests.ItemAt(i); 113 } 114 115 116 void 117 TestWindow::MessageReceived(BMessage* message) 118 { 119 switch (message->what) { 120 case MSG_SELECT_TEST: 121 { 122 int32 index; 123 if (message->FindInt32("index", &index) == B_OK) 124 SetToTest(index); 125 break; 126 } 127 128 default: 129 BWindow::MessageReceived(message); 130 } 131 } 132 133 134 void 135 TestWindow::AddTest(Test* test) 136 { 137 if (test == NULL || fTests.HasItem(test)) 138 return; 139 140 if (!fTests.AddItem(test)) { 141 delete test; 142 return; 143 } 144 145 BMessage* message = new BMessage(MSG_SELECT_TEST); 146 message->AddInt32("index", fTests.CountItems() - 1); 147 148 BMenuItem* item = new BMenuItem(test->Name(), message); 149 if (!fTestSelectionField->Menu()->AddItem(item)) { 150 fTests.RemoveItem(fTests.CountItems() - 1); 151 delete test; 152 delete item; 153 return; 154 } 155 156 if (fTests.CountItems() == 1) 157 SetToTest(0); 158 } 159 160 161 void 162 TestWindow::SetToTest(int32 index) 163 { 164 Test* test = (Test*)fTests.ItemAt(index); 165 if (test == NULL) 166 return; 167 168 fTestSelectionField->Menu()->ItemAt(index)->SetMarked(true); 169 170 fTestView->SetTest(test); 171 } 172 173 174