/gui/application.d

http://github.com/wilkie/djehuty · D · 156 lines · 94 code · 42 blank · 20 comment · 7 complexity · ffd4eac79541c8fb7a89ba10dfb43ebd MD5 · raw file

  1. /*
  2. * application.d
  3. *
  4. * This module implements the GUI application class that will be the starting
  5. * point for a GUI application. From this, Window classes with Widgets can be
  6. * created and pushed.
  7. *
  8. * Author: Dave Wilkinson
  9. * Originated: June 24th, 2009
  10. *
  11. */
  12. module gui.application;
  13. import core.application;
  14. import core.event;
  15. import gui.widget;
  16. import gui.window;
  17. // Platform Specific application entry
  18. import gui.apploop;
  19. import platform.vars.window;
  20. import scaffold.window;
  21. class GuiApplication : Application {
  22. public:
  23. // Constructors
  24. this() {
  25. _appController = new GuiApplicationController();
  26. super();
  27. }
  28. override void push(Dispatcher dsp) {
  29. if (cast(Window)dsp !is null) {
  30. addWindow(cast(Window)dsp);
  31. }
  32. super.push(dsp);
  33. }
  34. // Properties
  35. int numWindows() {
  36. return _windowCount;
  37. }
  38. int numVisible() {
  39. return _windowVisibleCount;
  40. }
  41. // Methods
  42. override bool isZombie() {
  43. return _windowVisibleCount == 0;
  44. }
  45. Window firstWindow() {
  46. return _windowListHead;
  47. }
  48. protected:
  49. package Window _windowListHead = null;
  50. package Window _windowListTail = null;
  51. package int _windowCount;
  52. package int _windowVisibleCount;
  53. override void start() {
  54. _appController.start();
  55. }
  56. override void end(uint exitCode) {
  57. _appController.end(exitCode);
  58. }
  59. private:
  60. GuiApplicationController _appController;
  61. // Description: Will add and create the window (as long as it hasn't been already) and add it to the root window hierarchy.
  62. // window: An instance of a Window class, or any the inherit from Window.
  63. void addWindow(Window window) {
  64. WindowPlatformVars* wpv = &window._pfvars;
  65. synchronized {
  66. // update the window linked list
  67. updateWindowList(window);
  68. // increase global window count
  69. _windowCount++;
  70. // create the window through platform calls
  71. WindowCreate(window, wpv);
  72. }
  73. if (window.visible) {
  74. WindowSetVisible(window, wpv, true);
  75. }
  76. }
  77. void updateWindowList(Window window) {
  78. window._inited = true;
  79. if (_windowListHead is null)
  80. {
  81. _windowListHead = window;
  82. _windowListTail = window;
  83. window._nextWindow = window;
  84. window._prevWindow = window;
  85. }
  86. else
  87. {
  88. window._nextWindow = _windowListHead;
  89. window._prevWindow = _windowListTail;
  90. _windowListHead._prevWindow = window;
  91. _windowListTail._nextWindow = window;
  92. _windowListHead = window;
  93. }
  94. if (window._visible)
  95. {
  96. _windowVisibleCount++;
  97. }
  98. }
  99. package void destroyAllWindows()
  100. {
  101. Window w = _windowListHead;
  102. if (w is null) { return; }
  103. Window tmp = w;
  104. _windowListHead = null;
  105. _windowListTail = null;
  106. do
  107. {
  108. w.remove();
  109. w = w._nextWindow;
  110. } while (w !is tmp)
  111. _windowCount = 0;
  112. _windowVisibleCount = 0;
  113. }
  114. }