Use the input style changed signal to update the gui.
[platform/core/uifw/dali-demo.git] / examples / text-editor / text-editor-example.cpp
1 /*
2  * Copyright (c) 2016 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 simple-text-editor-example.cpp
20  * @brief Very basic usage of TextEditor control
21  */
22
23 // EXTERNAL INCLUDES
24 #include <dali-toolkit/dali-toolkit.h>
25 #include <iostream>
26 #include <sstream>
27
28 // INTERNAL INCLUDES
29 #include "shared/view.h"
30
31 using namespace Dali;
32 using namespace Dali::Toolkit;
33
34 namespace
35 {
36
37 const Vector4 BACKGROUND_COLOR( 0.04f, 0.345f, 0.392f, 1.0f );      ///< The background color.
38 const char*   TOOLBAR_IMAGE = DEMO_IMAGE_DIR "top-bar.png";         ///< The tool-bar image.
39 const float   TOOLBAR_BUTTON_PERCENTAGE = 0.1f;                     ///< The button's space width as a percentage of the toolbar's width.
40 const float   TOOLBAR_TITLE_PERCENTAGE = 0.7f;                      ///< The title's width as a percentage of the toolbar's width.
41 const float   TOOLBAR_HEIGHT_PERCENTAGE = 0.05f;                    ///< The toolbar's height as a percentage of the stage's height.
42 const float   TOOLBAR_PADDING = 4.f;                                ///< The padding in pixels.
43 const Vector3 BUTTON_PERCENTAGE( 0.8f, 0.8f, 1.f );                 ///< The button's width as a percentage of the space for the buttons in the toolbar.
44 const Vector3 TEXT_EDITOR_RELATIVE_SIZE( 1.f, 0.45f, 1.0f );        ///< The size of the text editor as a percentage of the stage's size.
45 const Vector4 TEXT_EDITOR_BACKGROUND_COLOR( 1.f, 1.f, 1.f, 0.15f ); ///< The background color of the text editor.
46
47 const Vector4 COLORS[] = { Color::RED,
48                            Color::GREEN,
49                            Color::BLUE,
50                            Color::YELLOW,
51                            Color::CYAN,
52                            Color::MAGENTA,
53                            Color::WHITE,
54                            Color::BLACK };
55 const unsigned int NUMBER_OF_COLORS = sizeof( COLORS ) / sizeof( Vector4 );
56
57 } // Unnamed namespace
58
59 /**
60  * @brief The main class of the demo.
61  */
62 class TextEditorExample : public ConnectionTracker
63 {
64 public:
65   TextEditorExample( Application& application )
66   : mApplication( application )
67   {
68     // Connect to the Application's Init signal
69     mApplication.InitSignal().Connect( this, &TextEditorExample::Create );
70   }
71
72   ~TextEditorExample()
73   {
74     // Nothing to do here.
75   }
76
77   /**
78    * One-time setup in response to Application InitSignal.
79    */
80   void Create( Application& application )
81   {
82     Stage stage = Stage::GetCurrent();
83
84     // Respond to key events
85     stage.KeyEventSignal().Connect(this, &TextEditorExample::OnKeyEvent);
86
87     // Set a background color.
88     stage.SetBackgroundColor( BACKGROUND_COLOR );
89
90     // The stage size.
91     const Vector2 stageSize = stage.GetSize();
92
93     // Creates a default view with a default tool bar.
94     // The view is added to the stage.
95
96     // Set the toolbar style
97     const float toolBarHeight = TOOLBAR_HEIGHT_PERCENTAGE * stageSize.height;
98     const DemoHelper::ViewStyle viewStyle( TOOLBAR_BUTTON_PERCENTAGE,
99                                            TOOLBAR_TITLE_PERCENTAGE,
100                                            toolBarHeight,
101                                            TOOLBAR_PADDING );
102
103     Layer contents = DemoHelper::CreateView( mApplication,
104                                              mView,
105                                              mToolBar,
106                                              "",
107                                              TOOLBAR_IMAGE,
108                                              "",
109                                              viewStyle );
110
111     // Create a 'select color' button.
112     mColorButtonOption = Toolkit::PushButton::New();
113     mColorButtonOption.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
114     mColorButtonOption.SetSizeModeFactor( BUTTON_PERCENTAGE );
115
116     mColorButtonOption.SetProperty( Button::Property::UNSELECTED_COLOR, Color::BLACK );
117     mColorButtonOption.SetProperty( Button::Property::SELECTED_COLOR, Color::BLACK );
118
119     mColorButtonOption.ClickedSignal().Connect( this, &TextEditorExample::OnChangeColorButtonClicked );
120
121     mToolBar.AddControl( mColorButtonOption, viewStyle.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalLeft, DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
122
123     // Create the text editor.
124     mEditor = TextEditor::New();
125     mEditor.SetParentOrigin( ParentOrigin::TOP_CENTER );
126     mEditor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
127     mEditor.SetPosition( 0.f, toolBarHeight, 0.f );
128     mEditor.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
129     mEditor.SetSizeModeFactor( TEXT_EDITOR_RELATIVE_SIZE );
130
131     mEditor.SetBackgroundColor( TEXT_EDITOR_BACKGROUND_COLOR );
132
133     const Size boundingBoxSize( stageSize * TEXT_EDITOR_RELATIVE_SIZE.GetVectorXY() );
134     Rect<int> boundingBox( 0,
135                            static_cast<int>( toolBarHeight ),
136                            static_cast<int>( boundingBoxSize.width ),
137                            static_cast<int>( boundingBoxSize.height - toolBarHeight ) );
138
139     mEditor.SetProperty( TextEditor::Property::DECORATION_BOUNDING_BOX, boundingBox );
140     mEditor.SetProperty( TextEditor::Property::TEXT_COLOR, Color::BLACK );
141     mEditor.SetProperty( TextEditor::Property::TEXT,
142                          "Lorem ipsum dolor sit amet, aeque definiebas ea mei, posse iracundia ne cum.\n"
143                          "Usu ne nisl maiorum iudicabit, veniam epicurei oporteat eos an.\n"
144                          "Ne nec nulla regione albucius, mea doctus delenit ad!\n"
145                          "Et everti blandit adversarium mei, eam porro neglegentur suscipiantur an.\n"
146                          "Quidam corpora at duo. An eos possim scripserit?\n\n"
147                          "Aťqui dicant sěnťenťíae aň vel!\n"
148                          "Vis viris médiocrem elaboraret ét, verear civibus moderatius ex duo!\n"
149                          "Án veri laborě iňtěgré quó, mei aď poššit lobortis, mei prompťa čonsťitůťó eů.\n"
150                          "Aliquip sanctůs delicáta quí ěá, et natum aliquam est?\n"
151                          "Asšúm sapěret usu ůť.\n"
152                          "Síť ut apeirián laboramúš percipitur, sůas hařum ín éos?\n" );
153
154     mEditor.InputStyleChangedSignal().Connect( this, &TextEditorExample::OnTextInputStyleChanged );
155
156     contents.Add( mEditor );
157   }
158
159   void CreateButtonContainer()
160   {
161     mButtonContainer = Toolkit::TableView::New( NUMBER_OF_COLORS, 1u );
162     mButtonContainer.SetParentOrigin( ParentOrigin::TOP_LEFT );
163     mButtonContainer.SetAnchorPoint( AnchorPoint::TOP_LEFT );
164
165     Stage stage = Stage::GetCurrent();
166     const Vector2 stageSize = stage.GetSize();
167     const float toolBarHeight = TOOLBAR_HEIGHT_PERCENTAGE * stageSize.height;
168     mButtonContainer.SetPosition( TOOLBAR_PADDING, 2.f * TOOLBAR_PADDING + toolBarHeight, 0.f );
169
170     mButtonContainer.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
171
172     const Vector3 containerPercentage( 0.8f * TOOLBAR_BUTTON_PERCENTAGE, NUMBER_OF_COLORS, 1.f );
173     mButtonContainer.SetSizeModeFactor( containerPercentage );
174
175     Layer toolbarLayer = mToolBar.GetLayer();
176     toolbarLayer.Add( mButtonContainer );
177
178     const Vector3 buttonPercentage( 1.f, 0.8f / static_cast<float>( NUMBER_OF_COLORS ), 1.f );
179     for( unsigned int index = 0u; index < NUMBER_OF_COLORS; ++index )
180     {
181       Toolkit::PushButton button = Toolkit::PushButton::New();
182       button.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
183       button.SetSizeModeFactor( buttonPercentage );
184
185       std::ostringstream s;
186       s << "color" << index;
187       button.SetName( s.str() );
188
189       button.SetProperty( Button::Property::UNSELECTED_COLOR, COLORS[index] );
190       button.SetProperty( Button::Property::SELECTED_COLOR, COLORS[index] );
191
192       button.ClickedSignal().Connect( this, &TextEditorExample::OnColorButtonClicked );
193
194       mButtonContainer.Add( button );
195     }
196   }
197
198   void OnKeyEvent( const KeyEvent& event )
199   {
200     if( event.state == KeyEvent::Down )
201     {
202       if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
203       {
204         // Exit application when click back or escape.
205         mApplication.Quit();
206       }
207     }
208   }
209
210   bool OnChangeColorButtonClicked( Button button )
211   {
212     if( !mButtonContainer )
213     {
214       CreateButtonContainer();
215     }
216
217     mButtonContainer.SetVisible( true );
218     mButtonContainer.SetSensitive( true );
219     return true;
220   }
221
222   bool OnColorButtonClicked( Button button )
223   {
224     const std::string& name = button.GetName();
225
226     Vector4 color;
227     if( "color" == name.substr( 0u, 5u ) )
228     {
229       const unsigned int index = strtoul( name.substr( 5u, 1u ).c_str(), NULL, 10u );
230       color = COLORS[index];
231       mEditor.SetProperty( TextEditor::Property::INPUT_COLOR, color );
232     }
233
234     mColorButtonOption.SetProperty( Button::Property::UNSELECTED_COLOR, color );
235     mColorButtonOption.SetProperty( Button::Property::SELECTED_COLOR, color );
236
237     mButtonContainer.SetVisible( false );
238     mButtonContainer.SetSensitive( false );
239
240     return true;
241   }
242
243   void OnTextInputStyleChanged( TextEditor editor, TextEditor::InputStyle::Mask mask )
244   {
245     if( TextEditor::InputStyle::NONE != static_cast<TextEditor::InputStyle::Mask>( mask & TextEditor::InputStyle::COLOR ) )
246     {
247       const Vector4 color = editor.GetProperty( TextEditor::Property::INPUT_COLOR ).Get<Vector4>();
248
249       mColorButtonOption.SetProperty( Button::Property::UNSELECTED_COLOR, color );
250       mColorButtonOption.SetProperty( Button::Property::SELECTED_COLOR, color );
251     }
252
253     editor.Reset();
254   }
255
256 private:
257
258   Application& mApplication;
259
260   Toolkit::Control    mView;
261   Toolkit::ToolBar    mToolBar;
262   Toolkit::TextEditor mEditor;
263   Toolkit::PushButton mColorButtonOption;
264   Toolkit::TableView  mButtonContainer;
265 };
266
267 void RunTest( Application& application )
268 {
269   TextEditorExample test( application );
270
271   application.MainLoop();
272 }
273
274 /** Entry point for Linux & Tizen applications */
275 int main( int argc, char **argv )
276 {
277   // DALI_DEMO_THEME_PATH not passed to Application so TextEditor example uses default Toolkit style sheet.
278   Application application = Application::New( &argc, &argv );
279
280   RunTest( application );
281
282   return 0;
283 }