eb83eecf984ab8b6c0fbdc6cd1ca4e1befe19ba5
[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     const Size boundingBoxSize( stageSize * TEXT_EDITOR_RELATIVE_SIZE.GetVectorXY() );
135     Rect<int> boundingBox( 0,
136                            static_cast<int>( toolBarHeight ),
137                            static_cast<int>( boundingBoxSize.width ),
138                            static_cast<int>( boundingBoxSize.height - toolBarHeight ) );
139
140     mEditor.SetProperty( TextEditor::Property::DECORATION_BOUNDING_BOX, boundingBox );
141     mEditor.SetProperty( TextEditor::Property::TEXT_COLOR, Color::BLACK );
142     mEditor.SetProperty( TextEditor::Property::TEXT,
143                          "Lorem ipsum dolor sit amet, aeque definiebas ea mei, posse iracundia ne cum.\n"
144                          "Usu ne nisl maiorum iudicabit, veniam epicurei oporteat eos an.\n"
145                          "Ne nec nulla regione albucius, mea doctus delenit ad!\n"
146                          "Et everti blandit adversarium mei, eam porro neglegentur suscipiantur an.\n"
147                          "Quidam corpora at duo. An eos possim scripserit?\n\n"
148                          "Aťqui dicant sěnťenťíae aň vel!\n"
149                          "Vis viris médiocrem elaboraret ét, verear civibus moderatius ex duo!\n"
150                          "Án veri laborě iňtěgré quó, mei aď poššit lobortis, mei prompťa čonsťitůťó eů.\n"
151                          "Aliquip sanctůs delicáta quí ěá, et natum aliquam est?\n"
152                          "Asšúm sapěret usu ůť.\n"
153                          "Síť ut apeirián laboramúš percipitur, sůas hařum ín éos?\n" );
154
155     contents.Add( mEditor );
156   }
157
158   void CreateButtonContainer()
159   {
160     mButtonContainer = Toolkit::TableView::New( NUMBER_OF_COLORS, 1u );
161     mButtonContainer.SetParentOrigin( ParentOrigin::TOP_LEFT );
162     mButtonContainer.SetAnchorPoint( AnchorPoint::TOP_LEFT );
163
164     Stage stage = Stage::GetCurrent();
165     const Vector2 stageSize = stage.GetSize();
166     const float toolBarHeight = TOOLBAR_HEIGHT_PERCENTAGE * stageSize.height;
167     mButtonContainer.SetPosition( TOOLBAR_PADDING, 2.f * TOOLBAR_PADDING + toolBarHeight, 0.f );
168
169     mButtonContainer.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
170
171     const Vector3 containerPercentage( 0.8f * TOOLBAR_BUTTON_PERCENTAGE, NUMBER_OF_COLORS, 1.f );
172     mButtonContainer.SetSizeModeFactor( containerPercentage );
173
174     Layer toolbarLayer = mToolBar.GetLayer();
175     toolbarLayer.Add( mButtonContainer );
176
177     const Vector3 buttonPercentage( 1.f, 0.8f / static_cast<float>( NUMBER_OF_COLORS ), 1.f );
178     for( unsigned int index = 0u; index < NUMBER_OF_COLORS; ++index )
179     {
180       Toolkit::PushButton button = Toolkit::PushButton::New();
181       button.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
182       button.SetSizeModeFactor( buttonPercentage );
183
184       std::ostringstream s;
185       s << "color" << index;
186       button.SetName( s.str() );
187
188       button.SetProperty( Button::Property::UNSELECTED_COLOR, COLORS[index] );
189       button.SetProperty( Button::Property::SELECTED_COLOR, COLORS[index] );
190
191       button.ClickedSignal().Connect( this, &TextEditorExample::OnColorButtonClicked );
192
193       mButtonContainer.Add( button );
194     }
195   }
196
197   void OnKeyEvent( const KeyEvent& event )
198   {
199     if( event.state == KeyEvent::Down )
200     {
201       if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
202       {
203         // Exit application when click back or escape.
204         mApplication.Quit();
205       }
206     }
207   }
208
209   bool OnChangeColorButtonClicked( Button button )
210   {
211     if( !mButtonContainer )
212     {
213       CreateButtonContainer();
214     }
215
216     mButtonContainer.SetVisible( true );
217     mButtonContainer.SetSensitive( true );
218     return true;
219   }
220
221   bool OnColorButtonClicked( Button button )
222   {
223     const std::string& name = button.GetName();
224
225     if( "color" == name.substr( 0u, 5u ) )
226     {
227       const unsigned int index = strtoul( name.substr( 5u, 1u ).c_str(), NULL, 10u );
228       mEditor.SetProperty( TextEditor::Property::INPUT_COLOR, COLORS[index] );
229     }
230
231     mButtonContainer.SetVisible( false );
232     mButtonContainer.SetSensitive( false );
233
234     return true;
235   }
236
237 private:
238
239   Application& mApplication;
240
241   Toolkit::Control    mView;
242   Toolkit::ToolBar    mToolBar;
243   Toolkit::TextEditor mEditor;
244   Toolkit::PushButton mColorButtonOption;
245   Toolkit::TableView  mButtonContainer;
246 };
247
248 void RunTest( Application& application )
249 {
250   TextEditorExample test( application );
251
252   application.MainLoop();
253 }
254
255 /** Entry point for Linux & Tizen applications */
256 int main( int argc, char **argv )
257 {
258   // DALI_DEMO_THEME_PATH not passed to Application so TextEditor example uses default Toolkit style sheet.
259   Application application = Application::New( &argc, &argv );
260
261   RunTest( application );
262
263   return 0;
264 }