Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1816 fix parenting/close behavior for Gtk drop-down dialog box #1818

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Eto.Gtk/CustomControls/BaseComboBox.gtk3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ private void HandleButtonPressEvent(object o, ButtonPressEventArgs args)
{
if (args.Event.X > AllocatedWidth - ArrowWidth && Sensitive)
{
OnPopupButtonClicked(EventArgs.Empty);
if (args.Event.Type == Gdk.EventType.ButtonPress)
{
OnPopupButtonClicked(EventArgs.Empty);
}
}
}

Expand Down
77 changes: 69 additions & 8 deletions src/Eto.Gtk/CustomControls/DateComboBox.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
using System;
using System.Threading;
using Eto.Forms;
using Eto.Drawing;

using System.Globalization;
using Eto.GtkSharp.Forms;

namespace Eto.GtkSharp.CustomControls
{
public class DateComboBox : BaseComboBox
{
DateTime? selectedDate;
private DateComboBoxDialog dlg;
DateTimePickerMode mode = DateTimePickerMode.DateTime;

public event EventHandler DateChanged;
public event EventHandler DialogClosed;

protected void OnDateChanged(EventArgs e)
{
Expand All @@ -28,6 +33,7 @@ public DateTimePickerMode Mode
}
}


public Color ErrorColor { get; set; }

public Color NormalColor { get; set; }
Expand Down Expand Up @@ -73,29 +79,84 @@ void SetValue()
}
}


public DateComboBox()
{


MinDate = DateTime.MinValue;
MaxDate = DateTime.MaxValue;

ErrorColor = Colors.Red;
NormalColor = Entry.GetTextColor();

Entry.Changed += HandleChanged;
Entry.FocusOutEvent += Entry_FocusOutEvent;

// called on button press..
PopupButtonClicked += delegate
{
var dlg = new DateComboBoxDialog(selectedDate ?? DateTime.Now, this.Mode);
dlg.DateChanged += delegate
{
// if we are still up somehow, take the old dialog down..
if (dlg != null)
{
selectedDate = dlg.SelectedDate;
ValidateDateRange();
SetValue();
};
dlg.ShowPopup(this);
// this will appear to the user as a click that doesn't
// bring up a window..
dlg.Close();
dlg = null;
}
else
{
// move the focus to the Entry control so we can detect it moving away..
Entry.GrabFocusWithoutSelecting();
dlg = new DateComboBoxDialog(selectedDate ?? DateTime.Now, this.Mode);
dlg.DateChanged += delegate
{
selectedDate = dlg.SelectedDate;
ValidateDateRange();
SetValue();
};
dlg.Destroyed += Dlg_Destroyed;
dlg.ShowPopup(this);
}
};
}

/// <summary>
/// remove our reference to the dialog ONLY when we know it's
/// really gone..
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Dlg_Destroyed(object sender, EventArgs e)
{
dlg = null;
}

/// <summary>
/// If the drop down dialog is up and lose focus on the parent field, close it
/// </summary>
/// <param name="o"></param>
/// <param name="args"></param>
private void Entry_FocusOutEvent(object o, Gtk.FocusOutEventArgs args)
{
// if the pull-down is are up, close it
if (dlg != null)
{
dlg.CloseDialog();
}
}

/// <summary>
/// The drop-down dialog has closed - remove our reference to it
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void HandleDialogClose(object sender, EventArgs e)
{
// remove our reference to the drop-down dialog
dlg = null;
}

void HandleChanged(object sender, EventArgs e)
{
var isValid = IsDateValid();
Expand Down
22 changes: 15 additions & 7 deletions src/Eto.Gtk/CustomControls/DateComboBoxDialog.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;
using Eto.Forms;

namespace Eto.GtkSharp.CustomControls
Expand All @@ -14,6 +15,7 @@ public class DateComboBoxDialog : Gtk.Window

public event EventHandler<EventArgs> DateChanged;


protected virtual void OnDateChanged (EventArgs e)
{
if (DateChanged != null)
Expand Down Expand Up @@ -50,6 +52,7 @@ public DateComboBoxDialog (DateTime dateTime, DateTimePickerMode mode)
{
this.mode = mode;
this.CreateControls ();


if (HasDate) {
calendar.Date = dateTime;
Expand All @@ -61,23 +64,28 @@ public DateComboBoxDialog (DateTime dateTime, DateTimePickerMode mode)
UpdateClock ();
}


this.ButtonPressEvent += delegate(object o, Gtk.ButtonPressEventArgs args) {

if (args.Event.Type == Gdk.EventType.ButtonPress) {
// single click only!
CloseDialog ();
CloseDialog();
}
};


}

public void ShowPopup (Gtk.Widget parent)
{
int x, y;


parent.ParentWindow.GetOrigin (out x, out y);
Move(x + parent.Allocation.Left, y + parent.Allocation.Top + parent.Allocation.Height);

ShowAll();
this.Grab ();

//if (! this.HasGrab) this.Grab ();
}

#if GTK2
Expand All @@ -100,11 +108,11 @@ protected override bool OnDrawn (Cairo.Context cr)
}
#endif

void CloseDialog ()
public void CloseDialog ()
{
this.RemoveGrab ();
#if GTKCORE
Close();
Close();

#else
Destroy();
#endif
Expand Down