#!/usr/bin/python # -*- coding: iso-8859-15 -*- import wx class MyCanvas(wx.Frame): def __init__(self, titre): wx.Frame.__init__(self, parent = None, id = 1, title = titre, size = (1200, 1000)) self.conteneur = wx.Panel(self, 1, size = self.GetClientSize()) # Initialise les différents Sizer vbox = wx.BoxSizer(wx.VERTICAL) self.bordure1 = wx.BoxSizer(wx.VERTICAL) self.Commandes = wx.BoxSizer(wx.VERTICAL) # Création du Widget wxStaticBox self.staticBox1 = wx.StaticBox ( self.conteneur, -1, 'Dessin' ) self.staticBoxSizer1 = wx.StaticBoxSizer ( box = self.staticBox1, orient=wx.VERTICAL ) # Ecran "Map" self.sizer_Map = wx.BoxSizer(orient=wx.VERTICAL) # Création du panel "P_Map" global P_Map P_Map = Interface_Map(self.conteneur) self.sizer_Map.AddSizer(P_Map, 1, border=0, flag=wx.EXPAND) self.staticBoxSizer1.AddSizer(self.sizer_Map, 1, border=0, flag=wx.EXPAND) #----------------------------------------------------------- # Configure the box sizers vbox .Add(self.Commandes) self.bordure1.Add(self.staticBoxSizer1, 1, wx.EXPAND | wx.ALL, 10) self.bouton_ok=wx.Button(self,wx.ID_OK) self.Commandes.Add(self.bouton_ok, flag=wx.ALIGN_RIGHT | wx.ALL, border=10) self.Cellule=wx.TextCtrl(self,-1,"123", style=wx.TE_RIGHT) self.Commandes.Add(self.Cellule) self.bouton_ok.Bind(wx.EVT_BUTTON,P_Map.Id_Noir) # Attach everything self.conteneur.SetSizerAndFit (self.bordure1) vbox.Add(self.conteneur, 1, wx.EXPAND) # Rendu final du sizer self.SetSizer(vbox) self.pdc = wx.PseudoDC() # self.cel=self.Cellule.GetValue() #------------------------------------------------------------------------- class Interface_Map(wx.ScrolledWindow): """Création de l'interface """ #------------------------------------------------------------------------- def __init__(self, conteneur): wx.ScrolledWindow.__init__(self, conteneur) # Map "Dessin" : self.SetBackgroundColour("white") # Couleur 'Blanc' # create a PseudoDC to record our drawing self.pdc = wx.PseudoDC() self.OnAddGrille(self.pdc) # Evenements self.Bind(wx.EVT_PAINT, self.OnPaint) self.Bind(wx.EVT_ERASE_BACKGROUND, lambda x:None) self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseMove) self.Bind(wx.EVT_LEAVE_WINDOW, self._onLeaveWindow) self.Bind(wx.EVT_ENTER_WINDOW, self._onEnterWindow) # vars for handling mouse clicks self.dragid = -1 self.lastpos = (0,0) #********************************************************* def Id_Noir (self,evt): # Recherche de la position position = self.pdc.GetIdBounds(int(fen.Cellule.GetValue())) self.pdc.SetBrush(wx.Brush("black")) self.pdc.FloodFill(position[0], position[1], "white", style=wx.FLOOD_SURFACE) self.RefreshRect(position, False) def OnPaint(self, event): # Create a buffered paint DC. It will create the real # wx.PaintDC and then blit the bitmap to it when dc is # deleted. dc = wx.BufferedPaintDC(self) # use PrepateDC to set position correctly self.PrepareDC(dc) # we need to clear the dc BEFORE calling PrepareDC bg = wx.Brush(self.GetBackgroundColour()) dc.SetBackground(bg) dc.Clear() self.pdc.DrawToDC(dc) #--------------------------------------------------------- def _onLeaveWindow(self, event): self.dragidLeft = 0 self.dragidRight = 0 def _onEnterWindow(self, event): self.dragidLeft = 0 self.dragidRight = 0 def OnMouseMove(self, event): self.cel=fen.Cellule.GetValue() if event.LeftDown(): self.dragidLeft = 1 if event.RightDown(): self.dragidRight = 1 if wx.EVT_MOTION and (self.dragidLeft or self.dragidRight): x,y = event.GetPosition() l = self.pdc.FindObjects(x, y, 1) for id in l: if not self.pdc.GetIdGreyedOut(id): self.dragid = id break if self.dragidLeft: self.pdc.SetBrush(wx.Brush("black")) self.pdc.FloodFill(x, y, "white", style=wx.FLOOD_SURFACE) if self.dragidRight: self.pdc.SetBrush(wx.Brush("white")) self.pdc.FloodFill(x, y, "black", style=wx.FLOOD_SURFACE) r = self.pdc.GetIdBounds(self.dragid) self.RefreshRect(r, False) if event.LeftUp(): self.dragidLeft = 0 self.dragid = -1 if event.RightUp(): self.dragidRight = 0 self.dragid = -1 #--------------------------------------------------------- def OffsetRect(self, r): xView, yView = self.GetViewStart() xDelta, yDelta = self.GetScrollPixelsPerUnit() r.OffsetXY(-(xView*xDelta),-(yView*yDelta)) #--------------------------------------------------------- def OnAddGrille(self,dc): self.pDC = dc self.pDC.SetPen(wx.Pen('#d4d4d4')) self.pDC.BeginDrawing() self.pDC.SetPen(wx.Pen("grey",style=wx.TRANSPARENT)) self.pDC.SetBrush(wx.Brush("white", wx.SOLID)) taille=10 for y in range(100): for x in range(120): posX=x*(taille) posY=y*(taille) id = wx.NewId() self.pDC.SetId(id) self.pDC.DrawRectangle(posX,posY,taille,taille) r = wx.Rect(posX,posY,taille,taille) self.pDC.SetIdBounds(id,r) self.pDC.SetId self.pDC.EndDrawing() return self.pDC class MonApp(wx.App): def OnInit(self): global fen fen = MyCanvas("Création Trame") fen.Show(True) self.SetTopWindow(fen) return True app = MonApp() app.MainLoop()