* Boston, MA 02111-1307, USA.
*/
+#include <config.h>
#include "gtkcombobox.h"
+
+#include "gtkarrow.h"
+#include "gtkbindings.h"
#include "gtkcelllayout.h"
+#include "gtkcellrenderertext.h"
#include "gtkcellview.h"
#include "gtkcellviewmenuitem.h"
-
-#include "gtktreeselection.h"
+#include "gtkeventbox.h"
#include "gtkframe.h"
+#include "gtkliststore.h"
+#include "gtkmain.h"
+#include "gtkmenu.h"
#include "gtktogglebutton.h"
+#include "gtktreeselection.h"
#include "gtkvseparator.h"
-#include "gtkarrow.h"
-#include "gtkmenu.h"
-#include "gtkmain.h"
-#include "gtkeventbox.h"
-#include "gtkcellrenderertext.h"
-#include "gtkbindings.h"
-#include "gtkliststore.h"
#include "gtkwindow.h"
#include <gdk/gdkkeysyms.h>
guint inserted_id;
guint deleted_id;
+ guint reordered_id;
+ guint changed_id;
gint width;
GSList *cells;
- guint changed_id;
-
guint popup_in_progress : 1;
+ guint destroying : 1;
};
/* While debugging this evil code, I have learned that
GValue *value,
GParamSpec *spec);
+static void gtk_combo_box_state_changed (GtkWidget *widget,
+ GtkStateType previous);
static void gtk_combo_box_style_set (GtkWidget *widget,
- GtkStyle *previous_style,
- gpointer data);
+ GtkStyle *previous);
static void gtk_combo_box_button_toggled (GtkWidget *widget,
gpointer data);
static void gtk_combo_box_add (GtkContainer *container,
GtkWidget *widget);
+static void gtk_combo_box_remove (GtkContainer *container,
+ GtkWidget *widget);
static ComboCellInfo *gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
GtkCellRenderer *cell);
static void gtk_combo_box_set_popup_widget (GtkComboBox *combo_box,
GtkWidget *popup);
+static void gtk_combo_box_menu_position_below (GtkMenu *menu,
+ gint *x,
+ gint *y,
+ gint *push_in,
+ gpointer user_data);
+static void gtk_combo_box_menu_position_over (GtkMenu *menu,
+ gint *x,
+ gint *y,
+ gint *push_in,
+ gpointer user_data);
static void gtk_combo_box_menu_position (GtkMenu *menu,
gint *x,
gint *y,
static void gtk_combo_box_remeasure (GtkComboBox *combo_box);
static void gtk_combo_box_unset_model (GtkComboBox *combo_box);
-static void gtk_combo_box_set_model_internal (GtkComboBox *combo_box);
static void gtk_combo_box_size_request (GtkWidget *widget,
GtkRequisition *requisition);
GdkEventExpose *event);
static gboolean gtk_combo_box_scroll_event (GtkWidget *widget,
GdkEventScroll *event);
+static void gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
+ gint index);
+static gboolean gtk_combo_box_key_press (GtkWidget *widget,
+ GdkEventKey *event,
+ gpointer data);
+
+/* listening to the model */
+static void gtk_combo_box_model_row_inserted (GtkTreeModel *model,
+ GtkTreePath *path,
+ GtkTreeIter *iter,
+ gpointer user_data);
+static void gtk_combo_box_model_row_deleted (GtkTreeModel *model,
+ GtkTreePath *path,
+ gpointer user_data);
+static void gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
+ GtkTreePath *path,
+ GtkTreeIter *iter,
+ gint *new_order,
+ gpointer user_data);
+static void gtk_combo_box_model_row_changed (GtkTreeModel *model,
+ GtkTreePath *path,
+ GtkTreeIter *iter,
+ gpointer data);
/* list */
+static void gtk_combo_box_list_position (GtkComboBox *combo_box,
+ gint *x,
+ gint *y,
+ gint *width,
+ gint *height);
static void gtk_combo_box_list_setup (GtkComboBox *combo_box);
static void gtk_combo_box_list_destroy (GtkComboBox *combo_box);
/* menu */
static void gtk_combo_box_menu_setup (GtkComboBox *combo_box,
- gboolean add_childs);
+ gboolean add_children);
static void gtk_combo_box_menu_fill (GtkComboBox *combo_box);
static void gtk_combo_box_menu_destroy (GtkComboBox *combo_box);
static void gtk_combo_box_menu_row_deleted (GtkTreeModel *model,
GtkTreePath *path,
gpointer user_data);
+static void gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
+ GtkTreePath *path,
+ GtkTreeIter *iter,
+ gint *new_order,
+ gpointer user_data);
static void gtk_combo_box_menu_row_changed (GtkTreeModel *model,
GtkTreePath *path,
GtkTreeIter *iter,
gpointer data);
+static gboolean gtk_combo_box_menu_key_press (GtkWidget *widget,
+ GdkEventKey *event,
+ gpointer data);
/* cell layout */
static void gtk_combo_box_cell_layout_pack_start (GtkCellLayout *layout,
static gboolean gtk_combo_box_mnemonic_activate (GtkWidget *widget,
gboolean group_cycling);
+static void cell_view_sync_cells (GtkComboBox *combo_box,
+ GtkCellView *cell_view);
GType
gtk_combo_box_get_type (void)
container_class = (GtkContainerClass *)klass;
container_class->forall = gtk_combo_box_forall;
container_class->add = gtk_combo_box_add;
+ container_class->remove = gtk_combo_box_remove;
widget_class = (GtkWidgetClass *)klass;
widget_class->size_allocate = gtk_combo_box_size_allocate;
widget_class->expose_event = gtk_combo_box_expose_event;
widget_class->scroll_event = gtk_combo_box_scroll_event;
widget_class->mnemonic_activate = gtk_combo_box_mnemonic_activate;
+ widget_class->style_set = gtk_combo_box_style_set;
+ widget_class->state_changed = gtk_combo_box_state_changed;
gtk_object_class = (GtkObjectClass *)klass;
gtk_object_class->destroy = gtk_combo_box_destroy;
G_PARAM_READWRITE));
gtk_widget_class_install_style_property (widget_class,
- g_param_spec_boolean ("appearance",
- P_("ComboBox appareance"),
- P_("ComboBox appearance, where TRUE means Windows-style."),
+ g_param_spec_boolean ("appears-as-list",
+ P_("Appears as list"),
+ P_("Whether combobox dropdowns should look like lists rather than menus"),
FALSE,
G_PARAM_READWRITE));
{
combo_box->priv = GTK_COMBO_BOX_GET_PRIVATE (combo_box);
- g_signal_connect (combo_box, "style_set",
- G_CALLBACK (gtk_combo_box_style_set), NULL);
-
combo_box->priv->cell_view = gtk_cell_view_new ();
- gtk_container_add (GTK_CONTAINER (combo_box), combo_box->priv->cell_view);
+ gtk_widget_set_parent (combo_box->priv->cell_view, GTK_WIDGET (combo_box));
+ GTK_BIN (combo_box)->child = combo_box->priv->cell_view;
gtk_widget_show (combo_box->priv->cell_view);
combo_box->priv->width = 0;
}
static void
-gtk_combo_box_style_set (GtkWidget *widget,
- GtkStyle *previous_style,
- gpointer data)
+gtk_combo_box_state_changed (GtkWidget *widget,
+ GtkStateType previous)
{
- gboolean appearance;
GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
- gtk_widget_queue_resize (widget);
+ if (GTK_WIDGET_REALIZED (widget))
+ {
+ if (combo_box->priv->tree_view && combo_box->priv->cell_view)
+ gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
+ &widget->style->base[GTK_WIDGET_STATE (widget)]);
+ }
+
+ gtk_widget_queue_draw (widget);
+}
+
+static void
+gtk_combo_box_check_appearance (GtkComboBox *combo_box)
+{
+ gboolean appears_as_list;
/* if wrap_width > 0, then we are in grid-mode and forced to use
* unix style
*/
if (combo_box->priv->wrap_width)
- return;
-
- gtk_widget_style_get (widget,
- "appearance", &appearance,
- NULL);
+ appears_as_list = FALSE;
+ else
+ gtk_widget_style_get (GTK_WIDGET (combo_box),
+ "appears-as-list", &appears_as_list,
+ NULL);
- /* TRUE is windows style */
- if (appearance)
+ if (appears_as_list)
{
/* Destroy all the menu mode widgets, if they exist. */
if (GTK_IS_MENU (combo_box->priv->popup_widget))
}
}
+static void
+gtk_combo_box_style_set (GtkWidget *widget,
+ GtkStyle *previous)
+{
+ GtkComboBox *combo_box = GTK_COMBO_BOX (widget);
+
+ gtk_combo_box_check_appearance (combo_box);
+
+ if (combo_box->priv->tree_view && combo_box->priv->cell_view)
+ gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
+ &widget->style->base[GTK_WIDGET_STATE (widget)]);
+}
+
static void
gtk_combo_box_button_toggled (GtkWidget *widget,
gpointer data)
GtkComboBox *combo_box = GTK_COMBO_BOX (container);
if (combo_box->priv->cell_view && combo_box->priv->cell_view->parent)
- gtk_container_remove (container, combo_box->priv->cell_view);
-
- (* GTK_CONTAINER_CLASS (parent_class)->add) (container, widget);
+ {
+ gtk_widget_unparent (combo_box->priv->cell_view);
+ GTK_BIN (container)->child = NULL;
+ gtk_widget_queue_resize (GTK_WIDGET (container));
+ }
+
+ gtk_widget_set_parent (widget, GTK_WIDGET (container));
+ GTK_BIN (container)->child = widget;
if (combo_box->priv->cell_view &&
widget != combo_box->priv->cell_view)
}
}
+static void
+gtk_combo_box_remove (GtkContainer *container,
+ GtkWidget *widget)
+{
+ GtkComboBox *combo_box = GTK_COMBO_BOX (container);
+ gboolean appears_as_list;
+
+ gtk_widget_unparent (widget);
+ GTK_BIN (container)->child = NULL;
+
+ if (combo_box->priv->destroying)
+ return;
+
+ gtk_widget_queue_resize (GTK_WIDGET (container));
+
+ if (!combo_box->priv->tree_view)
+ appears_as_list = FALSE;
+ else
+ appears_as_list = TRUE;
+
+ if (appears_as_list)
+ gtk_combo_box_list_destroy (combo_box);
+ else if (GTK_IS_MENU (combo_box->priv->popup_widget))
+ {
+ gtk_combo_box_menu_destroy (combo_box);
+ gtk_menu_detach (GTK_MENU (combo_box->priv->popup_widget));
+ combo_box->priv->popup_widget = NULL;
+ }
+
+ if (!combo_box->priv->cell_view)
+ {
+ combo_box->priv->cell_view = gtk_cell_view_new ();
+ gtk_widget_set_parent (combo_box->priv->cell_view, GTK_WIDGET (container));
+ GTK_BIN (container)->child = combo_box->priv->cell_view;
+
+ gtk_widget_show (combo_box->priv->cell_view);
+ gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
+ combo_box->priv->model);
+ cell_view_sync_cells (combo_box, GTK_CELL_VIEW (combo_box->priv->cell_view));
+ }
+
+
+ if (appears_as_list)
+ gtk_combo_box_list_setup (combo_box);
+ else
+ gtk_combo_box_menu_setup (combo_box, TRUE);
+
+ gtk_combo_box_set_active_internal (combo_box, combo_box->priv->active_item);
+}
+
static ComboCellInfo *
gtk_combo_box_get_cell_info (GtkComboBox *combo_box,
GtkCellRenderer *cell)
if (combo_box->priv->popup_window)
{
gtk_widget_destroy (combo_box->priv->popup_window);
- combo_box->priv->popup_window = combo_box->priv->popup_frame = NULL;
+ combo_box->priv->popup_window = NULL;
+ combo_box->priv->popup_frame = NULL;
}
combo_box->priv->popup_widget = popup;
if (!combo_box->priv->popup_window)
{
combo_box->priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
+ gtk_window_set_resizable (GTK_WINDOW (combo_box->priv->popup_window), FALSE);
gtk_window_set_screen (GTK_WINDOW (combo_box->priv->popup_window),
gtk_widget_get_screen (GTK_WIDGET (combo_box)));
combo_box->priv->popup_frame = gtk_frame_new (NULL);
gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->popup_frame),
- GTK_SHADOW_NONE);
+ GTK_SHADOW_ETCHED_IN);
gtk_container_add (GTK_CONTAINER (combo_box->priv->popup_window),
combo_box->priv->popup_frame);
+
gtk_widget_show (combo_box->priv->popup_frame);
}
}
static void
-gtk_combo_box_menu_position (GtkMenu *menu,
- gint *x,
- gint *y,
- gint *push_in,
- gpointer user_data)
+gtk_combo_box_menu_position_below (GtkMenu *menu,
+ gint *x,
+ gint *y,
+ gint *push_in,
+ gpointer user_data)
{
gint sx, sy;
GtkWidget *child;
GtkRequisition req;
GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
-
+
/* FIXME: is using the size request here broken? */
- child = GTK_BIN (combo_box)->child;
+ child = GTK_BIN (combo_box)->child;
+
+ gdk_window_get_origin (child->window, &sx, &sy);
+
+ gtk_widget_size_request (GTK_WIDGET (menu), &req);
+
+ if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_LTR)
+ *x = sx;
+ else
+ *x = sx + child->allocation.width - req.width;
+ *y = sy + child->allocation.height;
+
+ if (GTK_WIDGET_NO_WINDOW (child))
+ {
+ *x += child->allocation.x;
+ *y += child->allocation.y;
+ }
+
+ *push_in = TRUE;
+}
- gdk_window_get_origin (child->window, &sx, &sy);
+static void
+gtk_combo_box_menu_position_over (GtkMenu *menu,
+ gint *x,
+ gint *y,
+ gboolean *push_in,
+ gpointer user_data)
+{
+ GtkComboBox *combo_box;
+ GtkWidget *active;
+ GtkWidget *child;
+ GtkWidget *widget;
+ GtkRequisition requisition;
+ GList *children;
+ gint screen_width;
+ gint menu_xpos;
+ gint menu_ypos;
+ gint menu_width;
+
+ g_return_if_fail (GTK_IS_COMBO_BOX (user_data));
+
+ combo_box = GTK_COMBO_BOX (user_data);
+ widget = GTK_WIDGET (combo_box);
- gtk_widget_size_request (GTK_WIDGET (menu), &req);
+ gtk_widget_get_child_requisition (GTK_WIDGET (menu), &requisition);
+ menu_width = requisition.width;
- if (gtk_widget_get_direction (GTK_WIDGET (combo_box)) == GTK_TEXT_DIR_RTL)
- *x = sx;
- else
- *x = sx + child->allocation.width - req.width;
- *y = sy + child->allocation.height;
+ active = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
+ gdk_window_get_origin (widget->window, &menu_xpos, &menu_ypos);
+
+ menu_xpos += widget->allocation.x;
+ menu_ypos += widget->allocation.y + widget->allocation.height / 2 - 2;
+
+ if (active != NULL)
+ {
+ gtk_widget_get_child_requisition (active, &requisition);
+ menu_ypos -= requisition.height / 2;
+ }
- if (GTK_WIDGET_NO_WINDOW (child))
+ children = GTK_MENU_SHELL (combo_box->priv->popup_widget)->children;
+ while (children)
{
- *x += child->allocation.x;
- *y += child->allocation.y;
+ child = children->data;
+
+ if (active == child)
+ break;
+
+ if (GTK_WIDGET_VISIBLE (child))
+ {
+ gtk_widget_get_child_requisition (child, &requisition);
+ menu_ypos -= requisition.height;
+ }
+
+ children = children->next;
}
+ if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
+ menu_xpos = menu_xpos + widget->allocation.width - menu_width;
+
+ /* Clamp the position on screen */
+ screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget));
+
+ if (menu_xpos < 0)
+ menu_xpos = 0;
+ else if ((menu_xpos + menu_width) > screen_width)
+ menu_xpos -= ((menu_xpos + menu_width) - screen_width);
+
+ *x = menu_xpos;
+ *y = menu_ypos;
+
*push_in = TRUE;
}
+static void
+gtk_combo_box_menu_position (GtkMenu *menu,
+ gint *x,
+ gint *y,
+ gint *push_in,
+ gpointer user_data)
+{
+ GtkComboBox *combo_box;
+ GtkWidget *menu_item;
+
+ combo_box = GTK_COMBO_BOX (user_data);
+
+ if (combo_box->priv->wrap_width > 0 || combo_box->priv->cell_view == NULL)
+ gtk_combo_box_menu_position_below (menu, x, y, push_in, user_data);
+ else
+ {
+ menu_item = gtk_menu_get_active (GTK_MENU (combo_box->priv->popup_widget));
+ if (menu_item)
+ gtk_menu_shell_select_item (GTK_MENU_SHELL (combo_box->priv->popup_widget),
+ menu_item);
+
+ gtk_combo_box_menu_position_over (menu, x, y, push_in, user_data);
+ }
+
+}
+
+static void
+gtk_combo_box_list_position (GtkComboBox *combo_box,
+ gint *x,
+ gint *y,
+ gint *width,
+ gint *height)
+{
+ GtkWidget *sample;
+ GdkScreen *screen;
+ gint monitor_num;
+ GdkRectangle monitor;
+ GtkRequisition popup_req;
+
+ sample = GTK_BIN (combo_box)->child;
+
+ *width = sample->allocation.width;
+ gtk_widget_size_request (combo_box->priv->popup_window, &popup_req);
+ *height = popup_req.height;
+
+ gdk_window_get_origin (sample->window, x, y);
+
+ if (combo_box->priv->cell_view_frame)
+ {
+ *x -= GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
+ GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
+ *width += 2 * (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
+ GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
+ }
+
+ if (GTK_WIDGET_NO_WINDOW (sample))
+ {
+ *x += sample->allocation.x;
+ *y += sample->allocation.y;
+ }
+
+ screen = gtk_widget_get_screen (GTK_WIDGET (combo_box));
+ monitor_num = gdk_screen_get_monitor_at_window (screen,
+ GTK_WIDGET (combo_box)->window);
+ gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
+
+ if (*x < monitor.x)
+ *x = monitor.x;
+ else if (*x + *width > monitor.x + monitor.width)
+ *x = monitor.x + monitor.width - *width;
+
+ if (*y + sample->allocation.height + *height <= monitor.y + monitor.height)
+ *y += sample->allocation.height;
+ else
+ *y -= *height;
+}
+
/**
* gtk_combo_box_popup:
* @combo_box: a #GtkComboBox
gtk_combo_box_popup (GtkComboBox *combo_box)
{
gint x, y, width, height;
- GtkWidget *sample;
-
+
g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
if (GTK_WIDGET_MAPPED (combo_box->priv->popup_widget))
if (GTK_IS_MENU (combo_box->priv->popup_widget))
{
- if (combo_box->priv->active_item != -1)
- {
- GList *childs;
-
- childs = gtk_container_get_children (GTK_CONTAINER (combo_box->priv->popup_widget));
- gtk_menu_shell_select_item (GTK_MENU_SHELL (combo_box->priv->popup_widget),
- g_list_nth_data (childs, combo_box->priv->active_item));
- g_list_free (childs);
- }
+ gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
+ combo_box->priv->active_item);
gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
- NULL, NULL,
- gtk_combo_box_menu_position, combo_box,
- 0, 0);
+ NULL, NULL,
+ gtk_combo_box_menu_position, combo_box,
+ 0, 0);
return;
}
- /* size it */
- sample = GTK_BIN (combo_box)->child;
-
- width = sample->allocation.width;
- height = sample->allocation.height;
-
- gdk_window_get_origin (sample->window,
- &x, &y);
-
- if (combo_box->priv->cell_view_frame)
- {
- x -= GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
- GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness;
- width += 2 * (GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
- GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
- }
-
- gtk_widget_set_size_request (combo_box->priv->popup_window,
- width, -1);
+ gtk_widget_show_all (combo_box->priv->popup_frame);
+ gtk_combo_box_list_position (combo_box, &x, &y, &width, &height);
- if (GTK_WIDGET_NO_WINDOW (sample))
- {
- x += sample->allocation.x;
- y += sample->allocation.y;
- }
-
- gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window),
- x, y + height);
+ gtk_widget_set_size_request (combo_box->priv->popup_window, width, -1);
+ gtk_window_move (GTK_WINDOW (combo_box->priv->popup_window), x, y);
/* popup */
- gtk_widget_show_all (combo_box->priv->popup_window);
+ gtk_widget_show (combo_box->priv->popup_window);
gtk_widget_grab_focus (combo_box->priv->popup_window);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (combo_box->priv->button),
GtkRequisition req;
if (combo_box->priv->cell_view)
- gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
+ gtk_cell_view_get_size_of_row (GTK_CELL_VIEW (combo_box->priv->cell_view),
path, &req);
else
req.width = 0;
if (combo_box->priv->cell_view)
{
- GtkRequisition sep_req, arrow_req;
+ GtkRequisition button_req, sep_req, arrow_req;
gint border_width, xthickness, ythickness;
+ gtk_widget_size_request (combo_box->priv->button, &button_req);
border_width = GTK_CONTAINER (combo_box->priv->button)->border_width;
xthickness = combo_box->priv->button->style->xthickness;
ythickness = combo_box->priv->button->style->ythickness;
width = bin_req.width + sep_req.width + arrow_req.width;
- height += border_width + 1 + xthickness * 2 + 4;
- width += border_width + 1 + ythickness * 2 + 4;
+ height += border_width + 1 + ythickness * 2 + 4;
+ width += border_width + 1 + xthickness * 2 + 4;
requisition->width = width;
requisition->height = height;
else
{
/* list mode */
- GtkRequisition button_req;
+ GtkRequisition button_req, frame_req;
/* sample + frame */
*requisition = bin_req;
if (combo_box->priv->cell_view_frame)
{
+ gtk_widget_size_request (combo_box->priv->cell_view_frame, &frame_req);
requisition->width += 2 *
(GTK_CONTAINER (combo_box->priv->cell_view_frame)->border_width +
GTK_WIDGET (combo_box->priv->cell_view_frame)->style->xthickness);
width = allocation->width - (border_width + 1 + xthickness * 2 + 4);
- /* handle the childs */
+ /* handle the children */
gtk_widget_size_request (combo_box->priv->arrow, &req);
child.width = req.width;
child.height = allocation->height - 2 * (child.y - allocation->y);
static void
gtk_combo_box_unset_model (GtkComboBox *combo_box)
{
- if (combo_box->priv->inserted_id != -1)
+ if (combo_box->priv->model)
{
g_signal_handler_disconnect (combo_box->priv->model,
combo_box->priv->inserted_id);
- combo_box->priv->inserted_id = -1;
- }
- if (combo_box->priv->deleted_id != -1)
- {
g_signal_handler_disconnect (combo_box->priv->model,
combo_box->priv->deleted_id);
- combo_box->priv->deleted_id = -1;
- }
- if (combo_box->priv->changed_id != -1)
- {
+ g_signal_handler_disconnect (combo_box->priv->model,
+ combo_box->priv->reordered_id);
g_signal_handler_disconnect (combo_box->priv->model,
combo_box->priv->changed_id);
- combo_box->priv->changed_id = -1;
}
/* menu mode */
gtk_container_foreach (GTK_CONTAINER (combo_box->priv->popup_widget),
(GtkCallback)gtk_widget_destroy, NULL);
}
-}
-static void
-gtk_combo_box_set_model_internal (GtkComboBox *combo_box)
-{
- if (!combo_box->priv->tree_view)
- {
- /* menu mode */
- combo_box->priv->inserted_id =
- g_signal_connect (combo_box->priv->model, "row_inserted",
- G_CALLBACK (gtk_combo_box_menu_row_inserted),
- combo_box);
- combo_box->priv->deleted_id =
- g_signal_connect (combo_box->priv->model, "row_deleted",
- G_CALLBACK (gtk_combo_box_menu_row_deleted),
- combo_box);
- combo_box->priv->changed_id =
- g_signal_connect (combo_box->priv->model, "row_changed",
- G_CALLBACK (gtk_combo_box_menu_row_changed),
- combo_box);
- }
- else
+ if (combo_box->priv->model)
{
- /* list mode */
- gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
- combo_box->priv->model);
-
- combo_box->priv->changed_id =
- g_signal_connect (combo_box->priv->model, "row_changed",
- G_CALLBACK (gtk_combo_box_list_row_changed),
- combo_box);
+ g_object_unref (G_OBJECT (combo_box->priv->model));
+ combo_box->priv->model = NULL;
}
}
static void
gtk_combo_box_menu_setup (GtkComboBox *combo_box,
- gboolean add_childs)
+ gboolean add_children)
{
GtkWidget *box;
- /* Unset any existing model. */
- gtk_combo_box_unset_model (combo_box);
-
if (combo_box->priv->cell_view)
{
combo_box->priv->button = gtk_toggle_button_new ();
g_signal_connect (combo_box->priv->button, "toggled",
G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
+ g_signal_connect_after (combo_box->priv->button, "key_press_event",
+ G_CALLBACK (gtk_combo_box_key_press), combo_box);
gtk_widget_set_parent (combo_box->priv->button,
GTK_BIN (combo_box)->child->parent);
combo_box->priv->button = gtk_toggle_button_new ();
g_signal_connect (combo_box->priv->button, "toggled",
G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
+ g_signal_connect_after (combo_box, "key_press_event",
+ G_CALLBACK (gtk_combo_box_key_press), combo_box);
gtk_widget_set_parent (combo_box->priv->button,
GTK_BIN (combo_box)->child->parent);
/* create our funky menu */
box = gtk_menu_new ();
+ g_signal_connect (box, "key_press_event",
+ G_CALLBACK (gtk_combo_box_menu_key_press), combo_box);
gtk_combo_box_set_popup_widget (combo_box, box);
- /* set the models */
- gtk_combo_box_set_model_internal (combo_box);
-
/* add items */
- if (add_childs)
+ if (add_children)
gtk_combo_box_menu_fill (combo_box);
}
GTK_CELL_VIEW (GTK_BIN (tmp)->child));
gtk_menu_shell_append (GTK_MENU_SHELL (menu), tmp);
+
+ if (combo_box->priv->wrap_width)
+ gtk_combo_box_relayout_item (combo_box, i);
+
gtk_widget_show (tmp);
gtk_tree_path_free (path);
static void
gtk_combo_box_menu_destroy (GtkComboBox *combo_box)
{
- /* disconnect signal handlers */
- gtk_combo_box_unset_model (combo_box);
-
g_signal_handlers_disconnect_matched (combo_box->priv->button,
G_SIGNAL_MATCH_DATA,
0, 0, NULL,
combo_box->priv->arrow = NULL;
}
- /* changing the popup window will unref the menu and the childs */
+ /* changing the popup window will unref the menu and the children */
}
/*
static void
gtk_combo_box_relayout (GtkComboBox *combo_box)
{
- gint i, items;
GList *list, *j;
GtkWidget *menu;
- /* ensure we are in menu style */
+ /* do nothing unless we are in menu style */
if (combo_box->priv->tree_view)
- gtk_combo_box_list_destroy (combo_box);
+ return;
menu = combo_box->priv->popup_widget;
- if (!GTK_IS_MENU_SHELL (menu))
- {
- gtk_combo_box_menu_setup (combo_box, FALSE);
- menu = combo_box->priv->popup_widget;
- }
-
/* get rid of all children */
g_return_if_fail (GTK_IS_MENU_SHELL (menu));
g_list_free (list);
/* and relayout */
- items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
-
- for (i = 0; i < items; i++)
- {
- GtkWidget *tmp;
- GtkTreePath *path;
-
- path = gtk_tree_path_new_from_indices (i, -1);
- tmp = gtk_cell_view_menu_item_new_from_model (combo_box->priv->model,
- path);
-
- g_signal_connect (tmp, "activate",
- G_CALLBACK (gtk_combo_box_menu_item_activate),
- combo_box);
-
- cell_view_sync_cells (combo_box, GTK_CELL_VIEW (GTK_BIN (tmp)->child));
-
- gtk_menu_shell_insert (GTK_MENU_SHELL (menu), tmp, i);
-
- if (combo_box->priv->wrap_width)
- gtk_combo_box_relayout_item (combo_box, i);
-
- gtk_widget_show (tmp);
-
- gtk_tree_path_free (path);
- }
+ gtk_combo_box_menu_fill (combo_box);
}
/* callbacks */
if (event->type == GDK_BUTTON_PRESS && event->button == 1)
{
combo_box->priv->popup_in_progress = TRUE;
+
+ gtk_menu_set_active (GTK_MENU (combo_box->priv->popup_widget),
+ combo_box->priv->active_item);
+
gtk_menu_popup (GTK_MENU (combo_box->priv->popup_widget),
NULL, NULL,
gtk_combo_box_menu_position, combo_box,
gtk_combo_box_set_active (combo_box, index);
}
+static void
+gtk_combo_box_model_row_inserted (GtkTreeModel *model,
+ GtkTreePath *path,
+ GtkTreeIter *iter,
+ gpointer user_data)
+{
+ GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
+ gint index = gtk_tree_path_get_indices (path)[0];
+
+ if (combo_box->priv->active_item >= index)
+ combo_box->priv->active_item++;
+
+ if (!combo_box->priv->tree_view)
+ gtk_combo_box_menu_row_inserted (model, path, iter, user_data);
+}
+
+static void
+gtk_combo_box_model_row_deleted (GtkTreeModel *model,
+ GtkTreePath *path,
+ gpointer user_data)
+{
+ GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
+ gint index = gtk_tree_path_get_indices (path)[0];
+
+ if (!combo_box->priv->tree_view)
+ gtk_combo_box_menu_row_deleted (model, path, user_data);
+
+ if (index == combo_box->priv->active_item)
+ {
+ gint items = gtk_tree_model_iter_n_children (model, NULL);
+
+ if (items == 0)
+ gtk_combo_box_set_active_internal (combo_box, -1);
+ else if (index == items)
+ gtk_combo_box_set_active_internal (combo_box, index - 1);
+ else
+ gtk_combo_box_set_active_internal (combo_box, index);
+ }
+ else if (combo_box->priv->active_item > index)
+ combo_box->priv->active_item--;
+}
+
+static void
+gtk_combo_box_model_rows_reordered (GtkTreeModel *model,
+ GtkTreePath *path,
+ GtkTreeIter *iter,
+ gint *new_order,
+ gpointer user_data)
+{
+ GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
+ gint items = gtk_tree_model_iter_n_children (model, NULL);
+ gint i;
+
+ for (i = 0; i < items; i++)
+ if (new_order[i] == combo_box->priv->active_item)
+ {
+ combo_box->priv->active_item = i;
+ break;
+ }
+
+ if (!combo_box->priv->tree_view)
+ gtk_combo_box_menu_rows_reordered (model, path, iter, new_order, user_data);
+}
+
+static void
+gtk_combo_box_model_row_changed (GtkTreeModel *model,
+ GtkTreePath *path,
+ GtkTreeIter *iter,
+ gpointer user_data)
+{
+ GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
+ gint index = gtk_tree_path_get_indices (path)[0];
+
+ if (index == combo_box->priv->active_item &&
+ combo_box->priv->cell_view)
+ gtk_widget_queue_resize (GTK_WIDGET (combo_box->priv->cell_view));
+
+ if (combo_box->priv->tree_view)
+ gtk_combo_box_list_row_changed (model, path, iter, user_data);
+ else
+ gtk_combo_box_menu_row_changed (model, path, iter, user_data);
+}
+
+
static void
gtk_combo_box_menu_row_inserted (GtkTreeModel *model,
GtkTreePath *path,
GtkTreePath *path,
gpointer user_data)
{
- gint index, items;
+ gint index;
GtkWidget *menu;
GtkWidget *item;
GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
return;
index = gtk_tree_path_get_indices (path)[0];
- items = gtk_tree_model_iter_n_children (model, NULL);
-
- if (gtk_combo_box_get_active (combo_box) == index)
- gtk_combo_box_set_active (combo_box, index + 1 % items);
menu = combo_box->priv->popup_widget;
g_return_if_fail (GTK_IS_MENU (menu));
gtk_container_remove (GTK_CONTAINER (menu), item);
}
+static void
+gtk_combo_box_menu_rows_reordered (GtkTreeModel *model,
+ GtkTreePath *path,
+ GtkTreeIter *iter,
+ gint *new_order,
+ gpointer user_data)
+{
+ GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
+
+ gtk_combo_box_relayout (combo_box);
+}
+
static void
gtk_combo_box_menu_row_changed (GtkTreeModel *model,
GtkTreePath *path,
GSList *i;
GtkTreeSelection *sel;
- /* Unset any existing model. */
- gtk_combo_box_unset_model (combo_box);
-
combo_box->priv->button = gtk_toggle_button_new ();
gtk_widget_set_parent (combo_box->priv->button,
GTK_BIN (combo_box)->child->parent);
G_CALLBACK (gtk_combo_box_list_button_pressed), combo_box);
g_signal_connect (combo_box->priv->button, "toggled",
G_CALLBACK (gtk_combo_box_button_toggled), combo_box);
+ g_signal_connect_after (combo_box, "key_press_event",
+ G_CALLBACK (gtk_combo_box_key_press), combo_box);
combo_box->priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
gtk_container_add (GTK_CONTAINER (combo_box->priv->button),
gtk_frame_set_shadow_type (GTK_FRAME (combo_box->priv->cell_view_frame),
GTK_SHADOW_IN);
- g_object_set (G_OBJECT (combo_box->priv->cell_view),
- "background", "white",
- "background_set", TRUE,
- NULL);
+ gtk_cell_view_set_background_color (GTK_CELL_VIEW (combo_box->priv->cell_view),
+ >K_WIDGET (combo_box)->style->base[GTK_WIDGET_STATE (combo_box)]);
gtk_widget_show (combo_box->priv->cell_view_frame);
}
gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (combo_box->priv->tree_view),
FALSE);
-
+ if (combo_box->priv->model)
+ gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
+ combo_box->priv->model);
+
g_signal_connect (combo_box->priv->tree_view, "button_press_event",
G_CALLBACK (gtk_combo_box_list_button_pressed),
combo_box);
gtk_tree_view_append_column (GTK_TREE_VIEW (combo_box->priv->tree_view),
combo_box->priv->column);
- /* set the models */
- gtk_combo_box_set_model_internal (combo_box);
-
/* sync up */
for (i = combo_box->priv->cells; i; i = i->next)
{
gtk_combo_box_list_destroy (GtkComboBox *combo_box)
{
/* disconnect signals */
- gtk_combo_box_unset_model (combo_box);
-
g_signal_handlers_disconnect_matched (combo_box->priv->tree_view,
G_SIGNAL_MATCH_DATA,
0, 0, NULL, NULL, combo_box);
if (GTK_WIDGET_HAS_GRAB (combo_box->priv->popup_window))
{
gtk_grab_remove (combo_box->priv->popup_window);
+ gdk_keyboard_ungrab (GDK_CURRENT_TIME);
gdk_pointer_ungrab (GDK_CURRENT_TIME);
}
}
}
static gboolean
-gtk_combo_box_list_key_press (GtkWidget *widget,
- GdkEventKey *event,
- gpointer data)
+gtk_combo_box_key_press (GtkWidget *widget,
+ GdkEventKey *event,
+ gpointer data)
{
GtkComboBox *combo_box = GTK_COMBO_BOX (data);
+ guint state = event->state & gtk_accelerator_get_default_mod_mask ();
+ gint items = gtk_tree_model_iter_n_children (combo_box->priv->model, NULL);
+ gint index = gtk_combo_box_get_active (combo_box);
+ gint new_index;
- if ((event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
- event->keyval == GDK_space || event->keyval == GDK_KP_Space) ||
- event->keyval == GDK_Escape)
+ if ((event->keyval == GDK_Down || event->keyval == GDK_KP_Down) &&
+ state == GDK_MOD1_MASK)
{
- if (event->keyval != GDK_Escape)
- {
- gboolean ret;
- GtkTreeIter iter;
- GtkTreeModel *model = NULL;
- GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
+ gtk_combo_box_popup (combo_box);
- ret = gtk_tree_selection_get_selected (sel, &model, &iter);
- if (ret)
- {
- GtkTreePath *path;
-
- path = gtk_tree_model_get_path (model, &iter);
- if (path)
- {
- gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
- gtk_tree_path_free (path);
- }
- }
- }
- else
- /* reset active item -- this is incredibly lame and ugly */
- gtk_combo_box_set_active (combo_box,
- gtk_combo_box_get_active (combo_box));
+ return TRUE;
+ }
+
+ switch (event->keyval)
+ {
+ case GDK_Down:
+ case GDK_KP_Down:
+ new_index = index + 1;
+ break;
+ case GDK_Up:
+ case GDK_KP_Up:
+ new_index = index - 1;
+ break;
+ case GDK_Page_Up:
+ case GDK_KP_Page_Up:
+ case GDK_Home:
+ case GDK_KP_Home:
+ new_index = 0;
+ break;
+ case GDK_Page_Down:
+ case GDK_KP_Page_Down:
+ case GDK_End:
+ case GDK_KP_End:
+ new_index = items - 1;
+ break;
+ default:
+ return FALSE;
+ }
+
+ gtk_combo_box_set_active (combo_box, CLAMP (new_index, 0, items - 1));
+ return TRUE;
+}
+
+static gboolean
+gtk_combo_box_menu_key_press (GtkWidget *widget,
+ GdkEventKey *event,
+ gpointer data)
+{
+ GtkComboBox *combo_box = GTK_COMBO_BOX (data);
+ guint state = event->state & gtk_accelerator_get_default_mod_mask ();
+
+ if ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) &&
+ state == GDK_MOD1_MASK)
+ {
gtk_combo_box_popdown (combo_box);
return TRUE;
}
+
+ return FALSE;
+}
+
+static gboolean
+gtk_combo_box_list_key_press (GtkWidget *widget,
+ GdkEventKey *event,
+ gpointer data)
+{
+ GtkComboBox *combo_box = GTK_COMBO_BOX (data);
+ guint state = event->state & gtk_accelerator_get_default_mod_mask ();
+
+ if (event->keyval == GDK_Escape ||
+ ((event->keyval == GDK_Up || event->keyval == GDK_KP_Up) &&
+ state == GDK_MOD1_MASK))
+ {
+ /* reset active item -- this is incredibly lame and ugly */
+ gtk_combo_box_set_active (combo_box,
+ gtk_combo_box_get_active (combo_box));
+
+ gtk_combo_box_popdown (combo_box);
+
+ return TRUE;
+ }
+
+ if (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter ||
+ event->keyval == GDK_space || event->keyval == GDK_KP_Space)
+ {
+ gboolean ret;
+ GtkTreeIter iter;
+ GtkTreeModel *model = NULL;
+ GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (combo_box->priv->tree_view));
+
+ ret = gtk_tree_selection_get_selected (sel, &model, &iter);
+ if (ret)
+ {
+ GtkTreePath *path;
+
+ path = gtk_tree_model_get_path (model, &iter);
+ if (path)
+ {
+ gtk_combo_box_set_active (combo_box, gtk_tree_path_get_indices (path)[0]);
+ gtk_tree_path_free (path);
+ }
+ }
+
+ gtk_combo_box_popdown (combo_box);
+
+ return TRUE;
+ }
return FALSE;
}
if (width != combo_box->priv->wrap_width)
{
combo_box->priv->wrap_width = width;
-
+
+ gtk_combo_box_check_appearance (combo_box);
gtk_combo_box_relayout (combo_box);
- gtk_combo_box_style_set (combo_box, NULL, NULL);
g_object_notify (G_OBJECT (combo_box), "wrap_width");
}
gtk_combo_box_set_active (GtkComboBox *combo_box,
gint index)
{
- GtkTreePath *path;
-
g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
/* -1 means "no item selected" */
g_return_if_fail (index >= -1);
if (combo_box->priv->active_item == index)
return;
+
+ gtk_combo_box_set_active_internal (combo_box, index);
+}
+
+static void
+gtk_combo_box_set_active_internal (GtkComboBox *combo_box,
+ gint index)
+{
+ GtkTreePath *path;
combo_box->priv->active_item = index;
path = gtk_tree_path_new_from_indices (index, -1);
if (combo_box->priv->tree_view)
- gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view), path, NULL, FALSE);
+ gtk_tree_view_set_cursor (GTK_TREE_VIEW (combo_box->priv->tree_view), path, NULL, FALSE);
else
{
GtkMenu *menu = GTK_MENU (combo_box->priv->popup_widget);
}
if (combo_box->priv->cell_view)
- gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), path);
+ gtk_cell_view_set_displayed_row (GTK_CELL_VIEW (combo_box->priv->cell_view), path);
gtk_tree_path_free (path);
}
g_return_if_fail (GTK_IS_COMBO_BOX (combo_box));
g_return_if_fail (GTK_IS_TREE_MODEL (model));
+ if (model == combo_box->priv->model)
+ return;
+
if (combo_box->priv->model)
- {
- gtk_combo_box_unset_model (combo_box);
- g_object_unref (G_OBJECT (combo_box->priv->model));
- }
+ gtk_combo_box_unset_model (combo_box);
combo_box->priv->model = model;
g_object_ref (G_OBJECT (combo_box->priv->model));
- gtk_combo_box_set_model_internal (combo_box);
- if (!combo_box->priv->tree_view && combo_box->priv->popup_widget)
- gtk_combo_box_menu_fill (combo_box);
+ combo_box->priv->inserted_id =
+ g_signal_connect (combo_box->priv->model, "row_inserted",
+ G_CALLBACK (gtk_combo_box_model_row_inserted),
+ combo_box);
+ combo_box->priv->deleted_id =
+ g_signal_connect (combo_box->priv->model, "row_deleted",
+ G_CALLBACK (gtk_combo_box_model_row_deleted),
+ combo_box);
+ combo_box->priv->reordered_id =
+ g_signal_connect (combo_box->priv->model, "rows_reordered",
+ G_CALLBACK (gtk_combo_box_model_rows_reordered),
+ combo_box);
+ combo_box->priv->changed_id =
+ g_signal_connect (combo_box->priv->model, "row_changed",
+ G_CALLBACK (gtk_combo_box_model_row_changed),
+ combo_box);
+
+ if (combo_box->priv->tree_view)
+ {
+ /* list mode */
+ gtk_tree_view_set_model (GTK_TREE_VIEW (combo_box->priv->tree_view),
+ combo_box->priv->model);
+ }
+ else
+ {
+ /* menu mode */
+ if (combo_box->priv->popup_widget)
+ gtk_combo_box_menu_fill (combo_box);
+
+ }
if (combo_box->priv->cell_view)
gtk_cell_view_set_model (GTK_CELL_VIEW (combo_box->priv->cell_view),
{
GtkComboBox *combo_box = GTK_COMBO_BOX (object);
+ combo_box->priv->destroying = 1;
+
GTK_OBJECT_CLASS (parent_class)->destroy (object);
combo_box->priv->cell_view = NULL;
+
+ combo_box->priv->destroying = 0;
}
static void
GtkComboBox *combo_box = GTK_COMBO_BOX (object);
GSList *i;
- gtk_combo_box_unset_model (combo_box);
-
if (GTK_IS_MENU (combo_box->priv->popup_widget))
gtk_combo_box_menu_destroy (combo_box);
if (combo_box->priv->popup_window)
gtk_widget_destroy (combo_box->priv->popup_window);
+ gtk_combo_box_unset_model (combo_box);
+
if (combo_box->priv->model)
g_object_unref (combo_box->priv->model);