Fix Sudoku's use of Fl_Sys_Menu->parent().

This commit is contained in:
Matthias Melcher 2024-08-04 16:46:04 +02:00
parent 72ee34d1cb
commit cee2af13b3

View File

@ -195,6 +195,7 @@ class Sudoku : public Fl_Double_Window {
void update_helpers(); void update_helpers();
}; };
Sudoku *sudoku = NULL;
// Sound class globals... // Sound class globals...
int SudokuSound::frequencies[9] = { int SudokuSound::frequencies[9] = {
@ -558,7 +559,7 @@ SudokuCell::handle(int event) {
if (value()) { if (value()) {
if (value() < 9) value(value() + 1); if (value() < 9) value(value() + 1);
else value(1); else value(1);
} else value(((Sudoku *)window())->next_value(this)); } else value(sudoku->next_value(this));
} }
Fl::focus(this); Fl::focus(this);
@ -745,7 +746,7 @@ Sudoku::~Sudoku() {
// Check for a solution to the game... // Check for a solution to the game...
void void
Sudoku::check_cb(Fl_Widget *widget, void *) { Sudoku::check_cb(Fl_Widget *widget, void *) {
((Sudoku *)(widget->window()))->check_game(); sudoku->check_game();
} }
@ -834,10 +835,8 @@ Sudoku::check_game(bool highlight) {
// Close the window, saving the game first... // Close the window, saving the game first...
void void
Sudoku::close_cb(Fl_Widget *widget, void *) { Sudoku::close_cb(Fl_Widget *widget, void *) {
Sudoku *s = (Sudoku *)(widget->window() ? widget->window() : widget); sudoku->save_game();
sudoku->hide();
s->save_game();
s->hide();
if (help_dialog_) help_dialog_->hide(); if (help_dialog_) help_dialog_->hide();
} }
@ -846,13 +845,12 @@ Sudoku::close_cb(Fl_Widget *widget, void *) {
// Set the level of difficulty... // Set the level of difficulty...
void void
Sudoku::diff_cb(Fl_Widget *widget, void *d) { Sudoku::diff_cb(Fl_Widget *widget, void *d) {
Sudoku *s = (Sudoku *)(widget->window() ? widget->window() : widget);
int diff = atoi((char *)d); int diff = atoi((char *)d);
if (diff != s->difficulty_) { if (diff != sudoku->difficulty_) {
s->difficulty_ = diff; sudoku->difficulty_ = diff;
s->new_game(s->seed_); sudoku->new_game(sudoku->seed_);
s->set_title(); sudoku->set_title();
if (diff > 1) if (diff > 1)
{ {
@ -871,15 +869,14 @@ Sudoku::diff_cb(Fl_Widget *widget, void *d) {
} }
} }
prefs_.set("difficulty", s->difficulty_); prefs_.set("difficulty", sudoku->difficulty_);
} }
} }
// Update the little marker numbers in all cells // Update the little marker numbers in all cells
void void
Sudoku::update_helpers_cb(Fl_Widget *widget, void *) { Sudoku::update_helpers_cb(Fl_Widget *widget, void *) {
Sudoku *s = (Sudoku *)(widget->window() ? widget->window() : widget); sudoku->update_helpers();
s->update_helpers();
} }
void void
@ -1048,14 +1045,12 @@ Sudoku::load_game() {
// Mute/unmute sound... // Mute/unmute sound...
void void
Sudoku::mute_cb(Fl_Widget *widget, void *) { Sudoku::mute_cb(Fl_Widget *widget, void *) {
Sudoku *s = (Sudoku *)(widget->window() ? widget->window() : widget); if (sudoku->sound_) {
delete sudoku->sound_;
if (s->sound_) { sudoku->sound_ = NULL;
delete s->sound_;
s->sound_ = NULL;
prefs_.set("mute_sound", 1); prefs_.set("mute_sound", 1);
} else { } else {
s->sound_ = new SudokuSound(); sudoku->sound_ = new SudokuSound();
prefs_.set("mute_sound", 0); prefs_.set("mute_sound", 0);
} }
} }
@ -1064,15 +1059,12 @@ Sudoku::mute_cb(Fl_Widget *widget, void *) {
// Create a new game... // Create a new game...
void void
Sudoku::new_cb(Fl_Widget *widget, void *) { Sudoku::new_cb(Fl_Widget *widget, void *) {
Sudoku *s = (Sudoku *)(widget->window() ? widget->window() : widget); if (sudoku->grid_cells_[0][0]->color() != FL_GREEN) {
if (s->grid_cells_[0][0]->color() != FL_GREEN) {
if (!fl_choice("Are you sure you want to change the difficulty level and " if (!fl_choice("Are you sure you want to change the difficulty level and "
"discard the current game?", "Keep Current Game", "Start New Game", "discard the current game?", "Keep Current Game", "Start New Game",
NULL)) return; NULL)) return;
} }
sudoku->new_game(time(NULL));
s->new_game(time(NULL));
} }
@ -1234,23 +1226,22 @@ Sudoku::resize(int X, int Y, int W, int H) {
// Restart game from beginning... // Restart game from beginning...
void void
Sudoku::restart_cb(Fl_Widget *widget, void *) { Sudoku::restart_cb(Fl_Widget *widget, void *) {
Sudoku *s = (Sudoku *)(widget->window());
bool solved = true; bool solved = true;
for (int j = 0; j < 9; j ++) for (int j = 0; j < 9; j ++)
for (int k = 0; k < 9; k ++) { for (int k = 0; k < 9; k ++) {
SudokuCell *cell = s->grid_cells_[j][k]; SudokuCell *cell = sudoku->grid_cells_[j][k];
if (!cell->readonly()) { if (!cell->readonly()) {
solved = false; solved = false;
int v = cell->value(); int v = cell->value();
cell->value(0); cell->value(0);
cell->color(FL_LIGHT3); cell->color(FL_LIGHT3);
if (v && s->sound_) s->sound_->play('A' + v - 1); if (v && sudoku->sound_) sudoku->sound_->play('A' + v - 1);
} }
} }
if (solved) s->new_game(s->seed_); if (solved) sudoku->new_game(sudoku->seed_);
} }
@ -1297,7 +1288,7 @@ Sudoku::set_title() {
// Solve the puzzle... // Solve the puzzle...
void void
Sudoku::solve_cb(Fl_Widget *widget, void *) { Sudoku::solve_cb(Fl_Widget *widget, void *) {
((Sudoku *)(widget->window()))->solve_game(); sudoku->solve_game();
} }
@ -1324,6 +1315,7 @@ Sudoku::solve_game() {
int int
main(int argc, char *argv[]) { main(int argc, char *argv[]) {
Sudoku s; Sudoku s;
sudoku = &s;
// Show the game... // Show the game...
s.show(argc, argv); s.show(argc, argv);