More mouse support, make sample quit, some mouse drag work

This commit is contained in:
Miguel de Icaza
2018-01-12 23:19:30 -05:00
parent 0269b0e9b7
commit 863343cd3e
6 changed files with 265 additions and 39 deletions

View File

@@ -25,6 +25,18 @@ namespace Terminal {
int hot_pos = -1;
bool is_default;
/// <summary>
/// Gets or sets a value indicating whether this <see cref="T:Terminal.Button"/> is the default action to activate on return on a dialog.
/// </summary>
/// <value><c>true</c> if is default; otherwise, <c>false</c>.</value>
public bool IsDefault {
get => is_default;
set {
is_default = value;
Update ();
}
}
/// <summary>
/// Clicked event, raised when the button is clicked.
/// </summary>
@@ -33,7 +45,7 @@ namespace Terminal {
/// raised when the button is activated either with
/// the mouse or the keyboard.
/// </remarks>
public event EventHandler Clicked;
public Action Clicked;
/// <summary>
/// Public constructor, creates a button based on
@@ -76,25 +88,31 @@ namespace Terminal {
set {
text = value;
if (is_default)
shown_text = "[< " + value + " >]";
else
shown_text = "[ " + value + " ]";
hot_pos = -1;
hot_key = (char)0;
int i = 0;
foreach (char c in shown_text) {
if (Char.IsUpper (c)) {
hot_key = c;
hot_pos = i;
break;
}
i++;
}
Update ();
}
}
internal void Update ()
{
if (IsDefault)
shown_text = "[< " + text + " >]";
else
shown_text = "[ " + text + " ]";
hot_pos = -1;
hot_key = (char)0;
int i = 0;
foreach (char c in shown_text) {
if (Char.IsUpper (c)) {
hot_key = c;
hot_pos = i;
break;
}
i++;
}
SetNeedsDisplay ();
}
/// <summary>
/// Public constructor, creates a button based on
/// the given text at the given position.
@@ -109,7 +127,7 @@ namespace Terminal {
{
CanFocus = true;
this.is_default = is_default;
this.IsDefault = is_default;
Text = s;
}
@@ -136,7 +154,7 @@ namespace Terminal {
if (Char.ToUpper ((char)key.KeyValue) == hot_key) {
this.SuperView.SetFocus (this);
if (Clicked != null)
Clicked (this, EventArgs.Empty);
Clicked ();
return true;
}
return false;
@@ -152,9 +170,9 @@ namespace Terminal {
public override bool ProcessColdKey (KeyEvent kb)
{
if (is_default && kb.KeyValue == '\n') {
if (IsDefault && kb.KeyValue == '\n') {
if (Clicked != null)
Clicked (this, EventArgs.Empty);
Clicked ();
return true;
}
return CheckKey (kb);
@@ -165,22 +183,23 @@ namespace Terminal {
var c = kb.KeyValue;
if (c == '\n' || c == ' ' || Char.ToUpper ((char)c) == hot_key) {
if (Clicked != null)
Clicked (this, EventArgs.Empty);
Clicked ();
return true;
}
return base.ProcessKey (kb);
}
#if false
public override void ProcessMouse (Curses.MouseEvent ev)
{
if ((ev.ButtonState & Curses.Event.Button1Clicked) != 0) {
Container.SetFocus (this);
Container.Redraw ();
if (Clicked != null)
Clicked (this, EventArgs.Empty);
}
}
#endif
public override bool MouseEvent(MouseEvent me)
{
if (me.Flags == MouseFlags.Button1Clicked) {
SuperView.SetFocus (this);
SetNeedsDisplay ();
if (Clicked != null)
Clicked ();
return true;
}
return false;
}
}
}

View File

@@ -192,6 +192,30 @@ namespace Terminal {
}
return base.ProcessKey (kb);
}
public override bool MouseEvent(MouseEvent me)
{
if (me.Flags == MouseFlags.Button1Clicked || me.Flags == MouseFlags.Button1Released) {
if (me.Y < 1)
return true;
var item = me.Y - 1;
if (item >= barItems.Children.Length)
return true;
host.CloseMenu ();
Run (barItems.Children [item].Action);
return true;
}
if (me.Flags == MouseFlags.Button1Pressed) {
if (me.Y < 1)
return true;
if (me.Y - 1 >= barItems.Children.Length)
return true;
current = me.Y - 1;
SetNeedsDisplay ();
return true;
}
return false;
}
}
/// <summary>
@@ -275,6 +299,7 @@ namespace Terminal {
SuperView.SetFocus (openMenu);
}
// Starts the menu from a hotkey
void StartMenu ()
{
if (openMenu != null)
@@ -286,6 +311,18 @@ namespace Terminal {
OpenMenu (selected);
}
// Activates the menu, handles either first focus, or activating an entry when it was already active
// For mouse events.
void Activate (int idx)
{
selected = idx;
if (openMenu == null)
previousFocused = SuperView.Focused;
OpenMenu (idx);
SetNeedsDisplay ();
}
internal void CloseMenu ()
{
selected = -1;
@@ -368,6 +405,22 @@ namespace Terminal {
SetNeedsDisplay ();
return true;
}
public override bool MouseEvent(MouseEvent me)
{
if (me.Flags == MouseFlags.Button1Clicked) {
int pos = 1;
int cx = me.X;
for (int i = 0; i < Menus.Length; i++) {
if (cx > pos && me.X < pos + 1 + Menus [i].TitleLength) {
Activate (i);
return true;
}
pos += 2 + Menus [i].TitleLength + 1;
}
}
return false;
}
}
}