1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
|
#!/usr/bin/env python
# -*- mode: python; coding: utf-8; -*-
# ---------------------------------------------------------------------------##
#
# Copyright (C) 1998-2003 Markus Franz Xaver Johannes Oberhumer
# Copyright (C) 2003 Mt. Hood Playing Card Co.
# Copyright (C) 2005-2009 Skomoroh
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ---------------------------------------------------------------------------##
import os
import tkinter
from pysollib.mfxutil import Image, ImageTk
from pysollib.mygettext import _, n_
from pysollib.settings import TITLE
from pysollib.ui.tktile.menubar import MfxMenu, createToolbarMenu
from pysollib.ui.tktile.tkconst import EVENT_HANDLED
from pysollib.util import IMAGE_EXTENSIONS
from pysollib.winsystems import TkSettings
from .tkwidget import MfxTooltip
class AbstractToolbarButton:
def __init__(self, parent, toolbar, toolbar_name, position):
self.toolbar = toolbar
self.toolbar_name = toolbar_name
self.position = position
self.visible = False
def show(self, orient, force=False):
if self.visible and not force:
return
self.visible = True
padx, pady = 2, 2
if orient == 'horizontal':
self.grid(row=0,
column=self.position,
ipadx=padx, ipady=pady,
sticky='nsew')
else:
self.grid(row=self.position,
column=0,
ipadx=padx, ipady=pady,
sticky='nsew')
def hide(self):
if not self.visible:
return
self.visible = False
self.grid_forget()
class ToolbarCheckbutton(AbstractToolbarButton, tkinter.Checkbutton):
def __init__(self, parent, toolbar, toolbar_name, position, **kwargs):
tkinter.Checkbutton.__init__(self, parent, kwargs)
AbstractToolbarButton.__init__(
self, parent, toolbar, toolbar_name, position)
class ToolbarButton(AbstractToolbarButton, tkinter.Button):
def __init__(self, parent, toolbar, toolbar_name, position, **kwargs):
tkinter.Button.__init__(self, parent, kwargs)
AbstractToolbarButton.__init__(
self, parent, toolbar, toolbar_name, position)
class ToolbarSeparator(tkinter.Frame):
def __init__(self, parent, toolbar, position, **kwargs):
tkinter.Frame.__init__(self, parent, kwargs)
self.toolbar = toolbar
self.position = position
self.visible = False
def show(self, orient, force=False):
if self.visible and not force:
return
self.visible = True
width = 4
height = 4
padx = 6
pady = 6
if orient == 'horizontal':
self.config(width=width, height=height)
self.grid(row=0,
column=self.position,
padx=padx, pady=pady,
sticky='ns')
else:
self.config(width=height, height=width)
self.grid(row=self.position,
column=0,
padx=pady, pady=padx,
sticky='ew')
def hide(self):
if not self.visible:
return
self.visible = False
self.grid_forget()
class ToolbarFlatSeparator(ToolbarSeparator):
pass
class ToolbarLabel(tkinter.Message):
def __init__(self, parent, toolbar, toolbar_name, position, **kwargs):
tkinter.Message.__init__(self, parent, **kwargs)
self.toolbar = toolbar
self.toolbar_name = toolbar_name
self.position = position
self.visible = False
def show(self, orient, force=False):
if self.visible and not force:
return
self.visible = True
padx, pady = TkSettings.toolbar_label_padding
if orient == 'horizontal':
self.grid(row=0,
column=self.position,
padx=padx, pady=pady,
sticky='nsew')
else:
self.grid(row=self.position,
column=0,
padx=padx, pady=pady,
sticky='nsew')
def hide(self):
if not self.visible:
return
self.visible = False
self.grid_forget()
# ************************************************************************
# * Note: Applications should call show/hide after constructor.
# ************************************************************************
class PysolToolbarTk:
def __init__(self, top, menubar, dir,
size=0, relief='flat', compound='none'):
self.top = top
self.menubar = menubar
self.side = -1
self._tooltips = []
self._widgets = []
self.dir = dir
self.size = size
self.compound = compound
self.orient = 'horizontal'
self.button_pad = 2
#
self.frame = tkinter.Frame(top, relief=TkSettings.toolbar_relief,
bd=TkSettings.toolbar_borderwidth)
from pysollib.options import calcCustomMouseButtonsBinding
def _bind2sep(sep):
sep.bind(
calcCustomMouseButtonsBinding("<{mouse_button1}>"),
self.clickHandler)
sep.bind(
calcCustomMouseButtonsBinding("<{mouse_button3}>"),
self.rightclickHandler)
for label, f, t in (
(n_("New"), self.mNewGame, _("New game")),
(n_("Restart"), self.mRestart, _("Restart the\ncurrent game")),
(None, None, None),
(n_("Open"), self.mOpen, _("Open a\nsaved game")),
(n_("Save"), self.mSave, _("Save game")),
(None, None, None),
(n_("Undo"), self.mUndo, _("Undo last move")),
(n_("Redo"), self.mRedo, _("Redo last move")),
(n_("Autodrop"), self.mDrop, _("Auto drop cards")),
(n_("Shuffle"), self.mShuffle, _("Shuffle tiles")),
(n_("Hint"), self.mHint, _("Hint")),
(n_("Pause"), self.mPause, _("Pause game")),
(None, None, None),
(n_("Statistics"), self.mPlayerStats, _("View statistics")),
(n_("Rules"), self.mHelpRules, _("Rules for this game")),
(None, None, None),
(n_("Quit"), self.mQuit, _("Quit %s") % TITLE),
):
if label is None:
sep = self._createSeparator()
_bind2sep(sep)
elif label == 'Pause':
self._createButton(label, f, check=True, tooltip=t)
else:
self._createButton(label, f, tooltip=t)
self.pause_button.config(variable=menubar.tkopt.pause)
sep = self._createFlatSeparator()
_bind2sep(sep)
self._createLabel("player", label=n_('Player'),
tooltip=_("Player options"))
#
self.player_label.bind(
calcCustomMouseButtonsBinding("<{mouse_button1}>"),
self.mOptPlayerOptions)
# self.player_label.bind("<3>",self.mOptPlayerOptions)
self.popup = MfxMenu(master=None, label=n_('Toolbar'), tearoff=0)
createToolbarMenu(menubar, self.popup)
_bind2sep(self.frame)
#
self.setCompound(compound, force=True)
def config(self, w, v):
if w == 'player':
# label
if v:
self.player_label.show(orient=self.orient)
else:
self.player_label.hide()
else:
# button
widget = getattr(self, w+'_button')
if v:
widget.show(orient=self.orient)
else:
widget.hide()
#
prev_visible = None
last_visible = None
for w in self._widgets:
if isinstance(w, ToolbarSeparator):
if prev_visible is None or isinstance(prev_visible,
ToolbarSeparator):
w.hide()
else:
w.show(orient=self.orient)
elif isinstance(w, ToolbarFlatSeparator):
if isinstance(prev_visible, ToolbarSeparator):
prev_visible.hide()
if w.visible:
prev_visible = w
if not isinstance(w, ToolbarLabel):
last_visible = w
if isinstance(last_visible, ToolbarSeparator):
last_visible.hide()
# util
def _loadImage(self, name):
file = os.path.join(self.dir, name)
image = None
for ext in IMAGE_EXTENSIONS:
file = os.path.join(self.dir, name+ext)
if os.path.isfile(file):
if Image:
image = ImageTk.PhotoImage(Image.open(file))
else:
image = tkinter.PhotoImage(file=file)
break
return image
def _createSeparator(self):
position = len(self._widgets)
sep = ToolbarSeparator(self.frame,
position=position,
toolbar=self,
bd=1,
highlightthickness=1,
width=4,
takefocus=0,
relief=TkSettings.toolbar_separator_relief)
sep.show(orient=self.orient)
self._widgets.append(sep)
return sep
def _createFlatSeparator(self):
position = len(self._widgets)
sep = ToolbarFlatSeparator(self.frame,
position=position,
toolbar=self,
bd=1,
highlightthickness=1,
width=5,
takefocus=0,
relief='flat')
sep.show(orient=self.orient)
self.frame.rowconfigure(position, weight=1)
self.frame.columnconfigure(position, weight=1)
self._widgets.append(sep)
return sep
def _createButton(self, label, command, check=False, tooltip=None):
name = label.lower()
image = self._loadImage(name)
position = len(self._widgets)
button_relief = TkSettings.toolbar_button_relief
bd = TkSettings.toolbar_button_borderwidth
padx, pady = TkSettings.toolbar_button_padding
kw = {
'position': position,
'toolbar': self,
'toolbar_name': name,
'command': command,
'takefocus': 0,
'text': _(label),
'bd': bd,
'relief': button_relief,
'padx': padx,
'pady': pady,
'overrelief': 'raised',
}
if image:
kw['image'] = image
if check:
kw['offrelief'] = button_relief
kw['indicatoron'] = False
kw['selectcolor'] = ''
button = ToolbarCheckbutton(self.frame, **kw)
else:
button = ToolbarButton(self.frame, **kw)
button.show(orient=self.orient)
setattr(self, name + "_image", image)
setattr(self, name + "_button", button)
self._widgets.append(button)
if tooltip:
b = MfxTooltip(button)
self._tooltips.append(b)
b.setText(tooltip)
return button
def _createLabel(self, name, label=None, tooltip=None):
aspect = (400, 300)[self.getSize() != 0]
position = len(self._widgets)
label = ToolbarLabel(self.frame,
position=position,
toolbar=self,
toolbar_name=name,
relief="ridge",
justify="center",
aspect=aspect)
label.show(orient=self.orient)
setattr(self, name + "_label", label)
self._widgets.append(label)
if tooltip:
b = MfxTooltip(label)
self._tooltips.append(b)
b.setText(tooltip)
return label
def _busy(self):
if not self.side or not self.game or not self.menubar:
return 1
self.game.stopDemo()
self.game.interruptSleep()
return self.game.busy
#
# public methods
#
def show(self, side=1, resize=1):
if self.side == side:
return 0
if resize:
self.top.wm_geometry("") # cancel user-specified geometry
if not side:
# hide
self.frame.grid_forget()
else:
# show
pack_func = self.frame.grid_configure
if side == 1:
# top
pack_func(row=0, column=1, sticky='ew')
elif side == 2:
# bottom
pack_func(row=2, column=1, sticky='ew')
elif side == 3:
# left
pack_func(row=1, column=0, sticky='ns')
else:
# right
pack_func(row=1, column=2, sticky='ns')
# set orient
orient = side in (1, 2) and 'horizontal' or 'vertical'
self._setOrient(orient)
self.side = side
return 1
def hide(self, resize=1):
self.show(0, resize)
def destroy(self):
for w in self._tooltips:
if w:
w.destroy()
self._tooltips = []
for w in self._widgets:
if w:
w.destroy()
self._widgets = []
def setCursor(self, cursor):
if self.side:
self.frame.config(cursor=cursor)
self.frame.update_idletasks()
def updateText(self, **kw):
for name in kw.keys():
label = getattr(self, name + "_label")
label["text"] = kw[name]
def updateImages(self, dir, size):
if dir == self.dir and size == self.size:
return 0
if not os.path.isdir(dir):
return 0
old_dir, old_size = self.dir, self.size
self.dir, self.size = dir, size
data = []
try:
for w in self._widgets:
if not isinstance(w, (ToolbarButton, ToolbarCheckbutton)):
continue
name = w.toolbar_name
image = self._loadImage(name)
data.append((name, w, image))
except Exception:
self.dir, self.size = old_dir, old_size
return 0
label = self.player_label
aspect = (400, 300)[size != 0]
label.config(aspect=aspect)
for name, w, image in data:
w.config(image=image)
setattr(self, name + "_image", image)
self.setCompound(self.compound, force=True)
return 1
def setCompound(self, compound, force=False):
if not force and self.compound == compound:
return False
for w in self._widgets:
if not isinstance(w, (ToolbarButton, ToolbarCheckbutton)):
continue
if compound == 'text':
w.config(compound='none', image='')
else:
image = getattr(self, w.toolbar_name+'_image')
w.config(compound=compound, image=image)
self.compound = compound
return True
def _setOrient(self, orient='horizontal', force=False):
if not force and self.orient == orient:
return False
for w in self._widgets:
if w.visible:
w.show(orient=orient, force=True)
self.orient = orient
return True
#
# Mouse event handlers
#
def clickHandler(self, event):
if self._busy():
return EVENT_HANDLED
return EVENT_HANDLED
def rightclickHandler(self, event):
if self._busy():
return EVENT_HANDLED
if self.popup:
self.popup.tk_popup(event.x_root, event.y_root)
return EVENT_HANDLED
def middleclickHandler(self, event):
if self._busy():
return EVENT_HANDLED
if 1 <= self.side <= 2:
self.menubar.setToolbarSide(3 - self.side)
return EVENT_HANDLED
def getSize(self):
if self.compound == 'text':
return 0
size = self.size
comp = int(self.compound in ('top', 'bottom'))
return int((size+comp) != 0)
|