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