Source

sage-iphone-app / Classes / EditViewController.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
//
//  EditViewController.m
//  iSage
//
//  Created by Ivan Andrus on 4/29/11.
//  Copyright 2011 Ivan Andrus. All rights reserved.
//

#import "EditViewController.h"
#import "RootViewController.h"
#import "AlephCell.h"
#import "JSONKit.h"
#import "NSData+Base64.h"

@implementation EditViewController

@synthesize selectedObject;
@synthesize delegate;
@synthesize keyboardAccessory;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];

    isFullScreenOutput = NO;
    isLoaded = NO;
    shouldLoad = NO;
    actionSheet = nil;
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *HTMLFile = [bundle pathForResource:@"template" ofType:@"html"];
    NSString *HTMLData = [NSString stringWithContentsOfFile:HTMLFile
                                               usedEncoding:nil
                                                      error:nil];

    NSString *oldOutput = [selectedObject valueForKey:@"html_output"];
    // We don't want (null) showing up...
    if ( !oldOutput && [selectedObject valueForKey:@"output"]) {
        // data migration
        oldOutput = [NSString stringWithFormat:@"<pre>%@</pre>",
                     [selectedObject valueForKey:@"output"]];
    }
    // If it was working but the app was quit in the middle
    if ([[selectedObject valueForKey:@"status"] intValue] == AlephCellStatusHasAlephID ) {
        [selectedObject setValue:[NSNumber numberWithInt:AlephCellStatusHasInput]
                          forKey:@"status"];
    }
    if ( !oldOutput || ([selectedObject isKindOfClass:[AlephCell class]]
                        && ![(AlephCell*)selectedObject canDisplayOutput]) ) {
        oldOutput = @"<span style=\"color:grey;\">There is no output yet.</span>";
    }

    NSString* alephURL = [[NSUserDefaults standardUserDefaults] stringForKey:@"sageServer"];
    if ( [alephURL isEqual:@"CustomURL"] ) {
        alephURL = [[NSUserDefaults standardUserDefaults] stringForKey:@"customSageServer"];
    }
    [outputView loadHTMLString:[NSString stringWithFormat:HTMLData, oldOutput]
                       baseURL:[NSURL URLWithString:alephURL]];

    [self registerForKeyboardNotifications];


    self.navigationItem.title = [selectedObject valueForKey:@"language"];

    evalButton = [[UIBarButtonItem alloc]
                  initWithTitle:@"Evaluate"
                  style:UIBarButtonItemStyleDone
                  target:self
                  action:@selector(eval:)];
    newButton = [[UIBarButtonItem alloc]
                 initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                 target:self
                 action:@selector(newCell:)];
    shareButton = [[UIBarButtonItem alloc]
                   initWithTitle:@"Share"
                   style:UIBarButtonItemStylePlain
                   target:self
                   action:@selector(shareCell:)];

    // Long press ((meant for eval button) but works anywhere in the
    // navigation bar) to select which language.
    longPress = [[UILongPressGestureRecognizer alloc]
                 initWithTarget:self
                 action:@selector(selectLanguage:)];
    longPress.minimumPressDuration = 1.0;

    // Swipe to move cursor
    UISwipeGestureRecognizer *swipeRight =
        [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                  action:@selector(swipe:)];
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    UISwipeGestureRecognizer *swipeLeft =
        [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                  action:@selector(swipe:)];
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;

    // ADDING SWIPE UP & DOWN
    UISwipeGestureRecognizer *swipeUp =
        [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                  action:@selector(swipe:)];
    swipeUp.direction = UISwipeGestureRecognizerDirectionUp;


    UISwipeGestureRecognizer *swipeDown =
        [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                  action:@selector(swipe:)];
    swipeDown.direction = UISwipeGestureRecognizerDirectionDown;

    // Swipe and increase
    UISwipeGestureRecognizer *doubleSwipeRight =
        [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                  action:@selector(swipe:)];
    doubleSwipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    doubleSwipeRight.numberOfTouchesRequired = 2;
    UISwipeGestureRecognizer *doubleSwipeLeft =
        [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                  action:@selector(swipe:)];
    doubleSwipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    doubleSwipeLeft.numberOfTouchesRequired = 2;

    UISwipeGestureRecognizer *doubleSwipeUp =
        [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                  action:@selector(swipe:)];
    doubleSwipeUp.direction = UISwipeGestureRecognizerDirectionLeft;
    doubleSwipeUp.numberOfTouchesRequired = 2;

    UISwipeGestureRecognizer *doubleSwipeDown =
        [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                  action:@selector(swipe:)];
    doubleSwipeDown.direction = UISwipeGestureRecognizerDirectionLeft;
    doubleSwipeDown.numberOfTouchesRequired = 2;

    [inputView addGestureRecognizer:swipeRight];
    [inputView addGestureRecognizer:swipeLeft];
    [inputView addGestureRecognizer:swipeUp];
    [inputView addGestureRecognizer:swipeDown];
    [inputView addGestureRecognizer:doubleSwipeRight];
    [inputView addGestureRecognizer:doubleSwipeLeft];
    [inputView addGestureRecognizer:doubleSwipeUp];
    [inputView addGestureRecognizer:doubleSwipeDown];

    [swipeRight autorelease];
    [swipeLeft autorelease];
    [swipeUp autorelease];
    [swipeDown autorelease];
    [doubleSwipeRight autorelease];
    [doubleSwipeLeft autorelease];
    [doubleSwipeUp autorelease];
    [doubleSwipeDown autorelease];


    // UIWebView does not play well with gesture recognizers.
    UITapGestureRecognizer * twoTaps = [[UITapGestureRecognizer alloc]
                                        initWithTarget:self
                                        action:@selector(twoTaps:)];
    twoTaps.numberOfTapsRequired = 2;
    twoTaps.numberOfTouchesRequired = 1;
    [outputView addGestureRecognizer:twoTaps];
    twoTaps.delegate = self;
    [twoTaps autorelease];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // Start editing right away if there is no output
    if ( [selectedObject valueForKey:@"output"] == nil ) {
        [inputView becomeFirstResponder];
    }

    // NSLog(@"newOut: %@",[[selectedObject valueForKey:@"output"] JSONString]);
    // NSLog(@"return from newOut :%@",[outputView
    // stringByEvaluatingJavaScriptFromString:
    // [NSString stringWithFormat:@"isageSetOutputFromStorage(%@)",
    // [[selectedObject valueForKey:@"output"] JSONString]]]);
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar addGestureRecognizer:longPress];

    inputView.text = [selectedObject valueForKey:@"input"];
    [self updateOutput];

    switch ( [[selectedObject valueForKey:@"status"] intValue] ) {
        case AlephCellStatusHasOutput:
        case AlephCellStatusSyntaxError:
        case AlephCellStatusHasAlephID: // It's working
        {
            NSRange r = [inputView.text rangeOfString:@"interact"];
            [self updateToolbarForEvaluation:(r.location != NSNotFound)];
            break;
        }
        case AlephCellStatusHasInput:
        case AlephCellStatusUninitialized:
        case AlephCellStatusJSONParseError:
        case AlephCellStatusNetworkError:
        default:
            [self updateToolbarForEvaluation:YES];
            break;
    }
}

- (void)updateToolbarForEvaluation:(BOOL)shouldEval{

    if ( [self.navigationItem respondsToSelector:@selector(setRightBarButtonItems:)] ) {
        BOOL isPhone = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone);
        if (isPhone) {

            self.navigationItem.rightBarButtonItems =
            [NSArray arrayWithObjects: newButton, (shouldEval ? evalButton : shareButton), nil];

        } else {
            self.navigationItem.rightBarButtonItems =
            [NSArray arrayWithObjects: newButton, shareButton, evalButton, nil];
            evalButton.style = (shouldEval ? UIBarButtonItemStyleDone : UIBarButtonItemStylePlain);
        }

    } else {
        self.navigationItem.rightBarButtonItem = (shouldEval ? evalButton : newButton);
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationController.navigationBar removeGestureRecognizer:longPress];

    // TODO: Maybe this is actually enough.  Maybe I don't need to poll in the javascript
    [self saveOutput:[outputView stringByEvaluatingJavaScriptFromString:@"isage_get_status()"]];
    [delegate saveEditViewForBackground:self];
}

#pragma mark -
#pragma mark Stuff

- (void)saveOutput:(NSString*)status {

    // NSLog(@"Updating status to %@",status);
    if ( ![selectedObject isKindOfClass:[AlephCell class]] ) {
        NSLog(@"Something really weird happened when evaluating");
        return;
    }
    AlephCell* cell = (AlephCell*)selectedObject;

    // Update status
    // If we don't return in this block then it's assumed we want to save such output as there is
    if ( [status isEqualToString:@"busy"] ) {
        //cell.isWorking = YES;
        cell.status = [NSNumber numberWithInt:AlephCellStatusHasAlephID];
        cell.isWorking = YES;
        return;
    }
    cell.isWorking = NO;
    if ( [status isEqualToString:@"input"] ) {
        cell.status = [NSNumber numberWithInt:AlephCellStatusHasInput];
        return;

    } else if ( [status isEqualToString:@"syntax_error"] ) {
        cell.status = [NSNumber numberWithInt:AlephCellStatusSyntaxError];

    } else if ( [status isEqualToString:@"output"] ) {
        cell.status = [NSNumber numberWithInt:AlephCellStatusHasOutput];

    }

    NSString * out = [outputView stringByEvaluatingJavaScriptFromString:@"isageGetOutputForStorage()"];
    if ( ! [out isEqualToString:@""] ) {

        cell.html_output = out;
        NSRange r1 = [out rangeOfString:@"data:image/png;base64,"];
        if (r1.location == NSNotFound) {
            cell.image = nil;
        } else {

            NSString* sub = [out substringFromIndex:NSMaxRange(r1)];
            NSRange r2 = [sub rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"\""]];

            // For some reason scaling it myself makes it look much nicer
            UIImage * image = [UIImage imageWithData:[NSData dataFromBase64String:[sub substringToIndex:r2.location]]];
            CGSize size = CGSizeMake(70.f, 70.0f);
            UIGraphicsBeginImageContext(size);
            [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
            UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            cell.image = [NSData dataWithData:UIImagePNGRepresentation(scaledImage)];

        }
        out = [outputView stringByEvaluatingJavaScriptFromString:@"isageGetOutputForStorage(true)"];
        cell.output = out;
    }
    [delegate saveEditViewForBackground:self];
}

#pragma mark -
#pragma mark Button methods

- (void)insertNewObjectWithString:(id)initial_value {
    [self newCell:self];
}

- (void)newCell:(id)sender {
    [self newCellWithDefault:@""];
}

- (void)newCellWithDefault:(NSString*)str {
    if ( ![[selectedObject valueForKey:@"input"] isEqual:[inputView text]] ) {
        [selectedObject setValue:[inputView text] forKey:@"input"];
        [selectedObject setValue:[NSNumber numberWithInt:AlephCellStatusHasInput] forKey:@"status"];
    }

    [self.navigationController popViewControllerAnimated:NO];
    [delegate editViewControllerDidFinish:self
                              nextDefault:str
                               inLanguage:([str isEqualToString:@""]
                                           ? nil
                                           : [selectedObject valueForKey:@"language"])];
}


- (void)eval:(id)sender {
    [selectedObject setValue:[inputView text] forKey:@"input"];
    [selectedObject setValue:[NSNumber numberWithInt:AlephCellStatusHasInput] forKey:@"status"];
    if ( !isLoaded ) {
        shouldLoad = YES;

    } else {
        [outputView stringByEvaluatingJavaScriptFromString:
         [NSString stringWithFormat:@"isageSetAndEval(%@,%@)",
          [[inputView text] JSONString],
          [[[selectedObject valueForKey:@"language"] lowercaseString] JSONString]]];
    }
    [self updateToolbarForEvaluation:NO];
    [inputView resignFirstResponder];
}

- (void)selectLanguage:(id)sender {

    BOOL isPhone = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone);
    if (!actionSheet) {
        actionSheet = [[UIActionSheet alloc] initWithTitle:@"Share"
                                                  delegate:self
                                         cancelButtonTitle:(isPhone ? @"Cancel" : nil)
                                    destructiveButtonTitle:nil
                                         otherButtonTitles:
                       @"Sage",
                       @"GAP",
                       @"GP",
                       @"HTML",
                       @"Maxima",
                       @"Octave",
                       @"Python",
                       @"R",
                       @"Singular",
                       nil];
    }
    if ( isPhone ) {
        [actionSheet showInView:[UIApplication sharedApplication].keyWindow];
    } else if ( [sender isKindOfClass:[UILongPressGestureRecognizer class]]
               && [sender state] == UIGestureRecognizerStateBegan ) {

        UIView *superview = [[sender view] superview];
        CGPoint p = [sender locationOfTouch:0 inView:superview];
        [actionSheet showFromRect:CGRectMake(p.x, p.y, 1, 1)
                           inView:superview animated:YES];
    }
}

- (void)shareCell:(id)sender {
    [[delegate shareButtonDelegate] displayShareSheet:self
                                              forCell:(AlephCell*)selectedObject
                                           fromWhence:sender];
}

-(void)didUpdateWithJSON:(NSNotification*)notification{
    // NSLog(@"didUpdateWithJSON edit view");
    [self updateOutput];
}

-(void)updateOutput {
    [self updateImageSize];
}

- (void)hideKeyboard:(id)sender {
    [inputView resignFirstResponder];
}

#pragma mark -
#pragma mark Gestures

- (void)swipe:(id)sender {
    if ( [inputView isFirstResponder] ) {
        NSRange selectedRange = inputView.selectedRange;
        if ( [sender direction] == UISwipeGestureRecognizerDirectionRight ) {
            if ( selectedRange.length == 0 ) {
                selectedRange.location += 1;
            } else {
                selectedRange.location += selectedRange.length;
                selectedRange.length = 0;
            }
        } else if ( [sender direction] == UISwipeGestureRecognizerDirectionLeft ) {
            if ( selectedRange.length == 0 ) {
                selectedRange.location -= 1;
            } else {
                selectedRange.length = 0;
            }
        } else if ( [sender direction] == UISwipeGestureRecognizerDirectionUp ) {
            UITextRange *textSelectedRange = [inputView selectedTextRange];
            CGRect rect = [inputView caretRectForPosition:textSelectedRange.start];
            UITextPosition *newPosition = [inputView closestPositionToPoint:CGPointMake(rect.origin.x, rect.origin.y - inputView.font.leading)];
            int newLocation = [inputView offsetFromPosition:inputView.beginningOfDocument
                                                 toPosition:newPosition];
            selectedRange.location = newLocation;
        } else if ([sender direction] == UISwipeGestureRecognizerDirectionDown ) {
            UITextRange *textSelectedRange = [inputView selectedTextRange];
            CGRect rect = [inputView caretRectForPosition:textSelectedRange.start];
            UITextPosition *newPosition = [inputView closestPositionToPoint:CGPointMake(rect.origin.x, rect.origin.y + inputView.font.leading)];
            int newLocation = [inputView offsetFromPosition:inputView.beginningOfDocument
                                                 toPosition:newPosition];
            selectedRange.location = newLocation;
        }
        inputView.selectedRange = selectedRange;
    }
}

- (void)doubleSwipe:(id)sender {
    if ( [inputView isFirstResponder] ) {
        NSRange selectedRange = inputView.selectedRange;
        selectedRange.length += 1;
        if ( [sender direction] == UISwipeGestureRecognizerDirectionLeft ) {
            selectedRange.location -= 1;
        }
        inputView.selectedRange = selectedRange;
    }
}

- (void)twoTaps:(id)sender {
    isFullScreenOutput = !isFullScreenOutput;
    if (isFullScreenOutput) {
        [inputView resignFirstResponder];
        outputView.frame = outputView.superview.frame;
    }
    [self updateImageSize];
}

#pragma mark -
#pragma mark Gesture Recognizer delegate methods

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

#pragma mark -
#pragma mark - UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)as clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != [as cancelButtonIndex]) {
        NSString * title = [as buttonTitleAtIndex:buttonIndex];
        self.navigationItem.title = title;
        [selectedObject setValue:title forKey:@"language"];
    }
    [self textViewDidChange:inputView]; // Change back to the Evaluate button
}


#pragma mark -
#pragma mark Web view delegate methods

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

    NSURL* url = [request URL];

    if ( [[url scheme] isEqualToString:@"isage"] ) {

        //Do your action here
        if ( [[url host] isEqualToString:@"doneLoading"] ) {
            // [self saveOutput];
            isLoaded = YES;
            if (shouldLoad) {
                [self eval:self];
            }
            shouldLoad = NO;
        } else if ( [[url host] isEqualToString:@"status"] ) {
            [self saveOutput:[[url path] substringFromIndex:1]]; // cut of leading '/'
        } else {
            NSLog(@"passed data from web view : %@",url);
        }
        return NO;
    }
    return YES;
}

#pragma mark -
#pragma mark Text view delegate methods

- (BOOL)textViewShouldBeginEditing:(UITextView *)aTextView {

    // Load the nib for the accessory view
    if (aTextView.inputAccessoryView == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"keyboardAccessory" owner:self options:nil];
        // Loading the AccessoryView nib file sets the accessoryView outlet.
        aTextView.inputAccessoryView = keyboardAccessory;
    }

    return YES;
}

- (void)textViewDidBeginEditing:(UITextView *)textview {
    activeField = textview;
}

- (void)textViewDidEndEditing:(UITextView *)textview {
    activeField = nil;
}

- (void)textViewDidChange:(UITextView *)textView {
    [self updateToolbarForEvaluation:YES];
}

#pragma mark -
#pragma mark Accessory View

- (IBAction)insertDelimiters:(id)sender {
    // When the accessory view button is tapped, add a suitable string to the text view.
    NSMutableString *text = [inputView.text mutableCopy];
    NSRange selectedRange = inputView.selectedRange;

    NSString* delimiters = [sender title];
    [text insertString:[delimiters substringFromIndex:1] atIndex:selectedRange.location+selectedRange.length];
    [text insertString:[delimiters substringToIndex:1] atIndex:selectedRange.location];

    selectedRange.location += 1;
    inputView.text = text;
    inputView.selectedRange = selectedRange;
    [text release];
}

- (IBAction)insertTab:(id)sender {
    // When the accessory view button is tapped, add a suitable string to the text view.
    NSMutableString *text = [inputView.text mutableCopy];
    NSRange selectedRange = inputView.selectedRange;

    // Set text
    [text replaceCharactersInRange:selectedRange withString:@"    "];
    inputView.text = text;
    [text release];

    // Set cursor position
    selectedRange.location += 4;  // we put 4 spaces in -- we maybe want to change this to a tab
    selectedRange.length = 0;
    inputView.selectedRange = selectedRange;
}

- (IBAction)insertCharacter:(id)sender {
    // When the accessory view button is tapped, add a suitable string to the text view.
    NSMutableString *text = [inputView.text mutableCopy];
    NSRange selectedRange = inputView.selectedRange;

    NSString * ins = [sender title];
    if ( selectedRange.length == 0 && selectedRange.location > 0
        && [ins isEqualToString:@">"]
        && [text characterAtIndex:selectedRange.location-1 ] == '>' )
    {
        selectedRange.location -= 1;
        selectedRange.length += 1;
        ins = @"<";
    }

    // Set text
    [text replaceCharactersInRange:selectedRange withString:ins];
    inputView.text = text;
    [text release];

    // Set cursor position
    selectedRange.location += 1;
    selectedRange.length = 0;
    inputView.selectedRange = selectedRange;
}


#pragma mark -
#pragma mark Keyboard management

// for EditViewController protocol
- (void)registerForKeyboardNotifications
{
}

#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
}


- (void)dealloc {
    if ( [self.navigationItem respondsToSelector:@selector(setRightBarButtonItems:)] ) {
        self.navigationItem.rightBarButtonItems = nil;
    } else {
        self.navigationItem.rightBarButtonItem = nil;
    }
    [longPress release];
    [actionSheet release];
    [newButton release];
    [evalButton release];
    [shareButton release];
    [outputView setDelegate:nil];

    [super dealloc];
}

#pragma mark -
#pragma mark View rotation

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    [self updateImageSize];
    [outputView stringByEvaluatingJavaScriptFromString:@"updateWidth()"];
}


- (void)updateImageSize {
    // Make the input area unobscured by the keyboard
    // The image view doesn't resize well, so use the outputView's size instead
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        if ( UIInterfaceOrientationIsLandscape(self.interfaceOrientation) ) {
            inputView.frame = CGRectMake( 5, 5, inputView.superview.frame.size.width-10, 67 - 11 );
            //outputView.frame = outputView.frame;
        } else {
            inputView.frame = CGRectMake( 5, 5, 310, 105 );
            //outputView.frame = outputView.frame;
        }
    }
    if ( !isFullScreenOutput ) {
        int yoffset = inputView.frame.size.height+10;
        outputView.frame = CGRectMake( 0, yoffset,
                                      inputView.superview.frame.size.width,
                                      inputView.superview.frame.size.height - yoffset);
    }
}


@end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.