1 /*
2  *  "GEDKeeper", the personal genealogical database editor.
3  *  Copyright (C) 2009-2020 by Sergey V. Zhdanovskih.
4  *
5  *  This file is part of "GEDKeeper".
6  *
7  *  This program is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 using System;
22 using System.Collections;
23 using System.Collections.Generic;
24 using BSLib;
25 using BSLib.Design.Graphics;
26 using BSLib.Design.MVP;
27 using BSLib.Design.MVP.Controls;
28 using Eto.Drawing;
29 using Eto.Forms;
30 using GDModel;
31 using GKCore.MVP.Controls;
32 using GKUI.Components;
33 
34 namespace GKUI.Platform
35 {
36     public abstract class BaseControlHandler<T, TThis> : ControlHandler<T, TThis>, IBaseControl
37         where T : Control
38         where TThis : ControlHandler<T, TThis>
39     {
BaseControlHandler(T control)40         protected BaseControlHandler(T control) : base(control)
41         {
42         }
43 
44         public bool Enabled
45         {
46             get { return Control.Enabled; }
47             set { Control.Enabled = value; }
48         }
49 
Activate()50         public void Activate()
51         {
52             Control.Focus();
53         }
54     }
55 
56 
57     public sealed class LabelHandler : BaseControlHandler<Label, LabelHandler>, ILabel
58     {
LabelHandler(Label control)59         public LabelHandler(Label control) : base(control)
60         {
61         }
62 
63         public string Text
64         {
65             get { return Control.Text; }
66             set { Control.Text = value; }
67         }
68     }
69 
70     public sealed class ButtonHandler : BaseControlHandler<Button, ButtonHandler>, IButton
71     {
ButtonHandler(Button control)72         public ButtonHandler(Button control) : base(control)
73         {
74         }
75 
76         public string Text
77         {
78             get { return Control.Text; }
79             set { Control.Text = value; }
80         }
81     }
82 
83     public sealed class CheckBoxHandler : BaseControlHandler<CheckBox, CheckBoxHandler>, ICheckBox
84     {
CheckBoxHandler(CheckBox control)85         public CheckBoxHandler(CheckBox control) : base(control)
86         {
87         }
88 
89         public bool Checked
90         {
91             get { return Control.Checked.GetValueOrDefault(); }
92             set { Control.Checked = value; }
93         }
94 
95         public string Text
96         {
97             get { return Control.Text; }
98             set { Control.Text = value; }
99         }
100     }
101 
102     public sealed class RadioButtonHandler : BaseControlHandler<RadioButton, RadioButtonHandler>, IRadioButton
103     {
RadioButtonHandler(RadioButton control)104         public RadioButtonHandler(RadioButton control) : base(control)
105         {
106         }
107 
108         public bool Checked
109         {
110             get { return Control.Checked; }
111             set { Control.Checked = value; }
112         }
113 
114         public string Text
115         {
116             get { return Control.Text; }
117             set { Control.Text = value; }
118         }
119     }
120 
121     public sealed class ComboBoxHandler : BaseControlHandler<ComboBox, ComboBoxHandler>, IComboBox
122     {
ComboBoxHandler(ComboBox control)123         public ComboBoxHandler(ComboBox control) : base(control)
124         {
125         }
126 
127         public new bool Enabled
128         {
129             get { return Control.Enabled; }
130             set {
131                 Control.Enabled = value;
132                 //Control.BackgroundColor = (value) ? SystemColors.WindowBackground : SystemColors.Control;
133             }
134         }
135 
136         public IList Items
137         {
138             get { return Control.Items; }
139         }
140 
141         public bool ReadOnly
142         {
143             get { return Control.ReadOnly; }
144             set { Control.ReadOnly = value; }
145         }
146 
147         public int SelectedIndex
148         {
149             get { return Control.SelectedIndex; }
150             set { Control.SelectedIndex = value; }
151         }
152 
153         public object SelectedItem
154         {
155             get { return Control.SelectedValue; }
156             set { Control.SelectedValue = value; }
157         }
158 
159         public bool Sorted
160         {
161             get { return false; }
162             set {
163                 if (value) {
164                     Sort();
165                 }
166             }
167         }
168 
169         public string Text
170         {
171             get { return Control.Text; }
172             set {
173                 Control.AutoComplete = true; // FIXME: Wrapper for EtoBug in ComboBox.setText
174                 Control.Text = value;
175                 Control.AutoComplete = false;
176             }
177         }
178 
Add(object item)179         public void Add(object item)
180         {
181             Control.Items.Add((string)item);
182         }
183 
AddItem(string caption, T tag, IImage image = null)184         public void AddItem<T>(string caption, T tag, IImage image = null)
185         {
186             Control.Items.Add(new GKComboItem<T>(caption, tag, image));
187         }
188 
AddRange(IEnumerable<object> items, bool sorted = false)189         public void AddRange(IEnumerable<object> items, bool sorted = false)
190         {
191             Control.Items.Clear();
192             //Control.Sorted = false;
193             foreach (var item in items) {
194                 Control.Items.Add(new GKComboItem<object>(item.ToString(), item));
195             }
196             //Control.Sorted = sorted;
197         }
198 
AddStrings(StringList strings)199         public void AddStrings(StringList strings)
200         {
201             int num = strings.Count;
202             for (int i = 0; i < num; i++) {
203                 AddItem(strings[i], strings.GetObject(i));
204             }
205         }
206 
BeginUpdate()207         public void BeginUpdate()
208         {
209             //Control.BeginUpdate();
210         }
211 
Clear()212         public void Clear()
213         {
214             Control.Items.Clear();
215         }
216 
EndUpdate()217         public void EndUpdate()
218         {
219             //Control.EndUpdate();
220         }
221 
Sort()222         public void Sort()
223         {
224             Control.Items.Sort((x, y) => string.Compare(x.Text, y.Text, StringComparison.CurrentCulture));
225         }
226 
GetSelectedTag()227         public T GetSelectedTag<T>()
228         {
229             object selectedItem = Control.SelectedValue;
230             GKComboItem<T> comboItem = selectedItem as GKComboItem<T>;
231             T itemTag = (comboItem != null) ? comboItem.Tag : default(T);
232             return itemTag;
233         }
234 
SetSelectedTag(T tagValue, bool allowDefault = true)235         public void SetSelectedTag<T>(T tagValue, bool allowDefault = true)
236         {
237             foreach (object item in Control.Items) {
238                 GKComboItem<T> comboItem = item as GKComboItem<T>;
239 
240                 if (comboItem != null && object.Equals(comboItem.Tag, tagValue)) {
241                     Control.SelectedValue = item;
242                     return;
243                 }
244             }
245 
246             if (allowDefault) {
247                 Control.SelectedIndex = 0;
248             }
249         }
250     }
251 
252     public sealed class TextBoxHandler : BaseControlHandler<TextBox, TextBoxHandler>, ITextBox
253     {
TextBoxHandler(TextBox control)254         public TextBoxHandler(TextBox control) : base(control)
255         {
256         }
257 
258         public new bool Enabled
259         {
260             get { return Control.Enabled; }
261             set {
262                 Control.Enabled = value;
263                 SetBackColor();
264             }
265         }
266 
267         public string[] Lines
268         {
269             get { return UIHelper.Convert(Control.Text); }
270             set { /* TODO! */ }
271         }
272 
273         public bool ReadOnly
274         {
275             get { return Control.ReadOnly; }
276             set {
277                 Control.ReadOnly = value;
278                 SetBackColor();
279             }
280         }
281 
282         public string SelectedText
283         {
284             get { return Control.SelectedText; }
285             set { Control.SelectedText = value; }
286         }
287 
288         public string Text
289         {
290             get { return Control.Text; }
291             set { Control.Text = value; }
292         }
293 
AppendText(string text)294         public void AppendText(string text)
295         {
296             //Control.Append(text, true);
297         }
298 
Clear()299         public void Clear()
300         {
301             Control.Text = string.Empty;
302         }
303 
SetBackColor()304         private void SetBackColor()
305         {
306             Control.BackgroundColor = (!Control.ReadOnly && Enabled) ? SystemColors.WindowBackground : SystemColors.Control;
307         }
308 
Copy()309         public void Copy()
310         {
311             UIHelper.SetClipboardText(Control.SelectedText);
312         }
313 
SelectAll()314         public void SelectAll()
315         {
316             Control.SelectAll();
317         }
318     }
319 
320     public sealed class TextAreaHandler : BaseControlHandler<TextArea, TextAreaHandler>, ITextBox
321     {
TextAreaHandler(TextArea control)322         public TextAreaHandler(TextArea control) : base(control)
323         {
324         }
325 
326         public new bool Enabled
327         {
328             get { return Control.Enabled; }
329             set {
330                 Control.Enabled = value;
331                 SetBackColor();
332             }
333         }
334 
335         public string[] Lines
336         {
337             get { return UIHelper.Convert(Control.Text); }
338             set { /* TODO! */ }
339         }
340 
341         public bool ReadOnly
342         {
343             get { return Control.ReadOnly; }
344             set {
345                 Control.ReadOnly = value;
346                 SetBackColor();
347             }
348         }
349 
350         public string SelectedText
351         {
352             get { return Control.SelectedText; }
353             set { Control.SelectedText = value; }
354         }
355 
356         public string Text
357         {
358             get { return Control.Text; }
359             set { Control.Text = value; }
360         }
361 
AppendText(string text)362         public void AppendText(string text)
363         {
364             Control.Append(text, true);
365         }
366 
Clear()367         public void Clear()
368         {
369             Control.Text = string.Empty;
370         }
371 
SetBackColor()372         private void SetBackColor()
373         {
374             Control.BackgroundColor = (!Control.ReadOnly && Enabled) ? SystemColors.WindowBackground : SystemColors.Control;
375         }
376 
Copy()377         public void Copy()
378         {
379             UIHelper.SetClipboardText(Control.SelectedText);
380         }
381 
SelectAll()382         public void SelectAll()
383         {
384             Control.SelectAll();
385         }
386     }
387 
388     public sealed class MaskedTextBoxHandler : BaseControlHandler<MaskedTextBox, MaskedTextBoxHandler>, ITextBox
389     {
MaskedTextBoxHandler(MaskedTextBox control)390         public MaskedTextBoxHandler(MaskedTextBox control) : base(control)
391         {
392         }
393 
394         public new bool Enabled
395         {
396             get { return Control.Enabled; }
397             set {
398                 Control.Enabled = value;
399                 //Control.BackgroundColor = (value) ? SystemColors.WindowBackground : SystemColors.Control;
400             }
401         }
402 
403         public string[] Lines
404         {
405             get { return UIHelper.Convert(Control.Text); }
406             set { /* TODO! */ }
407         }
408 
409         public bool ReadOnly
410         {
411             get { return Control.ReadOnly; }
412             set { Control.ReadOnly = value; }
413         }
414 
415         public string SelectedText
416         {
417             get { return Control.SelectedText; }
418             set { Control.SelectedText = value; }
419         }
420 
421         public string Text
422         {
423             get { return Control.Text; }
424             set { Control.Text = value; }
425         }
426 
AppendText(string text)427         public void AppendText(string text)
428         {
429             //Control.Append(text, true);
430         }
431 
Clear()432         public void Clear()
433         {
434             Control.Text = string.Empty;
435         }
436 
Copy()437         public void Copy()
438         {
439             UIHelper.SetClipboardText(Control.SelectedText);
440         }
441 
SelectAll()442         public void SelectAll()
443         {
444             Control.SelectAll();
445         }
446     }
447 
448     public sealed class DateBoxHandler : BaseControlHandler<GKDateBox, DateBoxHandler>, IDateBox
449     {
DateBoxHandler(GKDateBox control)450         public DateBoxHandler(GKDateBox control) : base(control)
451         {
452         }
453 
454         public string NormalizeDate
455         {
456             get { return Control.NormalizeDate; }
457             set { Control.NormalizeDate = value; }
458         }
459 
460         public bool ReadOnly
461         {
462             get { return Control.ReadOnly; }
463             set { Control.ReadOnly = value; }
464         }
465 
466         public string SelectedText
467         {
468             get { return Control.SelectedText; }
469             set { Control.SelectedText = value; }
470         }
471 
472         public string Text
473         {
474             get { return Control.Text; }
475             set { Control.Text = value; }
476         }
477 
Clear()478         public void Clear()
479         {
480             Control.Text = string.Empty;
481         }
482 
Copy()483         public void Copy()
484         {
485             UIHelper.SetClipboardText(Control.SelectedText);
486         }
487 
SelectAll()488         public void SelectAll()
489         {
490             Control.SelectAll();
491         }
492     }
493 
494     public sealed class NumericBoxHandler : BaseControlHandler<NumericUpDown, NumericBoxHandler>, INumericBox
495     {
NumericBoxHandler(NumericUpDown control)496         public NumericBoxHandler(NumericUpDown control) : base(control)
497         {
498         }
499 
500         public bool ReadOnly
501         {
502             get { return Control.ReadOnly; }
503             set { Control.ReadOnly = value; }
504         }
505 
506         public string Text
507         {
508             get { return Value.ToString(); }
509             set { /* TODO */ }
510         }
511 
512         public double Value
513         {
514             get { return Control.Value; }
515             set { Control.Value = value; }
516         }
517     }
518 
519     public sealed class TreeViewHandler : BaseControlHandler<TreeView, TreeViewHandler>, ITreeView
520     {
521         private TreeItem fRootNode;
522 
TreeViewHandler(TreeView control)523         public TreeViewHandler(TreeView control) : base(control)
524         {
525         }
526 
AddNode(ITVNode parent, string name, object tag)527         public ITVNode AddNode(ITVNode parent, string name, object tag)
528         {
529             var node = new GKTreeNode(name, tag);
530             if (parent == null) {
531                 fRootNode.Children.Add(node);
532             } else {
533                 ((GKTreeNode)parent).Children.Add(node);
534             }
535             return node;
536         }
537 
BeginUpdate()538         public void BeginUpdate()
539         {
540             Control.DataStore = null;
541             fRootNode = new TreeItem();
542         }
543 
Clear()544         public void Clear()
545         {
546         }
547 
EndUpdate()548         public void EndUpdate()
549         {
550             Control.DataStore = fRootNode;
551             Control.RefreshData();
552         }
553 
Expand(ITVNode node)554         public void Expand(ITVNode node)
555         {
556             GKTreeNode treeNode = node as GKTreeNode;
557             if (treeNode != null) {
558                 treeNode.Expanded = true;
559             }
560         }
561 
GetSelectedData()562         public object GetSelectedData()
563         {
564             GKTreeNode node = Control.SelectedItem as GKTreeNode;
565             return (node == null) ? null : node.Tag;
566         }
567     }
568 
569     public sealed class ProgressBarHandler : BaseControlHandler<ProgressBar, ProgressBarHandler>, IProgressBar
570     {
ProgressBarHandler(ProgressBar control)571         public ProgressBarHandler(ProgressBar control) : base(control)
572         {
573         }
574 
575         public int Minimum
576         {
577             get { return Control.MinValue; }
578             set { Control.MinValue = value; }
579         }
580 
581         public int Maximum
582         {
583             get { return Control.MaxValue; }
584             set { Control.MaxValue = value; }
585         }
586 
587         public int Value
588         {
589             get { return Control.Value; }
590             set { Control.Value = value; }
591         }
592 
Increment(int value)593         public void Increment(int value)
594         {
595             Control.Value += value;
596         }
597     }
598 
599     public sealed class LogChartHandler : BaseControlHandler<LogChart, LogChartHandler>, ILogChart
600     {
LogChartHandler(LogChart control)601         public LogChartHandler(LogChart control) : base(control)
602         {
603         }
604 
AddFragment(int val)605         public void AddFragment(int val)
606         {
607             Control.AddFragment(val);
608         }
609 
Clear()610         public void Clear()
611         {
612             Control.Clear();
613         }
614     }
615 
616     public sealed class TabControlHandler : BaseControlHandler<TabControl, TabControlHandler>, ITabControl
617     {
TabControlHandler(TabControl control)618         public TabControlHandler(TabControl control) : base(control)
619         {
620         }
621 
622         public int SelectedIndex
623         {
624             get { return Control.SelectedIndex; }
625             set { Control.SelectedIndex = value; }
626         }
627     }
628 
629     public sealed class MenuItemHandler : ControlHandler<ButtonMenuItem, MenuItemHandler>, IMenuItem
630     {
MenuItemHandler(ButtonMenuItem control)631         public MenuItemHandler(ButtonMenuItem control) : base(control)
632         {
633         }
634 
635         public bool Checked
636         {
637             get { return false; }
638             set { }
639         }
640 
641         public bool Enabled
642         {
643             get { return Control.Enabled; }
644             set { Control.Enabled = value; }
645         }
646 
647         public object Tag
648         {
649             get { return Control.Tag; }
650             set { Control.Tag = value; }
651         }
652 
653         public int ItemsCount
654         {
655             get { return Control.Items.Count; }
656         }
657 
AddItem(string text, object tag, IImage image, ItemAction action)658         public IMenuItem AddItem(string text, object tag, IImage image, ItemAction action)
659         {
660             var item = new MenuItemEx(text, tag, image, action);
661             Control.Items.Add(item);
662             return item;
663         }
664 
ClearItems()665         public void ClearItems()
666         {
667             Control.Items.Clear();
668         }
669     }
670 
671     public sealed class DateControlHandler : BaseControlHandler<GKDateControl, DateControlHandler>, IDateControl
672     {
673         public GDMCustomDate Date
674         {
675             get { return Control.Date; }
676             set { Control.Date = value; }
677         }
678 
DateControlHandler(GKDateControl control)679         public DateControlHandler(GKDateControl control) : base(control)
680         {
681         }
682     }
683 }
684