Merge pull request #6 from wdlkmpx/patch1

consistent indentation
This commit is contained in:
Eugene Morozov 2021-01-16 15:20:57 +03:00 committed by GitHub
commit f96b8abc49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 2850 additions and 2655 deletions

View File

@ -22,19 +22,20 @@
volatile sig_atomic_t _child_writer_alive = 0;
gboolean _child_writer_dead_warned = 0;
int child_writer_alive(void) {
int child_writer_alive(void){
return _child_writer_alive;
}
int child_writer_dead_handler(void) {
if(_child_writer_alive == 0 && _child_writer_dead_warned == 0) {
if (_child_writer_alive == 0 && _child_writer_dead_warned == 0) {
_child_writer_dead_warned = 1;
return 1;
}
return 0;
}
void child_process_score_writer(int chfd, gchar *score_file) {
void child_process_score_writer(int chfd, gchar *score_file)
{
fd_set rfds;
int i;
int fd=-1;
@ -44,11 +45,13 @@ void child_process_score_writer(int chfd, gchar *score_file) {
struct flock lockinfo;
gchar *score_file_full = g_strconcat(LOCALSTATEDIR, score_file, NULL);
while(1) {
while (1)
{
FD_ZERO(&rfds);
FD_SET(chfd, &rfds);
if(select(chfd + 1, &rfds, NULL, NULL, NULL) > 0) {
if(read(chfd, &sz, sizeof(sz)) <= 0) {
if (select(chfd + 1, &rfds, NULL, NULL, NULL) > 0)
{
if (read(chfd, &sz, sizeof(sz)) <= 0) {
exit(0);
}
/* block signals before writing, to prevent possible file corruption */
@ -59,53 +62,56 @@ void child_process_score_writer(int chfd, gchar *score_file) {
sigaddset(&sset, SIGTERM);
sigprocmask(SIG_BLOCK, &sset, NULL);
fd = open(score_file_full, O_WRONLY | O_TRUNC);
if(fd != -1) {
if (fd != -1) {
/* get write lock before writing scores */
lockinfo.l_whence = SEEK_SET;
lockinfo.l_start = 0;
lockinfo.l_len = 0;
i = 0;
while(1) {
while (1) {
lockinfo.l_type=F_WRLCK;
if(!fcntl(fd, F_SETLK, &lockinfo)) break;
if(i >= 3) {
if (!fcntl(fd, F_SETLK, &lockinfo)) {
break;
}
if (i >= 3) {
close(fd);
fd = -1;
}
i++;
}
}
while(sz > 0) {
while (sz > 0) {
buf=g_malloc(sz);
read(chfd, buf, sz);
if(fd != -1) {
if (fd != -1) {
write(fd, buf, sz);
}
g_free(buf);
read(chfd, &sz, sizeof(sz));
}
if(fd != -1) {
if (fd != -1) {
close(fd);
} else {
/* FIXME: here should be some sort of error
reporting to parent */
/* FIXME: here should be some sort of error reporting to parent */
}
sigprocmask(SIG_UNBLOCK, &sset, NULL);
}
}
}
static void sigchld_handler(int param) {
static void sigchld_handler(int param)
{
pid_t ret = waitpid(0, NULL, WNOHANG);
if(ret > 0) { /* score writer process killed by bastards! */
if (ret > 0) { /* score writer process killed by bastards! */
_child_writer_alive = 0;
}
}
int child_setup(gchar *score_file) {
int child_setup(gchar *score_file)
{
pid_t pid;
struct sigaction sact;
int sfds[2];
@ -118,19 +124,19 @@ int child_setup(gchar *score_file) {
#ifdef SA_RESTART
sact.sa_flags |= SA_RESTART;
#endif
if(sigaction(SIGCHLD, &sact, NULL) < 0) {
if (sigaction(SIGCHLD, &sact, NULL) < 0) {
printf("cannot setup SIGCHLD handler.\n");
return -1;
}
if(pipe(sfds) == -1) {
if (pipe(sfds) == -1) {
printf("pipe() failed: %s\n", strerror(errno));
return -1;
}
pid = fork();
if(pid == -1) {
if (pid == -1) {
printf("cannot fork: %s\n", strerror(errno));
return -1;
} else if(pid == 0) {
} else if (pid == 0) {
close(sfds[1]);
child_process_score_writer(sfds[0], score_file);
}

View File

@ -141,18 +141,21 @@ void rules_set(gint width, gint height, gint colors, gint next, gint destroy) {
}
gboolean _rules_check_rules(GtkbGameRules r) {
if((r.width < 4 || r.width > 99) ||
(r.height < 4 || r.height > 99) ||
(r.colors < 3 || r.colors > 99) ||
(r.next < 2 || r.next > 99) ||
(r.destroy < 3 || r.destroy > 99))
if ((r.width < 4 || r.width > 99)
|| (r.height < 4 || r.height > 99)
|| (r.colors < 3 || r.colors > 99)
|| (r.next < 2 || r.next > 99)
|| (r.destroy < 3 || r.destroy > 99))
{
return 0;
}
return 1;
}
gchar *_rules_to_str(GtkbGameRules r) {
if(!_rules_check_rules(r))
if (!_rules_check_rules(r)) {
return NULL;
}
return g_strdup_printf(_save_fmt, r.width, r.height, r.colors, r.next, r.destroy);
}
@ -171,21 +174,23 @@ gint rules_get_str_len(void) {
gboolean rules_check_str(gchar *rstr) {
GtkbGameRules r;
if(strlen(rstr) != _save_fmt_len ||
if (strlen(rstr) != _save_fmt_len ||
strstr(rstr, " ") ||
strstr(rstr, "\t") ||
strstr(rstr, "\r") ||
strstr(rstr, "\n") ||
sscanf(rstr, _save_fmt, &r.width, &r.height, &r.colors, &r.next, &r.destroy) != 5 ||
!_rules_check_rules(r))
{
return 0;
}
return 1;
}
gboolean rules_get_from_str(gchar *s, gint *width, gint *height, gint *colors, gint *next, gint *destroy) {
if(!rules_check_str(s))
if (!rules_check_str(s)) {
return 0;
}
sscanf(s, _save_fmt, width, height, colors, next, destroy);
return 1;
}
@ -193,9 +198,9 @@ gboolean rules_get_from_str(gchar *s, gint *width, gint *height, gint *colors, g
gchar *rules_conv_3_0_to_str(gchar *w, gchar *h, gchar *c, gchar *n, gchar *d) {
GtkbGameRules r;
if(!w[0] || !h[0] || !c[0] || !n[0] || !d[0])
if (!w[0] || !h[0] || !c[0] || !n[0] || !d[0]) {
return NULL;
}
r.width = strtol(w, NULL, 10);
r.height = strtol(h, NULL, 10);
r.colors = strtol(c, NULL, 10);
@ -209,8 +214,9 @@ gboolean rules_is_current_str(gchar *r) {
gboolean rval = 0;
gchar *cur = _rules_to_str(_rules);
if(strcmp(r, cur) == 0)
if (strcmp(r, cur) == 0) {
rval = 1;
}
g_free(cur);
return rval;
@ -240,34 +246,41 @@ void game_save_state_for_undo(void) {
}
void game_restore_state_from_undo(void) {
if(_score_undo == -1 || _hi_score_undo == -1)
if (_score_undo == -1 || _hi_score_undo == -1) {
/* cannot undo */
return;
}
memcpy(_board, _board_undo, sizeof(gint) * _rules.width * _rules.height);
memcpy(_next_colors, _next_colors_undo, sizeof(gint) * _rules.next);
_score = _score_undo;
_hi_score = _hi_score_undo;
}
void game_init_game(gint *balls, gint *nextballs) {
if(_board)
void game_init_game(gint *balls, gint *nextballs)
{
if (_board) {
g_free(_board);
if(_board_undo)
}
if (_board_undo) {
g_free(_board_undo);
if(_board_destroys)
}
if (_board_destroys) {
g_free(_board_destroys);
if(_next_colors)
}
if (_next_colors) {
g_free(_next_colors);
if(_next_colors_undo)
}
if (_next_colors_undo) {
g_free(_next_colors_undo);
}
_board = g_malloc0(sizeof(gint) * _rules.width * _rules.height);
_board_destroys = g_malloc0(sizeof(gint) * _rules.width * _rules.height);
if(balls) {
if (balls) {
memcpy(_board, balls, sizeof(gint) * _rules.width * _rules.height);
}
_board_undo = g_malloc0(sizeof(gint) * _rules.width * _rules.height);
_next_colors = g_malloc0(sizeof(gint) * _rules.next);
if(nextballs) {
if (nextballs) {
memcpy(_next_colors, nextballs, sizeof(gint) * _rules.next);
}
_next_colors_undo = g_malloc0(sizeof(gint) * _rules.next);
@ -280,10 +293,10 @@ gint game_count_free_cells(void) {
gint i, counter = 0;
gint *bp = _board;
for(i = 0; i < _rules.width * _rules.height; i++)
if(*bp++ == 0)
for (i = 0; i < _rules.width * _rules.height; i++) {
if (*bp++ == 0)
counter++;
}
return counter;
}
@ -304,44 +317,51 @@ gint *game_get_next_as_int_arr(void) {
}
gint board_get_at_node(gint node) {
if(node >= _rules.width * _rules.height)
if (node >= _rules.width * _rules.height) {
return 0;
}
return _board[node];
}
gint board_get_at_xy(gint x, gint y) {
if(x >= _rules.width || y > _rules.height)
if (x >= _rules.width || y > _rules.height) {
return 0;
}
return _board[y * _rules.width + x];
}
gint board_get_destroy_at_xy(gint x, gint y) {
if(x >= _rules.width || y > _rules.height)
if (x >= _rules.width || y > _rules.height) {
return 0;
}
return _board_destroys[y * _rules.width + x];
}
void board_set_at_node(gint node, gint col) {
if(node >= _rules.width * _rules.height)
if (node >= _rules.width * _rules.height) {
return;
}
_board[node] = col;
}
void board_set_at_xy(gint x, gint y, gint col) {
if(x >= _rules.width || y > _rules.height)
if (x >= _rules.width || y > _rules.height) {
return;
}
_board[y * _rules.width + x] = col;
}
gint next_get(gint num) {
if(num >= _rules.next)
if (num >= _rules.next) {
return 0;
}
return _next_colors[num];
}
void next_set(gint num, gint col) {
if(num >= _rules.next)
if (num >= _rules.next) {
return;
}
_next_colors[num] = col;
}
@ -350,21 +370,23 @@ void timer_start(void) {
}
gboolean timer_is_running(void) {
if(_timer_start_time == -1 || _timer_limit <= 0)
if (_timer_start_time == -1 || _timer_limit <= 0) {
return 0;
}
return 1;
}
gboolean timer_is_expired(void) {
time_t nowt = time(NULL);
if(nowt - _timer_start_time >= _timer_limit)
if (nowt - _timer_start_time >= _timer_limit) {
return 1;
}
return 0;
}
gint timer_get_remaining(void) {
time_t nowt = time(NULL);
time_t nowt = time (NULL);
return _timer_limit + _timer_start_time - nowt;
}
@ -381,78 +403,87 @@ struct gtkb_animarray {
gint color, x, y, phase, time;
};
int animsort(const void *a, const void *b) {
if(((const struct gtkb_animarray *)a)->time == ((const struct gtkb_animarray *)b)->time) return 0;
if(((const struct gtkb_animarray *)a)->time > ((const struct gtkb_animarray *)b)->time) return 1;
int animsort(const void *a, const void *b)
{
if (((const struct gtkb_animarray *)a)->time == ((const struct gtkb_animarray *)b)->time) {
return 0;
}
if (((const struct gtkb_animarray *)a)->time > ((const struct gtkb_animarray *)b)->time) {
return 1;
}
return -1;
}
gint game_destroy_lines(gboolean count_score) {
gint game_destroy_lines(gboolean count_score)
{
gint x, y, i, j;
gint *del, have_del = 0;
if(rules_get_width() < rules_get_destroy() &&
if (rules_get_width() < rules_get_destroy() &&
rules_get_height() < rules_get_destroy()) { /* destroy is impossible */
return 0;
}
del = g_malloc0(rules_get_width() * rules_get_height() * sizeof(gint));
for(y = 0; y < rules_get_height(); y++) {
for(x = 0; x < rules_get_width(); x++) {
if(board_get_at_xy(x, y) != 0) {
for (y = 0; y < rules_get_height(); y++)
{
for (x = 0; x < rules_get_width(); x++)
{
if (board_get_at_xy(x, y) != 0) {
/* horizontal */
if(rules_get_width() - x >= rules_get_destroy()) {
for(i = 1;
if (rules_get_width() - x >= rules_get_destroy()) {
for (i = 1;
i < rules_get_width() - x &&
board_get_at_xy(x + i, y) == board_get_at_xy(x, y);
i++);
if(i >= rules_get_destroy()) {
if (i >= rules_get_destroy()) {
have_del = 1;
for(j = 0; j < i; j ++) {
for (j = 0; j < i; j ++) {
del[y * rules_get_width() + x + j] = 1;
}
}
}
/* vertical */
if(rules_get_height() - y >= rules_get_destroy()) {
for(i = 1;
if (rules_get_height() - y >= rules_get_destroy()) {
for (i = 1;
i < rules_get_height() - y &&
board_get_at_xy(x, y + i) == board_get_at_xy(x, y);
i++);
if(i >= rules_get_destroy()) {
if (i >= rules_get_destroy()) {
have_del = 1;
for(j = 0; j < i; j ++) {
for (j = 0; j < i; j ++) {
del[(y + j) * rules_get_width() + x] = 1;
}
}
}
/* diagonal left -> right */
if(rules_get_width() - x >= rules_get_destroy() &&
if (rules_get_width() - x >= rules_get_destroy() &&
rules_get_height() - y >= rules_get_destroy()) {
for(i = 1;
for (i = 1;
i < rules_get_width() - x &&
i < rules_get_height() - y &&
board_get_at_xy(x + i, y + i) == board_get_at_xy(x, y);
i++);
if(i >= rules_get_destroy()) {
if (i >= rules_get_destroy()) {
have_del = 1;
for(j = 0; j < i; j ++) {
for (j = 0; j < i; j ++) {
del[(y + j) * rules_get_width() + x + j] = 1;
}
}
}
/* diagonal right -> left */
if(x + 1 >= rules_get_destroy() &&
if (x + 1 >= rules_get_destroy() &&
rules_get_height() - y >= rules_get_destroy()) {
for(i = 1;
for (i = 1;
i <= x &&
i < rules_get_height() - y &&
board_get_at_xy(x - i, y + i) == board_get_at_xy(x, y);
i++);
if(i >= rules_get_destroy()) {
if (i >= rules_get_destroy()) {
have_del = 1;
for(j = 0; j < i; j ++) {
for (j = 0; j < i; j ++) {
del[(y + j) * rules_get_width() + x - j] = 1;
}
}
@ -460,22 +491,27 @@ gint game_destroy_lines(gboolean count_score) {
}
}
}
i = 0;
if(have_del) {
if(pref_get_show_destroy()) {
if (have_del)
{
if (pref_get_show_destroy())
{
gint animcadres = 0, animpos = 0, animtime;
struct gtkb_animarray *animarray;
struct timeval tvs, tve;
animarray = g_new0(struct gtkb_animarray, rules_get_width() * rules_get_height() * gtkbTheme->maxdestphases);
for(y = 0; y < rules_get_height(); y++) {
for(x = 0; x < rules_get_width(); x++) {
if(del[y * rules_get_width() + x] == 1) {
for (y = 0; y < rules_get_height(); y++)
{
for (x = 0; x < rules_get_width(); x++)
{
if (del[y * rules_get_width() + x] == 1) {
gint color = board_get_at_xy(x, y);
animcadres += gtkbTheme->balls[color - 1].destroyphases + 1;
for(j = 0, animtime = 0; j <= gtkbTheme->balls[color - 1].destroyphases; j++) {
if(j != gtkbTheme->balls[color - 1].destroyphases) {
for (j = 0, animtime = 0; j <= gtkbTheme->balls[color - 1].destroyphases; j++) {
if (j != gtkbTheme->balls[color - 1].destroyphases) {
animarray[animpos].color = color;
animarray[animpos].phase = j;
animtime += gtkbTheme->balls[color - 1].destroydelays[j];
@ -494,10 +530,11 @@ gint game_destroy_lines(gboolean count_score) {
qsort(animarray, animcadres, sizeof(struct gtkb_animarray), animsort);
lock_actions(1);
draw_board();
for(animtime = 0, i = 0; i < animcadres;) {
for (animtime = 0, i = 0; i < animcadres;)
{
gettimeofday(&tvs, NULL);
gint isav = i;
for(; animtime == animarray[i].time && i < animcadres; i++) {
for (; animtime == animarray[i].time && i < animcadres; i++) {
draw_ball(animarray[i].color, animarray[i].x, animarray[i].y, 0, animarray[i].phase + 1);
_board_destroys[animarray[i].y * rules_get_width() + animarray[i].x] = animarray[i].phase + 1;
}
@ -512,9 +549,9 @@ gint game_destroy_lines(gboolean count_score) {
lock_actions(0);
}
for(i = 0, y = 0; y < rules_get_height(); y++) {
for(x = 0; x < rules_get_width(); x++) {
if(del[y * rules_get_width() + x] == 1) {
for (i = 0, y = 0; y < rules_get_height(); y++) {
for (x = 0; x < rules_get_width(); x++) {
if (del[y * rules_get_width() + x] == 1) {
i++;
board_set_at_xy(x, y, 0);
}

183
src/gfx.c
View File

@ -19,7 +19,7 @@
#include "mainwin.h"
#include "game.h"
GdkPixmap *_pixmap=NULL;
GdkPixmap *_pixmap = NULL;
/* x and y of cell "under pointer" */
gint _pointer_x = 0, _pointer_y = 0;
@ -45,15 +45,18 @@ gint xy_to_cell_number(gint x, gint y) {
return find_node_of_x_y(x, y, rules_get_width());
}
gint get_jump_phase(gint x, gint y) {
if(!_animation_in_progress || xy_to_cell_number(x, y) != _jumping_ball)
if (!_animation_in_progress || xy_to_cell_number(x, y) != _jumping_ball) {
return 0;
}
return _phase;
}
void set_jump_phase(gint p) {
if(!_animation_in_progress)
if (!_animation_in_progress) {
return;
}
_phase = p;
}
@ -64,7 +67,7 @@ gint get_destroy_phase(gint x, gint y) {
void update_rectangle(gint x, gint y, gint w, gint h) {
GtkWidget *widget = mw_get_da();
if(!widget || !_pixmap) {
if (!widget || !_pixmap) {
return;
}
gdk_draw_drawable (gtk_widget_get_window (widget),
@ -72,7 +75,8 @@ void update_rectangle(gint x, gint y, gint w, gint h) {
_pixmap, x, y, x, y, w, h);
}
void draw_ball_no_update(gint ballcolor, gint x, gint y, gint jumpnum, gint destroynum) {
void draw_ball_no_update(gint ballcolor, gint x, gint y, gint jumpnum, gint destroynum)
{
GtkWidget *widget = mw_get_da();
GdkGC *gc = widget->style->fg_gc[gtk_widget_get_state(widget)];
gint cxs = gtkbTheme->emptycell.xsize;
@ -80,11 +84,11 @@ void draw_ball_no_update(gint ballcolor, gint x, gint y, gint jumpnum, gint dest
GtkbPixmap *obj;
gint xr, yr;
if(!widget || !_pixmap) {
if (!widget || !_pixmap) {
return;
}
if(pref_get_show_highlight() && x == _pointer_x && y == _pointer_y) {
if (pref_get_show_highlight() && x == _pointer_x && y == _pointer_y) {
obj = &gtkbTheme->hemptycell;
} else {
obj = &gtkbTheme->emptycell;
@ -97,15 +101,15 @@ void draw_ball_no_update(gint ballcolor, gint x, gint y, gint jumpnum, gint dest
cxs, cys,
GDK_RGB_DITHER_NONE, 0, 0);
if(ballcolor > 0) { /* ball */
if(!jumpnum && !destroynum) { /* still ball */
if (ballcolor > 0) { /* ball */
if (!jumpnum && !destroynum) { /* still ball */
obj = &gtkbTheme->balls[ballcolor - 1].ball;
} else if(jumpnum) { /* jumping ball */
} else if (jumpnum) { /* jumping ball */
obj = &gtkbTheme->balls[ballcolor - 1].jump[jumpnum - 1];
} else { /* disappearing ball */
obj = &gtkbTheme->balls[ballcolor - 1].destroy[destroynum - 1];
}
} else if(ballcolor < 0) { /* paw */
} else if (ballcolor < 0) { /* paw */
obj = &gtkbTheme->paws[-1 - ballcolor];
} else { /* empty cell */
return;
@ -144,12 +148,12 @@ void redraw_pointer(void) {
void draw_board(void) {
gint i, j;
if(!mw_get_da()) {
if (!mw_get_da()) {
return;
}
for(j = 0; j < rules_get_height(); j++) {
for(i = 0; i < rules_get_width(); i++) {
if(!_animation_in_progress || ((xy_to_cell_number(i, j)) != _jumping_ball)) {
for (j = 0; j < rules_get_height(); j++) {
for (i = 0; i < rules_get_width(); i++) {
if (!_animation_in_progress || ((xy_to_cell_number(i, j)) != _jumping_ball)) {
draw_ball_no_update(board_get_at_xy(i, j), i, j, 0, 0);
}
}
@ -158,14 +162,16 @@ void draw_board(void) {
}
gint inc_with_limit(gint val, gint lim) {
if(val < lim)
if (val < lim) {
return val + 1;
}
return lim;
}
gint dec_with_limit(gint val, gint lim) {
if(val > lim)
if (val > lim) {
return val - 1;
}
return lim;
}
@ -174,39 +180,41 @@ gboolean animate_ball(gpointer data) {
int x, y, bc = board_get_at_node(_jumping_ball);
find_x_y_of_the_node(&x, &y, _jumping_ball, rules_get_width(), rules_get_height());
if(widget != NULL) {
if (widget != NULL) {
draw_ball(bc, x, y, _phase + 1, 0);
}
++_phase;
if(_phase >= gtkbTheme->balls[bc - 1].jumpphases) {
if (_phase >= gtkbTheme->balls[bc - 1].jumpphases) {
_phase = 0;
}
_timer_tag = g_timeout_add(gtkbTheme->balls[bc - 1].jumpdelays[_phase], animate_ball, widget);
_timer_tag = g_timeout_add (gtkbTheme->balls[bc - 1].jumpdelays[_phase], animate_ball, widget);
return FALSE;
}
void stop_jumping_animation(void) {
if(_animation_in_progress) {
g_source_remove(_timer_tag);
if (_animation_in_progress) {
g_source_remove (_timer_tag);
_animation_in_progress = FALSE;
}
}
void move_pointer_to(gint x, gint y) {
void move_pointer_to(gint x, gint y)
{
gint i, jp, dp, xc = _pointer_x, yc = _pointer_y;
if(x >= rules_get_width() || y >= rules_get_height() || x < 0 || y < 0)
if (x >= rules_get_width() || y >= rules_get_height() || x < 0 || y < 0) {
/* "boundary check" */
return;
if(_pointer_x == x && _pointer_y == y)
}
if (_pointer_x == x && _pointer_y == y) {
return;
}
_pointer_x = x;
_pointer_y = y;
for(i = 0; i < 2; i++) {
if(i) {
for (i = 0; i < 2; i++) {
if (i) {
xc = _pointer_x;
yc = _pointer_y;
}
@ -219,17 +227,21 @@ void move_pointer_to(gint x, gint y) {
void move_pointer(Direction dir) {
gint xn = _pointer_x, yn = _pointer_y;
if(dir == DIR_LEFT || dir == DIR_UP_LEFT || dir == DIR_DOWN_LEFT)
if (dir == DIR_LEFT || dir == DIR_UP_LEFT || dir == DIR_DOWN_LEFT) {
xn = dec_with_limit(_pointer_x, 0);
if(dir == DIR_RIGHT || dir == DIR_UP_RIGHT || dir == DIR_DOWN_RIGHT)
}
if (dir == DIR_RIGHT || dir == DIR_UP_RIGHT || dir == DIR_DOWN_RIGHT) {
xn = inc_with_limit(_pointer_x, rules_get_width() - 1);
if(dir == DIR_UP || dir == DIR_UP_LEFT || dir == DIR_UP_RIGHT)
}
if (dir == DIR_UP || dir == DIR_UP_LEFT || dir == DIR_UP_RIGHT) {
yn = dec_with_limit(_pointer_y, 0);
if(dir == DIR_DOWN || dir == DIR_DOWN_LEFT || dir == DIR_DOWN_RIGHT)
}
if (dir == DIR_DOWN || dir == DIR_DOWN_LEFT || dir == DIR_DOWN_RIGHT) {
yn = inc_with_limit(_pointer_y, rules_get_height() - 1);
if(xn == _pointer_x && yn == _pointer_y)
}
if (xn == _pointer_x && yn == _pointer_y) {
return;
}
move_pointer_to(xn, yn);
}
@ -237,20 +249,20 @@ void move_pointer(Direction dir) {
void pointer_pressed(gint x, gint y) {
gint xn, yn, node, bc;
if(x == -1 || y == -1) {
if (x == -1 || y == -1) {
x = _pointer_x;
y = _pointer_y;
}
node = xy_to_cell_number(x, y);
if(_animation_in_progress && node == _jumping_ball) {
if (_animation_in_progress && node == _jumping_ball) {
/* already selected ball clicked */
return;
}
if(board_get_at_node(node) > 0) {
if (board_get_at_node(node) > 0) {
/* cell with ball pressed */
if(_animation_in_progress) {
if (_animation_in_progress) {
/* another ball already jumping, stop him */
stop_jumping_animation();
find_x_y_of_the_node(&xn, &yn, _jumping_ball, rules_get_width(), rules_get_height());
@ -262,13 +274,13 @@ void pointer_pressed(gint x, gint y) {
_animation_in_progress = TRUE;
_phase = 0;
_timer_tag = g_timeout_add(gtkbTheme->balls[bc - 1].jumpdelays[_phase], animate_ball, mw_get_da());
} else if(_animation_in_progress && have_path(_jumping_ball, node)) {
} else if (_animation_in_progress && have_path(_jumping_ball, node)) {
/* cell without ball pressed while ball selected */
game_save_state_for_undo();
menu_set_sensitive_undo(TRUE);
stop_jumping_animation();
move_ball(_jumping_ball, node);
if(!destroy_lines(TRUE)) {
if (!destroy_lines(TRUE)) {
new_turn(rules_get_next(), FALSE);
}
timer_start();
@ -280,16 +292,16 @@ void reinit_board(gint *newboard, gint *newnext, gint score, gint oldnext) {
gint cells = rules_get_width() * rules_get_height() - 1;
stop_jumping_animation();
if(_pointer_x >= rules_get_width()) {
if (_pointer_x >= rules_get_width()) {
_pointer_x = rules_get_width() - 1;
}
if(_pointer_y >= rules_get_height()) {
if (_pointer_y >= rules_get_height()) {
_pointer_y = rules_get_height() - 1;
}
if(rules_get_destroy() > cells) {
if (rules_get_destroy() > cells) {
rules_set_destroy(cells);
}
if(rules_get_next() > cells) {
if (rules_get_next() > cells) {
rules_set_next(cells);
}
game_init_game(newboard, newnext);
@ -297,7 +309,7 @@ void reinit_board(gint *newboard, gint *newnext, gint score, gint oldnext) {
mw_set_hi_score(score > sb[0].score ? score : sb[0].score);
mw_set_user_score(score);
remake_board(oldnext, newnext ? 1 : 0);
if(!newboard) {
if (!newboard) {
new_game();
} else {
menu_set_sensitive_undo(FALSE);
@ -308,15 +320,16 @@ void reinit_board(gint *newboard, gint *newnext, gint score, gint oldnext) {
void remake_board(gint numoldchilds, gboolean isnextvalid) {
gint cxs, cys, i;
if(numoldchilds && numoldchilds != rules_get_next()) {
if(_small_balls) {
for(i = 0; i < numoldchilds; i++) {
if (numoldchilds && numoldchilds != rules_get_next())
{
if (_small_balls) {
for (i = 0; i < numoldchilds; i++) {
gtk_widget_destroy(_small_balls[i]);
}
}
_small_balls = g_realloc(_small_balls, rules_get_next() * sizeof(GtkWidget *));
for(i = 0; i < rules_get_next(); i++) {
if(isnextvalid) {
for (i = 0; i < rules_get_next(); i++) {
if (isnextvalid) {
_small_balls[i] = gtk_image_new_from_pixbuf(gtkbTheme->balls[next_get(i) - 1].small.pixbuf);
} else {
_small_balls[i] = gtk_image_new_from_pixbuf(gtkbTheme->balls[0].small.pixbuf);
@ -329,7 +342,7 @@ void remake_board(gint numoldchilds, gboolean isnextvalid) {
cxs = gtkbTheme->emptycell.xsize;
cys = gtkbTheme->emptycell.ysize;
gtk_widget_set_size_request(mw_get_da(), rules_get_width() * cxs, rules_get_height() * cys);
if(_pixmap) {
if (_pixmap) {
g_object_unref(_pixmap);
}
_pixmap = gdk_pixmap_new(mw_get_da()->window, rules_get_width() * cxs, rules_get_height() * cys, -1);
@ -339,7 +352,7 @@ void remake_board(gint numoldchilds, gboolean isnextvalid) {
void draw_next_balls(void) {
gint i;
for(i = 0; i < rules_get_next(); i++) {
for (i = 0; i < rules_get_next(); i++) {
gtk_image_set_from_pixbuf(GTK_IMAGE(_small_balls[i]), gtkbTheme->balls[next_get(i) - 1].small.pixbuf);
}
}
@ -352,7 +365,7 @@ gint expose_event(GtkWidget *widget, GdkEventExpose *event) {
gdk_region_get_rectangles(event->region, &rects, &n_rects);
for(i = 0; i < n_rects; i++) {
for (i = 0; i < n_rects; i++) {
gdk_draw_drawable (gtk_widget_get_window (widget),
widget->style->fg_gc[gtk_widget_get_state(widget)],
_pixmap, rects[i].x, rects[i].y, rects[i].x, rects[i].y,
@ -364,35 +377,36 @@ gint expose_event(GtkWidget *widget, GdkEventExpose *event) {
return FALSE;
}
void find_pawnum_and_direction(gint pawx, gint pawy, gint x, gint y, gint *pawnum, gint *direction) {
if(pawy < y) {
void find_pawnum_and_direction(gint pawx, gint pawy, gint x, gint y, gint *pawnum, gint *direction)
{
if (pawy < y) {
*pawnum = 2;
if(*direction == 1) *pawnum = 6;
if(*direction == 3) *pawnum = 7;
if (*direction == 1) *pawnum = 6;
if (*direction == 3) *pawnum = 7;
*direction = 2; /* up */
} else if(pawy > y) {
} else if (pawy > y) {
*pawnum = 0;
if(*direction == 1) *pawnum = 4;
if(*direction == 3) *pawnum = 5;
if (*direction == 1) *pawnum = 4;
if (*direction == 3) *pawnum = 5;
*direction = 0; /* down */
} else if(pawx < x) {
} else if (pawx < x) {
*pawnum = 1;
if(*direction == 0) *pawnum = 4;
if(*direction == 2) *pawnum = 6;
if (*direction == 0) *pawnum = 4;
if (*direction == 2) *pawnum = 6;
*direction = 1; /* left */
} else if(pawx > x) {
} else if (pawx > x) {
*pawnum = 3;
if(*direction == 0) *pawnum = 5;
if(*direction == 2) *pawnum = 7;
if (*direction == 0) *pawnum = 5;
if (*direction == 2) *pawnum = 7;
*direction = 3; /* right */
}
}
gint find_direction(gint pawx, gint pawy, gint x, gint y) {
if(pawy < y) return 2; /* up */
if(pawy > y) return 0; /* down */
if(pawx < x) return 1; /* left */
if(pawx > x) return 3; /* right */
if (pawy < y) return 2; /* up */
if (pawy > y) return 0; /* down */
if (pawx < x) return 1; /* left */
if (pawx > x) return 3; /* right */
return -1; /* should never happens */
}
@ -403,17 +417,18 @@ gboolean have_path(gint source_ball, gint target_ball) {
gint *np = nodes;
gint i;
for(i = 0; i < rules_get_height() * rules_get_width(); i++) {
for (i = 0; i < rules_get_height() * rules_get_width(); i++) {
*np++ = board_get_at_node(i) > 0 ? -1 : 0;
}
nodes[source_ball] = nodes[target_ball] = 0;
if(!find_path(nodes, source_ball, target_ball, path, rules_get_width(), rules_get_height())) {
if (!find_path(nodes, source_ball, target_ball, path, rules_get_width(), rules_get_height())) {
return 0;
}
return 1;
}
void move_ball(gint source_ball, gint target_ball) {
void move_ball(gint source_ball, gint target_ball)
{
gint nodes[rules_get_width() * rules_get_height()];
gint path[rules_get_width() * rules_get_height()];
gint pawx = -1, pawy = -1;
@ -426,7 +441,7 @@ void move_ball(gint source_ball, gint target_ball) {
g_assert(board_get_at_node(target_ball) == 0);
find_x_y_of_the_node(&tbx, &tby, target_ball, rules_get_width(), rules_get_height());
for(blah = 0; blah < rules_get_height() * rules_get_width(); blah++) {
for (blah = 0; blah < rules_get_height() * rules_get_width(); blah++) {
*np++ = board_get_at_node(blah) > 0 ? -1 : 0;
}
@ -439,24 +454,26 @@ void move_ball(gint source_ball, gint target_ball) {
phase = 0;
draw_ball(0, x, y, 0, 0);
if(pref_get_show_path()) {
if (pref_get_show_path())
{
lock_actions(1);
for(k = path[0] - 1; k; k--) {
for (k = path[0] - 1; k; k--)
{
gettimeofday(&tvs, NULL);
find_x_y_of_the_node(&i, &j, path[k], rules_get_width(), rules_get_height());
if(k == path[0] - 1) {
if (k == path[0] - 1) {
/* x and y are the coordinates of starting position */
pawx = x;
pawy = y;
} else {
find_x_y_of_the_node(&pawx, &pawy, path[k + 1], rules_get_width(), rules_get_height());
}
if(k != path[0] - 1) {
if (k != path[0] - 1) {
find_pawnum_and_direction(pawx, pawy, i, j, &pawnum, &direction);
} else {
pawnum = direction = find_direction(pawx, pawy, i, j);
}
if(pref_get_show_footprints()) {
if (pref_get_show_footprints()) {
board_set_at_xy(pawx, pawy, -1 - pawnum);
draw_ball(-1 - pawnum, pawx, pawy, 0, 0);
}
@ -465,12 +482,12 @@ void move_ball(gint source_ball, gint target_ball) {
do {
gtk_main_iteration_do(0);
gettimeofday(&tve, NULL);
} while((tve.tv_sec - tvs.tv_sec) * 1000000 + tve.tv_usec - tvs.tv_usec < 100000);
} while ((tve.tv_sec - tvs.tv_sec) * 1000000 + tve.tv_usec - tvs.tv_usec < 100000);
board_set_at_xy(i, j, 0);
draw_ball(0, i, j, 0, 0);
}
lock_actions(0);
if(k == path[0] - 1) {
if (k == path[0] - 1) {
/* x and y are the coordinates of starting position */
pawx = x;
pawy = y;
@ -483,9 +500,9 @@ void move_ball(gint source_ball, gint target_ball) {
draw_ball(-1 - pawnum, pawx, pawy, 0, 0);
}
}
if(pref_get_show_path() && pref_get_show_footprints()) {
if (pref_get_show_path() && pref_get_show_footprints()) {
board_set_at_node(source_ball, 0);
for(k = path[0] - 1; k; k--) {
for (k = path[0] - 1; k; k--) {
board_set_at_node(path[k], 0);
}
}

View File

@ -25,7 +25,7 @@
gint destroy_lines(gboolean count_score) {
gint i = game_destroy_lines(count_score);
if(count_score && i) {
if (count_score && i) {
gint ml = rules_get_destroy();
gint sc = game_get_score();
gint nsc = sc + ml * 2 + (i - ml) * (i - ml) * 2 + (1 - pref_get_show_next_colors());
@ -53,7 +53,8 @@ gint random_cell(void) {
return (gint)(((gfloat)rules_get_width() * rules_get_height()) * rand() / (RAND_MAX + 1.0));
}
void new_turn(gint number, gboolean first) {
void new_turn(gint number, gboolean first)
{
gint i, k = 0, free_cells_number, c;
struct score_board scoreboard[10];
@ -61,12 +62,12 @@ void new_turn(gint number, gboolean first) {
timer_start();
k = 0;
if(number < rules_get_next()) {
if (number < rules_get_next()) {
number = rules_get_next();
}
if((free_cells_number = game_count_free_cells()) <= number) {
if(!read_score(scoreboard, NULL, NULL) ||
if ((free_cells_number = game_count_free_cells()) <= number) {
if (!read_score(scoreboard, NULL, NULL) ||
game_get_score() > scoreboard[9].score) {
input_name_dialog();
}
@ -76,8 +77,8 @@ void new_turn(gint number, gboolean first) {
do {
i = random_cell();
if(board_get_at_node(i) < 1) {
if(first) {
if (board_get_at_node(i) < 1) {
if (first) {
c = 1 + random_color();
board_set_at_node(i, c);
} else {
@ -88,20 +89,21 @@ void new_turn(gint number, gboolean first) {
continue;
}
/* I hope that k!=0 */
if(k <= rules_get_next()) {
if (k <= rules_get_next()) {
next_set(k - 1, 1 + random_color());
}
} while(k < ((number < free_cells_number) ? number : free_cells_number));
} while (k < ((number < free_cells_number) ? number : free_cells_number));
if(!first) {
if (!first) {
draw_next_balls();
/* if line complete, don't increase user's score--it's not his merit */
destroy_lines(FALSE);
}
} while((free_cells_number = game_count_free_cells()) == rules_get_width() * rules_get_height());
} while ((free_cells_number = game_count_free_cells()) == rules_get_width() * rules_get_height());
}
void new_game(void){
void new_game(void)
{
stop_jumping_animation();
game_init_game(NULL, NULL);
menu_set_sensitive_undo(FALSE);
@ -111,7 +113,9 @@ void new_game(void){
draw_board();
}
int main(int argc, char **argv) {
int main(int argc, char **argv)
{
gint i;
struct timeval tv;
struct timezone tz;
@ -126,7 +130,8 @@ int main(int argc, char **argv) {
#endif /* ENABLE_NLS */
/* drop privileges after spawning child with extra privs */
if(score_setup() == -1) return 1;
if (score_setup() == -1)
return 1;
setregid(getgid(), getgid());
/* initialize random seed */
@ -137,18 +142,18 @@ int main(int argc, char **argv) {
load_preferences();
/* initialize gtk */
gtk_init(&argc, &argv);
gtk_init (&argc, &argv);
/* load theme, fallback to default if specifed theme cannot be loaded */
if(!(i = load_theme(pref_get_theme_name()))) {
if(strcmp(pref_get_theme_name(), pref_get_default_theme_name()) != 0) {
if (!(i = load_theme(pref_get_theme_name()))) {
if (strcmp(pref_get_theme_name(), pref_get_default_theme_name()) != 0) {
err = g_strdup_printf(_("Failed loading theme \"%s\"! Trying \"%s\"\n"), pref_get_theme_name(), pref_get_default_theme_name());
ut_simple_message_box(err);
g_free(err);
pref_set_theme_name(pref_get_default_theme_name());
i = load_theme(pref_get_theme_name());
}
if(!i) {
if (!i) {
err = g_strdup_printf(_("Failed loading theme \"%s\"! Exiting.\n"), pref_get_theme_name());
ut_simple_message_box(err);
g_free(err);
@ -165,7 +170,7 @@ int main(int argc, char **argv) {
remake_board(-1, 1);
/* read and set scores */
if(!read_score(scoreboard, NULL, NULL)) {
if (!read_score(scoreboard, NULL, NULL)) {
ut_simple_message_box(_("Unable to read score.\n"));
}
mw_set_hi_score(scoreboard[0].score);
@ -173,12 +178,12 @@ int main(int argc, char **argv) {
mapfile = g_strconcat(getenv("HOME"), G_DIR_SEPARATOR_S, ".gtkballs",
G_DIR_SEPARATOR_S, "accel.map", NULL);
gtk_accel_map_load(mapfile);
gtk_accel_map_load (mapfile);
/* enter main application loop */
gtk_main();
gtk_accel_map_save(mapfile);
gtk_accel_map_save (mapfile);
return 0;
}

View File

@ -8,7 +8,8 @@
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
gboolean ut_key_pressed_cb(GtkWidget *widget, GdkEventKey *event) {
gboolean ut_key_pressed_cb(GtkWidget *widget, GdkEventKey *event)
{
if(widget && event && event->keyval == GDK_Escape) {
gtk_widget_destroy(widget);
return TRUE;
@ -16,9 +17,11 @@ gboolean ut_key_pressed_cb(GtkWidget *widget, GdkEventKey *event) {
return FALSE;
}
GtkWidget *ut_window_new(gchar *title, gchar *wmname, gchar *wmclass,
gboolean escaable, gboolean modal, gboolean resizable,
gint border) {
gint border)
{
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_modal(GTK_WINDOW(window), modal);
gtk_window_set_resizable(GTK_WINDOW(window), resizable);
@ -31,7 +34,9 @@ GtkWidget *ut_window_new(gchar *title, gchar *wmname, gchar *wmclass,
return window;
}
GtkWidget *ut_check_button_new(gchar *label, gboolean active, GtkWidget *parent) {
GtkWidget *ut_check_button_new(gchar *label, gboolean active, GtkWidget *parent)
{
GtkWidget *button;
button = gtk_check_button_new_with_label(label);
@ -41,7 +46,9 @@ GtkWidget *ut_check_button_new(gchar *label, gboolean active, GtkWidget *parent)
return button;
}
GtkWidget *ut_button_new(gchar *label, gpointer func, gpointer func_data, GtkWidget *parent) {
GtkWidget *ut_button_new(gchar *label, gpointer func, gpointer func_data, GtkWidget *parent)
{
GtkWidget *button;
button = gtk_button_new_with_label(label);
@ -52,7 +59,9 @@ GtkWidget *ut_button_new(gchar *label, gpointer func, gpointer func_data, GtkWid
return button;
}
GtkWidget *ut_button_new_stock(const gchar *stock_id, gpointer func, gpointer func_data, GtkWidget *parent) {
GtkWidget *ut_button_new_stock(const gchar *stock_id, gpointer func, gpointer func_data, GtkWidget *parent)
{
GtkWidget *button;
button = gtk_button_new_from_stock(stock_id);
@ -63,7 +72,9 @@ GtkWidget *ut_button_new_stock(const gchar *stock_id, gpointer func, gpointer fu
return button;
}
GtkWidget *ut_button_new_stock_swap(const gchar *stock_id, gpointer func, gpointer func_data, GtkWidget *parent) {
GtkWidget *ut_button_new_stock_swap(const gchar *stock_id, gpointer func, gpointer func_data, GtkWidget *parent)
{
GtkWidget *button;
button = gtk_button_new_from_stock(stock_id);
@ -74,7 +85,9 @@ GtkWidget *ut_button_new_stock_swap(const gchar *stock_id, gpointer func, gpoint
return button;
}
GtkWidget *ut_spin_button_new(gchar *label, gint min, gint max, gint val, GtkWidget *parent) {
GtkWidget *ut_spin_button_new(gchar *label, gint min, gint max, gint val, GtkWidget *parent)
{
GtkAdjustment *adj;
GtkWidget *button, *hbox, *labelw;
@ -92,6 +105,7 @@ GtkWidget *ut_spin_button_new(gchar *label, gint min, gint max, gint val, GtkWid
return button;
}
GtkWidget *ut_spin_button_start_new(gchar *label, gint min, gint max, gint val, GtkWidget *parent)
{
GtkAdjustment *adj;
@ -111,13 +125,16 @@ GtkWidget *ut_spin_button_start_new(gchar *label, gint min, gint max, gint val,
return button;
}
/* shows simple message box */
void ut_simple_message_box(gchar *message) {
void ut_simple_message_box(gchar *message)
{
GtkWidget *dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, message);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}
/* shows simple message box */
void ut_simple_message_box_with_title(gchar *message, gchar *title) {
GtkWidget *dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, message);

View File

@ -1,7 +1,6 @@
#ifndef __GTKUTILS_H
#define __GTKUTILS_H
gboolean ut_key_pressed_cb (GtkWidget *widget,
GdkEventKey *event);
gboolean ut_key_pressed_cb (GtkWidget *widget, GdkEventKey *event);
GtkWidget *ut_window_new (gchar *title,
gchar *wmname,
@ -41,9 +40,9 @@ GtkWidget *ut_spin_button_start_new (gchar *label,
gint max,
gint val,
GtkWidget *parent);
void ut_simple_message_box (gchar *message);
void ut_simple_message_box_with_title (gchar *message,
gchar *title);
void ut_simple_message_box_with_title (gchar *message, gchar *title);
#endif

View File

@ -11,11 +11,12 @@
#include "gtkutils.h"
#include "scoreboard.h"
void show_hall_of_fame(GtkWidget *widget, gpointer data, struct score_board b[10]) {
GtkWidget *hall_of_fame;
GtkWidget *frame, *sw, *tv;
GtkWidget *vbox, *button_box;
GtkWidget *close_button;
void show_hall_of_fame (GtkWidget *widget, gpointer data, struct score_board b[10])
{
GtkWidget * hall_of_fame;
GtkWidget * frame, * sw, * tv;
GtkWidget * vbox, * button_box;
GtkWidget * close_button;
gint i;
struct score_board *bs = b;
struct score_board bn[10];
@ -26,8 +27,8 @@ void show_hall_of_fame(GtkWidget *widget, gpointer data, struct score_board b[10
gchar *str;
GtkTreePath *path;
if(data == NULL) {
if(!read_score(bn, NULL, NULL)) {
if (data == NULL) {
if (!read_score(bn, NULL, NULL)) {
ut_simple_message_box(_("Unable to read score.\n"));
return;
}
@ -48,36 +49,39 @@ void show_hall_of_fame(GtkWidget *widget, gpointer data, struct score_board b[10
gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_ETCHED_IN);
gtk_container_add(GTK_CONTAINER(frame), sw);
store = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
for(i = 0; bs[i].score && i < 10; i++) {
store = gtk_list_store_new (3,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_STRING);
for (i = 0; bs[i].score && i < 10; i++) {
gtk_list_store_append(store, &iter);
str = g_strdup_printf("%d", bs[i].score);
gtk_list_store_set(store, &iter, 0, bs[i].name, 1, str, 2, bs[i].date, -1);
g_free(str);
}
if(i == 0) {
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, 0, _("No scores"), 1, "", 2, "", -1);
if (i == 0) {
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, 0, _("No scores"), 1, "", 2, "", -1);
}
tv = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tv), TRUE);
gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(tv)), GTK_SELECTION_BROWSE);
g_object_unref(G_OBJECT(store));
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes(_("Name"), renderer, "text", 0, NULL);
tv = gtk_tree_view_new_with_model (GTK_TREE_MODEL(store));
gtk_tree_view_set_rules_hint (GTK_TREE_VIEW(tv), TRUE);
gtk_tree_selection_set_mode (gtk_tree_view_get_selection(GTK_TREE_VIEW(tv)), GTK_SELECTION_BROWSE);
g_object_unref (G_OBJECT(store));
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes (_("Name"), renderer, "text", 0, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(tv), column);
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes(_("Score"), renderer, "text", 1, NULL);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes (_("Score"), renderer, "text", 1, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(tv), column);
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes(_("Date"), renderer, "text", 2, NULL);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes (_("Date"), renderer, "text", 2, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(tv), column);
gtk_container_add(GTK_CONTAINER(sw), tv);
if(data) {
if (data) {
str = g_strdup_printf("%u", (gint)data - 1);
if((path = gtk_tree_path_new_from_string(str))) {
if ((path = gtk_tree_path_new_from_string(str))) {
gtk_tree_selection_select_path(gtk_tree_view_get_selection(GTK_TREE_VIEW(tv)), path);
gtk_tree_view_set_cursor(GTK_TREE_VIEW(tv), path, NULL, FALSE);
gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(tv), path, NULL, TRUE, 0, 0);
@ -91,11 +95,13 @@ void show_hall_of_fame(GtkWidget *widget, gpointer data, struct score_board b[10
close_button = ut_button_new_stock_swap(GTK_STOCK_CLOSE, gtk_widget_destroy, hall_of_fame, button_box);
gtk_widget_grab_focus(close_button);
gtk_widget_grab_default(close_button);
gtk_widget_show_all(hall_of_fame);
gtk_widget_grab_focus (close_button);
gtk_widget_grab_default (close_button);
gtk_widget_show_all (hall_of_fame);
}
void show_hall_of_fame_cb(void) {
void show_hall_of_fame_cb(void)
{
show_hall_of_fame(NULL, NULL, NULL);
}

View File

@ -21,7 +21,8 @@ static GtkWidget *_dialog = NULL;
static gchar _last_player_name[15] = "";
static gint _saved_score = 0;
void read_entry(GtkWidget *widget, gpointer data) {
void read_entry(GtkWidget *widget, gpointer data)
{
struct score_board current_entry;
time_t current_time;
struct tm *timeptr;
@ -37,16 +38,17 @@ void read_entry(GtkWidget *widget, gpointer data) {
current_entry.score = _saved_score;
current_time = time(NULL);
timeptr = localtime(&current_time);
if(!timeptr) {
if (!timeptr) {
ut_simple_message_box(_("Unable to determine current date.\n"));
strncpy(current_entry.date, _("Unknown"), sizeof(current_entry.date));
} else {
if(!strftime(current_entry.date, sizeof(current_entry.date), _("%a %b %d %H:%M %Y"), timeptr)) {
if (!strftime(current_entry.date, sizeof(current_entry.date), _("%a %b %d %H:%M %Y"), timeptr)) {
ut_simple_message_box(_("Unable to determine current date.\n"));
strncpy(current_entry.date, _("Unknown"), sizeof(current_entry.date));
} else {
tstr = g_locale_to_utf8(current_entry.date, -1, &br, &bw, NULL);
if(!tstr) {
if (!tstr) {
strncpy(current_entry.date, _("Unknown"), sizeof(current_entry.date));
} else {
strncpy(current_entry.date, tstr, sizeof(current_entry.date));
@ -55,16 +57,16 @@ void read_entry(GtkWidget *widget, gpointer data) {
}
}
if(!read_score(b, &bf, &fbn)) {
if (!read_score(b, &bf, &fbn)) {
ut_simple_message_box (_("Unable to read score.\n"));
} else {
pos = insert_entry_in_score_board(b, current_entry);
if(!write_score(b, bf, fbn)) {
if (!write_score(b, bf, fbn)) {
ut_simple_message_box(_("Unable to save score.\n"));
}
/*FIXME: free bf here */
free_score_board_full(bf, fbn);
if(pos != -1) {
if (pos != -1) {
pos++;
show_hall_of_fame(NULL, (gpointer)pos, b);
}
@ -74,11 +76,13 @@ void read_entry(GtkWidget *widget, gpointer data) {
/* show scores to let user see if (s)he's on top ;) */
}
void input_name_dialog(void) {
GtkWidget *prompt_label, * vbox;
GtkWidget *name;
GtkWidget *button;
gchar *s;
void input_name_dialog(void)
{
GtkWidget * prompt_label, * vbox;
GtkWidget * name;
GtkWidget * button;
gchar * s;
/* we have to save score, because they will be set to 0 in new_game() */
_saved_score = game_get_score();
@ -97,8 +101,8 @@ void input_name_dialog(void) {
g_signal_connect(G_OBJECT(name), "activate", G_CALLBACK(read_entry), name);
/* restore the last player's name */
if(!(*_last_player_name)) {
if((s = getenv("USER"))) {
if (!(*_last_player_name)) {
if ((s = getenv("USER"))) {
strncpy(_last_player_name, s, sizeof(_last_player_name));
}
}

View File

@ -68,7 +68,8 @@ static gchar *_menu_translate(const gchar *path, gpointer data) {
return gettext(path);
}
void menu_get_main(GtkWidget *window, GtkWidget **menubar) {
void menu_get_main(GtkWidget *window, GtkWidget **menubar)
{
GtkAccelGroup *accel_group;
_action_group = gtk_action_group_new("MenuActions");
@ -83,14 +84,16 @@ void menu_get_main(GtkWidget *window, GtkWidget **menubar) {
*menubar = gtk_ui_manager_get_widget(_ui_manager, "/MainMenu");
}
void _menu_set_sensitive(const gchar *path, gboolean sensitive) {
void _menu_set_sensitive(const gchar *path, gboolean sensitive)
{
GtkWidget *widget;
widget = gtk_ui_manager_get_widget(_ui_manager, path);
gtk_widget_set_sensitive(widget, sensitive);
}
void menu_set_sensitive_undo(gboolean sensitive) {
void menu_set_sensitive_undo(gboolean sensitive)
{
_menu_set_sensitive("/MainMenu/EditMenu/Undo", sensitive);
}

View File

@ -38,7 +38,7 @@ void mw_small_balls_add(GtkWidget *child) {
}
void mw_show_hide_next_balls(gboolean show) {
if(show) {
if (show) {
gtk_widget_show_all(_small_balls_box);
} else {
gtk_widget_hide(_small_balls_box);
@ -60,7 +60,7 @@ void mw_set_user_score(gint score) {
gchar *str;
game_set_score(score);
if(score > game_get_hi_score()) {
if (score > game_get_hi_score()) {
mw_set_hi_score(score);
}
str = g_strdup_printf(_("Your score: %i"), score);
@ -68,22 +68,23 @@ void mw_set_user_score(gint score) {
g_free(str);
}
gboolean _countdown_timer(gpointer data) {
gboolean _countdown_timer(gpointer data)
{
gchar *text;
GtkLabel *label = data;
gint trem;
if(child_writer_dead_handler()) {
if (child_writer_dead_handler()) {
ut_simple_message_box_with_title(_("Score writer process died unexpectedly. No scores will be writen!\n" \
"Save your game and restart programm.\n"),
_("Who Killed Bambi?"));
}
if(!timer_is_running()) {
if (!timer_is_running()) {
gtk_label_set_text(label, _("Time is unlimited"));
return TRUE;
}
if(timer_is_expired()) {
if (timer_is_expired()) {
new_turn(rules_get_next(), FALSE);
}
trem = timer_get_remaining();
@ -94,40 +95,41 @@ gboolean _countdown_timer(gpointer data) {
return TRUE;
}
gint _user_action_event(GtkWidget *w, GdkEvent *ev) {
if(ev->type == GDK_MOTION_NOTIFY) {
gint _user_action_event(GtkWidget *w, GdkEvent *ev)
{
if (ev->type == GDK_MOTION_NOTIFY) {
move_pointer_to(gtkb_theme_get_coord_at_x(ev->motion.x),
gtkb_theme_get_coord_at_y(ev->motion.y));
}
if(ev->type == GDK_KEY_PRESS) {
if(ev->key.keyval == GDK_Left || ev->key.keyval == GDK_KP_Left) {
if (ev->type == GDK_KEY_PRESS) {
if (ev->key.keyval == GDK_Left || ev->key.keyval == GDK_KP_Left) {
move_pointer(DIR_LEFT);
} else if(ev->key.keyval == GDK_Right || ev->key.keyval == GDK_KP_Right) {
} else if (ev->key.keyval == GDK_Right || ev->key.keyval == GDK_KP_Right) {
move_pointer(DIR_RIGHT);
} else if(ev->key.keyval == GDK_Up || ev->key.keyval == GDK_KP_Up) {
} else if (ev->key.keyval == GDK_Up || ev->key.keyval == GDK_KP_Up) {
move_pointer(DIR_UP);
} else if(ev->key.keyval == GDK_Down || ev->key.keyval == GDK_KP_Down) {
} else if (ev->key.keyval == GDK_Down || ev->key.keyval == GDK_KP_Down) {
move_pointer(DIR_DOWN);
} else if(ev->key.keyval == GDK_KP_Home) {
} else if (ev->key.keyval == GDK_KP_Home) {
move_pointer(DIR_UP_LEFT);
} else if(ev->key.keyval == GDK_KP_Page_Up) {
} else if (ev->key.keyval == GDK_KP_Page_Up) {
move_pointer(DIR_UP_RIGHT);
} else if(ev->key.keyval == GDK_KP_End) {
} else if (ev->key.keyval == GDK_KP_End) {
move_pointer(DIR_DOWN_LEFT);
} else if(ev->key.keyval == GDK_KP_Page_Down) {
} else if (ev->key.keyval == GDK_KP_Page_Down) {
move_pointer(DIR_DOWN_RIGHT);
} else if(ev->key.keyval == GDK_Return ||
} else if (ev->key.keyval == GDK_Return ||
ev->key.keyval == GDK_KP_Space ||
ev->key.keyval == GDK_KP_Enter ||
ev->key.keyval == GDK_space) {
if(is_actions_locked()) {
if (is_actions_locked()) {
return FALSE;
}
pointer_pressed(-1, -1);
}
} else if(ev->type == GDK_BUTTON_PRESS && ev->button.button == 1) {
if(is_actions_locked()) {
} else if (ev->type == GDK_BUTTON_PRESS && ev->button.button == 1) {
if (is_actions_locked()) {
return FALSE;
}
pointer_pressed(gtkb_theme_get_coord_at_x(ev->button.x),
@ -137,65 +139,68 @@ gint _user_action_event(GtkWidget *w, GdkEvent *ev) {
return FALSE;
}
static void main_window_destroy_cb (GtkWidget * w, gpointer user_data)
{
gtkb_theme_free_handler (NULL, NULL);
gtk_main_quit ();
}
void mw_create(gint da_width, gint da_height) {
GtkWidget *mainwin;
GtkWidget *menubar;
GtkWidget *vbox, *hbox, *hbox1, *drawing_area_box;
GtkWidget *timer_label;
GdkPixbuf *icon;
GError *error=NULL;
mainwin = ut_window_new(_("GtkBalls"), "GtkBalls_Main", "GtkBalls", FALSE, FALSE, FALSE, 0);
void mw_create(gint da_width, gint da_height)
{
GtkWidget * mainwin;
GtkWidget * menubar;
GtkWidget * vbox, * hbox, * hbox1, * drawing_area_box;
GtkWidget * timer_label;
GdkPixbuf * icon;
GError *error = NULL;
mainwin = ut_window_new (_("GtkBalls"), "GtkBalls_Main", "GtkBalls", FALSE, FALSE, FALSE, 0);
main_window = GTK_WINDOW (mainwin);
g_signal_connect (G_OBJECT(mainwin), "destroy", G_CALLBACK(main_window_destroy_cb), mainwin);
g_signal_connect (G_OBJECT (mainwin), "destroy", G_CALLBACK(main_window_destroy_cb), mainwin);
icon = gdk_pixbuf_new_from_file(DATADIR "/gtkballs/gtkballs_16x16.png", &error);
if(icon) {
gtk_window_set_icon(GTK_WINDOW(mainwin), icon);
icon = gdk_pixbuf_new_from_file (DATADIR "/gtkballs/gtkballs_16x16.png", &error);
if (icon) {
gtk_window_set_icon (GTK_WINDOW(mainwin), icon);
} else {
g_error_free(error);
g_error_free (error);
}
vbox = gtk_vbox_new(FALSE, 5);
gtk_container_add(GTK_CONTAINER(mainwin), vbox);
vbox = gtk_vbox_new (FALSE, 5);
gtk_container_add (GTK_CONTAINER(mainwin), vbox);
menu_get_main(mainwin, &menubar);
menu_set_sensitive_undo(FALSE);
gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
menu_get_main (mainwin, &menubar);
menu_set_sensitive_undo (FALSE);
gtk_box_pack_start (GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
hbox = gtk_hbox_new(FALSE, 0);
_hi_score_label = gtk_label_new("");
gtk_box_pack_start(GTK_BOX(hbox), _hi_score_label, FALSE, FALSE, 5);
hbox = gtk_hbox_new (FALSE, 0);
_hi_score_label = gtk_label_new ("");
gtk_box_pack_start (GTK_BOX(hbox), _hi_score_label, FALSE, FALSE, 5);
_user_score_label = gtk_label_new("");
gtk_box_pack_end(GTK_BOX(hbox), _user_score_label, FALSE, FALSE, 5);
gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(hbox), FALSE, FALSE, 0);
gtk_box_pack_end (GTK_BOX(hbox), _user_score_label, FALSE, FALSE, 5);
gtk_box_pack_start (GTK_BOX(vbox), GTK_WIDGET(hbox), FALSE, FALSE, 0);
_small_balls_box = gtk_hbox_new(TRUE, 0);
gtk_box_pack_start(GTK_BOX(hbox), _small_balls_box, TRUE, FALSE, 0);
gtk_box_pack_start (GTK_BOX(hbox), _small_balls_box, TRUE, FALSE, 0);
hbox1 = gtk_hbox_new(FALSE, 0);
timer_label = gtk_label_new(NULL);
gtk_box_pack_start(GTK_BOX(hbox1), timer_label, TRUE, TRUE, 5);
hbox1 = gtk_hbox_new (FALSE, 0);
timer_label = gtk_label_new (NULL);
gtk_box_pack_start (GTK_BOX(hbox1), timer_label, TRUE, TRUE, 5);
g_timeout_add(250, _countdown_timer, timer_label);
gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(hbox1), FALSE, FALSE, 0);
gtk_box_pack_start (GTK_BOX(vbox), GTK_WIDGET(hbox1), FALSE, FALSE, 0);
drawing_area_box = gtk_hbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), drawing_area_box, FALSE, FALSE, 10);
gtk_box_pack_start (GTK_BOX(vbox), drawing_area_box, FALSE, FALSE, 10);
_drawing_area = gtk_drawing_area_new();
gtk_widget_set_size_request(_drawing_area, da_width, da_height);
gtk_box_pack_start(GTK_BOX(drawing_area_box), _drawing_area, TRUE, FALSE, 10);
gtk_widget_set_events(_drawing_area, GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | GDK_KEY_PRESS_MASK | GDK_POINTER_MOTION_MASK);
g_signal_connect(G_OBJECT(_drawing_area), "expose_event", G_CALLBACK(expose_event), NULL);
g_signal_connect(G_OBJECT(_drawing_area), "button_press_event", G_CALLBACK(_user_action_event), NULL);
g_signal_connect(G_OBJECT(_drawing_area), "motion_notify_event", G_CALLBACK(_user_action_event), NULL);
gtk_widget_set_size_request (_drawing_area, da_width, da_height);
gtk_box_pack_start (GTK_BOX(drawing_area_box), _drawing_area, TRUE, FALSE, 10);
gtk_widget_set_events (_drawing_area, GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | GDK_KEY_PRESS_MASK | GDK_POINTER_MOTION_MASK);
g_signal_connect (G_OBJECT(_drawing_area), "expose_event", G_CALLBACK(expose_event), NULL);
g_signal_connect (G_OBJECT(_drawing_area), "button_press_event", G_CALLBACK(_user_action_event), NULL);
g_signal_connect (G_OBJECT(_drawing_area), "motion_notify_event", G_CALLBACK(_user_action_event), NULL);
/* FIXME: imho catching keypress on whole window is stupid... */
g_signal_connect(G_OBJECT(mainwin), "key_press_event", G_CALLBACK(_user_action_event), NULL);
g_signal_connect (G_OBJECT(mainwin), "key_press_event", G_CALLBACK(_user_action_event), NULL);
gtk_widget_show_all(mainwin);
gtk_widget_show_all (mainwin);
}

View File

@ -48,21 +48,23 @@ int find_node_by_coords(int x, int y, int number_of_x_cells, int number_of_y_cel
mark: number to mark neighbours
number_of_cells: number of cells in the row
returns number of marked nodes */
int mark_neighbours_of_the_nodes(int *nodes, int *source_nodes, int *neighbours, int mark,
int number_of_x_cells, int number_of_y_cells) {
int mark_neighbours_of_the_nodes (int *nodes, int *source_nodes, int *neighbours, int mark,
int number_of_x_cells, int number_of_y_cells)
{
int i, j, neighbours_count = 0;
int x, y, node;
int xses[4] = { 0, 0, 1, -1};
int yses[4] = {-1, 1, 0, 0};
for(i = 1; i <= source_nodes[0]; i++) {
for (i = 1; i <= source_nodes[0]; i++)
{
find_x_y_of_the_node(&x, &y, source_nodes[i], number_of_x_cells, number_of_y_cells);
if(x != -1 && y != -1) {
for(j = 0; j < 4; j++) {
if(x + xses[j] >= 0 && x + xses[j] < number_of_x_cells &&
if (x != -1 && y != -1) {
for (j = 0; j < 4; j++) {
if (x + xses[j] >= 0 && x + xses[j] < number_of_x_cells &&
y + yses[j] >= 0 && y + yses[j] < number_of_y_cells) {
node = find_node_by_coords(x + xses[j], y + yses[j], number_of_x_cells, number_of_y_cells);
if((node != -1) && nodes[node] == 0) {
if ((node != -1) && nodes[node] == 0) {
nodes[node] = mark;
neighbours[++neighbours_count] = node;
}
@ -75,25 +77,28 @@ int mark_neighbours_of_the_nodes(int *nodes, int *source_nodes, int *neighbours,
}
/* check if nodes are neighbours */
int is_neighbours(int node1, int node2, int xn, int yn) {
int is_neighbours(int node1, int node2, int xn, int yn)
{
int x1, y1, x2, y2;
find_x_y_of_the_node(&x1, &y1, node1, xn, yn);
find_x_y_of_the_node(&x2, &y2, node2, xn, yn);
if((x1 == x2) && ((y1 == y2 + 1) || (y2 == y1 + 1))) {
if ((x1 == x2) && ((y1 == y2 + 1) || (y2 == y1 + 1))) {
return 1;
} else if((y1 == y2) && ((x1 == x2 + 1) || (x2 == x1 + 1))) {
} else if ((y1 == y2) && ((x1 == x2 + 1) || (x2 == x1 + 1))) {
return 1;
}
return 0;
}
/* find the path between source_node and the target_node
result stored in path
returns 0 on failure and 1 on success */
int find_path(int *nodes, int source_node, int target_node, int *path,
int number_of_x_cells, int number_of_y_cells) {
int find_path (int *nodes, int source_node, int target_node, int *path,
int number_of_x_cells, int number_of_y_cells)
{
int waves[number_of_x_cells * number_of_y_cells][number_of_x_cells * number_of_y_cells];
int i, j, k = 1, finish = 0;
@ -101,14 +106,15 @@ int find_path(int *nodes, int source_node, int target_node, int *path,
waves[0][1] = source_node;
nodes[source_node] = -1;
while(!finish) {
if(!mark_neighbours_of_the_nodes(nodes, waves[k - 1], waves[k], k,
while (!finish)
{
if (!mark_neighbours_of_the_nodes(nodes, waves[k - 1], waves[k], k,
number_of_x_cells, number_of_y_cells)) {
/* the destination can never be reached */
return 0;
}
for(i = 1; i <= waves[k][0]; i++) {
if(waves[k][i] == target_node) {
for (i = 1; i <= waves[k][0]; i++) {
if (waves[k][i] == target_node) {
finish = 1;
break;
}
@ -118,10 +124,11 @@ int find_path(int *nodes, int source_node, int target_node, int *path,
path[0] = k;
path[1] = waves[k - 1][i];
for(j = k - 2; j > 0; j--) {
for (j = k - 2; j > 0; j--)
{
finish = 0;
for(i = 1; i <= waves[j][0]; i++) {
if(is_neighbours(waves[j][i], path[k - j - 1],
for (i = 1; i <= waves[j][0]; i++) {
if (is_neighbours(waves[j][i], path[k - j - 1],
number_of_x_cells, number_of_y_cells)) {
path[k - j] = waves[j][i];
break;

View File

@ -3,8 +3,10 @@
int find_path (int *nodes, int source_node, int target_node, int *path,
int number_of_x_cells, int number_of_y_cells);
void find_x_y_of_the_node (int *x, int *y,
int node, int number_of_x_cells, int number_of_y_cells);
int find_node_of_x_y (int x, int y, int number_of_x_cells);
#endif

View File

@ -16,7 +16,8 @@
#include "game.h"
#include "mainwin.h"
enum {
enum
{
PR_DIALOG,
PR_SHOW_NEXT,
PR_SHOW_PATH,
@ -50,7 +51,9 @@ gboolean fix_draw_next_balls(gpointer data) {
return FALSE;
}
void preferences_apply(GtkWidget *widget, gpointer data) {
void preferences_apply(GtkWidget *widget, gpointer data)
{
GtkWidget **buttons = data;
GtkTreeModel *model;
GtkTreeIter iter;
@ -59,10 +62,10 @@ void preferences_apply(GtkWidget *widget, gpointer data) {
gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(buttons[PR_THEME_LIST])), &model, &iter);
gtk_tree_model_get(model, &iter, 0, &themename, -1);
if(strcmp(themename, pref_get_theme_name()) != 0) {
if (strcmp(themename, pref_get_theme_name()) != 0) {
save = g_strdup(pref_get_theme_name());
pref_set_theme_name(themename);
if(!load_theme(pref_get_theme_name())) {
if (!load_theme(pref_get_theme_name())) {
msg = g_strconcat(_("Failed loading theme \""), pref_get_theme_name(), "\"!\n", NULL);
ut_simple_message_box(msg);
g_free(msg);
@ -76,75 +79,79 @@ void preferences_apply(GtkWidget *widget, gpointer data) {
}
g_free(themename);
pref_set_show_next_colors(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(buttons[PR_SHOW_NEXT])));
pref_set_show_next_colors(gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(buttons[PR_SHOW_NEXT])));
mw_show_hide_next_balls(pref_get_show_next_colors());
g_timeout_add(50, fix_draw_next_balls, NULL);
pref_set_show_path(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(buttons[PR_SHOW_PATH])));
pref_set_show_footprints(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(buttons[PR_SHOW_PAWS])));
pref_set_show_destroy(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(buttons[PR_SHOW_ANIM])));
pref_set_show_path(gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(buttons[PR_SHOW_PATH])));
pref_set_show_footprints(gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(buttons[PR_SHOW_PAWS])));
pref_set_show_destroy (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(buttons[PR_SHOW_ANIM])));
pref_set_show_highlight(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(buttons[PR_SHOW_HL])));
pref_set_show_highlight (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(buttons[PR_SHOW_HL])));
prefs_set_hl_dr(gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(buttons[PR_HL_DR])));
prefs_set_hl_dg(gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(buttons[PR_HL_DG])));
prefs_set_hl_db(gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(buttons[PR_HL_DB])));
prefs_set_hl_dr(gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON(buttons[PR_HL_DR])));
prefs_set_hl_dg(gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON(buttons[PR_HL_DG])));
prefs_set_hl_db(gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON(buttons[PR_HL_DB])));
gtkb_make_hl_pixmap(gtkbTheme);
gtkb_make_hl_pixmap (gtkbTheme);
redraw_pointer();
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(buttons[PR_TIME_LIMIT]))) {
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(buttons[PR_TIME_LIMIT]))) {
timer_start();
timer_set_limit(gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(buttons[PR_TIME_VALUE])));
} else {
timer_set_limit(-1);
}
msg = save_preferences();
if(msg) {
if (msg) {
ut_simple_message_box(msg);
g_free(msg);
}
}
void preferences_ok(GtkWidget *widget, gpointer data) {
GtkWidget **buttons = data;
preferences_apply(widget, buttons);
gtk_widget_destroy(buttons[PR_DIALOG]);
}
void preferences_dialog(void) {
GtkWidget **buttons;
GtkWidget *frame;
GtkWidget *big_vbox, *vbox, *buttons_box;
GtkWidget *theme_scrolled_window;
GtkWidget *separator;
gint i, st;
GtkListStore *store;
GtkTreeIter iter;
GtkTreePath *path;
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
gchar *pathstr;
gchar **themelist;
if(!(themelist = get_available_themes())) {
void preferences_dialog (void)
{
GtkWidget ** buttons;
GtkWidget * frame;
GtkWidget * big_vbox, * vbox, * buttons_box;
GtkWidget * theme_scrolled_window;
GtkWidget * separator;
gint i, st;
GtkListStore * store;
GtkTreeIter iter;
GtkTreePath * path;
GtkCellRenderer * renderer;
GtkTreeViewColumn * column;
gchar * pathstr;
gchar ** themelist;
if (!(themelist = get_available_themes())) {
ut_simple_message_box(_("No themes available! =(\n"));
return;
}
buttons = g_malloc(PR_SIZE * sizeof(GtkWidget));
buttons = g_malloc (PR_SIZE * sizeof(GtkWidget));
buttons[PR_DIALOG] = ut_window_new(_("Preferences"), "GtkBalls_Preferences", "GtkBalls", TRUE, TRUE, TRUE, 5);
g_signal_connect(G_OBJECT(buttons[PR_DIALOG]), "destroy", G_CALLBACK(preferences_destroy_cb), buttons);
g_signal_connect (G_OBJECT(buttons[PR_DIALOG]), "destroy", G_CALLBACK(preferences_destroy_cb), buttons);
big_vbox = gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(buttons[PR_DIALOG]), big_vbox);
big_vbox = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER(buttons[PR_DIALOG]), big_vbox);
frame = gtk_frame_new(_("Preferences"));
gtk_box_pack_start(GTK_BOX(big_vbox), frame, FALSE, FALSE, 0);
frame = gtk_frame_new (_("Preferences"));
gtk_box_pack_start (GTK_BOX(big_vbox), frame, FALSE, FALSE, 0);
vbox = gtk_vbox_new(FALSE, 0);
gtk_container_set_border_width(GTK_CONTAINER(vbox), 5);
gtk_container_add(GTK_CONTAINER(frame), vbox);
vbox = gtk_vbox_new (FALSE, 0);
gtk_container_set_border_width (GTK_CONTAINER(vbox), 5);
gtk_container_add (GTK_CONTAINER(frame), vbox);
buttons[PR_SHOW_NEXT] = ut_check_button_new(_("Show colors that will appear on next turn"), pref_get_show_next_colors(), vbox);
buttons[PR_SHOW_PATH] = ut_check_button_new(_("Show path of the ball"), pref_get_show_path(), vbox);
@ -155,66 +162,66 @@ void preferences_dialog(void) {
buttons[PR_HL_DR] = ut_spin_button_start_new(_("Highlight red value: "), -128, 128, prefs_get_hl_dr(), vbox);
buttons[PR_HL_DG] = ut_spin_button_start_new(_("Highlight green value: "), -128, 128, prefs_get_hl_dg(), vbox);
buttons[PR_HL_DB] = ut_spin_button_start_new(_("Highlight blue value: "), -128, 128, prefs_get_hl_db(), vbox);
g_signal_connect(G_OBJECT(buttons[PR_SHOW_HL]), "toggled", G_CALLBACK(parent_toggled), buttons[PR_HL_DR]);
g_signal_connect(G_OBJECT(buttons[PR_SHOW_HL]), "toggled", G_CALLBACK(parent_toggled), buttons[PR_HL_DG]);
g_signal_connect(G_OBJECT(buttons[PR_SHOW_HL]), "toggled", G_CALLBACK(parent_toggled), buttons[PR_HL_DB]);
gtk_widget_set_sensitive(buttons[PR_HL_DR], pref_get_show_highlight());
gtk_widget_set_sensitive(buttons[PR_HL_DG], pref_get_show_highlight());
gtk_widget_set_sensitive(buttons[PR_HL_DB], pref_get_show_highlight());
g_signal_connect (G_OBJECT(buttons[PR_SHOW_HL]), "toggled", G_CALLBACK(parent_toggled), buttons[PR_HL_DR]);
g_signal_connect (G_OBJECT(buttons[PR_SHOW_HL]), "toggled", G_CALLBACK(parent_toggled), buttons[PR_HL_DG]);
g_signal_connect (G_OBJECT(buttons[PR_SHOW_HL]), "toggled", G_CALLBACK(parent_toggled), buttons[PR_HL_DB]);
gtk_widget_set_sensitive (buttons[PR_HL_DR], pref_get_show_highlight());
gtk_widget_set_sensitive (buttons[PR_HL_DG], pref_get_show_highlight());
gtk_widget_set_sensitive (buttons[PR_HL_DB], pref_get_show_highlight());
buttons[PR_TIME_LIMIT] = ut_check_button_new(_("Enable time limit"), timer_get_limit() > 0 ? 1 : 0, vbox);
buttons[PR_TIME_VALUE] = ut_spin_button_start_new(_("Time limit (seconds): "), 1, 3600, timer_get_limit() > 0 ? timer_get_limit() : 60, vbox);
g_signal_connect(G_OBJECT(buttons[PR_TIME_LIMIT]), "toggled", G_CALLBACK(parent_toggled), buttons[PR_TIME_VALUE]);
gtk_widget_set_sensitive(buttons[PR_TIME_VALUE], timer_get_limit() > 0 ? 1 : 0);
g_signal_connect (G_OBJECT(buttons[PR_TIME_LIMIT]), "toggled", G_CALLBACK(parent_toggled), buttons[PR_TIME_VALUE]);
gtk_widget_set_sensitive (buttons[PR_TIME_VALUE], timer_get_limit() > 0 ? 1 : 0);
theme_scrolled_window = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(theme_scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(theme_scrolled_window), GTK_SHADOW_ETCHED_IN);
gtk_box_pack_start(GTK_BOX(vbox), theme_scrolled_window, FALSE, FALSE, 5);
theme_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(theme_scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW(theme_scrolled_window), GTK_SHADOW_ETCHED_IN);
gtk_box_pack_start (GTK_BOX(vbox), theme_scrolled_window, FALSE, FALSE, 5);
store = gtk_list_store_new(1, G_TYPE_STRING);
for(i = 0, st = 0; themelist[i] != NULL; i++) {
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, 0, themelist[i], -1);
if(!strcmp(themelist[i], pref_get_theme_name())) {
store = gtk_list_store_new (1, G_TYPE_STRING);
for (i = 0, st = 0; themelist[i] != NULL; i++) {
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, 0, themelist[i], -1);
if (!strcmp(themelist[i], pref_get_theme_name())) {
st = i;
}
g_free(themelist[i]);
g_free (themelist[i]);
}
g_free(themelist);
g_free (themelist);
buttons[PR_THEME_LIST] = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
gtk_widget_set_size_request(buttons[PR_THEME_LIST], -1, 150);
g_object_unref(G_OBJECT(store));
gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(buttons[PR_THEME_LIST]), TRUE);
gtk_tree_view_set_search_column(GTK_TREE_VIEW(buttons[PR_THEME_LIST]), 0);
gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(buttons[PR_THEME_LIST])), GTK_SELECTION_BROWSE);
gtk_container_add(GTK_CONTAINER(theme_scrolled_window), buttons[PR_THEME_LIST]);
buttons[PR_THEME_LIST] = gtk_tree_view_new_with_model (GTK_TREE_MODEL(store));
gtk_widget_set_size_request (buttons[PR_THEME_LIST], -1, 150);
g_object_unref (G_OBJECT(store));
gtk_tree_view_set_rules_hint (GTK_TREE_VIEW(buttons[PR_THEME_LIST]), TRUE);
gtk_tree_view_set_search_column (GTK_TREE_VIEW(buttons[PR_THEME_LIST]), 0);
gtk_tree_selection_set_mode (gtk_tree_view_get_selection(GTK_TREE_VIEW(buttons[PR_THEME_LIST])), GTK_SELECTION_BROWSE);
gtk_container_add (GTK_CONTAINER(theme_scrolled_window), buttons[PR_THEME_LIST]);
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes(_("Select Theme"), renderer, "text", 0, NULL);
gtk_tree_view_column_set_sort_column_id(column, 0);
gtk_tree_view_append_column(GTK_TREE_VIEW(buttons[PR_THEME_LIST]), column);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes (_("Select Theme"), renderer, "text", 0, NULL);
gtk_tree_view_column_set_sort_column_id (column, 0);
gtk_tree_view_append_column (GTK_TREE_VIEW(buttons[PR_THEME_LIST]), column);
pathstr = g_strdup_printf("%u", st);
if((path = gtk_tree_path_new_from_string(pathstr))) {
gtk_tree_selection_select_path(gtk_tree_view_get_selection(GTK_TREE_VIEW(buttons[PR_THEME_LIST])), path);
gtk_tree_view_set_cursor(GTK_TREE_VIEW(buttons[PR_THEME_LIST]), path, NULL, FALSE);
gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(buttons[PR_THEME_LIST]), path, NULL, TRUE, 0, 0);
gtk_tree_path_free(path);
pathstr = g_strdup_printf ("%u", st);
if ((path = gtk_tree_path_new_from_string (pathstr))) {
gtk_tree_selection_select_path (gtk_tree_view_get_selection(GTK_TREE_VIEW(buttons[PR_THEME_LIST])), path);
gtk_tree_view_set_cursor (GTK_TREE_VIEW(buttons[PR_THEME_LIST]), path, NULL, FALSE);
gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW(buttons[PR_THEME_LIST]), path, NULL, TRUE, 0, 0);
gtk_tree_path_free (path);
}
g_free(pathstr);
g_free (pathstr);
separator = gtk_hseparator_new();
gtk_box_pack_start(GTK_BOX(big_vbox), separator, FALSE, FALSE, 5);
separator = gtk_hseparator_new ();
gtk_box_pack_start (GTK_BOX(big_vbox), separator, FALSE, FALSE, 5);
buttons_box = gtk_hbutton_box_new();
gtk_button_box_set_layout(GTK_BUTTON_BOX(buttons_box), GTK_BUTTONBOX_SPREAD);
gtk_box_pack_start(GTK_BOX(big_vbox), buttons_box, TRUE, TRUE, 0);
buttons_box = gtk_hbutton_box_new ();
gtk_button_box_set_layout (GTK_BUTTON_BOX(buttons_box), GTK_BUTTONBOX_SPREAD);
gtk_box_pack_start (GTK_BOX(big_vbox), buttons_box, TRUE, TRUE, 0);
gtk_widget_grab_default(ut_button_new_stock(GTK_STOCK_OK, preferences_ok, buttons, buttons_box));
ut_button_new_stock(GTK_STOCK_CANCEL, preferences_cancel, buttons, buttons_box);
ut_button_new_stock(GTK_STOCK_APPLY, preferences_apply, buttons, buttons_box);
gtk_widget_grab_default (ut_button_new_stock(GTK_STOCK_OK, preferences_ok, buttons, buttons_box));
ut_button_new_stock (GTK_STOCK_CANCEL, preferences_cancel, buttons, buttons_box);
ut_button_new_stock (GTK_STOCK_APPLY, preferences_apply, buttons, buttons_box);
gtk_widget_show_all(buttons[PR_DIALOG]);
gtk_widget_show_all (buttons[PR_DIALOG]);
}

View File

@ -106,7 +106,7 @@ gchar *pref_get_default_theme_name(void) {
}
void pref_set_theme_name(gchar *name) {
if(_theme_name) {
if (_theme_name) {
g_free(_theme_name);
}
_theme_name = g_strdup(name);
@ -115,7 +115,7 @@ void pref_set_theme_name(gchar *name) {
gchar *find_rc_file(void) {
gchar *rc_file = NULL;
if(getenv("HOME")) {
if (getenv("HOME")) {
rc_file = g_strdup_printf("%s/%s", getenv ("HOME"), CONFIG_FILE_NAME);
} else { /* unable to find $HOME, assuming current directory */
rc_file = g_strdup(CONFIG_FILE_NAME);
@ -126,7 +126,7 @@ gchar *find_rc_file(void) {
/* converts string to TRUE/FALSE. "true", "yes" or "1" is TRUE, otherwise - FALSE */
gboolean pref_str_to_bool(gchar *val) {
if(!g_ascii_strcasecmp(val, "true") ||
if (!g_ascii_strcasecmp(val, "true") ||
!g_ascii_strcasecmp(val, "yes") ||
!g_ascii_strcasecmp(val, "1")) {
return TRUE;
@ -142,53 +142,57 @@ gchar *pref_bool_to_str(gboolean val) {
/* we use very lame preferences file format: ``property=value\n''.
so - there must be no whitespaces on begin/end of line =) */
void load_preferences(void) {
void load_preferences(void)
{
FILE *fp;
gchar *rc_file;
gchar buffer[BUFFER_SIZE];
gchar **prop_val;
if(!_theme_name) {
if (!_theme_name) {
_theme_name = g_strdup(_default_theme_name);
}
rc_file = find_rc_file();
if((fp = fopen(rc_file, "r"))) {
while(fgets(buffer, BUFFER_SIZE, fp)) {
if ((fp = fopen(rc_file, "r")))
{
while(fgets(buffer, BUFFER_SIZE, fp))
{
g_strchomp(buffer);
prop_val = g_strsplit(buffer, "=", 2);
if(prop_val[0] && prop_val[0][0] && prop_val[1] && prop_val[1][0]) {
if(!g_ascii_strcasecmp(prop_val[0], "show_hints")) {
if (prop_val[0] && prop_val[0][0] && prop_val[1] && prop_val[1][0])
{
if (!g_ascii_strcasecmp(prop_val[0], "show_hints")) {
_show_next_colors = pref_str_to_bool(prop_val[1]);
} else if(!g_ascii_strcasecmp(prop_val[0], "show_path")) {
} else if (!g_ascii_strcasecmp(prop_val[0], "show_path")) {
_show_path = pref_str_to_bool(prop_val[1]);
} else if(!g_ascii_strcasecmp(prop_val[0], "show_footprints")) {
} else if (!g_ascii_strcasecmp(prop_val[0], "show_footprints")) {
_show_footprints = pref_str_to_bool(prop_val[1]);
} else if(!g_ascii_strcasecmp(prop_val[0], "show_destroy")) {
} else if (!g_ascii_strcasecmp(prop_val[0], "show_destroy")) {
_show_destroy = pref_str_to_bool(prop_val[1]);
} else if(!g_ascii_strcasecmp(prop_val[0], "show_highlight")) {
} else if (!g_ascii_strcasecmp(prop_val[0], "show_highlight")) {
_show_highlight = pref_str_to_bool(prop_val[1]);
} else if(!g_ascii_strcasecmp(prop_val[0], "theme_name")) {
if(_theme_name) {
} else if (!g_ascii_strcasecmp(prop_val[0], "theme_name")) {
if (_theme_name) {
g_free(_theme_name);
}
_theme_name = g_strdup(prop_val[1]);
} else if(!g_ascii_strcasecmp(prop_val[0], "rules_xsize")) {
} else if (!g_ascii_strcasecmp(prop_val[0], "rules_xsize")) {
rules_set_width(atoi(prop_val[1]));
} else if(!g_ascii_strcasecmp(prop_val[0], "rules_ysize")) {
} else if (!g_ascii_strcasecmp(prop_val[0], "rules_ysize")) {
rules_set_height(atoi(prop_val[1]));
} else if(!g_ascii_strcasecmp(prop_val[0], "rules_colors")) {
} else if (!g_ascii_strcasecmp(prop_val[0], "rules_colors")) {
rules_set_colors(atoi(prop_val[1]));
} else if(!g_ascii_strcasecmp(prop_val[0], "rules_next")) {
} else if (!g_ascii_strcasecmp(prop_val[0], "rules_next")) {
rules_set_next(atoi(prop_val[1]));
} else if(!g_ascii_strcasecmp(prop_val[0], "rules_destroy")) {
} else if (!g_ascii_strcasecmp(prop_val[0], "rules_destroy")) {
rules_set_destroy(atoi(prop_val[1]));
} else if(!g_ascii_strcasecmp(prop_val[0], "time_limit")) {
} else if (!g_ascii_strcasecmp(prop_val[0], "time_limit")) {
timer_set_limit(atoi(prop_val[1]));
} else if(!g_ascii_strcasecmp(prop_val[0], "highlight_dr")) {
} else if (!g_ascii_strcasecmp(prop_val[0], "highlight_dr")) {
prefs_set_hl_dr(atoi(prop_val[1]));
} else if(!g_ascii_strcasecmp(prop_val[0], "highlight_dg")) {
} else if (!g_ascii_strcasecmp(prop_val[0], "highlight_dg")) {
prefs_set_hl_dg(atoi(prop_val[1]));
} else if(!g_ascii_strcasecmp(prop_val[0], "highlight_db")) {
} else if (!g_ascii_strcasecmp(prop_val[0], "highlight_db")) {
prefs_set_hl_db(atoi(prop_val[1]));
}
}
@ -202,20 +206,21 @@ void load_preferences(void) {
/* writes ``property=value\n'' to fp.
why i made it separate function? for "feature purposes" =) */
void write_pref_string(FILE *fp, gchar *property, gchar *value) {
fprintf(fp, "%s=%s\n", property, value);
fprintf (fp, "%s=%s\n", property, value);
}
void write_pref_int(FILE *fp, gchar *property, gint value) {
fprintf(fp, "%s=%d\n", property, value);
fprintf (fp, "%s=%d\n", property, value);
}
gchar *save_preferences(void) {
gchar *save_preferences (void)
{
FILE *fp;
gchar *rc_file/*, *err*/;
gchar *ret = NULL;
rc_file = find_rc_file();
if((fp = fopen(rc_file, "w"))) {
if ((fp = fopen(rc_file, "w"))) {
write_pref_string(fp, "show_hints", pref_bool_to_str(_show_next_colors));
write_pref_string(fp, "show_path", pref_bool_to_str(_show_path));
write_pref_string(fp, "show_footprints", pref_bool_to_str(_show_footprints));

View File

@ -11,27 +11,28 @@
#include "gtkutils.h"
/* Show dialog box with game rules*/
void show_rules(GtkWidget *widget, gpointer data) {
GtkWidget *window;
GtkWidget *vbox, *hbox, *button_box;
GtkWidget *label;
GtkWidget *frame;
GtkWidget *separator;
GtkWidget *ok_button;
void show_rules (GtkWidget *widget, gpointer data)
{
GtkWidget * window;
GtkWidget * vbox, * hbox, * button_box;
GtkWidget * label;
GtkWidget * frame;
GtkWidget * separator;
GtkWidget * ok_button;
window=ut_window_new(_("Rules"), "GtkBalls_Rules", "GtkBalls", TRUE, FALSE, FALSE, 5);
window = ut_window_new(_("Rules"), "GtkBalls_Rules", "GtkBalls", TRUE, FALSE, FALSE, 5);
vbox=gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(window), vbox);
vbox = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER(window), vbox);
frame=gtk_frame_new(_("Rules"));
gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 3);
frame = gtk_frame_new (_("Rules"));
gtk_box_pack_start (GTK_BOX(vbox), frame, TRUE, TRUE, 3);
hbox=gtk_hbox_new(FALSE, 0);
gtk_container_set_border_width(GTK_CONTAINER(hbox), 10);
gtk_container_add(GTK_CONTAINER(frame), hbox);
hbox = gtk_hbox_new (FALSE, 0);
gtk_container_set_border_width (GTK_CONTAINER(hbox), 10);
gtk_container_add (GTK_CONTAINER(frame), hbox);
label=gtk_label_new(_("The standard play area of GtkBalls is a 9x9\n" \
label = gtk_label_new (_("The standard play area of GtkBalls is a 9x9\n" \
"grid (it can be changed through \"Rules\"\n" \
"option in ingame menu). When GtkBalls is\n" \
"first started a number of balls are placed\n" \
@ -61,16 +62,16 @@ void show_rules(GtkWidget *widget, gpointer data) {
"is an indicator on top of the grid that shows\n" \
"what colors the next balls that will appear on\n" \
"the grid will be."));
gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 5);
gtk_box_pack_start (GTK_BOX(hbox), label, TRUE, TRUE, 5);
separator=gtk_hseparator_new();
gtk_box_pack_start(GTK_BOX(vbox), separator, FALSE, FALSE, 5);
separator = gtk_hseparator_new ();
gtk_box_pack_start (GTK_BOX(vbox), separator, FALSE, FALSE, 5);
button_box=gtk_hbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(vbox), button_box, TRUE, TRUE, 2);
button_box = gtk_hbox_new (FALSE, 0);
gtk_box_pack_start (GTK_BOX(vbox), button_box, TRUE, TRUE, 2);
ok_button=ut_button_new_stock_swap(GTK_STOCK_CLOSE, gtk_widget_destroy, window, button_box);
ok_button = ut_button_new_stock_swap (GTK_STOCK_CLOSE, gtk_widget_destroy, window, button_box);
gtk_widget_grab_default(ok_button);
gtk_widget_show_all(window);
gtk_widget_grab_default (ok_button);
gtk_widget_show_all (window);
}

View File

@ -20,29 +20,32 @@ struct _gtkb_rules_dialog {
struct _gtkb_rules_dialog _rd;
void rules_ok(GtkWidget *widget, gpointer data) {
void rules_ok(GtkWidget *widget, gpointer data)
{
gint oldnext = rules_get_next();
gchar *msg;
rules_set_width(gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(_rd.xrange)));
rules_set_height(gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(_rd.yrange)));
rules_set_colors(gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(_rd.crange)));
rules_set_next(gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(_rd.nrange)));
rules_set_destroy(gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(_rd.drange)));
rules_set_width (gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(_rd.xrange)));
rules_set_height (gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(_rd.yrange)));
rules_set_colors (gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(_rd.crange)));
rules_set_next (gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(_rd.nrange)));
rules_set_destroy (gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(_rd.drange)));
reinit_board(NULL, NULL, 0, oldnext);
reinit_board (NULL, NULL, 0, oldnext);
if(data) {
gtk_widget_destroy(GTK_WIDGET(data));
gtk_widget_destroy (GTK_WIDGET(data));
}
msg = save_preferences();
msg = save_preferences ();
if(msg) {
ut_simple_message_box(msg);
g_free(msg);
ut_simple_message_box (msg);
g_free (msg);
}
}
void rules_classic(void) {
void rules_classic(void)
{
gtk_spin_button_set_value(GTK_SPIN_BUTTON(_rd.xrange), rules_get_classic_width());
gtk_spin_button_set_value(GTK_SPIN_BUTTON(_rd.yrange), rules_get_classic_height());
gtk_spin_button_set_value(GTK_SPIN_BUTTON(_rd.crange), rules_get_classic_colors());
@ -50,46 +53,48 @@ void rules_classic(void) {
gtk_spin_button_set_value(GTK_SPIN_BUTTON(_rd.drange), rules_get_classic_destroy());
}
void rules_dialog(void) {
GtkWidget *dialog;
GtkWidget *frame;
GtkWidget *big_vbox, *vbox, *buttons_box;
GtkWidget *separator;
GtkWidget *ok_button, *cancel_button, *classic_button;
void rules_dialog (void)
{
GtkWidget * dialog;
GtkWidget * frame;
GtkWidget * big_vbox, * vbox, * buttons_box;
GtkWidget * separator;
GtkWidget * ok_button, * cancel_button, * classic_button;
dialog = ut_window_new(_("Game rules"), "GtkBalls_Rules", "GtkBalls", TRUE, TRUE, TRUE, 5);
big_vbox = gtk_vbox_new(FALSE, 5);
gtk_container_add(GTK_CONTAINER(dialog), big_vbox);
big_vbox = gtk_vbox_new (FALSE, 5);
gtk_container_add (GTK_CONTAINER(dialog), big_vbox);
frame = gtk_frame_new(_("Game rules"));
gtk_box_pack_start(GTK_BOX(big_vbox), frame, FALSE, FALSE, 0);
frame = gtk_frame_new (_("Game rules"));
gtk_box_pack_start (GTK_BOX(big_vbox), frame, FALSE, FALSE, 0);
vbox = gtk_vbox_new(FALSE, 0);
gtk_container_set_border_width(GTK_CONTAINER(vbox), 5);
gtk_container_add(GTK_CONTAINER(frame), vbox);
vbox = gtk_vbox_new (FALSE, 0);
gtk_container_set_border_width (GTK_CONTAINER(vbox), 5);
gtk_container_add (GTK_CONTAINER(frame), vbox);
_rd.xrange = ut_spin_button_new(_("Board width"), 4, 99, rules_get_width(), vbox);
_rd.yrange = ut_spin_button_new(_("Board height"), 4, 99, rules_get_height(), vbox);
_rd.crange = ut_spin_button_new(_("Number of different objects"),
_rd.xrange = ut_spin_button_new (_("Board width"), 4, 99, rules_get_width(), vbox);
_rd.yrange = ut_spin_button_new (_("Board height"), 4, 99, rules_get_height(), vbox);
_rd.crange = ut_spin_button_new (_("Number of different objects"),
3, gtkb_theme_get_balls_num(), rules_get_colors(), vbox);
_rd.nrange = ut_spin_button_new(_("Number of 'next' objects"), 2, 99, rules_get_next(), vbox);
_rd.drange = ut_spin_button_new(_("How many balls at line eliminate it"),
_rd.nrange = ut_spin_button_new (_("Number of 'next' objects"), 2, 99, rules_get_next(), vbox);
_rd.drange = ut_spin_button_new (_("How many balls at line eliminate it"),
3, 99, rules_get_destroy(), vbox);
classic_button = ut_button_new(_("Classic rules"), rules_classic, dialog, big_vbox);
classic_button = ut_button_new (_("Classic rules"), rules_classic, dialog, big_vbox);
separator = gtk_hseparator_new();
gtk_box_pack_start(GTK_BOX(big_vbox), separator, FALSE, FALSE, 5);
separator = gtk_hseparator_new ();
gtk_box_pack_start (GTK_BOX(big_vbox), separator, FALSE, FALSE, 5);
buttons_box = gtk_hbutton_box_new();
gtk_button_box_set_layout(GTK_BUTTON_BOX(buttons_box), GTK_BUTTONBOX_SPREAD);
gtk_box_pack_start(GTK_BOX(big_vbox), buttons_box, TRUE, TRUE, 0);
buttons_box = gtk_hbutton_box_new ();
gtk_button_box_set_layout (GTK_BUTTON_BOX(buttons_box), GTK_BUTTONBOX_SPREAD);
gtk_box_pack_start (GTK_BOX(big_vbox), buttons_box, TRUE, TRUE, 0);
ok_button = ut_button_new_stock(GTK_STOCK_OK, rules_ok, dialog, buttons_box);
cancel_button = ut_button_new_stock_swap(GTK_STOCK_CANCEL, gtk_widget_destroy, dialog, buttons_box);
ok_button = ut_button_new_stock (GTK_STOCK_OK, rules_ok, dialog, buttons_box);
cancel_button = ut_button_new_stock_swap (GTK_STOCK_CANCEL, gtk_widget_destroy, dialog, buttons_box);
gtk_widget_grab_default(ok_button);
gtk_widget_show_all(dialog);
gtk_widget_grab_default (ok_button);
gtk_widget_show_all (dialog);
}

View File

@ -25,15 +25,15 @@ void do_load_game(GtkWidget *widget, gpointer data) {
gchar *rules = NULL;
gint w, h, c, n, d;
if(!_selected_save_load) {
if (!_selected_save_load) {
ut_simple_message_box(_("No game selected for load.\n"));
return;
}
if(!parse_save_game(_selected_save_load, &rules, &score, &board, &next)) {
if (!parse_save_game(_selected_save_load, &rules, &score, &board, &next)) {
errormsg = g_strdup_printf(_("Cannot load game from:\n%s\n"), _selected_save_load);
} else {
rules_get_from_str(rules, &w, &h, &c, &n, &d);
if(c > gtkb_theme_get_balls_num()) {
if (c > gtkb_theme_get_balls_num()) {
errormsg = g_strdup_printf(_("Not enough balls(%d) in current theme.\nWe need %d balls.\nLoad another theme and try again."),
gtkb_theme_get_balls_num(), c);
g_free(rules);
@ -42,7 +42,7 @@ void do_load_game(GtkWidget *widget, gpointer data) {
}
}
g_free(_selected_save_load);
if(errormsg) {
if (errormsg) {
ut_simple_message_box(errormsg);
g_free(errormsg);
return;
@ -53,7 +53,7 @@ void do_load_game(GtkWidget *widget, gpointer data) {
reinit_board(board, next, score, oldnext);
g_free(board);
g_free(next);
if(data) {
if (data) {
gtk_widget_destroy(GTK_WIDGET(data));
}
}
@ -63,21 +63,21 @@ void do_delete_game(GtkWidget *widget, GtkWidget *treeview) {
GtkTreeIter iter, nextiter;
GValue value = {0, };
if(gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview)),
&model, &iter)) {
if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview)),&model, &iter))
{
gtk_tree_model_get_value(model, &iter, 1, &value);
if(g_value_get_string(&value)) {
if (g_value_get_string(&value)) {
unlink(g_value_get_string(&value));
gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
if(iter.stamp == 0) {
if(gtk_tree_model_get_iter_first(model, &iter)) {
if (iter.stamp == 0) {
if (gtk_tree_model_get_iter_first(model, &iter)) {
do {
nextiter = iter;
} while(gtk_tree_model_iter_next(model, &iter));
} while (gtk_tree_model_iter_next(model, &iter));
iter = nextiter;
}
}
if(iter.stamp != 0) {
if (iter.stamp != 0) {
gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview)), &iter);
}
}
@ -89,7 +89,7 @@ void do_save_game(GtkWidget *widget, gpointer data) {
gchar *fname = NULL, *errormsg, *rules;
gint *b, *n;
if(_selected_save_load) {
if (_selected_save_load) {
/* TODO: alert stupid user about erasing file... */
unlink(_selected_save_load);
g_free(_selected_save_load);
@ -103,13 +103,13 @@ void do_save_game(GtkWidget *widget, gpointer data) {
g_free(b);
g_free(rules);
if(fname != NULL) {
if (fname != NULL) {
errormsg = g_strdup_printf(_("Cannot save game to:\n%s\n%s"), fname, strerror(errno));
ut_simple_message_box(errormsg);
g_free(fname);
g_free(errormsg);
}
if(data) {
if (data) {
gtk_widget_destroy(GTK_WIDGET(data));
}
}
@ -126,12 +126,12 @@ void sl_row_activated(GtkTreeSelection *selection, GtkTreeModel *model) {
GtkTreeIter iter;
GValue value = {0, };
if(gtk_tree_selection_get_selected(selection, NULL, &iter)) {
if(_selected_save_load) {
if (gtk_tree_selection_get_selected(selection, NULL, &iter)) {
if (_selected_save_load) {
g_free(_selected_save_load);
}
gtk_tree_model_get_value(model, &iter, 1, &value);
if(g_value_get_string(&value)) {
if (g_value_get_string(&value)) {
_selected_save_load = g_strdup((gchar *)g_value_get_string(&value));
} else {
_selected_save_load = NULL;
@ -143,13 +143,15 @@ void sl_row_activated(GtkTreeSelection *selection, GtkTreeModel *model) {
void free_gamelist(gchar **gamelist, gint num) {
gint i;
for(i = 0; i < num * 2; i++) {
for (i = 0; i < num * 2; i++) {
g_free(gamelist[i]);
}
g_free(gamelist);
}
gint game_compare_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer user_data) {
gint game_compare_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer user_data)
{
GValue value_a = {0, };
GValue value_b = {0, };
gchar *str_a, *str_b;
@ -158,9 +160,9 @@ gint game_compare_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpoi
gtk_tree_model_get_value(model, a, 0, &value_a);
gtk_tree_model_get_value(model, b, 0, &value_b);
if((str_a = (gchar *)g_value_get_string(&value_a)) && (str_b = (gchar *)g_value_get_string(&value_b))) {
for(i = 0; i < sizeof(sort_pos) / sizeof(gint); i++) {
if(str_a[sort_pos[i]] != str_b[sort_pos[i]]) {
if ((str_a = (gchar *)g_value_get_string(&value_a)) && (str_b = (gchar *)g_value_get_string(&value_b))) {
for (i = 0; i < sizeof(sort_pos) / sizeof(gint); i++) {
if (str_a[sort_pos[i]] != str_b[sort_pos[i]]) {
r = str_a[sort_pos[i]] < str_b[sort_pos[i]] ? 1 : -1;
break;
}
@ -172,23 +174,26 @@ gint game_compare_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpoi
return r;
}
void save_load_game_dialog(gboolean is_save) {
GtkListStore *store;
void save_load_game_dialog(gboolean is_save)
{
GtkListStore * store;
GtkTreeIter iter;
GtkWidget *treeview;
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
GtkTreePath *path;
GtkWidget *window, *swindow;
GtkWidget *vbox, *button_box;
GtkWidget *ok_button, *cancel_button, *delete_button;
GtkWidget * treeview;
GtkCellRenderer * renderer;
GtkTreeViewColumn * column;
GtkTreePath * path;
GtkWidget * window, * swindow;
GtkWidget * vbox, * button_box;
GtkWidget * ok_button, * cancel_button, *delete_button;
gint i, num;
gchar **gamelist, str1[20], *str2;
gchar ** gamelist, str1[20], * str2;
_selected_save_load = NULL;
num = get_saved_games(&gamelist);
if(!is_save && !num) {
num = get_saved_games (&gamelist);
if (!is_save && !num) {
ut_simple_message_box(_("No saved games found.\n"));
return;
}
@ -197,94 +202,97 @@ void save_load_game_dialog(gboolean is_save) {
is_save ? "GtkBalls_Save" : "GtkBalls_Load",
"GtkBalls", TRUE, TRUE, TRUE, 5);
vbox = gtk_vbox_new(FALSE, 0);
gtk_container_set_border_width(GTK_CONTAINER(vbox), 1);
gtk_container_add(GTK_CONTAINER(window), vbox);
vbox = gtk_vbox_new (FALSE, 0);
gtk_container_set_border_width (GTK_CONTAINER(vbox), 1);
gtk_container_add (GTK_CONTAINER(window), vbox);
swindow = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swindow), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swindow), GTK_SHADOW_ETCHED_IN);
gtk_box_pack_start(GTK_BOX(vbox), swindow, TRUE, TRUE, 0);
swindow = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(swindow), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW(swindow), GTK_SHADOW_ETCHED_IN);
gtk_box_pack_start (GTK_BOX(vbox), swindow, TRUE, TRUE, 0);
store = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
for(i = 0; i < num; i++) {
gtk_list_store_append(store, &iter);
memcpy(str1, gamelist[i * 2], 19 * sizeof(gchar));
store = gtk_list_store_new (3,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_STRING);
for (i = 0; i < num; i++) {
gtk_list_store_append (store, &iter);
memcpy (str1, gamelist[i * 2], 19 * sizeof(gchar));
str1[19] = '\0';
str2 = g_strndup(gamelist[i * 2] + 21 * sizeof(gchar),
str2 = g_strndup (gamelist[i * 2] + 21 * sizeof(gchar),
(strlen(gamelist[i * 2]) - 22) * sizeof(gchar));
gtk_list_store_set(store, &iter, 0, str1, 1, (GValue *)gamelist[i * 2 + 1], 2, str2, -1);
g_free(str2);
gtk_list_store_set (store, &iter, 0, str1, 1, (GValue *)gamelist[i * 2 + 1], 2, str2, -1);
g_free (str2);
}
if(is_save) {
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, 0, _("Empty"), -1);
if (is_save) {
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, 0, _("Empty"), -1);
}
free_gamelist(gamelist, num);
treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(treeview), TRUE);
gtk_tree_view_set_search_column(GTK_TREE_VIEW(treeview), 0);
gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview)), GTK_SELECTION_BROWSE);
g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview))),
treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL(store));
gtk_tree_view_set_rules_hint (GTK_TREE_VIEW(treeview), TRUE);
gtk_tree_view_set_search_column (GTK_TREE_VIEW(treeview), 0);
gtk_tree_selection_set_mode (gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview)), GTK_SELECTION_BROWSE);
g_signal_connect (G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview))),
"changed", G_CALLBACK(sl_row_activated), store);
gtk_container_add(GTK_CONTAINER(swindow), treeview);
gtk_container_add (GTK_CONTAINER(swindow), treeview);
renderer = gtk_cell_renderer_text_new();
g_object_set(G_OBJECT(renderer), "xpad", 5, NULL);
column = gtk_tree_view_column_new_with_attributes(_("Date"), renderer, "text", 0, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
renderer = gtk_cell_renderer_text_new ();
g_object_set (G_OBJECT(renderer), "xpad", 5, NULL);
column = gtk_tree_view_column_new_with_attributes (_("Date"), renderer, "text", 0, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW(treeview), column);
gtk_tree_view_column_set_sort_column_id(column, 0);
gtk_tree_view_column_set_sort_order(column, GTK_SORT_DESCENDING);
gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), 0, game_compare_func, NULL, NULL);
gtk_tree_view_column_clicked(column);
gtk_tree_view_column_set_sort_column_id (column, 0);
gtk_tree_view_column_set_sort_order (column, GTK_SORT_DESCENDING);
gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE(store), 0, game_compare_func, NULL, NULL);
gtk_tree_view_column_clicked (column);
renderer = gtk_cell_renderer_text_new();
g_object_set(G_OBJECT(renderer), "xalign", 1.0, NULL);
g_object_set(G_OBJECT(renderer), "xpad", 5, NULL);
column = gtk_tree_view_column_new_with_attributes(_("Score"), renderer, "text", 2, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
renderer = gtk_cell_renderer_text_new ();
g_object_set (G_OBJECT(renderer), "xalign", 1.0, NULL);
g_object_set (G_OBJECT(renderer), "xpad", 5, NULL);
column = gtk_tree_view_column_new_with_attributes (_("Score"), renderer, "text", 2, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW(treeview), column);
if(iter.stamp == store->stamp) {
if(is_save) {
path = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &iter);
if (iter.stamp == store->stamp) {
if (is_save) {
path = gtk_tree_model_get_path (GTK_TREE_MODEL(store), &iter);
} else {
path = gtk_tree_path_new_from_string("0");
path = gtk_tree_path_new_from_string ("0");
}
if(path) {
gtk_tree_selection_select_path(gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview)), path);
gtk_tree_view_set_cursor(GTK_TREE_VIEW(treeview), path, NULL, FALSE);
gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(treeview), path, NULL, TRUE, 0, 0);
gtk_tree_path_free(path);
if (path) {
gtk_tree_selection_select_path (gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview)), path);
gtk_tree_view_set_cursor (GTK_TREE_VIEW(treeview), path, NULL, FALSE);
gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW(treeview), path, NULL, TRUE, 0, 0);
gtk_tree_path_free (path);
}
}
g_object_unref(G_OBJECT(store));
g_object_unref (G_OBJECT(store));
button_box = gtk_hbox_new(TRUE, 10);
if(is_save) {
ok_button = ut_button_new(_("Save game"), do_save_game, window, button_box);
g_signal_connect(G_OBJECT(treeview), "row_activated", G_CALLBACK(save_row_activated_cb), window);
button_box = gtk_hbox_new (TRUE, 10);
if (is_save) {
ok_button = ut_button_new (_("Save game"), do_save_game, window, button_box);
g_signal_connect (G_OBJECT(treeview), "row_activated", G_CALLBACK(save_row_activated_cb), window);
} else {
ok_button = ut_button_new(_("Load game"), do_load_game, window, button_box);
ok_button = ut_button_new (_("Load game"), do_load_game, window, button_box);
g_signal_connect(G_OBJECT(treeview), "row_activated", G_CALLBACK(load_row_activated_cb), window);
}
delete_button = ut_button_new(_("Delete game"), do_delete_game, treeview, button_box);
delete_button = ut_button_new (_("Delete game"), do_delete_game, treeview, button_box);
cancel_button = ut_button_new_stock_swap(GTK_STOCK_CANCEL, gtk_widget_destroy, window, button_box);
gtk_box_pack_start(GTK_BOX(vbox), button_box, FALSE, FALSE, 4);
cancel_button = ut_button_new_stock_swap (GTK_STOCK_CANCEL, gtk_widget_destroy, window, button_box);
gtk_box_pack_start (GTK_BOX(vbox), button_box, FALSE, FALSE, 4);
gtk_widget_grab_default(ok_button);
gtk_widget_grab_focus(ok_button);
gtk_widget_grab_default (ok_button);
gtk_widget_grab_focus (ok_button);
gtk_window_set_default_size(GTK_WINDOW(window), -1, 300);
gtk_widget_show_all(window);
gtk_window_set_default_size (GTK_WINDOW(window), -1, 300);
gtk_widget_show_all (window);
}
void save_game_cb(GtkWidget *widget, gpointer data) {
save_load_game_dialog(TRUE);
save_load_game_dialog (TRUE);
}
void load_game_cb(GtkWidget *widget, gpointer data) {
save_load_game_dialog(FALSE);
save_load_game_dialog (FALSE);
}

View File

@ -17,22 +17,22 @@
#include "game.h"
gint parse_save_game(gchar *sgame, gchar **rules, gint *score, gint **board, gint **next) {
gint parse_save_game(gchar *sgame, gchar **rules, gint *score, gint **board, gint **next)
{
struct stat buf;
gchar *sdata, *psdata, *srules;
FILE *f;
gint i, val;
gint rw, rh, rc, rn, rd, rlen;
if(stat(sgame, &buf) != 0 || !S_ISREG(buf.st_mode) || buf.st_size < 20 ||
(f = fopen(sgame, "r")) == NULL) {
if (stat(sgame, &buf) != 0 || !S_ISREG(buf.st_mode) || buf.st_size < 20 || (f = fopen(sgame, "r")) == NULL) {
return 0;
}
sdata = g_malloc(buf.st_size);
if(fread(sdata, 1, buf.st_size, f) != buf.st_size) {
g_free(sdata);
if (fread(sdata, 1, buf.st_size, f) != buf.st_size) {
g_free (sdata);
return 0;
}
@ -43,56 +43,58 @@ gint parse_save_game(gchar *sgame, gchar **rules, gint *score, gint **board, gin
srules[rlen] = 0;
memcpy(srules, sdata, rlen);
if(!rules_get_from_str(srules, &rw, &rh, &rc, &rn, &rd) ||
if (!rules_get_from_str(srules, &rw, &rh, &rc, &rn, &rd) ||
sscanf(sdata + rlen, "%010u", score) != 1 ||
buf.st_size != rlen + 10 + rw * rh * 2 + rn * 2) {
printf("[%s]\n", srules);
g_free(srules);
g_free(sdata);
g_free (srules);
g_free (sdata);
return 0;
}
g_free(srules);
g_free (srules);
*board = g_malloc(rw * rh * sizeof(gint));
*next = g_malloc(rn * sizeof(gint));
psdata = sdata + rlen + 10;
for(i = 0; i < rw * rh; i++, psdata += 2) {
if(sscanf(psdata, "%02d", &val) != 1 || val < 0 || val > rc) {
g_free(*next);
g_free(*board);
g_free(sdata);
for (i = 0; i < rw * rh; i++, psdata += 2) {
if (sscanf(psdata, "%02d", &val) != 1 || val < 0 || val > rc) {
g_free (*next);
g_free (*board);
g_free (sdata);
return 0;
}
(*board)[i] = val;
}
for(i = 0; i < rn; i++, psdata += 2) {
if(sscanf(psdata, "%02d", &val) != 1 || val < 0 || val > rc) {
g_free(*next);
g_free(*board);
g_free(sdata);
for (i = 0; i < rn; i++, psdata += 2) {
if (sscanf(psdata, "%02d", &val) != 1 || val < 0 || val > rc) {
g_free (*next);
g_free (*board);
g_free (sdata);
return 0;
}
(*next)[i] = val;
}
*rules = g_strndup(sdata, rlen);
g_free(sdata);
g_free (sdata);
return 1;
}
/* check that save game name is in form YYYY-MM-DD-HHMMSS.sav and have correct content
return string "DD.MM.YYYY HH:MM:SS (score)" on success, NULL on failure */
gchar *is_valid_save_game(gchar *name, gchar *path) {
gchar *is_valid_save_game(gchar *name, gchar *path)
{
guint i, y, m, d, h, min, s;
gint score, *board, *next;
gchar *sgame;
gchar *rules;
if((i = sscanf(name, "%04u-%02u-%02u-%02u%02u%02u", &y, &m, &d, &h, &min, &s)) != 6 ||
if ((i = sscanf(name, "%04u-%02u-%02u-%02u%02u%02u", &y, &m, &d, &h, &min, &s)) != 6 ||
!m || m > 12 || !d || d > 31 || h > 23 || min > 59 || s > 61 ||
(strcmp(name + strlen(name) - 4, ".sav") != 0)) {
return NULL;
@ -101,13 +103,13 @@ gchar *is_valid_save_game(gchar *name, gchar *path) {
sgame=g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
i = parse_save_game(sgame, &rules, &score, &board, &next);
g_free(sgame);
g_free (sgame);
if(!i) return NULL;
if (!i) return NULL;
g_free(rules);
g_free(board);
g_free(next);
g_free (rules);
g_free (board);
g_free (next);
return g_strdup_printf("%02d.%02d.%04d %02d:%02d:%02d (%d)",
d, m, y, h, min, s, score);
@ -118,7 +120,9 @@ gchar *is_valid_save_game(gchar *name, gchar *path) {
*/
}
gint get_saved_games(gchar ***gamelist) {
gint get_saved_games(gchar ***gamelist)
{
gchar *datapath;
struct stat buf;
struct dirent *dir_entry;
@ -127,18 +131,19 @@ gint get_saved_games(gchar ***gamelist) {
gint num = 0;
datapath = g_strconcat(getenv("HOME"), G_DIR_SEPARATOR_S, ".gtkballs", NULL);
if(stat(datapath, &buf) != 0) { /* no ~/.gtkballs */
if(mkdir(datapath, 0700) != 0) { /* and cannot create it... */
g_free(datapath);
if (stat(datapath, &buf) != 0) { /* no ~/.gtkballs */
if (mkdir(datapath, 0700) != 0) { /* and cannot create it... */
g_free (datapath);
return -1;
}
} else if(!S_ISDIR(buf.st_mode)) { /* ~/.gtkballs is not a directory */
g_free(datapath);
} else if (!S_ISDIR(buf.st_mode)) { /* ~/.gtkballs is not a directory */
g_free (datapath);
return -1;
}
if((directory = opendir(datapath))) {
while((dir_entry = readdir(directory))) {
if((game = is_valid_save_game(dir_entry->d_name, datapath)) != NULL) {
if ((directory = opendir(datapath)))
{
while ((dir_entry = readdir(directory))) {
if ((game = is_valid_save_game(dir_entry->d_name, datapath)) != NULL) {
num++;
games = g_realloc(games, sizeof(gchar *) * num * 2);
games[(num - 1) * 2] = game;
@ -147,12 +152,14 @@ gint get_saved_games(gchar ***gamelist) {
}
closedir(directory);
}
g_free(datapath);
g_free (datapath);
*gamelist = games;
return num;
}
gchar *save_game(gchar *rules, gint score, gint *board, gint *nextcolors) {
gchar *save_game(gchar *rules, gint score, gint *board, gint *nextcolors)
{
FILE *f;
gchar *fname;
time_t nowtime;
@ -163,15 +170,15 @@ gchar *save_game(gchar *rules, gint score, gint *board, gint *nextcolors) {
strftime(ftime, sizeof(ftime), "%Y-%m-%d-%H%M%S", localtime(&nowtime));
fname = g_strconcat(getenv("HOME"), G_DIR_SEPARATOR_S, ".gtkballs",
G_DIR_SEPARATOR_S, ftime, ".sav", NULL);
if((f = fopen(fname, "w")) != NULL) {
if ((f = fopen(fname, "w")) != NULL) {
chmod(fname, 0600);
/* TODO: check for errors ! */
fprintf(f, rules);
fprintf(f, "%010d", score);
for(i = 0; i < rules_get_width() * rules_get_height(); i++) {
for (i = 0; i < rules_get_width() * rules_get_height(); i++) {
fprintf(f, "%02d", board[i]);
}
for(i = 0; i < rules_get_next(); i++) {
for (i = 0; i < rules_get_next(); i++) {
fprintf(f, "%02d", nextcolors[i]);
}
fclose(f);
@ -179,7 +186,7 @@ gchar *save_game(gchar *rules, gint score, gint *board, gint *nextcolors) {
return fname;
}
g_free(fname);
g_free (fname);
return NULL;
}

View File

@ -1,7 +1,7 @@
/* scoreboard.c - save/load scores
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* modif (y 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.
*/
@ -23,37 +23,43 @@
int _score_fd = -1;
gint score_setup(void) {
gint score_setup(void)
{
_score_fd = child_setup(SCORE_FILE);
return _score_fd;
}
void free_score_board_full(struct score_board_full *bf, gint nbf) {
void free_score_board_full(struct score_board_full *bf, gint nbf)
{
gint i;
for(i = 0; i < nbf; i++) {
for (i = 0; i < nbf; i++) {
g_free(bf[i].rules);
}
g_free(bf);
}
gint write_score(struct score_board *b, struct score_board_full *bf, gint nbf) {
gint write_score(struct score_board *b, struct score_board_full *bf, gint nbf)
{
gint i;
gchar *buf, *tname, *tdate, *rules;
size_t sz;
if(child_writer_alive() == 0) {
if (child_writer_alive() == 0) {
return FALSE;
}
for(i = 0; i < 10; i++) {
if(strlen(b[i].name)) {
for (i = 0; i < 10; i++)
{
if (strlen(b[i].name)) {
tname = g_strdup(b[i].name);
if(!tname) {
if (!tname) {
tname = g_strdup(_("Unknown"));
}
tdate = g_strdup(b[i].date);
if(!tdate) {
if (!tdate) {
tdate = g_strdup(_("Unknown"));
}
rules = rules_get_as_str();
@ -67,8 +73,8 @@ gint write_score(struct score_board *b, struct score_board_full *bf, gint nbf) {
g_free(buf);
}
}
for(i = 0; i < nbf; i++) {
if(strlen(bf[i].name)) {
for (i = 0; i < nbf; i++) {
if (strlen(bf[i].name)) {
buf = g_strdup_printf("%s\t%i\t%s\t%s\n", bf[i].name, bf[i].score, bf[i].date, bf[i].rules);
sz = strlen(buf);
write(_score_fd, &sz, sizeof(sz));
@ -82,13 +88,17 @@ gint write_score(struct score_board *b, struct score_board_full *bf, gint nbf) {
return TRUE;
}
int score_sort(const void *a, const void *b) {
if(((const struct score_board *)a)->score == ((const struct score_board *)b)->score) return 0;
if(((const struct score_board *)a)->score < ((const struct score_board *)b)->score) return 1;
int score_sort(const void *a, const void *b)
{
if (((const struct score_board *)a)->score == ((const struct score_board *)b)->score) return 0;
if (((const struct score_board *)a)->score < ((const struct score_board *)b)->score) return 1;
return -1;
}
gint read_score(struct score_board *b, struct score_board_full **bf, gint *nbf) {
gint read_score(struct score_board *b, struct score_board_full **bf, gint *nbf)
{
FILE *fp;
gchar buffer[BUFFER_SIZE];
gchar **str_val, *tstr, **tstr_val;
@ -100,7 +110,7 @@ gint read_score(struct score_board *b, struct score_board_full **bf, gint *nbf)
memset(b, 0, sizeof(struct score_board) * 10);
if(!(fp = fopen(LOCALSTATEDIR SCORE_FILE, "r"))) {
if (!(fp = fopen(LOCALSTATEDIR SCORE_FILE, "r"))) {
return FALSE;
}
@ -110,29 +120,31 @@ gint read_score(struct score_board *b, struct score_board_full **bf, gint *nbf)
lockinfo.l_len = 0;
lockinfo.l_type = F_WRLCK;
fcntl(fileno(fp), F_GETLK, &lockinfo);
} while(lockinfo.l_type != F_UNLCK);
} while (lockinfo.l_type != F_UNLCK);
sc = 0;
fsc = 0;
while(fgets(buffer, BUFFER_SIZE, fp)) {
while (fgets(buffer, BUFFER_SIZE, fp))
{
g_strchomp(buffer);
str_val = g_strsplit(buffer, "\t", 4);
if(str_val[0] && str_val[0][0] &&
if (str_val[0] && str_val[0][0] &&
str_val[1] && str_val[1][0] &&
str_val[2] && str_val[2][0]) {
str_val[2] && str_val[2][0])
{
valid = 0;
if(!str_val[3]) { /* < 2.2.0 file format */
if (!str_val[3]) { /* < 2.2.0 file for mat */
g_rules = rules_get_classic_as_str();
valid = 1;
} else {
if((valid = rules_check_str(str_val[3]))) {
if ((valid = rules_check_str(str_val[3]))) {
g_rules = g_strdup(str_val[3]);
} else { /* > 2.2.0 && < 3.0.2 file format ? */
} else { /* > 2.2.0 && < 3.0.2 file for mat ? */
tstr_val = g_strsplit(str_val[3], "\t", 5);
if(tstr_val[0] && tstr_val[1] && tstr_val[2] &&
if (tstr_val[0] && tstr_val[1] && tstr_val[2] &&
tstr_val[3] && tstr_val[4]) {
g_rules = rules_conv_3_0_to_str(tstr_val[0], tstr_val[1], tstr_val[2], tstr_val[3], tstr_val[4]);
if(g_rules) { /* yes. its < 3.0.2 format */
if (g_rules) { /* yes. its < 3.0.2 for mat */
valid = 1;
} else { /* nope. just piece of shit. */
g_rules = g_strdup("");
@ -141,40 +153,40 @@ gint read_score(struct score_board *b, struct score_board_full **bf, gint *nbf)
g_strfreev(tstr_val);
}
}
if(valid && g_ascii_strcasecmp(str_val[0], "<none>")) {
if(rules_is_current_str(g_rules)) {
if(g_utf8_validate(str_val[0], -1, NULL)) {
if (valid && g_ascii_strcasecmp(str_val[0], "<none>")) {
if (rules_is_current_str(g_rules)) {
if (g_utf8_validate(str_val[0], -1, NULL)) {
tstr = g_strdup(str_val[0]);
} else {
tstr = g_locale_to_utf8(str_val[0], -1, &br, &bw, NULL);
}
if(tstr) {
if (tstr) {
strncpy(b[sc].name, tstr, sizeof(b[sc].name));
g_free(tstr);
} else {
strncpy(b[sc].name, _("Unknown"), sizeof(b[sc].name));
}
b[sc].score = strtol(str_val[1], NULL, 10);
if((b[sc].score == LONG_MIN) || (b[sc].score == LONG_MAX)) {
if ((b[sc].score == LONG_MIN) || (b[sc].score == LONG_MAX)) {
b[sc].score = 0;
}
if(g_utf8_validate(str_val[2], -1, NULL)) {
if (g_utf8_validate(str_val[2], -1, NULL)) {
tstr = g_strdup(str_val[2]);
} else {
tstr = g_locale_to_utf8(str_val[2], -1, &br, &bw, NULL);
}
if(tstr) {
if (tstr) {
strncpy(b[sc].date, tstr, sizeof(b[sc].date));
g_free(tstr);
} else {
strncpy(b[sc].date, _("Unknown"), sizeof(b[sc].date));
}
sc++;
} else if(bf) {
} else if (bf) {
*bf = g_realloc(*bf, sizeof(struct score_board_full) * (fsc + 1));
strncpy((*bf)[fsc].name, str_val[0], sizeof((*bf)[fsc].name));
(*bf)[fsc].score = strtol(str_val[1], NULL, 10);
if(((*bf)[fsc].score == LONG_MIN) || ((*bf)[fsc].score == LONG_MAX)) {
if (((*bf)[fsc].score == LONG_MIN) || ((*bf)[fsc].score == LONG_MAX)) {
(*bf)[fsc].score = 0;
}
strncpy((*bf)[fsc].date, str_val[2], sizeof((*bf)[fsc].date));
@ -190,29 +202,31 @@ gint read_score(struct score_board *b, struct score_board_full **bf, gint *nbf)
qsort(b, 10, sizeof(struct score_board), score_sort);
if(nbf) {
if (nbf) {
*nbf = fsc;
}
return TRUE;
}
gint insert_entry_in_score_board(struct score_board *board, struct score_board entry) {
gint insert_entry_in_score_board(struct score_board *board, struct score_board entry)
{
gint i=0,j;
if(entry.score <= 0) {
if (entry.score <= 0) {
return -1;
}
while(i < 10 && board[i].score > entry.score) {
while (i < 10 && board[i].score > entry.score) {
i++;
}
if(i > 9) {
if (i > 9) {
return -1;
}
for(j = 8;j >= i; j--) {
for (j = 8;j >= i; j--) {
strncpy(board[j + 1].name, board[j].name, sizeof(board[j + 1].name));
strncpy(board[j + 1].date, board[j].date, sizeof(board[j + 1].date));
board[j + 1].score = board[j].score;

View File

@ -1,7 +1,7 @@
/* theme.c - functions related to theme handling
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* modif (y 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.
*/
@ -30,7 +30,9 @@ gchar *THEMEPREFIX="/.gtkballs/themes/";
#define DELTA(d, h) \
d = h < 0 ? (d > -h ? d + h : 0) : (d + h < 255 ? d + h : 255)
void hilight_pixbuff8(GdkPixbuf *pb, gint dr, gint dg, gint db) {
void hilight_pixbuff8(GdkPixbuf *pb, gint dr, gint dg, gint db)
{
/* pb created with 8b/rgb without alpha */
gint i;
gint nc = gdk_pixbuf_get_n_channels(pb);
@ -40,8 +42,8 @@ void hilight_pixbuff8(GdkPixbuf *pb, gint dr, gint dg, gint db) {
gint d = gdk_pixbuf_get_rowstride(pb) - w * nc;
guchar *data = gdk_pixbuf_get_pixels(pb);
for(i = 0; i < l; i++) {
if(i && (i%w == 0)) {
for (i = 0; i < l; i++) {
if (i && (i%w == 0)) {
data += d;
}
DELTA(*data, dr);
@ -53,117 +55,129 @@ void hilight_pixbuff8(GdkPixbuf *pb, gint dr, gint dg, gint db) {
}
}
gchar *find_theme_path(gchar *themename) {
gchar *find_theme_path(gchar *themename)
{
gchar *homedir;
gchar *themepath;
struct stat buf;
if((homedir = getenv("HOME")) &&
if ((homedir = getenv("HOME")) &&
(themepath = g_strconcat(homedir, THEMEPREFIX, themename, G_DIR_SEPARATOR_S, NULL))) {
if(!stat(themepath, &buf) && S_ISDIR(buf.st_mode)) {
if (!stat(themepath, &buf) && S_ISDIR(buf.st_mode)) {
return themepath;
}
g_free(themepath);
g_free (themepath);
}
if((themepath = g_strconcat(INSTALLPATH, themename, G_DIR_SEPARATOR_S, NULL))) {
if(!stat(themepath, &buf) && S_ISDIR(buf.st_mode)) {
if ((themepath = g_strconcat(INSTALLPATH, themename, G_DIR_SEPARATOR_S, NULL))) {
if (!stat(themepath, &buf) && S_ISDIR(buf.st_mode)) {
return themepath;
}
g_free(themepath);
g_free (themepath);
}
return NULL;
}
gint gtkb_load_pixmap(GtkbPixmap *pixmap, gchar *path, gchar *pixmapname) {
gint gtkb_load_pixmap(GtkbPixmap *pixmap, gchar *path, gchar *pixmapname)
{
gchar *fname;
GError *error = NULL;
gint rv = 1;
if(!(fname = g_strconcat(path, pixmapname, NULL))) {
if (!(fname = g_strconcat(path, pixmapname, NULL))) {
return 0;
}
if(pixmap->pixbuf) g_object_unref(pixmap->pixbuf);
if (pixmap->pixbuf) {
g_object_unref(pixmap->pixbuf);
}
pixmap->pixbuf = gdk_pixbuf_new_from_file(fname, &error);
if(!pixmap->pixbuf) {
if (!pixmap->pixbuf) {
ut_simple_message_box(error->message);
g_error_free(error);
g_error_free (error);
rv = 0;
} else {
pixmap->xsize = gdk_pixbuf_get_width(pixmap->pixbuf);
pixmap->ysize = gdk_pixbuf_get_height(pixmap->pixbuf);
pixmap->xsize = gdk_pixbuf_get_width (pixmap->pixbuf);
pixmap->ysize = gdk_pixbuf_get_height (pixmap->pixbuf);
}
g_free(fname);
g_free (fname);
return rv;
}
void gtkb_pixmap_free(GtkbPixmap pixmap) {
if(pixmap.pixbuf) {
if (pixmap.pixbuf) {
g_object_unref(pixmap.pixbuf);
}
}
void gtkb_theme_free(GtkbTheme *theme) {
void gtkb_theme_free(GtkbTheme *theme)
{
gint i, j;
if(!theme) {
if (!theme) {
return;
}
gtkb_pixmap_free(theme->emptycell);
gtkb_pixmap_free(theme->hemptycell);
for(i = 0; i < 8; i++) {
for (i = 0; i < 8; i++) {
gtkb_pixmap_free(theme->paws[i]);
}
if(theme->balls) {
for(i = 0; i < theme->numballs; i++) {
if (theme->balls) {
for (i = 0; i < theme->numballs; i++) {
gtkb_pixmap_free(theme->balls[i].ball);
gtkb_pixmap_free(theme->balls[i].small);
if(theme->balls[i].jump) {
for(j = 0; j < theme->balls[i].jumpphases; j++) {
if (theme->balls[i].jump) {
for (j = 0; j < theme->balls[i].jumpphases; j++) {
gtkb_pixmap_free(theme->balls[i].jump[j]);
}
g_free(theme->balls[i].jump);
if(theme->balls[i].jumpdelays) {
g_free(theme->balls[i].jumpdelays);
g_free (theme->balls[i].jump);
if (theme->balls[i].jumpdelays) {
g_free (theme->balls[i].jumpdelays);
}
}
if(theme->balls[i].destroy) {
for(j = 0; j < theme->balls[i].destroyphases; j++) {
if (theme->balls[i].destroy) {
for (j = 0; j < theme->balls[i].destroyphases; j++) {
gtkb_pixmap_free(theme->balls[i].destroy[j]);
}
g_free(theme->balls[i].destroy);
if(theme->balls[i].destroydelays) {
g_free(theme->balls[i].destroydelays);
g_free (theme->balls[i].destroy);
if (theme->balls[i].destroydelays) {
g_free (theme->balls[i].destroydelays);
}
}
}
}
g_free(theme->balls);
g_free(theme);
g_free (theme->balls);
g_free (theme);
}
/* warning! tmpname will be free()'d! */
gint gtkb_load_pixmap_from(gchar **trc, gchar *themepath, gchar *tmpname, GtkbPixmap *pixmap) {
gint gtkb_load_pixmap_from(gchar **trc, gchar *themepath, gchar *tmpname, GtkbPixmap *pixmap)
{
gchar *val;
gint ret;
val = trc_get_str(trc, tmpname);
g_free(tmpname);
if(val == NULL) return 0;
g_free (tmpname);
if (val == NULL) {
return 0;
}
ret = gtkb_load_pixmap(pixmap, themepath, val);
g_free(val);
g_free (val);
return ret;
}
#define CHECKRET(ret, cond) \
if(ret == cond) { \
if (ret == cond) { \
gtkb_theme_free(theme); \
trc_close(trc); \
return NULL; \
}
void gtkb_make_hl_pixmap(GtkbTheme *theme) {
if(theme->hemptycell.pixbuf) {
if (theme->hemptycell.pixbuf) {
gtkb_pixmap_free(theme->hemptycell);
}
theme->hemptycell.pixbuf = gdk_pixbuf_copy(theme->emptycell.pixbuf);
@ -172,7 +186,9 @@ void gtkb_make_hl_pixmap(GtkbTheme *theme) {
hilight_pixbuff8(theme->hemptycell.pixbuf, prefs_get_hl_dr(), prefs_get_hl_dg(), prefs_get_hl_db());
}
GtkbTheme *gtkb_load_theme(gchar *themepath) {
GtkbTheme *gtkb_load_theme(gchar *themepath)
{
gchar **trc, *opt;
gchar *paws[] = {"down_up", "left_right", "up_down", "right_left",
"down_right", "down_left", "up_right", "up_left", NULL};
@ -181,8 +197,8 @@ GtkbTheme *gtkb_load_theme(gchar *themepath) {
opt = g_strconcat(themepath, "themerc", NULL);
trc = trc_open(opt); /* open theme description file */
g_free(opt);
if(!trc) {
g_free (opt);
if (!trc) {
return NULL;
}
theme = g_new0(GtkbTheme, 1);
@ -191,7 +207,7 @@ GtkbTheme *gtkb_load_theme(gchar *themepath) {
opt = trc_get_str(trc, "cell");
CHECKRET(opt, NULL);
ret = gtkb_load_pixmap(&theme->emptycell, themepath, opt);
g_free(opt);
g_free (opt);
CHECKRET(ret, 0);
/*
theme->hemptycell.pixbuf = gdk_pixbuf_copy(theme->emptycell.pixbuf);
@ -203,65 +219,65 @@ GtkbTheme *gtkb_load_theme(gchar *themepath) {
gtkb_make_hl_pixmap(theme);
/* find and load "footprints" pixmaps. */
for(i = 0; paws[i]; i++) {
for (i = 0; paws[i]; i++) {
CHECKRET(gtkb_load_pixmap_from(trc, themepath, g_strconcat("paw.", paws[i], NULL),
&theme->paws[i]),
0);
&theme->paws[i]), 0);
}
/* query number of available balls in theme */
theme->numballs = trc_get_uint(trc, "ball.numbers");
CHECKRET(theme->numballs, -1);
if(theme->numballs < rules_get_colors()) CHECKRET(0, 0); /* yes, i know. its ugly =) */
if (theme->numballs < rules_get_colors()) {
CHECKRET(0, 0); /* yes, i know. its ugly =) */
}
theme->balls = g_new0(GtkbBall, theme->numballs);
/* find and load all balls data. */
for(i = 0; i < theme->numballs; i++) {
for (i = 0; i < theme->numballs; i++) {
CHECKRET(gtkb_load_pixmap_from(trc, themepath, g_strdup_printf("ball.%d.still", i + 1),
&theme->balls[i].ball),
0);
&theme->balls[i].ball), 0);
CHECKRET(gtkb_load_pixmap_from(trc, themepath, g_strdup_printf("ball.%d.small", i + 1),
&theme->balls[i].small),
0);
&theme->balls[i].small), 0);
opt = g_strdup_printf("ball.%d.jump.numbers", i + 1);
theme->balls[i].jumpphases = trc_get_uint(trc, opt);
g_free(opt);
g_free (opt);
CHECKRET(theme->balls[i].jumpphases, -1);
if(theme->balls[i].jumpphases < 2) CHECKRET(0, 0); /* yes, i know. its ugly =) */
if (theme->balls[i].jumpphases < 2) {
CHECKRET(0, 0); /* yes, i know. its ugly =) */
}
theme->balls[i].jump = g_new0(GtkbPixmap, theme->balls[i].jumpphases);
theme->balls[i].jumpdelays = g_new0(gint, theme->balls[i].jumpphases);
for(j = 0; j < theme->balls[i].jumpphases; j++) {
for (j = 0; j < theme->balls[i].jumpphases; j++) {
CHECKRET(gtkb_load_pixmap_from(trc, themepath,
g_strdup_printf("ball.%d.jump.%d", i + 1, j + 1),
&theme->balls[i].jump[j]),
0);
&theme->balls[i].jump[j]), 0);
opt = g_strdup_printf("ball.%d.jump.%d.usec", i + 1, j + 1);
theme->balls[i].jumpdelays[j] = trc_get_uint(trc, opt);
g_free(opt);
g_free (opt);
CHECKRET(theme->balls[i].jumpdelays[j], -1);
}
opt = g_strdup_printf("ball.%d.destroy.numbers", i + 1);
theme->balls[i].destroyphases = trc_get_uint(trc, opt);
g_free(opt);
g_free (opt);
CHECKRET(theme->balls[i].destroyphases, -1);
if(theme->balls[i].destroyphases < 2) CHECKRET(0, 0); /* yes, i know. its ugly =) */
if(theme->balls[i].destroyphases > theme->maxdestphases) {
if (theme->balls[i].destroyphases < 2) {
CHECKRET(0, 0); /* yes, i know. its ugly =) */
}
if (theme->balls[i].destroyphases > theme->maxdestphases) {
theme->maxdestphases = theme->balls[i].destroyphases;
}
theme->balls[i].destroy = g_new0(GtkbPixmap, theme->balls[i].destroyphases);
theme->balls[i].destroydelays = g_new0(gint, theme->balls[i].destroyphases);
for(j = 0; j < theme->balls[i].destroyphases; j++) {
for (j = 0; j < theme->balls[i].destroyphases; j++) {
CHECKRET(gtkb_load_pixmap_from(trc, themepath,
g_strdup_printf("ball.%d.destroy.%d", i + 1, j + 1),
&theme->balls[i].destroy[j]),
0);
&theme->balls[i].destroy[j]), 0);
opt = g_strdup_printf("ball.%d.destroy.%d.usec", i + 1, j + 1);
theme->balls[i].destroydelays[j] = trc_get_uint(trc, opt);
g_free(opt);
g_free (opt);
CHECKRET(theme->balls[i].destroydelays[j], -1);
}
}
@ -270,16 +286,22 @@ GtkbTheme *gtkb_load_theme(gchar *themepath) {
return theme;
}
gint load_theme(gchar *themename) {
gint load_theme(gchar *themename)
{
gchar *themepath;
GtkbTheme *theme;
if(!(themepath = find_theme_path(themename))) return 0;
if (!(themepath = find_theme_path(themename))) {
return 0;
}
theme = gtkb_load_theme(themepath);
g_free(themepath);
g_free (themepath);
if(!theme) return 0;
if (!theme) {
return 0;
}
gtkb_theme_free(gtkbTheme);
gtkbTheme = theme;
@ -287,26 +309,34 @@ gint load_theme(gchar *themename) {
return 1;
}
gint gtkb_theme_free_handler(GtkWidget *widget, gpointer data) {
gint gtkb_theme_free_handler(GtkWidget *widget, gpointer data)
{
gtkb_theme_free(gtkbTheme);
gtkbTheme = NULL;
return 0;
}
gint gtkb_theme_get_balls_num(void) {
gint gtkb_theme_get_balls_num(void)
{
return gtkbTheme ? gtkbTheme->numballs : 0;
}
/* returns board coordinate of the pointer */
gint gtkb_theme_get_coord_at_x(gint x) {
if(gtkbTheme) {
gint gtkb_theme_get_coord_at_x(gint x)
{
if (gtkbTheme) {
return (x - 1) / gtkbTheme->emptycell.xsize;
}
return -1;
}
gint gtkb_theme_get_coord_at_y(gint y) {
if(gtkbTheme) {
gint gtkb_theme_get_coord_at_y(gint y)
{
if (gtkbTheme) {
return (y - 1) / gtkbTheme->emptycell.ysize;
}
return -1;
@ -320,8 +350,10 @@ gint theme_get_height(void) {
return gtkbTheme->emptycell.xsize;
}
/* find all available themes. */
gchar **get_available_themes(void) {
gchar **get_available_themes(void)
{
DIR *directory;
struct dirent *dir_entry;
struct stat entry_stat;
@ -329,43 +361,47 @@ gchar **get_available_themes(void) {
gint i, j, num, flag;
gchar **tlist = NULL;
if(getenv("HOME")) {
if (getenv("HOME")) {
hdir = g_strconcat(getenv("HOME"), THEMEPREFIX, NULL);
} else {
hdir = g_strdup("./"); /* FIXME: does it work on non unix os? */
}
for(j = 0, currdir = INSTALLPATH, num = 0; j < 2; j++, currdir = hdir) {
if(!(directory = opendir(currdir))) {
for (j = 0, currdir = INSTALLPATH, num = 0; j < 2; j++, currdir = hdir)
{
if (!(directory = opendir(currdir))) {
continue;
}
while((dir_entry = readdir(directory))) {
if(!strncmp(dir_entry->d_name, ".", 2) ||
while((dir_entry = readdir(directory)))
{
if (!strncmp(dir_entry->d_name, ".", 2) ||
!strncmp(dir_entry->d_name, "..", 3)) {
continue;
}
entry = g_strconcat(currdir, dir_entry->d_name, NULL);
if(!stat(entry, &entry_stat) && S_ISDIR(entry_stat.st_mode)) {
if (!stat(entry, &entry_stat) && S_ISDIR(entry_stat.st_mode)) {
rcentry = g_strconcat(entry, G_DIR_SEPARATOR_S, "themerc", NULL);
if(!stat(rcentry, &entry_stat)) {
if (!stat(rcentry, &entry_stat)) {
flag = 0;
for(i=0; i < num && !flag; i++) {
if(!strcmp(tlist[i], dir_entry->d_name)) {
for (i=0; i < num && !flag; i++) {
if (!strcmp(tlist[i], dir_entry->d_name)) {
flag++;
}
}
if(!flag) {
if (!flag) {
num++;
tlist = g_realloc(tlist, num * sizeof(gchar *));
tlist[num-1] = g_strdup(dir_entry->d_name);
}
}
g_free(rcentry);
g_free (rcentry);
}
g_free(entry);
g_free (entry);
}
closedir(directory);
}
g_free(hdir);
g_free (hdir);
tlist = g_realloc(tlist, (num + 1) * sizeof(gchar *));
tlist[num] = NULL;
return tlist;

View File

@ -18,35 +18,36 @@
#include <errno.h>
/* read contents of fname into gchar * */
gchar **trc_open(gchar *fname) {
gchar **trc_open(gchar *fname)
{
gint fd;
struct stat fds;
ssize_t rb;
gchar *rc,**rcs;
gchar *rc, **rcs;
if((fd=open(fname, O_RDONLY))==-1) {
if ((fd=open(fname, O_RDONLY)) == -1) {
fprintf(stderr, "%s: open() %s failed: %s\n", __FUNCTION__, fname, strerror(errno));
return NULL;
}
if((fstat(fd, &fds))==-1) {
if ((fstat(fd, &fds)) == -1) {
close(fd);
fprintf(stderr, "%s: fstat() failed: %s\n", __FUNCTION__, strerror(errno));
return NULL;
}
if(!(fds.st_size)) {
if (!(fds.st_size)) {
close(fd);
fprintf(stderr, "%s: zero length file.\n", __FUNCTION__);
return NULL;
}
if(!(rc=malloc(fds.st_size+1))) {
if (!(rc=malloc(fds.st_size+1))) {
close(fd);
fprintf(stderr, "%s: malloc() failed: cannot alloc %d bytes\n", __FUNCTION__, (int)fds.st_size);
return NULL;
}
if((rb=read(fd, rc, fds.st_size))!=fds.st_size) {
if ((rb=read(fd, rc, fds.st_size)) != fds.st_size) {
free(rc);
close(fd);
if(rb==-1) {
if (rb == -1) {
fprintf(stderr, "%s: read() failed: %s\n", __FUNCTION__, strerror(errno));
} else {
fprintf(stderr, "%s: read() reads less bytes than expected =/\n", __FUNCTION__);
@ -62,27 +63,28 @@ gchar **trc_open(gchar *fname) {
/* free rc... */
void trc_close(gchar **rcs) {
if(rcs) {
if (rcs) {
g_strfreev(rcs);
}
}
/* return string value for given parameter. if not found retun NULL */
gchar *trc_get_str(gchar **rcs, gchar *param) {
gchar *trc_get_str(gchar **rcs, gchar *param)
{
gint i;
gchar **strval,*val;
if(!rcs) {
if (!rcs) {
fprintf(stderr, "%s called with uninitialised rcs. strange. \n", __FUNCTION__);
return NULL;
}
if(!param) {
if (!param) {
fprintf(stderr, "%s called with NULL param. strange. \n", __FUNCTION__);
return NULL;
}
for(i=0;rcs[i];i++) {
if(rcs[i][0] && rcs[i][0]!='#' && (strval=g_strsplit(rcs[i],"=",2)) && strval[0] && strval[1]) {
if(!(strcmp(g_strstrip(strval[0]),param))) {
for (i=0;rcs[i];i++) {
if (rcs[i][0] && rcs[i][0]!='#' && (strval=g_strsplit(rcs[i],"=",2)) && strval[0] && strval[1]) {
if (!(strcmp(g_strstrip(strval[0]),param))) {
val=g_strdup(g_strstrip(strval[1]));
g_strfreev(strval);
return val;
@ -98,8 +100,10 @@ gint trc_get_uint(gchar **rcs, gchar *param) {
gchar *val;
gint ret;
if(!(val=trc_get_str(rcs, param))) return -1;
ret=atoi(val);
g_free(val);
if (!(val=trc_get_str(rcs, param))) {
return -1;
}
ret = atoi(val);
g_free (val);
return ret;
}