[NUI] Fix build warnings (#1084)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Button.cs
1 /*
2  * Copyright(c) 2019 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 using System;
18 using System.ComponentModel;
19 using Tizen.NUI.BaseComponents;
20
21 namespace Tizen.NUI.Components
22 {
23     /// <summary>
24     /// Button is one kind of common component, a button clearly describes what action will occur when the user selects it.
25     /// Button may contain text or an icon.
26     /// </summary>
27     /// <since_tizen> 6 </since_tizen>
28     public class Button : Control
29     {
30         private ImageView backgroundImage;
31         private ImageView shadowImage;
32         private ImageView overlayImage;
33
34         private TextLabel buttonText;
35         private ImageView buttonIcon;
36
37         private ButtonAttributes buttonAttributes;
38         private EventHandler<StateChangedEventArgs> stateChangeHander;
39
40         private bool isSelected = false;
41         private bool isEnabled = true;
42         private bool isPressed = false;
43         /// <summary>
44         /// Creates a new instance of a Button.
45         /// </summary>
46         /// <since_tizen> 6 </since_tizen>
47         public Button() : base()
48         {
49             Initialize();
50         }
51         /// <summary>
52         /// Creates a new instance of a Button with style.
53         /// </summary>
54         /// <param name="style">Create Button by special style defined in UX.</param>
55         /// <since_tizen> 6 </since_tizen>
56         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
57         [EditorBrowsable(EditorBrowsableState.Never)]
58         public Button(string style) : base(style)
59         {
60             Initialize();
61         }
62         /// <summary>
63         /// Creates a new instance of a Button with attributes.
64         /// </summary>
65         /// <param name="attributes">Create Button by attributes customized by user.</param>
66         /// <since_tizen> 6 </since_tizen>
67         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
68         [EditorBrowsable(EditorBrowsableState.Never)]
69         public Button(ButtonAttributes attributes) : base(attributes)
70         {
71             Initialize();
72         }
73         /// <summary>
74         /// An event for the button clicked signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
75         /// </summary>
76         /// <since_tizen> 6 </since_tizen>
77         public event EventHandler<ClickEventArgs> ClickEvent;
78         /// <summary>
79         /// An event for the button state changed signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
80         /// </summary>
81         /// <since_tizen> 6 </since_tizen>
82         public event EventHandler<StateChangedEventArgs> StateChangedEvent
83         {
84             add
85             {
86                 stateChangeHander += value;
87             }
88             remove
89             {
90                 stateChangeHander -= value;
91             }
92         }
93         /// <summary>
94         /// Icon orientation.
95         /// </summary>
96         /// <since_tizen> 6 </since_tizen>
97         public enum IconOrientation
98         {
99             /// <summary>
100             /// Top.
101             /// </summary>
102             /// <since_tizen> 6 </since_tizen>
103             Top,
104             /// <summary>
105             /// Bottom.
106             /// </summary>
107             /// <since_tizen> 6 </since_tizen>
108             Bottom,
109             /// <summary>
110             /// Left.
111             /// </summary>
112             /// <since_tizen> 6 </since_tizen>
113             Left,
114             /// <summary>
115             /// Right.
116             /// </summary>
117             /// <since_tizen> 6 </since_tizen>
118             Right,
119         }
120         /// <summary>
121         /// Flag to decide Button can be selected or not.
122         /// </summary>
123         /// <since_tizen> 6 </since_tizen>
124         public bool IsSelectable
125         {
126             get
127             {
128                 return buttonAttributes?.IsSelectable ?? false;
129             }
130             set
131             {
132                 buttonAttributes.IsSelectable = value;
133             }
134         }
135         /// <summary>
136         /// Background image's resource url in Button.
137         /// </summary>
138         /// <since_tizen> 6 </since_tizen>
139         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
140         [EditorBrowsable(EditorBrowsableState.Never)]
141         public string BackgroundImageURL
142         {
143             get
144             {
145                 return buttonAttributes?.BackgroundImageAttributes?.ResourceURL?.All;
146             }
147             set
148             {
149                 if (value != null)
150                 {
151                     CreateBackgroundAttributes();
152                     if (buttonAttributes.BackgroundImageAttributes.ResourceURL == null)
153                     {
154                         buttonAttributes.BackgroundImageAttributes.ResourceURL = new StringSelector();
155                     }
156                     buttonAttributes.BackgroundImageAttributes.ResourceURL.All = value;
157                     RelayoutRequest();
158                 }
159             }
160         }
161         /// <summary>
162         /// Background image's border in Button.
163         /// </summary>
164         /// <since_tizen> 6 </since_tizen>
165         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
166         [EditorBrowsable(EditorBrowsableState.Never)]
167         public Rectangle BackgroundImageBorder
168         {
169             get
170             {
171                 return buttonAttributes?.BackgroundImageAttributes?.Border?.All;
172             }
173             set
174             {
175                 if (value != null)
176                 {
177                     CreateBackgroundAttributes();
178                     if (buttonAttributes.BackgroundImageAttributes.Border == null)
179                     {
180                         buttonAttributes.BackgroundImageAttributes.Border = new RectangleSelector();
181                     }
182                     buttonAttributes.BackgroundImageAttributes.Border.All = value;
183                     RelayoutRequest();
184                 }
185             }
186         }
187         /// <summary>
188         /// Shadow image's resource url in Button.
189         /// </summary>
190         /// <since_tizen> 6 </since_tizen>
191         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
192         [EditorBrowsable(EditorBrowsableState.Never)]
193         public string ShadowImageURL
194         {
195             get
196             {
197                 return buttonAttributes?.ShadowImageAttributes?.ResourceURL?.All;
198             }
199             set
200             {
201                 if (value != null)
202                 {
203                     CreateShadowAttributes();
204                     if (buttonAttributes.ShadowImageAttributes.ResourceURL == null)
205                     {
206                         buttonAttributes.ShadowImageAttributes.ResourceURL = new StringSelector();
207                     }
208                     buttonAttributes.ShadowImageAttributes.ResourceURL.All = value;
209                     RelayoutRequest();
210                 }
211             }
212         }
213         /// <summary>
214         /// Shadow image's border in Button.
215         /// </summary>
216         /// <since_tizen> 6 </since_tizen>
217         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
218         [EditorBrowsable(EditorBrowsableState.Never)]
219         public Rectangle ShadowImageBorder
220         {
221             get
222             {
223                 return buttonAttributes?.ShadowImageAttributes?.Border?.All;
224             }
225             set
226             {
227                 if (value != null)
228                 {
229                     CreateShadowAttributes();
230                     if (buttonAttributes.ShadowImageAttributes.Border == null)
231                     {
232                         buttonAttributes.ShadowImageAttributes.Border = new RectangleSelector();
233                     }
234                     buttonAttributes.ShadowImageAttributes.Border.All = value;
235                     RelayoutRequest();
236                 }
237             }
238         }
239         /// <summary>
240         /// Overlay image's resource url in Button.
241         /// </summary>
242         /// <since_tizen> 6 </since_tizen>
243         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
244         [EditorBrowsable(EditorBrowsableState.Never)]
245         public string OverlayImageURL
246         {
247             get
248             {
249                 return buttonAttributes?.OverlayImageAttributes?.ResourceURL?.All;
250             }
251             set
252             {
253                 if (value != null)
254                 {
255                     CreateOverlayAttributes();
256                     if (buttonAttributes.OverlayImageAttributes.ResourceURL == null)
257                     {
258                         buttonAttributes.OverlayImageAttributes.ResourceURL = new StringSelector();
259                     }
260                     buttonAttributes.OverlayImageAttributes.ResourceURL.All = value;
261                     RelayoutRequest();
262                 }
263             }
264         }
265         /// <summary>
266         /// Overlay image's border in Button.
267         /// </summary>
268         /// <since_tizen> 6 </since_tizen>
269         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
270         [EditorBrowsable(EditorBrowsableState.Never)]
271         public Rectangle OverlayImageBorder
272         {
273             get
274             {
275                 return buttonAttributes?.OverlayImageAttributes?.Border?.All;
276             }
277             set
278             {
279                 if (value != null)
280                 {
281                     CreateOverlayAttributes();
282                     if (buttonAttributes.OverlayImageAttributes.Border == null)
283                     {
284                         buttonAttributes.OverlayImageAttributes.Border = new RectangleSelector();
285                     }
286                     buttonAttributes.OverlayImageAttributes.Border.All = value;
287                     RelayoutRequest();
288                 }
289             }
290         }
291         /// <summary>
292         /// Text string in Button.
293         /// </summary>
294         /// <since_tizen> 6 </since_tizen>
295         public string Text
296         {
297             get
298             {
299                 return buttonAttributes?.TextAttributes?.Text?.All;
300             }
301             set
302             {
303                 if (value != null)
304                 {
305                     CreateTextAttributes();
306                     if(buttonAttributes.TextAttributes.Text == null)
307                     {
308                         buttonAttributes.TextAttributes.Text = new StringSelector();
309                     }
310                     buttonAttributes.TextAttributes.Text.All = value;
311
312                     RelayoutRequest();
313                 }
314             }
315         }
316         /// <summary>
317         /// Translate text string in Button.
318         /// </summary>
319         /// <since_tizen> 6 </since_tizen>
320         public string TranslatableText
321         {
322             get
323             {
324                 return buttonAttributes?.TextAttributes?.TranslatableText?.All;
325             }
326             set
327             {
328                 if (value != null)
329                 {
330                     CreateTextAttributes();
331                     if (buttonAttributes.TextAttributes.TranslatableText == null)
332                     {
333                         buttonAttributes.TextAttributes.TranslatableText = new StringSelector();
334                     }
335                     buttonAttributes.TextAttributes.TranslatableText.All = value;
336
337                     RelayoutRequest();
338                 }
339             }
340         }
341         /// <summary>
342         /// Text point size in Button.
343         /// </summary>
344         /// <since_tizen> 6 </since_tizen>
345         public float PointSize
346         {
347             get
348             {
349                 return buttonAttributes?.TextAttributes?.PointSize?.All ?? 0;
350             }
351             set
352             {
353                 CreateTextAttributes();
354                 if (buttonAttributes.TextAttributes.PointSize == null)
355                 {
356                     buttonAttributes.TextAttributes.PointSize = new FloatSelector();
357                 }
358                 buttonAttributes.TextAttributes.PointSize.All = value;
359                 RelayoutRequest();
360             }
361         }
362         /// <summary>
363         /// Text font family in Button.
364         /// </summary>
365         /// <since_tizen> 6 </since_tizen>
366         public string FontFamily
367         {
368             get
369             {
370                 return buttonAttributes?.TextAttributes?.FontFamily;
371             }
372             set
373             {
374                 CreateTextAttributes();
375                 buttonAttributes.TextAttributes.FontFamily = value;
376                 RelayoutRequest();
377             }
378         }
379         /// <summary>
380         /// Text color in Button.
381         /// </summary>
382         /// <since_tizen> 6 </since_tizen>
383         public Color TextColor
384         {
385             get
386             {
387                 return buttonAttributes?.TextAttributes?.TextColor?.All;
388             }
389             set
390             {
391                 CreateTextAttributes();
392                 if (buttonAttributes.TextAttributes.TextColor == null)
393                 {
394                     buttonAttributes.TextAttributes.TextColor = new ColorSelector();
395                 }
396                 buttonAttributes.TextAttributes.TextColor.All = value;
397                 RelayoutRequest();
398             }
399         }
400         /// <summary>
401         /// Text horizontal alignment in Button.
402         /// </summary>
403         /// <since_tizen> 6 </since_tizen>
404         public HorizontalAlignment TextAlignment
405         {
406             get
407             {
408                 return buttonAttributes?.TextAttributes?.HorizontalAlignment ?? HorizontalAlignment.Center;
409             }
410             set
411             {
412                 CreateTextAttributes();
413                 buttonAttributes.TextAttributes.HorizontalAlignment = value;
414                 RelayoutRequest();
415             }
416         }
417         /// <summary>
418         /// Icon image's resource url in Button.
419         /// </summary>
420         /// <since_tizen> 6 </since_tizen>
421         public string IconURL
422         {
423             get
424             {
425                 return buttonAttributes?.IconAttributes?.ResourceURL?.All;
426             }
427             set
428             {
429                 if (value != null)
430                 {
431                     CreateIconAttributes();
432                     if (buttonAttributes.IconAttributes.ResourceURL == null)
433                     {
434                         buttonAttributes.IconAttributes.ResourceURL = new StringSelector();
435                     }
436                     buttonAttributes.IconAttributes.ResourceURL.All = value;
437                     RelayoutRequest();
438                 }
439             }
440         }
441         /// <summary>
442         /// Text string selector in Button.
443         /// </summary>
444         /// <since_tizen> 6 </since_tizen>
445         public StringSelector TextSelector
446         {
447             get
448             {
449                 return buttonAttributes?.TextAttributes?.Text;
450             }
451             set
452             {
453                 if (value != null)
454                 {
455                     CreateTextAttributes();
456                     buttonAttributes.TextAttributes.Text = value.Clone() as StringSelector;
457                     RelayoutRequest();
458                 }
459             }
460         }
461         /// <summary>
462         /// Translateable text string selector in Button.
463         /// </summary>
464         /// <since_tizen> 6 </since_tizen>
465         public StringSelector TranslatableTextSelector
466         {
467             get
468             {
469                 return buttonAttributes?.TextAttributes?.TranslatableText;
470             }
471             set
472             {
473                 if (value != null)
474                 {
475                     CreateTextAttributes();
476                     buttonAttributes.TextAttributes.TranslatableText = value.Clone() as StringSelector;
477                     RelayoutRequest();
478                 }
479             }
480         }
481         /// <summary>
482         /// Text color selector in Button.
483         /// </summary>
484         /// <since_tizen> 6 </since_tizen>
485         public ColorSelector TextColorSelector
486         {
487             get
488             {
489                 return buttonAttributes?.TextAttributes?.TextColor;
490             }
491             set
492             {
493                 if(value != null)
494                 {
495                     CreateTextAttributes();
496                     buttonAttributes.TextAttributes.TextColor = value.Clone() as ColorSelector;
497                     RelayoutRequest();
498                 }
499             }
500         }
501         /// <summary>
502         /// Text font size selector in Button.
503         /// </summary>
504         /// <since_tizen> 6 </since_tizen>
505         public FloatSelector PointSizeSelector
506         {
507             get
508             {
509                 return buttonAttributes?.TextAttributes?.PointSize;
510             }
511             set
512             {
513                 if (value != null)
514                 {
515                     CreateTextAttributes();
516                     buttonAttributes.TextAttributes.PointSize = value.Clone() as FloatSelector;
517                     RelayoutRequest();
518                 }
519             }
520         }
521         /// <summary>
522         /// Icon image's resource url selector in Button.
523         /// </summary>
524         /// <since_tizen> 6 </since_tizen>
525         public StringSelector IconURLSelector
526         {
527             get
528             {
529                 return buttonAttributes?.IconAttributes?.ResourceURL;
530             }
531             set
532             {
533                 if (value != null)
534                 {
535                     CreateIconAttributes();
536                     buttonAttributes.IconAttributes.ResourceURL = value.Clone() as StringSelector;
537                     RelayoutRequest();
538                 }
539             }
540         }
541         /// <summary>
542         /// Background image's resource url selector in Button.
543         /// </summary>
544         /// <since_tizen> 6 </since_tizen>
545         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
546         [EditorBrowsable(EditorBrowsableState.Never)]
547         public StringSelector BackgroundImageURLSelector
548         {
549             get
550             {
551                 return buttonAttributes?.BackgroundImageAttributes?.ResourceURL;
552             }
553             set
554             {
555                 if (value != null)
556                 {
557                     CreateBackgroundAttributes();
558                     buttonAttributes.BackgroundImageAttributes.ResourceURL = value.Clone() as StringSelector;
559                     RelayoutRequest();
560                 }
561             }
562         }
563         /// <summary>
564         /// Background image's border selector in Button.
565         /// </summary>
566         /// <since_tizen> 6 </since_tizen>
567         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
568         [EditorBrowsable(EditorBrowsableState.Never)]
569         public RectangleSelector BackgroundImageBorderSelector
570         {
571             get
572             {
573                 return buttonAttributes?.BackgroundImageAttributes?.Border;
574             }
575             set
576             {
577                 if (value != null)
578                 {
579                     CreateBackgroundAttributes();
580                     buttonAttributes.BackgroundImageAttributes.Border = value.Clone() as RectangleSelector;
581                     RelayoutRequest();
582                 }
583             }
584         }
585         /// <summary>
586         /// Shadow image's resource url selector in Button.
587         /// </summary>
588         /// <since_tizen> 6 </since_tizen>
589         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
590         [EditorBrowsable(EditorBrowsableState.Never)]
591         public StringSelector ShadowImageURLSelector
592         {
593             get
594             {
595                 return buttonAttributes?.ShadowImageAttributes?.ResourceURL;
596             }
597             set
598             {
599                 if (value != null)
600                 {
601                     CreateShadowAttributes();
602                     buttonAttributes.ShadowImageAttributes.ResourceURL = value.Clone() as StringSelector;
603                     RelayoutRequest();
604                 }
605             }
606         }
607         /// <summary>
608         /// Shadow image's border selector in Button.
609         /// </summary>
610         /// <since_tizen> 6 </since_tizen>
611         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
612         [EditorBrowsable(EditorBrowsableState.Never)]
613         public RectangleSelector ShadowImageBorderSelector
614         {
615             get
616             {
617                 return buttonAttributes?.ShadowImageAttributes?.Border;
618             }
619             set
620             {
621                 if (value != null)
622                 {
623                     CreateShadowAttributes();
624                     buttonAttributes.ShadowImageAttributes.Border = value.Clone() as RectangleSelector;
625                     RelayoutRequest();
626                 }
627             }
628         }
629         /// <summary>
630         /// Overlay image's resource url selector in Button.
631         /// </summary>
632         /// <since_tizen> 6 </since_tizen>
633         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
634         [EditorBrowsable(EditorBrowsableState.Never)]
635         public StringSelector OverlayImageURLSelector
636         {
637             get
638             {
639                 return buttonAttributes?.OverlayImageAttributes?.ResourceURL;
640             }
641             set
642             {
643                 if (value != null)
644                 {
645                     CreateOverlayAttributes();
646                     buttonAttributes.OverlayImageAttributes.ResourceURL = value.Clone() as StringSelector;
647                     RelayoutRequest();
648                 }
649             }
650         }
651         /// <summary>
652         /// Overlay image's border selector in Button.
653         /// </summary>
654         /// <since_tizen> 6 </since_tizen>
655         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
656         [EditorBrowsable(EditorBrowsableState.Never)]
657         public RectangleSelector OverlayImageBorderSelector
658         {
659             get
660             {
661                 return buttonAttributes?.OverlayImageAttributes?.Border;
662             }
663             set
664             {
665                 if (value != null)
666                 {
667                     CreateOverlayAttributes();
668                     buttonAttributes.OverlayImageAttributes.Border = value.Clone() as RectangleSelector;
669                     RelayoutRequest();
670                 }
671             }
672         }
673         /// <summary>
674         /// Flag to decide selected state in Button.
675         /// </summary>
676         /// <since_tizen> 6 </since_tizen>
677         public bool IsSelected
678         {
679             get
680             {
681                 return isSelected;
682             }
683             set
684             {
685                 isSelected = value;
686                 UpdateState();
687             }
688         }
689         /// <summary>
690         /// Flag to decide enable or disable in Button.
691         /// </summary>
692         /// <since_tizen> 6 </since_tizen>
693         public bool IsEnabled
694         {
695             get
696             {
697                 return isEnabled;
698             }
699             set
700             {
701                 isEnabled = value;
702                 UpdateState();
703             }
704         }
705
706         /// <summary>
707         /// Icon relative orientation in Button, work only when show icon and text.
708         /// </summary>
709         /// <since_tizen> 6 </since_tizen>
710         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
711         [EditorBrowsable(EditorBrowsableState.Never)]
712         public IconOrientation? IconRelativeOrientation
713         {
714             get
715             {
716                 return buttonAttributes?.IconRelativeOrientation;
717             }
718             set
719             {
720                 if(buttonAttributes != null && buttonAttributes.IconRelativeOrientation != value)
721                 {
722                     buttonAttributes.IconRelativeOrientation = value;
723                     RelayoutRequest();
724                 }
725             }
726         }
727
728         /// <summary>
729         /// Icon padding in Button, work only when show icon and text.
730         /// </summary>
731         /// <since_tizen> 6 </since_tizen>
732         public Extents IconPadding
733         {
734             get
735             {
736                 if (null == buttonAttributes || null == buttonAttributes.IconAttributes)
737                 {
738                     return null;
739                 }
740                 else
741                 {
742                     return new Extents((ushort)buttonAttributes.IconAttributes.PaddingLeft, (ushort)buttonAttributes.IconAttributes.PaddingRight, (ushort)buttonAttributes.IconAttributes.PaddingTop, (ushort)buttonAttributes.IconAttributes.PaddingBottom);
743                 }
744             }
745             set
746             {
747                 if (null != value)
748                 {
749                     CreateIconAttributes();
750                     buttonAttributes.IconAttributes.PaddingLeft = value.Start;
751                     buttonAttributes.IconAttributes.PaddingRight = value.End;
752                     buttonAttributes.IconAttributes.PaddingTop = value.Top;
753                     buttonAttributes.IconAttributes.PaddingBottom = value.Bottom;
754                     RelayoutRequest();
755                 }
756             }
757         }
758
759         /// <summary>
760         /// Text padding in Button, work only when show icon and text.
761         /// </summary>
762         /// <since_tizen> 6 </since_tizen>
763         public Extents TextPadding
764         {
765             get
766             {
767                 if (null == buttonAttributes || null == buttonAttributes.TextAttributes)
768                 {
769                     return null;
770                 }
771                 else
772                 {
773                     return new Extents((ushort)buttonAttributes.TextAttributes.PaddingLeft, (ushort)buttonAttributes.TextAttributes.PaddingRight, (ushort)buttonAttributes.TextAttributes.PaddingTop, (ushort)buttonAttributes.TextAttributes.PaddingBottom);
774                 }
775             }
776             set
777             {
778                 if (null != value)
779                 {
780                     CreateTextAttributes();
781                     buttonAttributes.TextAttributes.PaddingLeft = value.Start;
782                     buttonAttributes.TextAttributes.PaddingRight = value.End;
783                     buttonAttributes.TextAttributes.PaddingTop = value.Top;
784                     buttonAttributes.TextAttributes.PaddingBottom = value.Bottom;
785                     RelayoutRequest();
786                 }
787             }
788         }
789
790         /// <summary>
791         /// Dispose Button and all children on it.
792         /// </summary>
793         /// <param name="type">Dispose type.</param>
794         /// <since_tizen> 6 </since_tizen>
795         protected override void Dispose(DisposeTypes type)
796         {
797             if (disposed)
798             {
799                 return;
800             }
801
802             if (type == DisposeTypes.Explicit)
803             {
804                 if (buttonIcon != null)
805                 {
806                     buttonIcon.Relayout -= OnIconRelayout;
807                     Utility.Dispose(buttonIcon);
808                 }
809                 if (buttonText != null)
810                 {
811                     Utility.Dispose(buttonText);
812                 }
813                 if (overlayImage != null)
814                 {
815                     Utility.Dispose(overlayImage);
816                 }
817                 if (backgroundImage != null)
818                 {
819                     Utility.Dispose(backgroundImage);
820                 }
821                 if (shadowImage != null)
822                 {
823                     Utility.Dispose(shadowImage);
824                 }
825             }
826
827             base.Dispose(type);
828         }
829         /// <summary>
830         /// Called after a key event is received by the view that has had its focus set.
831         /// </summary>
832         /// <param name="key">The key event.</param>
833         /// <returns>True if the key event should be consumed.</returns>
834         /// <since_tizen> 6 </since_tizen>
835         public override bool OnKey(Key key)
836         {
837             if (key.State == Key.StateType.Down)
838             {
839                 if (key.KeyPressedName == "Return")
840                 {
841                     isPressed = true;
842                     UpdateState();
843                     if(isEnabled)
844                     {
845                         ClickEventArgs eventArgs = new ClickEventArgs();
846                         OnClick(eventArgs);
847                     }
848                 }
849             }
850             else if (key.State == Key.StateType.Up)
851             {
852                 if (key.KeyPressedName == "Return")
853                 {
854                     isPressed = false;
855                     if (buttonAttributes.IsSelectable != null && buttonAttributes.IsSelectable == true)
856                     {
857                         isSelected = !isSelected;
858                     }
859                     UpdateState();
860                 }
861             }
862             return base.OnKey(key);
863         }
864
865         /// <summary>
866         /// Called when the control gain key input focus. Should be overridden by derived classes if they need to customize what happens when the focus is gained.
867         /// </summary>
868         /// <since_tizen> 6 </since_tizen>
869         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
870         [EditorBrowsable(EditorBrowsableState.Never)]
871         public override void OnFocusGained()
872         {
873             base.OnFocusGained();
874             UpdateState();
875         }
876         /// <summary>
877         /// Called when the control loses key input focus. Should be overridden by derived classes if they need to customize what happens when the focus is lost.
878         /// </summary>
879         /// <since_tizen> 6 </since_tizen>
880         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
881         [EditorBrowsable(EditorBrowsableState.Never)]
882         public override void OnFocusLost()
883         {
884             base.OnFocusLost();
885             UpdateState();
886         }
887
888         /// <summary>
889         /// Tap gesture event callback.
890         /// </summary>
891         /// <param name="source">Source which recieved touch event.</param>
892         /// <param name="e">Tap gesture event argument.</param>
893         /// <since_tizen> 6 </since_tizen>
894         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
895         [EditorBrowsable(EditorBrowsableState.Never)]
896         protected override void OnTapGestureDetected(object source, TapGestureDetector.DetectedEventArgs e)
897         {
898             if (isEnabled)
899             {
900                 ClickEventArgs eventArgs = new ClickEventArgs();
901                 OnClick(eventArgs);
902                 base.OnTapGestureDetected(source, e);
903             }
904         }
905         /// <summary>
906         /// Called after a touch event is received by the owning view.<br />
907         /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
908         /// </summary>
909         /// <param name="touch">The touch event.</param>
910         /// <returns>True if the event should be consumed.</returns>
911         /// <since_tizen> 6 </since_tizen>
912         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
913         [EditorBrowsable(EditorBrowsableState.Never)]
914         public override bool OnTouch(Touch touch)
915         {
916             PointStateType state = touch.GetState(0);
917       
918             switch(state)
919             {
920                 case PointStateType.Down:
921                     isPressed = true;
922                     UpdateState();
923                     return true;
924                 case PointStateType.Interrupted:
925                     isPressed = false;
926                     UpdateState();
927                     return true;
928                 case PointStateType.Up:
929                     isPressed = false;
930                     if (buttonAttributes.IsSelectable != null && buttonAttributes.IsSelectable == true)
931                     {
932                         isSelected = !isSelected;
933                     }
934                     UpdateState();
935                     return true;
936                 default:
937                     break;
938             }
939             return base.OnTouch(touch);
940         }
941         /// <summary>
942         /// Get Button attribues.
943         /// </summary>
944         /// <since_tizen> 6 </since_tizen>
945         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
946         [EditorBrowsable(EditorBrowsableState.Never)]
947         protected override Attributes GetAttributes()
948         {
949             return new ButtonAttributes();
950         }
951         /// <summary>
952         /// Update Button by attributes.
953         /// </summary>
954         /// <since_tizen> 6 </since_tizen>
955         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
956         [EditorBrowsable(EditorBrowsableState.Never)]
957         protected override void OnUpdate()
958         {
959             if (buttonAttributes.ShadowImageAttributes != null)
960             {
961                 if(shadowImage == null)
962                 {
963                     shadowImage = new ImageView()
964                     {
965                         WidthResizePolicy = ResizePolicyType.FillToParent,
966                         HeightResizePolicy = ResizePolicyType.FillToParent
967                     };
968                     this.Add(shadowImage);
969                 }
970                 ApplyAttributes(shadowImage, buttonAttributes.ShadowImageAttributes);
971             }
972
973             if (buttonAttributes.BackgroundImageAttributes != null)
974             {
975                 if(backgroundImage == null)
976                 {
977                     backgroundImage = new ImageView()
978                     {
979                         WidthResizePolicy = ResizePolicyType.FillToParent,
980                         HeightResizePolicy = ResizePolicyType.FillToParent
981                     };
982                     this.Add(backgroundImage);
983                 }
984                 ApplyAttributes(backgroundImage, buttonAttributes.BackgroundImageAttributes);
985             }
986
987             if (buttonAttributes.OverlayImageAttributes != null)
988             {
989                 if(overlayImage == null)
990                 {
991                     overlayImage = new ImageView()
992                     {
993                         WidthResizePolicy = ResizePolicyType.FillToParent,
994                         HeightResizePolicy = ResizePolicyType.FillToParent
995                     };
996                     this.Add(overlayImage);
997                 }
998                 ApplyAttributes(overlayImage, buttonAttributes.OverlayImageAttributes);
999             }
1000
1001             if (buttonAttributes.TextAttributes != null)
1002             {
1003                 if(buttonText == null)
1004                 {
1005                     buttonText = new TextLabel();
1006                     this.Add(buttonText);
1007                 }
1008                 ApplyAttributes(buttonText, buttonAttributes.TextAttributes);
1009             }
1010
1011             if (buttonAttributes.IconAttributes != null)
1012             {
1013                 if(buttonIcon == null)
1014                 {
1015                     buttonIcon = new ImageView();
1016                     buttonIcon.Relayout += OnIconRelayout;
1017                     this.Add(buttonIcon);
1018                 }
1019                 ApplyAttributes(buttonIcon, buttonAttributes.IconAttributes);
1020             }
1021
1022             MeasureText();
1023             LayoutChild();
1024
1025             Sensitive = isEnabled ? true : false;
1026         }
1027
1028         /// <summary>
1029         /// Update Button State.
1030         /// </summary>
1031         /// <since_tizen> 6 </since_tizen>
1032         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
1033         [EditorBrowsable(EditorBrowsableState.Never)]
1034         protected void UpdateState()
1035         {
1036             ControlStates sourceState = State;
1037             ControlStates targetState;
1038
1039             if(isEnabled)
1040             {
1041                 targetState = isPressed ? ControlStates.Pressed : (IsFocused ? (IsSelected ? ControlStates.SelectedFocused : ControlStates.Focused) : (IsSelected ? ControlStates.Selected : ControlStates.Normal));
1042             }
1043             else
1044             {
1045                 targetState = IsSelected ? ControlStates.DisabledSelected : (IsFocused ? ControlStates.DisabledFocused : ControlStates.Disabled);
1046             }
1047             if(sourceState != targetState)
1048             {
1049                 State = targetState;
1050
1051                 OnUpdate();
1052
1053                 StateChangedEventArgs e = new StateChangedEventArgs
1054                 {
1055                     PreviousState = sourceState,
1056                     CurrentState = targetState
1057                 };
1058                 stateChangeHander?.Invoke(this, e);
1059             }
1060         }
1061         /// <summary>
1062         /// It is hijack by using protected, attributes copy problem when class inherited from Button.
1063         /// </summary>
1064         /// <since_tizen> 6 </since_tizen>
1065         private void Initialize()
1066         {
1067             buttonAttributes = attributes as ButtonAttributes;
1068             if (buttonAttributes == null)
1069             {
1070                 throw new Exception("Button attribute parse error.");
1071             }
1072
1073             ApplyAttributes(this, buttonAttributes);
1074             LayoutDirectionChanged += OnLayoutDirectionChanged;
1075         }
1076
1077         /// <summary>
1078         /// Measure text, it can be override.
1079         /// </summary>
1080         /// <since_tizen> 6 </since_tizen>
1081         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
1082         [EditorBrowsable(EditorBrowsableState.Never)]
1083         protected virtual void MeasureText()
1084         {
1085             if (buttonAttributes.IconRelativeOrientation == null || buttonIcon == null || buttonText == null)
1086             {
1087                 return;
1088             }
1089             buttonText.WidthResizePolicy = ResizePolicyType.Fixed;
1090             buttonText.HeightResizePolicy = ResizePolicyType.Fixed;
1091             int textPaddingLeft = buttonAttributes.TextAttributes.PaddingLeft;
1092             int textPaddingRight = buttonAttributes.TextAttributes.PaddingRight;
1093             int textPaddingTop = buttonAttributes.TextAttributes.PaddingTop;
1094             int textPaddingBottom = buttonAttributes.TextAttributes.PaddingBottom;
1095
1096             int iconPaddingLeft = buttonAttributes.IconAttributes.PaddingLeft;
1097             int iconPaddingRight = buttonAttributes.IconAttributes.PaddingRight;
1098             int iconPaddingTop = buttonAttributes.IconAttributes.PaddingTop;
1099             int iconPaddingBottom = buttonAttributes.IconAttributes.PaddingBottom;
1100
1101             if (IconRelativeOrientation == IconOrientation.Top || IconRelativeOrientation == IconOrientation.Bottom)
1102             {
1103                 buttonText.SizeWidth = SizeWidth - textPaddingLeft - textPaddingRight;
1104                 buttonText.SizeHeight = SizeHeight - textPaddingTop - textPaddingBottom - iconPaddingTop - iconPaddingBottom - buttonIcon.SizeHeight;
1105             }
1106             else
1107             {
1108                 buttonText.SizeWidth = SizeWidth - textPaddingLeft - textPaddingRight - iconPaddingLeft - iconPaddingRight - buttonIcon.SizeWidth;
1109                 buttonText.SizeHeight = SizeHeight - textPaddingTop - textPaddingBottom;
1110             }
1111         }
1112         /// <summary>
1113         /// Layout child, it can be override.
1114         /// </summary>
1115         /// <since_tizen> 6 </since_tizen>
1116         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
1117         [EditorBrowsable(EditorBrowsableState.Never)]
1118         protected virtual void LayoutChild()
1119         {
1120             if (buttonAttributes.IconRelativeOrientation == null || buttonIcon == null || buttonText == null)
1121             {
1122                 return;
1123             }
1124
1125             int textPaddingLeft = buttonAttributes.TextAttributes.PaddingLeft;
1126             int textPaddingRight = buttonAttributes.TextAttributes.PaddingRight;
1127             int textPaddingTop = buttonAttributes.TextAttributes.PaddingTop;
1128             int textPaddingBottom = buttonAttributes.TextAttributes.PaddingBottom;
1129
1130             int iconPaddingLeft = buttonAttributes.IconAttributes.PaddingLeft;
1131             int iconPaddingRight = buttonAttributes.IconAttributes.PaddingRight;
1132             int iconPaddingTop = buttonAttributes.IconAttributes.PaddingTop;
1133             int iconPaddingBottom = buttonAttributes.IconAttributes.PaddingBottom;
1134
1135             switch (IconRelativeOrientation)
1136             {
1137                 case IconOrientation.Top:
1138                     buttonIcon.PositionUsesPivotPoint = true;
1139                     buttonIcon.ParentOrigin = NUI.ParentOrigin.TopCenter;
1140                     buttonIcon.PivotPoint = NUI.PivotPoint.TopCenter;
1141                     buttonIcon.Position2D = new Position2D(0, iconPaddingTop);
1142
1143                     buttonText.PositionUsesPivotPoint = true;
1144                     buttonText.ParentOrigin = NUI.ParentOrigin.BottomCenter;
1145                     buttonText.PivotPoint = NUI.PivotPoint.BottomCenter;
1146                     buttonText.Position2D = new Position2D(0, -textPaddingBottom);
1147                     break;
1148                 case IconOrientation.Bottom:
1149                     buttonIcon.PositionUsesPivotPoint = true;
1150                     buttonIcon.ParentOrigin = NUI.ParentOrigin.BottomCenter;
1151                     buttonIcon.PivotPoint = NUI.PivotPoint.BottomCenter;
1152                     buttonIcon.Position2D = new Position2D(0, -iconPaddingBottom);
1153
1154                     buttonText.PositionUsesPivotPoint = true;
1155                     buttonText.ParentOrigin = NUI.ParentOrigin.TopCenter;
1156                     buttonText.PivotPoint = NUI.PivotPoint.TopCenter;
1157                     buttonText.Position2D = new Position2D(0, textPaddingTop);
1158                     break;
1159                 case IconOrientation.Left:
1160                     if (LayoutDirection == ViewLayoutDirectionType.LTR)
1161                     {
1162                         buttonIcon.PositionUsesPivotPoint = true;
1163                         buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterLeft;
1164                         buttonIcon.PivotPoint = NUI.PivotPoint.CenterLeft;
1165                         buttonIcon.Position2D = new Position2D(iconPaddingLeft, 0);
1166
1167                         buttonText.PositionUsesPivotPoint = true;
1168                         buttonText.ParentOrigin = NUI.ParentOrigin.CenterRight;
1169                         buttonText.PivotPoint = NUI.PivotPoint.CenterRight;
1170                         buttonText.Position2D = new Position2D(-textPaddingRight, 0);
1171                     }
1172                     else
1173                     {
1174                         buttonIcon.PositionUsesPivotPoint = true;
1175                         buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterRight;
1176                         buttonIcon.PivotPoint = NUI.PivotPoint.CenterRight;
1177                         buttonIcon.Position2D = new Position2D(-iconPaddingLeft, 0);
1178
1179                         buttonText.PositionUsesPivotPoint = true;
1180                         buttonText.ParentOrigin = NUI.ParentOrigin.CenterLeft;
1181                         buttonText.PivotPoint = NUI.PivotPoint.CenterLeft;
1182                         buttonText.Position2D = new Position2D(textPaddingRight, 0);
1183                     }
1184
1185                     break;
1186                 case IconOrientation.Right:
1187                     if (LayoutDirection == ViewLayoutDirectionType.RTL)
1188                     {
1189                         buttonIcon.PositionUsesPivotPoint = true;
1190                         buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterLeft;
1191                         buttonIcon.PivotPoint = NUI.PivotPoint.CenterLeft;
1192                         buttonIcon.Position2D = new Position2D(iconPaddingRight, 0);
1193
1194                         buttonText.PositionUsesPivotPoint = true;
1195                         buttonText.ParentOrigin = NUI.ParentOrigin.CenterRight;
1196                         buttonText.PivotPoint = NUI.PivotPoint.CenterRight;
1197                         buttonText.Position2D = new Position2D(-textPaddingLeft, 0);
1198                     }
1199                     else
1200                     {
1201                         buttonIcon.PositionUsesPivotPoint = true;
1202                         buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterRight;
1203                         buttonIcon.PivotPoint = NUI.PivotPoint.CenterRight;
1204                         buttonIcon.Position2D = new Position2D(-iconPaddingRight, 0);
1205
1206                         buttonText.PositionUsesPivotPoint = true;
1207                         buttonText.ParentOrigin = NUI.ParentOrigin.CenterLeft;
1208                         buttonText.PivotPoint = NUI.PivotPoint.CenterLeft;
1209                         buttonText.Position2D = new Position2D(textPaddingLeft, 0);
1210                     }
1211                     break;
1212                 default:
1213                     break;
1214             }
1215         }
1216         /// <summary>
1217         /// Theme change callback when theme is changed, this callback will be trigger.
1218         /// </summary>
1219         /// <since_tizen> 6 </since_tizen>
1220         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
1221         [EditorBrowsable(EditorBrowsableState.Never)]
1222         protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
1223         {
1224             ButtonAttributes tempAttributes = StyleManager.Instance.GetAttributes(style) as ButtonAttributes;
1225             if(tempAttributes != null)
1226             {
1227                 attributes = buttonAttributes = tempAttributes;
1228                 RelayoutRequest();
1229             }
1230         }
1231
1232         private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
1233         {
1234             MeasureText();
1235             LayoutChild();
1236         }
1237
1238         private void OnClick(ClickEventArgs eventArgs)
1239         {
1240             ClickEvent?.Invoke(this, eventArgs);
1241         }
1242
1243         private void OnIconRelayout(object sender, EventArgs e)
1244         {
1245             MeasureText();
1246             LayoutChild();
1247         }
1248
1249         private void CreateBackgroundAttributes()
1250         {
1251             if (buttonAttributes.BackgroundImageAttributes == null)
1252             {
1253                 buttonAttributes.BackgroundImageAttributes = new ImageAttributes()
1254                 {
1255                     PositionUsesPivotPoint = true,
1256                     ParentOrigin = Tizen.NUI.ParentOrigin.Center,
1257                     PivotPoint = Tizen.NUI.PivotPoint.Center,
1258                     WidthResizePolicy = ResizePolicyType.FillToParent,
1259                     HeightResizePolicy = ResizePolicyType.FillToParent
1260                 };
1261             }
1262         }
1263
1264         private void CreateShadowAttributes()
1265         {
1266             if (buttonAttributes.ShadowImageAttributes == null)
1267             {
1268                 buttonAttributes.ShadowImageAttributes = new ImageAttributes()
1269                 {
1270                     PositionUsesPivotPoint = true,
1271                     ParentOrigin = Tizen.NUI.ParentOrigin.Center,
1272                     PivotPoint = Tizen.NUI.PivotPoint.Center,
1273                     WidthResizePolicy = ResizePolicyType.FillToParent,
1274                     HeightResizePolicy = ResizePolicyType.FillToParent
1275                 };
1276             }
1277         }
1278
1279         private void CreateOverlayAttributes()
1280         {
1281             if (buttonAttributes.OverlayImageAttributes == null)
1282             {
1283                 buttonAttributes.OverlayImageAttributes = new ImageAttributes()
1284                 {
1285                     PositionUsesPivotPoint = true,
1286                     ParentOrigin = Tizen.NUI.ParentOrigin.Center,
1287                     PivotPoint = Tizen.NUI.PivotPoint.Center,
1288                     WidthResizePolicy = ResizePolicyType.FillToParent,
1289                     HeightResizePolicy = ResizePolicyType.FillToParent
1290                 };
1291             }
1292         }
1293
1294         private void CreateTextAttributes()
1295         {
1296             if (buttonAttributes.TextAttributes == null)
1297             {
1298                 buttonAttributes.TextAttributes = new TextAttributes()
1299                 {
1300                     PositionUsesPivotPoint = true,
1301                     ParentOrigin = Tizen.NUI.ParentOrigin.Center,
1302                     PivotPoint = Tizen.NUI.PivotPoint.Center,
1303                     WidthResizePolicy = ResizePolicyType.FillToParent,
1304                     HeightResizePolicy = ResizePolicyType.FillToParent,
1305                     HorizontalAlignment = HorizontalAlignment.Center,
1306                     VerticalAlignment = VerticalAlignment.Center
1307                 };
1308             }
1309         }
1310
1311         private void CreateIconAttributes()
1312         {
1313             if (buttonAttributes.IconAttributes == null)
1314             {
1315                 buttonAttributes.IconAttributes = new ImageAttributes()
1316                 {
1317                     PositionUsesPivotPoint = true,
1318                     ParentOrigin = Tizen.NUI.ParentOrigin.Center,
1319                     PivotPoint = Tizen.NUI.PivotPoint.Center,
1320                     WidthResizePolicy = ResizePolicyType.UseNaturalSize,
1321                     HeightResizePolicy = ResizePolicyType.UseNaturalSize,
1322                 };
1323             }
1324         }
1325         /// <summary>
1326         /// ClickEventArgs is a class to record button click event arguments which will sent to user.
1327         /// </summary>
1328         /// <since_tizen> 6 </since_tizen>
1329         public class ClickEventArgs : EventArgs
1330         {
1331         }
1332         /// <summary>
1333         /// StateChangeEventArgs is a class to record button state change event arguments which will sent to user.
1334         /// </summary>
1335         /// <since_tizen> 6 </since_tizen>
1336         public class StateChangedEventArgs : EventArgs
1337         {
1338             /// <summary> previous state of Button </summary>
1339             /// <since_tizen> 6 </since_tizen>
1340             public ControlStates PreviousState;
1341             /// <summary> current state of Button </summary>
1342             /// <since_tizen> 6 </since_tizen>
1343             public ControlStates CurrentState;
1344         }
1345
1346     }
1347 }