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_acceptsFirstMouse(bool &acceptsFirstMouse, WX_NSEvent theEvent)
226 acceptsFirstMouse = true; return true;
229 bool wxToolBar::Cocoa_drawRect(const NSRect &rect)
231 wxToolBarToolsList::compatibility_iterator node;
232 for(node = m_tools.GetFirst(); node; node = node->GetNext())
234 wxToolBarTool *tool = static_cast<wxToolBarTool*>(node->GetData());
235 tool->DrawTool(m_cocoaNSView);
237 return wxToolBarBase::Cocoa_drawRect(rect);
240 static const NSSize toolPadding = { 4.0, 4.0 };
242 static NSRect AddToolPadding(NSRect toolRect)
244 toolRect.origin.x -= toolPadding.width;
245 toolRect.size.width += 2.0*toolPadding.width;
246 toolRect.origin.y -= toolPadding.height;
247 toolRect.size.height += 2.0*toolPadding.height;
251 bool wxToolBar::Cocoa_mouseDragged(WX_NSEvent theEvent)
253 if(m_mouseDownTool && [m_cocoaNSView
254 mouse:[m_cocoaNSView convertPoint:[theEvent locationInWindow]
256 inRect:AddToolPadding(m_mouseDownTool->GetFrameRect())])
258 NSButtonCell *buttonCell = m_mouseDownTool->GetNSButtonCell();
262 [buttonCell setHighlighted: YES];
263 if([buttonCell trackMouse: theEvent
264 inRect:AddToolPadding(m_mouseDownTool->GetFrameRect()) ofView:m_cocoaNSView
267 CocoaToolClickEnded();
268 m_mouseDownTool = NULL;
269 wxLogTrace(wxTRACE_COCOA,wxT("Button was clicked after drag!"));
271 [buttonCell setHighlighted: NO];
272 [buttonCell release];
275 return wxToolBarBase::Cocoa_mouseDragged(theEvent);
278 bool wxToolBar::Cocoa_mouseDown(WX_NSEvent theEvent)
280 wxToolBarTool *tool = CocoaFindToolForPosition([m_cocoaNSView convertPoint:[theEvent locationInWindow] fromView:nil]);
283 NSButtonCell *buttonCell = tool->GetNSButtonCell();
287 m_mouseDownTool = tool;
288 [buttonCell setHighlighted: YES];
289 if([buttonCell trackMouse: theEvent
290 inRect:AddToolPadding(tool->GetFrameRect()) ofView:m_cocoaNSView
293 CocoaToolClickEnded();
294 m_mouseDownTool = NULL;
295 wxLogTrace(wxTRACE_COCOA,wxT("Button was clicked!"));
297 [buttonCell setHighlighted: NO];
298 [buttonCell release];
301 return wxToolBarBase::Cocoa_mouseDown(theEvent);
304 bool wxToolBar::Realize()
306 wxAutoNSAutoreleasePool pool;
308 wxToolBarToolsList::compatibility_iterator node;
309 NSSize totalSize = NSZeroSize;
310 // This is for horizontal, TODO: vertical
311 for(node = m_tools.GetFirst(); node; node = node->GetNext())
313 wxToolBarTool *tool = static_cast<wxToolBarTool*>(node->GetData());
314 if(tool->IsControl())
316 totalSize.width = ceil(totalSize.width);
317 wxControl *control = tool->GetControl();
318 wxSize controlSize = control->GetSize();
319 control->SetPosition(wxPoint((wxCoord)totalSize.width,0));
320 totalSize.width += controlSize.x;
321 if(controlSize.y > totalSize.height)
322 totalSize.height = controlSize.y;
324 else if(tool->IsSeparator())
326 totalSize.width += 2.0;
330 NSButtonCell *buttonCell = tool->GetNSButtonCell();
331 NSSize toolSize = [buttonCell cellSize];
332 tool->SetFrameRect(NSMakeRect(totalSize.width+toolPadding.width,toolPadding.height,toolSize.width,toolSize.height));
333 toolSize.width += 2.0*toolPadding.width;
334 toolSize.height += 2.0*toolPadding.height;
335 totalSize.width += toolSize.width;
336 if(toolSize.height > totalSize.height)
337 totalSize.height = toolSize.height;
340 m_bestSize = wxSize((wxCoord)ceil(totalSize.width),(wxCoord)ceil(totalSize.height));
342 m_owningFrame->UpdateFrameNSView();
346 wxSize wxToolBar::DoGetBestSize() const
351 // ----------------------------------------------------------------------------
352 // wxToolBar tools state
353 // ----------------------------------------------------------------------------
355 void wxToolBar::DoEnableTool(wxToolBarToolBase *toolBase, bool enable)
359 void wxToolBar::DoToggleTool( wxToolBarToolBase *toolBase, bool toggle )
363 void wxToolBar::DoSetToggle(wxToolBarToolBase * WXUNUSED(tool),
364 bool WXUNUSED(toggle))
368 // ----------------------------------------------------------------------------
369 // wxToolBar geometry
370 // ----------------------------------------------------------------------------
372 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
377 wxToolBarTool *wxToolBar::CocoaFindToolForPosition(const NSPoint& pos) const
379 wxToolBarToolsList::compatibility_iterator node;
380 for(node = m_tools.GetFirst(); node; node = node->GetNext())
382 wxToolBarTool *tool = static_cast<wxToolBarTool*>(node->GetData());
383 if(tool->IsControl())
387 else if(tool->IsSeparator())
392 if([m_cocoaNSView mouse:pos inRect:AddToolPadding(tool->GetFrameRect())])
399 void wxToolBar::SetMargins( int x, int y )
403 void wxToolBar::SetToolSeparation( int separation )
405 m_toolSeparation = separation;
408 void wxToolBar::SetToolShortHelp( int id, const wxString& helpString )
412 // ----------------------------------------------------------------------------
413 // wxToolBar idle handling
414 // ----------------------------------------------------------------------------
416 void wxToolBar::OnInternalIdle()
420 #endif // wxUSE_TOOLBAR_NATIVE