# -*- test-case-name: mv3d.test.util.test_guideweb -*- # Copyright (C) 2010-2012 Mortal Coil Games # See LICENSE for details. """ Guide implementation for the web. @author: mike """ from nevow import rend, loaders, inevow, tags as T from mv3d.util import guide class Label(guide.Label, rend.Fragment): """ A text box that is a guide widget """ def __init__(self, **attribs): guide.Label.__init__(self, **attribs) self.tag = [T.a(render=T.directive("text"))] self.docFactory = loaders.stan(self.tag) def render_text(self, _ctx, _tag): """ Render the text. """ return self.text class TextBox(guide.TextBox, rend.Fragment): """ A text box that is a guide widget """ def __init__(self, **attribs): guide.TextBox.__init__(self, **attribs) if self.name is None: self.name = str(id(self)) self.docFactory = loaders.stan(T.a(render=T.directive("text"))) def render_text(self, ctx, _tag): """ Render the textbox """ request = inevow.IRequest(ctx) if request.args.has_key(self.name): self.text = request.args[self.name][0] return T.input(type="text", name=self.name, value=self.text) class CheckBox(guide.CheckBox, rend.Fragment): """ A text box that is a guide widget """ def __init__(self, **attribs): guide.CheckBox.__init__(self, **attribs) if self.name is None: self.name = str(id(self)) self.docFactory = loaders.stan(T.a(render=T.directive("check"))) def render_check(self, ctx, _tag): """ Render the checkbox """ request = inevow.IRequest(ctx) if request.args.has_key(self.name): self.checked = request.args[self.name][0] if not self.checked: return [T.input(type="checkbox", name=self.name), self.text] else: return [T.input(type="checkbox", name=self.name, checked=True), self.text] class ComboBox(guide.ComboBox, rend.Fragment): """ A text box that is a guide widget """ def __init__(self, **attribs): guide.ComboBox.__init__(self, **attribs) if self.name is None: self.name = str(id(self)) self.docFactory = loaders.stan(T.a(render=T.directive("combo"))) def render_combo(self, ctx, _tag): """ Render the combobox """ request = inevow.IRequest(ctx) if request.args.has_key(self.name): self.text = request.args[self.name][0] items = [] for item in self.items: if self.text == item: items.append(T.option(value=item, selected=1)[item]) else: items.append(T.option(value=item)[item]) return T.select(name=self.name)[items] ComboBoxItem = guide.ComboBoxItem class ListBox(guide.ListBox, rend.Fragment): """ A text box that is a guide widget """ def __init__(self, **attribs): guide.ListBox.__init__(self, **attribs) if self.name is None: self.name = str(id(self)) self.docFactory = loaders.stan(T.a(render=T.directive("list"))) def render_list(self, ctx, _tag): """ Render the listbox """ request = inevow.IRequest(ctx) if request.args.has_key(self.name): self.selection = request.args[self.name][0] items = [] for item in self.items: if item == self.selection: items.append(T.option(value=item, selected=1)[ item]) else: items.append(T.option(value=item)[ item]) return T.select(name=self.name, multiple=True)[items] ListBoxItem = guide.ListBoxItem class Button(guide.Button, rend.Fragment): """ A button that is a guide widget """ def __init__(self, **attribs): guide.Button.__init__(self, **attribs) if self.name is None: self.name = str(id(self)) self.tag = T.input(type="submit", name=self.name, value=self.text) self.docFactory = loaders.stan(self.tag) class StackFrame(guide.StackFrame, rend.Fragment): """ A frame that stacks """ def onGetChildren(self, _parser): """ Called once our children have been parsed. """ guide.StackFrame.onGetChildren(self, _parser) self.tag = T.table(border=0)[ [T.tr[T.td[child]] for child in self.children]] self.docFactory = loaders.stan(self.tag) class GridFrame(guide.GridFrame, rend.Fragment): """ A panel with a box sizer """ def onGetChildren(self, _parser): """ Called once our children have been parsed. """ guide.GridFrame.onGetChildren(self, _parser) self.tag = [child for child in self.children] self.docFactory = loaders.stan(self.tag) class GridRow(guide.GridRow, rend.Fragment): """ A single row in the grid """ def onGetChildren(self, _parser): """ Called once our children have been parsed. """ guide.GridRow.onGetChildren(self, _parser) children = [] for child in self.children: children.append(T.td[child]) self.tag = T.tr[children] self.docFactory = loaders.stan(self.tag) class GridCell(guide.GridCell, rend.Fragment): """ A single cell in the grid """ def onGetChildren(self, _parser): """ Called once our children have been parsed. """ guide.GridCell.onGetChildren(self, _parser) self.tag = self.children self.docFactory = loaders.stan(self.tag) class DockableFrame(GridFrame): """ TODO: Implement me somehow """ class TabFrame(GridFrame): """ TODO: Implement me """ class Tab(GridFrame): """ TODO: Implement me """ class ExpandFrame(GridFrame): """ TODO: Implement me """ class Window(guide.Widget, rend.Fragment): """ Top level guide frame """ def onGetChildren(self, _parser): """ Called once our children have been parsed. """ self.subLayout = [] childTag = [] for child in self.children: childTag.append(T.tr[T.td[child]]) self.docFactory = loaders.stan(T.form(method="POST", action="#") [T.table(border=1)[childTag]]) class WebParser(guide.Parser): """ The web version of the parser """ def getWidgetClass(self, name): """ Return a web based widget """ return globals()[name]