gtkStatsLabelStack.cxx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file gtkStatsLabelStack.cxx
  10. * @author drose
  11. * @date 2006-01-16
  12. */
  13. #include "gtkStatsLabelStack.h"
  14. #include "gtkStatsLabel.h"
  15. #include "pnotify.h"
  16. /**
  17. *
  18. */
  19. GtkStatsLabelStack::
  20. GtkStatsLabelStack() {
  21. _widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
  22. _highlight_label = -1;
  23. }
  24. /**
  25. *
  26. */
  27. GtkStatsLabelStack::
  28. ~GtkStatsLabelStack() {
  29. clear_labels();
  30. }
  31. /**
  32. * Returns the widget for this stack.
  33. */
  34. GtkWidget *GtkStatsLabelStack::
  35. get_widget() const {
  36. return _widget;
  37. }
  38. /**
  39. * Returns the y position of the indicated label's bottom edge, relative to
  40. * the indicated target widget.
  41. */
  42. int GtkStatsLabelStack::
  43. get_label_y(int label_index, GtkWidget *target_widget) const {
  44. nassertr(label_index >= 0 && label_index < (int)_labels.size(), 0);
  45. GtkAllocation allocation;
  46. gtk_widget_get_allocation(_widget, &allocation);
  47. // Assume all labels have the same height.
  48. int height = _labels[0]->get_height();
  49. int start_y = allocation.height - height * label_index;
  50. int x, y;
  51. gtk_widget_translate_coordinates(_widget, target_widget,
  52. 0, start_y, &x, &y);
  53. return y;
  54. }
  55. /**
  56. * Returns the height of the indicated label.
  57. */
  58. int GtkStatsLabelStack::
  59. get_label_height(int label_index) const {
  60. nassertr(label_index >= 0 && label_index < (int)_labels.size(), 0);
  61. return _labels[label_index]->get_height();
  62. }
  63. /**
  64. * Returns the collector index associated with the indicated label.
  65. */
  66. int GtkStatsLabelStack::
  67. get_label_collector_index(int label_index) const {
  68. nassertr(label_index >= 0 && label_index < (int)_labels.size(), -1);
  69. return _labels[label_index]->get_collector_index();
  70. }
  71. /**
  72. * Removes the set of labels and starts a new set.
  73. */
  74. void GtkStatsLabelStack::
  75. clear_labels(bool delete_widgets) {
  76. Labels::iterator li;
  77. for (li = _labels.begin(); li != _labels.end(); ++li) {
  78. GtkStatsLabel *label = (*li);
  79. if (delete_widgets) {
  80. gtk_container_remove(GTK_CONTAINER(_widget), label->get_widget());
  81. }
  82. delete label;
  83. }
  84. _labels.clear();
  85. }
  86. /**
  87. * Adds a new label to the top of the stack; returns the new label index.
  88. */
  89. int GtkStatsLabelStack::
  90. add_label(GtkStatsMonitor *monitor, GtkStatsGraph *graph,
  91. int thread_index, int collector_index, bool use_fullname) {
  92. GtkStatsLabel *label =
  93. new GtkStatsLabel(monitor, graph, thread_index, collector_index, use_fullname);
  94. gtk_box_pack_end(GTK_BOX(_widget), label->get_widget(),
  95. FALSE, FALSE, 0);
  96. int label_index = (int)_labels.size();
  97. _labels.push_back(label);
  98. return label_index;
  99. }
  100. /**
  101. * Returns the number of labels in the stack.
  102. */
  103. int GtkStatsLabelStack::
  104. get_num_labels() const {
  105. return _labels.size();
  106. }
  107. /**
  108. * Draws a highlight around the label representing the indicated collector,
  109. * and removes the highlight from any other label. Specify -1 to remove the
  110. * highlight from all labels.
  111. */
  112. void GtkStatsLabelStack::
  113. highlight_label(int collector_index) {
  114. if (_highlight_label != collector_index) {
  115. _highlight_label = collector_index;
  116. Labels::iterator li;
  117. for (li = _labels.begin(); li != _labels.end(); ++li) {
  118. GtkStatsLabel *label = (*li);
  119. label->set_highlight(label->get_collector_index() == _highlight_label);
  120. }
  121. }
  122. }