-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPersianCalculatorLandscape.m
840 lines (673 loc) · 33.4 KB
/
PersianCalculatorLandscape.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
//
// PersianCalculatorLandscape.m
// PersianCalculator
//
// Created by volek on 4/10/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
// 43.405x963.134
#import "PersianCalculatorLandscape.h"
#import "CalculatorViewController.h"
#import "PlotCalc.h"
@implementation PersianCalculatorLandscape
@synthesize display;
@synthesize degreeLabel;
@synthesize sinButton ;
@synthesize cosButton;
@synthesize tanButton;
@synthesize lnButton;
@synthesize sinhButton;
@synthesize coshButton;
@synthesize tanhButton;
@synthesize exButton;
@synthesize calcRecord;
@synthesize inputedFormula ;
@synthesize magnifiedLetter;
@synthesize magnifiedLetterRubOut;
@synthesize memory;
int secondButtonFlag = 0 ;
int radianFlag = 1 ;
bool soundFlag;
NSError *eval_error = nil;
NSString *fractionValue;
-(IBAction) userTouchDown:(id)sender{
UIButton *keyButton = (UIButton*) sender ;
[keyButton setContentMode:UIViewContentModeCenter];
CGRect keyButtonFrame = keyButton.frame;
CGRect magFrame = magnifiedLetter.frame;
magFrame.origin.x = (keyButtonFrame.origin.x) - 3;
magFrame.origin.y = keyButtonFrame.origin.y - 60;
if(keyButton.tag == 8) {
magnifiedLetterRubOut.frame = magFrame;
magnifiedLetterRubOut.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:magnifiedLetterRubOut];
[self.view bringSubviewToFront:magnifiedLetterRubOut ];
magnifiedLetterRubOut.hidden=NO;
}
else {
magnifiedLetter.frame = magFrame;
magnifiedLetter.contentMode = UIViewContentModeScaleAspectFit;
UILabel *tempText = [[UILabel alloc] initWithFrame:CGRectMake(10, -20, 40, 90)];
tempText.text = NSLocalizedString(keyButton.titleLabel.text,nil);
tempText.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(18.0)];
tempText.backgroundColor = [UIColor clearColor];
tempText.contentMode = UIViewContentModeCenter;
tempText.adjustsFontSizeToFitWidth = YES ;
tempText.textAlignment = UITextAlignmentCenter;
tempText.tag = 500;
[magnifiedLetter addSubview:tempText];
[tempText release];
[self.view addSubview:magnifiedLetter];
[self.view bringSubviewToFront:magnifiedLetter ];
magnifiedLetter.hidden=NO;
}
}
-(IBAction) numericKeyPressed: (id)sender{
UIButton *keyButton = (UIButton*)sender;
if (soundFlag) {
[theAudio play];
}
//user lifts his finger, remove magnifying letter from view
magnifiedLetter.hidden=YES;
[[magnifiedLetter viewWithTag:500] removeFromSuperview];
[[self.view viewWithTag:2000] removeFromSuperview];
display.text = [display.text stringByAppendingString:NSLocalizedString(keyButton.titleLabel.text,nil)];
inputedFormula = [inputedFormula stringByAppendingString:keyButton.titleLabel.text];
}
-(IBAction) signPressed: (id) sender {
UIButton *signButton = (UIButton*)sender;
if (soundFlag) {
[theAudio play];
}
//user lifts his finger, remove magnifying letter from view
magnifiedLetter.hidden=YES;
[[magnifiedLetter viewWithTag:500] removeFromSuperview];
[[self.view viewWithTag:2000] removeFromSuperview];
if ([signButton.titleLabel.text isEqualToString:@"("] ) {
display.text = [display.text stringByAppendingString:signButton.titleLabel.text];
inputedFormula = [inputedFormula stringByAppendingString:signButton.titleLabel.text];
}
else if ([signButton.titleLabel.text isEqualToString:@")"] ) {
display.text = [display.text stringByAppendingString:signButton.titleLabel.text];
inputedFormula = [inputedFormula stringByAppendingString:signButton.titleLabel.text];
}
else if ([signButton.titleLabel.text isEqualToString:@"m+"] )
{
if (display.text.length > 0){
NSString *tempDisplay = [self convertLocalizedToUSNumbers:display.text];
int tempValue= [tempDisplay intValue] + [self.memory intValue];
self.memory = [NSString stringWithFormat:@"%d", tempValue];
//set state of the mr(tag=13) button to active
UIButton *m_plus = (UIButton *)[signButton.superview viewWithTag:13];
[m_plus setBackgroundImage:[UIImage imageNamed:@"green_button_gloss.png"] forState:UIControlStateNormal];
}
}
else if ([signButton.titleLabel.text isEqualToString:@"m-"] )
{
if (display.text.length > 0){
NSString *tempDisplay = [self convertLocalizedToUSNumbers:display.text];
int tempValue= [tempDisplay intValue] - [self.memory intValue];
self.memory = [NSString stringWithFormat:@"%d", tempValue];
//set state of the m- button to active
}
}
else if ([signButton.titleLabel.text isEqualToString:@"mr"] )
{
if (self.memory > 0){
display.text = [display.text stringByAppendingString:[self convertUSToLocalizedNumbers:self.memory]];
}
}
else if ([signButton.titleLabel.text isEqualToString:@"mc"] )
{
self.memory = nil;
UIButton *m_plus = (UIButton *)[signButton.superview viewWithTag:13];
[m_plus setBackgroundImage:[UIImage imageNamed:@"white_button_gloss.png"] forState:UIControlStateNormal];
}
else if ([signButton.titleLabel.text isEqualToString:@"%"] ) {
display.text = [display.text stringByAppendingString:signButton.titleLabel.text];
inputedFormula = [inputedFormula stringByAppendingString:signButton.titleLabel.text];
}
else if ([signButton.titleLabel.text isEqualToString:NSLocalizedString(@"1/x",nil) ] ){
display.text = [display.text stringByAppendingString:NSLocalizedString(@"1/",nil)];
}
else if ([signButton.titleLabel.text isEqualToString:NSLocalizedString(@"x2",nil)] ) {
display.text = [display.text stringByAppendingString:NSLocalizedString(@"**2",nil)];
}
else if ([signButton.titleLabel.text isEqualToString:NSLocalizedString(@"x3",nil)] ) {
display.text = [display.text stringByAppendingString:NSLocalizedString(@"**3",nil)];
}
else if ([signButton.titleLabel.text isEqualToString:@"yx"] ) {
display.text = [display.text stringByAppendingString:@"**"];
}
else if ([signButton.titleLabel.text isEqualToString:@"x!"] ) {
display.text = [display.text stringByAppendingString:@"!"];
}
else if ([signButton.titleLabel.text isEqualToString:@"√"] ) {
display.text = [display.text stringByAppendingString:@"sqrt("];
}
else if ([signButton.titleLabel.text isEqualToString:@"y^x"] ) {
display.text = [display.text stringByAppendingString:@"^"];
}
else if ([signButton.titleLabel.text isEqualToString:@"log"] ) {
display.text = [display.text stringByAppendingString:@"log("];
}
else if ([signButton.titleLabel.text isEqualToString:@"sin"] ) {
if (radianFlag == 1) {
display.text = [display.text stringByAppendingString:@"sin("];
} else {
display.text = [display.text stringByAppendingString:@"sin(dtor("];
//display.text = [display.text stringByAppendingString:@"sin°);
}
}
else if ([signButton.titleLabel.text isEqualToString:@"cos"] ) {
if (radianFlag == 1) {
display.text = [display.text stringByAppendingString:@"cos("];
}
else {
display.text = [display.text stringByAppendingString:@"cos(dtor("];
}
}
else if ([signButton.titleLabel.text isEqualToString:@"tan"] ) {
if (radianFlag == 1) {
display.text = [display.text stringByAppendingString:@"tan("];
}
else {
display.text = [display.text stringByAppendingString:@"tan(dtor("];
}
}
else if ([signButton.titleLabel.text isEqualToString:@"ln"] ) {
display.text = [display.text stringByAppendingString:@"ln("];
}
else if ([signButton.titleLabel.text isEqualToString:@"sinh"] ) {
if (radianFlag == 1) {
display.text = [display.text stringByAppendingString:@"sinh("];
}
else {
display.text = [display.text stringByAppendingString:@"sinh(dtor("];
}
}
else if ([signButton.titleLabel.text isEqualToString:@"cosh"] ) {
if (radianFlag == 1) {
display.text = [display.text stringByAppendingString:@"cosh("];
}
else {
display.text = [display.text stringByAppendingString:@"cosh(dtor("];
}
}
else if ([signButton.titleLabel.text isEqualToString:@"tanh"] ) {
if (radianFlag == 1) {
display.text = [display.text stringByAppendingString:@"tanh("];
}
else {
display.text = [display.text stringByAppendingString:@"tanh(dtor("];
}
}
else if ([signButton.titleLabel.text isEqualToString:@"ex"] ) {
display.text = [display.text stringByAppendingString:
NSLocalizedString(@"2.718252**",nil)];
}
else if ([signButton.titleLabel.text isEqualToString:@"π"] ) {
display.text = [display.text stringByAppendingString:
NSLocalizedString(@"3.141592653589793",nil)];
}
else if ([signButton.titleLabel.text isEqualToString:@"EE"] ) {
display.text = [display.text stringByAppendingString:
NSLocalizedString(@"10^",nil)];
}
else if ([signButton.titleLabel.text isEqualToString:@"Rand"] ) {
display.text = [display.text stringByAppendingString:NSLocalizedString(@"random(0,1000)",nil)];
}
else if ([signButton.titleLabel.text isEqualToString:NSLocalizedString(@"sin-1",nil)] ) {
if (radianFlag == 1) {
display.text = [display.text stringByAppendingString:@"asin("];
}
else {
display.text = [display.text stringByAppendingString:@"asin(dtor("];
}
}
else if ([signButton.titleLabel.text isEqualToString:NSLocalizedString(@"cos-1",nil)] ) {
if (radianFlag == 1) {
display.text = [display.text stringByAppendingString:@"acos("];
}
else {
display.text = [display.text stringByAppendingString:@"acos(dtor("];
}
}
else if ([signButton.titleLabel.text isEqualToString:NSLocalizedString(@"tan-1",nil)] ) {
if (radianFlag == 1) {
display.text = [display.text stringByAppendingString:@"atan("];
}
else {
display.text = [display.text stringByAppendingString:@"atan(dtor("];
}
}
else if ([signButton.titleLabel.text isEqualToString:NSLocalizedString(@"log2",nil)] ) {
display.text = [display.text stringByAppendingString:NSLocalizedString(@"log2(",nil)];
}
else if ([signButton.titleLabel.text isEqualToString:NSLocalizedString(@"sinh-2",nil)] ) {
if (radianFlag == 1) {
display.text = [display.text stringByAppendingString:@"asinh("];
}
else {
display.text = [display.text stringByAppendingString:@"asinh(dtor("];
}
}
else if ([signButton.titleLabel.text isEqualToString:NSLocalizedString(@"cosh-1",nil)] ) {
if (radianFlag == 1) {
display.text = [display.text stringByAppendingString:@"acosh("];
}
else {
display.text = [display.text stringByAppendingString:@"acosh(dtor("];
}
}
else if ([signButton.titleLabel.text isEqualToString:NSLocalizedString(@"tanh-1",nil)] ) {
if (radianFlag == 1) {
display.text = [display.text stringByAppendingString:@"atanh("];
}
else {
display.text = [display.text stringByAppendingString:@"atanh(dtor("];
}
}
else if ([signButton.titleLabel.text isEqualToString:NSLocalizedString(@"2^x",nil)] ) {
display.text = [display.text stringByAppendingString:NSLocalizedString(@"2**",nil)];
}
else if ([signButton.titleLabel.text isEqualToString:@"+"] ) {
display.text = [display.text stringByAppendingString:signButton.titleLabel.text];
}
else if ([signButton.titleLabel.text isEqualToString:@"-"] ) {
display.text = [display.text stringByAppendingString:signButton.titleLabel.text];
}
else if ([signButton.titleLabel.text isEqualToString:@"×"] ) {
display.text = [display.text stringByAppendingString:signButton.titleLabel.text];
}
else if ([signButton.titleLabel.text isEqualToString:@"÷"] ) {
display.text = [display.text stringByAppendingString:@"÷"];
}
else if ([signButton.titleLabel.text isEqualToString:@"+/-"] ) {
if([display.text hasSuffix:@"-"] )
{
display.text = [display.text substringToIndex:display.text.length -1];
}else{
display.text = [display.text stringByAppendingString:@"(-"];
}
}
else if ([signButton.titleLabel.text isEqualToString:@"Deg"] ) {
[signButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[signButton setTitle:@"Rad" forState:UIControlStateNormal ];
degreeLabel.text = @"Deg";
//set Radian falg to be checked when user inputs Trigonometry
radianFlag = 0 ;
}
else if ([signButton.titleLabel.text isEqualToString:@"Rad"] ) {
[signButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[signButton setTitle:@"Deg" forState:UIControlStateNormal ];
degreeLabel.text = @"Rad";
//set Radian falg to be checked when user inputs Trigonometry
radianFlag = 1 ;
}
signButton.titleLabel.text= NSLocalizedString(signButton.titleLabel.text,nil);
}
-(NSString*) convertUSToLocalizedNumbers:(NSString*)result{
result= [result stringByReplacingOccurrencesOfString:@"1" withString:NSLocalizedString(@"1", nil)];
result =[result stringByReplacingOccurrencesOfString:@"2" withString:NSLocalizedString(@"2", nil)];
result =[result stringByReplacingOccurrencesOfString:@"3" withString:NSLocalizedString(@"3", nil)];
result =[result stringByReplacingOccurrencesOfString:@"4" withString:NSLocalizedString(@"4", nil)];
result =[result stringByReplacingOccurrencesOfString:@"5" withString:NSLocalizedString(@"5", nil)];
result =[result stringByReplacingOccurrencesOfString:@"6" withString:NSLocalizedString(@"6", nil)];
result =[result stringByReplacingOccurrencesOfString:@"7" withString:NSLocalizedString(@"7", nil)];
result =[result stringByReplacingOccurrencesOfString:@"8" withString:NSLocalizedString(@"8", nil)];
result =[result stringByReplacingOccurrencesOfString:@"9" withString:NSLocalizedString(@"9", nil)];
result =[result stringByReplacingOccurrencesOfString:@"0" withString:NSLocalizedString(@"0", nil)];
result =[result stringByReplacingOccurrencesOfString:@"." withString:NSLocalizedString(@".", nil)];
result =[result stringByReplacingOccurrencesOfString:@"*" withString:NSLocalizedString(@"x", nil)];
//display.text = result;
return result;
}
-(NSString*) convertLocalizedToUSNumbers:(NSString*)result{
result = [result stringByReplacingOccurrencesOfString:NSLocalizedString(@"1",nil)withString: @"1"];
result = [result stringByReplacingOccurrencesOfString:NSLocalizedString(@"2",nil)withString: @"2"];
result = [result stringByReplacingOccurrencesOfString:NSLocalizedString(@"3",nil)withString: @"3"];
result = [result stringByReplacingOccurrencesOfString:NSLocalizedString(@"4",nil)withString: @"4"];
result = [result stringByReplacingOccurrencesOfString:NSLocalizedString(@"5",nil)withString: @"5"];
result = [result stringByReplacingOccurrencesOfString:NSLocalizedString(@"6",nil)withString: @"6"];
result = [result stringByReplacingOccurrencesOfString:NSLocalizedString(@"7",nil)withString: @"7"];
result = [result stringByReplacingOccurrencesOfString:NSLocalizedString(@"8",nil)withString: @"8"];
result = [result stringByReplacingOccurrencesOfString:NSLocalizedString(@"9",nil)withString: @"9"];
result = [result stringByReplacingOccurrencesOfString:NSLocalizedString(@"0",nil)withString: @"0"];
result = [result stringByReplacingOccurrencesOfString:NSLocalizedString(@".",nil)withString: @"."];
result = [result stringByReplacingOccurrencesOfString:NSLocalizedString(@"x",nil)withString: @"*"];
return result;
}
-(IBAction) equalButtonLandscape:(id)sender {
if (soundFlag) {
[theAudio play];
}
if (display.text.length < 1)
{ // do nothing user inputed nothing
}
else {
eval_error = nil;
display.text = [self convertLocalizedToUSNumbers:display.text];
DDMathEvaluator *eval = [DDMathEvaluator sharedMathEvaluator];
NSNumber *eval_result= nil;
eval_result = [eval evaluateString:display.text withSubstitutions:nil error:&eval_error];
if(eval_result != nil & eval_error == nil) {
DLog(@"eval_result !=nill");
// format result with user after ecimial decision in mind.
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setRoundingMode: NSNumberFormatterRoundUp];
numberFormatter.maximumFractionDigits = [fractionValue integerValue] ;
display.text = [display.text stringByAppendingString:@"="];
display.text = [display.text stringByAppendingString: [numberFormatter stringFromNumber:eval_result]];
display.text = [self convertUSToLocalizedNumbers:display.text];
display.text = [ display.text stringByAppendingString:@"\n"];
// add results to result array for record keeping.
[calcRecord addObject:display.text ];
[numberFormatter release];
}
else {
if(eval_error==nil) {}
else {
NSString * ErrorCode = @"errorcode" ;
ErrorCode = [ErrorCode stringByAppendingString:[NSString stringWithFormat:@"%d", eval_error.code]];
display.text =[display.text stringByAppendingFormat:@"="];
display.text = [self convertUSToLocalizedNumbers:display.text];
display.text = [display.text stringByAppendingString:NSLocalizedString(ErrorCode, nill)];
display.text = [display.text stringByAppendingString:@"\n"];
}
}
display.selectedRange = NSMakeRange(display.text.length - 1, 0);
}
}
-(IBAction)emailResults {
if (soundFlag) {
[theAudio play];
}
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
composer.mailComposeDelegate = self ;
if ([MFMailComposeViewController canSendMail]) {
NSLog(@"Size of array = %d",[calcRecord count]);
NSString *emailMessage ;
emailMessage = [calcRecord componentsJoinedByString:@" "];
emailMessage = [emailMessage stringByAppendingString:@"\n"];
[composer setToRecipients:[NSArray arrayWithObjects:@"[email protected]",nil ]];
[composer setSubject:NSLocalizedString(@"Calculator Results:",nil)];
[composer setMessageBody:emailMessage isHTML:NO];
[self presentModalViewController:composer animated:YES];
//[self presentViewController:composer animated:YES];
}
[composer release];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self dismissModalViewControllerAnimated:YES];
if (result == MFMailComposeResultFailed) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failed" message:@"Failed to sendemail" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[alert show ];
[alert release];
}
}
// "2sn" Button was pressed, Change certain buttons for their second fucntionality
-(IBAction) secondButton:(id)sender{
if (soundFlag) {
[theAudio play];
}
UIButton *myButton = (UIButton*) sender ;
//keyButton.tag = 1000;
[myButton setContentMode:UIViewContentModeCenter];
CGRect keyButtonFrame = myButton.frame;
CGRect magFrame = magnifiedLetter.frame;
magFrame.origin.x = (keyButtonFrame.origin.x) - 3;
magFrame.origin.y = keyButtonFrame.origin.y - 60;
magnifiedLetter.frame = magFrame;
magnifiedLetter.contentMode = UIViewContentModeScaleAspectFit;
UILabel *tempText = [[UILabel alloc] initWithFrame:CGRectMake(10, -20, 40, 90)];
tempText.text = myButton.titleLabel.text;
tempText.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(18.0)];
tempText.backgroundColor = [UIColor clearColor];
tempText.contentMode = UIViewContentModeCenter;
tempText.adjustsFontSizeToFitWidth = YES ;
tempText.textAlignment = UITextAlignmentCenter;
tempText.tag = 500;
[magnifiedLetter addSubview:tempText];
[tempText release];
[self.view addSubview:magnifiedLetter];
[self.view bringSubviewToFront:magnifiedLetter ];
magnifiedLetter.hidden=NO;
UIButton *keyButton = (UIButton*)sender;
//test to see if secondButton is not pressed, then set state to press if it is true.
if (secondButtonFlag == 0 ) {
secondButtonFlag = 1 ;
[keyButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[keyButton setBackgroundImage:[UIImage imageNamed:@"green_button_gloss.png"] forState:UIControlStateNormal];
[sinButton setTitle:NSLocalizedString(@"sin-1",nil) forState:UIControlStateNormal ];
[cosButton setTitle:NSLocalizedString(@"cos-1",nil) forState:UIControlStateNormal ];
[tanButton setTitle:NSLocalizedString(@"tan-1",nil) forState:UIControlStateNormal ];
[lnButton setTitle:NSLocalizedString(@"log2",nil) forState:UIControlStateNormal ];
[sinhButton setTitle:NSLocalizedString(@"sinh-1",nil) forState:UIControlStateNormal ];
[coshButton setTitle:NSLocalizedString(@"cosh-1",nil) forState:UIControlStateNormal ];
[tanhButton setTitle:NSLocalizedString(@"tanh-1",nil) forState:UIControlStateNormal ];
[exButton setTitle:NSLocalizedString(@"2^x",nil) forState:UIControlStateNormal ];
}
else {
secondButtonFlag = 0 ; // set back secondButton flag to 0
[keyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[keyButton setBackgroundImage:[UIImage imageNamed:@"white_button_gloss.png"] forState:UIControlStateNormal];
[sinButton setTitle:@"sin" forState:UIControlStateNormal ];
[cosButton setTitle:@"cos" forState:UIControlStateNormal ];
[tanButton setTitle:@"tan" forState:UIControlStateNormal ];
[lnButton setTitle:@"ln" forState:UIControlStateNormal ];
[sinhButton setTitle:@"sinh" forState:UIControlStateNormal ];
[coshButton setTitle:@"cosh" forState:UIControlStateNormal ];
[tanhButton setTitle:@"tanh" forState:UIControlStateNormal ];
[exButton setTitle:@"ex" forState:UIControlStateNormal ];
}
}
- (IBAction)showInfo:(id)sender {
InfoViewController *controller = [[[InfoViewController alloc] initWithNibName:@"InfoView" bundle:nil] autorelease];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController presentModalViewController:controller animated:YES];
//[controller release];
}
-(IBAction)clear {
if (soundFlag) {
[theAudio play];
}
display.text = @"";
}
//implement the delete button on dislay text area
#pragma mark removecharacter
-(IBAction) removeLastCharacter{
if (soundFlag) {
[theAudio play];
}
magnifiedLetterRubOut.hidden=YES;
//[[magnifiedLetter viewWithTag:500] removeFromSuperview];
[[self.view viewWithTag:2001] removeFromSuperview];
if (display.text.length >0) {
NSRange selectedRange = display.selectedRange;
//Erase the character at the spot user pressed his finer
if ( selectedRange.location > 0 ) {
NSString *tempString = [display.text substringFromIndex:selectedRange.location];
display.text = [display.text substringToIndex:selectedRange.location -1];
display.text = [display.text stringByAppendingString:tempString];
}
}
}
//disable iphone keyboard on text area
#pragma mark UITextViewDelegate
- (BOOL) textViewShouldBeginEditing:(UITextView *)textView{
//NSRange range = NSMakeRange([[textView textStorage] length], 0);
//[textView setSelectedRange: range];
//[textView resignFirstResponder];
//return NO; // return no, so there will be no keyoard display
//display.inputView = [[UIView new] autorelease];
return YES;
}
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification*)aNotification
{
[display resignFirstResponder];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil whichTab:(int)tabselected
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
selectedTabindex = tabselected;
[self registerForKeyboardNotifications];
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)initMagnifiedLetter {
UIImage *magniferImage = [UIImage imageNamed:@"keysymbol.png"];
self.magnifiedLetter = [[[ UIImageView alloc ] initWithFrame:CGRectMake(0.0, 0.0, magniferImage.size.width, magniferImage.size.height)] autorelease];
magnifiedLetter.image = magniferImage;
self.magnifiedLetter.tag = 2000 ;
// opaque for better performance
self.magnifiedLetter.opaque = YES;
// construct an UIImage view to host the magnifying effect for rubout
UIImage *magniferImageRubOut = [UIImage imageNamed:@"keysymbolrub.png"];
// magnifiedLetterRubOut = [[ UIImageView alloc ] initWithFrame:CGRectMake(0.0, 0.0, magniferImageRubOut.size.width, magniferImageRubOut.size.height)];
magnifiedLetterRubOut = [[ UIImageView alloc ] initWithFrame:CGRectMake(0.0, 0.0, magniferImage.size.width, magniferImage.size.height)];
magnifiedLetterRubOut.image = magniferImageRubOut;
self.magnifiedLetterRubOut.tag = 2001 ;
self.magnifiedLetterRubOut.opaque = YES;
//[magnifiedLetter release];
}
#pragma mark - viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"We are in landscape mode");
//self.navigationController.navigationBarHidden = YES;
//self.tabBarController.tabBar.hidden = YES;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
soundFlag= [userDefaults boolForKey:@"audioSwitch"];
fractionValue = [userDefaults stringForKey:@"Fraction"];
calcRecord = [[NSMutableArray alloc] init];
// Do any additional setup after loading the view from its nib.
//attach an inputView to be able to hide keyboard on display UITextView
display.inputView = [[UIView new] autorelease];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gradientBackground.jpg"]];
imageView.frame = CGRectMake(0, 0, display.frame.size.width, display.frame.size.height );
[display addSubview: imageView];
[display sendSubviewToBack:imageView];
[imageView release];
NSString *path = [[NSBundle mainBundle] pathForResource:@"button5" ofType:@"wav"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio prepareToPlay];
[[display layer] setCornerRadius:5];
//Call initMagnifiedLetter to set magnification Icon
[self initMagnifiedLetter];
// look for all BUttons on the main view and localize their title string.
for(UIView *view in [self.view subviews]) {
if([view isKindOfClass:[UIButton class]]) {
UIButton *btn = (UIButton*)view;
btn.titleLabel.text = NSLocalizedString(btn.titleLabel.text, nil);
[btn setTitle:NSLocalizedString(btn.titleLabel.text,nil) forState:UIControlStateNormal];
}
else{}
}
//Localize any strings on the views
[LocalizationHelper localizeView:self.view];
[[self tabBarItem] setTitle:NSLocalizedString(@"Scientific",nil)];
}
- (IBAction)showDoneMessage:(id)sender {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Calc Preferences!"
message:NSLocalizedString(@"You need to exit and come back for Preferences to take effect.",nil)
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK",nil)
otherButtonTitles:nil];
[message show];
[message release];
}
- (void)viewDidUnload
{
[[NSUserDefaults standardUserDefaults] synchronize];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if ( (toInterfaceOrientation == UIInterfaceOrientationPortrait) || (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) )
{
if (selectedTabindex == 0)
{
CalculatorViewController *calcViewController = [[[[CalculatorViewController alloc] init] initWithNibName:@"CalculatorViewController" bundle:nil]autorelease];
NSMutableArray* tabbarArray = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
[tabbarArray replaceObjectAtIndex:0 withObject:calcViewController];
[self.tabBarController setViewControllers:tabbarArray animated:YES];
UIViewController *tab1 = [tabbarArray objectAtIndex:0];
tab1.tabBarItem.image = [UIImage imageNamed:@"161-calculator.png"];
tab1.tabBarItem.title = NSLocalizedString(@"Calculator",nill);
}
else if (selectedTabindex == 1) {
PlotCalc *plotViewController = [[[[PlotCalc alloc] init] initWithNibName:@"PlotCalc" bundle:nil]autorelease];
NSMutableArray* tabbarArray = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
[tabbarArray replaceObjectAtIndex:1 withObject:plotViewController];
[self.tabBarController setViewControllers:tabbarArray animated:YES];
UIViewController *tab1 = [tabbarArray objectAtIndex:1];
tab1.tabBarItem.image = [UIImage imageNamed:@"16-line-chart.png"];
tab1.tabBarItem.title = NSLocalizedString(@"Plot",nill);
}
else if (selectedTabindex == 2) {
CurrencyViewController *currencyViewController = [[[[CurrencyViewController alloc] init] initWithNibName:@"CurrencyView" bundle:nil]autorelease];
NSMutableArray* tabbarArray = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
[tabbarArray replaceObjectAtIndex:2 withObject:currencyViewController];
[self.tabBarController setViewControllers:tabbarArray animated:YES];
UIViewController *tab1 = [tabbarArray objectAtIndex:2];
tab1.tabBarItem.image = [UIImage imageNamed:@"190-bank.png"];
tab1.tabBarItem.title = NSLocalizedString(@"Currency",nill);
}
else if (selectedTabindex == 3) {
Units *unitViewController = [[[[Units alloc] init] initWithNibName:@"Units" bundle:nil]autorelease];
NSMutableArray* tabbarArray = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
[tabbarArray replaceObjectAtIndex:3 withObject:unitViewController];
[self.tabBarController setViewControllers:tabbarArray animated:YES];
UIViewController *tab1 = [tabbarArray objectAtIndex:3];
tab1.tabBarItem.image = [UIImage imageNamed:@"138-scales.png"];
tab1.tabBarItem.title = NSLocalizedString(@"Units",nill);
}
}
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
//CGRect previousRect = self.view.frame;
UIInterfaceOrientation toOrientation = self.interfaceOrientation;
if ( self.tabBarController.view.subviews.count >= 2 )
{
UIView *transView = [self.tabBarController.view.subviews objectAtIndex:0];
UIView *tabBar = [self.tabBarController.view.subviews objectAtIndex:1];
if ( (toOrientation == UIInterfaceOrientationPortrait) || (toOrientation == UIInterfaceOrientationPortraitUpsideDown) ) {
//transView.frame = CGRectMake(0, 0, 480, 320 );
tabBar.hidden = FALSE;
}
else
{
transView.frame = CGRectMake(0, 0, 480, 320 );
tabBar.hidden = TRUE;
}
}
}
- (void)dealloc
{
[magnifiedLetter release];
[calcRecord release];
[super dealloc];
}
@end