initial commit, lordsawar source, slightly modified
[lordsawar] / src / gui / main-preferences-dialog.cpp
1 //  Copyright (C) 2008, 2009 Ben Asselstine
2 //
3 //  This program is free software; you can redistribute it and/or modify
4 //  it under the terms of the GNU General Public License as published by
5 //  the Free Software Foundation; either version 3 of the License, or
6 //  (at your option) any later version.
7 //
8 //  This program is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 //  GNU Library General Public License for more details.
12 //
13 //  You should have received a copy of the GNU General Public License
14 //  along with this program; if not, write to the Free Software
15 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
16 //  02110-1301, USA.
17
18 #include <config.h>
19
20 #include <gtkmm.h>
21 #include <sigc++/functors/mem_fun.h>
22
23 #include "main-preferences-dialog.h"
24
25 #include "glade-helpers.h"
26 #include "image-helpers.h"
27 #include "ucompose.hpp"
28 #include "defs.h"
29 #include "Configuration.h"
30 #include "sound.h"
31
32
33 MainPreferencesDialog::MainPreferencesDialog()
34 {
35     Glib::RefPtr<Gtk::Builder> xml
36         = Gtk::Builder::create_from_file(get_glade_path()
37                                     + "/main-preferences-dialog.ui");
38
39     xml->get_widget("dialog", dialog);
40     decorate(dialog);
41     window_closed.connect(sigc::mem_fun(dialog, &Gtk::Dialog::hide));
42
43     xml->get_widget("show_turn_popup_checkbutton", show_turn_popup_checkbutton);
44     xml->get_widget("commentator_checkbutton", commentator_checkbutton);
45     xml->get_widget("show_decorated_windows_checkbutton", show_decorated_windows_checkbutton);
46     xml->get_widget("play_music_checkbutton", play_music_checkbutton);
47     xml->get_widget("music_volume_scale", music_volume_scale);
48     xml->get_widget("music_volume_hbox", music_volume_hbox);
49     show_turn_popup_checkbutton->signal_toggled().connect(
50         sigc::mem_fun(this, &MainPreferencesDialog::on_show_turn_popup_toggled));
51     commentator_checkbutton->signal_toggled().connect(
52         sigc::mem_fun(this, &MainPreferencesDialog::on_show_commentator_toggled));
53     show_decorated_windows_checkbutton->signal_toggled().connect(
54         sigc::mem_fun(this, &MainPreferencesDialog::on_show_decorated_windows_toggled));
55     play_music_checkbutton->signal_toggled().connect(
56         sigc::mem_fun(this, &MainPreferencesDialog::on_play_music_toggled));
57     music_volume_scale->signal_value_changed().connect(
58         sigc::mem_fun(this, &MainPreferencesDialog::on_music_volume_changed));
59
60     show_turn_popup_checkbutton->set_active(Configuration::s_showNextPlayer);
61     commentator_checkbutton->set_active(Configuration::s_displayCommentator);
62     show_decorated_windows_checkbutton->set_active(!Configuration::s_decorated);
63     play_music_checkbutton->set_active(Configuration::s_musicenable);
64     music_volume_hbox->set_sensitive(Configuration::s_musicenable);
65     music_volume_scale->set_value(Configuration::s_musicvolume * 100.0 / 128);
66     
67 }
68
69 MainPreferencesDialog::~MainPreferencesDialog()
70 {
71   delete dialog;
72 }
73 void MainPreferencesDialog::set_parent_window(Gtk::Window &parent)
74 {
75     dialog->set_transient_for(parent);
76     //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
77 }
78
79 void MainPreferencesDialog::hide()
80 {
81   dialog->hide();
82 }
83
84 void MainPreferencesDialog::run()
85 {
86     dialog->show();
87     dialog->run();
88     
89     Configuration::saveConfigurationFile(Configuration::configuration_file_path);
90     dialog->hide();
91 }
92
93 void MainPreferencesDialog::on_show_turn_popup_toggled()
94 {
95     Configuration::s_showNextPlayer = show_turn_popup_checkbutton->get_active();
96 }
97
98 void MainPreferencesDialog::on_show_decorated_windows_toggled()
99 {
100     Configuration::s_decorated = !show_decorated_windows_checkbutton->get_active();
101 }
102
103 void MainPreferencesDialog::on_play_music_toggled()
104 {
105     bool play_music = play_music_checkbutton->get_active();
106
107     Configuration::s_musicenable = play_music;
108
109     if (play_music)
110     {
111         Sound::getInstance()->enableBackground();
112     }
113     else
114     {
115         Sound::getInstance()->haltMusic();
116         Sound::getInstance()->disableBackground();
117     }
118     music_volume_hbox->set_sensitive(Configuration::s_musicenable);
119 }
120
121 void MainPreferencesDialog::on_music_volume_changed()
122 {
123     int volume = int(music_volume_scale->get_value() / 100 * 128);
124     
125 #ifdef FL_SOUND
126     Mix_VolumeMusic(volume);
127 #endif
128
129     Configuration::s_musicvolume = volume;
130 }
131
132 void MainPreferencesDialog::on_show_commentator_toggled()
133 {
134   Configuration::s_displayCommentator = commentator_checkbutton->get_active();
135 }
136