1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
// GTK.Application.cs - GTK Main Event Loop class implementation
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// 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.
namespace Gtk {
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using Gdk;
public partial class Application {
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);
static Application ()
{
if (!GLib.Thread.Supported)
GLib.Thread.Init ();
switch (Environment.OSVersion.Platform) {
case PlatformID.Win32NT:
case PlatformID.Win32S:
case PlatformID.Win32Windows:
case PlatformID.WinCE:
// 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);
break;
default:
break;
}
}
[DllImport (Global.GtkNativeDll, CallingConvention = CallingConvention.Cdecl)]
static extern void gtk_init (ref int argc, ref IntPtr argv);
[DllImport (Global.GtkNativeDll, CallingConvention = CallingConvention.Cdecl)]
static extern bool gtk_init_check (ref int argc, ref IntPtr argv);
static void SetPrgname ()
{
var args = Environment.GetCommandLineArgs ();
if (args != null && args.Length > 0)
GLib.Global.ProgramName = System.IO.Path.GetFileNameWithoutExtension (args [0]);
}
public static void Init ()
{
SetPrgname ();
IntPtr argv = new IntPtr(0);
int argc = 0;
gtk_init (ref argc, ref argv);
SynchronizationContext.SetSynchronizationContext (new GLib.GLibSynchronizationContext ());
}
static bool do_init (string progname, ref string[] args, bool check)
{
SetPrgname ();
bool res = false;
string[] progargs = new string[args.Length + 1];
progargs[0] = progname;
args.CopyTo (progargs, 1);
GLib.Argv argv = new GLib.Argv (progargs);
IntPtr buf = argv.Handle;
int argc = progargs.Length;
if (check)
res = gtk_init_check (ref argc, ref buf);
else
gtk_init (ref argc, ref buf);
if (buf != argv.Handle)
throw new Exception ("init returned new argv handle");
// copy back the resulting argv, minus argv[0], which we're
// not interested in.
if (argc <= 1)
args = new string[0];
else {
progargs = argv.GetArgs (argc);
args = new string[argc - 1];
Array.Copy (progargs, 1, args, 0, argc - 1);
}
return res;
}
public static void Init (string progname, ref string[] args)
{
do_init (progname, ref args, false);
}
public static bool InitCheck (string progname, ref string[] args)
{
return do_init (progname, ref args, true);
}
[DllImport (Global.GtkNativeDll, CallingConvention = CallingConvention.Cdecl)]
static extern void gtk_main ();
public static void Run ()
{
gtk_main ();
}
[DllImport (Global.GtkNativeDll, CallingConvention = CallingConvention.Cdecl)]
static extern bool gtk_events_pending ();
public static bool EventsPending ()
{
return gtk_events_pending ();
}
[DllImport (Global.GtkNativeDll, CallingConvention = CallingConvention.Cdecl)]
static extern void gtk_main_iteration ();
[DllImport (Global.GtkNativeDll, CallingConvention = CallingConvention.Cdecl)]
static extern bool gtk_main_iteration_do (bool blocking);
public static void RunIteration ()
{
gtk_main_iteration ();
}
public static bool RunIteration (bool blocking)
{
return gtk_main_iteration_do (blocking);
}
[DllImport (Global.GtkNativeDll, CallingConvention = CallingConvention.Cdecl)]
static extern void gtk_main_quit ();
public static void Quit ()
{
gtk_main_quit ();
}
[DllImport (Global.GtkNativeDll, CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr gtk_get_current_event ();
public static Gdk.Event CurrentEvent {
get {
return Gdk.Event.GetEvent (gtk_get_current_event ());
}
}
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);
GLib.Timeout.Add (0, new GLib.TimeoutHandler (icb.Invoke));
}
public static void Invoke (object sender, EventArgs args, EventHandler d)
{
InvokeCB icb = new InvokeCB (d, sender, args);
GLib.Timeout.Add (0, new GLib.TimeoutHandler (icb.Invoke));
}
}
}
|