2001-09-16 23:15:56 +00:00
|
|
|
// GTK.Application.cs - GTK Main Event Loop class implementation
|
|
|
|
//
|
|
|
|
// Author: Mike Kestner <mkestner@speakeasy.net>
|
|
|
|
//
|
2004-06-25 18:42:19 +00:00
|
|
|
// Copyright (c) 2001 Mike Kestner
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of version 2 of the Lesser GNU General
|
|
|
|
// Public License as published by the Free Software Foundation.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this program; if not, write to the
|
|
|
|
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
// Boston, MA 02111-1307, USA.
|
2001-09-16 23:15:56 +00:00
|
|
|
|
2001-09-19 02:04:57 +00:00
|
|
|
namespace Gtk {
|
2001-09-16 23:15:56 +00:00
|
|
|
|
|
|
|
using System;
|
2009-05-12 23:05:37 +00:00
|
|
|
using System.Reflection;
|
2001-09-16 23:15:56 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2012-11-08 21:26:09 +00:00
|
|
|
using System.Threading;
|
2003-03-15 20:49:37 +00:00
|
|
|
using Gdk;
|
2001-09-16 23:15:56 +00:00
|
|
|
|
2011-02-09 04:15:37 +00:00
|
|
|
public partial class Application {
|
2009-09-11 17:19:46 +00:00
|
|
|
|
|
|
|
const int WS_EX_TOOLWINDOW = 0x00000080;
|
|
|
|
const int WS_OVERLAPPEDWINDOW = 0x00CF0000;
|
|
|
|
|
|
|
|
[DllImport ("user32.dll", EntryPoint="CreateWindowExW", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
|
|
|
|
static extern IntPtr Win32CreateWindow (int dwExStyle, string lpClassName, string lpWindowName,int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lParam);
|
|
|
|
|
|
|
|
[DllImport ("user32.dll", EntryPoint="DestroyWindow", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)]
|
|
|
|
static extern bool Win32DestroyWindow (IntPtr window);
|
|
|
|
|
2009-05-05 15:15:46 +00:00
|
|
|
static Application ()
|
|
|
|
{
|
|
|
|
if (!GLib.Thread.Supported)
|
|
|
|
GLib.Thread.Init ();
|
2009-05-12 23:05:37 +00:00
|
|
|
|
|
|
|
switch (Environment.OSVersion.Platform) {
|
|
|
|
case PlatformID.Win32NT:
|
|
|
|
case PlatformID.Win32S:
|
|
|
|
case PlatformID.Win32Windows:
|
|
|
|
case PlatformID.WinCE:
|
2009-09-11 17:19:46 +00:00
|
|
|
// No idea why we need to create that window, but it enables visual styles on the Windows platform
|
|
|
|
IntPtr window = Win32CreateWindow (WS_EX_TOOLWINDOW, "static", "gtk-sharp visual styles window", WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
|
|
|
|
Win32DestroyWindow (window);
|
2009-05-12 23:05:37 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-05-05 15:15:46 +00:00
|
|
|
}
|
2009-09-11 17:19:46 +00:00
|
|
|
|
2011-02-09 22:54:12 +00:00
|
|
|
[DllImport ("libgtk-win32-3.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
|
2004-01-19 03:24:25 +00:00
|
|
|
static extern void gtk_init (ref int argc, ref IntPtr argv);
|
|
|
|
|
2011-02-09 22:54:12 +00:00
|
|
|
[DllImport ("libgtk-win32-3.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
|
2004-01-19 03:24:25 +00:00
|
|
|
static extern bool gtk_init_check (ref int argc, ref IntPtr argv);
|
2002-03-26 01:29:43 +00:00
|
|
|
|
2007-03-05 22:33:19 +00:00
|
|
|
static void SetPrgname ()
|
|
|
|
{
|
2013-11-02 20:53:20 +00:00
|
|
|
var args = Environment.GetCommandLineArgs ();
|
|
|
|
if (args != null && args.Length > 0)
|
|
|
|
GLib.Global.ProgramName = System.IO.Path.GetFileNameWithoutExtension (args [0]);
|
2007-03-05 22:33:19 +00:00
|
|
|
}
|
|
|
|
|
2002-03-26 01:29:43 +00:00
|
|
|
public static void Init ()
|
|
|
|
{
|
2007-03-05 22:33:19 +00:00
|
|
|
SetPrgname ();
|
2004-01-19 03:24:25 +00:00
|
|
|
IntPtr argv = new IntPtr(0);
|
|
|
|
int argc = 0;
|
|
|
|
|
|
|
|
gtk_init (ref argc, ref argv);
|
2012-11-08 21:26:09 +00:00
|
|
|
|
|
|
|
SynchronizationContext.SetSynchronizationContext (new GLib.GLibSynchronizationContext ());
|
2002-03-26 01:29:43 +00:00
|
|
|
}
|
|
|
|
|
2004-01-19 03:24:25 +00:00
|
|
|
static bool do_init (string progname, ref string[] args, bool check)
|
|
|
|
{
|
2007-03-05 22:33:19 +00:00
|
|
|
SetPrgname ();
|
2004-01-19 03:24:25 +00:00
|
|
|
bool res = false;
|
|
|
|
string[] progargs = new string[args.Length + 1];
|
|
|
|
|
|
|
|
progargs[0] = progname;
|
|
|
|
args.CopyTo (progargs, 1);
|
|
|
|
|
2004-12-21 19:47:55 +00:00
|
|
|
GLib.Argv argv = new GLib.Argv (progargs);
|
|
|
|
IntPtr buf = argv.Handle;
|
2004-01-19 03:24:25 +00:00
|
|
|
int argc = progargs.Length;
|
|
|
|
|
|
|
|
if (check)
|
|
|
|
res = gtk_init_check (ref argc, ref buf);
|
|
|
|
else
|
|
|
|
gtk_init (ref argc, ref buf);
|
|
|
|
|
2004-12-21 19:47:55 +00:00
|
|
|
if (buf != argv.Handle)
|
|
|
|
throw new Exception ("init returned new argv handle");
|
|
|
|
|
2004-01-19 03:24:25 +00:00
|
|
|
// copy back the resulting argv, minus argv[0], which we're
|
|
|
|
// not interested in.
|
|
|
|
|
2004-12-21 19:47:55 +00:00
|
|
|
if (argc <= 1)
|
2004-01-19 03:24:25 +00:00
|
|
|
args = new string[0];
|
|
|
|
else {
|
2004-12-21 19:47:55 +00:00
|
|
|
progargs = argv.GetArgs (argc);
|
2004-01-19 03:24:25 +00:00
|
|
|
args = new string[argc - 1];
|
|
|
|
Array.Copy (progargs, 1, args, 0, argc - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2001-09-16 23:15:56 +00:00
|
|
|
|
2004-01-19 03:24:25 +00:00
|
|
|
public static void Init (string progname, ref string[] args)
|
2001-09-16 23:15:56 +00:00
|
|
|
{
|
2004-01-19 03:24:25 +00:00
|
|
|
do_init (progname, ref args, false);
|
2001-09-16 23:15:56 +00:00
|
|
|
}
|
|
|
|
|
2004-01-19 03:24:25 +00:00
|
|
|
public static bool InitCheck (string progname, ref string[] args)
|
2003-03-11 02:26:25 +00:00
|
|
|
{
|
2004-01-19 03:24:25 +00:00
|
|
|
return do_init (progname, ref args, true);
|
2003-03-11 02:26:25 +00:00
|
|
|
}
|
|
|
|
|
2011-02-09 22:54:12 +00:00
|
|
|
[DllImport ("libgtk-win32-3.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
|
2002-06-23 18:49:33 +00:00
|
|
|
static extern void gtk_main ();
|
|
|
|
|
2001-09-16 23:15:56 +00:00
|
|
|
public static void Run ()
|
|
|
|
{
|
|
|
|
gtk_main ();
|
|
|
|
}
|
|
|
|
|
2011-02-09 22:54:12 +00:00
|
|
|
[DllImport ("libgtk-win32-3.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
|
2002-08-31 20:37:52 +00:00
|
|
|
static extern bool gtk_events_pending ();
|
|
|
|
|
|
|
|
|
|
|
|
public static bool EventsPending ()
|
|
|
|
{
|
|
|
|
return gtk_events_pending ();
|
|
|
|
}
|
|
|
|
|
2011-02-09 22:54:12 +00:00
|
|
|
[DllImport ("libgtk-win32-3.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
|
2002-08-31 20:37:52 +00:00
|
|
|
static extern void gtk_main_iteration ();
|
|
|
|
|
2011-02-09 22:54:12 +00:00
|
|
|
[DllImport ("libgtk-win32-3.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
|
2003-03-11 02:26:25 +00:00
|
|
|
static extern bool gtk_main_iteration_do (bool blocking);
|
2002-08-31 20:37:52 +00:00
|
|
|
|
|
|
|
public static void RunIteration ()
|
|
|
|
{
|
|
|
|
gtk_main_iteration ();
|
|
|
|
}
|
|
|
|
|
2003-03-11 02:26:25 +00:00
|
|
|
public static bool RunIteration (bool blocking)
|
|
|
|
{
|
|
|
|
return gtk_main_iteration_do (blocking);
|
|
|
|
}
|
|
|
|
|
2011-02-09 22:54:12 +00:00
|
|
|
[DllImport ("libgtk-win32-3.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
|
2002-06-23 18:49:33 +00:00
|
|
|
static extern void gtk_main_quit ();
|
2001-09-16 23:15:56 +00:00
|
|
|
|
|
|
|
public static void Quit ()
|
|
|
|
{
|
|
|
|
gtk_main_quit ();
|
|
|
|
}
|
2003-03-11 02:26:25 +00:00
|
|
|
|
|
|
|
|
2011-02-09 22:54:12 +00:00
|
|
|
[DllImport ("libgtk-win32-3.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
|
2003-03-11 02:26:25 +00:00
|
|
|
static extern IntPtr gtk_get_current_event ();
|
2003-03-15 20:49:37 +00:00
|
|
|
|
2004-02-20 22:33:32 +00:00
|
|
|
public static Gdk.Event CurrentEvent {
|
2003-03-15 20:49:37 +00:00
|
|
|
get {
|
2008-05-21 15:52:04 +00:00
|
|
|
return Gdk.Event.GetEvent (gtk_get_current_event ());
|
2003-03-15 20:49:37 +00:00
|
|
|
}
|
|
|
|
}
|
2005-09-02 22:39:17 +00:00
|
|
|
|
|
|
|
internal class InvokeCB {
|
|
|
|
EventHandler d;
|
|
|
|
object sender;
|
|
|
|
EventArgs args;
|
|
|
|
|
|
|
|
internal InvokeCB (EventHandler d)
|
|
|
|
{
|
|
|
|
this.d = d;
|
|
|
|
args = EventArgs.Empty;
|
|
|
|
sender = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal InvokeCB (EventHandler d, object sender, EventArgs args)
|
|
|
|
{
|
|
|
|
this.d = d;
|
|
|
|
this.args = args;
|
|
|
|
this.sender = sender;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal bool Invoke ()
|
|
|
|
{
|
|
|
|
d (sender, args);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Invoke (EventHandler d)
|
|
|
|
{
|
|
|
|
InvokeCB icb = new InvokeCB (d);
|
|
|
|
|
2005-10-18 02:46:31 +00:00
|
|
|
GLib.Timeout.Add (0, new GLib.TimeoutHandler (icb.Invoke));
|
2005-09-02 22:39:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void Invoke (object sender, EventArgs args, EventHandler d)
|
|
|
|
{
|
2005-09-03 04:14:10 +00:00
|
|
|
InvokeCB icb = new InvokeCB (d, sender, args);
|
2005-09-02 22:39:17 +00:00
|
|
|
|
2005-10-18 02:46:31 +00:00
|
|
|
GLib.Timeout.Add (0, new GLib.TimeoutHandler (icb.Invoke));
|
2005-09-02 22:39:17 +00:00
|
|
|
}
|
2001-09-16 23:15:56 +00:00
|
|
|
}
|
|
|
|
}
|