1 /* 2 * Copyright 2012, Haiku, Inc. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Aaron Hill <serac@hillvisions.com> 7 * Alexander von Gluck <kallisti5@unixzen.com> 8 */ 9 10 11 #include "GLifeConfig.h" 12 13 #include <Catalog.h> 14 #include <GroupLayoutBuilder.h> 15 #include <Slider.h> 16 #include <String.h> 17 #include <StringView.h> 18 #include <View.h> 19 20 #include <stdio.h> 21 22 #include "GLifeState.h" 23 24 #undef B_TRANSLATION_CONTEXT 25 #define B_TRANSLATION_CONTEXT "GLife ScreenSaver" 26 27 28 // ------------------------------------------------------ 29 // GLifeConfig Class Constructor Definition 30 GLifeConfig::GLifeConfig(BRect frame, GLifeState* pglsState) 31 : 32 BView(frame, "", B_FOLLOW_ALL_SIDES, B_WILL_DRAW), 33 m_pglsState(pglsState) 34 { 35 SetLayout(new BGroupLayout(B_HORIZONTAL)); 36 SetViewUIColor(B_PANEL_BACKGROUND_COLOR); 37 38 // Info text 39 BStringView* name = new BStringView(frame, B_EMPTY_STRING, 40 B_TRANSLATE("OpenGL \"Game of Life\""), B_FOLLOW_LEFT); 41 BStringView* author = new BStringView(frame, B_EMPTY_STRING, B_TRANSLATE("by Aaron Hill"), 42 B_FOLLOW_LEFT); 43 44 // Sliders 45 fGridDelay = new BSlider(frame, "GridDelay", 46 B_TRANSLATE("Grid life delay: "), 47 new BMessage(kGridDelay), 48 0, 4, B_BLOCK_THUMB); 49 50 fGridDelay->SetHashMarks(B_HASH_MARKS_BOTTOM); 51 fGridDelay->SetLimitLabels(B_TRANSLATE("None"), 52 B_TRANSLATE_COMMENT("4x", "This is a factor: the x represents 'times'")); 53 fGridDelay->SetValue(pglsState->GridDelay()); 54 fGridDelay->SetHashMarkCount(5); 55 56 fGridBorder = new BSlider(frame, "GridBorder", B_TRANSLATE("Grid border: "), 57 new BMessage(kGridBorder), 0, 10, B_BLOCK_THUMB); 58 59 fGridBorder->SetHashMarks(B_HASH_MARKS_BOTTOM); 60 fGridBorder->SetLimitLabels("0", "10"); 61 fGridBorder->SetValue(pglsState->GridBorder()); 62 fGridBorder->SetHashMarkCount(11); 63 64 fGridWidth = new BSlider(frame, "GridWidth", B_TRANSLATE("Grid width: "), 65 new BMessage(kGridWidth), 10, 100, B_BLOCK_THUMB); 66 67 fGridWidth->SetHashMarks(B_HASH_MARKS_BOTTOM); 68 // fGridWidth->SetLimitLabels("10", "100"); 69 fGridWidth->SetValue(pglsState->GridWidth()); 70 fGridWidth->SetHashMarkCount(10); 71 72 fGridHeight = new BSlider(frame, "GridHeight", B_TRANSLATE("Grid height: "), 73 new BMessage(kGridHeight), 10, 100, B_BLOCK_THUMB); 74 75 fGridHeight->SetHashMarks(B_HASH_MARKS_BOTTOM); 76 // fGridHeight->SetLimitLabels("10", "100"); 77 fGridHeight->SetValue(pglsState->GridHeight()); 78 fGridHeight->SetHashMarkCount(10); 79 80 AddChild(BGroupLayoutBuilder(B_VERTICAL, B_USE_DEFAULT_SPACING) 81 .Add(BGroupLayoutBuilder(B_VERTICAL, 0) 82 .Add(name) 83 .Add(author) 84 ) 85 .AddGlue() 86 .Add(fGridDelay) 87 .Add(fGridBorder) 88 .Add(BGroupLayoutBuilder(B_HORIZONTAL, 0) 89 .Add(fGridWidth) 90 .Add(fGridHeight) 91 ) 92 .SetInsets(B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING, 93 B_USE_DEFAULT_SPACING, B_USE_DEFAULT_SPACING) 94 ); 95 96 // Do our first label update 97 _UpdateLabels(); 98 } 99 100 101 // ------------------------------------------------------ 102 // GLifeConfig Class AttachedToWindow Definition 103 void 104 GLifeConfig::AttachedToWindow(void) 105 { 106 fGridWidth->SetTarget(this); 107 fGridHeight->SetTarget(this); 108 fGridBorder->SetTarget(this); 109 fGridDelay->SetTarget(this); 110 111 #ifdef _USE_ASYNCHRONOUS 112 113 m_uiWindowFlags = Window()->Flags(); 114 Window()->SetFlags(m_uiWindowFlags | B_ASYNCHRONOUS_CONTROLS); 115 116 #endif 117 } 118 119 120 void 121 GLifeConfig::_UpdateLabels() 122 { 123 BString newLabel; 124 newLabel.SetToFormat(B_TRANSLATE("Grid width: %d"), (int)fGridWidth->Value()); 125 fGridWidth->SetLabel(newLabel); 126 127 newLabel.SetToFormat(B_TRANSLATE("Grid height: %d"), (int)fGridHeight->Value()); 128 fGridHeight->SetLabel(newLabel); 129 130 newLabel.SetToFormat(B_TRANSLATE("Grid border: %d"), (int)fGridBorder->Value()); 131 fGridBorder->SetLabel(newLabel); 132 133 int delay = fGridDelay->Value(); 134 if (delay <= 0) 135 newLabel = B_TRANSLATE("Grid life delay: none"); 136 else { 137 newLabel.SetToFormat(B_TRANSLATE_CONTEXT("Grid life delay: %d×", 138 "This is a factor: the × represents 'times', don't translate '%d'"), 139 delay); 140 } 141 fGridDelay->SetLabel(newLabel); 142 } 143 144 145 // ------------------------------------------------------ 146 // GLifeConfig Class MessageReceived Definition 147 void 148 GLifeConfig::MessageReceived(BMessage* pbmMessage) 149 { 150 switch (pbmMessage->what) { 151 case kGridWidth: 152 m_pglsState->GridWidth() = fGridWidth->Value(); 153 break; 154 case kGridHeight: 155 m_pglsState->GridHeight() = fGridHeight->Value(); 156 break; 157 case kGridBorder: 158 m_pglsState->GridBorder() = fGridBorder->Value(); 159 break; 160 case kGridDelay: 161 m_pglsState->GridDelay() = fGridDelay->Value(); 162 break; 163 default: 164 BView::MessageReceived(pbmMessage); 165 return; 166 } 167 _UpdateLabels(); 168 } 169