libyui-gtk  2.43.3
 All Classes
YGIntField.cc
1 /********************************************************************
2  * YaST2-GTK - http://en.opensuse.org/YaST2-GTK *
3  ********************************************************************/
4 
5 #define YUILogComponent "gtk"
6 #include "YGUI.h"
7 #include "YGUtils.h"
8 #include "YGWidget.h"
9 
10 class YGSpinBox : public YGLabeledWidget
11 {
12  YIntField *m_yfield;
13  GtkWidget *m_spiner, *m_slider;
14 
15 public:
16  YGSpinBox (YWidget *ywidget, YWidget *parent, const std::string &label,
17  int minValue, int maxValue, int initialValue, bool show_slider)
18  : YGLabeledWidget (ywidget, parent, label, YD_HORIZ,
19  GTK_TYPE_HBOX, "spacing", 6, NULL)
20  {
21  m_spiner = gtk_spin_button_new_with_range (minValue, maxValue, 1);
22 
23  if (show_slider) {
24  m_slider = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL, minValue, maxValue, 1);
25  if (maxValue - minValue < 20)
26  // GtkScale by default uses a step of 10 -- use a lower for low values
27  gtk_range_set_increments (GTK_RANGE (m_slider), 1, 2);
28  gtk_scale_set_draw_value (GTK_SCALE (m_slider), FALSE);
29  YGLabeledWidget::setBuddy (m_slider);
30  gtk_widget_set_size_request (m_slider, 100, -1);
31 
32  gtk_box_pack_start (GTK_BOX (getWidget()), m_slider, TRUE, TRUE, 0);
33  gtk_box_pack_start (GTK_BOX (getWidget()), m_spiner, FALSE, TRUE, 0);
34  gtk_widget_show (m_slider);
35  }
36  else {
37  m_slider = NULL;
38  YGLabeledWidget::setBuddy (m_spiner);
39  gtk_container_add (GTK_CONTAINER (getWidget()), m_spiner);
40  }
41  gtk_widget_show (m_spiner);
42 
43  doSetValue (initialValue);
44  connect (m_spiner, "value-changed", G_CALLBACK (spiner_changed_cb), this);
45  if (m_slider)
46  connect (m_slider, "value-changed", G_CALLBACK (slider_changed_cb), this);
47  }
48 
49  GtkSpinButton *getSpiner()
50  { return GTK_SPIN_BUTTON (m_spiner); }
51 
52  bool useSlider()
53  { return m_slider != NULL; }
54  GtkRange *getSlider()
55  { return GTK_RANGE (m_slider); }
56 
57  virtual void reportValue (int value) = 0;
58 
59  int doGetValue()
60  { return gtk_spin_button_get_value_as_int (getSpiner()); }
61 
62  void doSetValue (int value)
63  {
64  BlockEvents block (this);
65  gtk_spin_button_set_value (getSpiner(), value);
66  if (useSlider())
67  gtk_range_set_value (getSlider(), value);
68  }
69 
70  // Events callbacks
71  static void spiner_changed_cb (GtkSpinButton *widget, YGSpinBox *pThis)
72  {
73  int value = gtk_spin_button_get_value_as_int (pThis->getSpiner());
74  pThis->reportValue (value);
75  if (pThis->useSlider())
76  gtk_range_set_value (pThis->getSlider(), value);
77  pThis->emitEvent (YEvent::ValueChanged);
78  }
79 
80  static void slider_changed_cb (GtkRange *range, YGSpinBox *pThis)
81  {
82  int value = (int) gtk_range_get_value (range);
83  gtk_spin_button_set_value (pThis->getSpiner(), value);
84  pThis->reportValue (value);
85  pThis->emitEvent (YEvent::ValueChanged);
86  }
87 };
88 
89 #define YGSPIN_BOX_IMPL(ParentClass) \
90  virtual void reportValue (int value) { \
91  ParentClass::setValue (value); \
92  } \
93  virtual int value() { \
94  return doGetValue(); \
95  } \
96  virtual void setValueInternal (int value) { \
97  doSetValue (value); \
98  }
99 
100 #include "YIntField.h"
101 
102 class YGIntField : public YIntField, public YGSpinBox
103 {
104 public:
105  YGIntField (YWidget *parent, const std::string &label, int minValue, int maxValue,
106  int initialValue)
107  : YIntField (NULL, label, minValue, maxValue)
108  , YGSpinBox (this, parent, label, minValue, maxValue, initialValue, false)
109  {}
110 
111  YGLABEL_WIDGET_IMPL (YIntField)
112  YGSPIN_BOX_IMPL (YIntField)
113 };
114 
115 YIntField *YGWidgetFactory::createIntField (YWidget *parent, const std::string &label,
116  int minValue, int maxValue, int initialValue)
117 { return new YGIntField (parent, label, minValue, maxValue, initialValue); }
118 
119 #include "YSlider.h"
120 
121 class YGSlider : public YSlider, public YGSpinBox
122 {
123 public:
124  YGSlider (YWidget *parent, const std::string &label, int minValue, int maxValue,
125  int initialValue)
126  : YSlider (NULL, label, minValue, maxValue)
127  , YGSpinBox (this, parent, label, minValue, maxValue, initialValue, true)
128  {}
129 
130  virtual unsigned int getMinSize (YUIDimension dim)
131  { return dim == YD_HORIZ ? 200 : 0; }
132 
133  YGLABEL_WIDGET_IMPL (YSlider)
134  YGSPIN_BOX_IMPL (YSlider)
135 };
136 
137 YSlider *YGOptionalWidgetFactory::createSlider (YWidget *parent, const std::string &label,
138  int minValue, int maxValue, int initialValue)
139 { return new YGSlider (parent, label, minValue, maxValue, initialValue); }
140