Again, in the attempt of porting code from windows to macOS I have
some trouble.
This time the task was to add to widgets the functionalaty of going
full screen (a splash screen) and, on closing, just going back in the
right position. I achieved that with child frame and reparenting.
However, whil on windows the code work fine, on mac: the widgets is
not displayed (although functionality is still there...try to click on
the screen...); on close the widget is not in the right position (but
if I move the frame the widget is placed correctly).
It seems to me that on mac the SendEventSize does not work properly.
Where I'm doing wrong?
Thanks
<code>
import wx
try:
from wx import glcanvas
haveGLCanvas = True
except ImportError:
haveGLCanvas = False
try:
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
haveOpenGL = True
except ImportError:
haveOpenGL = False
import numpy as np
···
##---------------------------------------
class MySplash(wx.Frame):
def __init__(self, element, parent, id = wx.ID_ANY, title =
wx.EmptyString):
wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition,
wx.DefaultSize)#, wx.CAPTION|wx.CLOSE_BOX|wx.SYSTEM_MENU|
wx.CLIP_CHILDREN)#|wx.MAXIMIZE)
self.m_parentWindow = element.GetParent()
self.m_parentFrame = parent
self.m_oldPos = self.m_parentWindow.GetPosition()
self.m_oldSize = self.m_parentWindow.GetClientSize()
self.splashedelement = element
self.outer_sizer = wx.BoxSizer(wx.VERTICAL)
self.SetBackgroundColour(wx.Colour( 0, 0, 0 ) )
self.outer_sizer.Add(element, 1, wx.ALIGN_CENTER|wx.EXPAND|
wx.SHAPED, 5)
self.SetSizer(self.outer_sizer)
self.outer_sizer.Fit(self)
self.Layout()
self.Maximize()
self.Show(False)
self.Bind(wx.EVT_CLOSE, self.onClose)
def setOldSize(self,size):
self.m_oldSize = size
def setOldPos(self,pos):
#print "passed pos",pos
self.m_oldPos = pos
def getOldSize(self):
return self.m_oldSize
def getOldPos(self):
return self.m_oldPos
def onClose(self, event):
self.splashedelement.Reparent(self.m_parentWindow)
self.Show(False)
self.splashedelement.SetClientSize(self.m_oldSize)
self.splashedelement.SetPosition(self.m_oldPos)
self.m_parentFrame.SendSizeEvent()
##---------------------------------------
class GLPanePixelSel(glcanvas.GLCanvas):
def __init__(self, parent, ID = wx.ID_ANY, pos =
wx.DefaultPosition, size = wx.DefaultSize,
style = 0, name = wx.EmptyString, gl_attr = None):
glcanvas.GLCanvas.__init__(self, parent, ID, pos, size, style
= wx.FULL_REPAINT_ON_RESIZE, name = wx.EmptyString, attribList = None)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.init_fl = False
self.m_imgHight = size[1]
self.m_imgWidth = size[0]
self.m_panHight = 0
self.m_panWidth = 0
self.m_zoomH = 1
self.m_zoomW = 1
self.Bind(wx.EVT_LEFT_UP, self.OnLeftDown)
self.zoomed=[]
self.real=[]
self.contDot=0
self.radius = 10
self.sinR=self.radius*np.sin(np.linspace(0,2*np.pi,20))
self.cosR=self.radius*np.cos(np.linspace(0,2*np.pi,20))
self.splashFrame = MySplash(self, wx.GetTopLevelParent(self))
self.Bind(wx.EVT_RIGHT_DOWN, self.mouseRightClick)
def mouseRightClick(self, event):
self.splashFrame.setOldSize(self.GetClientSize())
self.splashFrame.setOldPos(self.GetPosition())
self.Reparent(self.splashFrame)
self.splashFrame.Show(True)
self.onDraw()
def OnLeftDown(self, event):
pt = event.GetPosition() # position tuple
realpt = (round(pt[0]/self.m_zoomW),round(pt[1]/self.m_zoomH))
self.real.append(realpt)
print pt
self.zoomed.append(pt)
self.contDot+=1
self.Refresh()
def initGL(self):
##openGL required!!
glClearColor(0, 0, 0, 0)
glShadeModel(GL_FLAT)
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
self.init_fl = True
def onDraw(self):
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
glPushMatrix()
glPopMatrix()
glPointSize(10.0)
#########CIRCLES
glColor3f(0.5, 1.0, 0.5)
glLineWidth(2.0)
glEnable(GL_LINE_SMOOTH)
for dot in self.zoomed:
glBegin(GL_LINE_LOOP)
for s,c in zip(self.sinR,self.cosR):
glVertex2f( dot[0]+s, dot[1]+c)
glEnd()
glFlush()
self.SwapBuffers()
def OnPaint(self, event):
dc = wx.PaintDC(self)
if not self.GetContext():
return
if not self.init_fl:
self.initGL()
self.SetCurrent()
self.onDraw()
def OnSize(self, event):
size = self.GetClientSize()
self.m_panWidth,self.m_panHight = size.width, size.height
self.m_zoomH = (1.0*self.m_panHight)/self.m_imgHight
self.m_zoomW = (1.0*self.m_panWidth)/self.m_imgWidth
cont = 0
while cont<self.contDot:
self.zoomed[cont]=(self.real[cont]
[0]*self.m_zoomW,self.real[cont][1]*self.m_zoomH)
cont+=1
if self.GetContext():
self.SetCurrent()
glViewport(0, 0, self.m_panWidth,self.m_panHight)
glMatrixMode (GL_PROJECTION)
glLoadIdentity ()
gluOrtho2D (0.0, self.m_panWidth, self.m_panHight, 0.0)
def OnEraseBackground(self, event):
pass
##--------------------------------
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "Click for mouseposition",
size=(400,300),
style=wx.DEFAULT_FRAME_STYLE |
wx.NO_FULL_REPAINT_ON_RESIZE)
self.panel1=wx.Panel(self,-1)
self.panel1.SetBackgroundColour('Goldenrod')
self.bitmap1=GLPanePixelSel(self.panel1, size=(200,100))
self.bitmap2=GLPanePixelSel(self.panel1, size=(200,100))
sizer1=wx.BoxSizer(wx.HORIZONTAL)
sizer2=wx.BoxSizer(wx.HORIZONTAL)
sizer2.Add(self.bitmap1, 1, wx.ALL|wx.EXPAND|wx.SHAPED|
wx.ALIGN_CENTER, 5)
sizer2.Add(self.bitmap2, 1, wx.ALL|wx.EXPAND|wx.SHAPED|
wx.ALIGN_CENTER, 5)
self.panel1.SetSizer(sizer2)
sizer1.Add(self.panel1,1,wx.EXPAND, 0)
self.SetSizer(sizer1)
sizer1.Fit(self)
self.Layout()
self.Maximize(False)
##--------------------------------
app = wx.PySimpleApp()
frame = MyFrame(None)
frame.Show(True)
app.MainLoop()
</code>