Changeset 315

Show
Ignore:
Timestamp:
07/08/07 22:41:14 (1 year ago)
Author:
takkaria
Message:

"I went ahead and hacked the x11 pref code into main-gtk.c, and cleaned it up a bit. I've tested it enough to know that it has reasonable defaults, and that everything appears to be working properly. It doesn't really have any safeguards for if it's fed wacky numbers that are greater then 0, but then, neither does the code it's based on." (closes #209)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main-gtk.c

    r273 r315  
    7171static term_data data[MAX_TERM_DATA]; 
    7272 
     73/* Save the prefs. */ 
     74static void save_prefs(void); 
     75 
     76/* Ignore the prefs. */ 
     77bool ignore_prefs; 
     78 
    7379/* 
    7480 * game in progress 
     
    8187static int num_term = 1; 
    8288 
    83 /* Our glade file */ 
     89/* 
     90 * Remember the number of terminal windows open 
     91 */ 
     92static int term_windows_open = 1; 
     93 
     94/*  
     95 * Our glade file  
     96 */ 
    8497GladeXML *xml; 
    85  
    8698 
    8799/* 
     
    370382static void hook_quit(cptr str) 
    371383{ 
     384        save_prefs(); 
    372385        gtk_exit(0); 
    373386} 
     
    377390{ 
    378391        save_game_gtk(); 
     392        save_prefs(); 
    379393        quit(NULL); 
    380394        gtk_exit(0); 
     
    790804} 
    791805 
     806/* Prefs - copied straight from main-x11.c and hacked. 
     807 * It'd probably be better to use something more gtkish, 
     808 * but at lest we'll have prefs. */ 
     809 
     810static void save_prefs(void) 
     811{ 
     812        FILE *fff; 
     813        int i; 
     814        int x,y; 
     815 
     816        /* Open the settings file */ 
     817        fff = my_fopen(settings, "w"); 
     818 
     819        /* Oops */ 
     820        if (!fff) return; 
     821 
     822        /* Header */ 
     823        fprintf(fff, "# %s GTK settings\n\n", VERSION_NAME); 
     824 
     825        /* Number of term windows to open */ 
     826        fprintf(fff, "TERM_WINS=%d\n\n", term_windows_open); 
     827 
     828        /* Save window prefs */ 
     829        for (i = 0; i < MAX_TERM_DATA; i++) 
     830        { 
     831                term_data *td = &data[i]; 
     832 
     833                if (!td->t.mapped_flag) continue; 
     834 
     835                /* Header */ 
     836                fprintf(fff, "# Term %d\n", i); 
     837 
     838                         
     839                gtk_window_get_position(GTK_WINDOW(td->window), &x, &y); 
     840                 
     841                /* 
     842                 * This doesn't seem to work under various WMs 
     843                 * since the decoration messes the position up 
     844                 * 
     845                 * Hack -- Use saved window positions. 
     846                 * This means that we won't remember ingame repositioned 
     847                 * windows, but also means that WMs won't screw predefined 
     848                 * positions up. -CJN- 
     849                 */ 
     850 
     851                /* Window specific location (x) */ 
     852                fprintf(fff, "AT_X_%d=%d\n", i, x); 
     853 
     854                /* Window specific location (y) */ 
     855                fprintf(fff, "AT_Y_%d=%d\n", i, y); 
     856 
     857                /* Window specific cols */ 
     858                fprintf(fff, "COLS_%d=%d\n", i, td->cols); 
     859 
     860                /* Window specific rows */ 
     861                fprintf(fff, "ROWS_%d=%d\n", i, td->rows); 
     862                 
     863                /* Window specific font name */ 
     864                fprintf(fff, "FONT_%d=%s\n", i, pango_font_description_to_string(td->font)); 
     865 
     866                /* Window specific tile width */ 
     867                fprintf(fff, "TILE_WIDTH_%d=%d\n", i, td->tile_wid); 
     868 
     869                /* Window specific tile height */ 
     870                fprintf(fff, "TILE_HEIGHT_%d=%d\n", i, td->tile_hgt); 
     871 
     872                /* Footer */ 
     873                fprintf(fff, "\n"); 
     874        } 
     875 
     876        /* Close */ 
     877        (void)my_fclose(fff); 
     878} 
     879 
     880static int check_env_i(char* name, int i, int dfault) 
     881{ 
     882        cptr str; 
     883        int val; 
     884        char buf[1024]; 
     885         
     886        sprintf(buf, name, i); 
     887        str = getenv(buf); 
     888        val = (str != NULL) ? atoi(str) : -1; 
     889         
     890        if (val <= 0) val = dfault; 
     891                 
     892        return val; 
     893} 
     894 
     895static int get_value(cptr buf) 
     896{ 
     897        cptr str; 
     898        int i; 
     899         
     900        str = strstr(buf, "="); 
     901        i = (str != NULL) ? atoi(str + 1) : -1; 
     902         
     903        return i; 
     904} 
     905 
     906static void load_prefs(term_data *td, int i) 
     907{ 
     908        cptr font = ""; 
     909 
     910        int x = 0; 
     911        int y = 0; 
     912 
     913        int cols = 80; 
     914        int rows = 24; 
     915 
     916        cptr str; 
     917 
     918        int val; 
     919 
     920        FILE *fff; 
     921 
     922        char buf[1024]; 
     923        char cmd[40]; 
     924        char font_name[256]; 
     925 
     926        int line = 0; 
     927         
     928         
     929 
     930        /* Build the filename and open the file */ 
     931        path_build(settings, sizeof(settings), ANGBAND_DIR_USER, "gtk-settings.prf"); 
     932        fff = my_fopen(settings, "r"); 
     933 
     934        /* File exists */ 
     935        if ((fff) && (!ignore_prefs)) 
     936        { 
     937                /* Process the file */ 
     938                while (0 == my_fgets(fff, buf, sizeof(buf))) 
     939                { 
     940                        /* Count lines */ 
     941                        line++; 
     942 
     943                        /* Skip "empty" lines, "blank" lines, and comments */ 
     944                        if (!buf[0]) continue; 
     945                        if (isspace((unsigned char)buf[0])) continue; 
     946                        if (buf[0] == '#') continue; 
     947 
     948                        /* Window specific location (x) */ 
     949                        sprintf(cmd, "AT_X_%d", i); 
     950 
     951                        if (prefix(buf, cmd)) 
     952                        { 
     953                                x = get_value(buf); 
     954                                continue; 
     955                        } 
     956                         
     957                        /* Window specific location (y) */ 
     958                        sprintf(cmd, "AT_Y_%d", i); 
     959 
     960                        if (prefix(buf, cmd)) 
     961                        { 
     962                                y = get_value(buf); 
     963                                continue; 
     964                        } 
     965                 
     966                        /* Window specific cols */ 
     967                        sprintf(cmd, "COLS_%d", i); 
     968 
     969                        if (prefix(buf, cmd)) 
     970                        { 
     971                                val = get_value(buf); 
     972                                if (val > 0) cols = val; 
     973                                continue; 
     974                        } 
     975                                 
     976 
     977                        /* Window specific rows */ 
     978                        sprintf(cmd, "ROWS_%d", i); 
     979 
     980                        if (prefix(buf, cmd)) 
     981                        { 
     982                                val = get_value(buf); 
     983                                if (val > 0) rows = val; 
     984                                continue; 
     985                        } 
     986 
     987                        /* Window specific font name */ 
     988                        sprintf(cmd, "FONT_%d", i); 
     989 
     990                        if (prefix(buf, cmd)) 
     991                        { 
     992                                str = strstr(buf, "="); 
     993                                if (str != NULL) 
     994                                { 
     995                                        my_strcpy(font_name, str + 1, sizeof(font_name)); 
     996                                        font = font_name; 
     997                                } 
     998                                continue; 
     999                        } 
     1000 
     1001                        /* Window specific tile width */ 
     1002                        sprintf(cmd, "TILE_WIDTH_%d", i); 
     1003 
     1004                        if (prefix(buf, cmd)) 
     1005                        { 
     1006                                val = get_value(buf); 
     1007                                if (val > 0) td->tile_wid = val; 
     1008                                continue; 
     1009                        } 
     1010 
     1011                        /* Window specific tile height */ 
     1012                        sprintf(cmd, "TILE_HEIGHT_%d", i); 
     1013 
     1014                        if (prefix(buf, cmd)) 
     1015                        { 
     1016                                val = get_value(buf); 
     1017                                if (val > 0) td->tile_hgt = val; 
     1018                                continue; 
     1019                        } 
     1020                } 
     1021 
     1022                /* Close */ 
     1023                my_fclose(fff); 
     1024        } 
     1025 
     1026        /* 
     1027         * Env-vars overwrite the settings in the settings file 
     1028         */ 
     1029 
     1030        x = check_env_i("ANGBAND_X11_AT_X_%d", i, x); 
     1031        y = check_env_i("ANGBAND_X11_AT_Y_%d", i, y); 
     1032        cols = check_env_i("ANGBAND_X11_COLS_%d", i,  cols); 
     1033        rows = check_env_i("ANGBAND_X11_ROWS_%d", i, rows); 
     1034                 
     1035        /* Window specific font name */ 
     1036        sprintf(buf, "ANGBAND_X11_FONT_%d", i); 
     1037        str = getenv(buf); 
     1038        if (str) font = str; 
     1039         
     1040        if (cols <= 0) cols = 80; 
     1041        if (rows <= 0) rows = 24; 
     1042        if ((x <= 0) && (y <= 0)) 
     1043        { 
     1044                x = 100; 
     1045                y = 100; 
     1046        } 
     1047         
     1048        td->cols = cols; 
     1049        td->rows = rows; 
     1050         
     1051        if (font != "") load_font_by_name(td, font); 
     1052         
     1053        gtk_widget_set_size_request(GTK_WIDGET(td->drawing_area),  td->cols * td->font_wid + 1, td->rows * td->font_hgt + 1); 
     1054        gtk_window_move( GTK_WINDOW(td->window), x, y); 
     1055} 
     1056 
    7921057gboolean expose_event_handler(GtkWidget *widget, GdkEventExpose *event, gpointer user_data) 
    7931058{ 
     
    8381103        /* Activate (important) */ 
    8391104        Term_activate(t); 
    840  
     1105         
    8411106        /* Success */ 
    8421107        return (0); 
     
    8881153        /* Set attributes */ 
    8891154        gtk_window_set_title(GTK_WINDOW(td->window), td->name); 
    890         gtk_widget_set_size_request(GTK_WIDGET(td->drawing_area), td->cols * td->font_wid + 1, td->rows * td->font_hgt + 1); 
    891         gtk_window_move( GTK_WINDOW(td->window), 100, 100); 
     1155         
     1156        /* Load window and other prefs */ 
     1157        load_prefs(td, i); 
    8921158 
    8931159        /* Create a pixmap as buffer for screen updates */ 
     
    9091175 
    9101176const char help_gtk[] = 
    911         "GTK for X11, subopts -n<windows> and standard GTK options"; 
     1177        "GTK for X11, subopts -n<windows>, -i to ignore prefs, and standard GTK options"; 
    9121178 
    9131179 
     
    9241190        /* Parse args */ 
    9251191        for (i = 1; i < argc; i++) 
    926         { 
     1192        {       
    9271193                if (prefix(argv[i], "-n")) 
    9281194                { 
     
    9321198                        continue; 
    9331199                } 
    934  
     1200                 
     1201                if (prefix(argv[i], "-i")) 
     1202                { 
     1203                        plog("Ignoring preferences."); 
     1204                        ignore_prefs = TRUE; 
     1205                        continue; 
     1206                } 
     1207                 
    9351208                plog_fmt("Ignoring option: %s", argv[i]); 
    9361209        }