Skip to content

Commit

Permalink
Gtk: Ensure handler is not null in connector before using it
Browse files Browse the repository at this point in the history
  • Loading branch information
cwensley committed Apr 24, 2022
1 parent 68b4d17 commit 136faa5
Show file tree
Hide file tree
Showing 39 changed files with 325 additions and 172 deletions.
2 changes: 1 addition & 1 deletion src/Eto.Gtk/Forms/Cells/CellHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ protected class CellConnector : WeakConnector

public void HandleEditingStarted(object o, Gtk.EditingStartedArgs args)
{
Handler.Source.BeginCellEditing(new Gtk.TreePath(args.Path), Handler.ColumnIndex);
Handler?.Source.BeginCellEditing(new Gtk.TreePath(args.Path), Handler.ColumnIndex);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Eto.Gtk/Forms/Cells/CheckBoxCellHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ protected class CheckBoxCellEventConnector : CellConnector

public void HandleToggled(object o, Gtk.ToggledArgs args)
{
Handler.SetValue(args.Path, !Handler.Control.Active);
Handler?.SetValue(args.Path, !Handler.Control.Active);
}

public void HandleEndCellEditing(object o, Gtk.ToggledArgs args)
{
Handler.Source.EndCellEditing(new Gtk.TreePath(args.Path), Handler.ColumnIndex);
Handler?.Source.EndCellEditing(new Gtk.TreePath(args.Path), Handler.ColumnIndex);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Eto.Gtk/Forms/Cells/ComboBoxCellHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ protected class ComboBoxCellEventConnector : CellConnector

public void HandleEdited(object o, Gtk.EditedArgs args)
{
Handler.SetValue(args.Path, args.NewText);
Handler?.SetValue(args.Path, args.NewText);
}

public void HandleEndEditing(object o, Gtk.EditedArgs args)
{
Handler.Source.EndCellEditing(new Gtk.TreePath(args.Path), Handler.ColumnIndex);
Handler?.Source.EndCellEditing(new Gtk.TreePath(args.Path), Handler.ColumnIndex);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Eto.Gtk/Forms/Cells/ImageTextCellHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ protected class ImageTextCellEventConnector : CellConnector

public void HandleEdited(object o, Gtk.EditedArgs args)
{
Handler.SetValue(args.Path, args.NewText);
Handler?.SetValue(args.Path, args.NewText);
}

public void HandleEndCellEditing(object o, Gtk.EditedArgs args)
{
Handler.Source.EndCellEditing(new Gtk.TreePath(args.Path), Handler.ColumnIndex);
Handler?.Source.EndCellEditing(new Gtk.TreePath(args.Path), Handler.ColumnIndex);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Eto.Gtk/Forms/Cells/TextBoxCellHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ protected class TextBoxCellEventConnector : CellConnector

public void HandleEdited(object o, Gtk.EditedArgs args)
{
Handler.SetValue(args.Path, args.NewText);
Handler?.SetValue(args.Path, args.NewText);
}

public void HandleEndEditing(object o, Gtk.EditedArgs args)
{
Handler.Source.EndCellEditing(new Gtk.TreePath(args.Path), Handler.ColumnIndex);
Handler?.Source.EndCellEditing(new Gtk.TreePath(args.Path), Handler.ColumnIndex);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/Eto.Gtk/Forms/Controls/CheckBoxHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ protected class CheckBoxConnector : GtkControlConnector
public void HandleToggled(object sender, EventArgs e)
{
var h = Handler;
if (h == null)
return;
var c = h.Control;
if (toggling)
return;
Expand Down
2 changes: 1 addition & 1 deletion src/Eto.Gtk/Forms/Controls/ColorPickerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected class ColorPickerConnector : GtkControlConnector

public void HandleSelectedColorChanged(object sender, EventArgs e)
{
Handler.Callback.OnColorChanged(Handler.Widget, EventArgs.Empty);
Handler?.Callback.OnColorChanged(Handler.Widget, EventArgs.Empty);
}
}
#if GTK3
Expand Down
2 changes: 1 addition & 1 deletion src/Eto.Gtk/Forms/Controls/DateTimePickerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected class DateTimePickerConnector : GtkControlConnector

public void HandleDateChanged(object sender, EventArgs e)
{
Handler.Callback.OnValueChanged(Handler.Widget, EventArgs.Empty);
Handler?.Callback.OnValueChanged(Handler.Widget, EventArgs.Empty);
}
}

Expand Down
21 changes: 13 additions & 8 deletions src/Eto.Gtk/Forms/Controls/DrawableHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ protected class DrawableConnector : GtkPanelEventConnector

public void HandleDrawableButtonPressEvent(object o, Gtk.ButtonPressEventArgs args)
{
if (Handler.CanFocus)
Handler.Control.GrabFocus();
var handler = Handler;
if (handler == null)
return;
if (handler.CanFocus)
handler.Control.GrabFocus();
}

#if GTK2
Expand All @@ -82,21 +85,23 @@ public void HandleExpose(object o, Gtk.ExposeEventArgs args)
[GLib.ConnectBefore]
public void HandleDrawn(object o, Gtk.DrawnArgs args)
{
var h = Handler;
var handler = Handler;
if (handler == null)
return;

var allocation = h.Control.Allocation.Size;
var allocation = handler.Control.Allocation.Size;
args.Cr.Rectangle(new Cairo.Rectangle(0, 0, allocation.Width, allocation.Height));
args.Cr.Clip();
Gdk.Rectangle rect = new Gdk.Rectangle();
if (!GraphicsHandler.GetClipRectangle(args.Cr, ref rect))
rect = new Gdk.Rectangle(Gdk.Point.Zero, allocation);

using (var graphics = new Graphics(new GraphicsHandler(args.Cr, h.Control.PangoContext, false)))
using (var graphics = new Graphics(new GraphicsHandler(args.Cr, handler.Control.PangoContext, false)))
{
if (h.SelectedBackgroundColor != null)
graphics.Clear(h.SelectedBackgroundColor.Value);
if (handler.SelectedBackgroundColor != null)
graphics.Clear(handler.SelectedBackgroundColor.Value);

h.Callback.OnPaint(h.Widget, new PaintEventArgs(graphics, rect.ToEto()));
handler.Callback.OnPaint(handler.Widget, new PaintEventArgs(graphics, rect.ToEto()));
}
}
#endif
Expand Down
24 changes: 16 additions & 8 deletions src/Eto.Gtk/Forms/Controls/DropDownHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,34 +67,42 @@ protected class DropDownConnector : GtkControlConnector

public virtual void HandleChanged(object sender, EventArgs e)
{
if (Handler.SuppressIndexChanged > 0)
var handler = Handler;
if (handler == null)
return;
var newIndex = Handler.SelectedIndex;

if (handler.SuppressIndexChanged > 0)
return;
var newIndex = handler.SelectedIndex;
if (newIndex != lastIndex)
{
Handler.Callback.OnSelectedIndexChanged(Handler.Widget, EventArgs.Empty);
handler.Callback.OnSelectedIndexChanged(handler.Widget, EventArgs.Empty);
lastIndex = newIndex;
}
}

#if GTK2
internal void HandlePopupShownChanged(object o, GLib.NotifyArgs args)
{
if (Handler.Control.PopupShown)
Handler.Callback.OnDropDownOpening(Handler.Widget, EventArgs.Empty);
var handler = Handler;
if (handler == null)
return;

if (handler.Control.PopupShown)
handler.Callback.OnDropDownOpening(handler.Widget, EventArgs.Empty);
else
Handler.Callback.OnDropDownClosed(Handler.Widget, EventArgs.Empty);
handler.Callback.OnDropDownClosed(handler.Widget, EventArgs.Empty);
}
#elif GTK3
[GLib.ConnectBefore]
public virtual void HandlePoppedUp(object sender, EventArgs e)
{
Handler.Callback.OnDropDownOpening(Handler.Widget, EventArgs.Empty);
Handler?.Callback.OnDropDownOpening(Handler.Widget, EventArgs.Empty);
}

public virtual void HandlePoppedDown(object o, Gtk.PoppedDownArgs args)
{
Handler.Callback.OnDropDownClosed(Handler.Widget, EventArgs.Empty);
Handler?.Callback.OnDropDownClosed(Handler.Widget, EventArgs.Empty);
}
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion src/Eto.Gtk/Forms/Controls/ExpanderHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected class ExpanderConnector : GtkPanelEventConnector

public void HandleActivated(object sender, EventArgs e)
{
Handler.Callback.OnExpandedChanged(Handler.Widget, EventArgs.Empty);
Handler?.Callback.OnExpandedChanged(Handler.Widget, EventArgs.Empty);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Eto.Gtk/Forms/Controls/FontPickerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected class FontPickerConnector : GtkControlConnector

public void HandleValueChanged(object sender, EventArgs e)
{
Handler.Callback.OnValueChanged(Handler.Widget, EventArgs.Empty);
Handler?.Callback.OnValueChanged(Handler.Widget, EventArgs.Empty);
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions src/Eto.Gtk/Forms/Controls/GridHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ public void HandleButtonPress(object o, Gtk.ButtonPressEventArgs args)
}

var handler = Handler;
if (handler == null)
return;

if (handler.contextMenu != null && args.Event.Button == 3 && args.Event.Type == Gdk.EventType.ButtonPress)
{
var menu = ((ContextMenuHandler)handler.contextMenu.Handler).Control;
Expand Down Expand Up @@ -199,12 +202,15 @@ static bool ArraysEqual<T>(T[] a1, T[] a2)

public void HandleGridSelectionChanged(object sender, EventArgs e)
{
if (!Handler.SkipSelectedChange)
var handler = Handler;
if (handler == null)
return;
if (!handler.SkipSelectedChange)
{
var selected = Handler.SelectedRows.ToArray();
var selected = handler.SelectedRows.ToArray();
if (!ArraysEqual(selectedRows, selected))
{
Handler.Callback.OnSelectionChanged(Handler.Widget, EventArgs.Empty);
handler.Callback.OnSelectionChanged(handler.Widget, EventArgs.Empty);
selectedRows = selected;
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/Eto.Gtk/Forms/Controls/ImageViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,15 @@ public void HandleExpose(object o, Gtk.ExposeEventArgs args)
{
Gdk.EventExpose ev = args.Event;
var h = Handler;
if (h == null)
return;
var handler = new GraphicsHandler(h.Control, ev.Window);
#else
public void HandleDrawn(object o, Gtk.DrawnArgs args)
{
var h = Handler;
if (h == null)
return;
var handler = new GraphicsHandler(args.Cr, h.Control.CreatePangoContext(), false);
#endif
if (h.image == null)
Expand Down
2 changes: 1 addition & 1 deletion src/Eto.Gtk/Forms/Controls/LinkButtonHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected class LinkButtonConnector : GtkControlConnector

public void HandleClicked(object sender, EventArgs e)
{
Handler.Callback.OnClick(Handler.Widget, EventArgs.Empty);
Handler?.Callback.OnClick(Handler.Widget, EventArgs.Empty);
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/Eto.Gtk/Forms/Controls/ListBoxHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,25 @@ protected class ListBoxEventConnector : GtkControlConnector
[GLib.ConnectBefore]
public void HandleTreeButtonPressEvent(object o, Gtk.ButtonPressEventArgs args)
{
if (Handler.contextMenu != null && args.Event.Button == 3 && args.Event.Type == Gdk.EventType.ButtonPress)
var handler = Handler;
if (handler == null)
return;
if (handler.contextMenu != null && args.Event.Button == 3 && args.Event.Type == Gdk.EventType.ButtonPress)
{
var menu = (Gtk.Menu)Handler.contextMenu.ControlObject;
var menu = (Gtk.Menu)handler.contextMenu.ControlObject;
menu.Popup();
menu.ShowAll();
}
}

public void HandleSelectionChanged(object sender, EventArgs e)
{
Handler.Callback.OnSelectedIndexChanged(Handler.Widget, EventArgs.Empty);
Handler?.Callback.OnSelectedIndexChanged(Handler.Widget, EventArgs.Empty);
}

public void HandleTreeRowActivated(object o, Gtk.RowActivatedArgs args)
{
Handler.Callback.OnActivated(Handler.Widget, EventArgs.Empty);
Handler?.Callback.OnActivated(Handler.Widget, EventArgs.Empty);
}
}

Expand Down
41 changes: 24 additions & 17 deletions src/Eto.Gtk/Forms/Controls/NumericStepperHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@ protected class NumericStepperConnector : GtkControlConnector

public void HandleValueChanged(object sender, EventArgs e)
{
if (Handler.SuppressValueChanged <= 0)
var handler = Handler;
if (handler == null)
return;
if (handler.SuppressValueChanged <= 0)
{
Handler.UpdateRequiredDigits();
Handler.Callback.OnValueChanged(Handler.Widget, EventArgs.Empty);
handler.UpdateRequiredDigits();
handler.Callback.OnValueChanged(handler.Widget, EventArgs.Empty);
}
}

Expand All @@ -67,38 +70,42 @@ public void HandleValueChanged(object sender, EventArgs e)
[GLib.ConnectBefore]
public void HandleInput(object o, InputArgs args)
{
var h = Handler;
if (h.NeedsFormat)
var handler = Handler;
if (handler == null)
return;
if (handler.NeedsFormat)
{
var text = h.Text;
if (h.HasFormatString)
text = Regex.Replace(text, $@"(?!\d|{Regex.Escape(h.CultureInfo.NumberFormat.NumberDecimalSeparator)}|{Regex.Escape(h.CultureInfo.NumberFormat.NegativeSign)}).", ""); // strip any non-numeric value
var text = handler.Text;
if (handler.HasFormatString)
text = Regex.Replace(text, $@"(?!\d|{Regex.Escape(handler.CultureInfo.NumberFormat.NumberDecimalSeparator)}|{Regex.Escape(handler.CultureInfo.NumberFormat.NegativeSign)}).", ""); // strip any non-numeric value

double result;
if (double.TryParse(text, NumberStyles.Any, h.CultureInfo, out result))
if (double.TryParse(text, NumberStyles.Any, handler.CultureInfo, out result))
{
if (h.HasFormatString && result > 0 && NumberStringsMatch((-result).ToString(h.FormatString, h.CultureInfo), h.Text))
if (handler.HasFormatString && result > 0 && NumberStringsMatch((-result).ToString(handler.FormatString, handler.CultureInfo), handler.Text))
result = -result;

args.NewValue = result;
args.RetVal = 1;
return;
}
}
args.NewValue = h.Control.Adjustment.Value;
args.NewValue = handler.Control.Adjustment.Value;
args.RetVal = 0;
}

[GLib.ConnectBefore]
public void HandleOutput(object o, OutputArgs args)
{
var h = Handler;
if (h.NeedsFormat)
var handler = Handler;
if (handler == null)
return;
if (handler.NeedsFormat)
{
var val = h.Control.Adjustment.Value;
var format = h.CurrentFormatString;
var text = format == null ? val.ToString(h.CultureInfo) : val.ToString(format, h.CultureInfo);
h.Control.Text = text;
var val = handler.Control.Adjustment.Value;
var format = handler.CurrentFormatString;
var text = format == null ? val.ToString(handler.CultureInfo) : val.ToString(format, handler.CultureInfo);
handler.Control.Text = text;
args.RetVal = 1;
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Eto.Gtk/Forms/Controls/PasswordBoxHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected class PasswordBoxConnector : GtkControlConnector

public void HandleTextChanged(object sender, EventArgs e)
{
Handler.Callback.OnTextChanged(Handler.Widget, EventArgs.Empty);
Handler?.Callback.OnTextChanged(Handler.Widget, EventArgs.Empty);
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/Eto.Gtk/Forms/Controls/RadioButtonHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ protected class RadioButtonConnector : GtkControlConnector
{
public new RadioButtonHandler Handler { get { return (RadioButtonHandler)base.Handler; } }

public void HandleCheckedChanged(object sender, EventArgs e)
{
Handler.Callback.OnCheckedChanged(Handler.Widget, EventArgs.Empty);
}
public void HandleCheckedChanged(object sender, EventArgs e) => Handler?.Callback.OnCheckedChanged(Handler.Widget, EventArgs.Empty);

internal void Control_Realized(object sender, EventArgs e) => Handler?.Control_Realized(sender, e);
}
Expand Down
Loading

0 comments on commit 136faa5

Please sign in to comment.