From a197db54f62be56f4dfe44bfc1d943052973b5fd Mon Sep 17 00:00:00 2001 From: Jose Antonio Ortega Ruiz Date: Wed, 23 Jun 2004 10:50:10 +0000 Subject: First, incomplete port to gtk 2.0. Basic functionality in place. --- mixgtk/mixgtk_input.c | 178 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 107 insertions(+), 71 deletions(-) (limited to 'mixgtk/mixgtk_input.c') diff --git a/mixgtk/mixgtk_input.c b/mixgtk/mixgtk_input.c index a516ced..90aa803 100644 --- a/mixgtk/mixgtk_input.c +++ b/mixgtk/mixgtk_input.c @@ -1,24 +1,24 @@ /* -*-c-*- -------------- mixgtk_input.c : * Implementation of the functions declared in mixgtk_input.h * ------------------------------------------------------------------ - * $Id: mixgtk_input.c,v 1.5 2002/04/09 23:28:19 jao Exp $ + * $Id: mixgtk_input.c,v 1.6 2004/06/23 10:50:10 jao Exp $ * ------------------------------------------------------------------ - * Copyright (C) 2001, 2002 Free Software Foundation, Inc. - * + * Copyright (C) 2001, 2002, 2004 Free Software Foundation, Inc. + * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * + * */ #include @@ -42,6 +42,11 @@ static GtkWidget *childs_[SIZE_]; static input_callback_t callback_; static gpointer data_; + +static void init_dialog_ (void); +static void set_word_ (mix_word_t word); + + /* init */ void mixgtk_input_init (void) @@ -49,41 +54,6 @@ mixgtk_input_init (void) dialog_ = NULL; } -static void -init_dialog_ (void) -{ - gint k; - - dialog_ = GTK_DIALOG (mixgtk_widget_factory_get_dialog (MIXGTK_WORD_DIALOG)); - g_assert (dialog_ != NULL); - for (k = 0; k < SIZE_; ++k) - { - childs_[k] = mixgtk_widget_factory_get_child_by_name - (MIXGTK_WORD_DIALOG, WGT_NAMES_[k]); - g_assert (childs_[k] != NULL); - } -} - -static void -set_word_ (mix_word_t word) -{ - enum {SIZE = 50}; - static gchar BUFFER[SIZE] = {0}; - gint k; - gint val = mix_word_magnitude (word); - gboolean neg = mix_word_is_negative (word); - g_snprintf (BUFFER, SIZE, "%s%d", neg ? "-" : "", val); - gtk_entry_set_text (GTK_ENTRY (childs_[DEC_]), BUFFER); - gtk_entry_set_text (GTK_ENTRY (childs_[SIGN_]), neg ? "-" : "+"); - for (k = 1; k < 6; ++k) - { - mix_byte_t b = mix_word_get_byte (word, k); - g_snprintf (BUFFER, SIZE, "%d", (int)b); - gtk_entry_set_text (GTK_ENTRY (childs_[SIGN_ + k]), BUFFER); - } -} - - /* get a word */ void mixgtk_input_word (const gchar *message, mix_word_t def, @@ -105,6 +75,7 @@ void mixgtk_input_short (const gchar *message, mix_short_t def, input_callback_t cb, gpointer data) { + if (!dialog_) init_dialog_ (); gtk_label_set_text (GTK_LABEL (childs_[MSG_]), message); set_word_ (mix_word_to_short_fast (def)); callback_ = cb; @@ -118,33 +89,34 @@ mixgtk_input_short (const gchar *message, mix_short_t def, #include /* dec and bytes input handler */ -gboolean -on_word_dec_key_press (GtkWidget *dec, GdkEvent *event, gpointer *data) +void +on_word_dec_changed (GtkEditable *dec, gpointer *data) { - GdkEventKey *key = (GdkEventKey *)event; - if (key->string && strlen (key->string) > 0 && !iscntrl (key->string[0])) + gchar *txt = gtk_editable_get_chars (dec, 0, -1); + + if (strlen (txt) > 0) { - gchar *text = gtk_entry_get_text (GTK_ENTRY (childs_[DEC_])); - gint val = atoi (text); + gint val = atoi (txt); set_word_ (mix_word_new (val)); } - return FALSE; + g_free (txt); } -gboolean -on_word_byte_key_press (GtkWidget *widget, GdkEvent *event, gpointer *data) +void +on_word_byte_changed (GtkEditable *byte, gpointer *data) { - GdkEventKey *key = (GdkEventKey *)event; - if (key->string && strlen (key->string) > 0 && !iscntrl (key->string[0])) + gchar *txt = gtk_editable_get_chars (byte, 0, 2); + + if (strlen (txt) > 0) { mix_byte_t bytes[5]; gint k; - gchar *s; + const gchar *s; mix_word_t w; - + for (k = 0; k < 5; ++k) { - gchar *text = gtk_entry_get_text (GTK_ENTRY (childs_[B1_ + k])); + const gchar *text = gtk_entry_get_text (GTK_ENTRY (childs_[B1_ + k])); bytes[k] = mix_byte_new (atoi (text)); } w = mix_bytes_to_word (bytes, 5); @@ -152,27 +124,32 @@ on_word_byte_key_press (GtkWidget *widget, GdkEvent *event, gpointer *data) if (s && s[0] == '-') mix_word_reverse_sign (w); set_word_ (w); } - return FALSE; + g_free (txt); } -gboolean -on_word_sign_key_press (GtkWidget *widget, GdkEvent *event, gpointer *data) +void +on_word_sign_changed (GtkEditable *sign, gpointer *data) { - GdkEventKey *key = (GdkEventKey *)event; - if (key->string && strlen (key->string) > 0 && !iscntrl (key->string[0])) + gchar *txt = gtk_editable_get_chars (sign, 0, 1); + + if (strlen (txt) > 0) { - gchar s = key->string[0]; - gchar *txt = gtk_entry_get_text (GTK_ENTRY (childs_[SIGN_])); - gchar c = txt ? txt[0] : '+'; - if ( (s == '+' || s == '-') && s != c) - { - mix_word_t w = - mix_word_new (atoi - (gtk_entry_get_text (GTK_ENTRY (childs_[DEC_])))); - set_word_ (mix_word_negative (w)); - } + gint pos = 0; + mix_word_t w = + mix_word_new (atoi (gtk_entry_get_text (GTK_ENTRY (childs_[DEC_])))); + + g_signal_handlers_block_by_func (GTK_OBJECT (sign), + on_word_sign_changed, data); + + if (txt[0] != '+' && txt[0] != '-') txt[0] = '+'; + gtk_editable_delete_text (sign, 0, 1); + gtk_editable_insert_text (sign, txt, 1, &pos); + + g_signal_handlers_unblock_by_func (GTK_OBJECT (sign), + on_word_sign_changed, data); + set_word_ (txt[0] == '-' ? mix_word_negative (w) : w); } - return FALSE; + g_free (txt); } void @@ -180,6 +157,11 @@ on_word_ok_clicked (GtkWidget *widget, gpointer *data) { const gchar *text = gtk_entry_get_text (GTK_ENTRY (childs_[DEC_])); mix_word_t w = mix_word_new (atoi (text)); + if (w == MIX_WORD_ZERO) + { + const gchar *sign = gtk_entry_get_text (GTK_ENTRY (childs_[SIGN_])); + if (sign && sign[0] == '-') mix_word_reverse_sign (w); + } callback_ (w, data_); gtk_widget_hide (GTK_WIDGET (dialog_)); } @@ -195,3 +177,57 @@ on_word_reset_clicked (GtkWidget *w, gpointer *data) { set_word_ (MIX_WORD_ZERO); } + +static void +init_dialog_ (void) +{ + gint k; + + dialog_ = GTK_DIALOG (mixgtk_widget_factory_get_dialog (MIXGTK_WORD_DIALOG)); + g_assert (dialog_ != NULL); + for (k = 0; k < SIZE_; ++k) + { + childs_[k] = mixgtk_widget_factory_get_child_by_name + (MIXGTK_WORD_DIALOG, WGT_NAMES_[k]); + g_assert (childs_[k] != NULL); + } +} + +static void +set_word_ (mix_word_t word) +{ + enum {SIZE = 50}; + static gchar BUFFER[SIZE] = {0}; + gint k; + gint val = mix_word_magnitude (word); + gboolean neg = mix_word_is_negative (word); + g_snprintf (BUFFER, SIZE, "%s%d", neg ? "-" : "", val); + + + g_signal_handlers_block_by_func (GTK_OBJECT (childs_[DEC_]), + on_word_dec_changed, NULL); + g_signal_handlers_block_by_func (GTK_OBJECT (childs_[SIGN_]), + on_word_sign_changed, NULL); + for (k = B1_; k <= B5_; ++k) + g_signal_handlers_block_by_func (GTK_OBJECT (childs_[k]), + on_word_byte_changed, NULL); + + gtk_entry_set_text (GTK_ENTRY (childs_[DEC_]), BUFFER); + gtk_entry_set_text (GTK_ENTRY (childs_[SIGN_]), neg ? "-" : "+"); + + for (k = 1; k < 6; ++k) + { + mix_byte_t b = mix_word_get_byte (word, k); + g_snprintf (BUFFER, SIZE, "%d", (int)b); + gtk_entry_set_text (GTK_ENTRY (childs_[SIGN_ + k]), BUFFER); + } + + for (k = B1_; k <= B5_; ++k) + g_signal_handlers_unblock_by_func (GTK_OBJECT (childs_[k]), + on_word_byte_changed, NULL); + g_signal_handlers_unblock_by_func (GTK_OBJECT (childs_[DEC_]), + on_word_dec_changed, NULL); + g_signal_handlers_unblock_by_func (GTK_OBJECT (childs_[SIGN_]), + on_word_sign_changed, NULL); +} + -- cgit v1.2.3