libyui-gtk  2.43.3
 All Classes
YGDumbTab.cc
1 /********************************************************************
2  * YaST2-GTK - http://en.opensuse.org/YaST2-GTK *
3  ********************************************************************/
4 
5 #define YUILogComponent "gtk"
6 #include <yui/Libyui_config.h>
7 #include "YGUI.h"
8 #include "YGWidget.h"
9 #include "YGUtils.h"
10 #include "YDumbTab.h"
11 #include <gtk/gtk.h>
12 #include "ygtkratiobox.h"
13 
14 class YGDumbTab : public YDumbTab, public YGWidget
15 {
16  GtkWidget *m_containee;
17  GtkWidget *m_last_tab;
18 
19 public:
20  YGDumbTab (YWidget *parent)
21  : YDumbTab (NULL),
22  YGWidget (this, parent, GTK_TYPE_NOTEBOOK, NULL)
23  {
24  m_containee = gtk_event_box_new();
25  g_object_ref_sink (G_OBJECT (m_containee));
26  gtk_widget_show (m_containee);
27 
28  m_last_tab = 0;
29  // GTK+ keeps the notebook size set to the biggset page. We can't
30  // do this since pages are set dynamically, but at least don't let
31  // the notebook reduce its size.
32  ygtk_adj_size_set_only_expand (YGTK_ADJ_SIZE (m_adj_size), TRUE);
33 
34  connect (getWidget(), "switch-page", G_CALLBACK (switch_page_cb), this);
35  }
36 
37  virtual ~YGDumbTab()
38  {
39  gtk_widget_destroy (m_containee);
40  g_object_unref (G_OBJECT (m_containee));
41  }
42 
43  virtual GtkWidget *getContainer()
44  { return m_containee; }
45 
46  virtual void addItem (YItem *item)
47  {
48  BlockEvents block (this);
49  YDumbTab::addItem (item);
50  GtkWidget *tab_label, *image = 0, *label;
51  label = gtk_label_new (YGUtils::mapKBAccel (item->label()).c_str());
52  gtk_label_set_use_underline (GTK_LABEL (label), TRUE);
53  if (item->hasIconName()) {
54  std::string path = iconFullPath (item->iconName());
55  GdkPixbuf *pixbuf = YGUtils::loadPixbuf (path);
56  if (pixbuf) {
57  image = gtk_image_new_from_pixbuf (pixbuf);
58  g_object_unref (G_OBJECT (pixbuf));
59  }
60  }
61  if (image) {
62  tab_label = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
63  gtk_box_set_homogeneous (GTK_BOX (tab_label), FALSE);
64  gtk_box_pack_start (GTK_BOX (tab_label), image, FALSE, TRUE, 0);
65  gtk_box_pack_start (GTK_BOX (tab_label), label, TRUE, TRUE, 0);
66  }
67  else
68  tab_label = label;
69  gchar *label_id = g_strdup_printf ("label-%d", item->index());
70  g_object_set_data (G_OBJECT (getWidget()), label_id, label);
71  g_free (label_id);
72  gtk_widget_show_all (tab_label);
73 
74  GtkNotebook *notebook = GTK_NOTEBOOK (getWidget());
75 
76  GtkWidget *page = gtk_event_box_new();
77  gtk_widget_show (page);
78  item->setData ((void *) page);
79  g_object_set_data (G_OBJECT (page), "yitem", item);
80 
81  gtk_notebook_append_page (notebook, page, tab_label);
82  selectItem (item, item->selected() || !m_last_tab /*first tab*/);
83  }
84 
85  virtual void deleteAllItems()
86  {
87  GList *children = gtk_container_get_children (GTK_CONTAINER (getWidget()));
88  for (GList *i = children; i; i = i->next)
89  gtk_container_remove (GTK_CONTAINER (getWidget()), (GtkWidget *) i->data);
90  g_list_free (children);
91  YDumbTab::deleteAllItems();
92  }
93 
94  // to re-use the same widget in all tabs (m_fixed), we will remove and
95  // add to the tabs' child as tabs are changed
96  void syncTabPage()
97  {
98  if (m_last_tab)
99  gtk_container_remove (GTK_CONTAINER (m_last_tab), m_containee);
100 
101  GtkNotebook *notebook = GTK_NOTEBOOK (getWidget());
102  int nb = gtk_notebook_get_current_page (notebook);
103  m_last_tab = gtk_notebook_get_nth_page (notebook, nb);
104  gtk_container_add (GTK_CONTAINER (m_last_tab), m_containee);
105  }
106 
107  virtual YItem *selectedItem()
108  {
109  GtkNotebook *notebook = GTK_NOTEBOOK (getWidget());
110  int nb = gtk_notebook_get_current_page (notebook);
111  if (nb < 0) return NULL;
112  GtkWidget *child = gtk_notebook_get_nth_page (notebook, nb);
113  return (YItem *) g_object_get_data (G_OBJECT (child), "yitem");
114  }
115 
116  virtual void selectItem (YItem *item, bool selected)
117  {
118  if (selected) {
119  BlockEvents block (this);
120  GtkWidget *child = (GtkWidget *) item->data();
121  int page = gtk_notebook_page_num (GTK_NOTEBOOK (getWidget()), child);
122 
123  gtk_notebook_set_current_page (GTK_NOTEBOOK (getWidget()), page);
124  syncTabPage();
125  }
126  YDumbTab::selectItem (item, selected);
127  }
128 
129  virtual void shortcutChanged()
130  {
131  for (YItemConstIterator it = itemsBegin(); it != itemsEnd(); it++) {
132  YItem *item = *it;
133  gchar *label_id = g_strdup_printf ("label-%d", item->index());
134  GtkWidget *label;
135  label = (GtkWidget *) g_object_get_data (G_OBJECT (getWidget()), label_id);
136  g_free (label_id);
137 
138  std::string text = YGUtils::mapKBAccel (item->label());
139  gtk_label_set_text (GTK_LABEL (label), text.c_str());
140  gtk_label_set_use_underline (GTK_LABEL (label), TRUE);
141  }
142  }
143 
144  // callbacks
145  static void switch_page_cb (GtkNotebook *notebook, GtkWidget *page,
146  guint tab_nb, YGDumbTab *pThis)
147  {
148  GtkWidget *child = gtk_notebook_get_nth_page (notebook, tab_nb);
149  YItem *item = (YItem *) g_object_get_data (G_OBJECT (child), "yitem");
150 
151  pThis->YDumbTab::selectItem (item);
152  YGUI::ui()->sendEvent (new YMenuEvent (item));
153  pThis->syncTabPage();
154  }
155 
156  YGWIDGET_IMPL_CONTAINER (YDumbTab)
157 };
158 
159 YDumbTab *YGOptionalWidgetFactory::createDumbTab (YWidget *parent)
160 { return new YGDumbTab (parent); }
161