Simple TextEditor demo.
[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
66   TextEditorExample( Application& application )
67   : mApplication( application )
68   {
69     // Connect to the Application's Init signal
70     mApplication.InitSignal().Connect( this, &TextEditorExample::Create );
71   }
72
73   ~TextEditorExample()
74   {
75     // Nothing to do here.
76   }
77
78   /**
79    * One-time setup in response to Application InitSignal.
80    */
81   void Create( Application& application )
82   {
83     Stage stage = Stage::GetCurrent();
84
85     // Respond to key events
86     stage.KeyEventSignal().Connect(this, &TextEditorExample::OnKeyEvent);
87
88     // Set a background color.
89     stage.SetBackgroundColor( BACKGROUND_COLOR );
90
91     // The stage size.
92     const Vector2 stageSize = stage.GetSize();
93
94     // Creates a default view with a default tool bar.
95     // The view is added to the stage.
96
97     // Set the toolbar style
98     const float toolBarHeight = TOOLBAR_HEIGHT_PERCENTAGE * stageSize.height;
99     const DemoHelper::ViewStyle viewStyle( TOOLBAR_BUTTON_PERCENTAGE,
100                                            TOOLBAR_TITLE_PERCENTAGE,
101                                            toolBarHeight,
102                                            TOOLBAR_PADDING );
103
104     Layer contents = DemoHelper::CreateView( mApplication,
105                                              mView,
106                                              mToolBar,
107                                              "",
108                                              TOOLBAR_IMAGE,
109                                              "",
110                                              viewStyle );
111
112     // Create a 'select color' button.
113     mColorButtonOption = Toolkit::PushButton::New();
114     mColorButtonOption.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
115     mColorButtonOption.SetSizeModeFactor( BUTTON_PERCENTAGE );
116
117     mColorButtonOption.SetProperty( Button::Property::UNSELECTED_COLOR, Color::BLACK );
118     mColorButtonOption.SetProperty( Button::Property::SELECTED_COLOR, Color::BLACK );
119
120     mColorButtonOption.ClickedSignal().Connect( this, &TextEditorExample::OnChangeColorButtonClicked );
121
122     mToolBar.AddControl( mColorButtonOption, viewStyle.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalLeft, DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
123
124     // Create the text editor.
125     mEditor = TextEditor::New();
126     mEditor.SetParentOrigin( ParentOrigin::TOP_CENTER );
127     mEditor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
128     mEditor.SetPosition( 0.f, toolBarHeight, 0.f );
129     mEditor.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
130     mEditor.SetSizeModeFactor( TEXT_EDITOR_RELATIVE_SIZE );
131
132     mEditor.SetBackgroundColor( TEXT_EDITOR_BACKGROUND_COLOR );
133
134     mEditor.SetProperty( TextEditor::Property::TEXT_COLOR, Color::BLACK );
135     mEditor.SetProperty( TextEditor::Property::TEXT,
136                          "Lorem ipsum dolor sit amet, aeque definiebas ea mei, posse iracundia ne cum.\n"
137                          "Usu ne nisl maiorum iudicabit, veniam epicurei oporteat eos an.\n"
138                          "Ne nec nulla regione albucius, mea doctus delenit ad!\n"
139                          "Et everti blandit adversarium mei, eam porro neglegentur suscipiantur an.\n"
140                          "Quidam corpora at duo. An eos possim scripserit?\n\n"
141                          "Aťqui dicant sěnťenťíae aň vel!\n"
142                          "Vis viris médiocrem elaboraret ét, verear civibus moderatius ex duo!\n"
143                          "Án veri laborě iňtěgré quó, mei aď poššit lobortis, mei prompťa čonsťitůťó eů.\n"
144                          "Aliquip sanctůs delicáta quí ěá, et natum aliquam est?\n"
145                          "Asšúm sapěret usu ůť.\n"
146                          "Síť ut apeirián laboramúš percipitur, sůas hařum ín éos?\n" );
147
148     contents.Add( mEditor );
149   }
150
151   void CreateButtonContainer()
152   {
153     mButtonContainer = Toolkit::TableView::New( NUMBER_OF_COLORS, 1u );
154     mButtonContainer.SetParentOrigin( ParentOrigin::TOP_LEFT );
155     mButtonContainer.SetAnchorPoint( AnchorPoint::TOP_LEFT );
156
157     Stage stage = Stage::GetCurrent();
158     const Vector2 stageSize = stage.GetSize();
159     const float toolBarHeight = TOOLBAR_HEIGHT_PERCENTAGE * stageSize.height;
160     mButtonContainer.SetPosition( TOOLBAR_PADDING, 2.f * TOOLBAR_PADDING + toolBarHeight, 0.f );
161
162     mButtonContainer.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
163
164     const Vector3 containerPercentage( 0.8f * TOOLBAR_BUTTON_PERCENTAGE, NUMBER_OF_COLORS, 1.f );
165     mButtonContainer.SetSizeModeFactor( containerPercentage );
166
167     Layer toolbarLayer = mToolBar.GetLayer();
168     toolbarLayer.Add( mButtonContainer );
169
170     const Vector3 buttonPercentage( 1.f, 0.8f / static_cast<float>( NUMBER_OF_COLORS ), 1.f );
171     for( unsigned int index = 0u; index < NUMBER_OF_COLORS; ++index )
172     {
173       Toolkit::PushButton button = Toolkit::PushButton::New();
174       button.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
175       button.SetSizeModeFactor( buttonPercentage );
176
177       std::ostringstream s;
178       s << "color" << index;
179       button.SetName( s.str() );
180
181       button.SetProperty( Button::Property::UNSELECTED_COLOR, COLORS[index] );
182       button.SetProperty( Button::Property::SELECTED_COLOR, COLORS[index] );
183
184       button.ClickedSignal().Connect( this, &TextEditorExample::OnColorButtonClicked );
185
186       mButtonContainer.Add( button );
187     }
188   }
189
190   void OnKeyEvent( const KeyEvent& event )
191   {
192     if( event.state == KeyEvent::Down )
193     {
194       if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
195       {
196         // Exit application when click back or escape.
197         mApplication.Quit();
198       }
199     }
200   }
201
202   bool OnChangeColorButtonClicked( Button button )
203   {
204     if( !mButtonContainer )
205     {
206       CreateButtonContainer();
207     }
208
209     mButtonContainer.SetVisible( true );
210     mButtonContainer.SetSensitive( true );
211     return true;
212   }
213
214   bool OnColorButtonClicked( Button button )
215   {
216     const std::string& name = button.GetName();
217
218     if( "color" == name.substr( 0u, 5u ) )
219     {
220       const unsigned int index = strtoul( name.substr( 5u, 1u ).c_str(), NULL, 10u );
221       mEditor.SetProperty( TextEditor::Property::INPUT_COLOR, COLORS[index] );
222     }
223
224     mButtonContainer.SetVisible( false );
225     mButtonContainer.SetSensitive( false );
226
227     return true;
228   }
229
230 private:
231
232   Application& mApplication;
233
234   Toolkit::Control    mView;
235   Toolkit::ToolBar    mToolBar;
236   Toolkit::TextEditor mEditor;
237   Toolkit::PushButton mColorButtonOption;
238   Toolkit::TableView  mButtonContainer;
239 };
240
241 void RunTest( Application& application )
242 {
243   TextEditorExample test( application );
244
245   application.MainLoop();
246 }
247
248 /** Entry point for Linux & Tizen applications */
249 int main( int argc, char **argv )
250 {
251   // DALI_DEMO_THEME_PATH not passed to Application so TextEditor example uses default Toolkit style sheet.
252   Application application = Application::New( &argc, &argv );
253
254   RunTest( application );
255
256   return 0;
257 }