PageRenderTime 46ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/platform/unix/scaffold/window.d

http://github.com/wilkie/djehuty
D | 382 lines | 222 code | 93 blank | 67 comment | 22 complexity | 6d41d84df9a8e74feef4c6029e31ad83 MD5 | raw file
  1. /*
  2. * window.d
  3. *
  4. * This Scaffold holds the Window implementations for the Linux platform
  5. *
  6. * Author: Dave Wilkinson
  7. *
  8. */
  9. module scaffold.window;
  10. import platform.vars.window;
  11. import platform.vars.view;
  12. import platform.vars.brush;
  13. import platform.vars.pen;
  14. import scaffold.graphics;
  15. import platform.unix.common;
  16. import platform.unix.main;
  17. import graphics.view;
  18. import graphics.graphics;
  19. import core.color;
  20. import core.string;
  21. import core.main;
  22. import core.definitions;
  23. import io.file;
  24. import gui.application;
  25. import gui.window;
  26. import io.console;
  27. struct MWMHints {
  28. Clong flags;
  29. Clong functions;
  30. Clong decorations;
  31. Clong input_mode;
  32. Clong status;
  33. }
  34. const int PROP_MWM_HINTS_ELEMENTS = 5;
  35. const int MWM_HINTS_FUNCTIONS = 1;
  36. const int MWM_HINTS_DECORATIONS = 2;
  37. const int MWM_HINTS_INPUT_MODE = 4;
  38. const int MWM_HINTS_STATUS = 8;
  39. /* functions */
  40. const int MWM_FUNC_ALL = 1;
  41. const int MWM_FUNC_RESIZE = 2;
  42. const int MWM_FUNC_MOVE = 4;
  43. const int MWM_FUNC_MINIMIZE = 8;
  44. const int MWM_FUNC_MAXIMIZE = 16;
  45. const int MWM_FUNC_CLOSE = 32;
  46. /* decorations */
  47. const int MWM_DECOR_ALL = 1;
  48. const int MWM_DECOR_BORDER = 2;
  49. const int MWM_DECOR_RESIZEH = 4;
  50. const int MWM_DECOR_TITLE = 8;
  51. const int MWM_DECOR_MENU = 16;
  52. const int MWM_DECOR_MINIMIZE = 32;
  53. const int MWM_DECOR_MAXIMIZE = 64;
  54. const int USPosition = 1; /* user specified x, y */
  55. const int USSize = 2; /* user specified width, height
  56. */
  57. const int PPosition = 4; /* program specified position
  58. */
  59. const int PSize = 8; /* program specified size */
  60. const int PMinSize = 16; /* program specified minimum
  61. size */
  62. const int PMaxSize = 32; /* program specified maximum
  63. size */
  64. const int PResizeInc = 64; /* program specified Size
  65. increments */
  66. const int PAspect = 128; /* program specified min and
  67. max aspect ratios */
  68. const int PBaseSize = 256;
  69. const int PWinGravity = 512;
  70. const int PAllHints = (PPosition|PSize|
  71. PMinSize|PMaxSize|
  72. PResizeInc|PAspect);
  73. // all windows
  74. void WindowCreate(ref Window window, WindowPlatformVars* windowVars) {
  75. // code to create the window
  76. windowVars.destroy_called = false;
  77. X.XSetWindowAttributes attributes;
  78. if (window.style == WindowStyle.Popup) {
  79. attributes.override_redirect = X.Bool.True;
  80. }
  81. attributes.event_mask = X.EventMask.ExposureMask | X.EventMask.ButtonPressMask | X.EventMask.KeyPressMask |
  82. X.EventMask.ButtonReleaseMask | X.EventMask.PointerMotionMask | X.EventMask.KeyReleaseMask | X.EventMask.StructureNotifyMask |
  83. X.EventMask.EnterWindowMask | X.EventMask.LeaveWindowMask | X.EventMask.FocusChangeMask;
  84. attributes.win_gravity = X.BitGravity.StaticGravity;
  85. X.Window parent = X.XRootWindow(_pfvars.display, _pfvars.screen);
  86. uint w_x = window.x;
  87. uint w_y = window.y;
  88. if (window.position == WindowPosition.Center) {
  89. // Get the display width and height and center the window
  90. uint d_width = X.XDisplayWidth(_pfvars.display, _pfvars.screen);
  91. uint d_height = X.XDisplayHeight(_pfvars.display, _pfvars.screen);
  92. w_x = d_width - window.width;
  93. w_x >>= 1;
  94. w_y = d_height - window.height;
  95. w_y >>= 1;
  96. }
  97. windowVars.window = X.XCreateWindow(_pfvars.display,
  98. parent, w_x,w_y,window.width,window.height,0,
  99. X.CopyFromParent,
  100. X.WindowClass.InputOutput,
  101. cast(X.Visual*)X.CopyFromParent,
  102. X.WindowAttribute.CWOverrideRedirect | X.WindowAttribute.CWEventMask | X.WindowAttribute.CWWinGravity,
  103. &attributes);
  104. X.Window root;
  105. X.Window* children;
  106. uint childrenCount;
  107. X.Window last = windowVars.window;
  108. X.Status r = X.XSetWMProtocols(_pfvars.display, windowVars.window, &_pfvars.wm_destroy_window, 1);
  109. WindowRebound(window, windowVars);
  110. WindowSetTitle(window, windowVars);
  111. if (window.visible) {
  112. WindowSetVisible(window, windowVars, true);
  113. }
  114. // int x_return, y_return, width_return, height_return, grav_return;
  115. // X.XSizeHints hints;
  116. // X.XWMGeometry(_pfvars.display, _pfvars.screen, null, null, &hints, &x_return, &y_return, &width_return, &height_return, &grav_return);
  117. if (window.position != WindowPosition.Default) {
  118. X.XMoveWindow(_pfvars.display, windowVars.window, w_x, w_y);
  119. }
  120. // X.XMoveWindow(_pfvars.display, windowVars.wm_parent, window.x, window.y);
  121. // Create View
  122. window.onInitialize();
  123. // Run Add
  124. window.onAdd();
  125. }
  126. void WindowCreate(ref Window parent, WindowPlatformVars* parentHelper, ref Window window, WindowPlatformVars* windowVars) {
  127. // code to create a child window
  128. //int screen;
  129. WindowCreate(window, windowVars);
  130. return;
  131. }
  132. void WindowSetStyle(ref Window window, WindowPlatformVars* windowVars) {
  133. // code to change the style of a window
  134. }
  135. void WindowReposition(ref Window window, WindowPlatformVars* windowVars) {
  136. // code to move a window
  137. X.Window wind = windowVars.window;
  138. if (windowVars.wm_parent != 0) {
  139. wind = windowVars.wm_parent;
  140. }
  141. X.XMoveWindow(_pfvars.display, wind, window.x, window.y);
  142. }
  143. void WindowSetState(ref Window window, WindowPlatformVars* windowVars) {
  144. // code to change the state of a window
  145. }
  146. void WindowRebound(ref Window window, WindowPlatformVars* windowVars) {
  147. // code to Size a window
  148. int width, height;
  149. width = window.width;
  150. height = window.height;
  151. MWMHints mwmhints;
  152. X.XSizeHints* xhints;
  153. X.Atom wm_normal_hints = X.XInternAtom(_pfvars.display, "WM_NORMAL_HINTS\0"c.ptr, X.Bool.True);
  154. if (window.style == WindowStyle.Popup) {
  155. X.Atom prop = X.XInternAtom(_pfvars.display, "_MOTIF_WM_HINTS\0"c.ptr, X.Bool.True);
  156. if (prop != X.None) {
  157. //set with MWM
  158. mwmhints.flags = MWM_HINTS_DECORATIONS;
  159. mwmhints.functions = MWM_FUNC_MOVE;
  160. mwmhints.decorations = 0;
  161. X.XChangeProperty(_pfvars.display, windowVars.window, prop, prop, 32, X.PropertyMode.PropModeReplace,
  162. cast(ubyte*)&mwmhints, PROP_MWM_HINTS_ELEMENTS);
  163. }
  164. //set with WM_NORMAL_HINTS (disable Size function)
  165. xhints = X.XAllocSizeHints();
  166. xhints.flags = PMinSize | PMaxSize | PResizeInc;
  167. xhints.width_inc = 0;
  168. xhints.height_inc = 0;
  169. xhints.max_width = xhints.min_width = width;
  170. xhints.max_height = xhints.min_height = height;
  171. X.XSetWMNormalHints(_pfvars.display, windowVars.window, xhints);
  172. X.XFree(xhints);
  173. }
  174. else if (window.style == WindowStyle.Fixed) {
  175. X.Atom prop = X.XInternAtom(_pfvars.display, "_MOTIF_WM_HINTS\0"c.ptr, X.Bool.True);
  176. if (prop != X.None) {
  177. //set with MWM
  178. mwmhints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
  179. mwmhints.functions = MWM_FUNC_MINIMIZE | MWM_FUNC_CLOSE | MWM_FUNC_MOVE;
  180. mwmhints.decorations = MWM_DECOR_BORDER | MWM_DECOR_TITLE | MWM_DECOR_MENU | MWM_DECOR_MAXIMIZE;
  181. X.XChangeProperty(_pfvars.display, windowVars.window, prop, prop, 32, X.PropertyMode.PropModeReplace,
  182. cast(ubyte*)&mwmhints, PROP_MWM_HINTS_ELEMENTS);
  183. }
  184. //set with WM_NORMAL_HINTS (disable Size function)
  185. xhints = X.XAllocSizeHints();
  186. xhints.flags = PMinSize | PMaxSize | PResizeInc;
  187. xhints.width_inc = 0;
  188. xhints.height_inc = 0;
  189. xhints.max_width = xhints.min_width = width;
  190. xhints.max_height = xhints.min_height = height;
  191. X.XSetWMNormalHints(_pfvars.display, windowVars.window, xhints);
  192. X.XFree(xhints);
  193. }
  194. }
  195. void WindowDestroy(ref Window window, WindowPlatformVars* windowVars) {
  196. // code to destroy a window
  197. windowVars.destroy_called = true;
  198. //destroy window
  199. X.XDestroyWindow(_pfvars.display, windowVars.window);
  200. }
  201. void WindowSetVisible(ref Window window, WindowPlatformVars* windowVars, bool bShow) {
  202. // code to show or hide a window
  203. if (bShow) {
  204. X.XMapWindow(_pfvars.display, windowVars.window);
  205. }
  206. else {
  207. X.XUnmapWindow(_pfvars.display, windowVars.window);
  208. }
  209. }
  210. void WindowSetTitle(ref Window window, WindowPlatformVars* windowVars) {
  211. // code to change a window's title
  212. //Set the window's text
  213. //Fill in a text property
  214. string str = window.text.dup;
  215. str ~= '\0';
  216. // Alternative Syntax:
  217. /*
  218. X.XTextProperty tprop;
  219. tprop.value = str.ptr;
  220. tprop.encoding = _pfvars.utf8string;
  221. tprop.format = 8;
  222. tprop.nitems = str.array.length-1;
  223. X.XSetTextProperty(_pfvars.display, windowVars.window, &tprop, _pfvars.wm_name);
  224. */
  225. // Update the Property:
  226. X.XChangeProperty(_pfvars.display, windowVars.window,
  227. X.XInternAtom(_pfvars.display, "_NET_WM_NAME", X.Bool.False),
  228. X.XInternAtom(_pfvars.display, "UTF8_STRING", X.Bool.False),
  229. 8, X.PropertyMode.PropModeReplace, cast(ubyte*)str.ptr, str.length-1);
  230. X.XChangeProperty(_pfvars.display, windowVars.window,
  231. X.XInternAtom(_pfvars.display, "_NET_WM_ICON_NAME", X.Bool.False),
  232. X.XInternAtom(_pfvars.display, "UTF8_STRING", X.Bool.False),
  233. 8, X.PropertyMode.PropModeReplace, cast(ubyte*)str.ptr, str.length-1);
  234. }
  235. // CLIENT TO SCREEN
  236. // Takes a point on the window's client area and returns the actual screen
  237. // coordinates for that point.
  238. void WindowClientToScreen(ref Window window, WindowPlatformVars* windowVars, ref int x, ref int y) {
  239. //Coord pt = {x,y};
  240. //ClientToScreen(windowVars.hWnd, &pt);
  241. Window wret;
  242. X.XTranslateCoordinates(_pfvars.display, windowVars.window,
  243. X.RootWindow(_pfvars.display, _pfvars.screen), x,y, &x, &y,cast(Culong*)&wret);
  244. }
  245. void WindowClientToScreen(ref Window window, WindowPlatformVars* windowVars, ref Coord pt) {
  246. //ClientToScreen(windowVars.hWnd, &pt);
  247. Window wret;
  248. X.XTranslateCoordinates(_pfvars.display, windowVars.window,
  249. X.RootWindow(_pfvars.display, _pfvars.screen), pt.x, pt.y, &pt.x, &pt.y, cast(Culong*)&wret);
  250. }
  251. void WindowClientToScreen(ref Window window, WindowPlatformVars* windowVars, ref Rect rt) {
  252. }
  253. // Viewable windows
  254. void WindowStartDraw(ref Window window, WindowPlatformVars* windowVars, ref WindowView view, ref ViewPlatformVars viewVars) {
  255. X.XLockDisplay(_pfvars.display);
  256. // code executed at the start of a redraw for a window
  257. viewVars.textclr_red = 0.0;
  258. viewVars.textclr_green = 0.0;
  259. viewVars.textclr_blue = 0.0;
  260. viewVars.textclr_alpha = 1.0;
  261. viewVars.isOpaqueRendering = 0;
  262. Cairo.cairo_new_path(viewVars.cr);
  263. Cairo.cairo_identity_matrix(viewVars.cr);
  264. if (viewVars.aa) {
  265. Cairo.cairo_set_antialias(viewVars.cr, Cairo.cairo_antialias_t.CAIRO_ANTIALIAS_DEFAULT);
  266. }
  267. else {
  268. Cairo.cairo_set_antialias(viewVars.cr, Cairo.cairo_antialias_t.CAIRO_ANTIALIAS_NONE);
  269. }
  270. }
  271. void WindowEndDraw(ref Window window, WindowPlatformVars* windowVars, ref WindowView view, ref ViewPlatformVars viewVars) {
  272. // code to reclaim resources, and executed after all components have drawn to the window
  273. //copy over area
  274. X.XFlush(_pfvars.display);
  275. X.XCopyArea(_pfvars.display, viewVars.pixmap,
  276. windowVars.window, viewVars.gc,
  277. 0, 0, window.width, window.height, 0, 0);
  278. X.XUnlockDisplay(_pfvars.display);
  279. }
  280. void WindowCaptureMouse(ref Window window, WindowPlatformVars* windowVars) {
  281. // capture the mouse
  282. }
  283. void WindowReleaseMouse(ref Window window, WindowPlatformVars* windowVars) {
  284. // release the mouse
  285. }