import core
from core import *


class DMIDE_MenuBar(wx.MenuBar):
	def __init__(self, window=None):
		wx.MenuBar.__init__(self, wx.MB_DOCKABLE)
		self.installMenuService()

	def installMenuService(self):
		""" Build a MenuBar for the top-level window as read from an XML file """

		if os.path.exists(os.path.join(wx.GetApp().get_dir(), 'settings', 'menubar.xml')):
			handler = MenuBarHandler(self)
			parser = xml.sax.parse(os.path.join(wx.GetApp().get_dir(), 'settings', 'menubar.xml'), handler)

		elif os.path.exists('menubar.xml'):
			handler = MenuBarHandler(self)
			parser = xml.sax.parse('menubar.xml', handler)

		else:
			handler = MenuBarHandler(self)
			parser = xml.sax.parseString(default_menu, handler)


class DMIDE_FancyMenuBar(wxFlatMenu.FlatMenuBar):
	def __init__(self, window):
		wxFlatMenu.FlatMenuBar.__init__(self, window, ID_MENUBAR)
		self.installMenuService()

	def installMenuService(self):
		""" Build a MenuBar for the top-level window as read from an XML file """

		if os.path.exists(os.path.join(wx.GetApp().get_dir(), 'settings', 'menubar.xml')):
			handler = FancyMenuBarHandler(self)
			parser = xml.sax.parse(os.path.join(wx.GetApp().get_dir(), 'settings', 'menubar.xml'), handler)

		elif os.path.exists('menubar.xml'):
			handler = FancyMenuBarHandler(self)
			parser = xml.sax.parse('menubar.xml', handler)

		else:
			handler = FancyMenuBarHandler(self)
			parser = xml.sax.parseString(default_menu, handler)

	def FindItemById(self, id):
		return self.FindMenuItem(id)


class MenuBarHandler(xml.sax.handler.ContentHandler):
	""" Handler for reading the XML """

	def __init__(self, menubar):
		self.ordered_list = []
		self.current = []
		self.menubar = menubar

	def startElement(self, name, attributes):
		if name == 'menu_bar':
			if not self.current:
				menu_title = attributes['title']
				self.current = [(wx.Menu(), menu_title)]

			else:
				menu_title = attributes['title']
				self.current.append((wx.Menu(), menu_title))

		elif name == 'menu':
			if not self.current:
				return

			if attributes.has_key('type'):
				if attributes['type'] == 'separator':
					self.current[-1][0].AppendSeparator()
				return

			id = attributes['id']
			title = attributes['title']
			macro = ''
			desc = ''
			flags = ''

			if attributes.has_key('macro'):
				macro = attributes['macro']
			if attributes.has_key('desc'):
				desc = attributes['desc']
			if attributes.has_key('flags'):
				flags = attributes['flags']

			if macro:
				title = '%s\t%s' % (title, macro)

			if flags:
				self.current[-1][0].Append(globals()[id], title, desc, globals()[flags])
			else:
				self.current[-1][0].Append(globals()[id], title, desc)

	def endElement(self, name):
		if name == 'menu_bar':
			if self.current == [self.current[-1]]:
				self.menubar.Append(*self.current[-1])
				self.current = []

			else:
				self.current[-2][0].AppendMenu(wx.ID_ANY, self.current[-1][1], self.current[-1][0])
				self.current = self.current[:-1]


class FancyMenuBarHandler(xml.sax.handler.ContentHandler):
	""" Handler for reading the XML """

	def __init__(self, menubar):
		self.ordered_list = []
		self.current = []
		self.menubar = menubar

	def startElement(self, name, attributes):
		if name == 'menu_bar':
			if not self.current:
				menu_title = attributes['title']
				self.current = [(wxFlatMenu.FlatMenu(), menu_title)]

			else:
				menu_title = attributes['title']
				self.current.append((wxFlatMenu.FlatMenu(), menu_title))

		elif name == 'menu':
			if not self.current:
				return

			if attributes.has_key('type'):
				if attributes['type'] == 'separator':
					self.current[-1][0].AppendSeparator()
				return

			id = attributes['id']
			title = attributes['title']
			macro = ''
			desc = ''
			flags = ''

			if attributes.has_key('macro'):
				macro = attributes['macro']
			if attributes.has_key('desc'):
				desc = attributes['desc']
			if attributes.has_key('flags'):
				flags = attributes['flags']

			if macro:
				title = '%s\t%s' % (title, macro)

			'''
			if flags:
				self.current[-1][0].Append(globals()[id], title, desc, globals()[flags])
			else:
				self.current[-1][0].Append(globals()[id], title, desc)
			'''

			if not flags:
				flags = 'ID_ITEM_NORMAL'

			if id in id_to_art:
				bmp = wx.GetApp().art.getFromWx(id_to_art[id], (dmide_menu_art_size, dmide_menu_art_size), wx.ART_MENU)
				disabled = wx.BitmapFromImage(wx.ImageFromBitmap(bmp).ConvertToGreyscale())

			else:
				bmp = wx.NullBitmap
				disabled = wx.NullBitmap