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];
70 - (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel
72 NSSize labelSize = [super sizeOfLabel:shouldTruncateLabel];
75 NSSize imageSize = [m_image size];
77 if(imageSize.height > labelSize.height)
79 imageSize.width *= labelSize.height/imageSize.height;
80 imageSize.height *= labelSize.height/imageSize.height;
81 [m_image setScalesWhenResized:YES];
82 [m_image setSize: imageSize];
84 labelSize.width += imageSize.width;
88 - (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect
92 NSSize imageSize = [m_image size];
93 [m_image compositeToPoint:NSMakePoint(tabRect.origin.x,
94 tabRect.origin.y+imageSize.height)
95 operation:NSCompositeSourceOver];
96 tabRect.size.width -= imageSize.width;
97 tabRect.origin.x += imageSize.width;
99 [super drawLabel:shouldTruncateLabel inRect:tabRect];
107 - (void)setImage:(NSImage*)image
114 [[NSPasteboard generalPasteboard]
115 declareTypes:[NSArray arrayWithObject:NSTIFFPboardType]
117 [[NSPasteboard generalPasteboard]
118 setData:[m_image TIFFRepresentation]
119 forType:NSTIFFPboardType];
122 @end // implementation WXCTabViewImageItem : NSTabViewItem
124 // ========================================================================
126 // ========================================================================
127 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
128 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
129 IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
131 // ========================================================================
133 // ========================================================================
134 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
135 BEGIN_EVENT_TABLE(wxNotebook, wxNotebookBase)
137 WX_IMPLEMENT_COCOA_OWNER(wxNotebook,NSTabView,NSView,NSView)
139 bool wxNotebook::Create(wxWindow *parent, wxWindowID winid,
143 const wxString& name)
145 wxAutoNSAutoreleasePool pool;
146 if(!CreateControl(parent,winid,pos,size,style,wxDefaultValidator,name))
148 m_cocoaNSView = NULL;
149 SetNSTabView([[NSTabView alloc] initWithFrame:MakeDefaultNSRect(size)]);
151 m_parent->CocoaAddChild(this);
152 SetInitialFrameRect(pos,size);
157 wxNotebook::~wxNotebook()
161 void wxNotebook::SetPadding(const wxSize& padding)
165 void wxNotebook::SetTabSize(const wxSize& sz)
169 void wxNotebook::SetPageSize(const wxSize& size)
174 wxNotebookPage *wxNotebook::DoRemovePage(size_t nPage)
176 wxNotebookPage *page = wxNotebookBase::DoRemovePage(nPage);
179 NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage];
182 [GetNSTabView() removeTabViewItem:tvitem];
183 // Remove the child window as a notebook page
184 wxASSERT(static_cast<NSView*>([tvitem view]) == page->GetNSViewForSuperview());
185 [tvitem setView:nil];
187 // Make it back into a normal child window
188 [m_cocoaNSView addSubview: page->GetNSViewForSuperview()];
193 bool wxNotebook::DeletePage(size_t nPage)
195 return wxNotebookBase::DeletePage(nPage);
198 bool wxNotebook::InsertPage( size_t pos,
199 wxNotebookPage *page, const wxString& title,
200 bool bSelect, int imageId)
202 wxAutoNSAutoreleasePool pool;
203 m_pages.Insert(page,pos);
204 NSTabViewItem *tvitem = [[WXCTabViewImageItem alloc] initWithIdentifier:nil];
205 [tvitem setLabel: wxNSStringWithWxString(title)];
206 const wxBitmap *bmp = (imageId!=-1)?m_imageList->GetBitmapPtr(imageId):NULL;
208 [(WXCTabViewImageItem*) tvitem setImage: bmp->GetNSImage(true)];
210 NSView *pageNSView = page->GetNSViewForSuperview();
211 // Remove it as a normal child
212 wxASSERT(m_cocoaNSView == [pageNSView superview]);
213 [pageNSView removeFromSuperview];
214 // And make it a notebook page
215 [tvitem setView: pageNSView];
217 [GetNSTabView() insertTabViewItem:tvitem atIndex:pos];
223 bool wxNotebook::DeleteAllPages()
225 while(!m_pages.IsEmpty())
231 bool wxNotebook::SetPageText(size_t nPage, const wxString& title)
233 NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage];
236 [tvitem setLabel: wxNSStringWithWxString(title)];
240 wxString wxNotebook::GetPageText(size_t nPage) const
242 return wxStringWithNSString([[GetNSTabView() tabViewItemAtIndex: nPage] label]);
246 int wxNotebook::GetPageImage(size_t nPage) const
248 // To do this we'd need to keep track of this, which we don't!
252 bool wxNotebook::SetPageImage(size_t nPage, int nImage)
254 const wxBitmap *bmp = nImage!=-1?m_imageList->GetBitmapPtr(nImage):NULL;
257 NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage];
260 [(WXCTabViewImageItem*) tvitem setImage: bmp->GetNSImage(true)];
265 int wxNotebook::SetSelection(size_t nPage)
267 wxAutoNSAutoreleasePool pool;
268 [GetNSTabView() selectTabViewItemAtIndex:nPage];
269 return GetSelection();
272 int wxNotebook::GetSelection() const
274 NSTabViewItem *selectedItem = [GetNSTabView() selectedTabViewItem];
277 return [GetNSTabView() indexOfTabViewItem:selectedItem];
280 void wxNotebook::CocoaDelegate_tabView_didSelectTabViewItem(WX_NSTabViewItem tabViewItem)
282 // FIXME: oldSel probably == newSel
283 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, GetId(),
284 [GetNSTabView() indexOfTabViewItem:tabViewItem], GetSelection());
285 event.SetEventObject(this);
288 bool wxNotebook::CocoaDelegate_tabView_shouldSelectTabViewItem(WX_NSTabViewItem tabViewItem)
290 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, GetId(),
291 [GetNSTabView() indexOfTabViewItem:tabViewItem], GetSelection());
292 event.SetEventObject(this);
293 return !GetEventHandler()->ProcessEvent(event) || event.IsAllowed();
296 #endif // wxUSE_NOTEBOOK