~mdoyen/homebank/5.3.x

« back to all changes in this revision

Viewing changes to src/ui-widgets.c

  • Committer: Maxime Doyen
  • Date: 2019-05-13 05:39:47 UTC
  • Revision ID: homebank@free.fr-20190513053947-09debj3ad7alxwba
5.2.5 release

Show diffs side-by-side

added added

removed removed

Lines of Context:
413
413
}
414
414
 
415
415
 
416
 
 
417
416
static void hb_amount_insert_text_handler (GtkEntry *entry, const gchar *text, gint length, gint *position, gpointer data)
418
417
{
419
418
GtkEditable *editable = GTK_EDITABLE(entry);
420
 
int i, count=0, dcpos=-1;
421
 
gchar *result = g_new0 (gchar, length+1);
422
 
int digits = 2;
423
 
const gchar *numtext;
424
 
 
425
 
        //g_message("insert_text-handler: text:%s - pos:%d - length:%d", text, *position, length);
 
419
gint i, digits, count=0, dcpos=-1;
 
420
gchar *clntxt;
 
421
        
 
422
        DB( g_print("-----\ninsert_text-handler: instxt:%s pos:%d len:%d\n", text, *position, length) );
426
423
 
427
424
        digits = gtk_spin_button_get_digits(GTK_SPIN_BUTTON(entry));
428
 
        numtext = gtk_entry_get_text(entry);
429
 
        for (i=0 ; numtext[i]!='\0' ; i++)
430
 
        {
431
 
                if(numtext[i]==',' || numtext[i]=='.')
432
 
                        dcpos = i;
433
 
        }
434
 
 
435
 
        //g_message("previous text='%s' dcpos:'%d'", numtext, dcpos);
436
 
        for (i=0 ; i < length ; i++)
437
 
        {
438
 
                if( g_ascii_isdigit(text[i]) && ( (*position <= dcpos + digits) || dcpos < 0) )
439
 
                        goto inserttext;
440
 
 
441
 
                if( text[i]=='-' && *position==0 )      /* minus sign only at position 0 */
442
 
                        goto inserttext;
443
 
 
444
 
                if( dcpos < 0 && (text[i]=='.' || text[i]==',') )       /* decimal separator if not in previous string */
445
 
                        result[count++] = '.';
446
 
 
447
 
                continue;
448
 
 
449
 
        inserttext:
450
 
                result[count++] = text[i];
451
 
        }
452
 
 
453
 
        if (count > 0) {
 
425
 
 
426
        // most common : only 1 char to be inserted
 
427
        if( length == 1 )
 
428
        {
 
429
        const gchar *curtxt = gtk_entry_get_text(entry);
 
430
 
 
431
                for (i=0 ; curtxt[i]!='\0' ; i++)
 
432
                {
 
433
                        if(curtxt[i]==',' || curtxt[i]=='.')
 
434
                                dcpos = i;
 
435
                }
 
436
                DB( g_print(" dcpos:'%d'\n", dcpos) );
 
437
 
 
438
                clntxt = g_new0 (gchar, length+1);
 
439
                for (i=0 ; i < length ; i++)
 
440
                {
 
441
                        if( g_ascii_isdigit(text[i]) && ( (*position <= dcpos + digits) || dcpos < 0) )
 
442
                                goto doinsert;
 
443
 
 
444
                        if( text[i]=='-' && *position==0 )      /* minus sign only at position 0 */
 
445
                                goto doinsert;
 
446
 
 
447
                        if( dcpos < 0 && (text[i]=='.' || text[i]==',') )       /* decimal separator if not in previous string */
 
448
                                clntxt[count++] = '.';
 
449
 
 
450
                        continue;
 
451
 
 
452
                doinsert:
 
453
                        clntxt[count++] = text[i];
 
454
                }
 
455
        }
 
456
        // less common: paste a full text
 
457
        else
 
458
        {
 
459
                clntxt = hb_string_dup_raw_amount_clean(text, digits);
 
460
                count = strlen(clntxt);
 
461
        }
 
462
                
 
463
        if (count > 0)
 
464
        {
 
465
                DB( g_print(" insert %d char '%s' at %d\n", count, clntxt, *position) );
454
466
                g_signal_handlers_block_by_func (G_OBJECT (editable), G_CALLBACK (hb_amount_insert_text_handler), data);
455
 
                gtk_editable_insert_text (editable, result, count, position);
 
467
                gtk_editable_insert_text (editable, clntxt, count, position);
456
468
                g_signal_handlers_unblock_by_func (G_OBJECT (editable), G_CALLBACK (hb_amount_insert_text_handler), data);
457
469
        }
 
470
 
 
471
        g_free (clntxt);
 
472
 
458
473
        g_signal_stop_emission_by_name (G_OBJECT (editable), "insert-text");
459
 
 
460
 
        g_free (result);
461
474
}
462
475
 
463
 
/*
464
 
**
465
 
*/
 
476
 
466
477
GtkWidget *make_amount(GtkWidget *label)
467
478
{
468
479
GtkWidget *spinner;