forked from vlachoudis/bCNC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ribbon.py
664 lines (577 loc) · 21 KB
/
Ribbon.py
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
# -*- coding: ascii -*-
#
# Author: vvlachoudis@gmail.com
# Date: 18-Jun-2015
__author__ = "Vasilis Vlachoudis"
__email__ = "vvlachoudis@gmail.com"
try:
from Tkinter import *
except ImportError:
from tkinter import *
import Utils
import tkExtra
_TABFONT = ("Sans","-14","bold")
_FONT = ("Sans","-11")
#_BACKGROUND_DISABLE = "#D6D2D0"
#_BACKGROUND = "White"
#_BACKGROUND_GROUP = "LightGray"
#_BACKGROUND_GROUP2 = "#D0E0E0"
#_FOREGROUND_GROUP = "Black"
_BACKGROUND_DISABLE = "#A6A2A0"
_BACKGROUND = "#E6E2E0"
_BACKGROUND_GROUP = "#B6B2B0"
_BACKGROUND_GROUP2 = "#B0C0C0"
_BACKGROUND_GROUP3 = "#A0C0A0"
_BACKGROUND_GROUP4 = "#B0C0A0"
_FOREGROUND_GROUP = "White"
_ACTIVE_COLOR = "LightYellow"
_LABEL_SELECT_COLOR = "#C0FFC0"
# Ribbon show state
RIBBON_HIDDEN = 0 # Hidden
RIBBON_SHOWN = 1 # Displayed
RIBBON_TEMP = -1 # Show temporarily
#===============================================================================
# Frame Group with a button at bottom
#===============================================================================
class LabelGroup(Frame):
def __init__(self, master, name, command=None, **kw):
Frame.__init__(self, master, **kw)
self.name = name
self.config( #bg="Green",
background=_BACKGROUND,
borderwidth=0,
highlightthickness=0,
pady=0)
# right frame as a separator
f = Frame(self, borderwidth=2, relief=GROOVE, background=_BACKGROUND_DISABLE)
f.pack(side=RIGHT, fill=Y, padx=0, pady=0)
# frame to insert the buttons
self.frame = Frame(self,
#bg="Orange",
background=_BACKGROUND,
padx=0,
pady=0)
self.frame.pack(side=TOP, expand=TRUE, fill=BOTH, padx=0, pady=0)
if command:
self.label = LabelButton(self, self, "<<%s>>"%(name), text=name)
self.label.config(command=command,
image=Utils.icons["triangle_down"],
foreground=_FOREGROUND_GROUP,
background=_BACKGROUND_GROUP,
highlightthickness=0,
borderwidth=0,
pady=0,
compound=RIGHT)
else:
self.label = Label(self, text=_(name),
font = _FONT,
foreground = _FOREGROUND_GROUP,
background = _BACKGROUND_GROUP,
padx=2,
pady=0) # Button takes 1px for border width
self.label.pack(side=BOTTOM, fill=X, pady=0)
#-----------------------------------------------------------------------
def grid2rows(self):
self.frame.grid_rowconfigure(0, weight=1)
self.frame.grid_rowconfigure(1, weight=1)
#-----------------------------------------------------------------------
def grid3rows(self):
self.grid2rows()
self.frame.grid_rowconfigure(2, weight=1)
#===============================================================================
class _KeyboardFocus:
#-----------------------------------------------------------------------
def _bind(self):
self.bind("<Return>", self._invoke)
self.bind("<FocusIn>", self._focusIn)
self.bind("<FocusOut>", self._focusOut)
#-----------------------------------------------------------------------
def _focusIn(self, event):
self.__backgroundColor = self.cget("background")
self.config(background = _ACTIVE_COLOR)
#-----------------------------------------------------------------------
def _focusOut(self, event):
self.config(background = self.__backgroundColor)
#-----------------------------------------------------------------------
def _invoke(self, event):
self.invoke()
#===============================================================================
# Button with Label that generates a Virtual Event or calls a command
#===============================================================================
class LabelButton(Button, _KeyboardFocus):
def __init__(self, master, recipient=None, event=None, **kw):
Button.__init__(self, master, **kw)
self.config( relief = FLAT,
activebackground = _ACTIVE_COLOR,
font = _FONT,
borderwidth = 1,
highlightthickness = 0,
padx = 2,
pady = 0)
_KeyboardFocus._bind(self)
if recipient is not None:
self.config(command = self.sendEvent)
self._recipient = recipient
self._event = event
else:
self._recipient = None
self._event = None
#-----------------------------------------------------------------------
def sendEvent(self):
self._recipient.event_generate(self._event)
#===============================================================================
class LabelCheckbutton(Checkbutton, _KeyboardFocus):
def __init__(self, master, **kw):
Checkbutton.__init__(self, master, **kw)
self.config( selectcolor = _LABEL_SELECT_COLOR,
activebackground = _ACTIVE_COLOR,
background = _BACKGROUND,
indicatoron = FALSE,
relief = FLAT,
borderwidth = 0,
highlightthickness = 0,
padx = 0,
pady = 0,
font = _FONT
)
_KeyboardFocus._bind(self)
#===============================================================================
class LabelRadiobutton(Radiobutton, _KeyboardFocus):
def __init__(self, master, **kw):
Radiobutton.__init__(self, master, **kw)
self.config(
selectcolor = _LABEL_SELECT_COLOR,
activebackground = _ACTIVE_COLOR,
background = _BACKGROUND,
indicatoron = FALSE,
borderwidth = 0,
highlightthickness = 0,
pady = 0,
font = _FONT
)
_KeyboardFocus._bind(self)
#===============================================================================
class LabelCombobox(tkExtra.Combobox, _KeyboardFocus):
def __init__(self, master, **kw):
tkExtra.Combobox.__init__(self, master, **kw)
self.config(background=_BACKGROUND, font=_FONT)
Frame.config(self, background=_BACKGROUND, padx=0, pady=0)
_KeyboardFocus._bind(self)
#-----------------------------------------------------------------------
def _focusOut(self, event):
self.config(background = _BACKGROUND) #self.__backgroundColor)
Frame.config(self, background= _BACKGROUND) #self.__backgroundColor)
#===============================================================================
# Button with Label that popup a menu
#===============================================================================
class MenuButton(Button, _KeyboardFocus):
def __init__(self, master, menulist, **kw):
Button.__init__(self, master, **kw)
self.config( relief = FLAT,
activebackground = _ACTIVE_COLOR,
font = _FONT,
borderwidth = 0,
highlightthickness= 0,
padx = 2,
pady = 0,
command = self.showMenu)
_KeyboardFocus._bind(self)
self.bind("<Return>", self.showMenu)
if menulist is not None:
self._menu = MenuButton.createMenuFromList(self, menulist)
else:
self._menu = None
#-----------------------------------------------------------------------
def showMenu(self, event=None):
if self._menu is not None:
self._showMenu(self._menu)
else:
self._showMenu(self.createMenu())
#-----------------------------------------------------------------------
def _showMenu(self, menu):
if menu is not None:
menu.tk_popup(
self.winfo_rootx(),
self.winfo_rooty() + self.winfo_height())
#-----------------------------------------------------------------------
def createMenu(self):
return None
#-----------------------------------------------------------------------
@staticmethod
def createMenuFromList(master, menulist):
mainmenu = menu = Menu(master, tearoff=0, activebackground=_ACTIVE_COLOR)
for item in menulist:
if item is None:
menu.add_separator()
elif isinstance(item,str):
menu = Menu(mainmenu)
mainmenu.add_cascade(label=item, menu=menu)
else:
name, icon, cmd = item
if icon is None: icon = "empty"
menu.add_command(label=name,
image=Utils.icons[icon],
compound=LEFT,
command=cmd)
return menu
#===============================================================================
# A label group with a drop down menu
#===============================================================================
class MenuGroup(LabelGroup):
def __init__(self, master, name, menulist=None, **kw):
LabelGroup.__init__(self, master, name, command=self._showMenu, **kw)
self._menulist = menulist
#-----------------------------------------------------------------------
def createMenu(self):
if self._menulist is not None:
return MenuButton.createMenuFromList(self, self._menulist)
else:
return None
#-----------------------------------------------------------------------
def _showMenu(self):
menu = self.createMenu()
if menu is not None:
menu.tk_popup(
self.winfo_rootx(),
self.winfo_rooty() + self.winfo_height())
#===============================================================================
# Context group for a specific item in the Ribbon
#===============================================================================
#class ContextGroup(LabelGroup):
# def __init__(self, master, name, **kw):
# LabelGroup.__init__(self, master, name, **kw)
#===============================================================================
# Page Tab buttons
#===============================================================================
class TabButton(Radiobutton):
def __init__(self, master, **kw):
Radiobutton.__init__(self, master, **kw)
self.config( selectcolor = _BACKGROUND,
activebackground = _ACTIVE_COLOR,
indicatoron = FALSE,
relief = FLAT,
font = _TABFONT,
borderwidth = 0,
highlightthickness = 0,
padx = 5,
pady = 0,
background = _BACKGROUND_DISABLE
)
self.bind("<FocusIn>", self._focusIn)
self.bind("<FocusOut>", self._focusOut)
#-----------------------------------------------------------------------
# Bind events on TabFrame
#----------------------------------------------------------------------
def bindClicks(self, tabframe):
self.bind("<Double-1>", tabframe.double)
self.bind("<Button-1>", tabframe.dragStart)
self.bind("<B1-Motion>", tabframe.drag)
self.bind("<ButtonRelease-1>", tabframe.dragStop)
self.bind("<Control-ButtonRelease-1>", tabframe.pinActive)
self.bind("<Left>", tabframe._tabLeft)
self.bind("<Right>", tabframe._tabRight)
self.bind("<Down>", tabframe._tabDown)
#----------------------------------------------------------------------
def _focusIn(self, evenl=None):
self.config(selectcolor = _ACTIVE_COLOR)
#----------------------------------------------------------------------
def _focusOut(self, evenl=None):
self.config(selectcolor = _BACKGROUND)
#===============================================================================
# Page
#===============================================================================
class Page: # <--- should be possible to be a toplevel as well
_motionClasses = (LabelButton, LabelRadiobutton, LabelCheckbutton, LabelCombobox, MenuButton)
_name_ = None
_icon_ = None
_doc_ = "Tooltip"
#-----------------------------------------------------------------------
def __init__(self, master, **kw):
self.master = master
self.name = self._name_
self._icon = Utils.icons[self._icon_]
self._tab = None # Tab button
self.ribbons = []
self.frames = []
self.init()
self.create()
#-----------------------------------------------------------------------
# Override initialization
#-----------------------------------------------------------------------
def init(self):
pass
#-----------------------------------------------------------------------
# The tab page can change master if undocked
#-----------------------------------------------------------------------
# FIXME XXX SHOULD BE REMOVED
#-----------------------------------------------------------------------
def create(self):
self.createPage()
# self.ribbonBindMotion()
# self.refresh()
#-----------------------------------------------------------------------
# FIXME XXX SHOULD BE REMOVED
#-----------------------------------------------------------------------
def createPage(self):
self.page = Frame(self.master._pageFrame)
return self.page
#-----------------------------------------------------------------------
# Called when a page is activated
#-----------------------------------------------------------------------
def activate(self):
pass
#-----------------------------------------------------------------------
def refresh(self):
pass
# ----------------------------------------------------------------------
def canUndo(self): return True
def canRedo(self): return True
def resetUndo(self): pass
def undo(self, event=None): pass
def redo(self, event=None): pass
# ----------------------------------------------------------------------
def refreshUndoButton(self):
# Check if frame provides undo/redo
if self.master is None: return
if self.page is None: return
if self.canUndo():
state = NORMAL
else:
state = DISABLED
self.master.tool["undo"].config(state=state)
self.master.tool["undolist"].config(state=state)
if self.canRedo():
state = NORMAL
else:
state = DISABLED
self.master.tool["redo"].config(state=state)
#-----------------------------------------------------------------------
def keyboardFocus(self):
self._tab.focus_set()
#-----------------------------------------------------------------------
# Return the closest widget along a direction
#-----------------------------------------------------------------------
@staticmethod
def __compareDown(x,y,xw,yw): return yw>y+1
@staticmethod
def __compareUp(x,y,xw,yw): return yw<y-1
@staticmethod
def __compareRight(x,y,xw,yw): return xw>x+1
@staticmethod
def __compareLeft(x,y,xw,yw): return xw<x-1
#-----------------------------------------------------------------------
@staticmethod
def __closest(widget, compare, x, y):
closest = None
dc2 = 10000000
if widget is None: return closest, dc2
for child in widget.winfo_children():
for class_ in Page._motionClasses:
if isinstance(child, class_):
if child["state"] == DISABLED: continue
xw = child.winfo_rootx()
yw = child.winfo_rooty()
if compare(x,y,xw,yw):
d2 = (xw-x)**2 + (yw-y)**2
if d2 < dc2:
closest = child
dc2 = d2
break
else:
c,d2 = Page.__closest(child, compare, x, y)
if d2 < dc2:
closest = c
dc2 = d2
return closest, dc2
#-----------------------------------------------------------------------
# Select/Focus the closest element
#-----------------------------------------------------------------------
def _ribbonUp(self, event=None):
x = event.widget.winfo_rootx()
y = event.widget.winfo_rooty()
closest,d2 = Page.__closest(self.ribbon, Page.__compareUp, x, y)
if closest is not None:
closest.focus_set()
#-----------------------------------------------------------------------
def _ribbonDown(self, event=None):
x = event.widget.winfo_rootx()
y = event.widget.winfo_rooty()
closest,d2 = Page.__closest(self.ribbon, Page.__compareDown, x, y)
if closest is not None:
closest.focus_set()
#-----------------------------------------------------------------------
def _ribbonLeft(self, event=None):
x = event.widget.winfo_rootx()
y = event.widget.winfo_rooty()
closest,d2 = Page.__closest(self.ribbon, Page.__compareLeft, x, y)
if closest is not None:
closest.focus_set()
#-----------------------------------------------------------------------
def _ribbonRight(self, event=None):
x = event.widget.winfo_rootx()
y = event.widget.winfo_rooty()
closest,d2 = Page.__closest(self.ribbon, Page.__compareRight, x, y)
if closest is not None:
closest.focus_set()
#===============================================================================
# TabRibbonFrame
#===============================================================================
class TabRibbonFrame(Frame):
def __init__(self, master, **kw):
Frame.__init__(self, master, kw)
self.config(background=_BACKGROUND_DISABLE)
self.oldActive = None
self.activePage = StringVar(self)
self.tool = {}
self.pages = {}
# === Top frame with buttons ===
frame = Frame(self, background=_BACKGROUND_DISABLE)
frame.pack(side=TOP, fill=X)
# --- Basic buttons ---
b = LabelButton(frame, self, "<<New>>",
image=Utils.icons["new"],
background=_BACKGROUND_DISABLE)
tkExtra.Balloon.set(b, _("New file"))
b.pack(side=LEFT)
b = LabelButton(frame, self, "<<Open>>",
image=Utils.icons["load"],
background=_BACKGROUND_DISABLE)
tkExtra.Balloon.set(b, _("Open file [Ctrl-O]"))
b.pack(side=LEFT)
b = LabelButton(frame, self, "<<Save>>",
image=Utils.icons["save"],
background=_BACKGROUND_DISABLE)
tkExtra.Balloon.set(b, _("Save all [Ctrl-S]"))
b.pack(side=LEFT)
b = LabelButton(frame, self, "<<Undo>>",
image=Utils.icons["undo"],
background=_BACKGROUND_DISABLE)
tkExtra.Balloon.set(b, _("Undo [Ctrl-Z]"))
b.pack(side=LEFT)
self.tool["undo"] = b
b = LabelButton(frame, image=Utils.icons["triangle_down"],
command=self.undolist,
background=_BACKGROUND_DISABLE)
b.pack(side=LEFT)
self.tool["undolist"] = b
b = LabelButton(frame, self, "<<Redo>>",
image=Utils.icons["redo"],
background=_BACKGROUND_DISABLE)
tkExtra.Balloon.set(b, _("Redo [Ctrl-Y]"))
b.pack(side=LEFT)
self.tool["redo"] = b
Label(frame, image=Utils.icons["sep"],
background=_BACKGROUND_DISABLE).pack(side=LEFT, padx=3)
# --- Help ---
b = LabelButton(frame, self, "<<Help>>",
image=Utils.icons["info"],
background=_BACKGROUND_DISABLE)
tkExtra.Balloon.set(b, _("Help [F1]"))
b.pack(side=RIGHT, padx=2)
Label(frame, image=Utils.icons["sep"],
background=_BACKGROUND_DISABLE).pack(side=RIGHT, padx=3)
# --- TabBar ---
self._tabFrame = Frame(frame, background=_BACKGROUND_DISABLE)
self._tabFrame.pack(side=LEFT, fill=BOTH, expand=YES)
# ==== Ribbon Frame ====
self._ribbonFrame = Frame(self,
background=_BACKGROUND,
pady=0,
relief=RAISED)
self._ribbonFrame.pack(fill=BOTH, expand=YES, padx=0, pady=0)
self.setPageFrame(None)
#-----------------------------------------------------------------------
def setPageFrame(self, frame):
self._pageFrame = frame
#-----------------------------------------------------------------------
def undolist(self, event=None): self.event_generate("<<UndoList>>")
#-----------------------------------------------------------------------
def getActivePage(self):
return self.pages[self.activePage.get()]
#-----------------------------------------------------------------------
# Add page to the tabs
#-----------------------------------------------------------------------
def addPage(self, page, side=LEFT):
self.pages[page.name] = page
page._tab = TabButton(self._tabFrame,
image = page._icon,
text = _(page.name),
compound = LEFT,
value = page.name,
variable = self.activePage,
command = self.changePage)
tkExtra.Balloon.set(page._tab, page.__doc__)
page._tab.pack(side=side, fill=Y, padx=5)
# ----------------------------------------------------------------------
# Unpack the old page
# ----------------------------------------------------------------------
def _forgetPage(self):
if self.oldActive:
for frame,args in self.oldActive.ribbons:
frame.pack_forget()
for frame,args in self.oldActive.frames:
frame.pack_forget()
self.oldActive = None
# ----------------------------------------------------------------------
# Change ribbon and page
# ----------------------------------------------------------------------
def changePage(self, page=None):
#import traceback
#traceback.print_stack()
if page is not None:
if not isinstance(page, Page):
try:
page = self.pages[page]
except KeyError:
return
self.activePage.set(page.name)
else:
try:
page = self.pages[self.activePage.get()]
except KeyError:
return
if page is self.oldActive: return
self._forgetPage()
for frame,args in page.ribbons:
frame.pack(in_=self._ribbonFrame, **args)
for frame,args in page.frames:
frame.pack(in_=self._pageFrame, **args)
self.oldActive = page
page.activate()
self.event_generate("<<ChangePage>>", data=page.name)
# #-----------------------------------------------------------------------
# # Give focus to the tab on the left
# #-----------------------------------------------------------------------
# def _tabLeft(self, event=None):
# slaves = self._tabFrame.pack_slaves()
# try:
# pos = slaves.index(event.widget)-1
# except ValueError:
# if event.widget is self.dynamic:
# pos = len(slaves)-1
# else:
# return
# if pos < 0: return # Do not replace First tab
# slaves[pos].select()
# #self.changePage()
# slaves[pos].focus_set()
#
# #-----------------------------------------------------------------------
# # Give focus to the tab on the right
# #-----------------------------------------------------------------------
# def _tabRight(self, event=None):
# slaves = self._tabFrame.pack_slaves()
# try:
# pos = slaves.index(event.widget)+1
# except ValueError:
# return
# if pos < len(slaves):
# slaves[pos].select()
# #self.changePage()
# slaves[pos].focus_set()
# else:
# # Open dynamic menu
# self.dynamic.select()
# self.dynamic.focus_set()
# self.dynamicMenu()
#