Fixes for text related actors after new size negotiation.
[platform/core/uifw/dali-demo.git] / examples / text-field / text-field-example.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 /**
19  * @file text-field-example.cpp
20  * @brief Basic usage of TextField control
21  */
22
23 // EXTERNAL INCLUDES
24 #include <dali-toolkit/dali-toolkit.h>
25 #include <dali/public-api/text-abstraction/text-abstraction.h>
26
27 using namespace Dali;
28 using namespace Dali::Toolkit;
29
30 namespace
31 {
32
33 const char* const BACKGROUND_IMAGE = DALI_IMAGE_DIR "button-up.9.png";
34
35 const float BORDER_WIDTH = 4.0f;
36
37 } // unnamed namespace
38
39 /**
40  * @brief The main class of the demo.
41  */
42 class TextFieldExample : public ConnectionTracker
43 {
44 public:
45
46   TextFieldExample( Application& application )
47   : mApplication( application )
48   {
49     // Connect to the Application's Init signal
50     mApplication.InitSignal().Connect( this, &TextFieldExample::Create );
51   }
52
53   ~TextFieldExample()
54   {
55     // Nothing to do here.
56   }
57
58   /**
59    * One-time setup in response to Application InitSignal.
60    */
61   void Create( Application& application )
62   {
63     Stage stage = Stage::GetCurrent();
64
65     stage.KeyEventSignal().Connect(this, &TextFieldExample::OnKeyEvent);
66
67     Vector2 stageSize = stage.GetSize();
68
69     mContainer = Control::New();
70     mContainer.SetName( "Container" );
71     mContainer.SetParentOrigin( ParentOrigin::CENTER );
72     mContainer.SetResizePolicy( FIXED, ALL_DIMENSIONS );
73     mContainer.SetPreferredSize( Vector2(stageSize.width*0.6f, stageSize.width*0.6f) );
74     mContainer.SetBackgroundImage( ResourceImage::New( BACKGROUND_IMAGE ) );
75     mContainer.GetChildAt(0).SetZ(-1.0f);
76     stage.Add( mContainer );
77
78     TextField field = TextField::New();
79     field.SetAnchorPoint( AnchorPoint::TOP_LEFT );
80     field.SetDimensionDependency( HEIGHT, WIDTH );
81     field.SetResizePolicy( FILL_TO_PARENT, WIDTH );
82
83     mContainer.Add( field );
84
85     field.SetProperty( TextField::Property::TEXT, "Hello" );
86
87     Property::Value fieldText = field.GetProperty( TextField::Property::TEXT );
88     std::cout << "Displaying text: " << fieldText.Get< std::string >() << std::endl;
89   }
90
91   /**
92    * Main key event handler
93    */
94   void OnKeyEvent(const KeyEvent& event)
95   {
96     if(event.state == KeyEvent::Down)
97     {
98       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
99       {
100         mApplication.Quit();
101       }
102     }
103   }
104
105 private:
106
107   Application& mApplication;
108
109   Control mContainer;
110 };
111
112 void RunTest( Application& application )
113 {
114   TextFieldExample test( application );
115
116   application.MainLoop();
117 }
118
119 /** Entry point for Linux & Tizen applications */
120 int main( int argc, char **argv )
121 {
122   Application application = Application::New( &argc, &argv );
123
124   RunTest( application );
125
126   return 0;
127 }