#import "editor.h"
#import <UIKit/UIKeyboard.h>

@implementation EditorApplication

- (void) applicationDidFinishLaunching: (id) unused
{
    struct CGRect screenRect = [UIHardware fullScreenApplicationContentRect];
    screenRect.origin = CGPointZero;

    /* ウィンドウ生成 */
    UIWindow *window;
    window = [[UIWindow alloc] initWithContentRect: screenRect];
    [window orderFront: self];
    [window makeKey: self];
    [window _setHidden: NO];

    /* 表示領域の設定 */
    struct CGRect mainRect = [UIHardware fullScreenApplicationContentRect];
    mainView = [[UIView alloc] initWithFrame: screenRect];

    /* キーボードの大きさ */
    struct CGSize keyboardSize = [UIKeyboard defaultSize];

    /* テキスト入力欄 */
    textView = [[UITextView alloc]
        initWithFrame: CGRectMake(0, 0, screenRect.size.width,
        screenRect.size.height-keyboardSize.height)];
    [textView setEditable: YES];
    [textView setTextSize: 14];

    /* ソフトウェアキーボード */
    UIKeyboard *keyboard = [[UIKeyboard alloc]
        initWithFrame: CGRectMake(0, screenRect.size.height-keyboardSize.height,
        screenRect.size.width, keyboardSize.height)];

    /* ウィンドウに表示領域を設定 */
    [window setContentView: mainView];

    /* 表示領域にテキスト入力欄を設定 */
    [mainView addSubview: textView];

    /* 表示領域にキーボードを設定 */
    [mainView addSubview: keyboard];

    /* ファイル名 */
    filename = @"/var/root/memo.txt";

    /* ファイルからメモを読みtextViewにセット */
    [textView setText:
        [NSMutableString
            stringWithContentsOfFile: filename
            encoding: NSUTF8StringEncoding
            error: NULL]];
}

/* アプリ終了時に呼ばれる */
- (void) applicationWillSuspend
{
   [[textView text]
        writeToFile: filename
        atomically: NO
        encoding: NSUTF8StringEncoding
        error: NULL];
}
@end