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 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 ) );
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" );
155 contents.Add( mEditor );
158 void CreateButtonContainer()
160 mButtonContainer = Toolkit::TableView::New( NUMBER_OF_COLORS, 1u );
161 mButtonContainer.SetParentOrigin( ParentOrigin::TOP_LEFT );
162 mButtonContainer.SetAnchorPoint( AnchorPoint::TOP_LEFT );
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 );
169 mButtonContainer.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
171 const Vector3 containerPercentage( 0.8f * TOOLBAR_BUTTON_PERCENTAGE, NUMBER_OF_COLORS, 1.f );
172 mButtonContainer.SetSizeModeFactor( containerPercentage );
174 Layer toolbarLayer = mToolBar.GetLayer();
175 toolbarLayer.Add( mButtonContainer );
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 )
180 Toolkit::PushButton button = Toolkit::PushButton::New();
181 button.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
182 button.SetSizeModeFactor( buttonPercentage );
184 std::ostringstream s;
185 s << "color" << index;
186 button.SetName( s.str() );
188 button.SetProperty( Button::Property::UNSELECTED_COLOR, COLORS[index] );
189 button.SetProperty( Button::Property::SELECTED_COLOR, COLORS[index] );
191 button.ClickedSignal().Connect( this, &TextEditorExample::OnColorButtonClicked );
193 mButtonContainer.Add( button );
197 void OnKeyEvent( const KeyEvent& event )
199 if( event.state == KeyEvent::Down )
201 if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
203 // Exit application when click back or escape.
209 bool OnChangeColorButtonClicked( Button button )
211 if( !mButtonContainer )
213 CreateButtonContainer();
216 mButtonContainer.SetVisible( true );
217 mButtonContainer.SetSensitive( true );
221 bool OnColorButtonClicked( Button button )
223 const std::string& name = button.GetName();
225 if( "color" == name.substr( 0u, 5u ) )
227 const unsigned int index = strtoul( name.substr( 5u, 1u ).c_str(), NULL, 10u );
228 mEditor.SetProperty( TextEditor::Property::INPUT_COLOR, COLORS[index] );
231 mButtonContainer.SetVisible( false );
232 mButtonContainer.SetSensitive( false );
239 Application& mApplication;
241 Toolkit::Control mView;
242 Toolkit::ToolBar mToolBar;
243 Toolkit::TextEditor mEditor;
244 Toolkit::PushButton mColorButtonOption;
245 Toolkit::TableView mButtonContainer;
248 void RunTest( Application& application )
250 TextEditorExample test( application );
252 application.MainLoop();
255 /** Entry point for Linux & Tizen applications */
256 int main( int argc, char **argv )
258 // DALI_DEMO_THEME_PATH not passed to Application so TextEditor example uses default Toolkit style sheet.
259 Application application = Application::New( &argc, &argv );
261 RunTest( application );