This repository has been archived on 2022-12-22. You can view files and clone it, but cannot push or open issues/pull-requests.
Concorde-IDE/gui_helper.py

130 lines
4.1 KiB
Python

import tkinter as tk
import util
# easy way to communicate across events
class Communication:
def __init__(self):
self.__messages = {}
# send a message tagged with name and containing content, adds to the beginning of the list of messages with the same tag by default
def send(self, name, content, reverse_order=True):
if name in self.__messages:
if reverse_order:
self.__messages[name] = [content] + self.__messages[name]
else:
self.__messages[name] = self.__messages[name] + [content]
else:
self.__messages[name] = [content]
# get the content of the first message tagged with name, removes the returned message by default
def get(self, name, remove=True):
if name in self.__messages and len(self.__messages[name])>0:
if remove:
content = self.__messages[name].pop(0)
if len(self.__messages[name])==0:
del self.__messages[name]
return content
else:
return self.__messages[name][0]
else:
return None
# get the contents for all messages tagged with name
def get_all(self, name, clear=False):
if name in self.__messages and len(self.__messages[name])>0:
contents = self.__messages[name]
if clear:
del self.__messages[name]
return contents
# deletes all messages tagged with name
def clear(self, name):
if name in self.__messages:
del self.__messages[name]
def __del__(self):
if len(self.__messages)>0:
util.warn("__messages not empty upon destruction of Communitation object:\n"+str(self.__messages))
def not_implemented():
util.warn("Not implemented!")
# format:
# "":{} -> menu or submenu
# "":function -> menu entry
# "":None -> disabled menu entry
# None:None -> separator
#
# Entries with ... at the end are supposed to open dialogs whereas entries without dots are supposed to take effect immediately
menu_structure = {
"IDE": {
"Preferences...": not_implemented,
None: None,
"Quit": not_implemented
},
"Project": {
"New": {
"No known project types": None
},
"Open...": not_implemented,
"Close": {
"No open projects": None
},
None: None,
"Preferences...": not_implemented,
"Search...": not_implemented,
"Build": not_implemented
},
"File": {
"New...": not_implemented,
"Open...": not_implemented,
"Save": not_implemented,
"Close": not_implemented,
None: None,
"Rename...": not_implemented,
"Move...": not_implemented,
"View in File Explorer...": not_implemented
},
"Edit": {
"Cut": not_implemented,
"Copy": not_implemented,
"Paste": not_implemented,
"Move code...": not_implemented,
None: None,
"Search and Replace...": not_implemented,
None: None,
"Format": not_implemented,
"Indent": not_implemented,
"Unindent": not_implemented,
"Toggle Comment": not_implemented
},
"View": {
"Zoom in": not_implemented,
"Zoom out": not_implemented,
"Normal Size": not_implemented
},
"Help": {
"Manual...": not_implemented,
"About IDE...": not_implemented,
}
}
#FIXME: come up with a way to uniquely declare separators so they don't become the same element of a dict
def build_menu(structure_dict, menu):
for entry in structure_dict:
if structure_dict[entry]==None:
if entry==None:
menu.add_separator()
else:
menu.add_command(label=entry)
menu.entryconfig(entry, state="disabled")
if isinstance(structure_dict[entry], dict):
submenu = tk.Menu(menu, tearoff=False)
build_menu(structure_dict[entry], submenu)
menu.add_cascade(label=entry, menu=submenu)
if callable(structure_dict[entry]):
menu.add_command(label=entry, command=structure_dict[entry])