Changeset 8239 for trunk/gtk/tr-window.c


Ignore:
Timestamp:
Apr 15, 2009, 3:10:22 PM (13 years ago)
Author:
charles
Message:

(trunk gtk) clean up yesterday's option menu code a bit

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gtk/tr-window.c

    r8238 r8239  
    7474typedef struct
    7575{
     76    GtkWidget *           speedlimit_on_item[2];
     77    GtkWidget *           speedlimit_off_item[2];
    7678    GtkWidget *           ratio_on_item;
    7779    GtkWidget *           ratio_off_item;
    78     GtkWidget *           downlimit_on_item;
    79     GtkWidget *           downlimit_off_item;
    80     GtkWidget *           uplimit_on_item;
    81     GtkWidget *           uplimit_off_item;
    8280    GtkWidget *           scroll;
    8381    GtkWidget *           view;
     
    583581}
    584582
     583/***
     584****  Speed limit menu
     585***/
     586
     587#define DIRECTION_KEY "direction-key"
    585588#define ENABLED_KEY "enabled-key"
    586589#define SPEED_KEY "speed-key"
    587590
    588591static void
    589 onSpeedLimitToggled( GtkCheckMenuItem * check, tr_direction direction, gpointer vp )
     592onSpeedToggled( GtkCheckMenuItem * check, gpointer vp )
     593{
     594    PrivateData * p = vp;
     595    GObject * o = G_OBJECT( check );
     596    gboolean isEnabled = g_object_get_data( o, ENABLED_KEY ) != 0;
     597    tr_direction dir = GPOINTER_TO_INT( g_object_get_data( o, DIRECTION_KEY ) );
     598    const char * key = dir == TR_UP ? TR_PREFS_KEY_USPEED_ENABLED
     599                                    : TR_PREFS_KEY_DSPEED_ENABLED;
     600
     601    if( gtk_check_menu_item_get_active( check ) )
     602        tr_core_set_pref_bool( p->core, key, isEnabled );
     603}
     604
     605static void
     606onSpeedSet( GtkCheckMenuItem * check, gpointer vp )
     607{
     608    const char * key;
     609    PrivateData * p = vp;
     610    GObject * o = G_OBJECT( check );
     611    const int speed = GPOINTER_TO_INT( g_object_get_data( o, SPEED_KEY ) );
     612    tr_direction dir = GPOINTER_TO_INT( g_object_get_data( o, DIRECTION_KEY ) );
     613
     614    key = dir==TR_UP ? TR_PREFS_KEY_USPEED : TR_PREFS_KEY_DSPEED;
     615    tr_core_set_pref_int( p->core, key, speed );
     616
     617    key = dir==TR_UP ? TR_PREFS_KEY_USPEED_ENABLED : TR_PREFS_KEY_DSPEED_ENABLED;
     618    tr_core_set_pref_bool( p->core, key, TRUE );
     619}
     620
     621static GtkWidget*
     622createSpeedMenu( PrivateData * p, tr_direction dir )
     623{
     624    int i, n;
     625    GtkWidget *w, *m;
     626    const int speeds[] = { 5, 10, 20, 30, 40, 50, 75, 100, 150, 200, 250, 500, 750 };
     627
     628    m = gtk_menu_new( );
     629
     630    w = gtk_radio_menu_item_new_with_label( NULL, _( "Unlimited" ) );
     631    p->speedlimit_off_item[dir] = w;
     632    g_object_set_data( G_OBJECT( w ), DIRECTION_KEY, GINT_TO_POINTER( dir ) );
     633    g_object_set_data( G_OBJECT( w ), ENABLED_KEY, GINT_TO_POINTER( FALSE ) );
     634    g_signal_connect( w, "toggled", G_CALLBACK(onSpeedToggled), p );
     635    gtk_menu_shell_append( GTK_MENU_SHELL( m ), w );
     636
     637    w = gtk_radio_menu_item_new_with_label_from_widget( GTK_RADIO_MENU_ITEM( w ), "" );
     638    p->speedlimit_on_item[dir] = w;
     639    g_object_set_data( G_OBJECT( w ), DIRECTION_KEY, GINT_TO_POINTER( dir ) );
     640    g_object_set_data( G_OBJECT( w ), ENABLED_KEY, GINT_TO_POINTER( TRUE ) );
     641    g_signal_connect( w, "toggled", G_CALLBACK(onSpeedToggled), p );
     642    gtk_menu_shell_append( GTK_MENU_SHELL( m ), w );
     643
     644    w = gtk_separator_menu_item_new( );
     645    gtk_menu_shell_append( GTK_MENU_SHELL( m ), w );
     646
     647    for( i=0, n=G_N_ELEMENTS(speeds); i<n; ++i )
     648    {
     649        char buf[128];
     650        tr_strlspeed( buf, speeds[i], sizeof( buf ) );
     651        w = gtk_menu_item_new_with_label( buf );
     652        g_object_set_data( G_OBJECT( w ), DIRECTION_KEY, GINT_TO_POINTER( dir ) );
     653        g_object_set_data( G_OBJECT( w ), SPEED_KEY, GINT_TO_POINTER( speeds[i] ) );
     654        g_signal_connect( w, "activate", G_CALLBACK(onSpeedSet), p );
     655        gtk_menu_shell_append( GTK_MENU_SHELL( m ), w );
     656    }
     657
     658    return m;
     659}
     660
     661/***
     662****  Speed limit menu
     663***/
     664
     665#define RATIO_KEY "stock-ratio-index"
     666
     667static const double stockRatios[] = { 0.25, 0.5, 0.75, 1, 1.5, 2, 3 };
     668
     669static void
     670onRatioToggled( GtkCheckMenuItem * check, gpointer vp )
    590671{
    591672    PrivateData * p = vp;
    592673    if( gtk_check_menu_item_get_active( check ) )
    593674    {
    594         const gboolean isEnabled = g_object_get_data( G_OBJECT( check ), ENABLED_KEY ) != 0;
    595         const char * key = direction == TR_UP ? TR_PREFS_KEY_USPEED_ENABLED : TR_PREFS_KEY_DSPEED_ENABLED;
    596         tr_core_set_pref_bool( p->core, key, isEnabled );
    597     }
    598 }
    599 static void
    600 onSpeedLimitSet( GtkCheckMenuItem * check, tr_direction direction, gpointer vp )
     675        gboolean f = g_object_get_data( G_OBJECT( check ), ENABLED_KEY ) != 0;
     676        tr_core_set_pref_bool( p->core, TR_PREFS_KEY_RATIO_ENABLED, f );
     677    }
     678}
     679static void
     680onRatioSet( GtkCheckMenuItem * check, gpointer vp )
    601681{
    602682    PrivateData * p = vp;
    603     gpointer d = g_object_get_data( G_OBJECT( check ), SPEED_KEY );
    604     const int kb = GPOINTER_TO_INT( d );
    605     const char * key = direction == TR_UP ? TR_PREFS_KEY_USPEED : TR_PREFS_KEY_DSPEED;
    606     tr_core_set_pref_int( p->core, key, kb );
    607 }
    608 static void
    609 onUploadLimitedToggled( GtkCheckMenuItem * check, gpointer p )
    610 {
    611     onSpeedLimitToggled( check, TR_UP, p );
    612 }
    613 static void
    614 onUploadLimitSet( GtkCheckMenuItem * check, gpointer vp )
    615 {
    616     PrivateData * p = vp;
    617     onSpeedLimitSet( check, TR_UP, p );
    618     tr_core_set_pref_bool( p->core, TR_PREFS_KEY_USPEED_ENABLED, TRUE );
    619 }
    620 static void
    621 onDownloadLimitedToggled( GtkCheckMenuItem * check, gpointer p )
    622 {
    623     onSpeedLimitToggled( check, TR_DOWN, p );
    624 }
    625 static void
    626 onDownloadLimitSet( GtkCheckMenuItem * check, gpointer vp )
    627 {
    628     PrivateData * p = vp;
    629     onSpeedLimitSet( check, TR_DOWN, p );
    630     tr_core_set_pref_bool( p->core, TR_PREFS_KEY_DSPEED_ENABLED, TRUE );
    631 }
    632 static void
    633 onRatioToggled( GtkCheckMenuItem * check, gpointer vp )
    634 {
    635     PrivateData * p = vp;
    636     if( gtk_check_menu_item_get_active( check ) )
    637     {
    638         const gboolean isEnabled = g_object_get_data( G_OBJECT( check ), ENABLED_KEY ) != 0;
    639         tr_core_set_pref_bool( p->core, TR_PREFS_KEY_RATIO_ENABLED, isEnabled );
    640     }
    641 }
    642 static void
    643 onRatioSet( GtkCheckMenuItem * check, gpointer vp )
    644 {
    645     PrivateData * p = vp;
    646     gpointer d = g_object_get_data( G_OBJECT( check ), SPEED_KEY );
    647     const int ratio = GPOINTER_TO_INT( d );
    648     tr_core_set_pref_double( p->core, TR_PREFS_KEY_RATIO, ratio/100.0 );
    649 }
     683    int i = GPOINTER_TO_INT( g_object_get_data( G_OBJECT( check ), RATIO_KEY ) );
     684    const double ratio = stockRatios[i];
     685    tr_core_set_pref_double( p->core, TR_PREFS_KEY_RATIO, ratio );
     686    tr_core_set_pref_bool  ( p->core, TR_PREFS_KEY_RATIO_ENABLED, TRUE );
     687}
     688
     689static GtkWidget*
     690createRatioMenu( PrivateData * p )
     691{
     692    int i, n;
     693    GtkWidget *m, *w;
     694
     695    m = gtk_menu_new( );
     696
     697    w = gtk_radio_menu_item_new_with_label( NULL, _( "Seed Forever" ) );
     698    p->ratio_off_item = w;
     699    g_object_set_data( G_OBJECT( w ), ENABLED_KEY, GINT_TO_POINTER( FALSE ) );
     700    g_signal_connect( w, "toggled", G_CALLBACK(onRatioToggled), p );
     701    gtk_menu_shell_append( GTK_MENU_SHELL( m ), w );
     702
     703    w = gtk_radio_menu_item_new_with_label_from_widget( GTK_RADIO_MENU_ITEM( w ), "" );
     704    p->ratio_on_item = w;
     705    g_object_set_data( G_OBJECT( w ), ENABLED_KEY, GINT_TO_POINTER( TRUE ) );
     706    g_signal_connect( w, "toggled", G_CALLBACK(onRatioToggled), p );
     707    gtk_menu_shell_append( GTK_MENU_SHELL( m ), w );
     708
     709    w = gtk_separator_menu_item_new( );
     710    gtk_menu_shell_append( GTK_MENU_SHELL( m ), w );
     711
     712    for( i=0, n=G_N_ELEMENTS(stockRatios); i<n; ++i )
     713    {
     714        char buf[128];
     715        tr_strlratio( buf, stockRatios[i], sizeof( buf ) );
     716        w = gtk_menu_item_new_with_label( buf );
     717        g_object_set_data( G_OBJECT( w ), RATIO_KEY, GINT_TO_POINTER( i ) );
     718        g_signal_connect( w, "activate", G_CALLBACK(onRatioSet), p );
     719        gtk_menu_shell_append( GTK_MENU_SHELL( m ), w );
     720    }
     721
     722    return m;
     723}
     724
     725/***
     726****  Option menu
     727***/
    650728
    651729static GtkWidget*
    652730createOptionsMenu( PrivateData * p )
    653731{
    654     int i, n;
    655     GtkWidget *sub, *w, *up, *down;
     732    GtkWidget * m;
    656733    GtkWidget * top = gtk_menu_new( );
    657     const int stockSpeeds[] = { 5, 10, 20, 30, 40, 50, 75, 100, 150, 200, 250, 500, 750 };
    658     const int stockRatios[] = { 25, 50, 75, 100, 150, 200, 300 };
    659 
    660     down = gtk_menu_item_new_with_label( _( "Limit Download Speed" ) );
    661     gtk_menu_shell_append( GTK_MENU_SHELL( top ), down );
    662     gtk_widget_show( down );
    663 
    664     sub = gtk_menu_new( );
    665 
    666     w = p->downlimit_off_item = gtk_radio_menu_item_new_with_label( NULL, _( "Unlimited" ) );
    667     gtk_menu_shell_append( GTK_MENU_SHELL( sub ), w );
    668     gtk_widget_show( w );
    669     g_object_set_data( G_OBJECT( w ), ENABLED_KEY, GINT_TO_POINTER( 0 ) );
    670     g_signal_connect( w, "toggled", G_CALLBACK(onDownloadLimitedToggled), p );
    671 
    672     w = p->downlimit_on_item = gtk_radio_menu_item_new_with_label_from_widget( GTK_RADIO_MENU_ITEM( w ), _( "Limit at XXX" ) );
    673     gtk_menu_shell_append( GTK_MENU_SHELL( sub ), w );
    674     gtk_widget_show( w );
    675     g_object_set_data( G_OBJECT( w ), ENABLED_KEY, GINT_TO_POINTER( 1 ) );
    676     g_signal_connect( w, "toggled", G_CALLBACK(onDownloadLimitedToggled), p );
    677 
    678     w = gtk_separator_menu_item_new( );
    679     gtk_menu_shell_append( GTK_MENU_SHELL( sub ), w );
    680     gtk_widget_show( w );
    681 
    682     for( i=0, n=G_N_ELEMENTS(stockSpeeds); i<n; ++i ) {
    683         char buf[128];
    684         tr_strlspeed( buf, stockSpeeds[i], sizeof( buf ) );
    685         w = gtk_menu_item_new_with_label( buf );
    686         gtk_menu_shell_append( GTK_MENU_SHELL( sub ), w );
    687         gtk_widget_show( w );
    688         g_object_set_data( G_OBJECT( w ), SPEED_KEY, GINT_TO_POINTER( stockSpeeds[i] ) );
    689         g_signal_connect( w, "activate", G_CALLBACK(onDownloadLimitSet), p );
    690     }
    691 
    692     gtk_menu_item_set_submenu( GTK_MENU_ITEM( down ), sub );
    693 
    694     up = gtk_menu_item_new_with_label( _( "Limit Upload Speed" ) );
    695     gtk_menu_shell_append( GTK_MENU_SHELL( top ), up );
    696     gtk_widget_show( up );
    697 
    698     sub = gtk_menu_new( );
    699 
    700     w = p->uplimit_off_item = gtk_radio_menu_item_new_with_label( NULL, _( "Unlimited" ) );
    701     gtk_menu_shell_append( GTK_MENU_SHELL( sub ), w );
    702     gtk_widget_show( w );
    703     g_object_set_data( G_OBJECT( w ), ENABLED_KEY, GINT_TO_POINTER( 0 ) );
    704     g_signal_connect( w, "toggled", G_CALLBACK(onUploadLimitedToggled), p );
    705 
    706     w = p->uplimit_on_item = gtk_radio_menu_item_new_with_label_from_widget( GTK_RADIO_MENU_ITEM( w ), _( "Limit at XXX" ) );
    707     gtk_menu_shell_append( GTK_MENU_SHELL( sub ), w );
    708     gtk_widget_show( w );
    709     g_object_set_data( G_OBJECT( w ), ENABLED_KEY, GINT_TO_POINTER( 1 ) );
    710     g_signal_connect( w, "toggled", G_CALLBACK(onUploadLimitedToggled), p );
    711 
    712     w = gtk_separator_menu_item_new( );
    713     gtk_menu_shell_append( GTK_MENU_SHELL( sub ), w );
    714     gtk_widget_show( w );
    715 
    716     for( i=0, n=G_N_ELEMENTS(stockSpeeds); i<n; ++i ) {
    717         char buf[128];
    718         tr_strlspeed( buf, stockSpeeds[i], sizeof( buf ) );
    719         w = gtk_menu_item_new_with_label( buf );
    720         gtk_menu_shell_append( GTK_MENU_SHELL( sub ), w );
    721         gtk_widget_show( w );
    722         g_object_set_data( G_OBJECT( w ), SPEED_KEY, GINT_TO_POINTER( stockSpeeds[i] ) );
    723         g_signal_connect( w, "activate", G_CALLBACK(onUploadLimitSet), p );
    724     }
    725     gtk_menu_item_set_submenu( GTK_MENU_ITEM( up ), sub );
    726 
    727     w = gtk_separator_menu_item_new( );
    728     gtk_menu_shell_append( GTK_MENU_SHELL( top ), w );
    729     gtk_widget_show( w );
    730 
    731     up = gtk_menu_item_new_with_label( _( "Stop Seeding at Ratio" ) );
    732     gtk_menu_shell_append( GTK_MENU_SHELL( top ), up );
    733     gtk_widget_show( up );
    734 
    735     sub = gtk_menu_new( );
    736 
    737     w = p->ratio_off_item = gtk_radio_menu_item_new_with_label( NULL, _( "Seed Forever" ) );
    738     gtk_menu_shell_append( GTK_MENU_SHELL( sub ), w );
    739     gtk_widget_show( w );
    740     g_object_set_data( G_OBJECT( w ), ENABLED_KEY, GINT_TO_POINTER( 0 ) );
    741     g_signal_connect( w, "toggled", G_CALLBACK(onRatioToggled), p );
    742 
    743     w = p->ratio_on_item = gtk_radio_menu_item_new_with_label_from_widget( GTK_RADIO_MENU_ITEM( w ), _( "Stop at Ratio (XXX)" ) );
    744     gtk_menu_shell_append( GTK_MENU_SHELL( sub ), w );
    745     gtk_widget_show( w );
    746     g_object_set_data( G_OBJECT( w ), ENABLED_KEY, GINT_TO_POINTER( 1 ) );
    747     g_signal_connect( w, "toggled", G_CALLBACK(onRatioToggled), p );
    748 
    749     w = gtk_separator_menu_item_new( );
    750     gtk_menu_shell_append( GTK_MENU_SHELL( sub ), w );
    751     gtk_widget_show( w );
    752 
    753     for( i=0, n=G_N_ELEMENTS(stockRatios); i<n; ++i ) {
    754         char buf[128];
    755         tr_strlratio( buf, stockRatios[i]/100.0, sizeof( buf ) );
    756         w = gtk_menu_item_new_with_label( buf );
    757         gtk_menu_shell_append( GTK_MENU_SHELL( sub ), w );
    758         gtk_widget_show( w );
    759         g_object_set_data( G_OBJECT( w ), SPEED_KEY, GINT_TO_POINTER( stockRatios[i] ) );
    760         g_signal_connect( w, "activate", G_CALLBACK(onRatioSet), p );
    761     }
    762     gtk_menu_item_set_submenu( GTK_MENU_ITEM( up ), sub );
    763 
     734
     735    m = gtk_menu_item_new_with_label( _( "Limit Download Speed" ) );
     736    gtk_menu_item_set_submenu( GTK_MENU_ITEM( m ), createSpeedMenu( p, TR_DOWN ) );
     737    gtk_menu_shell_append( GTK_MENU_SHELL( top ), m );
     738
     739    m = gtk_menu_item_new_with_label( _( "Limit Upload Speed" ) );
     740    gtk_menu_item_set_submenu( GTK_MENU_ITEM( m ), createSpeedMenu( p, TR_UP ) );
     741    gtk_menu_shell_append( GTK_MENU_SHELL( top ), m );
     742
     743    m = gtk_separator_menu_item_new( );
     744    gtk_menu_shell_append( GTK_MENU_SHELL( top ), m );
     745
     746    m = gtk_menu_item_new_with_label( _( "Stop Seeding at Ratio" ) );
     747    gtk_menu_item_set_submenu( GTK_MENU_ITEM( m ), createRatioMenu( p ) );
     748    gtk_menu_shell_append( GTK_MENU_SHELL( top ), m );
     749
     750    gtk_widget_show_all( top );
    764751    return top;
    765752}
     
    771758    char buf2[512];
    772759    gboolean b;
     760    GtkWidget * w;
    773761    PrivateData * p = vp;
    774762
     763    w = p->speedlimit_on_item[TR_DOWN];
    775764    tr_strlspeed( buf1, pref_int_get( TR_PREFS_KEY_DSPEED ), sizeof( buf1 ) );
    776     g_snprintf( buf2, sizeof( buf2 ), _( "Limited to %s" ), buf1 );
    777     gtk_label_set_text( GTK_LABEL( gtk_bin_get_child( GTK_BIN( p->downlimit_on_item ) ) ), buf2 );
     765    gtk_label_set_text( GTK_LABEL( gtk_bin_get_child( GTK_BIN( w ) ) ), buf1 );
    778766
    779767    b = pref_flag_get( TR_PREFS_KEY_DSPEED_ENABLED );
    780     gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM( b ? p->downlimit_on_item : p->downlimit_off_item ), TRUE );
    781 
     768    w = b ? p->speedlimit_on_item[TR_DOWN] : p->speedlimit_off_item[TR_DOWN];
     769    gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM( w ), TRUE );
     770
     771    w = p->speedlimit_on_item[TR_UP];
    782772    tr_strlspeed( buf1, pref_int_get( TR_PREFS_KEY_USPEED ), sizeof( buf1 ) );
    783     g_snprintf( buf2, sizeof( buf2 ), _( "Limited to %s" ), buf1 );
    784     gtk_label_set_text( GTK_LABEL( gtk_bin_get_child( GTK_BIN( p->uplimit_on_item ) ) ), buf2 );
     773    gtk_label_set_text( GTK_LABEL( gtk_bin_get_child( GTK_BIN( w ) ) ), buf1 );
    785774
    786775    b = pref_flag_get( TR_PREFS_KEY_USPEED_ENABLED );
    787     gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM( b ? p->uplimit_on_item : p->uplimit_off_item ), TRUE );
     776    w = b ? p->speedlimit_on_item[TR_UP] : p->speedlimit_off_item[TR_UP];
     777    gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM( w ), TRUE );
    788778
    789779    tr_strlratio( buf1, pref_double_get( TR_PREFS_KEY_RATIO ), sizeof( buf1 ) );
Note: See TracChangeset for help on using the changeset viewer.