From: The D. <the...@bl...> - 2005-07-22 20:19:27
|
This is something I worked up today, to solve some problems. I decided to make it a full demonstration, which I'd like to add to the set of example files distributed with wxLua. There might be things that need changing or improving first, but it works well in Windows, and I avoided things that looked like they might not work elsewhere. It certainly needs testing. :) You'll need word-wrap off to see this clearly, as I put comments in a second column. If the mailing system screws it up, please let me know some other way to make it available. Does the list accept attachments? Crow. ------------------------------------------------------------------------------------------------------------------------------------------ -- Extended ToolBar example, by Lostgallifreyan. 22/07/05. -- This overrides standard behaviour but emulates it exactly while extending it to allow very versatile controls to be got from a very small number of tools. -- The purpose is to allow motor memory as well as visual memory to play a big part in program control, as this is faster amd easier to remember with practise. -- It reduces the clutter of controls and event handlers that would be needed if dedicated controls had to be used for everything, and allows useful grouping. local FS=wx.wxDEFAULT_FRAME_STYLE-wx.wxMAXIMIZE_BOX-wx.wxRESIZE_BORDER -- Set Frame Style for fixed size; reduced border saves space. FRAME=wx.wxFrame(wx.wxNull,-1,"Tools",wx.wxPoint(0,0),wx.wxSize(140,140),FS) -- Main GUI frame, supports menus and all good stuffs. PANEL=wx.wxPanel(FRAME,-1) -- Dialog-like control/surface in a Frame. TBAR=wx.wxToolBar(PANEL,wx.wxID_TOOLBAR,wx.wxPoint(-1,-1),wx.wxSize(-1,-1)) -- Toolbar. Size and position are best set after it is prepared. local BMP_N=wx.wxBitmapFromFile("bitmaps/new.bmp",wx.wxBITMAP_TYPE_BMP) -- Prepare the toolbar button bitmaps. local BMP_O=wx.wxBitmapFromFile("bitmaps/open.bmp",wx.wxBITMAP_TYPE_BMP) local BMP_S=wx.wxBitmapFromFile("bitmaps/save.bmp",wx.wxBITMAP_TYPE_BMP) TBAR:AddTool(wx.wxID_NEW,BMP_N,wx.wxNullBitmap,wx.FALSE,wx.wxNull) -- Add the prepared tools to the toolbar. TBAR:AddTool(wx.wxID_OPEN,BMP_O,wx.wxNullBitmap,wx.FALSE,wx.wxNull) TBAR:AddTool(wx.wxID_SAVE,BMP_S,wx.wxNullBitmap,wx.FALSE,wx.wxNull) TBAR:Realize() TBAR:Move(-1,-2) TBAR:SetSize(140,-1) -- Render the toolbar, setting size and position for best display. TEXT="Click a tool button.\nOr try double-click. :)\n" -- Set some initial descriptive text shown when the program is run. TEXT=TEXT.."Try it while holding\nCtrl, or Shift, or Alt,\n" TEXT=TEXT.."or any mix of them..." TEXT=wx.wxStaticText(PANEL,-1,TEXT,wx.wxPoint(0,32),wx.wxSize(140,80)) TOOL={"Open",0,"New","Save"} -- Table of names for tools used in this example. '0' is unused. MCLK={"Left Click","Right Click","Left Dbl-Click","Right Dbl-Click"} -- Table of names for Mouse Click types, used in text display. IDB=0 -- ID Buffer, used in detection of properly applied mouse clicks. function ReadTool(EV,MC) local ID=FindTool(EV) -- Get the toolbar button's ID, which is how the tool is indexed. if (ID~=IDB) then return end IDB=0 -- If button down/up were not on same tool, do nothing. Reset IDB. if ID then ID="Tool Used: "..TOOL[ID-4999]..", ID="..ID.."\n\n" -- Set display text describing the tool used, or the toolbar space. else ID="Tool Used: Toolbar space.\n\n" end MC="Click Type: "..MCLK[MC].."\n\n" -- Set display text describing the type of mouse click applied. local KEYS="Keys Held: " -- Set display text describing any combination of keys held. if (EV:ControlDown()) then KEYS=KEYS.." Control" end if (EV:ShiftDown()) then KEYS=KEYS.." Shift" end if (EV:AltDown()) then KEYS=KEYS.." Alt" end TEXT:SetLabel(ID..MC..KEYS) -- Render the decriptions as a single block of multi-line text. end function FindTool(Z) Z=TBAR:FindToolForPosition(Z:GetX(),Z:GetY()) -- When toolbar (or tool) is clicked, look for tool where clicked. if Z then Z=Z:GetId() end return Z -- If tool was found, return it's ID, or otherwise return nil. end TBAR:ConnectEvent(-1,wx.wxEVT_LEFT_DOWN, -- When any part of the toolbar is left-clicked, look for a tool. function(EV) IDB=FindTool(EV) EV:Skip() end -- Use Skip() to avoid interference with native left click handler. ) TBAR:ConnectEvent(-1,wx.wxEVT_RIGHT_DOWN, -- When any part of the toolbar is right-clicked, look for a tool. function(EV) IDB=FindTool(EV) end ) TBAR:ConnectEvent(-1,wx.wxEVT_LEFT_UP, -- When a left click on the toolbar is released, call ReadTool(). function(EV) ReadTool(EV,1) EV:Skip() end -- Use Skip() to avoid interference with native left click handler. ) TBAR:ConnectEvent(-1,wx.wxEVT_RIGHT_UP, -- When a right click on the toolbar is released, call ReadTool(). function(EV) ReadTool(EV,2) end ) TBAR:ConnectEvent(-1,wx.wxEVT_LEFT_DCLICK, -- When the toolbar is left double-clicked, call ReadTool(). function(EV) IDB=FindTool(EV) ReadTool(EV,3) end -- Set IDB for current tool, so that ReadTool() accepts the event. ) TBAR:ConnectEvent(-1,wx.wxEVT_RIGHT_DCLICK, -- When the toolbar is right double-clicked, call ReadTool(). function(EV) IDB=FindTool(EV) ReadTool(EV,4) end -- Set IDB for current tool, so that ReadTool() accepts the event. ) FRAME:Centre() FRAME:Show(wx.TRUE) -- Render the program window, centred in the screen. ------------------------------------------------------------------------------------------------------------------------------------------ |