dock_renderer.vala 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /********************************************************************
  2. # Copyright 2014 Daniel 'grindhold' Brendle
  3. #
  4. # This file is part of libgtkflow.
  5. #
  6. # libgtkflow is free software: you can redistribute it and/or
  7. # modify it under the terms of the GNU Lesser General Public License
  8. # as published by the Free Software Foundation, either
  9. # version 3 of the License, or (at your option) any later
  10. # version.
  11. #
  12. # libgtkflow is distributed in the hope that it will be
  13. # useful, but WITHOUT ANY WARRANTY; without even the implied
  14. # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. # PURPOSE. See the GNU Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public
  18. # License along with libgtkflow.
  19. # If not, see http://www.gnu.org/licenses/.
  20. *********************************************************************/
  21. namespace GtkFlow {
  22. public abstract class DockRenderer : GLib.Object {
  23. /**
  24. * Default value for the dock's dockpoint size.
  25. * Used for both height and width
  26. */
  27. public int dockpoint_height {get;set;default=16;}
  28. /**
  29. * Spacing between caption and dockpoint
  30. */
  31. public int spacing_x {get; set; default=5;}
  32. /**
  33. * Vertical spacing between docks
  34. */
  35. public int spacing_y {get; set; default=3;}
  36. /**
  37. * This signal is to be emitted whenever the size of this dock changes
  38. */
  39. public signal void size_changed();
  40. /**
  41. * Draw this dockrenderer's {@link GFlow.Dock} on the given
  42. * {@link Cairo.Context}
  43. */
  44. public abstract void draw_dock(Cairo.Context cr, Gtk.StyleContext sc,
  45. int offset_x, int offset_y, int width);
  46. /**
  47. * Implementations should return this dock's minimal height
  48. */
  49. public abstract int get_min_height();
  50. /**
  51. * Implementations should return this dock's minimal width
  52. */
  53. public abstract int get_min_width();
  54. /**
  55. * Implementations should update the graphical representation of the
  56. * dock's captionstring and typestring. If typestrings should be displayed,
  57. * the parameter show_types will be true
  58. */
  59. public abstract void update_name_layout(bool show_types);
  60. /**
  61. * Return the dock that belongs to this dockrenderer
  62. */
  63. public abstract GFlow.Dock get_dock();
  64. }
  65. private class DefaultDockRenderer : DockRenderer {
  66. private Pango.Layout layout = null;
  67. private GFlow.Dock d = null;
  68. public DefaultDockRenderer(Node n, GFlow.Dock d) {
  69. this.d = d;
  70. this.layout = (new Gtk.Label("")).create_pango_layout("");
  71. }
  72. public override GFlow.Dock get_dock () {
  73. return this.d;
  74. }
  75. public override void update_name_layout(bool show_types) {
  76. string labelstring;
  77. string name = this.d.name ?? "";
  78. string typename = this.d.typename ?? this.d.determine_typestring();
  79. if (show_types) {
  80. labelstring = "<i>%s</i> : %s".printf( typename, name );
  81. } else {
  82. labelstring = name;
  83. }
  84. this.layout.set_markup(labelstring, -1);
  85. this.size_changed();
  86. }
  87. /**
  88. * Get the minimum width for this dock
  89. */
  90. public override int get_min_height() {
  91. int width, height;
  92. this.layout.get_pixel_size(out width, out height);
  93. return (int)(Math.fmax(height, dockpoint_height))+spacing_y;
  94. }
  95. /**
  96. * Get the minimum height for this dock
  97. */
  98. public override int get_min_width() {
  99. int width, height;
  100. this.layout.get_pixel_size(out width, out height);
  101. return (int)(width + dockpoint_height + spacing_y);
  102. }
  103. public override void draw_dock(Cairo.Context cr, Gtk.StyleContext sc,
  104. int offset_x, int offset_y, int width) {
  105. if (d is GFlow.Sink)
  106. draw_sink(cr, sc, offset_x, offset_y, width);
  107. if (d is GFlow.Source)
  108. draw_source(cr, sc, offset_x, offset_y, width);
  109. }
  110. /**
  111. * Draw the given source onto a cairo context
  112. */
  113. public void draw_source(Cairo.Context cr, Gtk.StyleContext sc,
  114. int offset_x, int offset_y, int width) {
  115. sc.save();
  116. sc.set_state(Gtk.StateFlags.NORMAL);
  117. if (this.d.is_linked())
  118. sc.set_state(Gtk.StateFlags.CHECKED);
  119. if (this.d.highlight)
  120. sc.set_state(sc.get_state() | Gtk.StateFlags.PRELIGHT);
  121. if (this.d.active)
  122. sc.set_state(sc.get_state() | Gtk.StateFlags.ACTIVE);
  123. sc.add_class(Gtk.STYLE_CLASS_RADIO);
  124. sc.render_background(cr, offset_x+width-dockpoint_height-0.5, offset_y-0.5,
  125. dockpoint_height, dockpoint_height);
  126. sc.render_option(cr, offset_x+width-dockpoint_height, offset_y,
  127. dockpoint_height,dockpoint_height);
  128. sc.restore();
  129. sc.save();
  130. sc.add_class(Gtk.STYLE_CLASS_BUTTON);
  131. Gdk.RGBA col = sc.get_color(Gtk.StateFlags.NORMAL);
  132. cr.set_source_rgba(col.red,col.green,col.blue,col.alpha);
  133. cr.move_to(offset_x + width - this.get_min_width(), offset_y);
  134. Pango.cairo_show_layout(cr, this.layout);
  135. sc.restore();
  136. }
  137. /**
  138. * Draw the given sink onto a cairo context
  139. */
  140. public void draw_sink(Cairo.Context cr, Gtk.StyleContext sc,
  141. int offset_x, int offset_y, int width) {
  142. sc.save();
  143. sc.set_state(Gtk.StateFlags.NORMAL);
  144. if (this.d.is_linked())
  145. sc.set_state(Gtk.StateFlags.CHECKED);
  146. if (this.d.highlight)
  147. sc.set_state(sc.get_state() | Gtk.StateFlags.PRELIGHT);
  148. if (this.d.active)
  149. sc.set_state(sc.get_state() | Gtk.StateFlags.ACTIVE);
  150. sc.add_class(Gtk.STYLE_CLASS_RADIO);
  151. sc.render_background(cr, offset_x-0.5, offset_y-0.5,
  152. dockpoint_height, dockpoint_height);
  153. sc.render_option(cr, offset_x,offset_y,dockpoint_height,dockpoint_height);
  154. sc.restore();
  155. sc.save();
  156. sc.add_class(Gtk.STYLE_CLASS_BUTTON);
  157. Gdk.RGBA col = sc.get_color(Gtk.StateFlags.NORMAL);
  158. cr.set_source_rgba(col.red,col.green,col.blue,col.alpha);
  159. cr.move_to(offset_x+dockpoint_height+spacing_x, offset_y);
  160. Pango.cairo_show_layout(cr, this.layout);
  161. sc.restore();
  162. }
  163. }
  164. }