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"
25 #import <AppKit/NSTabView.h>
26 #import <AppKit/NSTabViewItem.h>
27 #import <AppKit/NSImage.h>
30 #import <AppKit/NSPasteboard.h>
31 #import <Foundation/NSArray.h>
33 // ========================================================================
34 // WXCTabViewImageItem
35 // ========================================================================
36 @interface WXCTabViewImageItem : NSTabViewItem
42 - (id)initWithIdentifier: (id)identifier;
45 - (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel;
46 - (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect;
49 - (void)setImage:(NSImage*)image;
50 @end // interface WXCTabViewImageItem : NSTabViewItem
52 @implementation WXCTabViewImageItem : NSTabViewItem
55 return [self initWithIdentifier:nil];
58 - (id)initWithIdentifier: (id)identifier;
61 return [super initWithIdentifier:identifier];
69 - (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel
71 NSSize labelSize = [super sizeOfLabel:shouldTruncateLabel];
74 NSSize imageSize = [m_image size];
76 if(imageSize.height > labelSize.height)
78 imageSize.width *= labelSize.height/imageSize.height;
79 imageSize.height *= labelSize.height/imageSize.height;
80 [m_image setScalesWhenResized:YES];
81 [m_image setSize: imageSize];
83 labelSize.width += imageSize.width;
87 - (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect
91 NSSize imageSize = [m_image size];
92 [m_image compositeToPoint:NSMakePoint(tabRect.origin.x,
93 tabRect.origin.y+imageSize.height)
94 operation:NSCompositeSourceOver];
95 tabRect.size.width -= imageSize.width;
96 tabRect.origin.x += imageSize.width;
98 [super drawLabel:shouldTruncateLabel inRect:tabRect];
106 - (void)setImage:(NSImage*)image
113 [[NSPasteboard generalPasteboard]
114 declareTypes:[NSArray arrayWithObject:NSTIFFPboardType]
116 [[NSPasteboard generalPasteboard]
117 setData:[m_image TIFFRepresentation]
118 forType:NSTIFFPboardType];
121 @end // implementation WXCTabViewImageItem : NSTabViewItem
123 // ========================================================================
125 // ========================================================================
126 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
127 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
128 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
130 // ========================================================================
132 // ========================================================================
133 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
134 BEGIN_EVENT_TABLE(wxNotebook, wxNotebookBase)
136 WX_IMPLEMENT_COCOA_OWNER(wxNotebook,NSTabView,NSView,NSView)
138 bool wxNotebook::Create(wxWindow *parent, wxWindowID winid,
142 const wxString& name)
144 wxAutoNSAutoreleasePool pool;
145 if(!CreateControl(parent,winid,pos,size,style,wxDefaultValidator,name))
147 m_cocoaNSView = NULL;
148 SetNSTabView([[NSTabView alloc] initWithFrame:MakeDefaultNSRect(size)]);
150 m_parent->CocoaAddChild(this);
151 SetInitialFrameRect(pos,size);
156 wxNotebook::~wxNotebook()
160 void wxNotebook::SetPadding(const wxSize& padding)
164 void wxNotebook::SetTabSize(const wxSize& sz)
168 void wxNotebook::SetPageSize(const wxSize& size)
173 wxNotebookPage *wxNotebook::DoRemovePage(size_t nPage)
175 wxNotebookPage *page = wxNotebookBase::DoRemovePage(nPage);
178 NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage];
181 [GetNSTabView() removeTabViewItem:tvitem];
182 // Remove the child window as a notebook page
183 wxASSERT(static_cast<NSView*>([tvitem view]) == page->GetNSViewForSuperview());
184 [tvitem setView:nil];
186 // Make it back into a normal child window
187 [m_cocoaNSView addSubview: page->GetNSViewForSuperview()];
192 bool wxNotebook::DeletePage(size_t nPage)
194 return wxNotebookBase::DeletePage(nPage);
197 bool wxNotebook::InsertPage( size_t pos,
198 wxNotebookPage *page, const wxString& title,
199 bool bSelect, int imageId)
201 wxAutoNSAutoreleasePool pool;
202 m_pages.Insert(page,pos);
203 NSTabViewItem *tvitem = [[WXCTabViewImageItem alloc] initWithIdentifier:nil];
204 [tvitem setLabel: wxNSStringWithWxString(title)];
205 const wxBitmap *bmp = (imageId!=-1)?m_imageList->GetBitmapPtr(imageId):NULL;
207 [(WXCTabViewImageItem*) tvitem setImage: bmp->GetNSImage(true)];
209 NSView *pageNSView = page->GetNSViewForSuperview();
210 // Remove it as a normal child
211 wxASSERT(m_cocoaNSView == [pageNSView superview]);
212 [pageNSView removeFromSuperview];
213 // And make it a notebook page
214 [tvitem setView: pageNSView];
216 [GetNSTabView() insertTabViewItem:tvitem atIndex:pos];
222 bool wxNotebook::DeleteAllPages()
224 while(!m_pages.IsEmpty())
230 bool wxNotebook::SetPageText(size_t nPage, const wxString& title)
232 NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage];
235 [tvitem setLabel: wxNSStringWithWxString(title)];
239 wxString wxNotebook::GetPageText(size_t nPage) const
241 return wxStringWithNSString([[GetNSTabView() tabViewItemAtIndex: nPage] label]);
245 int wxNotebook::GetPageImage(size_t nPage) const
247 // To do this we'd need to keep track of this, which we don't!
251 bool wxNotebook::SetPageImage(size_t nPage, int nImage)
253 const wxBitmap *bmp = nImage!=-1?m_imageList->GetBitmapPtr(nImage):NULL;
256 NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage];
259 [(WXCTabViewImageItem*) tvitem setImage: bmp->GetNSImage(true)];
264 int wxNotebook::SetSelection(size_t nPage)
266 wxAutoNSAutoreleasePool pool;
267 [GetNSTabView() selectTabViewItemAtIndex:nPage];
268 return GetSelection();
271 int wxNotebook::GetSelection() const
273 NSTabViewItem *selectedItem = [GetNSTabView() selectedTabViewItem];
276 return [GetNSTabView() indexOfTabViewItem:selectedItem];
279 void wxNotebook::CocoaDelegate_tabView_didSelectTabViewItem(WX_NSTabViewItem tabViewItem)
281 // FIXME: oldSel probably == newSel
282 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, GetId(),
283 [GetNSTabView() indexOfTabViewItem:tabViewItem], GetSelection());
284 event.SetEventObject(this);
287 bool wxNotebook::CocoaDelegate_tabView_shouldSelectTabViewItem(WX_NSTabViewItem tabViewItem)
289 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, GetId(),
290 [GetNSTabView() indexOfTabViewItem:tabViewItem], GetSelection());
291 event.SetEventObject(this);
292 return !GetEventHandler()->ProcessEvent(event) || event.IsAllowed();
295 #endif // wxUSE_NOTEBOOK