]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtktoolbar.c
Change FSF Address
[~andy/gtk] / gtk / gtktoolbar.c
index f6549194a7e241c649270b4cc0a61169047892d8..de5603c622a69b31ebad13f2978b0d9fa083bd29 100644 (file)
@@ -17,9 +17,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 
 /*
 
 #include "gtkarrow.h"
 #include "gtkbindings.h"
-#include "gtkhbox.h"
+#include "gtkcontainerprivate.h"
 #include "gtkimage.h"
 #include "gtklabel.h"
-#include "gtkmainprivate.h"
+#include "gtkmain.h"
 #include "gtkmarshalers.h"
 #include "gtkmenu.h"
 #include "gtkorientable.h"
@@ -52,7 +50,7 @@
 #include "gtkseparatortoolitem.h"
 #include "gtkstock.h"
 #include "gtktoolshell.h"
-#include "gtkvbox.h"
+#include "gtkbox.h"
 #include "gtkprivate.h"
 #include "gtkintl.h"
 #include "gtktypebuiltins.h"
@@ -117,7 +115,6 @@ struct _GtkToolbarPrivate
 
   GdkWindow       *event_window;
 
-  GList           *children;
   GList           *content;
 
   GTimer          *timer;
@@ -217,6 +214,7 @@ static void       gtk_toolbar_get_child_property   (GtkContainer        *contain
                                                    GValue              *value,
                                                    GParamSpec          *pspec);
 static void       gtk_toolbar_finalize             (GObject             *object);
+static void       gtk_toolbar_dispose              (GObject             *object);
 static void       gtk_toolbar_show_all             (GtkWidget           *widget);
 static void       gtk_toolbar_add                  (GtkContainer        *container,
                                                    GtkWidget           *widget);
@@ -227,6 +225,13 @@ static void       gtk_toolbar_forall               (GtkContainer        *contain
                                                    GtkCallback          callback,
                                                    gpointer             callback_data);
 static GType      gtk_toolbar_child_type           (GtkContainer        *container);
+static GtkWidgetPath * gtk_toolbar_get_path_for_child
+                                                  (GtkContainer        *container,
+                                                   GtkWidget           *child);
+static void       gtk_toolbar_invalidate_order    (GtkToolbar           *toolbar);
+
+static void       gtk_toolbar_direction_changed    (GtkWidget           *widget,
+                                                    GtkTextDirection     previous_direction);
 static void       gtk_toolbar_orientation_changed  (GtkToolbar          *toolbar,
                                                    GtkOrientation       orientation);
 static void       gtk_toolbar_real_style_changed   (GtkToolbar          *toolbar,
@@ -366,6 +371,7 @@ gtk_toolbar_class_init (GtkToolbarClass *klass)
   gobject_class->set_property = gtk_toolbar_set_property;
   gobject_class->get_property = gtk_toolbar_get_property;
   gobject_class->finalize = gtk_toolbar_finalize;
+  gobject_class->dispose = gtk_toolbar_dispose;
   
   widget_class->button_press_event = gtk_toolbar_button_press;
   widget_class->draw = gtk_toolbar_draw;
@@ -375,6 +381,8 @@ gtk_toolbar_class_init (GtkToolbarClass *klass)
   widget_class->style_updated = gtk_toolbar_style_updated;
   widget_class->focus = gtk_toolbar_focus;
 
+  gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_TOOL_BAR);
+
   /* need to override the base class function via override_class_handler,
    * because the signal slot is not available in GtkWidgetClass
    */
@@ -389,6 +397,7 @@ gtk_toolbar_class_init (GtkToolbarClass *klass)
   widget_class->unmap = gtk_toolbar_unmap;
   widget_class->popup_menu = gtk_toolbar_popup_menu;
   widget_class->show_all = gtk_toolbar_show_all;
+  widget_class->direction_changed = gtk_toolbar_direction_changed;
   
   container_class->add    = gtk_toolbar_add;
   container_class->remove = gtk_toolbar_remove;
@@ -396,7 +405,8 @@ gtk_toolbar_class_init (GtkToolbarClass *klass)
   container_class->child_type = gtk_toolbar_child_type;
   container_class->get_child_property = gtk_toolbar_get_child_property;
   container_class->set_child_property = gtk_toolbar_set_child_property;
-  
+  container_class->get_path_for_child = gtk_toolbar_get_path_for_child;
+
   klass->orientation_changed = gtk_toolbar_orientation_changed;
   klass->style_changed = gtk_toolbar_real_style_changed;
   
@@ -846,16 +856,11 @@ gtk_toolbar_draw (GtkWidget *widget,
   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
   GtkToolbarPrivate *priv = toolbar->priv;
   GtkStyleContext *context;
-  GtkStateFlags state;
   GList *list;
   guint border_width;
 
   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
   context = gtk_widget_get_style_context (widget);
-  state = gtk_widget_get_state_flags (widget);
-
-  gtk_style_context_save (context);
-  gtk_style_context_set_state (context, state);
 
   gtk_render_background (context, cr, border_width, border_width,
                          gtk_widget_get_allocated_width (widget) - 2 * border_width,
@@ -875,8 +880,6 @@ gtk_toolbar_draw (GtkWidget *widget,
                                priv->arrow_button,
                                cr);
 
-  gtk_style_context_restore (context);
-
   return FALSE;
 }
 
@@ -2527,34 +2530,34 @@ gtk_toolbar_remove (GtkContainer *container,
 
 static void
 gtk_toolbar_forall (GtkContainer *container,
-                   gboolean      include_internals,
-                   GtkCallback   callback,
-                   gpointer      callback_data)
+                    gboolean      include_internals,
+                    GtkCallback   callback,
+                    gpointer      callback_data)
 {
   GtkToolbar *toolbar = GTK_TOOLBAR (container);
   GtkToolbarPrivate *priv = toolbar->priv;
   GList *list;
-  
+
   g_return_if_fail (callback != NULL);
-  
+
   list = priv->content;
   while (list)
     {
       ToolbarContent *content = list->data;
       GList *next = list->next;
-      
+
       if (include_internals || !toolbar_content_is_placeholder (content))
-       {
-         GtkWidget *child = toolbar_content_get_widget (content);
-         
-         if (child)
-           callback (child, callback_data);
-       }
-      
+        {
+          GtkWidget *child = toolbar_content_get_widget (content);
+
+          if (child)
+            callback (child, callback_data);
+        }
+
       list = next;
     }
-  
-  if (include_internals)
+
+  if (include_internals && priv->arrow_button)
     callback (priv->arrow_button, callback_data);
 }
 
@@ -2647,7 +2650,7 @@ menu_position_func (GtkMenu  *menu,
                                                   gtk_widget_get_window (priv->arrow_button));
   if (monitor_num < 0)
     monitor_num = 0;
-  gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
+  gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
 
   gtk_widget_get_allocation (priv->arrow_button, &allocation);
 
@@ -2734,7 +2737,7 @@ gtk_toolbar_button_press (GtkWidget      *toolbar,
 {
   GtkWidget *window;
 
-  if (event->button == 3)
+  if (gdk_event_triggers_context_menu ((GdkEvent *) event))
     {
       gboolean return_value;
 
@@ -3121,34 +3124,38 @@ gtk_toolbar_get_drop_index (GtkToolbar *toolbar,
 }
 
 static void
-gtk_toolbar_finalize (GObject *object)
+gtk_toolbar_dispose (GObject *object)
 {
-  GList *list;
   GtkToolbar *toolbar = GTK_TOOLBAR (object);
   GtkToolbarPrivate *priv = toolbar->priv;
 
   if (priv->arrow_button)
-    gtk_widget_unparent (priv->arrow_button);
-
-  for (list = priv->content; list != NULL; list = list->next)
     {
-      ToolbarContent *content = list->data;
-
-      toolbar_content_free (content);
+      gtk_widget_unparent (priv->arrow_button);
+      priv->arrow_button = NULL;
     }
-  
-  g_list_free (priv->content);
-  g_list_free (priv->children);
-  
-  g_timer_destroy (priv->timer);
-  
+
   if (priv->menu)
     {
       g_signal_handlers_disconnect_by_func (priv->menu,
                                             menu_deactivated, toolbar);
       gtk_widget_destroy (GTK_WIDGET (priv->menu));
+      priv->menu = NULL;
     }
 
+ G_OBJECT_CLASS (gtk_toolbar_parent_class)->dispose (object);
+}
+
+static void
+gtk_toolbar_finalize (GObject *object)
+{
+  GtkToolbar *toolbar = GTK_TOOLBAR (object);
+  GtkToolbarPrivate *priv = toolbar->priv;
+
+  g_list_free_full (priv->content, (GDestroyNotify)toolbar_content_free);
+
+  g_timer_destroy (priv->timer);
+
   if (priv->idle_id)
     g_source_remove (priv->idle_id);
 
@@ -3253,6 +3260,7 @@ struct _ToolbarContent
   ItemState      state;
 
   GtkToolItem   *item;
+  GtkAllocation  allocation;
   GtkAllocation  start_allocation;
   GtkAllocation  goal_allocation;
   guint          is_placeholder : 1;
@@ -3260,6 +3268,16 @@ struct _ToolbarContent
   guint          has_menu : 2;
 };
 
+static void
+toolbar_item_visiblity_notify_cb (GObject *obj,
+                                  GParamSpec *pspec,
+                                  gpointer user_data)
+{
+  GtkToolbar *toolbar = user_data;
+
+  gtk_toolbar_invalidate_order (toolbar);
+}
+
 static ToolbarContent *
 toolbar_content_new_tool_item (GtkToolbar  *toolbar,
                               GtkToolItem *item,
@@ -3274,11 +3292,15 @@ toolbar_content_new_tool_item (GtkToolbar  *toolbar,
   content->state = NOT_ALLOCATED;
   content->item = item;
   content->is_placeholder = is_placeholder;
-  
-  gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (toolbar));
 
   priv->content = g_list_insert (priv->content, content, pos);
-  
+
+  gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (toolbar));
+  gtk_toolbar_invalidate_order (toolbar);
+
+  g_signal_connect (content->item, "notify::visible",
+                    G_CALLBACK (toolbar_item_visiblity_notify_cb), toolbar);
+
   if (!is_placeholder)
     {
       priv->num_children++;
@@ -3298,8 +3320,13 @@ toolbar_content_remove (ToolbarContent *content,
 {
   GtkToolbarPrivate *priv = toolbar->priv;
 
+  gtk_toolbar_invalidate_order (toolbar);
   gtk_widget_unparent (GTK_WIDGET (content->item));
 
+  g_signal_handlers_disconnect_by_func (content->item,
+                                        toolbar_item_visiblity_notify_cb,
+                                        toolbar);
+
   priv->content = g_list_remove (priv->content, content);
 
   if (!toolbar_content_is_placeholder (content))
@@ -3466,7 +3493,7 @@ static void
 toolbar_content_get_allocation (ToolbarContent *content,
                                GtkAllocation  *allocation)
 {
-  gtk_widget_get_allocation (GTK_WIDGET (content->item), allocation);
+  *allocation = content->allocation;
 }
 
 static void
@@ -3513,6 +3540,7 @@ static void
 toolbar_content_size_allocate (ToolbarContent *content,
                               GtkAllocation  *allocation)
 {
+  content->allocation = *allocation;
   gtk_widget_size_allocate (GTK_WIDGET (content->item),
                             allocation);
 }
@@ -3829,3 +3857,126 @@ toolbar_rebuild_menu (GtkToolShell *shell)
   
   gtk_widget_queue_resize (GTK_WIDGET (shell));
 }
+
+typedef struct _CountingData CountingData;
+struct _CountingData {
+  GtkWidget *widget;
+  gboolean found;
+  guint before;
+  guint after;
+};
+
+static void
+count_widget_position (GtkWidget *widget,
+                       gpointer   data)
+{
+  CountingData *count = data;
+
+  if (!gtk_widget_get_visible (widget))
+    return;
+
+  if (count->widget == widget)
+    count->found = TRUE;
+  else if (count->found)
+    count->after++;
+  else
+    count->before++;
+}
+
+static guint
+gtk_toolbar_get_visible_position (GtkToolbar *toolbar,
+                                  GtkWidget  *child)
+{
+  CountingData count = { child, FALSE, 0, 0 };
+
+  if (child == (GtkWidget*)toolbar->priv->highlight_tool_item)
+    return 0;
+
+  /* foreach iterates in visible order */
+  gtk_container_forall (GTK_CONTAINER (toolbar),
+                        count_widget_position,
+                        &count);
+
+  g_assert (count.found);
+
+  if (toolbar->priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
+      gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL)
+    return count.after;
+
+  return count.before;
+}
+
+static void
+add_widget_to_path (gpointer data,
+                    gpointer user_data)
+{
+  GtkWidget *widget = data;
+  GtkWidgetPath *path = user_data;
+
+  if (gtk_widget_get_visible (widget))
+    gtk_widget_path_append_for_widget (path, widget);
+}
+
+static GtkWidgetPath *
+gtk_toolbar_get_path_for_child (GtkContainer *container,
+                                GtkWidget    *child)
+{
+  GtkWidgetPath *path;
+  GtkToolbar *toolbar;
+  GtkToolbarPrivate *priv;
+  GtkWidgetPath *sibling_path;
+  gint vis_index;
+  GList *children;
+
+  toolbar = GTK_TOOLBAR (container);
+  priv = toolbar->priv;
+
+  /* build a path for all the visible children;
+   * get_children works in visible order
+   */
+  sibling_path = gtk_widget_path_new ();
+  children = _gtk_container_get_all_children (container);
+
+  if (priv->orientation != GTK_ORIENTATION_HORIZONTAL ||
+      gtk_widget_get_direction (GTK_WIDGET (toolbar)) != GTK_TEXT_DIR_RTL)
+    children = g_list_reverse (children);
+
+  g_list_foreach (children, add_widget_to_path, sibling_path);
+  g_list_free (children);
+
+  path = gtk_widget_path_copy (gtk_widget_get_path (GTK_WIDGET (container)));
+  if (gtk_widget_get_visible (child))
+    {
+      vis_index = gtk_toolbar_get_visible_position (toolbar, child);
+
+      if (vis_index < gtk_widget_path_length (sibling_path))
+        gtk_widget_path_append_with_siblings (path,
+                                              sibling_path,
+                                              vis_index);
+      else
+        gtk_widget_path_append_for_widget (path, child);
+    }
+  else
+    gtk_widget_path_append_for_widget (path, child);
+
+  gtk_widget_path_unref (sibling_path);
+  return path;
+}
+
+static void
+gtk_toolbar_invalidate_order (GtkToolbar *toolbar)
+{
+  gtk_container_forall (GTK_CONTAINER (toolbar),
+                        (GtkCallback) gtk_widget_reset_style,
+                        NULL);
+}
+
+static void
+gtk_toolbar_direction_changed (GtkWidget        *widget,
+                               GtkTextDirection  previous_direction)
+{
+  GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->direction_changed (widget, previous_direction);
+
+  gtk_toolbar_invalidate_order (GTK_TOOLBAR (widget));
+}
+