From: John L. <jr...@us...> - 2007-04-19 02:05:12
|
Update of /cvsroot/wxlua/wxLua/samples In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv7933/samples Modified Files: scribble.wx.lua Log Message: Add a custom widget to select colours in the toolbar Index: scribble.wx.lua =================================================================== RCS file: /cvsroot/wxlua/wxLua/samples/scribble.wx.lua,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** scribble.wx.lua 28 Feb 2007 04:28:34 -0000 1.16 --- scribble.wx.lua 19 Apr 2007 02:05:01 -0000 1.17 *************** *** 39,43 **** screenWidth, screenHeight = wx.wxDisplaySize() ! bitmap = wx.wxEmptyBitmap(screenWidth, screenHeight) -- --------------------------------------------------------------------------- --- 39,43 ---- screenWidth, screenHeight = wx.wxDisplaySize() ! bitmap = wx.wxEmptyBitmap(screenWidth, screenHeight) -- --------------------------------------------------------------------------- *************** *** 114,129 **** function DrawLastPoint(drawDC) ! if #pointsList < 1 then return end ! local listValue = pointsList[#pointsList] ! if #listValue < 2 then return end ! ! local pen = TableToPen(listValue.pen) ! drawDC:SetPen(pen) ! pen:Delete() ! ! local pt1 = listValue[#listValue-1] ! local pt2 = listValue[#listValue] ! drawDC:DrawLine(pt1.x, pt1.y, pt2.x, pt2.y) end --- 114,130 ---- function DrawLastPoint(drawDC) ! if #pointsList >= 1 then ! local listValue = pointsList[#pointsList] ! local count = #listValue ! if count > 1 then ! local pen = TableToPen(listValue.pen) ! drawDC:SetPen(pen) ! pen:Delete() ! local pt1 = listValue[count-1] ! local pt2 = listValue[count] ! drawDC:DrawLine(pt1.x, pt1.y, pt2.x, pt2.y) ! end ! end end *************** *** 142,152 **** local dc = wx.wxPaintDC(panel) - if redrawRequired and bitmap and bitmap:Ok() then - DrawBitmap(bitmap) - end - - redrawRequired = false -- reset since we at least tried to redraw it - if bitmap and bitmap:Ok() then dc:DrawBitmap(bitmap, 0, 0, false) end --- 143,152 ---- local dc = wx.wxPaintDC(panel) if bitmap and bitmap:Ok() then + if redrawRequired then + DrawBitmap(bitmap) + redrawRequired = false + end + dc:DrawBitmap(bitmap, 0, 0, false) end *************** *** 336,341 **** local editMenu = wx.wxMenu() ! editMenu:Append(ID_PENCOLOUR, "Set &Color\tCtrl+R", "Set the color of the pen to draw with") ! editMenu:Append(ID_PENWIDTH, "Set &Width\tCtrl+T", "Set width of the pen to draw with") -- Styles really only work for long lines, when you change direction the styles -- blur into each other and just look like a solid line. --- 336,341 ---- local editMenu = wx.wxMenu() ! editMenu:Append(ID_PENCOLOUR, "Set pen &Color\tCtrl+R", "Set the color of the pen to draw with") ! editMenu:Append(ID_PENWIDTH, "Set pen &Width\tCtrl+T", "Set width of the pen to draw with") -- Styles really only work for long lines, when you change direction the styles -- blur into each other and just look like a solid line. *************** *** 365,376 **** toolBar:AddToolSimple(wx.wxID_UNDO, "Undo", wx.wxArtProvider_GetBitmap(wx.wxART_UNDO, wx.wxART_MENU, toolBmpSize), "Undo last line drawn") toolBar:AddSeparator() ! local bmp = CreateToolBarColourBitmap(toolBmpSize) ! toolBar:AddToolSimple(ID_PENCOLOUR, "Color", bmp, "Set pen color") ! bmp:Delete() penWidthSpinCtrl = wx.wxSpinCtrl(toolBar, ID_PENWIDTH_SPINCTRL, 3, wx.wxDefaultPosition, wx.wxDefaultSize, wx.wxSP_ARROW_KEYS, 1, 100, 3) penWidthSpinCtrl:SetToolTip("Set pen width in pixels") toolBar:AddControl(penWidthSpinCtrl) toolBar:Realize() --- 365,433 ---- toolBar:AddToolSimple(wx.wxID_UNDO, "Undo", wx.wxArtProvider_GetBitmap(wx.wxART_UNDO, wx.wxART_MENU, toolBmpSize), "Undo last line drawn") toolBar:AddSeparator() ! penWidthSpinCtrl = wx.wxSpinCtrl(toolBar, ID_PENWIDTH_SPINCTRL, 3, wx.wxDefaultPosition, wx.wxDefaultSize, wx.wxSP_ARROW_KEYS, 1, 100, 3) + local w, h = penWidthSpinCtrl:GetSize() + penWidthSpinCtrl:SetSize(3*h, -1) penWidthSpinCtrl:SetToolTip("Set pen width in pixels") toolBar:AddControl(penWidthSpinCtrl) + toolBar:AddSeparator() + + local bmp = CreateToolBarColourBitmap(toolBmpSize) + toolBar:AddToolSimple(ID_PENCOLOUR, "Color", bmp, "Choose pen color") + bmp:Delete() + -- Create a custom control to choose some common colours. + local colourWin_height = math.floor(h/2)*2 -- round to be divisible by two + local colourWin = wx.wxControl(toolBar, wx.wxID_ANY, + wx.wxDefaultPosition, wx.wxSize(4*colourWin_height, colourWin_height), + wx.wxBORDER_NONE) + local colourWinColours = { + "black", "grey", "brown", "red", "orange", "green", "blue", "violet", + "white", "light grey", "tan", "pink", "yellow", "turquoise", "sky blue", "maroon" + } + local colourWinBmp = wx.wxEmptyBitmap(4*colourWin_height, colourWin_height) + do + local memDC = wx.wxMemoryDC() + memDC:SelectObject(colourWinBmp) + memDC:SetPen(wx.wxBLACK_PEN) + local w, h = colourWin:GetClientSize() + local w2 = math.floor(w/8) + local h2 = math.floor(h/2) + + for j = 1, 2 do + for i = 1, 8 do + local colour = wx.wxNamedColour(colourWinColours[i + 8*(j-1)]) + local brush = wx.wxBrush(colour, wx.wxSOLID) + memDC:SetBrush(brush) + memDC:DrawRectangle(w2*(i-1), h2*(j-1), w2, h2) + brush:Delete() + colour:Delete() + end + end + memDC:SelectObject(wx.wxNullBitmap) + memDC:Delete() + end + + colourWin:Connect(wx.wxEVT_ERASE_BACKGROUND, + function(event) + local dc = wx.wxClientDC(colourWin) + dc:DrawBitmap(colourWinBmp, 0, 0, false) -- this is our background + dc:Delete() + end) + colourWin:Connect(wx.wxEVT_LEFT_DOWN, + function(event) + local x, y = event:GetPositionXY() + local w, h = colourWin:GetClientSize() + local i = math.floor(8*x/w)+1 + 8*math.floor(2*y/h) + if colourWinColours[i] then + local c = wx.wxNamedColour(colourWinColours[i]) + currentPen:SetColour(c) + c:Delete() + end + end) + toolBar:AddControl(colourWin) + + -- once all the tools are added, layout all the tools toolBar:Realize() *************** *** 478,490 **** -- ----------------------------------------------------------------------- ! -- End menu events frame:Connect(ID_PENCOLOUR, wx.wxEVT_COMMAND_MENU_SELECTED, function (event) local oldColour = currentPen:GetColour() ! local c = wx.wxGetColourFromUser(frame, currentPen:GetColour(), "wxLua Scribble") oldColour:Delete() ! if c:Ok() then currentPen:SetColour(c) end --- 535,547 ---- -- ----------------------------------------------------------------------- ! -- Edit menu events frame:Connect(ID_PENCOLOUR, wx.wxEVT_COMMAND_MENU_SELECTED, function (event) local oldColour = currentPen:GetColour() ! local c = wx.wxGetColourFromUser(frame, oldColour, "wxLua Scribble") oldColour:Delete() ! if c:Ok() then -- returns invalid colour if canceled currentPen:SetColour(c) end *************** *** 496,500 **** local ret = wx.wxGetNumberFromUser("Select pen width in pixels", "Width", "wxLua Scribble", currentPen:GetWidth(), 1, 100, frame) ! if ret > 0 then currentPen:SetWidth(ret) end --- 553,557 ---- local ret = wx.wxGetNumberFromUser("Select pen width in pixels", "Width", "wxLua Scribble", currentPen:GetWidth(), 1, 100, frame) ! if ret > 0 then -- returns -1 if canceled currentPen:SetWidth(ret) end *************** *** 564,568 **** bitmap:Delete() bitmap = nil ! -- ensure the event is passed on so that it is handled event:Skip() end --- 621,625 ---- bitmap:Delete() bitmap = nil ! -- ensure the event is skipped to allow the frame to close event:Skip() end |