def __init__(self, parent, html, scale_dx, scale_dy): """ Initializes the object. """ wx.Frame.__init__(self, parent, -1, "Help", style=wx.SIMPLE_BORDER) self.SetBackgroundColour(WindowColor) # Wrap the dialog around the image button panel: sizer = wx.BoxSizer(wx.VERTICAL) html_control = wh.HtmlWindow(self) html_control.SetBorders(2) html_control.SetPage(html) sizer.Add(html_control, 1, wx.EXPAND) sizer.Add(wx.StaticLine(self, -1), 0, wx.EXPAND) b_sizer = wx.BoxSizer(wx.HORIZONTAL) button = wx.Button(self, -1, "OK") self.Bind(wx.EVT_BUTTON, self._on_ok, id=button.GetId()) b_sizer.Add(button, 0) sizer.Add(b_sizer, 0, wx.ALIGN_RIGHT | wx.ALL, 5) self.SetSizer(sizer) self.SetSize( wx.Size(int(scale_dx * SystemMetrics().screen_width), int(scale_dy * SystemMetrics().screen_height)) ) # Position and show the dialog: position_window(self, parent=parent) self.Show()
def __init__(self, dock_control, use_mouse=False, **traits): super(DockWindowShell, self).__init__(**traits) old_control = dock_control.control parent = wx.GetTopLevelParent(old_control) while True: next_parent = parent.GetParent() if next_parent is None: break parent = next_parent self.control = shell = wx.Frame( parent, -1, dock_control.name, style=wx.DEFAULT_FRAME_STYLE | wx.FRAME_FLOAT_ON_PARENT | wx.FRAME_NO_TASKBAR, ) shell.SetIcon(FrameIcon.create_icon()) shell.SetBackgroundColour(SystemMetrics().dialog_background_color) shell.Bind(wx.EVT_CLOSE, self._on_close) theme = dock_control.theme dw = DockWindow(shell, auto_close=True, theme=theme) dw.trait_set(style="tab") self._dock_window = dw sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(dw.control, 1, wx.EXPAND) shell.SetSizer(sizer) if use_mouse: x, y = wx.GetMousePosition() else: x, y = old_control.Sizer.GetPosition().Get() x, y = old_control.GetParent().Window.ClientToScreen(x, y) dx, dy = old_control.GetSize().Get() tis = theme.tab.image_slice tc = theme.tab.content tdy = theme.tab_active.image_slice.dy dx += tis.xleft + tc.left + tis.xright + tc.right dy += tis.xtop + tc.top + tis.xbottom + tc.bottom + tdy self.add_control(dock_control) # Set the correct window size and position, accounting for the tab size # and window borders: shell.SetSize(x, y, dx, dy) cdx, cdy = shell.GetClientSize().Get() ex_dx = dx - cdx ex_dy = dy - cdy shell.SetSize( x - (ex_dx // 2) - tis.xleft - tc.left, y - ex_dy + (ex_dx // 2) - tdy - tis.xtop - tc.top, dx + ex_dx, dy + ex_dy, ) shell.Show()
def position_window(window, width=None, height=None, parent=None): """Positions a window on the screen with a specified width and height so that the window completely fits on the screen if possible. """ dx, dy = window.GetSize() width = width or dx height = height or dy if parent is None: parent = window._parent if parent is None: # Center the popup on the screen: window.SetSize( (SystemMetrics().screen_width - width) // 2, (SystemMetrics().screen_height - height) // 2, width, height, ) return # Calculate the desired size of the popup control: if isinstance(parent, wx.Window): x, y = parent.ClientToScreen(0, 0) parent_dx, parent_dy = parent.GetSize() else: # Special case of parent being a screen position and size tuple (used # to pop-up a dialog for a table cell): x, y, parent_dx, parent_dy = parent adjacent = getattr(window, "_kind", "popup") == "popup" width = min(max(parent_dx, width), SystemMetrics().screen_width) height = min(height, SystemMetrics().screen_height) closest = find_closest_display(x, y) if adjacent: y += parent_dy x, y, dx, dy = get_position_for_display(x, y, width, height, closest) window.SetSize(x, y, dx, dy)
def _position_control(self): """Initializes the popup control's initial position and size.""" # Calculate the desired size of the popup control: px, cy = self.control.ClientToScreen(0, 0) cdx, cdy = self.control.GetSize() pdx, pdy = self.popup.GetSize() pdx, pdy = max(pdx, cdx, self.width), max(pdy, self.height) # Calculate the best position and size for the pop-up: py = cy + cdy if (py + pdy) > SystemMetrics().screen_height: if (cy - pdy) < 0: bottom = SystemMetrics().screen_height - py if cy > bottom: py, pdy = 0, cy else: pdy = bottom else: py = cy - pdy # Finally, position the popup control: self.popup.SetSize(px, py, pdx, pdy)
def position_window(window, width=None, height=None, parent=None): """Positions a window on the screen with a specified width and height so that the window completely fits on the screen if possible. """ # Get the available geometry of the screen containing the window. system_metrics = SystemMetrics() screen_dx = system_metrics.screen_width screen_dy = system_metrics.screen_height # Use the frame geometry even though it is very unlikely that the X11 frame # exists at this point. fgeom = window.frameGeometry() width = width or fgeom.width() height = height or fgeom.height() if parent is None: parent = window._parent if parent is None: # Center the popup on the screen. window.move((screen_dx - width) // 2, (screen_dy - height) // 2) return # Calculate the desired size of the popup control: if isinstance(parent, QtGui.QWidget): gpos = parent.mapToGlobal(QtCore.QPoint()) x = gpos.x() y = gpos.y() cdx = parent.width() cdy = parent.height() # Get the frame height of the parent and assume that the window will # have a similar frame. Note that we would really like the height of # just the top of the frame. pw = parent.window() fheight = pw.frameGeometry().height() - pw.height() else: # Special case of parent being a screen position and size tuple (used # to pop-up a dialog for a table cell): x, y, cdx, cdy = parent fheight = 0 x -= (width - cdx) / 2 y += cdy + fheight # Position the window (making sure it will fit on the screen). window.move( max(0, min(x, screen_dx - width)), max(0, min(y, screen_dy - height)) )
def __init__(self, parent, wid=-1, pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.FULL_REPAINT_ON_RESIZE, **traits): super(DockWindow, self).__init__(**traits) # Create the actual window: self.control = control = wx.Window(parent, wid, pos, size, style) control.owner = self # Set up the 'paint' event handler: control.Bind(wx.EVT_PAINT, self._paint) control.Bind(wx.EVT_SIZE, self._size) # Set up mouse event handlers: control.Bind(wx.EVT_LEFT_DOWN, self._left_down) control.Bind(wx.EVT_LEFT_UP, self._left_up) control.Bind(wx.EVT_LEFT_DCLICK, self._left_dclick) control.Bind(wx.EVT_RIGHT_DOWN, self._right_down) control.Bind(wx.EVT_RIGHT_UP, self.right_up) control.Bind(wx.EVT_MOTION, self._mouse_move) control.Bind(wx.EVT_LEAVE_WINDOW, self._mouse_leave) control.SetDropTarget(PythonDropTarget(self)) # Initialize the window background color: if self.theme.use_theme_color: color = self.theme.tab.image_slice.bg_color else: color = SystemMetrics().dialog_background_color color = wx.Colour(int(color[0] * 255), int(color[1] * 255), int(color[2] * 255)) self.control.SetBackgroundColour(color)
def init(self, ui, parent, style): self.is_modal = style == self.MODAL window_style = 0 view = ui.view if view.resizable: window_style |= wx.RESIZE_BORDER title = view.title if title == "": title = DefaultTitle history = ui.history window = ui.control if window is not None: if history is not None: history.observe( self._on_undoable, "undoable", remove=True, dispatch="ui" ) history.observe( self._on_redoable, "redoable", remove=True, dispatch="ui" ) history.observe( self._on_revertable, "undoable", remove=True, dispatch="ui" ) window.SetSizer(None) ui.reset() else: self.ui = ui if style == self.MODAL: if view.resizable: window_style |= wx.MAXIMIZE_BOX | wx.MINIMIZE_BOX window = wx.Dialog( parent, -1, title, style=window_style | wx.DEFAULT_DIALOG_STYLE, ) elif style == self.NONMODAL: if parent is not None: window_style |= ( wx.FRAME_FLOAT_ON_PARENT | wx.FRAME_NO_TASKBAR ) window = wx.Frame( parent, -1, title, style=window_style | (wx.DEFAULT_FRAME_STYLE & (~wx.RESIZE_BORDER)), ) else: if window_style == 0: window_style = wx.SIMPLE_BORDER if parent is not None: window_style |= ( wx.FRAME_FLOAT_ON_PARENT | wx.FRAME_NO_TASKBAR ) if isinstance(parent, tuple): window = wx.Frame(None, -1, "", style=window_style) window._control_region = parent else: window = wx.Frame(parent, -1, "", style=window_style) window._kind = ui.view.kind self._monitor = MouseMonitor(ui) # Set the correct default window background color: window.SetBackgroundColour(WindowColor) self.control = window window.Bind(wx.EVT_CLOSE, self._on_close_page) window.Bind(wx.EVT_CHAR, self._on_key) self.set_icon(view.icon) buttons = [self.coerce_button(button) for button in view.buttons] nbuttons = len(buttons) no_buttons = (nbuttons == 1) and self.is_button(buttons[0], "") has_buttons = (not no_buttons) and ( (nbuttons > 0) or view.undo or view.revert or view.ok or view.cancel ) if has_buttons or (view.menubar is not None): if history is None: history = UndoHistory() else: history = None ui.history = history # Create the actual trait sheet panel and imbed it in a scrollable # window (if requested): sw_sizer = wx.BoxSizer(wx.VERTICAL) if ui.scrollable: sizer = wx.BoxSizer(wx.VERTICAL) sw = TraitsUIScrolledPanel(window) trait_sheet = panel(ui, sw) sizer.Add(trait_sheet, 1, wx.EXPAND) tsdx, tsdy = trait_sheet.GetSize() sw.SetScrollRate(16, 16) max_dy = (2 * SystemMetrics().screen_height) // 3 sw.SetSizer(sizer) sw.SetSize( wx.Size( tsdx + ((tsdy > max_dy) * scrollbar_dx), min(tsdy, max_dy) ) ) else: sw = panel(ui, window) sw_sizer.Add(sw, 1, wx.EXPAND) sw_sizer.SetMinSize(sw.GetSize()) # Check to see if we need to add any of the special function buttons: if (not no_buttons) and (has_buttons or view.help): sw_sizer.Add(wx.StaticLine(window, -1), 0, wx.EXPAND) b_sizer = wx.BoxSizer(wx.HORIZONTAL) # Convert all button flags to actual button actions if no buttons # were specified in the 'buttons' trait: if nbuttons == 0: if view.undo: self.check_button(buttons, UndoButton) if view.revert: self.check_button(buttons, RevertButton) if view.ok: self.check_button(buttons, OKButton) if view.cancel: self.check_button(buttons, CancelButton) if view.help: self.check_button(buttons, HelpButton) # Create a button for each button action: for raw_button, button in zip(view.buttons, buttons): button = self.coerce_button(button) default = raw_button == view.default_button if self.is_button(button, "Undo"): self.undo = self.add_button( button, b_sizer, self._on_undo, False, default=default ) self.redo = self.add_button( button, b_sizer, self._on_redo, False, "Redo" ) history.observe( self._on_undoable, "undoable", dispatch="ui" ) history.observe( self._on_redoable, "redoable", dispatch="ui" ) if history.can_undo: self._on_undoable(True) if history.can_redo: self._on_redoable(True) elif self.is_button(button, "Revert"): self.revert = self.add_button( button, b_sizer, self._on_revert, False, default=default, ) history.observe( self._on_revertable, "undoable", dispatch="ui" ) if history.can_undo: self._on_revertable(True) elif self.is_button(button, "OK"): self.ok = self.add_button( button, b_sizer, self._on_ok, default=default ) ui.observe(self._on_error, "errors", dispatch="ui") elif self.is_button(button, "Cancel"): self.add_button( button, b_sizer, self._on_cancel, default=default ) elif self.is_button(button, "Help"): self.add_button( button, b_sizer, self._on_help, default=default ) elif not self.is_button(button, ""): self.add_button(button, b_sizer, default=default) sw_sizer.Add(b_sizer, 0, wx.ALIGN_RIGHT | wx.ALL, 5) # Add the menu bar, tool bar and status bar (if any): self.add_menubar() self.add_toolbar() self.add_statusbar() # Lay all of the dialog contents out: window.SetSizer(sw_sizer) window.Fit()
def init(self, ui, parent, style): self.is_modal = style == self.MODAL style = 0 view = ui.view if view.resizable: style |= wx.RESIZE_BORDER title = view.title if title == "": title = DefaultTitle revert = apply = False window = ui.control if window is not None: window.SetSizer(None) ui.reset() if hasattr(self, "revert"): revert = self.revert.IsEnabled() if hasattr(self, "apply"): apply = self.apply.IsEnabled() else: self.ui = ui if self.is_modal: window = wx.Dialog(parent, -1, title, style=style | wx.DEFAULT_DIALOG_STYLE) else: window = wx.Frame( parent, -1, title, style=style | (wx.DEFAULT_FRAME_STYLE & (~wx.RESIZE_BORDER)), ) window.SetBackgroundColour(WindowColor) self.control = window self.set_icon(view.icon) window.Bind(wx.EVT_CLOSE, self._on_close_page) window.Bind(wx.EVT_CHAR, self._on_key) # Create the 'context' copies we will need while editing: context = ui.context ui._context = context ui.context = self._copy_context(context) ui._revert = self._copy_context(context) # Create the actual trait sheet panel and imbed it in a scrollable # window (if requested): sw_sizer = wx.BoxSizer(wx.VERTICAL) if ui.scrollable: sizer = wx.BoxSizer(wx.VERTICAL) sw = TraitsUIScrolledPanel(window) trait_sheet = panel(ui, sw) sizer.Add(trait_sheet, 1, wx.EXPAND | wx.ALL, 4) tsdx, tsdy = trait_sheet.GetSize() tsdx += 8 tsdy += 8 sw.SetScrollRate(16, 16) max_dy = (2 * SystemMetrics().screen_height) // 3 sw.SetSizer(sizer) sw.SetSize( wx.Size(tsdx + ((tsdy > max_dy) * scrollbar_dx), min(tsdy, max_dy))) else: sw = panel(ui, window) sw_sizer.Add(sw, 1, wx.EXPAND) buttons = [self.coerce_button(button) for button in view.buttons] nbuttons = len(buttons) if (nbuttons != 1) or (not self.is_button(buttons[0], "")): # Create the necessary special function buttons: sw_sizer.Add(wx.StaticLine(window, -1), 0, wx.EXPAND) b_sizer = wx.BoxSizer(wx.HORIZONTAL) if nbuttons == 0: if view.apply: self.check_button(buttons, ApplyButton) if view.revert: self.check_button(buttons, RevertButton) if view.ok: self.check_button(buttons, OKButton) if view.cancel: self.check_button(buttons, CancelButton) if view.help: self.check_button(buttons, HelpButton) for raw_button, button in zip(view.buttons, buttons): default = raw_button == view.default_button if self.is_button(button, "Apply"): self.apply = self.add_button(button, b_sizer, self._on_apply, apply, default=default) ui.observe(self._on_applyable, "modified", dispatch="ui") elif self.is_button(button, "Revert"): self.revert = self.add_button( button, b_sizer, self._on_revert, revert, default=default, ) elif self.is_button(button, "OK"): self.ok = self.add_button(button, b_sizer, self._on_ok, default=default) ui.observe(self._on_error, "errors", dispatch="ui") elif self.is_button(button, "Cancel"): self.add_button(button, b_sizer, self._on_cancel, default=default) elif self.is_button(button, "Help"): self.add_button(button, b_sizer, self._on_help, default=default) elif not self.is_button(button, ""): self.add_button(button, b_sizer, default=default) sw_sizer.Add(b_sizer, 0, wx.ALIGN_RIGHT | wx.ALL, 5) # Add the menu bar, tool bar and status bar (if any): self.add_menubar() self.add_toolbar() self.add_statusbar() # Lay all of the dialog contents out: window.SetSizerAndFit(sw_sizer)
_palette = QtGui.QApplication.palette() # Default dialog title DefaultTitle = "Edit properties" # Color of valid input OKColor = _palette.color(QtGui.QPalette.Base) # Color to highlight input errors ErrorColor = QtGui.QColor(255, 192, 192) # Color for background of read-only fields ReadonlyColor = QtGui.QColor(244, 243, 238) # Color for background of fields where objects can be dropped DropColor = QtGui.QColor(215, 242, 255) # Color for an editable field EditableColor = _palette.color(QtGui.QPalette.Base) # Color for background of windows (like dialog background color) WindowColor = _palette.color(QtGui.QPalette.Window) del _palette #: Screen width screen_dx = SystemMetrics().screen_width #: Screen height screen_dy = SystemMetrics().screen_height
def position(self, ui): """ Positions the associated dialog window on the display. """ view = ui.view window = ui.control # Set up the default position of the window: parent = window.parent() if parent is None: px = 0 py = 0 pdx = SystemMetrics().screen_width pdy = SystemMetrics().screen_height else: pos = parent.pos() if int(parent.windowFlags()) & QtCore.Qt.Window == 0: pos = parent.mapToGlobal(pos) px = pos.x() py = pos.y() pdx = parent.width() pdy = parent.height() # Get the window's prefered size. size_hint = window.sizeHint() # Calculate the correct width and height for the window: cur_width = size_hint.width() cur_height = size_hint.height() width = view.width height = view.height if width < 0.0: width = cur_width elif width <= 1.0: width = int(width * SystemMetrics().screen_width) else: width = int(width) if height < 0.0: height = cur_height elif height <= 1.0: height = int(height * SystemMetrics().screen_height) else: height = int(height) # Calculate the correct position for the window: x = view.x y = view.y if x < -99999.0: x = px + ((pdx - width) // 2) elif x <= -1.0: x = px + pdx - width + int(x) + 1 elif x < 0.0: x = px + pdx - width + int(x * pdx) elif x <= 1.0: x = px + int(x * pdx) else: x = int(x) if y < -99999.0: y = py + ((pdy - height) // 2) elif y <= -1.0: y = py + pdy - height + int(y) + 1 elif x < 0.0: y = py + pdy - height + int(y * pdy) elif y <= 1.0: y = py + int(y * pdy) else: y = int(y) # Position and size the window as requested: layout = window.layout() if layout.sizeConstraint() == QtGui.QLayout.SetFixedSize: layout.setSizeConstraint(QtGui.QLayout.SetDefaultConstraint) window.move(max(0, x), max(0, y)) window.setFixedSize(QtCore.QSize(width, height)) else: window.setGeometry(max(0, x), max(0, y), width, height)
def position(self, ui): """ Positions the associated dialog window on the display. """ view = ui.view window = ui.control # Set up the default position of the window: parent = window.GetParent() if parent is None: px, py = 0, 0 pdx = SystemMetrics().screen_width pdy = SystemMetrics().screen_height else: px, py = parent.GetPosition() pdx, pdy = parent.GetSize() # Calculate the correct width and height for the window: cur_width, cur_height = window.GetSize() width = view.width height = view.height if width < 0.0: width = cur_width elif width <= 1.0: width = int(width * SystemMetrics().screen_width) else: width = int(width) if height < 0.0: height = cur_height elif height <= 1.0: height = int(height * SystemMetrics().screen_height) else: height = int(height) if view.kind in Popups: position_window(window, width, height) return # Calculate the correct position for the window: x = view.x y = view.y if x < -99999.0: # BH- I think this is the case when there is a parent # so this logic tries to place it in the middle of the parent # if possible, otherwise tries an offset from the parent x = px + (pdx - width) // 2 if x < 0: x = px + 20 elif x <= -1.0: x = px + pdx - width + int(x) + 1 elif x < 0.0: x = px + pdx - width + int(x * pdx) elif x <= 1.0: x = px + int(x * pdx) else: x = int(x) if y < -99999.0: # BH- I think this is the case when there is a parent # so this logic tries to place it in the middle of the parent # if possible, otherwise tries an offset from the parent y = py + (pdy - height) // 2 if y < 0: y = py + 20 elif y <= -1.0: y = py + pdy - height + int(y) + 1 elif x < 0.0: y = py + pdy - height + int(y * pdy) elif y <= 1.0: y = py + int(y * pdy) else: y = int(y) # make sure the position is on the visible screen, maybe # the desktop had been resized? x = min(x, wx.DisplaySize()[0]) y = min(y, wx.DisplaySize()[1]) # Position and size the window as requested: window.SetSize(max(0, x), max(0, y), width, height)