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"
15 #include "wx/notebook.h"
17 #include "wx/imaglist.h"
19 #include "wx/cocoa/autorelease.h"
20 #include "wx/cocoa/string.h"
22 #import <AppKit/NSTabView.h>
23 #import <AppKit/NSTabViewItem.h>
24 #import <AppKit/NSImage.h>
27 #import <AppKit/NSPasteboard.h>
28 #import <Foundation/NSArray.h>
30 // ========================================================================
31 // WXCTabViewImageItem
32 // ========================================================================
33 @interface WXCTabViewImageItem : NSTabViewItem
39 - (id)initWithIdentifier: (id)identifier;
42 - (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel;
43 - (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect;
46 - (void)setImage:(NSImage*)image;
47 @end // interface WXCTabViewImageItem : NSTabViewItem
49 @implementation WXCTabViewImageItem : NSTabViewItem
52 return [self initWithIdentifier:nil];
55 - (id)initWithIdentifier: (id)identifier;
58 return [super initWithIdentifier:identifier];
66 - (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel
68 NSSize labelSize = [super sizeOfLabel:shouldTruncateLabel];
71 NSSize imageSize = [m_image size];
73 if(imageSize.height > labelSize.height)
75 imageSize.width *= labelSize.height/imageSize.height;
76 imageSize.height *= labelSize.height/imageSize.height;
77 [m_image setScalesWhenResized:YES];
78 [m_image setSize: imageSize];
80 labelSize.width += imageSize.width;
84 - (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect
88 NSSize imageSize = [m_image size];
89 [m_image compositeToPoint:NSMakePoint(tabRect.origin.x,
90 tabRect.origin.y+imageSize.height)
91 operation:NSCompositeSourceOver];
92 tabRect.size.width -= imageSize.width;
93 tabRect.origin.x += imageSize.width;
95 [super drawLabel:shouldTruncateLabel inRect:tabRect];
103 - (void)setImage:(NSImage*)image
110 [[NSPasteboard generalPasteboard]
111 declareTypes:[NSArray arrayWithObject:NSTIFFPboardType]
113 [[NSPasteboard generalPasteboard]
114 setData:[m_image TIFFRepresentation]
115 forType:NSTIFFPboardType];
118 @end // implementation WXCTabViewImageItem : NSTabViewItem
120 // ========================================================================
122 // ========================================================================
123 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
124 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
125 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
127 // ========================================================================
129 // ========================================================================
130 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
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)]);
147 m_parent->CocoaAddChild(this);
148 SetInitialFrameRect(pos,size);
153 wxNotebook::~wxNotebook()
157 void wxNotebook::SetPadding(const wxSize& padding)
161 void wxNotebook::SetTabSize(const wxSize& sz)
165 void wxNotebook::SetPageSize(const wxSize& size)
170 wxNotebookPage *wxNotebook::DoRemovePage(size_t nPage)
172 wxNotebookPage *page = wxNotebookBase::DoRemovePage(nPage);
175 NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage];
178 [GetNSTabView() removeTabViewItem:tvitem];
179 // Remove the child window as a notebook page
180 wxASSERT([tvitem view] == page->GetNSViewForSuperview());
181 [tvitem setView:nil];
183 // Make it back into a normal child window
184 [m_cocoaNSView addSubview: page->GetNSViewForSuperview()];
189 bool wxNotebook::DeletePage(size_t nPage)
191 return wxNotebookBase::DeletePage(nPage);
194 bool wxNotebook::InsertPage( size_t pos,
195 wxNotebookPage *page, const wxString& title,
196 bool bSelect, int imageId)
198 wxAutoNSAutoreleasePool pool;
199 m_pages.Insert(page,pos);
200 NSTabViewItem *tvitem = [[WXCTabViewImageItem alloc] initWithIdentifier:nil];
201 [tvitem setLabel: wxNSStringWithWxString(title)];
202 const wxBitmap *bmp = (imageId!=-1)?m_imageList->GetBitmap(imageId):NULL;
204 [(WXCTabViewImageItem*) tvitem setImage: bmp->GetNSImage(true)];
206 NSView *pageNSView = page->GetNSViewForSuperview();
207 // Remove it as a normal child
208 wxASSERT(m_cocoaNSView == [pageNSView superview]);
209 [pageNSView removeFromSuperview];
210 // And make it a notebook page
211 [tvitem setView: pageNSView];
213 [GetNSTabView() insertTabViewItem:tvitem atIndex:pos];
219 bool wxNotebook::DeleteAllPages()
221 while(!m_pages.IsEmpty())
227 bool wxNotebook::SetPageText(size_t nPage, const wxString& title)
229 NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage];
232 [tvitem setLabel: wxNSStringWithWxString(title)];
236 wxString wxNotebook::GetPageText(size_t nPage) const
238 return wxStringWithNSString([[GetNSTabView() tabViewItemAtIndex: nPage] label]);
242 int wxNotebook::GetPageImage(size_t nPage) const
244 // To do this we'd need to keep track of this, which we don't!
248 bool wxNotebook::SetPageImage(size_t nPage, int nImage)
250 const wxBitmap *bmp = nImage!=-1?m_imageList->GetBitmap(nImage):NULL;
253 NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage];
256 [(WXCTabViewImageItem*) tvitem setImage: bmp->GetNSImage(true)];
261 int wxNotebook::SetSelection(size_t nPage)
263 wxAutoNSAutoreleasePool pool;
264 [GetNSTabView() selectTabViewItemAtIndex:nPage];
265 return GetSelection();
268 int wxNotebook::GetSelection() const
270 NSTabViewItem *selectedItem = [GetNSTabView() selectedTabViewItem];
273 return [GetNSTabView() indexOfTabViewItem:selectedItem];
276 void wxNotebook::CocoaDelegate_tabView_didSelectTabViewItem(WX_NSTabViewItem tabViewItem)
278 // FIXME: oldSel probably == newSel
279 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, GetId(),
280 [GetNSTabView() indexOfTabViewItem:tabViewItem], GetSelection());
281 event.SetEventObject(this);
284 bool wxNotebook::CocoaDelegate_tabView_shouldSelectTabViewItem(WX_NSTabViewItem tabViewItem)
286 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, GetId(),
287 [GetNSTabView() indexOfTabViewItem:tabViewItem], GetSelection());
288 event.SetEventObject(this);
289 return !GetEventHandler()->ProcessEvent(event) || event.IsAllowed();