summaryrefslogtreecommitdiff
path: root/ios/experimental/LibreOffice/LibreOffice/AppDelegate.m
blob: 2285f04f412ee9ab2232c31edb71039e7696b8ac (plain)
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
// -*- Mode: ObjC; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
//
// This file is part of the LibreOffice project.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#import <UIKit/UIKit.h>

#include <osl/detail/ios-bootstrap.h>
#include <touch/touch.h>

#import "AppDelegate.h"
#import "ViewController.h"

#import "lo.h"

static View *theView;

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    (void) application;
    (void) launchOptions;

    CGRect bounds = [[UIScreen mainScreen] bounds];

    NSLog(@"mainScreen bounds: %dx%d@(%d,%d)",
          (int) bounds.size.width, (int) bounds.size.height,
          (int) bounds.origin.x, (int) bounds.origin.y);

    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];

    NSLog(@"mainScreen applicationFrame: %dx%d@(%d,%d)",
          (int) applicationFrame.size.width, (int) applicationFrame.size.height,
          (int) applicationFrame.origin.x, (int) applicationFrame.origin.y);

    self.window = [[UIWindow alloc] initWithFrame:bounds];
    self.window.backgroundColor = [UIColor whiteColor];

    ViewController *vc = [[ViewController alloc] init];
    self.window.rootViewController = vc;

    [self.window makeKeyAndVisible];
    
    CGRect r = [self.window frame];
    r.origin = CGPointMake(0, 0);

    self.view = [[View alloc] initWithFrame: r];
    vc.view = self.view;
    theView = self.view;

    self.view->textView = [[UITextView alloc] initWithFrame: r];
    self.view->textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
    self.view->textView.alpha = 0;
    [self.view addSubview: self.view->textView];
    self.view->textView.delegate = self;

    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self.view action:@selector(tapGesture:)];

    [self.window addGestureRecognizer: tapRecognizer];

    if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
        lo_set_view_size(applicationFrame.size.height, applicationFrame.size.width);
    else
        lo_set_view_size(applicationFrame.size.width, applicationFrame.size.height);

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];

    NSThread* thread = [[NSThread alloc] initWithTarget:self
                                               selector:@selector(threadMainMethod:)
                                                 object:nil];
    [thread start];

    return YES;
}

- (void)threadMainMethod:(id)argument
{
    (void) argument;

    @autoreleasepool {
        lo_initialize();
        lo_runMain();
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSLog(@"textView: %@ shouldChangeTextInRange:[%u,%u] replacementText:%@", textView, range.location, range.length, text);
    assert(textView == theView->textView);

    for (NSUInteger i = 0; i < [text length]; i++)
        lo_keyboard_input([text characterAtIndex: i]);

    return NO;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    (void) application;
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    (void) application;
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    (void) application;
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    (void) application;
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    (void) application;
}

- (void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame
{
    (void) application;
    (void) oldStatusBarFrame;

    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
    NSLog(@"New applicationFrame: %dx%d@(%d,%d)",
          (int) applicationFrame.size.width, (int) applicationFrame.size.height,
          (int) applicationFrame.origin.x, (int) applicationFrame.origin.y);
    NSLog(@"statusBarOrientation: %d", [[UIApplication sharedApplication] statusBarOrientation]);

    if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
        lo_set_view_size(applicationFrame.size.height, applicationFrame.size.width);
    else
        lo_set_view_size(applicationFrame.size.width, applicationFrame.size.height);
}

- (void)keyboardWillShow:(NSNotification *)note
{
    NSDictionary *info = [note userInfo];
    CGRect frameBegin;
    CGRect frameEnd;

    [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&frameBegin];
    [[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&frameEnd];

    NSLog(@"keyboardWillShow: frame:%dx%d@(%d,%d)",
          (int) frameEnd.size.width, (int) frameEnd.size.height,
          (int) frameEnd.origin.x, (int) frameEnd.origin.y);
}

- (void)keyboardDidHide:(NSNotification *)note
{
    (void) note;

    NSLog(@"keyboardDidHide");

    lo_keyboard_did_hide();
}

@end

void lo_damaged(CGRect rect)
{
    (void) rect;
    dispatch_async(dispatch_get_main_queue(), ^{
            [theView setNeedsDisplayInRect:rect];
        });
    // NSLog(@"lo_damaged: %dx%d@(%d,%d)", (int)rect.size.width, (int)rect.size.height, (int)rect.origin.x, (int)rect.origin.y);
}

void lo_show_keyboard()
{
    dispatch_async(dispatch_get_main_queue(), ^{
            [theView->textView becomeFirstResponder];
        });
}

void lo_hide_keyboard()
{
    dispatch_async(dispatch_get_main_queue(), ^{
            [theView->textView resignFirstResponder];
        });
}

// vim:set shiftwidth=4 softtabstop=4 expandtab: