2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file simple-text-editor-example.cpp
20 * @brief Very basic usage of TextEditor control
24 #include <dali-toolkit/dali-toolkit.h>
29 #include "shared/view.h"
32 using namespace Dali::Toolkit;
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.
47 const Vector4 COLORS[] = { Color::RED,
55 const unsigned int NUMBER_OF_COLORS = sizeof( COLORS ) / sizeof( Vector4 );
57 } // Unnamed namespace
60 * @brief The main class of the demo.
62 class TextEditorExample : public ConnectionTracker
66 TextEditorExample( Application& application )
67 : mApplication( application )
69 // Connect to the Application's Init signal
70 mApplication.InitSignal().Connect( this, &TextEditorExample::Create );
75 // Nothing to do here.
79 * One-time setup in response to Application InitSignal.
81 void Create( Application& application )
83 Stage stage = Stage::GetCurrent();
85 // Respond to key events
86 stage.KeyEventSignal().Connect(this, &TextEditorExample::OnKeyEvent);
88 // Set a background color.
89 stage.SetBackgroundColor( BACKGROUND_COLOR );
92 const Vector2 stageSize = stage.GetSize();
94 // Creates a default view with a default tool bar.
95 // The view is added to the stage.
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,
104 Layer contents = DemoHelper::CreateView( mApplication,
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 );
117 mColorButtonOption.SetProperty( Button::Property::UNSELECTED_COLOR, Color::BLACK );
118 mColorButtonOption.SetProperty( Button::Property::SELECTED_COLOR, Color::BLACK );
120 mColorButtonOption.ClickedSignal().Connect( this, &TextEditorExample::OnChangeColorButtonClicked );
122 mToolBar.AddControl( mColorButtonOption, viewStyle.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalLeft, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
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 );
132 mEditor.SetBackgroundColor( TEXT_EDITOR_BACKGROUND_COLOR );
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" );
148 contents.Add( mEditor );
151 void CreateButtonContainer()
153 mButtonContainer = Toolkit::TableView::New( NUMBER_OF_COLORS, 1u );
154 mButtonContainer.SetParentOrigin( ParentOrigin::TOP_LEFT );
155 mButtonContainer.SetAnchorPoint( AnchorPoint::TOP_LEFT );
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 );
162 mButtonContainer.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
164 const Vector3 containerPercentage( 0.8f * TOOLBAR_BUTTON_PERCENTAGE, NUMBER_OF_COLORS, 1.f );
165 mButtonContainer.SetSizeModeFactor( containerPercentage );
167 Layer toolbarLayer = mToolBar.GetLayer();
168 toolbarLayer.Add( mButtonContainer );
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 )
173 Toolkit::PushButton button = Toolkit::PushButton::New();
174 button.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
175 button.SetSizeModeFactor( buttonPercentage );
177 std::ostringstream s;
178 s << "color" << index;
179 button.SetName( s.str() );
181 button.SetProperty( Button::Property::UNSELECTED_COLOR, COLORS[index] );
182 button.SetProperty( Button::Property::SELECTED_COLOR, COLORS[index] );
184 button.ClickedSignal().Connect( this, &TextEditorExample::OnColorButtonClicked );
186 mButtonContainer.Add( button );
190 void OnKeyEvent( const KeyEvent& event )
192 if( event.state == KeyEvent::Down )
194 if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
196 // Exit application when click back or escape.
202 bool OnChangeColorButtonClicked( Button button )
204 if( !mButtonContainer )
206 CreateButtonContainer();
209 mButtonContainer.SetVisible( true );
210 mButtonContainer.SetSensitive( true );
214 bool OnColorButtonClicked( Button button )
216 const std::string& name = button.GetName();
218 if( "color" == name.substr( 0u, 5u ) )
220 const unsigned int index = strtoul( name.substr( 5u, 1u ).c_str(), NULL, 10u );
221 mEditor.SetProperty( TextEditor::Property::INPUT_COLOR, COLORS[index] );
224 mButtonContainer.SetVisible( false );
225 mButtonContainer.SetSensitive( false );
232 Application& mApplication;
234 Toolkit::Control mView;
235 Toolkit::ToolBar mToolBar;
236 Toolkit::TextEditor mEditor;
237 Toolkit::PushButton mColorButtonOption;
238 Toolkit::TableView mButtonContainer;
241 void RunTest( Application& application )
243 TextEditorExample test( application );
245 application.MainLoop();
248 /** Entry point for Linux & Tizen applications */
249 int main( int argc, char **argv )
251 // DALI_DEMO_THEME_PATH not passed to Application so TextEditor example uses default Toolkit style sheet.
252 Application application = Application::New( &argc, &argv );
254 RunTest( application );