def __init__(self, top, menubar, dir, size=0, relief='flat', compound='none'): self.top = top self.menubar = menubar #self._setRelief(relief) 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) # for l, 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_("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 ")+TITLE), ): if l is None: sep = self._createSeparator() sep.bind("<1>", self.clickHandler) sep.bind("<3>", self.rightclickHandler) elif l == 'Pause': self._createButton(l, f, check=True, tooltip=t) else: self._createButton(l, f, tooltip=t) self.pause_button.config(variable=menubar.tkopt.pause) sep = self._createFlatSeparator() sep.bind("<1>", self.clickHandler) sep.bind("<3>", self.rightclickHandler) self._createLabel("player", label=n_('Player'), tooltip=_("Player options")) # self.player_label.bind("<1>",self.mOptPlayerOptions) ##self.player_label.bind("<3>",self.mOptPlayerOptions) self.popup = MfxMenu(master=None, label=n_('Toolbar'), tearoff=0) createToolbarMenu(menubar, self.popup) self.frame.bind("<1>", self.clickHandler) self.frame.bind("<3>", self.rightclickHandler) # self.setCompound(compound, force=True)
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.frame = ttk.Frame(top, class_='Toolbar', relief=TkSettings.toolbar_relief, borderwidth=TkSettings.toolbar_borderwidth) # for l, 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_("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 ")+TITLE), ): if l is None: sep = self._createSeparator() sep.bind("<3>", self.rightclickHandler) elif l == 'Pause': self._createButton(l, f, check=True, tooltip=t) else: self._createButton(l, f, tooltip=t) self.pause_button.config(variable=menubar.tkopt.pause) self.popup = MfxMenu(master=None, label=n_('Toolbar'), tearoff=0) createToolbarMenu(menubar, self.popup) position=len(self._widgets) self.frame.rowconfigure(position, weight=1) self.frame.columnconfigure(position, weight=1) # self._createLabel("player", label=n_('Player'), tooltip=_("Player options")) # self.player_label.bind("<1>",self.mOptPlayerOptions) self.frame.bind("<3>", self.rightclickHandler) # 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') position = widget.position 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) 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): image = loadImage(file=file) break return image def _createSeparator(self): position=len(self._widgets) sep = ToolbarSeparator(self.frame, position=position, toolbar=self, takefocus=0) sep.show(orient=self.orient) self._widgets.append(sep) return sep def _createDisabledButtonImage(self, tkim): # grayscale and light-up image if not tkim: return None im = tkim._pil_image dis_im = ImageOps.grayscale(im) ##color = '#ffffff' ##factor = 0.6 color = '#dedede' factor = 0.7 sh = Image.new(dis_im.mode, dis_im.size, color) tmp = Image.blend(dis_im, sh, factor) dis_im = Image.composite(tmp, im, im) dis_tkim = ImageTk.PhotoImage(image=dis_im) return dis_tkim def _setButtonImage(self, button, name): image = self._loadImage(name) setattr(self, name + "_image", image) if Image: dis_image = self._createDisabledButtonImage(image) if dis_image: setattr(self, name + "_disabled_image", dis_image) button.config(image=(image, 'disabled', dis_image)) else: button.config(image=image) def _createButton(self, label, command, check=False, tooltip=None): name = label.lower() position = len(self._widgets) kw = { 'position' : position, 'toolbar' : self, 'toolbar_name' : name, 'command' : command, 'takefocus' : 0, 'text' : _(label), } if check: button = ToolbarCheckbutton(self.frame, **kw) else: button = ToolbarButton(self.frame, **kw) self._setButtonImage(button, name) button.show(orient=self.orient) 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)+1 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 padx, pady = TkSettings.horizontal_toolbar_padding pack_func(row=0, column=1, sticky='ew', padx=padx, pady=pady) elif side == 2: # bottom padx, pady = TkSettings.horizontal_toolbar_padding pack_func(row=2, column=1, sticky='ew', padx=padx, pady=pady) elif side == 3: # left padx, pady = TkSettings.vertical_toolbar_padding pack_func(row=1, column=0, sticky='ns', padx=padx, pady=pady) else: # right padx, pady = TkSettings.vertical_toolbar_padding pack_func(row=1, column=2, sticky='ns', padx=padx, pady=pady) # 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 self.dir, self.size = dir, size data = [] for w in self._widgets: if not isinstance(w, (ToolbarButton, ToolbarCheckbutton)): continue name = w.toolbar_name data.append((name, w)) l = self.player_label aspect = (400, 300) [size != 0] l.config(aspect=aspect) for name, w in data: self._setButtonImage(w, name) 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 w.config(compound=compound) 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 rightclickHandler(self, event): if self._busy(): return EVENT_HANDLED if self.popup: ##print event.x, event.y, event.x_root, event.y_root, event.__dict__ self.popup.tk_popup(event.x_root, event.y_root) 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)
class PysolToolbarTk: def __init__(self, top, menubar, dir, size=0, relief='flat', compound='none'): self.top = top self.menubar = menubar #self._setRelief(relief) 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) # for l, 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_("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 ")+TITLE), ): if l is None: sep = self._createSeparator() sep.bind("<1>", self.clickHandler) sep.bind("<3>", self.rightclickHandler) elif l == 'Pause': self._createButton(l, f, check=True, tooltip=t) else: self._createButton(l, f, tooltip=t) self.pause_button.config(variable=menubar.tkopt.pause) sep = self._createFlatSeparator() sep.bind("<1>", self.clickHandler) sep.bind("<3>", self.rightclickHandler) self._createLabel("player", label=n_('Player'), tooltip=_("Player options")) # self.player_label.bind("<1>",self.mOptPlayerOptions) ##self.player_label.bind("<3>",self.mOptPlayerOptions) self.popup = MfxMenu(master=None, label=n_('Toolbar'), tearoff=0) createToolbarMenu(menubar, self.popup) self.frame.bind("<1>", self.clickHandler) self.frame.bind("<3>", self.rightclickHandler) # 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') position = widget.position if v: widget.show(orient=self.orient) else: widget.hide() # prev_visible = None last_visible = None for w in self._widgets: if w.__class__ is ToolbarSeparator: if prev_visible is None or prev_visible.__class__ is ToolbarSeparator: w.hide() else: w.show(orient=self.orient) elif w.__class__ is ToolbarFlatSeparator: if prev_visible.__class__ is 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: self.dir, self.size = old_dir, old_size return 0 l = self.player_label aspect = (400, 300) [size != 0] l.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: ##print event.x, event.y, event.x_root, event.y_root, event.__dict__ 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)