X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=gtk%2Fgtktogglebutton.c;h=999710c1671d93592c7f7a0f86e114a90356dc5d;hb=a89d420270d1a856e072ed87c365b0176f102e6c;hp=a60ffebae3fd789a13ec6d821d3fa84edbad2e32;hpb=abcfd2d5ef4caaf2c903624a3c7e61d61f30411a;p=~andy%2Fgtk diff --git a/gtk/gtktogglebutton.c b/gtk/gtktogglebutton.c index a60ffebae..999710c16 100644 --- a/gtk/gtktogglebutton.c +++ b/gtk/gtktogglebutton.c @@ -12,9 +12,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 . */ /* @@ -25,14 +23,70 @@ */ #include "config.h" + +#include "gtktogglebutton.h" + +#include "gtkbuttonprivate.h" #include "gtklabel.h" #include "gtkmain.h" #include "gtkmarshalers.h" -#include "gtktogglebutton.h" #include "gtktoggleaction.h" #include "gtkactivatable.h" #include "gtkprivate.h" #include "gtkintl.h" +#include "a11y/gtktogglebuttonaccessible.h" + + +/** + * SECTION:gtktogglebutton + * @Short_description: Create buttons which retain their state + * @Title: GtkToggleButton + * @See_also: #GtkButton, #GtkCheckButton, #GtkCheckMenuItem + * + * A #GtkToggleButton is a #GtkButton which will remain 'pressed-in' when + * clicked. Clicking again will cause the toggle button to return to its + * normal state. + * + * A toggle button is created by calling either gtk_toggle_button_new() or + * gtk_toggle_button_new_with_label(). If using the former, it is advisable to + * pack a widget, (such as a #GtkLabel and/or a #GtkImage), into the toggle + * button's container. (See #GtkButton for more information). + * + * The state of a #GtkToggleButton can be set specifically using + * gtk_toggle_button_set_active(), and retrieved using + * gtk_toggle_button_get_active(). + * + * To simply switch the state of a toggle button, use gtk_toggle_button_toggled(). + * + * + * Creating two #GtkToggleButton widgets. + * + * void make_toggles (void) { + * GtkWidget *dialog, *toggle1, *toggle2; + * + * dialog = gtk_dialog_new (); + * toggle1 = gtk_toggle_button_new_with_label ("Hi, i'm a toggle button."); + * + * // Makes this toggle button invisible + * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (toggle1), TRUE); + * + * g_signal_connect (toggle1, "toggled", + * G_CALLBACK (output_state), NULL); + * gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area), + * toggle1, FALSE, FALSE, 2); + * + * toggle2 = gtk_toggle_button_new_with_label ("Hi, i'm another toggle button."); + * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (toggle2), FALSE); + * g_signal_connect (toggle2, "toggled", + * G_CALLBACK (output_state), NULL); + * gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area), + * toggle2, FALSE, FALSE, 2); + * + * gtk_widget_show_all (dialog); + * } + * + * + */ #define DEFAULT_LEFT_POS 4 @@ -59,8 +113,6 @@ enum { }; -static gint gtk_toggle_button_draw (GtkWidget *widget, - cairo_t *cr); static gboolean gtk_toggle_button_mnemonic_activate (GtkWidget *widget, gboolean group_cycling); static void gtk_toggle_button_pressed (GtkButton *button); @@ -105,7 +157,6 @@ gtk_toggle_button_class_init (GtkToggleButtonClass *class) gobject_class->set_property = gtk_toggle_button_set_property; gobject_class->get_property = gtk_toggle_button_get_property; - widget_class->draw = gtk_toggle_button_draw; widget_class->mnemonic_activate = gtk_toggle_button_mnemonic_activate; button_class->pressed = gtk_toggle_button_pressed; @@ -140,6 +191,13 @@ gtk_toggle_button_class_init (GtkToggleButtonClass *class) FALSE, GTK_PARAM_READWRITE)); + /** + * GtkToggleButton::toggled: + * @togglebutton: the object which received the signal. + * + * Should be connected if you wish to perform an action whenever the + * #GtkToggleButton's state is changed. + */ toggle_button_signals[TOGGLED] = g_signal_new (I_("toggled"), G_OBJECT_CLASS_TYPE (gobject_class), @@ -150,6 +208,8 @@ gtk_toggle_button_class_init (GtkToggleButtonClass *class) G_TYPE_NONE, 0); g_type_class_add_private (class, sizeof (GtkToggleButtonPrivate)); + + gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_TOGGLE_BUTTON_ACCESSIBLE); } static void @@ -164,7 +224,7 @@ gtk_toggle_button_init (GtkToggleButton *toggle_button) priv->active = FALSE; priv->draw_indicator = FALSE; - GTK_BUTTON (toggle_button)->depress_on_activate = TRUE; + GTK_BUTTON (toggle_button)->priv->depress_on_activate = TRUE; } static void @@ -213,13 +273,27 @@ gtk_toggle_button_sync_action_properties (GtkActivatable *activatable, gtk_action_unblock_activate (action); } - +/** + * gtk_toggle_button_new: + * + * Creates a new toggle button. A widget should be packed into the button, as in gtk_button_new(). + * + * Returns: a new toggle button. + */ GtkWidget* gtk_toggle_button_new (void) { return g_object_new (GTK_TYPE_TOGGLE_BUTTON, NULL); } +/** + * gtk_toggle_button_new_with_label: + * @label: a string containing the message to be placed in the toggle button. + * + * Creates a new toggle button with a text label. + * + * Returns: a new toggle button. + */ GtkWidget* gtk_toggle_button_new_with_label (const gchar *label) { @@ -230,12 +304,13 @@ gtk_toggle_button_new_with_label (const gchar *label) * gtk_toggle_button_new_with_mnemonic: * @label: the text of the button, with an underscore in front of the * mnemonic character - * @returns: a new #GtkToggleButton * * Creates a new #GtkToggleButton containing a label. The label * will be created using gtk_label_new_with_mnemonic(), so underscores * in @label indicate the mnemonic for the button. - **/ + * + * Returns: a new #GtkToggleButton + */ GtkWidget* gtk_toggle_button_new_with_mnemonic (const gchar *label) { @@ -326,13 +401,25 @@ gtk_toggle_button_set_mode (GtkToggleButton *toggle_button, if (priv->draw_indicator != draw_indicator) { + GtkStyleContext *context; + priv->draw_indicator = draw_indicator; - GTK_BUTTON (toggle_button)->depress_on_activate = !draw_indicator; - + GTK_BUTTON (toggle_button)->priv->depress_on_activate = !draw_indicator; + if (gtk_widget_get_visible (GTK_WIDGET (toggle_button))) gtk_widget_queue_resize (GTK_WIDGET (toggle_button)); g_object_notify (G_OBJECT (toggle_button), "draw-indicator"); + + /* Make toggle buttons conditionally have the "button" + * class depending on draw_indicator. + */ + context = gtk_widget_get_style_context (GTK_WIDGET (toggle_button)); + + if (draw_indicator) + gtk_style_context_remove_class (context, GTK_STYLE_CLASS_BUTTON); + else + gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON); } } @@ -354,6 +441,16 @@ gtk_toggle_button_get_mode (GtkToggleButton *toggle_button) return toggle_button->priv->draw_indicator; } +/** + * gtk_toggle_button_set_active: + * @toggle_button: a #GtkToggleButton. + * @is_active: %TRUE or %FALSE. + * + * Sets the status of the toggle button. Set to %TRUE if you want the + * GtkToggleButton to be 'pressed in', and %FALSE to raise it. + * This action causes the #GtkToggleButton::toggled signal and the + * #GtkButton::clicked signal to be emitted. + */ void gtk_toggle_button_set_active (GtkToggleButton *toggle_button, gboolean is_active) @@ -377,6 +474,15 @@ _gtk_toggle_button_set_active (GtkToggleButton *toggle_button, toggle_button->priv->active = is_active; } +/** + * gtk_toggle_button_get_active: + * @toggle_button: a #GtkToggleButton. + * + * Queries a #GtkToggleButton and returns its current state. Returns %TRUE if + * the toggle button is pressed in and %FALSE if it is raised. + * + * Returns: a #gboolean value. + */ gboolean gtk_toggle_button_get_active (GtkToggleButton *toggle_button) { @@ -385,7 +491,14 @@ gtk_toggle_button_get_active (GtkToggleButton *toggle_button) return toggle_button->priv->active; } - +/** + * gtk_toggle_button_toggled: + * @toggle_button: a #GtkToggleButton. + * + * Emits the #GtkToggleButton::toggled signal on the + * #GtkToggleButton. There is no good reason for an + * application ever to call this function. + */ void gtk_toggle_button_toggled (GtkToggleButton *toggle_button) { @@ -448,40 +561,6 @@ gtk_toggle_button_get_inconsistent (GtkToggleButton *toggle_button) return toggle_button->priv->inconsistent; } -static gint -gtk_toggle_button_draw (GtkWidget *widget, - cairo_t *cr) -{ - GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (widget); - GtkToggleButtonPrivate *priv = toggle_button->priv; - GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget)); - GtkButton *button = GTK_BUTTON (widget); - GtkStateType state_type; - GtkShadowType shadow_type; - - state_type = gtk_widget_get_state (widget); - - if (priv->inconsistent) - { - if (state_type == GTK_STATE_ACTIVE) - state_type = GTK_STATE_NORMAL; - shadow_type = GTK_SHADOW_ETCHED_IN; - } - else - shadow_type = button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT; - - _gtk_button_paint (button, cr, - gtk_widget_get_allocated_width (widget), - gtk_widget_get_allocated_height (widget), - state_type, shadow_type, - "togglebutton", "togglebuttondefault"); - - if (child) - gtk_container_propagate_draw (GTK_CONTAINER (widget), child, cr); - - return FALSE; -} - static gboolean gtk_toggle_button_mnemonic_activate (GtkWidget *widget, gboolean group_cycling) @@ -503,7 +582,7 @@ gtk_toggle_button_mnemonic_activate (GtkWidget *widget, static void gtk_toggle_button_pressed (GtkButton *button) { - button->button_down = TRUE; + button->priv->button_down = TRUE; gtk_toggle_button_update_state (button); gtk_widget_queue_draw (GTK_WIDGET (button)); @@ -512,11 +591,11 @@ gtk_toggle_button_pressed (GtkButton *button) static void gtk_toggle_button_released (GtkButton *button) { - if (button->button_down) + if (button->priv->button_down) { - button->button_down = FALSE; + button->priv->button_down = FALSE; - if (button->in_button) + if (button->priv->in_button) gtk_button_clicked (button); gtk_toggle_button_update_state (button); @@ -547,25 +626,30 @@ gtk_toggle_button_update_state (GtkButton *button) { GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (button); GtkToggleButtonPrivate *priv = toggle_button->priv; - gboolean depressed, touchscreen; - GtkStateType new_state; + gboolean depressed; + GtkStateFlags new_state = 0; - g_object_get (gtk_widget_get_settings (GTK_WIDGET (button)), - "gtk-touchscreen-mode", &touchscreen, - NULL); + new_state = gtk_widget_get_state_flags (GTK_WIDGET (button)) & + ~(GTK_STATE_FLAG_INCONSISTENT | + GTK_STATE_FLAG_PRELIGHT | + GTK_STATE_FLAG_ACTIVE); + + if (priv->inconsistent) + new_state |= GTK_STATE_FLAG_INCONSISTENT; if (priv->inconsistent) depressed = FALSE; - else if (button->in_button && button->button_down) + else if (button->priv->in_button && button->priv->button_down) depressed = TRUE; else depressed = priv->active; - if (!touchscreen && button->in_button && (!button->button_down || priv->draw_indicator)) - new_state = GTK_STATE_PRELIGHT; - else - new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL; + if (button->priv->in_button) + new_state |= GTK_STATE_FLAG_PRELIGHT; + + if (depressed) + new_state |= GTK_STATE_FLAG_ACTIVE; - _gtk_button_set_depressed (button, depressed); - gtk_widget_set_state (GTK_WIDGET (toggle_button), new_state); + _gtk_button_set_depressed (button, depressed); + gtk_widget_set_state_flags (GTK_WIDGET (toggle_button), new_state, TRUE); }