* XplatUI.cs: Added Border3DSize, BorderSize, CaptionButtonSize,
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / XplatUIDriver.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2004-2006 Novell, Inc.
21 //
22 // Authors:
23 //      Peter Bartok            pbartok@novell.com
24 //      Sebastien Pouliot       sebastien@ximian.com
25 //
26
27 // COMPLETE
28
29 using System.Drawing;
30 using System.Threading;
31 using System.Runtime.InteropServices;
32
33 namespace System.Windows.Forms {
34         internal abstract class XplatUIDriver {
35                 internal abstract IntPtr        InitializeDriver();
36                 internal abstract void          ShutdownDriver(IntPtr token);
37                 internal delegate IntPtr        WndProc(IntPtr hwnd, Msg msg, IntPtr wParam, IntPtr lParam);
38
39
40                 #region XplatUI Driver Properties
41                 internal virtual Color ForeColor {
42                         get {
43                                 return ThemeEngine.Current.DefaultWindowForeColor;
44                         }
45                 }
46
47                 internal virtual  Color BackColor { 
48                         get {
49                                 return ThemeEngine.Current.DefaultWindowBackColor;
50                         }
51                 }
52
53                 internal virtual Size Border3DSize {
54                         get {
55                                 return new Size (2, 2);
56                         }
57                 }
58
59                 internal virtual Size BorderSize {
60                         get {
61                                 return new Size (1, 1);
62                         }
63                 }
64
65                 internal virtual Size CaptionButtonSize {
66                         get {
67                                 return new Size (18, 18);
68                         }
69                 }
70
71                 internal virtual Size DoubleClickSize {
72                         get {
73                                 return new Size (4, 4);
74                         }
75                 }
76
77                 internal virtual int DoubleClickTime {
78                         get {
79                                 return 500;
80                         }
81                 }
82
83                 internal virtual Size FixedFrameBorderSize {
84                         get {
85                                 return new Size (3, 3);
86                         }
87                 }
88
89                 internal virtual Font Font {
90                         get {
91                                 return ThemeEngine.Current.DefaultFont;
92                         }
93                 }
94
95                 internal virtual Keys ModifierKeys {
96                         get {
97                                 return Keys.None;
98                         }
99                 }
100
101                 internal virtual MouseButtons MouseButtons {
102                         get {
103                                 return MouseButtons.None;
104                         }
105                 }
106
107                 internal virtual Size MouseHoverSize {
108                         get {
109                                 return new Size (1, 1);
110                         }
111                 }
112
113                 internal virtual int MouseHoverTime {
114                         get {
115                                 return 500;
116                         }
117                 }
118
119                 internal virtual int MouseWheelScrollDelta {
120                         get {
121                                 return 120;
122                         }
123                 }
124                 
125                 internal virtual Point MousePosition {
126                         get {
127                                 return Point.Empty;
128                         }
129                 }
130
131                 internal virtual int MenuHeight {
132                         get {
133                                 return 19;
134                         }
135                 }
136
137                 internal virtual bool DropTarget {
138                         get {
139                                 return false;
140                         }
141
142                         set {
143                         }
144                 }
145
146                 internal virtual int HorizontalScrollBarHeight {
147                         get {
148                                 return 16;
149                         }
150                 }
151
152                 internal virtual bool UserClipWontExposeParent {
153                         get {
154                                 return true;
155                         }
156                 }
157
158                 internal virtual int VerticalScrollBarWidth {
159                         get {
160                                 return 16;
161                         }
162                 }
163
164                 internal abstract int CaptionHeight { get; }
165                 internal abstract Size CursorSize { get; }
166                 internal abstract bool DragFullWindows { get; }
167                 internal abstract Size DragSize { get; }
168                 internal abstract Size FrameBorderSize { get; }
169                 internal abstract Size IconSize { get; }
170                 internal abstract Size MaxWindowTrackSize { get; }
171                 internal abstract bool MenuAccessKeysUnderlined { get; }
172                 internal abstract Size MinimizedWindowSize { get; }
173                 internal abstract Size MinimizedWindowSpacingSize { get; }
174                 internal abstract Size MinimumWindowSize { get; }
175                 internal abstract Size MinWindowTrackSize { get; }
176                 internal abstract Size SmallIconSize { get; }
177                 internal abstract int MouseButtonCount { get; }
178                 internal abstract bool MouseButtonsSwapped { get; }
179                 internal abstract bool MouseWheelPresent { get; }
180                 internal abstract Rectangle VirtualScreen { get; }
181                 internal abstract Rectangle WorkingArea { get; }
182                 internal abstract bool ThemesEnabled { get; }
183
184                 #endregion      // XplatUI Driver Properties
185
186                 internal abstract event EventHandler Idle;
187
188                 #region XplatUI Driver Methods
189                 internal abstract void AudibleAlert();
190
191                 internal abstract void EnableThemes();
192
193                 internal abstract void GetDisplaySize(out Size size);
194
195                 internal abstract IntPtr CreateWindow(CreateParams cp);
196                 internal abstract IntPtr CreateWindow(IntPtr Parent, int X, int Y, int Width, int Height);
197                 internal abstract void DestroyWindow(IntPtr handle);
198
199                 internal abstract FormWindowState GetWindowState(IntPtr handle);
200                 internal abstract void SetWindowState(IntPtr handle, FormWindowState state);
201                 internal abstract void SetWindowMinMax(IntPtr handle, Rectangle maximized, Size min, Size max);
202
203                 internal abstract void SetWindowStyle(IntPtr handle, CreateParams cp);
204
205                 internal abstract double GetWindowTransparency(IntPtr handle);
206                 internal abstract void SetWindowTransparency(IntPtr handle, double transparency, Color key);
207                 internal abstract TransparencySupport SupportsTransparency();
208
209                 internal virtual void SetAllowDrop (IntPtr handle, bool value)
210                 {
211                         Console.Error.WriteLine ("Drag and Drop is currently " +
212                                         "not supported on this platform");
213                 }
214
215                 internal virtual DragDropEffects StartDrag(IntPtr handle, object data, DragDropEffects allowedEffects) {
216                         Console.Error.WriteLine ("Drag and Drop is currently " +
217                                         "not supported on this platform");
218                         return DragDropEffects.None;
219                 }
220
221                 internal abstract void SetBorderStyle(IntPtr handle, FormBorderStyle border_style);
222                 internal abstract void SetMenu(IntPtr handle, Menu menu);
223
224                 internal abstract bool GetText(IntPtr handle, out string text);
225                 internal abstract bool Text(IntPtr handle, string text);
226                 internal abstract bool SetVisible(IntPtr handle, bool visible, bool activate);
227                 internal abstract bool IsVisible(IntPtr handle);
228                 internal abstract bool IsEnabled(IntPtr handle);
229                 internal abstract IntPtr SetParent(IntPtr handle, IntPtr parent);
230                 internal abstract IntPtr GetParent(IntPtr handle);
231
232                 internal abstract void UpdateWindow(IntPtr handle);
233                 internal abstract PaintEventArgs PaintEventStart(IntPtr handle, bool client);
234                 internal abstract void PaintEventEnd(IntPtr handle, bool client);
235
236                 internal abstract void SetWindowPos(IntPtr handle, int x, int y, int width, int height);
237                 internal abstract void GetWindowPos(IntPtr handle, bool is_toplevel, out int x, out int y, out int width, out int height, out int client_width, out int client_height);
238                 internal abstract void Activate(IntPtr handle);
239                 internal abstract void EnableWindow(IntPtr handle, bool Enable);
240                 internal abstract void SetModal(IntPtr handle, bool Modal);
241                 internal abstract void Invalidate(IntPtr handle, Rectangle rc, bool clear);
242                 internal abstract void InvalidateNC(IntPtr handle);
243                 internal abstract IntPtr DefWndProc(ref Message msg);
244                 internal abstract void HandleException(Exception e);
245                 internal abstract void DoEvents();
246                 internal abstract bool PeekMessage(Object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax, uint flags);
247                 internal abstract void PostQuitMessage(int exitCode);
248                 internal abstract bool GetMessage(object queue_id, ref MSG msg, IntPtr hWnd, int wFilterMin, int wFilterMax);
249                 internal abstract bool TranslateMessage(ref MSG msg);
250                 internal abstract IntPtr DispatchMessage(ref MSG msg);
251
252                 internal abstract bool SetZOrder(IntPtr hWnd, IntPtr AfterhWnd, bool Top, bool Bottom);
253                 internal abstract bool SetTopmost(IntPtr hWnd, bool Enabled);
254                 internal abstract bool SetOwner(IntPtr hWnd, IntPtr hWndOwner);
255
256                 internal abstract bool CalculateWindowRect(ref Rectangle ClientRect, CreateParams cp, Menu menu, out Rectangle WindowRect);
257
258                 internal abstract Region GetClipRegion(IntPtr hwnd);
259                 internal abstract void SetClipRegion(IntPtr hwnd, Region region);
260
261                 internal abstract void SetCursor(IntPtr hwnd, IntPtr cursor);
262                 internal abstract void ShowCursor(bool show);
263                 internal abstract void OverrideCursor(IntPtr cursor);
264                 internal abstract IntPtr DefineCursor(Bitmap bitmap, Bitmap mask, Color cursor_pixel, Color mask_pixel, int xHotSpot, int yHotSpot);
265                 internal abstract IntPtr DefineStdCursor(StdCursor id);
266                 internal abstract Bitmap DefineStdCursorBitmap(StdCursor id);
267                 internal abstract void DestroyCursor(IntPtr cursor);
268                 internal abstract void GetCursorInfo(IntPtr cursor, out int width, out int height, out int hotspot_x, out int hotspot_y);
269
270                 internal abstract void GetCursorPos(IntPtr hwnd, out int x, out int y);
271                 internal abstract void SetCursorPos(IntPtr hwnd, int x, int y);
272
273                 internal abstract void ScreenToClient(IntPtr hwnd, ref int x, ref int y);
274                 internal abstract void ClientToScreen(IntPtr hwnd, ref int x, ref int y);
275
276                 internal abstract void GrabWindow(IntPtr hwnd, IntPtr ConfineToHwnd);
277                 internal abstract void GrabInfo(out IntPtr hwnd, out bool GrabConfined, out Rectangle GrabArea);
278                 internal abstract void UngrabWindow(IntPtr hwnd);
279
280                 internal abstract void SendAsyncMethod (AsyncMethodData method);
281                 internal abstract void SetTimer (Timer timer);
282                 internal abstract void KillTimer (Timer timer);
283
284                 internal abstract void CreateCaret(IntPtr hwnd, int width, int height);
285                 internal abstract void DestroyCaret(IntPtr hwnd);
286                 internal abstract void SetCaretPos(IntPtr hwnd, int x, int y);
287                 internal abstract void CaretVisible(IntPtr hwnd, bool visible);
288
289                 internal abstract IntPtr GetFocus();
290                 internal abstract void SetFocus(IntPtr hwnd);
291                 internal abstract IntPtr GetActive();
292
293                 internal abstract void ScrollWindow(IntPtr hwnd, Rectangle rectangle, int XAmount, int YAmount, bool with_children);
294                 internal abstract void ScrollWindow(IntPtr hwnd, int XAmount, int YAmount, bool with_children);
295
296                 internal abstract bool GetFontMetrics(Graphics g, Font font, out int ascent, out int descent);
297
298                 internal abstract bool SystrayAdd(IntPtr hwnd, string tip, Icon icon, out ToolTip tt);
299                 internal abstract bool SystrayChange(IntPtr hwnd, string tip, Icon icon, ref ToolTip tt);
300                 internal abstract void SystrayRemove(IntPtr hwnd, ref ToolTip tt);
301 #if NET_2_0             
302                 internal abstract void SystrayBalloon(IntPtr hwnd, int timeout, string title, string text, ToolTipIcon icon);
303 #endif
304
305                 internal abstract Point GetMenuOrigin(IntPtr hwnd);
306                 internal abstract void MenuToScreen(IntPtr hwnd, ref int x, ref int y);
307                 internal abstract void ScreenToMenu(IntPtr hwnd, ref int x, ref int y);
308
309                 internal abstract void SetIcon(IntPtr handle, Icon icon);
310
311                 internal abstract void ClipboardClose(IntPtr handle);
312                 internal abstract IntPtr ClipboardOpen (bool primary_selection);
313                 internal abstract int ClipboardGetID(IntPtr handle, string format);
314                 internal abstract void ClipboardStore(IntPtr handle, object obj, int id, XplatUI.ObjectToClipboard converter);
315                 internal abstract int[] ClipboardAvailableFormats(IntPtr handle);
316                 internal abstract object ClipboardRetrieve(IntPtr handle, int id, XplatUI.ClipboardToObject converter);
317
318                 internal abstract void DrawReversibleLine(Point start, Point end, Color backColor);
319                 internal abstract void DrawReversibleRectangle(IntPtr handle, Rectangle rect, int line_width);
320                 internal abstract void FillReversibleRectangle (Rectangle rectangle, Color backColor);
321                 internal abstract void DrawReversibleFrame (Rectangle rectangle, Color backColor, FrameStyle style);
322
323                 internal abstract SizeF GetAutoScaleSize(Font font);
324
325                 internal abstract IntPtr SendMessage(IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam);
326                 internal abstract bool PostMessage(IntPtr hwnd, Msg message, IntPtr wParam, IntPtr lParam);
327                 internal abstract int SendInput(IntPtr hwnd, System.Collections.Queue keys);
328
329                 internal abstract object StartLoop(Thread thread);
330                 internal abstract void EndLoop(Thread thread);
331
332                 internal abstract void RequestNCRecalc(IntPtr hwnd);
333                 internal abstract void ResetMouseHover(IntPtr hwnd);
334                 internal abstract void RequestAdditionalWM_NCMessages(IntPtr hwnd, bool hover, bool leave);
335
336                 // System information
337                 internal abstract int KeyboardSpeed { get; } 
338                 internal abstract int KeyboardDelay { get; } 
339
340
341                 // Double buffering
342                 internal virtual void CreateOffscreenDrawable (IntPtr handle,
343                                                                int width, int height,
344                                                                out object offscreen_drawable)
345                 {
346                         Bitmap bmp = new Bitmap (width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
347
348                         offscreen_drawable = bmp;
349                 }
350
351                 internal virtual void DestroyOffscreenDrawable (object offscreen_drawable)
352                 {
353                         Bitmap bmp = (Bitmap)offscreen_drawable;
354
355                         bmp.Dispose ();
356                 }
357                 
358                 internal virtual Graphics GetOffscreenGraphics (object offscreen_drawable)
359                 {
360                         Bitmap bmp = (Bitmap)offscreen_drawable;
361                         return Graphics.FromImage (bmp);
362                 }
363
364                 internal virtual void BlitFromOffscreen (IntPtr dest_handle,
365                                                          Graphics dest_dc,
366                                                          object offscreen_drawable,
367                                                          Graphics offscreen_dc,
368                                                          Rectangle r)
369                 {
370                         dest_dc.DrawImage ((Bitmap)offscreen_drawable, r, r, GraphicsUnit.Pixel);
371                 }
372
373 #endregion      // XplatUI Driver Methods
374         }
375
376         internal class XplatUIDriverSupport {
377                 #region XplatUI Driver Support Methods
378 #if NET_2_0
379                 internal static void ExecutionCallback (object state)
380                 {
381                         AsyncMethodData data = (AsyncMethodData) state;
382                         AsyncMethodResult result = data.Result;
383                         object ret = data.Method.DynamicInvoke (data.Args);
384                         if (result != null) {
385                                 result.Complete (ret);
386                         }
387                 }
388
389                 internal static void ExecuteClientMessage (GCHandle gchandle)
390                 {
391                         AsyncMethodData data = (AsyncMethodData) gchandle.Target;
392                         try {
393                                 if (data.Context == null) {
394                                         ExecutionCallback (data);
395                                 } else {
396                                         ExecutionContext.Run (data.Context, new ContextCallback (ExecutionCallback), data);
397                                 }
398                         }
399                         finally {
400                                 gchandle.Free ();
401                         }
402                 }
403 #else
404                 // for NET_1_0 and NET_1_1 no (public) ExecutionContext exists 
405                 // so we must use the System.Threading.CompressedStack class
406                 internal static void ExecuteClientMessage (GCHandle gchandle) {
407                         AsyncMethodData data = (AsyncMethodData) gchandle.Target;
408                         CompressedStack original = null;
409                         
410 #if !MWF_ON_MSRUNTIME
411                         // Stack is non-null only if the security manager is active
412                         if (data.Stack != null) {
413                                 original = Thread.CurrentThread.GetCompressedStack ();
414                                 Thread.CurrentThread.SetCompressedStack (data.Stack);
415                         }
416 #endif
417
418                         try {
419                                 AsyncMethodResult result = data.Result;
420                                 object ret = data.Method.DynamicInvoke (data.Args);
421                                 result.Complete (ret);
422                         }
423                         finally {
424 #if !MWF_ON_MSRUNTIME
425                                 if (data.Stack != null) {
426                                         // whatever occurs we must revert to the original compressed
427                                         // stack (null being a valid, empty, value in this case).
428                                         Thread.CurrentThread.SetCompressedStack (original);
429                                 }
430 #endif
431                                 gchandle.Free ();
432                         }
433                 }
434 #endif
435                 
436                 #endregion      // XplatUI Driver Support Methods
437         }
438 }