1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/notebook.mm
4 // Author: David Elliott
8 // Copyright: (c) 2004 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
19 #include "wx/notebook.h"
20 #include "wx/imaglist.h"
22 #include "wx/cocoa/autorelease.h"
23 #include "wx/cocoa/string.h"
24 #include "wx/cocoa/objc/objc_uniquifying.h"
26 #import <AppKit/NSTabView.h>
27 #import <AppKit/NSTabViewItem.h>
28 #import <AppKit/NSImage.h>
31 #import <AppKit/NSPasteboard.h>
32 #import <Foundation/NSArray.h>
34 // ========================================================================
35 // WXCTabViewImageItem
36 // ========================================================================
37 @interface WXCTabViewImageItem : NSTabViewItem
43 - (id)initWithIdentifier: (id)identifier;
46 - (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel;
47 - (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect;
50 - (void)setImage:(NSImage*)image;
51 @end // interface WXCTabViewImageItem : NSTabViewItem
52 WX_DECLARE_GET_OBJC_CLASS(WXCTabViewImageItem,NSTabViewItem)
54 @implementation WXCTabViewImageItem : NSTabViewItem
57 return [self initWithIdentifier:nil];
60 - (id)initWithIdentifier: (id)identifier;
63 return [super initWithIdentifier:identifier];
72 - (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel
74 NSSize labelSize = [super sizeOfLabel:shouldTruncateLabel];
77 NSSize imageSize = [m_image size];
79 if(imageSize.height > labelSize.height)
81 imageSize.width *= labelSize.height/imageSize.height;
82 imageSize.height *= labelSize.height/imageSize.height;
83 [m_image setScalesWhenResized:YES];
84 [m_image setSize: imageSize];
86 labelSize.width += imageSize.width;
90 - (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect
94 NSSize imageSize = [m_image size];
95 [m_image compositeToPoint:NSMakePoint(tabRect.origin.x,
96 tabRect.origin.y+imageSize.height)
97 operation:NSCompositeSourceOver];
98 tabRect.size.width -= imageSize.width;
99 tabRect.origin.x += imageSize.width;
101 [super drawLabel:shouldTruncateLabel inRect:tabRect];
109 - (void)setImage:(NSImage*)image
116 [[NSPasteboard generalPasteboard]
117 declareTypes:[NSArray arrayWithObject:NSTIFFPboardType]
119 [[NSPasteboard generalPasteboard]
120 setData:[m_image TIFFRepresentation]
121 forType:NSTIFFPboardType];
124 @end // implementation WXCTabViewImageItem : NSTabViewItem
125 WX_IMPLEMENT_GET_OBJC_CLASS(WXCTabViewImageItem,NSTabViewItem)
127 // ========================================================================
129 // ========================================================================
130 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
131 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
133 // ========================================================================
135 // ========================================================================
136 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxBookCtrlBase)
137 BEGIN_EVENT_TABLE(wxNotebook, wxNotebookBase)
139 WX_IMPLEMENT_COCOA_OWNER(wxNotebook,NSTabView,NSView,NSView)
141 bool wxNotebook::Create(wxWindow *parent, wxWindowID winid,
145 const wxString& name)
147 wxAutoNSAutoreleasePool pool;
148 if(!CreateControl(parent,winid,pos,size,style,wxDefaultValidator,name))
150 m_cocoaNSView = NULL;
151 SetNSTabView([[NSTabView alloc] initWithFrame:MakeDefaultNSRect(size)]);
155 NSTabViewType tabViewType;
157 tabViewType = NSTopTabsBezelBorder;
158 else if(style & wxNB_LEFT)
159 tabViewType = NSLeftTabsBezelBorder;
160 else if(style & wxNB_RIGHT)
161 tabViewType = NSRightTabsBezelBorder;
162 else if(style & wxNB_BOTTOM)
163 tabViewType = NSBottomTabsBezelBorder;
166 [GetNSTabView() setTabViewType:tabViewType];
170 m_parent->CocoaAddChild(this);
171 SetInitialFrameRect(pos,size);
176 wxNotebook::~wxNotebook()
180 void wxNotebook::SetPadding(const wxSize& padding)
184 void wxNotebook::SetTabSize(const wxSize& sz)
188 void wxNotebook::SetPageSize(const wxSize& size)
193 wxNotebookPage *wxNotebook::DoRemovePage(size_t nPage)
195 wxNotebookPage *page = wxNotebookBase::DoRemovePage(nPage);
198 NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage];
201 [GetNSTabView() removeTabViewItem:tvitem];
202 // Remove the child window as a notebook page
203 wxASSERT(static_cast<NSView*>([tvitem view]) == page->GetNSViewForSuperview());
204 [tvitem setView:nil];
206 // Make it back into a normal child window
207 [m_cocoaNSView addSubview: page->GetNSViewForSuperview()];
212 bool wxNotebook::DeletePage(size_t nPage)
214 return wxNotebookBase::DeletePage(nPage);
217 bool wxNotebook::InsertPage( size_t pos,
218 wxNotebookPage *page, const wxString& title,
219 bool bSelect, int imageId)
221 wxAutoNSAutoreleasePool pool;
222 m_pages.Insert(page,pos);
223 NSTabViewItem *tvitem = [[WX_GET_OBJC_CLASS(WXCTabViewImageItem) alloc] initWithIdentifier:nil];
224 [tvitem setLabel: wxNSStringWithWxString(title)];
225 const wxBitmap *bmp = (imageId!=-1)?m_imageList->GetBitmapPtr(imageId):NULL;
227 [(WXCTabViewImageItem*) tvitem setImage: bmp->GetNSImage(true)];
229 NSView *pageNSView = page->GetNSViewForSuperview();
230 // Remove it as a normal child
231 wxASSERT(m_cocoaNSView == [pageNSView superview]);
232 [pageNSView removeFromSuperview];
233 // And make it a notebook page
234 [tvitem setView: pageNSView];
236 [GetNSTabView() insertTabViewItem:tvitem atIndex:pos];
242 bool wxNotebook::DeleteAllPages()
244 while(!m_pages.IsEmpty())
250 bool wxNotebook::SetPageText(size_t nPage, const wxString& title)
252 NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage];
255 [tvitem setLabel: wxNSStringWithWxString(title)];
259 wxString wxNotebook::GetPageText(size_t nPage) const
261 return wxStringWithNSString([[GetNSTabView() tabViewItemAtIndex: nPage] label]);
265 int wxNotebook::GetPageImage(size_t nPage) const
267 // To do this we'd need to keep track of this, which we don't!
271 bool wxNotebook::SetPageImage(size_t nPage, int nImage)
273 const wxBitmap *bmp = nImage!=-1?m_imageList->GetBitmapPtr(nImage):NULL;
276 NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage];
279 [(WXCTabViewImageItem*) tvitem setImage: bmp->GetNSImage(true)];
283 int wxNotebook::SetSelection(size_t nPage)
285 const int pageOld = GetSelection();
287 if ( !SendPageChangingEvent(nPage) )
290 int page = ChangeSelection(nPage);
291 if ( page != wxNOT_FOUND )
293 SendPageChangedEvent(pageOld);
299 int wxNotebook::ChangeSelection(size_t nPage)
301 wxAutoNSAutoreleasePool pool;
302 [GetNSTabView() selectTabViewItemAtIndex:nPage];
303 return GetSelection();
306 int wxNotebook::GetSelection() const
308 NSTabViewItem *selectedItem = [GetNSTabView() selectedTabViewItem];
311 return [GetNSTabView() indexOfTabViewItem:selectedItem];
314 void wxNotebook::CocoaDelegate_tabView_didSelectTabViewItem(WX_NSTabViewItem tabViewItem)
316 // FIXME: oldSel probably == newSel
317 wxBookCtrlEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, GetId(),
318 [GetNSTabView() indexOfTabViewItem:tabViewItem], GetSelection());
319 event.SetEventObject(this);
320 GetEventHandler()->ProcessEvent(event);
323 bool wxNotebook::CocoaDelegate_tabView_shouldSelectTabViewItem(WX_NSTabViewItem tabViewItem)
325 wxBookCtrlEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, GetId(),
326 [GetNSTabView() indexOfTabViewItem:tabViewItem], GetSelection());
327 event.SetEventObject(this);
328 return !HandleWindowEvent(event) || event.IsAllowed();
331 #endif // wxUSE_NOTEBOOK