From: John L. <jr...@us...> - 2007-02-27 05:10:34
|
Update of /cvsroot/wxlua/wxLua/samples In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv32133/wxLua/samples Modified Files: scribble.wx.lua Log Message: add a toolbar, show off some dialogs to choose colours and line widths Index: scribble.wx.lua =================================================================== RCS file: /cvsroot/wxlua/wxLua/samples/scribble.wx.lua,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** scribble.wx.lua 26 Feb 2007 05:17:30 -0000 1.14 --- scribble.wx.lua 27 Feb 2007 05:10:29 -0000 1.15 *************** *** 15,20 **** mouseDown = false -- left mouse button is down pointsList = {} -- list of the points added to the drawing ! -- pointsList[segment] = {penColour = index, [n] = {x = x_pos, y = x_pos}} ! itemColour = 1 -- current pen colour isModified = false -- has the drawing been modified redrawRequired = true -- redraw the image --- 15,21 ---- mouseDown = false -- left mouse button is down pointsList = {} -- list of the points added to the drawing ! -- pointsList[segment] = ! -- { pen = {colour = {r, g, b}, width = 1, style = N}, ! -- [n] = {x = x_pos, y = x_pos} } isModified = false -- has the drawing been modified redrawRequired = true -- redraw the image *************** *** 22,33 **** fileName = "" -- filename to save to ID_SAVEBITMAP = wx.wxID_HIGHEST + 1 - penColours = { "Red", "Orange", "Yellow", "Green", "Blue", "Purple", "Black", "Grey" } - pens = {} screenWidth, screenHeight = wx.wxDisplaySize() bitmap = wx.wxEmptyBitmap(screenWidth, screenHeight) ! for n = 1, #penColours do ! table.insert(pens, wx.wxPenFromColourName(penColours[n], 3, wx.wxSOLID)) end --- 23,87 ---- fileName = "" -- filename to save to ID_SAVEBITMAP = wx.wxID_HIGHEST + 1 + ID_PENCOLOUR = wx.wxID_HIGHEST + 2 + ID_PENWIDTH = wx.wxID_HIGHEST + 3 + ID_PENSTYLE = wx.wxID_HIGHEST + 4 + ID_PENWIDTH_SPINCTRL = wx.wxID_HIGHEST + 5 + + currentPen = wx.wxRED_PEN; currentPen:SetWidth(3) + penStyles = { wx.wxSOLID, wx.wxDOT, wx.wxLONG_DASH, wx.wxSHORT_DASH, + wx.wxDOT_DASH, wx.wxBDIAGONAL_HATCH, wx.wxCROSSDIAG_HATCH, + wx.wxFDIAGONAL_HATCH, wx.wxCROSS_HATCH, wx.wxHORIZONTAL_HATCH, + wx.wxVERTICAL_HATCH } + penStyleNames = { "Solid style", "Dotted style", "Long dashed style", "Short dashed style", + "Dot and dash style", "Backward diagonal hatch", "Cross-diagonal hatch", + "Forward diagonal hatch", "Cross hatch", "Horizontal hatch", + "Vertical hatch" } screenWidth, screenHeight = wx.wxDisplaySize() bitmap = wx.wxEmptyBitmap(screenWidth, screenHeight) ! -- --------------------------------------------------------------------------- ! -- Pen to table and back functions ! -- --------------------------------------------------------------------------- ! function PenToTable(pen) ! local c = pen:GetColour() ! local t = { colour = { c:Red(), c:Green(), c:Blue() }, width = pen:GetWidth(), style = pen:GetStyle() } ! c:Delete() ! return t ! end ! ! function TableToPen(penTable) ! local c = wx.wxColour(unpack(penTable.colour)) ! local pen = wx.wxPen(c, penTable.width, penTable.style) ! c:Delete() ! return pen ! end ! ! -- --------------------------------------------------------------------------- ! -- Drawing functions ! -- --------------------------------------------------------------------------- ! function CreateToolBarColourBitmap(size) ! local w, h = size:GetWidth(), size:GetHeight() ! local bmp = wx.wxEmptyBitmap(w, h) ! local memDC = wx.wxMemoryDC() ! memDC:SelectObject(bmp) -- select our bitmap to draw into ! ! local colour = currentPen:GetColour() ! local brush = wx.wxBrush(colour, wx.wxSOLID) ! memDC:SetBrush(brush) ! memDC:DrawRectangle(0, 0, w, h) ! memDC:SetBrush(wx.wxRED_BRUSH) ! memDC:DrawRectangle(0, 0, w/3, h) ! memDC:SetBrush(wx.wxGREEN_BRUSH) ! memDC:DrawRectangle(w/3, 0, w/3, h) ! memDC:SetBrush(wx.wxBLUE_BRUSH) ! memDC:DrawRectangle(2*math.floor(w/3), 0, w/3, h) ! ! memDC:SelectObject(wx.wxNullBitmap) -- always release bitmap ! memDC:Delete() ! brush:Delete() ! colour:Delete() ! ! return bmp end *************** *** 42,46 **** for list_index = start_index, #pointsList do local listValue = pointsList[list_index] ! drawDC:SetPen(pens[listValue.penColour]) local point = listValue[1] --- 96,102 ---- for list_index = start_index, #pointsList do local listValue = pointsList[list_index] ! local pen = TableToPen(listValue.pen) ! drawDC:SetPen(pen) ! pen:Delete() local point = listValue[1] *************** *** 62,66 **** if #listValue < 2 then return end ! drawDC:SetPen(pens[listValue.penColour]) local pt1 = listValue[#listValue-1] local pt2 = listValue[#listValue] --- 118,125 ---- 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] *************** *** 96,108 **** end function OnLeftDown(event) ! local pointItem = {penColour = itemColour, {x = event:GetX(), y = event:GetY()}} table.insert(pointsList, pointItem) - itemColour = itemColour + 1 - if penColours[itemColour] == nil then -- have colours wrap around - itemColour = 1 - end - if (not panel:HasCapture()) then panel:CaptureMouse() end mouseDown = true --- 155,166 ---- end + -- --------------------------------------------------------------------------- + -- Mouse functions + -- --------------------------------------------------------------------------- + function OnLeftDown(event) ! local pointItem = {pen = PenToTable(currentPen), {x = event:GetX(), y = event:GetY()}} table.insert(pointsList, pointItem) if (not panel:HasCapture()) then panel:CaptureMouse() end mouseDown = true *************** *** 125,134 **** panel:Refresh() end - - frame:SetStatusText(penColours[itemColour], 1) end function OnMotion(event) ! frame:SetStatusText(string.format("%d, %d", event:GetX(), event:GetY()), 2) if event:LeftIsDown() then --- 183,190 ---- panel:Refresh() end end function OnMotion(event) ! frame:SetStatusText(string.format("%d, %d", event:GetX(), event:GetY()), 1) if event:LeftIsDown() then *************** *** 148,151 **** --- 204,211 ---- end + -- --------------------------------------------------------------------------- + -- File functions + -- --------------------------------------------------------------------------- + function QuerySaveChanges() local dialog = wx.wxMessageDialog( frame, *************** *** 211,215 **** "", "", ! "Scribble files(*.scribble)|*.scribble", wx.wxOPEN + wx.wxFILE_MUST_EXIST) local result = false --- 271,275 ---- "", "", ! "Scribble files(*.scribble)|*.scribble|All files(*)|*", wx.wxOPEN + wx.wxFILE_MUST_EXIST) local result = false *************** *** 227,234 **** function SaveAs() local fileDialog = wx.wxFileDialog(frame, ! "Save wxLua scribble file as", "", "", ! "Scribble files(*.scribble)|*.scribble", wx.wxSAVE + wx.wxOVERWRITE_PROMPT) local result = false --- 287,294 ---- function SaveAs() local fileDialog = wx.wxFileDialog(frame, ! "Save wxLua scribble file", "", "", ! "Scribble files(*.scribble)|*.scribble|All files(*)|*", wx.wxSAVE + wx.wxOVERWRITE_PROMPT) local result = false *************** *** 254,257 **** --- 314,320 ---- end + -- --------------------------------------------------------------------------- + -- The main program + -- --------------------------------------------------------------------------- function main() *************** *** 260,275 **** wx.wxDEFAULT_FRAME_STYLE ) -- Create the menubar local fileMenu = wx.wxMenu() ! fileMenu:Append(wx.wxID_NEW, "&New", "Begin a new drawing") ! fileMenu:Append(wx.wxID_OPEN, "&Open...", "Open an existing drawing") fileMenu:AppendSeparator() ! fileMenu:Append(wx.wxID_SAVE, "&Save", "Save the drawing lines") ! fileMenu:Append(wx.wxID_SAVEAS, "Save &as...", "Save the drawing lines to a new file") ! fileMenu:Append(ID_SAVEBITMAP, "Save &bitmap...", "Save the drawing as a bitmap file") fileMenu:AppendSeparator() ! fileMenu:Append(wx.wxID_EXIT, "E&xit", "Quit the program") local editMenu = wx.wxMenu() editMenu:Append(wx.wxID_UNDO, "&Undo\tCtrl-Z", "Undo last drawn segment") --- 323,345 ---- wx.wxDEFAULT_FRAME_STYLE ) + -- ----------------------------------------------------------------------- -- Create the menubar local fileMenu = wx.wxMenu() ! fileMenu:Append(wx.wxID_NEW, "&New\tCtrl+N", "Begin a new drawing") ! fileMenu:Append(wx.wxID_OPEN, "&Open...\tCtrl+O", "Open an existing drawing") fileMenu:AppendSeparator() ! fileMenu:Append(wx.wxID_SAVE, "&Save\tCtrl+S", "Save the drawing lines") ! fileMenu:Append(wx.wxID_SAVEAS, "Save &as...\tAlt+S", "Save the drawing lines to a new file") ! fileMenu:Append(ID_SAVEBITMAP, "Save &bitmap...", "Save the drawing as a bitmap file") fileMenu:AppendSeparator() ! fileMenu:Append(wx.wxID_EXIT, "E&xit\tCtrl+Q", "Quit the program") 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. + --editMenu:Append(ID_PENSTYLE, "Set &Style\tCtrl+Y", "Set style of the pen to draw with") + editMenu:AppendSeparator() editMenu:Append(wx.wxID_UNDO, "&Undo\tCtrl-Z", "Undo last drawn segment") *************** *** 283,292 **** frame:SetMenuBar(menuBar) -- Create the statusbar ! local statusBar = frame:CreateStatusBar(3) local status_width = statusBar:GetTextExtent("88888, 88888") ! frame:SetStatusWidths({ -1, status_width, status_width }) frame:SetStatusText("Welcome to wxLua Scribble.") - frame:SetStatusText(penColours[itemColour], 1) -- Create a wxPanel to draw on, always a good idea, it will fill the frame --- 353,384 ---- frame:SetMenuBar(menuBar) + -- ----------------------------------------------------------------------- + -- Create the toolbar + toolBar = frame:CreateToolBar(wx.wxNO_BORDER + wx.wxTB_FLAT + wx.wxTB_DOCKABLE) + -- note: Ususally the bmp size isn't necessary, but the HELP icon is not the right size in MSW + local toolBmpSize = toolBar:GetToolBitmapSize() + toolBar:AddToolSimple(wx.wxID_NEW, "New", wx.wxArtProvider_GetBitmap(wx.wxART_NORMAL_FILE, wx.wxART_MENU, toolBmpSize), "Create an empty scribble") + toolBar:AddToolSimple(wx.wxID_OPEN, "Open", wx.wxArtProvider_GetBitmap(wx.wxART_FILE_OPEN, wx.wxART_MENU, toolBmpSize), "Open an existing scribble") + toolBar:AddToolSimple(wx.wxID_SAVE, "Save", wx.wxArtProvider_GetBitmap(wx.wxART_FILE_SAVE, wx.wxART_MENU, toolBmpSize), "Save the current scribble") + toolBar:AddToolSimple(wx.wxID_SAVEAS, "Save as", wx.wxArtProvider_GetBitmap(wx.wxART_NEW_DIR, wx.wxART_MENU, toolBmpSize), "Save the current scribble to a new file") + toolBar:AddSeparator() + 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() + + -- ----------------------------------------------------------------------- -- Create the statusbar ! local statusBar = frame:CreateStatusBar(2) local status_width = statusBar:GetTextExtent("88888, 88888") ! frame:SetStatusWidths({ -1, status_width }) frame:SetStatusText("Welcome to wxLua Scribble.") -- Create a wxPanel to draw on, always a good idea, it will fill the frame *************** *** 299,307 **** panel:Connect(wx.wxEVT_MOTION, OnMotion ) ! -- Connect menu events ! frame:Connect(wx.wxID_EXIT, wx.wxEVT_COMMAND_MENU_SELECTED, ! function (event) ! frame:Close(true) ! end ) frame:Connect(wx.wxID_NEW, wx.wxEVT_COMMAND_MENU_SELECTED, --- 391,396 ---- panel:Connect(wx.wxEVT_MOTION, OnMotion ) ! -- ----------------------------------------------------------------------- ! -- File menu events frame:Connect(wx.wxID_NEW, wx.wxEVT_COMMAND_MENU_SELECTED, *************** *** 383,386 **** --- 472,521 ---- end ) + frame:Connect(wx.wxID_EXIT, wx.wxEVT_COMMAND_MENU_SELECTED, + function (event) + frame:Close(true) + end ) + + -- ----------------------------------------------------------------------- + -- 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 + c:Delete() + end ) + + frame:Connect(ID_PENWIDTH, wx.wxEVT_COMMAND_MENU_SELECTED, + function (event) + 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 + end ) + frame:Connect(ID_PENWIDTH_SPINCTRL, wx.wxEVT_COMMAND_SPINCTRL_UPDATED, + function (event) + currentPen:SetWidth(event:GetInt()) + end ) + + frame:Connect(ID_PENSTYLE, wx.wxEVT_COMMAND_MENU_SELECTED, + function (event) + local ret = wx.wxGetSingleChoice("Select pen style", "wxLua Scribble", + penStyleNames, + frame) + for n = 1, #penStyleNames do + if penStyleNames[n] == ret then + currentPen:SetStyle(penStyles[n]) + break + end + end + end ) + frame:Connect(wx.wxID_UNDO, wx.wxEVT_COMMAND_MENU_SELECTED, function (event) *************** *** 397,400 **** --- 532,538 ---- end ) + -- ----------------------------------------------------------------------- + -- Help menu events + frame:Connect(wx.wxID_ABOUT, wx.wxEVT_COMMAND_MENU_SELECTED, function (event) *************** *** 406,409 **** --- 544,549 ---- end ) + -- ----------------------------------------------------------------------- + frame:Connect(wx.wxEVT_CLOSE_WINDOW, function (event) |