/main/src/addins/MonoDevelop.GtkCore2/libsteticui/Windows/WindowsTheme.cs

http://github.com/mono/monodevelop · C# · 201 lines · 158 code · 43 blank · 0 comment · 0 complexity · 4716f4d70a31cff27a258e4985dcb7d7 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.InteropServices;
  5. namespace Stetic.Windows
  6. {
  7. class WindowsTheme
  8. {
  9. IntPtr hWnd;
  10. IntPtr hTheme;
  11. public WindowsTheme (Gdk.Window win)
  12. {
  13. hWnd = gdk_win32_drawable_get_handle (win.Handle);
  14. hTheme = OpenThemeData (hWnd, "WINDOW");
  15. }
  16. public void Dipose ( )
  17. {
  18. CloseThemeData (hTheme);
  19. }
  20. public void DrawWindowFrame (Gtk.Widget w, string caption, int x, int y, int width, int height)
  21. {
  22. Gdk.Drawable drawable = w.GdkWindow;
  23. Gdk.Pixmap pix = new Gdk.Pixmap (drawable, width, height, drawable.Depth);
  24. Gdk.GC gcc = new Gdk.GC (pix);
  25. gcc.Foreground = w.Style.Backgrounds [(int)Gtk.StateType.Normal];
  26. pix.DrawRectangle (gcc, true, 0, 0, width, height);
  27. IntPtr hdc = gdk_win32_hdc_get (pix.Handle, gcc.Handle, 0);
  28. RECT r = new RECT (0, 0, width, height);
  29. SIZE size;
  30. GetThemePartSize (hTheme, hdc, WP_CAPTION, CS_ACTIVE, ref r, 1, out size);
  31. r.Bottom = size.cy;
  32. DrawThemeBackground (hTheme, hdc, WP_CAPTION, CS_ACTIVE, ref r, ref r);
  33. RECT rf = new RECT (FrameBorder, FrameBorder, width - FrameBorder * 2, size.cy);
  34. LOGFONT lf = new LOGFONT ();
  35. GetThemeSysFont (hTheme, TMT_CAPTIONFONT, ref lf);
  36. IntPtr titleFont = CreateFontIndirect (ref lf);
  37. IntPtr oldFont = SelectObject (hdc, titleFont);
  38. SetBkMode (hdc, 1 /*TRANSPARENT*/);
  39. DrawThemeText (hTheme, hdc, WP_CAPTION, CS_ACTIVE, caption, -1, DT_LEFT | DT_SINGLELINE | DT_WORD_ELLIPSIS, 0, ref rf);
  40. SelectObject (hdc, oldFont);
  41. DeleteObject (titleFont);
  42. int ny = r.Bottom;
  43. r = new RECT (0, ny, width, height);
  44. DrawThemeBackground (hTheme, hdc, WP_FRAMEBOTTOM, 0, ref r, ref r);
  45. gdk_win32_hdc_release (pix.Handle, gcc.Handle, 0);
  46. Gdk.Pixbuf img = Gdk.Pixbuf.FromDrawable (pix, pix.Colormap, 0, 0, 0, 0, width, height);
  47. drawable.DrawPixbuf (new Gdk.GC (drawable), img, 0, 0, x, y, width, height, Gdk.RgbDither.None, 0, 0);
  48. drawable.DrawRectangle (w.Style.BackgroundGC (Gtk.StateType.Normal), true, x + FrameBorder, y + size.cy, width - FrameBorder * 2, height - FrameBorder - size.cy);
  49. }
  50. public Gdk.Rectangle GetWindowClientArea (Gdk.Rectangle allocation)
  51. {
  52. IntPtr hdc = GetDC (hWnd);
  53. RECT r = new RECT (allocation.X, allocation.Y, allocation.X + allocation.Width, allocation.Y + allocation.Height);
  54. SIZE size;
  55. GetThemePartSize (hTheme, hdc, WP_CAPTION, CS_ACTIVE, ref r, 1, out size);
  56. ReleaseDC (hWnd, hdc);
  57. return new Gdk.Rectangle (allocation.X + FrameBorder, allocation.Y + size.cy, allocation.Width - FrameBorder * 2, allocation.Height - size.cy - FrameBorder);
  58. }
  59. const int FrameBorder = 8;
  60. const int WP_CAPTION = 1;
  61. const int WP_FRAMEBOTTOM = 9;
  62. const int CS_ACTIVE = 1;
  63. const int TMT_CAPTIONFONT = 801;
  64. const int DT_LEFT = 0x0;
  65. const int DT_WORD_ELLIPSIS = 0x40000;
  66. const int DT_SINGLELINE = 0x20;
  67. [DllImport ("uxtheme", ExactSpelling = true)]
  68. extern static Int32 DrawThemeBackground (IntPtr hTheme, IntPtr hdc, int iPartId,
  69. int iStateId, ref RECT pRect, ref RECT pClipRect);
  70. [DllImport ("uxtheme.dll", ExactSpelling = true, CharSet = CharSet.Unicode)]
  71. static extern IntPtr OpenThemeData (IntPtr hWnd, String classList);
  72. [DllImport ("uxtheme.dll", ExactSpelling = true)]
  73. extern static Int32 CloseThemeData (IntPtr hTheme);
  74. [DllImport ("uxtheme", ExactSpelling = true)]
  75. extern static Int32 GetThemePartSize (IntPtr hTheme, IntPtr hdc, int part, int state, ref RECT pRect, int eSize, out SIZE size);
  76. [DllImport ("uxtheme", ExactSpelling = true)]
  77. extern static Int32 GetThemeBackgroundExtent (IntPtr hTheme, IntPtr hdc, int iPartId, int iStateId, ref RECT pBoundingRect, out RECT pContentRect);
  78. [DllImport ("uxtheme", ExactSpelling = true)]
  79. extern static Int32 GetThemeMargins (IntPtr hTheme, IntPtr hdc, int iPartId, int iStateId, int iPropId, out MARGINS pMargins);
  80. [DllImport ("uxtheme", ExactSpelling = true, CharSet = CharSet.Unicode)]
  81. extern static Int32 DrawThemeText (IntPtr hTheme, IntPtr hdc, int iPartId, int iStateId, String text, int textLength, UInt32 textFlags, UInt32 textFlags2, ref RECT pRect);
  82. [DllImport ("uxtheme", ExactSpelling = true, CharSet = CharSet.Unicode)]
  83. extern static Int32 GetThemeSysFont (IntPtr hTheme, int iFontId, ref LOGFONT plf);
  84. [DllImport ("gdi32.dll")]
  85. static extern IntPtr CreateFontIndirect ([In] ref LOGFONT lplf);
  86. [DllImport ("gdi32.dll")]
  87. static extern int SetBkMode (IntPtr hdc, int iBkMode);
  88. [DllImport ("gdi32.dll", ExactSpelling = true, PreserveSig = true, SetLastError = true)]
  89. static extern IntPtr SelectObject (IntPtr hdc, IntPtr hgdiobj);
  90. [DllImport ("gdi32.dll")]
  91. static extern bool DeleteObject (IntPtr hObject);
  92. [DllImport ("user32.dll")]
  93. static extern IntPtr GetDC (IntPtr hWnd);
  94. [DllImport ("user32.dll")]
  95. static extern int ReleaseDC (IntPtr hWnd, IntPtr hDC);
  96. [DllImport ("libgdk-win32-2.0-0.dll")]
  97. static extern IntPtr gdk_win32_drawable_get_handle (IntPtr raw);
  98. [DllImport ("libgdk-win32-2.0-0.dll")]
  99. static extern IntPtr gdk_win32_hdc_get (IntPtr drawable, IntPtr gc, int usage);
  100. [DllImport ("libgdk-win32-2.0-0.dll")]
  101. static extern void gdk_win32_hdc_release (IntPtr drawable, IntPtr gc, int usage);
  102. }
  103. [Serializable, StructLayout (LayoutKind.Sequential)]
  104. public struct RECT
  105. {
  106. public int Left;
  107. public int Top;
  108. public int Right;
  109. public int Bottom;
  110. public RECT (int left_, int top_, int right_, int bottom_)
  111. {
  112. Left = left_;
  113. Top = top_;
  114. Right = right_;
  115. Bottom = bottom_;
  116. }
  117. public int Height { get { return Bottom - Top; } }
  118. public int Width { get { return Right - Left; } }
  119. }
  120. [StructLayout (LayoutKind.Sequential)]
  121. public struct SIZE
  122. {
  123. public int cx;
  124. public int cy;
  125. public SIZE (int cx, int cy)
  126. {
  127. this.cx = cx;
  128. this.cy = cy;
  129. }
  130. }
  131. [StructLayout (LayoutKind.Sequential)]
  132. public struct MARGINS
  133. {
  134. public int leftWidth;
  135. public int rightWidth;
  136. public int topHeight;
  137. public int bottomHeight;
  138. }
  139. [StructLayout (LayoutKind.Sequential, CharSet = CharSet.Auto)]
  140. public struct LOGFONT
  141. {
  142. public int lfHeight;
  143. public int lfWidth;
  144. public int lfEscapement;
  145. public int lfOrientation;
  146. public int lfWeight;
  147. public byte lfItalic;
  148. public byte lfUnderline;
  149. public byte lfStrikeOut;
  150. public byte lfCharSet;
  151. public byte lfOutPrecision;
  152. public byte lfClipPrecision;
  153. public byte lfQuality;
  154. public byte lfPitchAndFamily;
  155. [MarshalAs (UnmanagedType.ByValTStr, SizeConst = 32)]
  156. public string lfFaceName;
  157. }
  158. }