2 // LiveCodingViewController.m
5 // Created by Axel Balley on 30/10/08.
6 // Copyright 2008 __MyCompanyName__. All rights reserved.
9 #import "LiveCodingViewController.h"
11 extern void rtf2txt(char *txt);
13 @implementation LiveCodingViewController
15 - (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
17 if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
27 [textView setAutocapitalizationType:UITextAutocapitalizationTypeNone];
28 [textView setAutocorrectionType:UITextAutocorrectionTypeNo];
29 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
30 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
31 [self showButtons:NO];
34 - (void) keyboardDidShow:(NSNotification *) notif
36 NSDictionary *info = [notif userInfo];
37 NSValue *val = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
38 CGRect r = [val CGRectValue];
40 CGRect done_frame = doneButton.frame;
41 CGRect exec_frame = execButton.frame;
43 r = [self.view convertRect:r fromView:nil];
45 done_frame.origin.y = r.origin.y - done_frame.size.height - 10;
46 exec_frame.origin.y = done_frame.origin.y;
48 [doneButton setFrame:done_frame];
49 [execButton setFrame:exec_frame];
51 [self showButtons:YES];
54 - (void) keyboardWillHide:(NSNotification *) notif
56 [self showButtons:NO];
59 - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
64 - (void) setTarget:(id)t withSelector:(SEL)s
70 - (void) loadFile:(NSString *)file
72 NSString *contents = [NSString stringWithContentsOfFile:file encoding:NSASCIIStringEncoding error:nil];
73 int length = [contents length];
74 char *buf = (char *) malloc(length+1);
75 [contents getCString:buf maxLength:length+1 encoding:NSASCIIStringEncoding];
77 while (*buf=='\n') buf++;
79 [textView setText:[NSString stringWithCString:buf encoding:NSASCIIStringEncoding]];
82 - (void) showButtons: (BOOL)state
84 [doneButton setHidden:!state];
85 [execButton setHidden:!state];
88 - (void) textViewDidBeginEditing: (UITextView *)theView
90 //[self showButtons:YES];
93 - (IBAction) triggerDone: (id)sender
95 [textView resignFirstResponder];
96 [self showButtons:NO];
99 - (IBAction) triggerExecute: (id)sender
101 NSRange range = [textView selectedRange];
102 NSString *text = [textView text];
103 NSUInteger start, end;
104 [text getLineStart:&start end:&end contentsEnd:nil forRange:range];
105 NSString *line = [text substringWithRange:NSMakeRange(start, end-start)];
107 if (target && [target respondsToSelector:selector]) [target performSelector:selector withObject:line];
109 [textView resignFirstResponder];
110 [self showButtons:NO];
113 - (IBAction) triggerExecuteFile: (id)sender
115 if (target && [target respondsToSelector:selector]) [target performSelector:selector withObject:[textView text]];