1 module calcface; 2 3 import bindbc.cocoa; 4 5 import calcbrain; 6 7 struct CalcFace 8 { 9 NSWindow parent; 10 alias parent this; 11 mixin NSObjectTemplate!(NSWindow, "NSWindow"); 12 13 NSButton[18] buttons; 14 NSTextField display; 15 16 void build(){ 17 display = NSTextField.alloc; display.initWithFrame(NSMakeRect (40, 84, 182, 24)); 18 display.setEditable(NO); 19 display.setBezeled(YES); 20 display.setDrawsBackground(YES); 21 display.setAlignment(right); 22 display.setSelectable(NO); 23 24 buttons[0] = NSButton.alloc; buttons[0].initWithFrame(NSMakeRect (77, 3, 34, 24)); 25 buttons[0].setButtonType(NSToggleButton); 26 buttons[0].setTitle(NSString.stringWith("0"w)); 27 buttons[0].setBezelStyle(NSRoundedBezelStyle); 28 buttons[0].setTag(0); 29 buttons[0].setState(NO); 30 buttons[0].setKeyEquivalent(NSString.stringWith("0"w)); 31 32 buttons[1] = NSButton.alloc; buttons[1].initWithFrame(NSMakeRect (114, 3, 34, 24)); 33 buttons[1].setButtonType(NSToggleButton); 34 buttons[1].setTitle(NSString.stringWith("1"w)); 35 buttons[1].setBezelStyle(NSRoundedBezelStyle); 36 buttons[1].setTag(1); 37 buttons[1].setState(NO); 38 buttons[1].setKeyEquivalent(NSString.stringWith("1"w)); 39 40 buttons[2] = NSButton.alloc; buttons[2].initWithFrame(NSMakeRect (151, 3, 34, 24)); 41 buttons[2].setButtonType(NSToggleButton); 42 buttons[2].setTitle(NSString.stringWith("2"w)); 43 buttons[2].setBezelStyle(NSRoundedBezelStyle); 44 buttons[2].setTag(2); 45 buttons[2].setState(NO); 46 buttons[2].setKeyEquivalent(NSString.stringWith("2"w)); 47 48 buttons[3] = NSButton.alloc; buttons[3].initWithFrame(NSMakeRect (188, 3, 34, 24)); 49 buttons[3].setButtonType(NSToggleButton); 50 buttons[3].setTitle(NSString.stringWith("3"w)); 51 buttons[3].setBezelStyle(NSRoundedBezelStyle); 52 buttons[3].setTag(3); 53 buttons[3].setState(NO); 54 buttons[3].setKeyEquivalent(NSString.stringWith("3"w)); 55 56 buttons[4] = NSButton.alloc; buttons[4].initWithFrame(NSMakeRect (114, 30, 34, 24)); 57 buttons[4].setButtonType(NSToggleButton); 58 buttons[4].setTitle(NSString.stringWith("4"w)); 59 buttons[4].setBezelStyle(NSRoundedBezelStyle); 60 buttons[4].setTag(4); 61 buttons[4].setState(NO); 62 buttons[4].setKeyEquivalent(NSString.stringWith("4"w)); 63 64 buttons[5] = NSButton.alloc; buttons[5].initWithFrame(NSMakeRect (151, 30, 34, 24)); 65 buttons[5].setButtonType(NSToggleButton); 66 buttons[5].setTitle(NSString.stringWith("5"w)); 67 buttons[5].setBezelStyle(NSRoundedBezelStyle); 68 buttons[5].setTag(5); 69 buttons[5].setState(NO); 70 buttons[5].setKeyEquivalent(NSString.stringWith("5"w)); 71 72 buttons[6] = NSButton.alloc; buttons[6].initWithFrame(NSMakeRect (188, 30, 34, 24)); 73 buttons[6].setButtonType(NSToggleButton); 74 buttons[6].setTitle(NSString.stringWith("6"w)); 75 buttons[6].setBezelStyle(NSRoundedBezelStyle); 76 buttons[6].setTag(6); 77 buttons[6].setState(NO); 78 buttons[6].setKeyEquivalent(NSString.stringWith("6"w)); 79 80 buttons[7] = NSButton.alloc; buttons[7].initWithFrame(NSMakeRect (114, 57, 34, 24)); 81 buttons[7].setButtonType(NSToggleButton); 82 buttons[7].setTitle(NSString.stringWith("7"w)); 83 buttons[7].setBezelStyle(NSRoundedBezelStyle); 84 buttons[7].setTag(7); 85 buttons[7].setState(NO); 86 buttons[7].setKeyEquivalent(NSString.stringWith("7"w)); 87 88 buttons[8] = NSButton.alloc; buttons[8].initWithFrame(NSMakeRect (151, 57, 34, 24)); 89 buttons[8].setButtonType(NSToggleButton); 90 buttons[8].setTitle(NSString.stringWith("8"w)); 91 buttons[8].setBezelStyle(NSRoundedBezelStyle); 92 buttons[8].setTag(8); 93 buttons[8].setState(NO); 94 buttons[8].setKeyEquivalent(NSString.stringWith("8"w)); 95 96 buttons[9] = NSButton.alloc; buttons[9].initWithFrame(NSMakeRect (188, 57, 34, 24)); 97 buttons[9].setButtonType(NSToggleButton); 98 buttons[9].setTitle(NSString.stringWith("9"w)); 99 buttons[9].setBezelStyle(NSRoundedBezelStyle); 100 buttons[9].setTag(9); 101 buttons[9].setState(NO); 102 buttons[9].setKeyEquivalent(NSString.stringWith("9"w)); 103 104 foreach (i; 0..10){ 105 buttons[i].setAction(&calcbrain.digit); 106 } 107 108 buttons[10] = NSButton.alloc; buttons[10].initWithFrame(NSMakeRect (77, 30, 34, 24)); 109 buttons[10].setButtonType(NSToggleButton); 110 buttons[10].setTitle(NSString.stringWith("."w)); 111 buttons[10].setBezelStyle(NSRoundedBezelStyle); 112 buttons[10].setState(NO); 113 buttons[10].setKeyEquivalent(NSString.stringWith("."w)); 114 buttons[10].setAction(&calcbrain.decimalSeparator); 115 116 buttons[11] = NSButton.alloc; buttons[11].initWithFrame(NSMakeRect (77, 57, 34, 24)); 117 buttons[11].setButtonType(NSToggleButton); 118 buttons[11].setTitle(NSString.stringWith("SQR"w)); 119 buttons[11].setBezelStyle(NSRoundedBezelStyle); 120 buttons[11].setState(NO); 121 buttons[11].setKeyEquivalent(NSString.stringWith("S"w)); 122 buttons[11].setAction(&calcbrain.squareRoot); 123 124 buttons[12] = NSButton.alloc; buttons[12].initWithFrame(NSMakeRect (3, 30, 34, 24)); 125 buttons[12].setButtonType(NSToggleButton); 126 buttons[12].setTitle(NSString.stringWith("+"w)); 127 buttons[12].setBezelStyle(NSRoundedBezelStyle); 128 buttons[12].setTag(addition); 129 buttons[12].setState(NO); 130 buttons[12].setKeyEquivalent(NSString.stringWith("+"w)); 131 buttons[12].setAction(&calcbrain.operation); 132 133 buttons[13] = NSButton.alloc; buttons[13].initWithFrame(NSMakeRect (40, 30, 34, 24)); 134 buttons[13].setButtonType(NSToggleButton); 135 buttons[13].setTitle(NSString.stringWith("-"w)); 136 buttons[13].setBezelStyle(NSRoundedBezelStyle); 137 buttons[13].setTag(subtraction); 138 buttons[13].setState(NO); 139 buttons[13].setKeyEquivalent(NSString.stringWith("-"w)); 140 buttons[13].setAction(&calcbrain.operation); 141 142 buttons[14] = NSButton.alloc; buttons[14].initWithFrame(NSMakeRect (3, 57, 34, 24)); 143 buttons[14].setButtonType(NSToggleButton); 144 buttons[14].setTitle(NSString.stringWith("*"w)); 145 buttons[14].setBezelStyle(NSRoundedBezelStyle); 146 buttons[14].setTag(multiplication); 147 buttons[14].setState(NO); 148 buttons[14].setKeyEquivalent(NSString.stringWith("*"w)); 149 buttons[14].setAction(&calcbrain.operation); 150 151 buttons[15] = NSButton.alloc; buttons[15].initWithFrame(NSMakeRect (40, 57, 34, 24)); 152 buttons[15].setButtonType(NSToggleButton); 153 buttons[15].setTitle(NSString.stringWith("/"w)); 154 buttons[15].setBezelStyle(NSRoundedBezelStyle); 155 buttons[15].setTag(division); 156 buttons[15].setState(NO); 157 buttons[15].setKeyEquivalent(NSString.stringWith("/"w)); 158 buttons[15].setAction(&calcbrain.operation); 159 160 buttons[16] = NSButton.alloc; buttons[16].initWithFrame(NSMakeRect (3, 84, 34, 24)); 161 buttons[16].setButtonType(NSToggleButton); 162 buttons[16].setTitle(NSString.stringWith("CL"w)); 163 buttons[16].setBezelStyle(NSRoundedBezelStyle); 164 buttons[16].setState(NO); 165 buttons[16].setKeyEquivalent(NSString.stringWith("C"w)); 166 buttons[16].setAction(&calcbrain.clear); 167 168 buttons[17] = NSButton.alloc; buttons[17].initWithFrame(NSMakeRect (3, 3, 34, 24)); 169 buttons[17].setButtonType(NSToggleButton); 170 buttons[17].setTitle(NSString.stringWith("="w)); 171 buttons[17].setBezelStyle(NSRoundedBezelStyle); 172 buttons[17].setState(NO); 173 buttons[17].setKeyEquivalent(NSString.stringWith("="w)); 174 buttons[17].setAction(&calcbrain.equal); 175 176 this.makeFirstResponder(NSResponder(buttons[17]._id)); 177 178 foreach (ref but; buttons) 179 { 180 this.contentView().addSubview(but.contentView()); 181 but.release; 182 } 183 184 this.contentView().addSubview(display.contentView()); 185 display.release; 186 187 this.setTitle(NSString.stringWith("Calculator"w)); 188 189 this.center(); 190 191 calcbrain.setFace(this); 192 } 193 194 void setDisplayedNumber(double aNumber, BOOL displayDecimalSeparator, int fractionalDigits) 195 { 196 import std.format, std.conv; 197 if (displayDecimalSeparator) 198 { 199 display.setStringValue(NSString.stringWith(format(format!"%%#.%df"(fractionalDigits), aNumber).to!wstring)); 200 } 201 else // !displayDecimalSeparator 202 display.setStringValue(NSString.stringWith(format("%.0f", aNumber).to!wstring)); 203 204 } 205 206 void setError(){ 207 display.setStringValue(NSString.stringWith("Error"w)); 208 } 209 } 210 211 /* 212 void onClicked(id self, SEL sel, ...){ 213 import std.conv; 214 import std.stdio; 215 writefln("You clicked the button with title `%s`", NSButton(self).title.UTF8String.to!string); 216 } 217 */ 218 enum : NSInteger{ 219 none = 0, 220 addition = 1, 221 subtraction = 2, 222 multiplication = 3, 223 division = 4 224 }