PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/fwt/dotnet/WidgetPeer.cs

https://bitbucket.org/bedlaczech/fan-1.0
C# | 161 lines | 90 code | 29 blank | 42 comment | 24 complexity | 6d50ae0511acdc3a0f9ebaa1ab356d7c MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright (c) 2008, Brian Frank and Andy Frank
  3. // Licensed under the Academic Free License version 3.0
  4. //
  5. // History:
  6. // 15 Jun 08 Andy Frank Creation
  7. //
  8. using Fan.Sys;
  9. using System.Drawing;
  10. using System.Windows.Forms;
  11. //using System.Collections;
  12. //using System.Diagnostics;
  13. //using System.Drawing;
  14. //using System.Drawing.Drawing2D;
  15. //using System.IO;
  16. //using System.Runtime.InteropServices;
  17. //using System.Windows.Forms;
  18. namespace Fan.Fwt
  19. {
  20. /// <summary>
  21. /// Native methods for Widget.
  22. /// </summary>
  23. public abstract class WidgetPeer
  24. {
  25. //////////////////////////////////////////////////////////////////////////
  26. // Constructor
  27. //////////////////////////////////////////////////////////////////////////
  28. public static WidgetPeer make(Widget self)
  29. {
  30. System.Type type = System.Type.GetType("Fan.Fwt." + self.type().name().val + "Peer");
  31. WidgetPeer peer = (WidgetPeer)System.Activator.CreateInstance(type);
  32. peer.m_self = self;
  33. return peer;
  34. }
  35. //////////////////////////////////////////////////////////////////////////
  36. // Layout
  37. //////////////////////////////////////////////////////////////////////////
  38. public Point pos(Widget self)
  39. {
  40. //if (control == null) return pos;
  41. //Point p = control.getLocation();
  42. //return fan.fwt.Point.make(Long.valueOf(p.x), Long.valueOf(p.y));
  43. return null;
  44. }
  45. public void pos(Widget self, Fan.Fwt.Point pos)
  46. {
  47. //if (control == null) { this.pos = pos; return; }
  48. //control.setLocation((int)pos.x.val, (int)pos.y.val);
  49. }
  50. public Size size(Widget self)
  51. {
  52. if (m_control == null) return m_size;
  53. return Size.make(Long.valueOf(m_control.Size.Width), Long.valueOf(m_control.Size.Height));
  54. }
  55. public void size(Widget self, Fan.Fwt.Size size)
  56. {
  57. if (m_control == null) { this.m_size = size; return; }
  58. m_control.Size = new System.Drawing.Size((int)size.m_w.val, (int)size.m_h.val);
  59. }
  60. public Rect bounds(Widget self)
  61. {
  62. if (m_control == null) return Rect.make(m_pos.m_x, m_pos.m_y, m_size.m_w, m_size.m_h);
  63. Rectangle b = m_control.Bounds;
  64. return Rect.make(Long.valueOf(b.X), Long.valueOf(b.Y), Long.valueOf(b.Width), Long.valueOf(b.Height));
  65. }
  66. public void bounds(Widget self, Rect b)
  67. {
  68. if (m_control == null) { m_pos = b.pos(); m_size = b.size(); return; }
  69. m_control.Bounds = new Rectangle((int)b.m_x.val, (int)b.m_y.val, (int)b.m_w.val, (int)b.m_h.val);
  70. }
  71. //////////////////////////////////////////////////////////////////////////
  72. // Attachment
  73. //////////////////////////////////////////////////////////////////////////
  74. public Boolean attached(Widget self)
  75. {
  76. return m_control != null ? Boolean.True : Boolean.False;
  77. }
  78. public void attach(Widget self)
  79. {
  80. // short circuit if I'm already attached
  81. if (m_control != null) return;
  82. // short circuit if my parent isn't attached
  83. Widget parentWidget = self.parent();
  84. if (parentWidget == null || parentWidget.m_peer.m_control == null) return;
  85. // create control and initialize
  86. attachTo(create(parentWidget.m_peer.m_control));
  87. }
  88. internal void attachTo(Control control)
  89. {
  90. // sync with native control
  91. this.m_control = control;
  92. if (m_pos != Fan.Fwt.Point.m_def) pos(m_self, m_pos);
  93. if (m_size != Fan.Fwt.Size.m_def) size(m_self, m_size);
  94. sync(null);
  95. // recursively attach my children
  96. List kids = m_self.m_kids;
  97. for (int i=0; i<kids.sz(); ++i)
  98. {
  99. Widget kid = (Widget)kids.get(i);
  100. kid.m_peer.attach(kid);
  101. }
  102. }
  103. public abstract Control create(Control parent);
  104. public void detach(Widget self)
  105. {
  106. if (m_control == null) return;
  107. m_control.Dispose();
  108. m_control = null;
  109. }
  110. //////////////////////////////////////////////////////////////////////////
  111. // Widget/Control synchronization
  112. //////////////////////////////////////////////////////////////////////////
  113. public void sync(Widget self, string id)
  114. {
  115. if (m_control == null) return;
  116. sync(id);
  117. }
  118. public abstract void sync(string id);
  119. public object send(Widget self, string id)
  120. {
  121. return send(id);
  122. }
  123. public virtual object send(string id) { System.Console.WriteLine(id); return null; }
  124. //////////////////////////////////////////////////////////////////////////
  125. // Fields
  126. //////////////////////////////////////////////////////////////////////////
  127. internal Widget m_self;
  128. internal Control m_control;
  129. internal Fan.Fwt.Point m_pos = Fan.Fwt.Point.m_def;
  130. internal Fan.Fwt.Size m_size = Fan.Fwt.Size.m_def;
  131. }
  132. }