[tomboy] [Windows] Enable proper popup behavior on Windows (fixes bugs #574627, #595848)



commit 6b1c1183679faaa852f4ee38a0771f6f99051318
Author: Cory Thomas <cthomas altep com>
Date:   Thu Aug 12 12:41:40 2010 +0200

    [Windows] Enable proper popup behavior on Windows (fixes bugs #574627, #595848)
    
    Using SetForegroundWindow to force Tomboy to the front whenever a
    popup menu is opened, solving the problem that clicks on non-gtk
    applications (Desktop, any other Windows application) didn't de-
    activate the menu and the missing keyboard focus for menus.

 Tomboy/Utils.cs |   45 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 45 insertions(+), 0 deletions(-)
---
diff --git a/Tomboy/Utils.cs b/Tomboy/Utils.cs
index 52a9c6d..026df23 100644
--- a/Tomboy/Utils.cs
+++ b/Tomboy/Utils.cs
@@ -2,6 +2,7 @@
 using System;
 using System.Collections;
 using System.Collections.Generic;
+using System.Diagnostics;
 using System.Runtime.InteropServices;
 using System.Text;
 using System.Threading;
@@ -86,8 +87,52 @@ namespace Tomboy
 			// Highlight the parent
 			if (menu.AttachWidget != null)
 				menu.AttachWidget.State = Gtk.StateType.Selected;
+
+#if WIN32
+			BringToForeground ();
+#endif
+		}
+
+		public static void BringToForeground () {
+			try {
+				Process current_proc = Process.GetCurrentProcess ();
+				int current_proc_id = current_proc.Id;
+				bool set_foreground_window = true;
+				IntPtr window_handle = GetForegroundWindow ();
+
+				if (window_handle != IntPtr.Zero) {
+					set_foreground_window = false;
+
+					int window_handle_proc_id;
+					GetWindowThreadProcessId (window_handle, out window_handle_proc_id);
+
+					if (window_handle_proc_id != current_proc_id) {
+						set_foreground_window = true;
+					}
+				}
+
+				if (set_foreground_window) {
+					window_handle = current_proc.MainWindowHandle;
+
+					if (window_handle != IntPtr.Zero) {
+						SetForegroundWindow (window_handle);
+					}
+				}
+			} catch (Exception e) {
+				Logger.Error("Error pulling Tomboy to foreground: {0}", e);
+			}
 		}
 
+		[DllImport ("user32.dll", SetLastError = true)]
+		static extern uint GetWindowThreadProcessId (IntPtr hWnd, out int lpdwProcessId);
+
+		[DllImport ("user32.dll")]
+		static extern IntPtr GetForegroundWindow ();
+
+		[DllImport ("user32.dll")]
+		[return: MarshalAs (UnmanagedType.Bool)]
+		static extern bool SetForegroundWindow (IntPtr hWnd);
+
 		public static Gdk.Pixbuf GetIcon (string resource_name, int size)
 		{
 			return GetIcon (null, resource_name, size);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]