]> Pileus Git - ~andy/gtk/blobdiff - tests/testtoolbar.c
Show the size column by default in the file chooser
[~andy/gtk] / tests / testtoolbar.c
index 6ad3b8e8486241f4585d5c0276f7f41754e49627..42e5feaea58c39b29b175836bb78403867500f96 100644 (file)
@@ -19,7 +19,8 @@
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */
-
+#undef GTK_DISABLE_DEPRECATED
+#include "config.h"
 #include <gtk/gtk.h>
 #include "prop-editor.h"
 
@@ -116,8 +117,8 @@ set_visible_func(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
 
   gtk_tree_model_get (model, iter, 0, &tool_item, -1);
 
-  g_object_get (G_OBJECT (tool_item), "visible", &visible, NULL);
-  g_object_set (G_OBJECT (cell), "active", visible, NULL);
+  g_object_get (tool_item, "visible", &visible, NULL);
+  g_object_set (cell, "active", visible, NULL);
   g_object_unref (tool_item);
 }
 
@@ -134,8 +135,8 @@ visibile_toggled(GtkCellRendererToggle *cell, const gchar *path_str,
   gtk_tree_model_get_iter (model, &iter, path);
 
   gtk_tree_model_get (model, &iter, 0, &tool_item, -1);
-  g_object_get (G_OBJECT (tool_item), "visible", &visible, NULL);
-  g_object_set (G_OBJECT (tool_item), "visible", !visible, NULL);
+  g_object_get (tool_item, "visible", &visible, NULL);
+  g_object_set (tool_item, "visible", !visible, NULL);
   g_object_unref (tool_item);
 
   gtk_tree_model_row_changed (model, path, &iter);
@@ -150,7 +151,7 @@ set_expand_func(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
 
   gtk_tree_model_get (model, iter, 0, &tool_item, -1);
 
-  g_object_set (G_OBJECT (cell), "active", tool_item->expand, NULL);
+  g_object_set (cell, "active", gtk_tool_item_get_expand (tool_item), NULL);
   g_object_unref (tool_item);
 }
 
@@ -166,7 +167,7 @@ expand_toggled(GtkCellRendererToggle *cell, const gchar *path_str,
   gtk_tree_model_get_iter (model, &iter, path);
 
   gtk_tree_model_get (model, &iter, 0, &tool_item, -1);
-  gtk_tool_item_set_expand (tool_item, !tool_item->expand);
+  gtk_tool_item_set_expand (tool_item, !gtk_tool_item_get_expand (tool_item));
   g_object_unref (tool_item);
 
   gtk_tree_model_row_changed (model, path, &iter);
@@ -174,20 +175,20 @@ expand_toggled(GtkCellRendererToggle *cell, const gchar *path_str,
 }
 
 static void
-set_pack_end_func(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
-                 GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
+set_homogeneous_func(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
+                    GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
 {
   GtkToolItem *tool_item;
 
   gtk_tree_model_get (model, iter, 0, &tool_item, -1);
 
-  g_object_set (G_OBJECT (cell), "active", tool_item->pack_end, NULL);
+  g_object_set (cell, "active", gtk_tool_item_get_homogeneous (tool_item), NULL);
   g_object_unref (tool_item);
 }
 
 static void
-pack_end_toggled(GtkCellRendererToggle *cell, const gchar *path_str,
-                GtkTreeModel *model)
+homogeneous_toggled(GtkCellRendererToggle *cell, const gchar *path_str,
+                   GtkTreeModel *model)
 {
   GtkTreePath *path;
   GtkTreeIter iter;
@@ -197,28 +198,29 @@ pack_end_toggled(GtkCellRendererToggle *cell, const gchar *path_str,
   gtk_tree_model_get_iter (model, &iter, path);
 
   gtk_tree_model_get (model, &iter, 0, &tool_item, -1);
-  gtk_tool_item_set_pack_end (tool_item, !tool_item->pack_end);
+  gtk_tool_item_set_homogeneous (tool_item, !gtk_tool_item_get_homogeneous (tool_item));
   g_object_unref (tool_item);
 
   gtk_tree_model_row_changed (model, path, &iter);
   gtk_tree_path_free (path);
 }
 
+
 static void
-set_homogeneous_func(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
-                    GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
+set_important_func(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
+                  GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
 {
   GtkToolItem *tool_item;
 
   gtk_tree_model_get (model, iter, 0, &tool_item, -1);
 
-  g_object_set (G_OBJECT (cell), "active", tool_item->homogeneous, NULL);
+  g_object_set (cell, "active", gtk_tool_item_get_is_important (tool_item), NULL);
   g_object_unref (tool_item);
 }
 
 static void
-homogeneous_toggled(GtkCellRendererToggle *cell, const gchar *path_str,
-                   GtkTreeModel *model)
+important_toggled(GtkCellRendererToggle *cell, const gchar *path_str,
+                 GtkTreeModel *model)
 {
   GtkTreePath *path;
   GtkTreeIter iter;
@@ -228,7 +230,7 @@ homogeneous_toggled(GtkCellRendererToggle *cell, const gchar *path_str,
   gtk_tree_model_get_iter (model, &iter, path);
 
   gtk_tree_model_get (model, &iter, 0, &tool_item, -1);
-  gtk_tool_item_set_homogeneous (tool_item, !tool_item->homogeneous);
+  gtk_tool_item_set_is_important (tool_item, !gtk_tool_item_get_is_important (tool_item));
   g_object_unref (tool_item);
 
   gtk_tree_model_row_changed (model, path, &iter);
@@ -268,20 +270,20 @@ create_items_list (GtkWidget **tree_view_p)
                                              set_expand_func, NULL, NULL);
 
   cell = gtk_cell_renderer_toggle_new ();
-  g_signal_connect (cell, "toggled", G_CALLBACK (pack_end_toggled),
+  g_signal_connect (cell, "toggled", G_CALLBACK (homogeneous_toggled),
                    list_store);
   gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (tree_view),
-                                             -1, "Pack End",
+                                             -1, "Homogeneous",
                                              cell,
-                                             set_pack_end_func, NULL, NULL);
+                                             set_homogeneous_func, NULL,NULL);
 
   cell = gtk_cell_renderer_toggle_new ();
-  g_signal_connect (cell, "toggled", G_CALLBACK (homogeneous_toggled),
+  g_signal_connect (cell, "toggled", G_CALLBACK (important_toggled),
                    list_store);
   gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (tree_view),
-                                             -1, "Homogeneous",
+                                             -1, "Important",
                                              cell,
-                                             set_homogeneous_func, NULL,NULL);
+                                             set_important_func, NULL,NULL);
 
   g_object_unref (list_store);
 
@@ -378,11 +380,128 @@ rtl_toggled (GtkCheckButton *check)
     gtk_widget_set_default_direction (GTK_TEXT_DIR_LTR);
 }
 
+typedef struct
+{
+  int x;
+  int y;
+} MenuPositionData;
+
+static void
+position_function (GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer user_data)
+{
+  /* Do not do this in your own code */
+
+  MenuPositionData *position_data = user_data;
+
+  if (x)
+    *x = position_data->x;
+
+  if (y)
+    *y = position_data->y;
+
+  if (push_in)
+    *push_in = FALSE;
+}
+
+static gboolean
+popup_context_menu (GtkToolbar *toolbar, gint x, gint y, gint button_number)
+{
+  MenuPositionData position_data;
+  
+  GtkMenu *menu = GTK_MENU (gtk_menu_new ());
+  int i;
+
+  for (i = 0; i < 5; i++)
+    {
+      GtkWidget *item;
+      gchar *label = g_strdup_printf ("Item _%d", i);
+      item = gtk_menu_item_new_with_mnemonic (label);
+      gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
+    }
+  gtk_widget_show_all (GTK_WIDGET (menu));
+
+  if (button_number != -1)
+    {
+      position_data.x = x;
+      position_data.y = y;
+      
+      gtk_menu_popup (menu, NULL, NULL, position_function,
+                     &position_data, button_number, gtk_get_current_event_time());
+    }
+  else
+    gtk_menu_popup (menu, NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time());
+
+  return TRUE;
+}
+
+static GtkToolItem *drag_item = NULL;
+
+static gboolean
+toolbar_drag_motion (GtkToolbar     *toolbar,
+                    GdkDragContext *context,
+                    gint            x,
+                    gint            y,
+                    guint           time,
+                    gpointer        null)
+{
+  gint index;
+  
+  if (!drag_item)
+    {
+      drag_item = gtk_tool_button_new (NULL, "A quite long button");
+      gtk_object_sink (GTK_OBJECT (g_object_ref (drag_item)));
+    }
+  
+  gdk_drag_status (context, GDK_ACTION_MOVE, time);
+
+  index = gtk_toolbar_get_drop_index (toolbar, x, y);
+  
+  gtk_toolbar_set_drop_highlight_item (toolbar, drag_item, index);
+  
+  return TRUE;
+}
+
+static void
+toolbar_drag_leave (GtkToolbar     *toolbar,
+                   GdkDragContext *context,
+                   guint           time,
+                   gpointer        null)
+{
+  if (drag_item)
+    {
+      g_object_unref (drag_item);
+      drag_item = NULL;
+    }
+  
+  gtk_toolbar_set_drop_highlight_item (toolbar, NULL, 0);
+}
+
+static gboolean
+timeout_cb (GtkWidget *widget)
+{
+  static gboolean sensitive = TRUE;
+  
+  sensitive = !sensitive;
+  
+  gtk_widget_set_sensitive (widget, sensitive);
+  
+  return TRUE;
+}
+
+static gboolean
+timeout_cb1 (GtkWidget *widget)
+{
+       static gboolean sensitive = TRUE;
+       sensitive = !sensitive;
+       gtk_widget_set_sensitive (widget, sensitive);
+       return TRUE;
+}
+
 gint
 main (gint argc, gchar **argv)
 {
   GtkWidget *window, *toolbar, *table, *treeview, *scrolled_window;
-  GtkWidget *hbox, *checkbox, *option_menu, *menu;
+  GtkWidget *hbox, *hbox1, *hbox2, *checkbox, *option_menu, *menu;
   gint i;
   static const gchar *toolbar_styles[] = { "icons", "text", "both (vertical)",
                                           "both (horizontal)" };
@@ -392,6 +511,7 @@ main (gint argc, gchar **argv)
   GtkWidget *menuitem;
   GtkWidget *button;
   GtkWidget *label;
+  GIcon *gicon;
   GSList *group;
   
   gtk_init (&argc, &argv);
@@ -407,24 +527,30 @@ main (gint argc, gchar **argv)
   gtk_table_attach (GTK_TABLE (table), toolbar,
                    0,2, 0,1, GTK_FILL|GTK_EXPAND, GTK_FILL, 0, 0);
 
-  hbox = gtk_hbox_new (FALSE, 5);
-  gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
-  gtk_table_attach (GTK_TABLE (table), hbox,
+  hbox1 = gtk_hbox_new (FALSE, 3);
+  gtk_container_set_border_width (GTK_CONTAINER (hbox1), 5);
+  gtk_table_attach (GTK_TABLE (table), hbox1,
                    1,2, 1,2, GTK_FILL|GTK_EXPAND, GTK_FILL, 0, 0);
 
+  hbox2 = gtk_hbox_new (FALSE, 2);
+  gtk_container_set_border_width (GTK_CONTAINER (hbox2), 5);
+  gtk_table_attach (GTK_TABLE (table), hbox2,
+                   1,2, 2,3, GTK_FILL|GTK_EXPAND, GTK_FILL, 0, 0);
+
   checkbox = gtk_check_button_new_with_mnemonic("_Vertical");
-  gtk_box_pack_start (GTK_BOX (hbox), checkbox, FALSE, FALSE, 0);
+  gtk_box_pack_start (GTK_BOX (hbox1), checkbox, FALSE, FALSE, 0);
   g_signal_connect (checkbox, "toggled",
                    G_CALLBACK (change_orientation), toolbar);
 
   checkbox = gtk_check_button_new_with_mnemonic("_Show Arrow");
-  gtk_box_pack_start (GTK_BOX (hbox), checkbox, FALSE, FALSE, 0);
+  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (checkbox), TRUE);
+  gtk_box_pack_start (GTK_BOX (hbox1), checkbox, FALSE, FALSE, 0);
   g_signal_connect (checkbox, "toggled",
                    G_CALLBACK (change_show_arrow), toolbar);
 
   checkbox = gtk_check_button_new_with_mnemonic("_Set Toolbar Style:");
   g_signal_connect (checkbox, "toggled", G_CALLBACK (set_toolbar_style_toggled), toolbar);
-  gtk_box_pack_start (GTK_BOX (hbox), checkbox, FALSE, FALSE, 0);
+  gtk_box_pack_start (GTK_BOX (hbox1), checkbox, FALSE, FALSE, 0);
   
   option_menu = gtk_option_menu_new();
   gtk_widget_set_sensitive (option_menu, FALSE);  
@@ -442,13 +568,13 @@ main (gint argc, gchar **argv)
   gtk_option_menu_set_menu (GTK_OPTION_MENU (option_menu), menu);
   gtk_option_menu_set_history (GTK_OPTION_MENU (option_menu),
                               GTK_TOOLBAR (toolbar)->style);
-  gtk_box_pack_start (GTK_BOX (hbox), option_menu, FALSE, FALSE, 0);
+  gtk_box_pack_start (GTK_BOX (hbox2), option_menu, FALSE, FALSE, 0);
   g_signal_connect (option_menu, "changed",
                    G_CALLBACK (change_toolbar_style), toolbar);
 
   checkbox = gtk_check_button_new_with_mnemonic("_Set Icon Size:"); 
   g_signal_connect (checkbox, "toggled", G_CALLBACK (set_icon_size_toggled), toolbar);
-  gtk_box_pack_start (GTK_BOX (hbox), checkbox, FALSE, FALSE, 0);
+  gtk_box_pack_start (GTK_BOX (hbox2), checkbox, FALSE, FALSE, 0);
 
   option_menu = gtk_option_menu_new();
   g_object_set_data (G_OBJECT (checkbox), "option-menu", option_menu);
@@ -465,7 +591,7 @@ main (gint argc, gchar **argv)
   gtk_widget_show (menuitem);
 
   gtk_option_menu_set_menu (GTK_OPTION_MENU (option_menu), menu);
-  gtk_box_pack_start (GTK_BOX (hbox), option_menu, FALSE, FALSE, 0);
+  gtk_box_pack_start (GTK_BOX (hbox2), option_menu, FALSE, FALSE, 0);
   g_signal_connect (option_menu, "changed",
                    G_CALLBACK (icon_size_history_changed), toolbar);
   
@@ -473,7 +599,7 @@ main (gint argc, gchar **argv)
   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
                                  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
   gtk_table_attach (GTK_TABLE (table), scrolled_window,
-                   1,2, 2,3, GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 0, 0);
+                   1,2, 3,4, GTK_FILL|GTK_EXPAND, GTK_FILL|GTK_EXPAND, 0, 0);
 
   store = create_items_list (&treeview);
   gtk_container_add (GTK_CONTAINER (scrolled_window), treeview);
@@ -483,17 +609,48 @@ main (gint argc, gchar **argv)
   gtk_tool_button_set_label (GTK_TOOL_BUTTON (item), NULL);
   add_item_to_list (store, item, "New");
   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
+  gdk_threads_add_timeout (3000, (GSourceFunc) timeout_cb, item);
+  gtk_tool_item_set_expand (item, TRUE);
+
+  menu = gtk_menu_new ();
+  for (i = 0; i < 20; i++)
+    {
+      char *text;
+      text = g_strdup_printf ("Menuitem %d", i);
+      menuitem = gtk_menu_item_new_with_label (text);
+      g_free (text);
+      gtk_widget_show (menuitem);
+      gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
+    }
 
-  item = gtk_tool_button_new_from_stock (GTK_STOCK_OPEN);
+  item = gtk_menu_tool_button_new_from_stock (GTK_STOCK_OPEN);
+  gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (item), menu);
   add_item_to_list (store, item, "Open");
   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
+  gdk_threads_add_timeout (3000, (GSourceFunc) timeout_cb1, item);
+  menu = gtk_menu_new ();
+  for (i = 0; i < 20; i++)
+    {
+      char *text;
+      text = g_strdup_printf ("A%d", i);
+      menuitem = gtk_menu_item_new_with_label (text);
+      g_free (text);
+      gtk_widget_show (menuitem);
+      gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
+    }
 
+  item = gtk_menu_tool_button_new_from_stock (GTK_STOCK_GO_BACK);
+  gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (item), menu);
+  add_item_to_list (store, item, "BackWithHistory");
+  gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
   item = gtk_separator_tool_item_new ();
   add_item_to_list (store, item, "-----");    
   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
   
   item = gtk_tool_button_new_from_stock (GTK_STOCK_REFRESH);
-  add_item_to_list (store, item, "Refresh");  
+  add_item_to_list (store, item, "Refresh");
   g_signal_connect (item, "clicked", G_CALLBACK (reload_clicked), NULL);
   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
 
@@ -520,31 +677,48 @@ main (gint argc, gchar **argv)
   g_signal_connect (item, "toggled", G_CALLBACK (bold_toggled), NULL);
   add_item_to_list (store, item, "Bold");  
   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
+  gtk_widget_set_sensitive (GTK_WIDGET (item), FALSE);
 
   item = gtk_separator_tool_item_new ();
   add_item_to_list (store, item, "-----");  
   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
-
+  gtk_tool_item_set_expand (item, TRUE);
+  gtk_separator_tool_item_set_draw (GTK_SEPARATOR_TOOL_ITEM (item), FALSE);
+  g_assert (gtk_toolbar_get_nth_item (GTK_TOOLBAR (toolbar), 0) != 0);
+  
   item = gtk_radio_tool_button_new_from_stock (NULL, GTK_STOCK_JUSTIFY_LEFT);
   group = gtk_radio_tool_button_get_group (GTK_RADIO_TOOL_BUTTON (item));
   add_item_to_list (store, item, "Left");
   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
   
-  item = gtk_radio_tool_button_new_from_stock (group, GTK_STOCK_JUSTIFY_CENTER);  make_prop_editor (G_OBJECT (item));
+  
+  item = gtk_radio_tool_button_new_from_stock (group, GTK_STOCK_JUSTIFY_CENTER);
+  make_prop_editor (G_OBJECT (item));
 
-  group = gtk_radio_tool_button_get_group (GTK_RADIO_TOOL_BUTTON (item));  
+  group = gtk_radio_tool_button_get_group (GTK_RADIO_TOOL_BUTTON (item));
   add_item_to_list (store, item, "Center");
   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
 
   item = gtk_radio_tool_button_new_from_stock (group, GTK_STOCK_JUSTIFY_RIGHT);
-  
   add_item_to_list (store, item, "Right");
   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
 
+  item = gtk_tool_button_new (gtk_image_new_from_file ("apple-red.png"), "_Apple");
+  add_item_to_list (store, item, "Apple");
+  gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
+  gtk_tool_button_set_use_underline (GTK_TOOL_BUTTON (item), TRUE);
+
+  gicon = g_content_type_get_icon ("video/ogg");
+  image = gtk_image_new_from_gicon (gicon, GTK_ICON_SIZE_LARGE_TOOLBAR);
+  g_object_unref (gicon);
+  item = gtk_tool_button_new (image, "Video");
+  add_item_to_list (store, item, "Video");
+  gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, -1);
+
   hbox = gtk_hbox_new (FALSE, 5);
   gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
   gtk_table_attach (GTK_TABLE (table), hbox,
-                   1,2, 3,4, GTK_FILL|GTK_EXPAND, GTK_FILL, 0, 0);
+                   1,2, 4,5, GTK_FILL|GTK_EXPAND, GTK_FILL, 0, 0);
 
   button = gtk_button_new_with_label ("Drag me to the toolbar");
   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
@@ -570,12 +744,20 @@ main (gint argc, gchar **argv)
   gtk_drag_dest_set (toolbar, GTK_DEST_DEFAULT_DROP,
                     target_table, G_N_ELEMENTS (target_table),
                     GDK_ACTION_MOVE);
+  g_signal_connect (toolbar, "drag_motion",
+                   G_CALLBACK (toolbar_drag_motion), NULL);
+  g_signal_connect (toolbar, "drag_leave",
+                   G_CALLBACK (toolbar_drag_leave), NULL);
   g_signal_connect (toolbar, "drag_drop",
                    G_CALLBACK (toolbar_drag_drop), label);
 
   gtk_widget_show_all (window);
 
   make_prop_editor (G_OBJECT (toolbar));
+
+  g_signal_connect (window, "delete_event", G_CALLBACK (gtk_main_quit), NULL);
+  
+  g_signal_connect (toolbar, "popup_context_menu", G_CALLBACK (popup_context_menu), NULL);
   
   gtk_main ();