2 * Copyright (c) 2017 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.
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali-toolkit/devel-api/controls/control-devel.h>
20 #include <dali-toolkit/devel-api/controls/tooltip/tooltip-properties.h>
23 using namespace Dali::Toolkit;
27 const Vector4 STAGE_COLOR( 211.0f / 255.0f, 211.0f / 255.0f, 211.0f / 255.0f, 1.0f ); ///< The color of the stage
28 const char * const THEME_PATH( DEMO_STYLE_DIR "tooltip-example-theme.json" ); ///< The theme used for this example
29 const float POSITION_INCREMENTER( 0.2f ); ///< The position difference between the controls along the Y-Axis.
30 } // unnamed namespace
33 * @brief Creates a controller which demonstrates the tooltip functionality in control.
35 * The Control base class supports tooltip functionality. However, the Toolkit Tooltip style is only set on Buttons by default.
36 * This example portrays the different ways in which a tooltip can be displayed and customised.
38 class TooltipController : public ConnectionTracker
42 TooltipController( Application& application )
43 : mApplication( application ),
44 previousPosition( 0.0f )
46 // Connect to the Application's Init signal
47 mApplication.InitSignal().Connect( this, &TooltipController::Create );
52 // The Init signal is received once (only) during the Application lifetime
53 void Create( Application& application )
55 // Hide the indicator bar
56 application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
58 // Set the stage background color and connect to the stage's key signal to allow Back and Escape to exit.
59 Stage stage = Stage::GetCurrent();
60 stage.SetBackgroundColor( STAGE_COLOR );
61 stage.KeyEventSignal().Connect( this, &TooltipController::OnKeyEvent );
62 const Vector2 stageSize = stage.GetSize();
64 // Add a text label at the top for information purposes
65 Control label = TextLabel::New( "Hover over buttons to see tooltip" );
66 label.SetParentOrigin( ParentOrigin::TOP_CENTER );
67 label.SetAnchorPoint( AnchorPoint::TOP_CENTER );
68 label.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, "Center" );
71 // Simple tooltip from stylesheet
72 Control simple = PushButton::New();
73 simple.SetStyleName( "TooltipTextOnly" );
74 SetLabel( simple, "Simple" );
75 Layout( simple, stageSize );
78 // Tooltip with icon and text, from stylesheet
79 Control iconWithText = PushButton::New();
80 iconWithText.SetStyleName( "TooltipArray" );
81 SetLabel( iconWithText, "Icon with Text" );
82 Layout( iconWithText, stageSize );
83 stage.Add( iconWithText );
85 // Tooltip with custom style, from stylesheet
86 Control customFromStylesheet = PushButton::New();
87 customFromStylesheet.SetStyleName( "TooltipCustom" );
88 SetLabel( customFromStylesheet, "Custom From Stylesheet" );
89 Layout( customFromStylesheet, stageSize );
90 stage.Add( customFromStylesheet );
92 // Tooltip with custom style, from code
93 Control customFromCode = PushButton::New();
94 SetLabel( customFromCode, "Custom From Code" );
95 Layout( customFromCode, stageSize );
96 customFromCode.SetProperty( DevelControl::Property::TOOLTIP,
97 Property::Map().Add( Tooltip::Property::CONTENT,
98 Property::Array().Add( Property::Map().Add( Toolkit::Visual::Property::TYPE, Visual::IMAGE )
99 .Add( ImageVisual::Property::URL, DEMO_IMAGE_DIR "Logo-for-demo.png" ) )
100 .Add( Property::Map().Add( Toolkit::Visual::Property::TYPE, Visual::TEXT )
101 .Add( TextVisual::Property::TEXT_COLOR, Color::WHITE )
102 .Add( TextVisual::Property::TEXT, "Custom coded style\nat hover point" )
103 .Add( TextVisual::Property::MULTI_LINE, true )
104 .Add( TextVisual::Property::HORIZONTAL_ALIGNMENT, "CENTER" )
105 .Add( TextVisual::Property::POINT_SIZE, 16 ) ) )
106 .Add( Tooltip::Property::LAYOUT, Vector2( 2, 1 ) )
107 .Add( Tooltip::Property::POSITION, Tooltip::Position::HOVER_POINT )
108 .Add( Tooltip::Property::BACKGROUND,
109 Property::Map().Add( Tooltip::Background::Property::VISUAL, DEMO_IMAGE_DIR "tooltip.9.png" )
110 .Add( Tooltip::Background::Property::BORDER, Rect< int >( 1, 5, 5, 1 ) ) )
112 stage.Add( customFromCode );
116 * @brief Called when any key event is received
118 * Will use this to quit the application if Back or the Escape key is received
119 * @param[in] event The key event information
121 void OnKeyEvent( const KeyEvent& event )
123 if( event.state == KeyEvent::Down )
125 if ( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
133 * @brief Sets the label on the control.
134 * @param[in] label The label to set.
136 void SetLabel( Control control, std::string label )
140 control.SetProperty( Button::Property::LABEL,
141 Property::Map().Add( Toolkit::Visual::Property::TYPE, Visual::TEXT )
142 .Add( TextVisual::Property::TEXT, label ) );
147 * @brief Lays out the control in the appropriate location.
148 * @param[in] control The control to layout.
149 * @param[in] stageSize The size of the stage, passing it in so we don't have to retrieve it every time.
151 void Layout( Control control, const Vector2& stageSize )
155 previousPosition += POSITION_INCREMENTER;
156 control.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
157 control.SetSizeModeFactor( Vector3( 0.75, 0.1, 1.0 ) );
158 control.SetAnchorPoint( AnchorPoint::CENTER );
159 control.SetParentOrigin( ParentOrigin::TOP_CENTER );
160 control.SetY( stageSize.height * previousPosition );
165 Application& mApplication;
166 float previousPosition;
169 int DALI_EXPORT_API main( int argc, char **argv )
171 Application application = Application::New( &argc, &argv, THEME_PATH );
173 TooltipController test( application );
175 application.MainLoop();