Enable creating FormsApplication based IMEs
[platform/core/csapi/tizenfx.git] / src / Tizen.Uix.InputMethod / Tizen.Uix.InputMethod / IMEApplication.cs
1 using System;
2 using Xamarin.Forms;
3 using Xamarin.Forms.Platform.Tizen;
4 using Tizen;
5 using ElmSharp;
6 using static Interop.InputMethod;
7
8 namespace Tizen.Uix.InputMethod
9 {
10     public class IMEWindow : Xamarin.Forms.Platform.Tizen.Native.Window
11     {
12         /* There can be only one single IME Window, so we are declaring the size variables as static */
13         static Xamarin.Forms.Size _portrait_size = new Xamarin.Forms.Size(0.0 ,0.0);
14         static Xamarin.Forms.Size _landscape_size = new Xamarin.Forms.Size(0.0, 0.0);
15
16         public static Xamarin.Forms.Size PortraitSize
17         {
18             get { return _portrait_size; }
19             internal set
20             {
21                 _portrait_size = value;
22                 ImeSetSize((int)PortraitSize.Width, (int)PortraitSize.Height,
23                            (int)LandscapeSize.Width, (int)LandscapeSize.Height);
24             }
25         }
26
27         public static Xamarin.Forms.Size LandscapeSize
28         {
29             get { return _landscape_size; }
30             internal set
31             {
32                 _landscape_size = value;
33                 ImeSetSize((int)PortraitSize.Width, (int)PortraitSize.Height,
34                            (int)LandscapeSize.Width, (int)LandscapeSize.Height);
35             }
36         }
37
38         public IMEWindow() : base()
39         {
40             if (_portrait_size.Width == 0 || _portrait_size.Height == 0)
41             {
42                 Log.Warn(LogTag, "The width and/or height of portrait IME size contains value 0");
43             }
44             if (_landscape_size.Width == 0 || _landscape_size.Height == 0)
45             {
46                 Log.Warn(LogTag, "The width and/or height of landscape IME size contains value 0");
47             }
48
49             ImeSetSize((int)PortraitSize.Width, (int)PortraitSize.Height,
50                        (int)LandscapeSize.Width, (int)LandscapeSize.Height);
51         }
52
53         protected override IntPtr CreateHandle(EvasObject parent)
54         {
55             /* We are acquiring IME Window's pointer which is expected to be created
56              * when calling Create() function of the InputMethodEditor class */
57             IntPtr handle = ImeGetMainWindow();
58             Log.Info(LogTag, "ImeGetMainWindow returned : " + handle.ToString());
59
60             return handle;
61         }
62     }
63
64     public class IMEApplication : Xamarin.Forms.Platform.Tizen.FormsApplication
65     {
66         protected IMEApplication()
67         {
68         }
69
70         public Xamarin.Forms.Size PortraitSize
71         {
72             get { return IMEWindow.PortraitSize; }
73             set { IMEWindow.PortraitSize = value; }
74         }
75
76         public Xamarin.Forms.Size LandscapeSize
77         {
78             get { return IMEWindow.LandscapeSize; }
79             set { IMEWindow.LandscapeSize = value; }
80         }
81
82         protected override void OnPreCreate()
83         {
84             try
85             {
86                 Application.ClearCurrent();
87
88                 /* Since the IMEWindow class acquires window handle from InputMethod module
89                  * which is created internally when calling InputMethodEditor.Create() function,
90                  * this needs to be called BEFORE creating new IMEWindow instance. */
91                 InputMethod.InputMethodEditor.Create();
92
93                 MainWindow = new IMEWindow();
94                 MainWindow.IndicatorMode = IndicatorMode.Hide;
95                 MainWindow.Show();
96             }
97             catch (Exception e)
98             {
99                 Log.Error("EXCEPTION", "Exception caught : " + e.ToString());
100             }
101         }
102
103         protected override void OnTerminate()
104         {
105             InputMethod.InputMethodEditor.Destroy();
106             base.OnTerminate();
107         }
108
109         protected override void OnPause()
110         {
111             base.OnPause();
112         }
113
114         protected override void OnResume()
115         {
116             base.OnResume();
117         }
118     }
119 }