1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/notebook.mm
4 // Author: David Elliott
8 // Copyright: (c) 2004 David Elliott
9 // Licence: wxWindows 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 // ========================================================================
131 BEGIN_EVENT_TABLE(wxNotebook, wxNotebookBase)
133 WX_IMPLEMENT_COCOA_OWNER(wxNotebook,NSTabView,NSView,NSView)
135 bool wxNotebook::Create(wxWindow *parent, wxWindowID winid,
139 const wxString& name)
141 wxAutoNSAutoreleasePool pool;
142 if(!CreateControl(parent,winid,pos,size,style,wxDefaultValidator,name))
144 m_cocoaNSView = NULL;
145 SetNSTabView([[NSTabView alloc] initWithFrame:MakeDefaultNSRect(size)]);
149 NSTabViewType tabViewType;
151 tabViewType = NSTopTabsBezelBorder;
152 else if(style & wxNB_LEFT)
153 tabViewType = NSLeftTabsBezelBorder;
154 else if(style & wxNB_RIGHT)
155 tabViewType = NSRightTabsBezelBorder;
156 else if(style & wxNB_BOTTOM)
157 tabViewType = NSBottomTabsBezelBorder;
160 [GetNSTabView() setTabViewType:tabViewType];
164 m_parent->CocoaAddChild(this);
165 SetInitialFrameRect(pos,size);
170 wxNotebook::~wxNotebook()
174 void wxNotebook::SetPadding(const wxSize& padding)
178 void wxNotebook::SetTabSize(const wxSize& sz)
182 void wxNotebook::SetPageSize(const wxSize& size)
187 wxNotebookPage *wxNotebook::DoRemovePage(size_t nPage)
189 wxNotebookPage *page = wxNotebookBase::DoRemovePage(nPage);
192 NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage];
195 [GetNSTabView() removeTabViewItem:tvitem];
196 // Remove the child window as a notebook page
197 wxASSERT(static_cast<NSView*>([tvitem view]) == page->GetNSViewForSuperview());
198 [tvitem setView:nil];
200 // Make it back into a normal child window
201 [m_cocoaNSView addSubview: page->GetNSViewForSuperview()];
206 bool wxNotebook::DeletePage(size_t nPage)
208 return wxNotebookBase::DeletePage(nPage);
211 bool wxNotebook::InsertPage( size_t pos,
212 wxNotebookPage *page, const wxString& title,
213 bool bSelect, int imageId)
215 wxAutoNSAutoreleasePool pool;
216 m_pages.Insert(page,pos);
217 NSTabViewItem *tvitem = [[WX_GET_OBJC_CLASS(WXCTabViewImageItem) alloc] initWithIdentifier:nil];
218 [tvitem setLabel: wxNSStringWithWxString(title)];
219 const wxBitmap *bmp = (imageId!=-1)?m_imageList->GetBitmapPtr(imageId):NULL;
221 [(WXCTabViewImageItem*) tvitem setImage: bmp->GetNSImage(true)];
223 NSView *pageNSView = page->GetNSViewForSuperview();
224 // Remove it as a normal child
225 wxASSERT(m_cocoaNSView == [pageNSView superview]);
226 [pageNSView removeFromSuperview];
227 // And make it a notebook page
228 [tvitem setView: pageNSView];
230 [GetNSTabView() insertTabViewItem:tvitem atIndex:pos];
236 bool wxNotebook::DeleteAllPages()
238 while(!m_pages.IsEmpty())
244 bool wxNotebook::SetPageText(size_t nPage, const wxString& title)
246 NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage];
249 [tvitem setLabel: wxNSStringWithWxString(title)];
253 wxString wxNotebook::GetPageText(size_t nPage) const
255 return wxStringWithNSString([[GetNSTabView() tabViewItemAtIndex: nPage] label]);
259 int wxNotebook::GetPageImage(size_t nPage) const
261 // To do this we'd need to keep track of this, which we don't!
265 bool wxNotebook::SetPageImage(size_t nPage, int nImage)
267 const wxBitmap *bmp = nImage!=-1?m_imageList->GetBitmapPtr(nImage):NULL;
270 NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage];
273 [(WXCTabViewImageItem*) tvitem setImage: bmp->GetNSImage(true)];
277 int wxNotebook::SetSelection(size_t nPage)
279 const int pageOld = GetSelection();
281 if ( !SendPageChangingEvent(nPage) )
284 int page = ChangeSelection(nPage);
285 if ( page != wxNOT_FOUND )
287 SendPageChangedEvent(pageOld);
293 int wxNotebook::ChangeSelection(size_t nPage)
295 wxAutoNSAutoreleasePool pool;
296 [GetNSTabView() selectTabViewItemAtIndex:nPage];
297 return GetSelection();
300 int wxNotebook::GetSelection() const
302 NSTabViewItem *selectedItem = [GetNSTabView() selectedTabViewItem];
305 return [GetNSTabView() indexOfTabViewItem:selectedItem];
308 void wxNotebook::CocoaDelegate_tabView_didSelectTabViewItem(WX_NSTabViewItem tabViewItem)
310 // FIXME: oldSel probably == newSel
311 wxBookCtrlEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, GetId(),
312 [GetNSTabView() indexOfTabViewItem:tabViewItem], GetSelection());
313 event.SetEventObject(this);
314 GetEventHandler()->ProcessEvent(event);
317 bool wxNotebook::CocoaDelegate_tabView_shouldSelectTabViewItem(WX_NSTabViewItem tabViewItem)
319 wxBookCtrlEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, GetId(),
320 [GetNSTabView() indexOfTabViewItem:tabViewItem], GetSelection());
321 event.SetEventObject(this);
322 return !HandleWindowEvent(event) || event.IsAllowed();
325 #endif // wxUSE_NOTEBOOK