Close cell editor

ioprst <[email protected]>
Newsgroups gmane.comp.python.wxpython
Message-ID <[email protected]>
Hello.

How to implement closing your own cell editor when it loses focus? Since 
the built-in wx editors work.

Now my editor closes only if you select another cell in the grid.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import wx
import wx.grid


class GridCellColourEditor(wx.grid.GridCellEditor):

    def __init__(self):
        super().__init__()

    def Create(self, parent, id, evtHandler):
        self._cp = wx.ColourPickerCtrl(parent, id)
        self.SetControl(self._cp)

        if evtHandler:
            self._cp.PushEventHandler(evtHandler)

    def BeginEdit(self, row, col, grid):
        self.startValue = grid.GetTable().GetValue(row, col)
        self._cp.SetColour(self.startValue)
        self._cp.SetFocus()

    def EndEdit(self, row, col, grid, oldval):
        val = self._cp.GetColour().GetAsString(wx.C2S_HTML_SYNTAX)
        if val != oldval:
            return val
        else:
            return None

    def ApplyEdit(self, row, col, grid):
        val = self._cp.GetColour().GetAsString(wx.C2S_HTML_SYNTAX)
        grid.GetTable().SetValue(row, col, val)

    def Reset(self):
        self._cp.SetColour(self.startValue)

    def Clone(self):
        return GridCellColourEditor()

class GridCellColourRenderer(wx.grid.GridCellRenderer):

    def __init__(self):
        super().__init__()

    def Draw(self, grid, attr, dc, rect, row, col, isSelected):
        if grid.IsEnabled():
            bgColour = grid.GetDefaultCellBackgroundColour()
        else:
            bgColour = grid.GetBackgroundColour()

        dc.SetBrush(wx.Brush(bgColour, wx.SOLID))
        dc.SetPen(wx.TRANSPARENT_PEN)
        dc.DrawRectangle(rect)

        colour = grid.GetTable().GetValue(row, col)

        x = rect.x + 3
        y = rect.y + 3
        width = rect.width - 6
        height = rect.height - 6

        dc.SetBrush(wx.Brush(colour, wx.SOLID))
        dc.SetPen(wx.Pen(wx.BLACK))
        dc.DrawRoundedRectangle(x, y, width, height, 3)

    def GetBestSize(self, grid, attr, dc, row, col):
        return attr.GetSize()

    def Clone(self):
        return GridCellColourRenderer()


For test
class Frame(wx.Frame):

    def __init__(self):
        super().__init__(None)

        vbox = wx.BoxSizer(wx.VERTICAL)

        self.grid = wx.grid.Grid(self, size=(100, 50))
        vbox.Add(self.grid, flag=wx.EXPAND)
        self.grid.CreateGrid(1, 2)

        self.grid.SetCellEditor(0, 0, GridCellColourEditor())
        self.grid.SetCellRenderer(0, 0, GridCellColourRenderer())

        self.grid.SetCellEditor(0, 1, wx.grid.GridCellTextEditor())
        self.grid.SetCellRenderer(0, 1, wx.grid.GridCellStringRenderer())

        btn = wx.Button(self, -1, 'For kill focus')
        vbox.Add(btn, 0, wx.ALL, 10)

        self.SetSizer(vbox)

app = wx.App()
frame = Frame()
frame.Show()
app.MainLoop()



-- 
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/wxpython-users/7955221a-77a9-4666-9108-1de2df51fc37%40googlegroups.com.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.