[release] version bump to 13.0 beta1
[vuplus_xbmc] / xbmc / osx / ios / IOSKeyboard.mm
1 /*
2  *      Copyright (C) 2012-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "XBMCController.h"
22 #include "IOSKeyboard.h"
23 #include "IOSKeyboardView.h"
24 #include "XBMCDebugHelpers.h"
25
26 #import "AutoPool.h"
27
28 KeyboardView *g_pIosKeyboard = nil;
29
30 bool CIOSKeyboard::ShowAndGetInput(char_callback_t pCallback, const std::string &initialString, std::string &typedString, const std::string &heading, bool bHiddenInput)
31 {
32   // we are in xbmc main thread or python module thread.
33
34   CCocoaAutoPool pool;
35   
36   @synchronized([KeyboardView class])
37   {
38     // in case twice open keyboard.
39     if (g_pIosKeyboard)
40       return false;
41     
42     // assume we are only drawn on the mainscreen ever!
43     UIScreen *pCurrentScreen = [UIScreen mainScreen];
44     CGRect keyboardFrame = CGRectMake(0, 0, pCurrentScreen.bounds.size.height, pCurrentScreen.bounds.size.width);
45 //    LOG(@"kb: kb frame: %@", NSStringFromCGRect(keyboardFrame));
46     
47     //create the keyboardview
48     g_pIosKeyboard = [[KeyboardView alloc] initWithFrame:keyboardFrame];
49     if (!g_pIosKeyboard)
50       return false;
51   }
52
53   m_pCharCallback = pCallback;
54
55   // init keyboard stuff
56   [g_pIosKeyboard setDefault:[NSString stringWithUTF8String:initialString.c_str()]];
57   [g_pIosKeyboard setHidden:bHiddenInput];
58   [g_pIosKeyboard setHeading:[NSString stringWithUTF8String:heading.c_str()]];
59   [g_pIosKeyboard registerKeyboard:this]; // for calling back
60   bool confirmed = false;
61   if (!m_bCanceled)
62   {
63     [g_pIosKeyboard setCancelFlag:&m_bCanceled];
64     [g_pIosKeyboard activate]; // blocks and loops our application loop (like a modal dialog)
65     // user is done - get resulted text and confirmation
66     confirmed = g_pIosKeyboard.isConfirmed;
67     if (confirmed)
68       typedString = [g_pIosKeyboard.text UTF8String];
69   }
70   [g_pIosKeyboard release]; // bye bye native keyboard
71   @synchronized([KeyboardView class])
72   {
73     g_pIosKeyboard = nil;
74   }
75   return confirmed;
76 }
77
78 void CIOSKeyboard::Cancel()
79 {
80   m_bCanceled = true;
81 }
82
83 //wrap our callback between objc and c++
84 void CIOSKeyboard::fireCallback(const std::string &str)
85 {
86   if(m_pCharCallback)
87   {
88     m_pCharCallback(this, str);
89   }
90 }