Example #1
0
class Dialog(Fixed):
    """An in-window message box

    **Signals**

        **on-close** *(sprite, pressed_button)*
        - fired when the window is closed. returns back the label of the button that triggered closing
    """
    __gsignals__ = {
        "on-close": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,)),
    }

    title_class = DialogTitle
    dialog_box_class = DialogBox

    def __init__(self, contents = None,
                 title = None, draggable = True,
                 width = 500,
                 modal = False,
                 **kwargs):
        Fixed.__init__(self, **kwargs)
        self.interactive, self.mouse_cursor = True, False

        #: whether the dialog is modal or not. in case of modality won't be able
        #: to click on other interface elements while dialog is being displayed
        self.modal = modal

        #: dialog content - message and such like
        self.contents = contents

        #: container for description and buttons
        # setting it interactive and filling extents so that they can't be clicked through
        self.box = VBox(contents, interactive=True, mouse_cursor = False)
        def fill_blank():
            self.box.graphics.rectangle(0, 0, self.box.width, self.box.height)
            self.box.graphics.new_path()
        self.box.do_render = fill_blank

        #: the main container box that contains title and contents
        components = []
        self.title_label = None
        if title:
            self.title_label = self.title_class(title)
            components.append(self.title_label)
        components.append(self.box)

        self.main_box = self.dialog_box_class(components, interactive=draggable, draggable=draggable, width=width, padding=0)

        self.connect_child(self.main_box, "on-drag", self.on_drag)
        self._dragged = False

        self.add_child(self.main_box)

        #: fill color for the background when the dialog is modal
        self.background_fill = "#fff"

        #: opacity of the background fill when the dialog is modal
        self.background_opacity = .5

        #: initial centered position as a tuple in lieu of scale_x, scale_y
        self.pre_dragged_absolute_position = None


    def on_drag(self, sprite, event):
        self.main_box.x = max(min(self.main_box.x, self.width - 10), -self.main_box.width + 10 )
        self.main_box.y = max(min(self.main_box.y, self.height - 10), -self.main_box.height + 10 )
        self._dragged = True


    def __setattr__(self, name, val):
        if name in ("width",) and hasattr(self, "main_box"):
            self.main_box.__setattr__(name, val)
        else:
            Fixed.__setattr__(self, name, val)

        if name == "contents" and hasattr(self, "box"):
            self.box.add_child(val)

    def show(self, scene):
        """show the dialog"""
        scene.add_child(self)

    def resize_children(self):
        if not self._dragged:
            if not self.pre_dragged_absolute_position:
                self.main_box.x = (self.width - self.main_box.width) * self.x_align
                self.main_box.y = (self.height - self.main_box.height) * self.y_align
            else:
                #put it right where we want it
                self.main_box.x = self.pre_dragged_absolute_position[0] - (self.main_box.width * 0.5)
                self.main_box.y = self.pre_dragged_absolute_position[1] - (self.main_box.height * 0.5)
                #but ensure it is in bounds, cause if it ain't you could be in a world of modal pain
                self.on_drag( None, None )
        else:
            self.on_drag(self.main_box, None)

    def close(self, label = ""):
        self.emit("on-close", label)
        scene = self.get_scene()
        if scene:
            scene.remove_child(self)


    def do_render(self):
        if self.modal:
            self.graphics.rectangle(0, 0, self.width + 1, self.height + 1)
            self.graphics.fill(self.background_fill, self.background_opacity)
        else:
            self.graphics.clear()
Example #2
0
    def add_child(self, *pages):
        VBox.add_child(self, *pages)

        for page in pages:
            self.connect_child(page, "on-caption-mouse-down",
                               self.on_caption_mouse_down)
Example #3
0
 def add_child(self, *sprites):
     for sprite in sprites:
         if sprite in (self._caption, self.container):
             VBox.add_child(self, sprite)
         else:
             self.container.add_child(sprite)
Example #4
0
    def add_child(self, *pages):
        VBox.add_child(self, *pages)

        for page in pages:
            self.connect_child(page, "on-caption-mouse-down", self.on_caption_mouse_down)
Example #5
0
 def add_child(self, *sprites):
     for sprite in sprites:
         if sprite in (self._caption, self.container):
             VBox.add_child(self, sprite)
         else:
             self.container.add_child(sprite)
Example #6
0
class Dialog(Fixed):
    """An in-window message box

    **Signals**

        **on-close** *(sprite, pressed_button)*
        - fired when the window is closed. returns back the label of the button that triggered closing
    """
    __gsignals__ = {
        "on-close": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
                     (gobject.TYPE_PYOBJECT, )),
    }

    title_class = DialogTitle
    dialog_box_class = DialogBox

    def __init__(self,
                 contents=None,
                 title=None,
                 draggable=True,
                 width=500,
                 modal=False,
                 **kwargs):
        Fixed.__init__(self, **kwargs)
        self.interactive, self.mouse_cursor = True, False

        #: whether the dialog is modal or not. in case of modality won't be able
        #: to click on other interface elements while dialog is being displayed
        self.modal = modal

        #: dialog content - message and such like
        self.contents = contents

        #: container for description and buttons
        # setting it interactive and filling extents so that they can't be clicked through
        self.box = VBox(contents, interactive=True, mouse_cursor=False)

        def fill_blank():
            self.box.graphics.rectangle(0, 0, self.box.width, self.box.height)
            self.box.graphics.new_path()

        self.box.do_render = fill_blank

        #: the main container box that contains title and contents
        components = []
        self.title_label = None
        if title:
            self.title_label = self.title_class(title)
            components.append(self.title_label)
        components.append(self.box)

        self.main_box = self.dialog_box_class(components,
                                              interactive=draggable,
                                              draggable=draggable,
                                              width=width,
                                              padding=0)

        self.connect_child(self.main_box, "on-drag", self.on_drag)
        self._dragged = False

        self.add_child(self.main_box)

        #: fill color for the background when the dialog is modal
        self.background_fill = "#fff"

        #: opacity of the background fill when the dialog is modal
        self.background_opacity = .5

        #: initial centered position as a tuple in lieu of scale_x, scale_y
        self.pre_dragged_absolute_position = None

    def on_drag(self, sprite, event):
        self.main_box.x = max(min(self.main_box.x, self.width - 10),
                              -self.main_box.width + 10)
        self.main_box.y = max(min(self.main_box.y, self.height - 10),
                              -self.main_box.height + 10)
        self._dragged = True

    def __setattr__(self, name, val):
        if name in ("width", ) and hasattr(self, "main_box"):
            self.main_box.__setattr__(name, val)
        else:
            Fixed.__setattr__(self, name, val)

        if name == "contents" and hasattr(self, "box"):
            self.box.add_child(val)

    def show(self, scene):
        """show the dialog"""
        scene.add_child(self)

    def resize_children(self):
        if not self._dragged:
            if not self.pre_dragged_absolute_position:
                self.main_box.x = (self.width -
                                   self.main_box.width) * self.x_align
                self.main_box.y = (self.height -
                                   self.main_box.height) * self.y_align
            else:
                #put it right where we want it
                self.main_box.x = self.pre_dragged_absolute_position[0] - (
                    self.main_box.width * 0.5)
                self.main_box.y = self.pre_dragged_absolute_position[1] - (
                    self.main_box.height * 0.5)
                #but ensure it is in bounds, cause if it ain't you could be in a world of modal pain
                self.on_drag(None, None)
        else:
            self.on_drag(self.main_box, None)

    def close(self, label=""):
        self.emit("on-close", label)
        scene = self.get_scene()
        if scene:
            scene.remove_child(self)

    def do_render(self):
        if self.modal:
            self.graphics.rectangle(0, 0, self.width + 1, self.height + 1)
            self.graphics.fill(self.background_fill, self.background_opacity)
        else:
            self.graphics.clear()