diff --git a/SparkleShare/Windows/SparkleNotifyIcon.cs b/SparkleShare/Windows/SparkleNotifyIcon.cs index 5c368a42..7b3c6bed 100644 --- a/SparkleShare/Windows/SparkleNotifyIcon.cs +++ b/SparkleShare/Windows/SparkleNotifyIcon.cs @@ -30,9 +30,9 @@ using Forms = System.Windows.Forms; namespace SparkleShare { - [ContentProperty("Text")] + [ContentProperty("Text")] [DefaultEvent("MouseDoubleClick")] - public class SparkleNotifyIcon : FrameworkElement, IAddChild { + public class SparkleNotifyIcon : FrameworkElement, IAddChild { [DllImport("kernel32.dll", CharSet = CharSet.Auto)] private static extern IntPtr GetModuleHandle(string module_name); @@ -45,7 +45,7 @@ namespace SparkleShare { [DllImport("user32.dll", EntryPoint = "DestroyIcon")] static extern bool DestroyIcon (IntPtr hIcon); - + [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)] private static extern int UnhookWindowsHookEx (int hook_id); @@ -60,12 +60,12 @@ namespace SparkleShare { internal int Info; } - + public Drawing.Bitmap Icon { set { - this.notify_icon.Icon = GetIconFromBitmap (value); + this.notify_icon.Icon = GetIconFromBitmap (value); } - } + } public string HeaderText { get { @@ -73,21 +73,21 @@ namespace SparkleShare { } set { - header_text = value; + header_text = value; } } - + public string Text { get { return (string) GetValue (TextProperty); } set { - string text = value; - - if (!string.IsNullOrEmpty (header_text)) - text = header_text + "\n" + text; - + string text = value; + + if (!string.IsNullOrEmpty (header_text)) + text = header_text + "\n" + text; + SetValue (TextProperty, text); } } @@ -114,27 +114,27 @@ namespace SparkleShare { public readonly RoutedEvent MouseClickEvent = EventManager.RegisterRoutedEvent ( - "MouseClick", RoutingStrategy.Bubble, typeof (MouseButtonEventHandler), typeof (SparkleNotifyIcon)); + "MouseClick", RoutingStrategy.Bubble, typeof (MouseButtonEventHandler), typeof (SparkleNotifyIcon)); - public readonly RoutedEvent MouseDoubleClickEvent = EventManager.RegisterRoutedEvent( - "MouseDoubleClick", RoutingStrategy.Bubble, typeof (MouseButtonEventHandler), typeof (SparkleNotifyIcon)); + public readonly RoutedEvent MouseDoubleClickEvent = EventManager.RegisterRoutedEvent( + "MouseDoubleClick", RoutingStrategy.Bubble, typeof (MouseButtonEventHandler), typeof (SparkleNotifyIcon)); - public readonly DependencyProperty TextProperty = DependencyProperty.Register( - "Text", typeof(string), typeof (SparkleNotifyIcon), new PropertyMetadata (OnTextChanged)); + public readonly DependencyProperty TextProperty = DependencyProperty.Register( + "Text", typeof(string), typeof (SparkleNotifyIcon), new PropertyMetadata (OnTextChanged)); - private string header_text; + private string header_text; private Forms.NotifyIcon notify_icon; - private HookProc hook_proc_ref; - private int mouse_hook_handle; + private HookProc hook_proc_ref; + private int mouse_hook_handle; private delegate int HookProc (int code, int param, IntPtr struct_pointer); - - - public SparkleNotifyIcon () - { - VisibilityProperty.OverrideMetadata (typeof (SparkleNotifyIcon), - new PropertyMetadata (OnVisibilityChanged)); - + + + public SparkleNotifyIcon () + { + VisibilityProperty.OverrideMetadata (typeof (SparkleNotifyIcon), + new PropertyMetadata (OnVisibilityChanged)); + this.notify_icon = new Forms.NotifyIcon () { Text = Text, Visible = true @@ -146,17 +146,17 @@ namespace SparkleShare { this.notify_icon.MouseDoubleClick += OnMouseDoubleClick; this.hook_proc_ref = OnMouseEventProc; - } - - - public void ShowBalloonTip (string title, string subtext, string image_path) - { + } + + + public void ShowBalloonTip (string title, string subtext, string image_path) + { // TODO: // - Use the image pointed to by image_path // - Find a way to use the prettier (Win7?) balloons this.notify_icon.ShowBalloonTip (5 * 1000, title, subtext, Forms.ToolTipIcon.Info); - } - + } + public void Dispose() { @@ -164,49 +164,49 @@ namespace SparkleShare { } - void IAddChild.AddChild (object value) - { - throw new InvalidOperationException (); - } + void IAddChild.AddChild (object value) + { + throw new InvalidOperationException (); + } - + void IAddChild.AddText (string text) - { - if (text == null) - throw new ArgumentNullException (); + { + if (text == null) + throw new ArgumentNullException (); - Text = text; - } + Text = text; + } - + private static MouseButtonEventArgs CreateMouseButtonEventArgs( - RoutedEvent handler, Forms.MouseButtons button) - { + RoutedEvent handler, Forms.MouseButtons button) + { MouseButton mouse_button; if (button == Forms.MouseButtons.Left) { mouse_button = MouseButton.Left; - - } else if (button == Forms.MouseButtons.Right) { - mouse_button = MouseButton.Right; - - } else if (button == Forms.MouseButtons.Middle) { - mouse_button = MouseButton.Middle; - - } else if (button == Forms.MouseButtons.XButton1) { - mouse_button = MouseButton.XButton1; - - } else if (button == Forms.MouseButtons.XButton2) { - mouse_button = MouseButton.XButton2; - - } else { - throw new InvalidOperationException (); - } + + } else if (button == Forms.MouseButtons.Right) { + mouse_button = MouseButton.Right; + + } else if (button == Forms.MouseButtons.Middle) { + mouse_button = MouseButton.Middle; + + } else if (button == Forms.MouseButtons.XButton1) { + mouse_button = MouseButton.XButton1; + + } else if (button == Forms.MouseButtons.XButton2) { + mouse_button = MouseButton.XButton2; + + } else { + throw new InvalidOperationException (); + } - return new MouseButtonEventArgs (InputManager.Current.PrimaryMouseDevice, 0, mouse_button) { - RoutedEvent = handler - }; - } + return new MouseButtonEventArgs (InputManager.Current.PrimaryMouseDevice, 0, mouse_button) { + RoutedEvent = handler + }; + } private void ShowContextMenu () @@ -214,60 +214,72 @@ namespace SparkleShare { if (ContextMenu != null) { ContextMenu.Opened += OnContextMenuOpened; ContextMenu.Closed += OnContextMenuClosed; - - ContextMenu.Placement = PlacementMode.Mouse; + + ContextMenu.Placement = PlacementMode.Mouse; ContextMenu.IsOpen = true; } } - + private Rect GetContextMenuRect (ContextMenu menu) { - Point start_point = menu.PointToScreen (new Point (0, 0)); - Point end_point = menu.PointToScreen (new Point (menu.ActualWidth, menu.ActualHeight)); + var source = PresentationSource.FromVisual (menu); + if (source != null) { + Point start_point = menu.PointToScreen(new Point(0, 0)); + Point end_point = menu.PointToScreen(new Point(menu.ActualWidth, menu.ActualHeight)); - return new Rect (start_point, end_point); + return new Rect(start_point, end_point); + } + + return new Rect(); } private Point GetHitPoint (IntPtr struct_pointer) { MouseLLHook mouse_hook = (MouseLLHook) Marshal.PtrToStructure ( - struct_pointer, typeof (MouseLLHook)); + struct_pointer, typeof (MouseLLHook)); - return new Point (mouse_hook.X, mouse_hook.Y); + return new Point (mouse_hook.X, mouse_hook.Y); } - - - private int OnMouseEventProc (int code, int button, IntPtr data_pointer) - { + + + private int OnMouseEventProc (int code, int button, IntPtr data_pointer) + { int left_button_down = 0x201; - int right_button_down = 0x204; + int right_button_down = 0x204; - if (button == left_button_down || button == right_button_down) { - Rect context_menu_rect = GetContextMenuRect (ContextMenu); - Point hit_point = GetHitPoint (data_pointer); + int ret; + try { + if (button == left_button_down || button == right_button_down) + { + Rect context_menu_rect = GetContextMenuRect(ContextMenu); + Point hit_point = GetHitPoint(data_pointer); - if (!context_menu_rect.Contains (hit_point)) - ContextMenu.IsOpen = false; - } + if (!context_menu_rect.Contains(hit_point)) + ContextMenu.IsOpen = false; + } + } + finally { + ret = CallNextHookEx(this.mouse_hook_handle, code, button, data_pointer); + } - return CallNextHookEx (this.mouse_hook_handle, code, button, data_pointer); - } - - - private void OnContextMenuOpened (object sender, RoutedEventArgs args) - { - using (Process process = Process.GetCurrentProcess ()) - using (ProcessModule module = process.MainModule) - { - this.mouse_hook_handle = SetWindowsHookEx (14, this.hook_proc_ref, - GetModuleHandle (module.ModuleName), 0); - } + return ret; + } + + + private void OnContextMenuOpened (object sender, RoutedEventArgs args) + { + using (Process process = Process.GetCurrentProcess ()) + using (ProcessModule module = process.MainModule) + { + this.mouse_hook_handle = SetWindowsHookEx (14, this.hook_proc_ref, + GetModuleHandle (module.ModuleName), 0); + } - if (this.mouse_hook_handle == 0) - throw new Win32Exception (Marshal.GetLastWin32Error ()); - } + if (this.mouse_hook_handle == 0) + throw new Win32Exception (Marshal.GetLastWin32Error ()); + } private void OnContextMenuClosed (object sender, RoutedEventArgs args) @@ -277,44 +289,44 @@ namespace SparkleShare { ContextMenu.Opened -= OnContextMenuOpened; ContextMenu.Closed -= OnContextMenuClosed; } - - + + private void OnVisibilityChanged (DependencyObject target, - DependencyPropertyChangedEventArgs args) + DependencyPropertyChangedEventArgs args) { SparkleNotifyIcon control = (SparkleNotifyIcon) target; control.notify_icon.Visible = (control.Visibility == Visibility.Visible); } - - - private void OnMouseDown(object sender, Forms.MouseEventArgs args) - { - RaiseEvent (CreateMouseButtonEventArgs (MouseDownEvent, args.Button)); - } - - - private void OnMouseClick (object sender, Forms.MouseEventArgs args) - { - RaiseEvent (CreateMouseButtonEventArgs (MouseClickEvent, args.Button)); - } - - + + + private void OnMouseDown(object sender, Forms.MouseEventArgs args) + { + RaiseEvent (CreateMouseButtonEventArgs (MouseDownEvent, args.Button)); + } + + + private void OnMouseClick (object sender, Forms.MouseEventArgs args) + { + RaiseEvent (CreateMouseButtonEventArgs (MouseClickEvent, args.Button)); + } + + private void OnMouseDoubleClick (object sender, Forms.MouseEventArgs args) { RaiseEvent (CreateMouseButtonEventArgs (MouseDoubleClickEvent, args.Button)); } - private void OnMouseUp (object sender, Forms.MouseEventArgs args) - { + private void OnMouseUp (object sender, Forms.MouseEventArgs args) + { if (args.Button == Forms.MouseButtons.Left || args.Button == Forms.MouseButtons.Right) { - + ShowContextMenu (); - } + } - RaiseEvent (CreateMouseButtonEventArgs (MouseUpEvent, args.Button)); - } + RaiseEvent (CreateMouseButtonEventArgs (MouseUpEvent, args.Button)); + } protected override void OnVisualParentChanged (DependencyObject parent) @@ -324,12 +336,12 @@ namespace SparkleShare { private static void OnTextChanged (DependencyObject target, - DependencyPropertyChangedEventArgs args) + DependencyPropertyChangedEventArgs args) { SparkleNotifyIcon control = (SparkleNotifyIcon) target; control.notify_icon.Text = control.Text; } - + private Drawing.Icon GetIconFromBitmap (Drawing.Bitmap bitmap) { @@ -339,5 +351,5 @@ namespace SparkleShare { return icon; } - } + } }