1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/toolbar.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 #if wxUSE_TOOLBAR_NATIVE
25 #include "wx/toolbar.h"
30 #include "wx/cocoa/string.h"
31 #include "wx/cocoa/autorelease.h"
33 #import <AppKit/NSView.h>
34 #import <AppKit/NSButtonCell.h>
35 #import <AppKit/NSMatrix.h>
36 #import <AppKit/NSImage.h>
37 #import <AppKit/NSEvent.h>
38 #import <AppKit/NSColor.h>
39 #import <AppKit/NSAttributedString.h>
40 #import <AppKit/NSFont.h>
44 // ========================================================================
46 // ========================================================================
47 class wxToolBarTool : public wxToolBarToolBase
50 wxToolBarTool(wxToolBar *tbar, int toolid, const wxString& label,
51 const wxBitmap& bitmap1, const wxBitmap& bitmap2,
52 wxItemKind kind, wxObject *clientData,
53 const wxString& shortHelpString, const wxString& longHelpString)
54 : wxToolBarToolBase(tbar, toolid, label, bitmap1, bitmap2, kind,
55 clientData, shortHelpString, longHelpString)
61 wxToolBarTool(wxToolBar *tbar, wxControl *control)
62 : wxToolBarToolBase(tbar, control)
68 bool CreateButtonCell();
70 // is this a radio button?
72 // unlike GetKind(), can be called for any kind of tools, not just buttons
73 bool IsRadio() const { return IsButton() && GetKind() == wxITEM_RADIO; }
76 { return m_frameRect; }
77 void SetFrameRect(NSRect frameRect)
78 { m_frameRect = frameRect; }
79 void DrawTool(NSView *nsview);
81 NSButtonCell *GetNSButtonCell()
82 { return m_cocoaNSButtonCell; }
85 NSButtonCell *m_cocoaNSButtonCell;
89 // ========================================================================
91 // ========================================================================
92 void wxToolBarTool::Init()
94 m_cocoaNSButtonCell = NULL;
95 m_frameRect = NSZeroRect;
98 void wxToolBar::CocoaToolClickEnded()
100 wxASSERT(m_mouseDownTool);
101 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, m_mouseDownTool->GetId());
102 InitCommandEvent(event);
106 wxToolBarTool::~wxToolBarTool()
108 [m_cocoaNSButtonCell release];
111 bool wxToolBarTool::CreateButtonCell()
113 wxAutoNSAutoreleasePool pool;
115 NSImage *nsimage = [m_bmpNormal.GetNSImage(true) retain];
116 m_cocoaNSButtonCell = [[NSButtonCell alloc] initTextCell:nil];
117 [m_cocoaNSButtonCell setImage:nsimage];
118 NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:wxNSStringWithWxString(m_label) attributes:[NSDictionary dictionaryWithObject:[NSFont labelFontOfSize:0.0] forKey:NSFontAttributeName]];
119 // [m_cocoaNSButtonCell setTitle:wxNSStringWithWxString(m_label)];
120 [m_cocoaNSButtonCell setAttributedTitle:[attributedTitle autorelease]];
122 // Create an alternate image in the style of NSToolBar
125 NSImage *alternateImage = [[NSImage alloc] initWithSize:[nsimage size]];
126 [alternateImage lockFocus];
127 // Paint the entire image with solid black at 50% transparency
128 NSRect imageRect = NSZeroRect;
129 imageRect.size = [alternateImage size];
130 [[NSColor colorWithCalibratedWhite:0.0 alpha:0.5] set];
131 NSRectFill(imageRect);
132 // Composite the original image with the alternate image
133 [nsimage compositeToPoint:NSZeroPoint operation:NSCompositeDestinationAtop];
134 [alternateImage unlockFocus];
135 [m_cocoaNSButtonCell setAlternateImage:alternateImage];
136 [alternateImage release];
140 NSMutableAttributedString *alternateTitle = [[NSMutableAttributedString alloc] initWithAttributedString:[m_cocoaNSButtonCell attributedTitle]];
141 [alternateTitle applyFontTraits:NSBoldFontMask range:NSMakeRange(0,[alternateTitle length])];
142 [m_cocoaNSButtonCell setAttributedAlternateTitle:alternateTitle];
143 [alternateTitle release];
146 [m_cocoaNSButtonCell setImagePosition:NSImageBelow];
147 // [m_cocoaNSButtonCell setBezeled:NO];
148 [m_cocoaNSButtonCell setButtonType:NSMomentaryChangeButton];
149 [m_cocoaNSButtonCell setBordered:NO];
150 // [m_cocoaNSButtonCell setHighlightsBy:NSContentsCellMask|NSPushInCellMask];
151 // [m_cocoaNSButtonCell setShowsStateBy:NSContentsCellMask|NSPushInCellMask];
155 void wxToolBarTool::DrawTool(NSView *nsview)
157 [m_cocoaNSButtonCell drawWithFrame:m_frameRect inView:nsview];
160 // ========================================================================
162 // ========================================================================
163 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl)
165 //-----------------------------------------------------------------------------
166 // wxToolBar construction
167 //-----------------------------------------------------------------------------
169 void wxToolBar::Init()
171 m_owningFrame = NULL;
172 m_mouseDownTool = NULL;
175 wxToolBar::~wxToolBar()
179 bool wxToolBar::Create( wxWindow *parent,
184 const wxString& name )
186 // Call wxControl::Create so we get a wxNonControlNSControl
187 return wxToolBarBase::Create(parent,winid,pos,size,style,wxDefaultValidator,name);
190 wxToolBarToolBase *wxToolBar::CreateTool(int toolid,
191 const wxString& text,
192 const wxBitmap& bitmap1,
193 const wxBitmap& bitmap2,
195 wxObject *clientData,
196 const wxString& shortHelpString,
197 const wxString& longHelpString)
199 return new wxToolBarTool(this, toolid, text, bitmap1, bitmap2, kind,
200 clientData, shortHelpString, longHelpString);
203 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
205 return new wxToolBarTool(this, control);
208 void wxToolBar::SetWindowStyleFlag( long style )
210 wxToolBarBase::SetWindowStyleFlag(style);
213 bool wxToolBar::DoInsertTool(size_t pos, wxToolBarToolBase *toolBase)
218 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase)
224 bool wxToolBar::Cocoa_drawRect(const NSRect &rect)
226 wxToolBarToolsList::compatibility_iterator node;
227 for(node = m_tools.GetFirst(); node; node = node->GetNext())
229 wxToolBarTool *tool = static_cast<wxToolBarTool*>(node->GetData());
230 tool->DrawTool(m_cocoaNSView);
232 return wxToolBarBase::Cocoa_drawRect(rect);
235 static const NSSize toolPadding = { 4.0, 4.0 };
237 static NSRect AddToolPadding(NSRect toolRect)
239 toolRect.origin.x -= toolPadding.width;
240 toolRect.size.width += 2.0*toolPadding.width;
241 toolRect.origin.y -= toolPadding.height;
242 toolRect.size.height += 2.0*toolPadding.height;
246 bool wxToolBar::Cocoa_mouseDragged(WX_NSEvent theEvent)
248 if(m_mouseDownTool && [m_cocoaNSView
249 mouse:[m_cocoaNSView convertPoint:[theEvent locationInWindow]
251 inRect:AddToolPadding(m_mouseDownTool->GetFrameRect())])
253 NSButtonCell *buttonCell = m_mouseDownTool->GetNSButtonCell();
256 [buttonCell setHighlighted: YES];
257 if([buttonCell trackMouse: theEvent
258 inRect:AddToolPadding(m_mouseDownTool->GetFrameRect()) ofView:m_cocoaNSView
261 CocoaToolClickEnded();
262 m_mouseDownTool = NULL;
263 wxLogTrace(wxTRACE_COCOA,wxT("Button was clicked after drag!"));
265 [buttonCell setHighlighted: NO];
268 return wxToolBarBase::Cocoa_mouseDragged(theEvent);
271 bool wxToolBar::Cocoa_mouseDown(WX_NSEvent theEvent)
273 wxToolBarTool *tool = CocoaFindToolForPosition([m_cocoaNSView convertPoint:[theEvent locationInWindow] fromView:nil]);
276 NSButtonCell *buttonCell = tool->GetNSButtonCell();
279 m_mouseDownTool = tool;
280 [buttonCell setHighlighted: YES];
281 if([buttonCell trackMouse: theEvent
282 inRect:AddToolPadding(tool->GetFrameRect()) ofView:m_cocoaNSView
285 CocoaToolClickEnded();
286 m_mouseDownTool = NULL;
287 wxLogTrace(wxTRACE_COCOA,wxT("Button was clicked!"));
289 [buttonCell setHighlighted: NO];
292 return wxToolBarBase::Cocoa_mouseDown(theEvent);
295 bool wxToolBar::Realize()
297 wxAutoNSAutoreleasePool pool;
299 wxToolBarToolsList::compatibility_iterator node;
300 NSSize totalSize = NSZeroSize;
301 // This is for horizontal, TODO: vertical
302 for(node = m_tools.GetFirst(); node; node = node->GetNext())
304 wxToolBarTool *tool = static_cast<wxToolBarTool*>(node->GetData());
305 if(tool->IsControl())
307 totalSize.width = ceil(totalSize.width);
308 wxControl *control = tool->GetControl();
309 wxSize controlSize = control->GetSize();
310 control->SetPosition(wxPoint((wxCoord)totalSize.width,0));
311 totalSize.width += controlSize.x;
312 if(controlSize.y > totalSize.height)
313 totalSize.height = controlSize.y;
315 else if(tool->IsSeparator())
317 totalSize.width += 2.0;
321 NSButtonCell *buttonCell = tool->GetNSButtonCell();
322 NSSize toolSize = [buttonCell cellSize];
323 tool->SetFrameRect(NSMakeRect(totalSize.width+toolPadding.width,toolPadding.height,toolSize.width,toolSize.height));
324 toolSize.width += 2.0*toolPadding.width;
325 toolSize.height += 2.0*toolPadding.height;
326 totalSize.width += toolSize.width;
327 if(toolSize.height > totalSize.height)
328 totalSize.height = toolSize.height;
331 m_bestSize = wxSize((wxCoord)ceil(totalSize.width),(wxCoord)ceil(totalSize.height));
333 m_owningFrame->UpdateFrameNSView();
337 wxSize wxToolBar::DoGetBestSize() const
342 // ----------------------------------------------------------------------------
343 // wxToolBar tools state
344 // ----------------------------------------------------------------------------
346 void wxToolBar::DoEnableTool(wxToolBarToolBase *toolBase, bool enable)
350 void wxToolBar::DoToggleTool( wxToolBarToolBase *toolBase, bool toggle )
354 void wxToolBar::DoSetToggle(wxToolBarToolBase * WXUNUSED(tool),
355 bool WXUNUSED(toggle))
359 // ----------------------------------------------------------------------------
360 // wxToolBar geometry
361 // ----------------------------------------------------------------------------
363 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
368 wxToolBarTool *wxToolBar::CocoaFindToolForPosition(const NSPoint& pos) const
370 wxToolBarToolsList::compatibility_iterator node;
371 for(node = m_tools.GetFirst(); node; node = node->GetNext())
373 wxToolBarTool *tool = static_cast<wxToolBarTool*>(node->GetData());
374 if(tool->IsControl())
378 else if(tool->IsSeparator())
383 if([m_cocoaNSView mouse:pos inRect:AddToolPadding(tool->GetFrameRect())])
390 void wxToolBar::SetMargins( int x, int y )
394 void wxToolBar::SetToolSeparation( int separation )
396 m_toolSeparation = separation;
399 void wxToolBar::SetToolShortHelp( int id, const wxString& helpString )
403 // ----------------------------------------------------------------------------
404 // wxToolBar idle handling
405 // ----------------------------------------------------------------------------
407 void wxToolBar::OnInternalIdle()
411 #endif // wxUSE_TOOLBAR_NATIVE