# HG changeset patch # User Frank Benoit # Date 1207506397 -7200 # Node ID 8ab606c0052094cd2bd4d05a7d531d218fa89baa # Parent 00180515eb650279308a9a200bea6e15e6bcf471 Fix side effects in getter methods diff -r 00180515eb65 -r 8ab606c00520 dwt/widgets/Spinner.d --- a/dwt/widgets/Spinner.d Wed Mar 19 21:36:28 2008 +0100 +++ b/dwt/widgets/Spinner.d Sun Apr 06 20:26:37 2008 +0200 @@ -28,10 +28,8 @@ import tango.stdc.stringz; import tango.util.Convert; -import Math = tango.math.Math; -static import tango.stdc.string; -static import tango.text.Util; -import Unicode = tango.text.Unicode; +import dwt.dwthelper.utils; +alias char[] String; /** * Instances of this class are selectable user interface @@ -203,16 +201,16 @@ checkWidget (); if (wHint !is DWT.DEFAULT && wHint < 0) wHint = 0; if (hHint !is DWT.DEFAULT && hHint < 0) hHint = 0; - int w, h; + int[1] w, h; OS.gtk_widget_realize (handle); auto layout = OS.gtk_entry_get_layout (cast(GtkEntry*)handle); auto adjustment = OS.gtk_spin_button_get_adjustment (cast(GtkSpinButton*)handle); int digits = OS.gtk_spin_button_get_digits (cast(GtkSpinButton*)handle); for (int i = 0; i < digits; i++) adjustment.upper *= 10; - char[] string = to!(char[])( (cast(int) adjustment.upper) ); + String string = to!(String)( (cast(int) adjustment.upper) ); if (digits > 0) { //PROTING_TODO: Efficiency - char[] buffer = string ~ getDecimalSeparator (); + String buffer = string ~ getDecimalSeparator (); int count = digits - string.length; while (count >= 0) { buffer ~= "0"; @@ -222,12 +220,12 @@ } char [] buffer1 = string; auto ptr = OS.pango_layout_get_text (layout); - char[] buffer2 = fromStringz( ptr ).dup; + String buffer2 = fromStringz( ptr ).dup; OS.pango_layout_set_text (layout, buffer1.ptr, buffer1.length); - OS.pango_layout_get_size (layout, &w, &h); + OS.pango_layout_get_size (layout, w.ptr, h.ptr); OS.pango_layout_set_text (layout, buffer2.ptr, buffer2.length); - int width = OS.PANGO_PIXELS (w); - int height = OS.PANGO_PIXELS (h); + int width = OS.PANGO_PIXELS (w [0]); + int height = OS.PANGO_PIXELS (h [0]); width = wHint is DWT.DEFAULT ? width : wHint; height = hHint is DWT.DEFAULT ? height : hHint; Rectangle trim = computeTrim (0, 0, width, height); @@ -391,8 +389,9 @@ checkWidget (); auto adjustment = OS.gtk_spin_button_get_adjustment (cast(GtkSpinButton*)handle); int digits = OS.gtk_spin_button_get_digits (cast(GtkSpinButton*)handle); - for (int i = 0; i < digits; i++) adjustment.step_increment *= 10; - return cast(int) (adjustment.step_increment + 0.5); + auto value = adjustment.step_increment; + for (int i = 0; i < digits; i++) value *= 10; + return cast(int) (value + 0.5); } /** @@ -409,8 +408,9 @@ checkWidget (); auto adjustment = OS.gtk_spin_button_get_adjustment (cast(GtkSpinButton*)handle); int digits = OS.gtk_spin_button_get_digits (cast(GtkSpinButton*)handle); - for (int i = 0; i < digits; i++) adjustment.upper *= 10; - return cast(int) (adjustment.upper + 0.5); + auto value = adjustment.upper; + for (int i = 0; i < digits; i++) value *= 10; + return cast(int) (value + 0.5); } /** @@ -427,8 +427,9 @@ checkWidget (); auto adjustment = OS.gtk_spin_button_get_adjustment (cast(GtkSpinButton*)handle); int digits = OS.gtk_spin_button_get_digits (cast(GtkSpinButton*)handle); - for (int i = 0; i < digits; i++) adjustment.lower *= 10; - return cast(int) (adjustment.lower + 0.5); + auto value = adjustment.lower; + for (int i = 0; i < digits; i++) value *= 10; + return cast(int) (value + 0.5); } /** @@ -446,8 +447,9 @@ checkWidget (); auto adjustment = OS.gtk_spin_button_get_adjustment (cast(GtkSpinButton*)handle); int digits = OS.gtk_spin_button_get_digits (cast(GtkSpinButton*)handle); - for (int i = 0; i < digits; i++) adjustment.page_increment *= 10; - return cast(int) (adjustment.page_increment + 0.5); + auto value = adjustment.page_increment; + for (int i = 0; i < digits; i++) value *= 10; + return cast(int) (value + 0.5); } /** @@ -464,8 +466,9 @@ checkWidget (); auto adjustment = OS.gtk_spin_button_get_adjustment (cast(GtkSpinButton*)handle); int digits = OS.gtk_spin_button_get_digits (cast(GtkSpinButton*)handle); - for (int i = 0; i < digits; i++) adjustment.value *= 10; - return cast(int) (adjustment.value + 0.5); + auto value = adjustment.value; + for (int i = 0; i < digits; i++) value *= 10; + return cast(int) (value + 0.5); } /** @@ -483,7 +486,7 @@ return OS.gtk_spin_button_get_digits (cast(GtkSpinButton*)handle); } -char[] getDecimalSeparator () { +String getDecimalSeparator () { auto ptr = OS.localeconv_decimal_point (); return fromStringz( ptr ).dup; } @@ -570,7 +573,7 @@ override int /*long*/ gtk_delete_text (GtkWidget* widget, int start_pos, int end_pos) { if (!hooks (DWT.Verify) && !filters (DWT.Verify)) return 0; - char[] newText = verifyText ("", cast(int)/*64*/start_pos, cast(int)/*64*/end_pos); + String newText = verifyText ("", cast(int)/*64*/start_pos, cast(int)/*64*/end_pos); if (newText is null) { OS.g_signal_stop_emission_by_name (handle, OS.delete_text.ptr); } else { @@ -601,14 +604,14 @@ override int /*long*/ gtk_insert_text (GtkEditable* widget, char* new_text, int new_text_length, int position) { // if (!hooks (DWT.Verify) && !filters (DWT.Verify)) return 0; if (new_text is null || new_text_length is 0) return 0; - char[] oldText = new_text[ 0 .. new_text_length ].dup; + String oldText = new_text[ 0 .. new_text_length ].dup; int pos; pos = position; if (pos is -1) { auto ptr = OS.gtk_entry_get_text (cast(GtkEntry*)handle); pos = cast(int)/*64*/OS.g_utf8_strlen (ptr, -1); } - char[] newText = verifyText (oldText, pos, pos); + String newText = verifyText (oldText, pos, pos); if (newText !is oldText) { int newStart, newEnd; OS.gtk_editable_get_selection_bounds (cast(GtkEditable*)handle, &newStart, &newEnd); @@ -1024,7 +1027,7 @@ return super.translateTraversal (keyEvent); } -char[] verifyText (char[] string, int start, int end) { +String verifyText (String string, int start, int end) { if (string.length is 0 && start is end) return null; Event event = new Event (); event.text = string; @@ -1043,15 +1046,15 @@ } int index = 0; if (OS.gtk_spin_button_get_digits (cast(GtkSpinButton*)handle) > 0) { - char[] decimalSeparator = getDecimalSeparator (); - index = tango.text.Util.locatePattern( string, decimalSeparator ); - if (index !is string.length) { - string = string[ 0 .. index ] ~ string[ index + 1 ]; + String decimalSeparator = getDecimalSeparator (); + index = string.indexOf( decimalSeparator ); + if (index !is -1 ) { + string = string.substring( 0, index ) ~ string.substring( index + 1 ); } index = 0; } while (index < string.length) { - if (!Unicode.isDigit (string[index])) break; + if (!CharacterIsDigit (string.charAt(index))) break; index++; } event.doit = index is string.length;