1#!/usr/bin/env python
2# -*- mode: python; coding: utf-8; -*-
3# ---------------------------------------------------------------------------
4#
5# Copyright (C) 1998-2003 Markus Franz Xaver Johannes Oberhumer
6# Copyright (C) 2003 Mt. Hood Playing Card Co.
7# Copyright (C) 2005-2009 Skomoroh
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21#
22# ---------------------------------------------------------------------------
23
24import os
25
26from pysollib.mfxutil import KwStruct
27from pysollib.mygettext import _
28from pysollib.pysolaudio import pysolsoundserver
29from pysollib.settings import TITLE
30from pysollib.ui.tktile.tkconst import EVENT_HANDLED
31
32from six.moves import tkinter
33from six.moves import tkinter_ttk as ttk
34
35from .tkwidget import MfxDialog, MfxMessageDialog
36from .tkwidget import PysolScale
37
38
39class SoundOptionsDialog(MfxDialog):
40
41    def __init__(self, parent, title, app, **kw):
42        self.app = app
43        kw = self.initKw(kw)
44        MfxDialog.__init__(self, parent, title, kw.resizable, kw.default)
45        top_frame, bottom_frame = self.createFrames(kw)
46        self.createBitmaps(top_frame, kw)
47        #
48        self.saved_opt = app.opt.copy()
49        self.sound = tkinter.BooleanVar()
50        self.sound.set(app.opt.sound != 0)
51        self.sound_mode = tkinter.BooleanVar()
52        self.sound_mode.set(app.opt.sound_mode != 0)
53        self.sample_volume = tkinter.IntVar()
54        self.sample_volume.set(app.opt.sound_sample_volume)
55        self.music = tkinter.BooleanVar()
56        self.music.set(app.opt.music != 0)
57        self.music_volume = tkinter.IntVar()
58        self.music_volume.set(app.opt.sound_music_volume)
59        self.samples = [
60            ('areyousure',    _('Are You Sure'),   tkinter.BooleanVar()),
61
62            ('deal',          _('Deal'),           tkinter.BooleanVar()),
63            ('dealwaste',     _('Deal waste'),     tkinter.BooleanVar()),
64
65            ('turnwaste',     _('Turn waste'),     tkinter.BooleanVar()),
66            ('startdrag',     _('Start drag'),     tkinter.BooleanVar()),
67
68            ('drop',          _('Drop'),           tkinter.BooleanVar()),
69            ('droppair',      _('Drop pair'),      tkinter.BooleanVar()),
70            ('autodrop',      _('Auto drop'),      tkinter.BooleanVar()),
71
72            ('flip',          _('Flip'),           tkinter.BooleanVar()),
73            ('autoflip',      _('Auto flip'),      tkinter.BooleanVar()),
74            ('move',          _('Move'),           tkinter.BooleanVar()),
75            ('nomove',        _('No move'),        tkinter.BooleanVar()),
76
77            ('undo',          _('Undo'),           tkinter.BooleanVar()),
78            ('redo',          _('Redo'),           tkinter.BooleanVar()),
79
80            ('autopilotlost', _('Autopilot lost'), tkinter.BooleanVar()),
81            ('autopilotwon',  _('Autopilot won'),  tkinter.BooleanVar()),
82
83            ('gamefinished',  _('Game finished'),  tkinter.BooleanVar()),
84            ('gamelost',      _('Game lost'),      tkinter.BooleanVar()),
85            ('gamewon',       _('Game won'),       tkinter.BooleanVar()),
86            ('gameperfect',   _('Perfect game'),   tkinter.BooleanVar()),
87
88            ('extra',         _('Other'),          tkinter.BooleanVar()),
89            ]
90
91        #
92        frame = ttk.Frame(top_frame)
93        frame.pack(expand=True, fill='both', padx=5, pady=5)
94        frame.columnconfigure(1, weight=1)
95        #
96        row = 0
97        w = ttk.Checkbutton(frame, variable=self.sound,
98                            text=_("Sound enabled"))
99        w.grid(row=row, column=0, columnspan=2, sticky='ew')
100        #
101        if os.name == "nt" and pysolsoundserver:
102            row += 1
103            w = ttk.Checkbutton(frame, variable=self.sound_mode,
104                                text=_("Use DirectX for sound playing"),
105                                command=self.mOptSoundDirectX)
106            w.grid(row=row, column=0, columnspan=2, sticky='ew')
107        #
108        if app.audio.CAN_PLAY_MUSIC:  # and app.startup_opt.sound_mode > 0:
109            row += 1
110            ttk.Label(frame, text=_('Sample volume:'), anchor='w'
111                      ).grid(row=row, column=0, sticky='ew')
112            w = PysolScale(frame, from_=0, to=128, resolution=1,
113                           orient='horizontal', takefocus=0,
114                           length="3i",  # label=_('Sample volume'),
115                           variable=self.sample_volume)
116            w.grid(row=row, column=1, sticky='w', padx=5)
117            row += 1
118            w = ttk.Checkbutton(frame, variable=self.music,
119                                text=_("Music enabled"))
120            w.grid(row=row, column=0, columnspan=2, sticky='ew')
121
122            row += 1
123            ttk.Label(frame, text=_('Music volume:'), anchor='w'
124                      ).grid(row=row, column=0, sticky='ew')
125            w = PysolScale(frame, from_=0, to=128, resolution=1,
126                           orient='horizontal', takefocus=0,
127                           length="3i",  # label=_('Music volume'),
128                           variable=self.music_volume)
129            w.grid(row=row, column=1, sticky='w', padx=5)
130
131        else:
132            # remove "Apply" button
133            kw.strings[1] = None
134        #
135        frame = ttk.LabelFrame(top_frame, text=_('Enable samples'))
136        frame.pack(expand=True, fill='both', padx=5, pady=5)
137        frame.columnconfigure(0, weight=1)
138        frame.columnconfigure(1, weight=1)
139        #
140        row = 0
141        col = 0
142        for n, t, v in self.samples:
143            v.set(app.opt.sound_samples[n])
144            w = ttk.Checkbutton(frame, text=t, variable=v)
145            w.grid(row=row, column=col, sticky='ew', padx=3, pady=1)
146            if col == 1:
147                col = 0
148                row += 1
149            else:
150                col = 1
151        #
152        top_frame.columnconfigure(1, weight=1)
153        #
154        focus = self.createButtons(bottom_frame, kw)
155        self.mainloop(focus, kw.timeout)
156
157    def initKw(self, kw):
158        strings = [_("&OK"), _("&Apply"), _("&Cancel"), ]
159        kw = KwStruct(kw,
160                      strings=strings,
161                      default=0,
162                      )
163        return MfxDialog.initKw(self, kw)
164
165    def mDone(self, button):
166        if button == 0 or button == 1:
167            self.app.opt.sound = self.sound.get()
168            self.app.opt.sound_mode = int(self.sound_mode.get())
169            self.app.opt.sound_sample_volume = self.sample_volume.get()
170            self.app.opt.sound_music_volume = self.music_volume.get()
171            self.app.opt.music = self.music.get()
172            for n, t, v in self.samples:
173                self.app.opt.sound_samples[n] = v.get()
174        elif button == 2:
175            self.app.opt = self.saved_opt
176        if self.app.audio:
177            self.app.audio.updateSettings()
178            if button == 1:
179                self.app.audio.playSample("drop", priority=1000)
180        if button == 1:
181            return EVENT_HANDLED
182        return MfxDialog.mDone(self, button)
183
184    def mCancel(self, *event):
185        return self.mDone(2)
186
187    def wmDeleteWindow(self, *event):
188        return self.mDone(0)
189
190    def mOptSoundDirectX(self, *event):
191        # print self.sound_mode.get()
192        MfxMessageDialog(
193            self.top, title=_("Sound preferences info"),
194            text=_("""\
195Changing DirectX settings will take effect
196the next time you restart """)+TITLE,
197            bitmap="warning",
198            default=0, strings=(_("&OK"),))
199