]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtknotebook.c
Clarify the docs. Pointed out by Wolfgang Oertl
[~andy/gtk] / gtk / gtknotebook.c
index dbacf011279a3e7ed27bd1340455fa1c2bc21fc4..b623e15ea31821fffe8f2018359806ee2b6fa7b3 100644 (file)
  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
  */
 
-#include <config.h>
+#include "config.h"
+
+#include <stdio.h>
+#include <string.h>
+
+#include <gdk/gdkkeysyms.h>
+
+#undef GTK_DISABLE_DEPRECATED
+
 #include "gtknotebook.h"
 #include "gtkmain.h"
 #include "gtkmenu.h"
 #include "gtkmenuitem.h"
 #include "gtklabel.h"
-#include <gdk/gdkkeysyms.h>
-#include <stdio.h>
 #include "gtkintl.h"
 #include "gtkmarshalers.h"
 #include "gtkbindings.h"
 #include "gtkprivate.h"
 #include "gtkdnd.h"
+#include "gtkbuildable.h"
+
 #include "gtkalias.h"
 
 #define SCROLL_DELAY_FACTOR   5
@@ -210,6 +218,8 @@ static void     gtk_notebook_move_focus_out      (GtkNotebook      *notebook,
 static gboolean gtk_notebook_reorder_tab         (GtkNotebook      *notebook,
                                                  GtkDirectionType  direction_type,
                                                  gboolean          move_to_last);
+static void     gtk_notebook_remove_tab_label    (GtkNotebook      *notebook,
+                                                 GtkNotebookPage  *page);
 
 /*** GtkObject Methods ***/
 static void gtk_notebook_destroy             (GtkObject        *object);
@@ -348,10 +358,8 @@ static GList * gtk_notebook_search_page      (GtkNotebook      *notebook,
                                              GList            *list,
                                              gint              direction,
                                              gboolean          find_visible);
-static GtkNotebook *gtk_notebook_create_window (GtkNotebook    *notebook,
-                                                GtkWidget      *page,
-                                                gint            x,
-                                                gint            y);
+static void  gtk_notebook_child_reordered    (GtkNotebook      *notebook,
+                                             GtkNotebookPage  *page);
 
 /*** GtkNotebook Drawing Functions ***/
 static void gtk_notebook_paint               (GtkWidget        *widget,
@@ -421,6 +429,12 @@ static void do_detach_tab  (GtkNotebook *from,
                            gint         x,
                            gint         y);
 
+/* GtkBuildable */
+static void gtk_notebook_buildable_init           (GtkBuildableIface *iface);
+static void gtk_notebook_buildable_add_child      (GtkBuildable *buildable,
+                                                  GtkBuilder   *builder,
+                                                  GObject      *child,
+                                                  const gchar  *type);
 
 static GtkNotebookWindowCreationFunc window_creation_hook = NULL;
 static gpointer window_creation_hook_data;
@@ -428,7 +442,9 @@ static GDestroyNotify window_creation_hook_destroy = NULL;
 
 static guint notebook_signals[LAST_SIGNAL] = { 0 };
 
-G_DEFINE_TYPE (GtkNotebook, gtk_notebook, GTK_TYPE_CONTAINER)
+G_DEFINE_TYPE_WITH_CODE (GtkNotebook, gtk_notebook, GTK_TYPE_CONTAINER,
+                        G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
+                                               gtk_notebook_buildable_init))
 
 static void
 add_tab_bindings (GtkBindingSet    *binding_set,
@@ -555,9 +571,9 @@ gtk_notebook_class_init (GtkNotebookClass *class)
                                   g_param_spec_int ("page",
                                                     P_("Page"),
                                                     P_("The index of the current page"),
-                                                    0,
+                                                    -1,
                                                     G_MAXINT,
-                                                    0,
+                                                    -1,
                                                     GTK_PARAM_READWRITE));
   g_object_class_install_property (gobject_class,
                                   PROP_TAB_POS,
@@ -638,6 +654,14 @@ gtk_notebook_class_init (GtkNotebookClass *class)
                                                     G_MAXINT,
                                                     -1,
                                                     GTK_PARAM_READWRITE));
+
+  /**
+   * GtkNotebook:group:
+   *  
+   * Group for tabs drag and drop.
+   *
+   * Since: 2.12
+   */    
   g_object_class_install_property (gobject_class,
                                   PROP_GROUP,
                                   g_param_spec_pointer ("group",
@@ -671,7 +695,7 @@ gtk_notebook_class_init (GtkNotebookClass *class)
                                              g_param_spec_boolean ("tab-expand", 
                                                                    P_("Tab expand"), 
                                                                    P_("Whether to expand the child's tab or not"),
-                                                                   TRUE,
+                                                                   FALSE,
                                                                    GTK_PARAM_READWRITE));
   gtk_container_class_install_child_property (container_class,
                                              CHILD_PROP_TAB_FILL,
@@ -1087,6 +1111,36 @@ gtk_notebook_init (GtkNotebook *notebook)
   gtk_drag_dest_set_track_motion (GTK_WIDGET (notebook), TRUE);
 }
 
+static void
+gtk_notebook_buildable_init (GtkBuildableIface *iface)
+{
+  iface->add_child = gtk_notebook_buildable_add_child;
+}
+
+static void
+gtk_notebook_buildable_add_child (GtkBuildable  *buildable,
+                                 GtkBuilder    *builder,
+                                 GObject       *child,
+                                 const gchar   *type)
+{
+  GtkNotebook *notebook = GTK_NOTEBOOK (buildable);
+
+  if (type && strcmp (type, "tab") == 0)
+    {
+      GtkWidget * page;
+
+      page = gtk_notebook_get_nth_page (notebook, -1);
+      /* To set the tab label widget, we must have already a child
+       * inside the tab container. */
+      g_assert (page != NULL);
+      gtk_notebook_set_tab_label (notebook, page, GTK_WIDGET (child));
+    }
+  else if (!type)
+    gtk_notebook_append_page (notebook, GTK_WIDGET (child), NULL);
+  else
+    GTK_BUILDER_WARN_INVALID_CHILD_TYPE (notebook, type);
+}
+
 static gboolean
 gtk_notebook_select_page (GtkNotebook *notebook,
                           gboolean     move_focus)
@@ -1409,7 +1463,7 @@ gtk_notebook_destroy (GtkObject *object)
 {
   GtkNotebook *notebook = GTK_NOTEBOOK (object);
   GtkNotebookPrivate *priv = GTK_NOTEBOOK_GET_PRIVATE (notebook);
-  
+
   if (notebook->menu)
     gtk_notebook_popup_disable (notebook);
 
@@ -1480,6 +1534,7 @@ gtk_notebook_set_property (GObject         *object,
       gtk_notebook_set_group (notebook, g_value_get_pointer (value));
       break;
     default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
     }
 }
@@ -2681,7 +2736,6 @@ hide_drag_window (GtkNotebook        *notebook,
       else
        gtk_widget_unparent (page->tab_label);
 
-      gtk_widget_set_parent_window (page->tab_label, widget->window);
       gtk_widget_set_parent (page->tab_label, widget);
       g_object_unref (page->tab_label);
     }
@@ -2704,7 +2758,7 @@ gtk_notebook_stop_reorder (GtkNotebook *notebook)
   else
     page = notebook->cur_page;
 
-  if (!page)
+  if (!page || !page->tab_label)
     return;
 
   priv->pressed_button = -1;
@@ -2719,7 +2773,8 @@ gtk_notebook_stop_reorder (GtkNotebook *notebook)
          element = get_drop_position (notebook, page->pack);
          old_page_num = g_list_position (notebook->children, notebook->focus_tab);
          page_num = reorder_tab (notebook, element, notebook->focus_tab);
-
+          gtk_notebook_child_reordered (notebook, page);
+          
          if (priv->has_scrolled || old_page_num != page_num)
            g_signal_emit (notebook,
                           notebook_signals[PAGE_REORDERED], 0,
@@ -3605,7 +3660,7 @@ gtk_notebook_get_child_property (GtkContainer    *container,
     case CHILD_PROP_TAB_LABEL:
       label = gtk_notebook_get_tab_label (notebook, child);
 
-      if (label && GTK_IS_LABEL (label))
+      if (GTK_IS_LABEL (label))
        g_value_set_string (value, GTK_LABEL (label)->label);
       else
        g_value_set_string (value, NULL);
@@ -3613,7 +3668,7 @@ gtk_notebook_get_child_property (GtkContainer    *container,
     case CHILD_PROP_MENU_LABEL:
       label = gtk_notebook_get_menu_label (notebook, child);
 
-      if (label && GTK_IS_LABEL (label))
+      if (GTK_IS_LABEL (label))
        g_value_set_string (value, GTK_LABEL (label)->label);
       else
        g_value_set_string (value, NULL);
@@ -3988,7 +4043,7 @@ gtk_notebook_real_insert_page (GtkNotebook *notebook,
 
   gtk_widget_freeze_child_notify (child);
 
-  page = g_new0 (GtkNotebookPage, 1);
+  page = g_slice_new0 (GtkNotebookPage);
   page->child = child;
 
   nchildren = g_list_length (notebook->children);
@@ -4267,15 +4322,18 @@ gtk_notebook_real_remove (GtkNotebook *notebook,
   GtkNotebookPage *page;
   GList * next_list;
   gint need_resize = FALSE;
+  GtkWidget *tab_label;
 
   gboolean destroying;
 
   priv = GTK_NOTEBOOK_GET_PRIVATE (notebook);
   destroying = GTK_OBJECT_FLAGS (notebook) & GTK_IN_DESTRUCTION;
   
-  next_list = gtk_notebook_search_page (notebook, list, STEP_PREV, TRUE);
+  next_list = gtk_notebook_search_page (notebook, list, STEP_NEXT, TRUE);
   if (!next_list)
-    next_list = gtk_notebook_search_page (notebook, list, STEP_NEXT, TRUE);
+    next_list = gtk_notebook_search_page (notebook, list, STEP_PREV, TRUE);
+
+  notebook->children = g_list_remove_link (notebook->children, list);
 
   if (notebook->cur_page == list->data)
     { 
@@ -4301,8 +4359,16 @@ gtk_notebook_real_remove (GtkNotebook *notebook,
 
   gtk_widget_unparent (page->child);
 
-  gtk_notebook_remove_tab_label (notebook, page);
-  
+  tab_label = page->tab_label;
+  if (tab_label)
+    {
+      g_object_ref (tab_label);
+      gtk_notebook_remove_tab_label (notebook, page);
+      if (destroying)
+        gtk_widget_destroy (tab_label);
+      g_object_unref (tab_label);
+    } 
+
   if (notebook->menu)
     {
       gtk_container_remove (GTK_CONTAINER (notebook->menu), 
@@ -4312,7 +4378,6 @@ gtk_notebook_real_remove (GtkNotebook *notebook,
   if (!page->default_menu)
     g_object_unref (page->menu_label);
   
-  notebook->children = g_list_remove_link (notebook->children, list);
   g_list_free (list);
 
   if (page->last_focus_child)
@@ -4321,7 +4386,7 @@ gtk_notebook_real_remove (GtkNotebook *notebook,
       page->last_focus_child = NULL;
     }
   
-  g_free (page);
+  g_slice_free (GtkNotebookPage, page);
 
   gtk_notebook_update_labels (notebook);
   if (need_resize)
@@ -4336,6 +4401,9 @@ gtk_notebook_update_labels (GtkNotebook *notebook)
   gchar string[32];
   gint page_num = 1;
 
+  if (!notebook->show_tabs && !notebook->menu)
+    return;
+
   for (list = gtk_notebook_search_page (notebook, NULL, STEP_NEXT, FALSE);
        list;
        list = gtk_notebook_search_page (notebook, list, STEP_NEXT, FALSE))
@@ -4365,9 +4433,9 @@ gtk_notebook_update_labels (GtkNotebook *notebook)
        }
       if (notebook->menu && page->default_menu)
        {
-         if (page->tab_label && GTK_IS_LABEL (page->tab_label))
+         if (GTK_IS_LABEL (page->tab_label))
            gtk_label_set_text (GTK_LABEL (page->menu_label),
-                          GTK_LABEL (page->tab_label)->label);
+                                GTK_LABEL (page->tab_label)->label);
          else
            gtk_label_set_text (GTK_LABEL (page->menu_label), string);
        }
@@ -5867,7 +5935,7 @@ gtk_notebook_menu_item_create (GtkNotebook *notebook,
   page = list->data;
   if (page->default_menu)
     {
-      if (page->tab_label && GTK_IS_LABEL (page->tab_label))
+      if (GTK_IS_LABEL (page->tab_label))
        page->menu_label = gtk_label_new (GTK_LABEL (page->tab_label)->label);
       else
        page->menu_label = gtk_label_new ("");
@@ -6903,10 +6971,10 @@ gtk_notebook_set_tab_label_text (GtkNotebook *notebook,
  * Retrieves the text of the tab label for the page containing
  *    @child.
  *
- * Returns value: the text of the tab label, or %NULL if the
- *                tab label widget is not a #GtkLabel. The
- *                string is owned by the widget and must not
- *                be freed.
+ * Return value: the text of the tab label, or %NULL if the
+ *               tab label widget is not a #GtkLabel. The
+ *               string is owned by the widget and must not
+ *               be freed.
  **/
 G_CONST_RETURN gchar *
 gtk_notebook_get_tab_label_text (GtkNotebook *notebook,
@@ -6919,7 +6987,7 @@ gtk_notebook_get_tab_label_text (GtkNotebook *notebook,
 
   tab_label = gtk_notebook_get_tab_label (notebook, child);
 
-  if (tab_label && GTK_IS_LABEL (tab_label))
+  if (GTK_IS_LABEL (tab_label))
     return gtk_label_get_text (GTK_LABEL (tab_label));
   else
     return NULL;
@@ -7037,11 +7105,11 @@ gtk_notebook_set_menu_label_text (GtkNotebook *notebook,
  * Retrieves the text of the menu label for the page containing
  *    @child.
  *
- * Returns value: the text of the tab label, or %NULL if the
- *                widget does not have a menu label other than
- *                the default menu label, or the menu label widget
- *                is not a #GtkLabel. The string is owned by
- *                the widget and must not be freed.
+ * Return value: the text of the tab label, or %NULL if the
+ *               widget does not have a menu label other than
+ *               the default menu label, or the menu label widget
+ *               is not a #GtkLabel. The string is owned by
+ *               the widget and must not be freed.
  **/
 G_CONST_RETURN gchar *
 gtk_notebook_get_menu_label_text (GtkNotebook *notebook,
@@ -7054,7 +7122,7 @@ gtk_notebook_get_menu_label_text (GtkNotebook *notebook,
  
   menu_label = gtk_notebook_get_menu_label (notebook, child);
 
-  if (menu_label && GTK_IS_LABEL (menu_label))
+  if (GTK_IS_LABEL (menu_label))
     return gtk_label_get_text (GTK_LABEL (menu_label));
   else
     return NULL;
@@ -7270,7 +7338,7 @@ gtk_notebook_set_window_creation_hook (GtkNotebookWindowCreationFunc  func,
  * not be able to exchange tabs with any other notebook.
  * 
  * Since: 2.10
- * Deprecated:2.12: use gtk_notebook_set_group() instead.
+ * Deprecated: 2.12: use gtk_notebook_set_group() instead.
  */
 void
 gtk_notebook_set_group_id (GtkNotebook *notebook,
@@ -7324,7 +7392,7 @@ gtk_notebook_set_group (GtkNotebook *notebook,
  * Return Value: the group identificator, or -1 if none is set.
  *
  * Since: 2.10
- * Deprecated:2.12: use gtk_notebook_get_group() instead.
+ * Deprecated: 2.12: use gtk_notebook_get_group() instead.
  */
 gint
 gtk_notebook_get_group_id (GtkNotebook *notebook)
@@ -7336,7 +7404,7 @@ gtk_notebook_get_group_id (GtkNotebook *notebook)
   priv = GTK_NOTEBOOK_GET_PRIVATE (notebook);
 
   /* substract 1 to get rid of the -1/NULL difference */
-  return GPOINTER_TO_INT (priv->group - 1);
+  return GPOINTER_TO_INT (priv->group) - 1;
 }
 
 /**
@@ -7464,8 +7532,7 @@ gtk_notebook_get_tab_detachable (GtkNotebook *notebook,
  * destination and accept the target "GTK_NOTEBOOK_TAB". The notebook
  * will fill the selection with a GtkWidget** pointing to the child
  * widget that corresponds to the dropped tab.
- *
- * <informalexample><programlisting>
+ * |[
  *  static void
  *  on_drop_zone_drag_data_received (GtkWidget        *widget,
  *                                   GdkDragContext   *context,
@@ -7485,7 +7552,7 @@ gtk_notebook_get_tab_detachable (GtkNotebook *notebook,
  *    process_widget (*child);
  *    gtk_container_remove (GTK_CONTAINER (notebook), *child);
  *  }
- * </programlisting></informalexample>
+ * ]|
  *
  * If you want a notebook to accept drags from other widgets,
  * you will have to set your own DnD code to do it.