libyui-gtk  2.44.9
YGBarGraph.cc
1 /********************************************************************
2  * YaST2-GTK - http://en.opensuse.org/YaST2-GTK *
3  ********************************************************************/
4 
5 #include <yui/Libyui_config.h>
6 #include "YGUI.h"
7 #include "YGWidget.h"
8 #include "ygtkbargraph.h"
9 
10 #include "YBarGraph.h"
11 #include "YGMacros.h"
12 
13 
14 class YGBarGraph : public YBarGraph, public YGWidget
15 {
16 public:
17  YGBarGraph (YWidget *parent)
18  : YBarGraph (NULL)
19  , YGWidget (this, parent, YGTK_TYPE_BAR_GRAPH, NULL)
20  {}
21 
22  // YBarGraph
23  virtual void doUpdate()
24  {
25  YGtkBarGraph *graph = YGTK_BAR_GRAPH (getWidget());
26  ygtk_bar_graph_create_entries (graph, segments());
27  for (int i = 0; i < segments(); i++) {
28  const YBarGraphSegment &s = segment (i);
29  ygtk_bar_graph_setup_entry (graph, i, s.label().c_str(), s.value());
30  if (s.hasSegmentColor()) {
31  GdkRGBA color = ycolorToGdk (s.segmentColor());
32  ygtk_bar_graph_customize_bg (graph, i, &color);
33  }
34  if (s.hasTextColor()) {
35  GdkRGBA color = ycolorToGdk (s.textColor());
36  ygtk_bar_graph_customize_fg (graph, i, &color);
37  }
38  }
39  }
40 
41  static GdkRGBA ycolorToGdk (const YColor &ycolor)
42  {
43  GdkRGBA color = { 0,
44  static_cast<gdouble> ( guint16(ycolor.red() << 8 ) ),
45  static_cast<gdouble> ( guint16(ycolor.green() << 8 ) ),
46  static_cast<gdouble> ( guint16(ycolor.blue() << 8 ) )
47  };
48  return color;
49  }
50 
51  virtual unsigned int getMinSize (YUIDimension dim)
52  { return dim == YD_HORIZ ? 80 : 30; }
53 
54  YGWIDGET_IMPL_COMMON (YBarGraph)
55 };
56 
57 YBarGraph *YGOptionalWidgetFactory::createBarGraph (YWidget *parent)
58 {
59  return new YGBarGraph (parent);
60 }
61 
62 #include "YPartitionSplitter.h"
63 
64 class YGPartitionSplitter : public YPartitionSplitter, public YGWidget
65 {
66 public:
67  YGtkBarGraph *m_barGraph;
68  GtkWidget *m_scale, *m_free_spin, *m_new_spin;
69 
70  YGPartitionSplitter (YWidget *parent, int usedSize, int totalFreeSize, int newPartSize,
71  int minNewPartSize, int minFreeSize, const std::string &usedLabel, const std::string &freeLabel,
72  const std::string &newPartLabel, const std::string &freeFieldLabel, const std::string &newPartFieldLabel)
73  : YPartitionSplitter (NULL, usedSize, totalFreeSize, newPartSize, minNewPartSize,
74  minFreeSize, usedLabel, freeLabel, newPartLabel, freeFieldLabel, newPartFieldLabel)
75  , YGWidget (this, parent, YGTK_VBOX_NEW(0), NULL)
76  {
77  /* Bar graph widget */
78  GtkWidget *graph = ygtk_bar_graph_new();
79  m_barGraph = YGTK_BAR_GRAPH (graph);
80  ygtk_bar_graph_create_entries (m_barGraph, 3);
81  ygtk_bar_graph_setup_entry (m_barGraph, 0, usedLabel.c_str(), usedSize);
82 
83  /* Labels over the slider */
84  GtkWidget *labels_box = YGTK_HBOX_NEW(0);
85  gtk_box_set_homogeneous (GTK_BOX (labels_box), FALSE);
86  gtk_box_pack_start (GTK_BOX (labels_box),
87  gtk_label_new (freeFieldLabel.c_str()), FALSE, TRUE, 0);
88  gtk_box_pack_start (GTK_BOX (labels_box), gtk_label_new (NULL), TRUE, TRUE, 0);
89  gtk_box_pack_start (GTK_BOX (labels_box),
90  gtk_label_new (newPartFieldLabel.c_str()), FALSE, TRUE, 0);
91 
92  /* Slider and the spinners */
93  GtkWidget *slider_box = YGTK_HBOX_NEW(0);
94  gtk_box_set_homogeneous (GTK_BOX (slider_box), FALSE);
95  m_scale = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL, (gdouble) minFreeSize, maxFreeSize(), 1);
96  gtk_scale_set_draw_value (GTK_SCALE (m_scale), FALSE);
97  m_free_spin = gtk_spin_button_new_with_range
98  (minFreeSize, maxFreeSize(), 1);
99  m_new_spin = gtk_spin_button_new_with_range
100  (minNewPartSize, maxNewPartSize(), 1);
101 
102  // keep the partition's order
103  gtk_widget_set_direction (labels_box, GTK_TEXT_DIR_LTR);
104  gtk_widget_set_direction (slider_box, GTK_TEXT_DIR_LTR);
105 
106  gtk_box_pack_start (GTK_BOX (slider_box), m_free_spin, FALSE, FALSE, 0);
107  gtk_box_pack_start (GTK_BOX (slider_box), m_scale, TRUE, TRUE, 0);
108  gtk_box_pack_start (GTK_BOX (slider_box), m_new_spin, FALSE, FALSE, 0);
109 
110  connect (m_scale, "value-changed", G_CALLBACK (scale_changed_cb), this);
111  connect (m_free_spin, "value-changed", G_CALLBACK (free_spin_changed_cb), this);
112  connect (m_new_spin, "value-changed", G_CALLBACK (new_spin_changed_cb), this);
113 
114  /* Main layout */
115  gtk_box_pack_start (GTK_BOX (getWidget()), graph, TRUE, TRUE, 6);
116  gtk_box_pack_start (GTK_BOX (getWidget()), labels_box, FALSE, TRUE, 2);
117  gtk_box_pack_start (GTK_BOX (getWidget()), slider_box, FALSE, TRUE, 2);
118 
119  setValue (newPartSize); // initialization
120  gtk_widget_show_all (getWidget());
121  }
122 
123  // YPartitionSplitter
124  virtual int value()
125  {
126  return gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (m_new_spin));
127  }
128 
129  virtual void setValue (int newValue)
130  {
131  BlockEvents block (this);
132  gtk_spin_button_set_value (GTK_SPIN_BUTTON (m_new_spin), newValue);
133  int freeSize = totalFreeSize() - newValue;
134  gtk_spin_button_set_value (GTK_SPIN_BUTTON (m_free_spin), freeSize);
135  gtk_range_set_value (GTK_RANGE (m_scale), freeSize);
136 
137  gtk_spin_button_set_value (GTK_SPIN_BUTTON (m_free_spin), freeSize);
138  gtk_spin_button_set_value (GTK_SPIN_BUTTON (m_new_spin), newValue);
139 
140  ygtk_bar_graph_setup_entry (m_barGraph, 1, freeLabel().c_str(), freeSize);
141  ygtk_bar_graph_setup_entry (m_barGraph, 2, newPartLabel().c_str(), newValue);
142  }
143 
144  static void scale_changed_cb (GtkRange *range, YGPartitionSplitter *pThis)
145  {
146  int newFreeSize = (int) gtk_range_get_value (range);
147  int newPartSize = pThis->totalFreeSize() - newFreeSize;
148 
149  pThis->setValue (newPartSize);
150  pThis->emitEvent (YEvent::ValueChanged);
151  }
152 
153  static void free_spin_changed_cb (GtkSpinButton *spin, YGPartitionSplitter *pThis)
154  {
155  int newFreeSize = gtk_spin_button_get_value_as_int (spin);
156  int newPartSize = pThis->totalFreeSize() - newFreeSize;
157  pThis->setValue (newPartSize);
158  pThis->emitEvent (YEvent::ValueChanged);
159  }
160 
161  static void new_spin_changed_cb (GtkSpinButton *spin, YGPartitionSplitter *pThis)
162  {
163  pThis->setValue (gtk_spin_button_get_value_as_int (spin));
164  pThis->emitEvent (YEvent::ValueChanged);
165  }
166 
167  YGWIDGET_IMPL_COMMON (YPartitionSplitter)
168 };
169 
170 YPartitionSplitter *YGOptionalWidgetFactory::createPartitionSplitter (YWidget *parent,
171  int usedSize, int totalFreeSize, int newPartSize, int minNewPartSize,
172  int minFreeSize, const std::string &usedLabel, const std::string &freeLabel,
173  const std::string &newPartLabel, const std::string &freeFieldLabel,
174  const std::string &newPartFieldLabel)
175 {
176  return new YGPartitionSplitter (parent, usedSize, totalFreeSize, newPartSize,
177  minNewPartSize, minFreeSize, usedLabel, freeLabel, newPartLabel, freeFieldLabel,
178  newPartFieldLabel);
179 }
180 
YGPartitionSplitter
Definition: YGBarGraph.cc:64
BlockEvents
Definition: YGWidget.h:76
_YGtkBarGraph
Definition: ygtkbargraph.h:30
YGWidget
Definition: YGWidget.h:13
YGBarGraph
Definition: YGBarGraph.cc:14