]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/notebook.mm
Eliminated WX_PG_DECLARE/IMPLEMENT_DERIVED_PROPERTY_CLASS
[wxWidgets.git] / src / cocoa / notebook.mm
CommitLineData
78480884
DE
1/////////////////////////////////////////////////////////////////////////////
2// Name: cocoa/notebook.mm
3// Purpose: wxNotebook
4// Author: David Elliott
5// Modified by:
6// Created: 2004/04/08
7// RCS-ID: $Id$
8// Copyright: (c) 2004 David Elliott
065e208e 9// Licence: wxWidgets licence
78480884
DE
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
28b22ccc
DE
13
14#if wxUSE_NOTEBOOK
15
78480884
DE
16#ifndef WX_PRECOMP
17 #include "wx/app.h"
78480884 18#endif //WX_PRECOMP
fa7de3b0 19#include "wx/notebook.h"
78480884
DE
20#include "wx/imaglist.h"
21
22#include "wx/cocoa/autorelease.h"
23#include "wx/cocoa/string.h"
e7e1ad7d 24#include "wx/cocoa/objc/objc_uniquifying.h"
78480884
DE
25
26#import <AppKit/NSTabView.h>
27#import <AppKit/NSTabViewItem.h>
28#import <AppKit/NSImage.h>
29
30// testing:
31#import <AppKit/NSPasteboard.h>
32#import <Foundation/NSArray.h>
33
34// ========================================================================
35// WXCTabViewImageItem
36// ========================================================================
37@interface WXCTabViewImageItem : NSTabViewItem
38{
39 NSImage *m_image;
40}
41
42- (id)init;
43- (id)initWithIdentifier: (id)identifier;
44- (void)dealloc;
45
46- (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel;
47- (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect;
48
49- (NSImage*)image;
50- (void)setImage:(NSImage*)image;
51@end // interface WXCTabViewImageItem : NSTabViewItem
e7e1ad7d 52WX_DECLARE_GET_OBJC_CLASS(WXCTabViewImageItem,NSTabViewItem)
78480884
DE
53
54@implementation WXCTabViewImageItem : NSTabViewItem
55- (id)init
56{
57 return [self initWithIdentifier:nil];
58}
59
60- (id)initWithIdentifier: (id)identifier;
61{
62 m_image = nil;
63 return [super initWithIdentifier:identifier];
64}
65
66- (void)dealloc
67{
68 [m_image release];
9a7247f0 69 [super dealloc];
78480884
DE
70}
71
72- (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel
73{
74 NSSize labelSize = [super sizeOfLabel:shouldTruncateLabel];
75 if(!m_image)
76 return labelSize;
77 NSSize imageSize = [m_image size];
78 // scale image size
79 if(imageSize.height > labelSize.height)
80 {
81 imageSize.width *= labelSize.height/imageSize.height;
82 imageSize.height *= labelSize.height/imageSize.height;
83 [m_image setScalesWhenResized:YES];
84 [m_image setSize: imageSize];
85 }
86 labelSize.width += imageSize.width;
87 return labelSize;
88}
89
90- (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect
91{
92 if(m_image)
93 {
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;
100 }
101 [super drawLabel:shouldTruncateLabel inRect:tabRect];
102}
103
104- (NSImage*)image
105{
106 return m_image;
107}
108
109- (void)setImage:(NSImage*)image
110{
111 [image retain];
112 [m_image release];
113 m_image = image;
114 if(!m_image)
115 return;
116 [[NSPasteboard generalPasteboard]
117 declareTypes:[NSArray arrayWithObject:NSTIFFPboardType]
118 owner:nil];
119 [[NSPasteboard generalPasteboard]
120 setData:[m_image TIFFRepresentation]
121 forType:NSTIFFPboardType];
122}
123
124@end // implementation WXCTabViewImageItem : NSTabViewItem
e7e1ad7d 125WX_IMPLEMENT_GET_OBJC_CLASS(WXCTabViewImageItem,NSTabViewItem)
78480884 126
78480884
DE
127// ========================================================================
128// wxNotebook
129// ========================================================================
8dba4c72 130IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxBookCtrlBase)
78480884
DE
131BEGIN_EVENT_TABLE(wxNotebook, wxNotebookBase)
132END_EVENT_TABLE()
133WX_IMPLEMENT_COCOA_OWNER(wxNotebook,NSTabView,NSView,NSView)
134
135bool wxNotebook::Create(wxWindow *parent, wxWindowID winid,
136 const wxPoint& pos,
137 const wxSize& size,
138 long style,
139 const wxString& name)
140{
141 wxAutoNSAutoreleasePool pool;
142 if(!CreateControl(parent,winid,pos,size,style,wxDefaultValidator,name))
143 return false;
144 m_cocoaNSView = NULL;
145 SetNSTabView([[NSTabView alloc] initWithFrame:MakeDefaultNSRect(size)]);
94a2af76
DE
146
147 do
148 {
149 NSTabViewType tabViewType;
150 if(style & wxNB_TOP)
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;
158 else
159 break;
160 [GetNSTabView() setTabViewType:tabViewType];
161 } while(0);
162
78480884
DE
163 if(m_parent)
164 m_parent->CocoaAddChild(this);
165 SetInitialFrameRect(pos,size);
166
167 return true;
168}
169
170wxNotebook::~wxNotebook()
171{
172}
173
174void wxNotebook::SetPadding(const wxSize& padding)
175{ // Can't do
176}
177
178void wxNotebook::SetTabSize(const wxSize& sz)
179{ // Can't do
180}
181
182void wxNotebook::SetPageSize(const wxSize& size)
183{
184}
185
186
187wxNotebookPage *wxNotebook::DoRemovePage(size_t nPage)
188{
189 wxNotebookPage *page = wxNotebookBase::DoRemovePage(nPage);
190 if(!page)
191 return NULL;
192 NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage];
193 wxASSERT(tvitem);
194 [tvitem retain];
195 [GetNSTabView() removeTabViewItem:tvitem];
196 // Remove the child window as a notebook page
294d965b 197 wxASSERT(static_cast<NSView*>([tvitem view]) == page->GetNSViewForSuperview());
78480884
DE
198 [tvitem setView:nil];
199 [tvitem release];
200 // Make it back into a normal child window
201 [m_cocoaNSView addSubview: page->GetNSViewForSuperview()];
202
203 return page;
204}
205
206bool wxNotebook::DeletePage(size_t nPage)
207{
208 return wxNotebookBase::DeletePage(nPage);
209}
210
211bool wxNotebook::InsertPage( size_t pos,
212 wxNotebookPage *page, const wxString& title,
213 bool bSelect, int imageId)
214{
215 wxAutoNSAutoreleasePool pool;
216 m_pages.Insert(page,pos);
e7e1ad7d 217 NSTabViewItem *tvitem = [[WX_GET_OBJC_CLASS(WXCTabViewImageItem) alloc] initWithIdentifier:nil];
78480884 218 [tvitem setLabel: wxNSStringWithWxString(title)];
49bf4e3e 219 const wxBitmap *bmp = (imageId!=-1)?m_imageList->GetBitmapPtr(imageId):NULL;
78480884
DE
220 if(bmp)
221 [(WXCTabViewImageItem*) tvitem setImage: bmp->GetNSImage(true)];
222
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];
229
230 [GetNSTabView() insertTabViewItem:tvitem atIndex:pos];
231 [tvitem release];
232
233 return true;
234}
235
236bool wxNotebook::DeleteAllPages()
237{
238 while(!m_pages.IsEmpty())
239 DeletePage(0);
240 return true;
241}
242
243
244bool wxNotebook::SetPageText(size_t nPage, const wxString& title)
245{
246 NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage];
247 if(!tvitem)
248 return false;
249 [tvitem setLabel: wxNSStringWithWxString(title)];
250 return true;
251}
252
253wxString wxNotebook::GetPageText(size_t nPage) const
254{
255 return wxStringWithNSString([[GetNSTabView() tabViewItemAtIndex: nPage] label]);
256}
257
258
259int wxNotebook::GetPageImage(size_t nPage) const
260{
261 // To do this we'd need to keep track of this, which we don't!
262 return -1;
263}
264
265bool wxNotebook::SetPageImage(size_t nPage, int nImage)
266{
49bf4e3e 267 const wxBitmap *bmp = nImage!=-1?m_imageList->GetBitmapPtr(nImage):NULL;
78480884
DE
268 if(!bmp)
269 return false;
270 NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage];
271 if(!tvitem)
272 return false;
273 [(WXCTabViewImageItem*) tvitem setImage: bmp->GetNSImage(true)];
274 return true;
275}
276
78480884 277int wxNotebook::SetSelection(size_t nPage)
a570e8f8
VZ
278{
279 const int pageOld = GetSelection();
280
281 if ( !SendPageChangingEvent(nPage) )
282 return pageOld;
283
284 int page = ChangeSelection(nPage);
285 if ( page != wxNOT_FOUND )
286 {
287 SendPageChangedEvent(pageOld);
288 }
289
290 return page;
291}
292
293int wxNotebook::ChangeSelection(size_t nPage)
78480884 294{
264c023f 295 wxAutoNSAutoreleasePool pool;
78480884
DE
296 [GetNSTabView() selectTabViewItemAtIndex:nPage];
297 return GetSelection();
298}
299
300int wxNotebook::GetSelection() const
301{
302 NSTabViewItem *selectedItem = [GetNSTabView() selectedTabViewItem];
303 if(!selectedItem)
304 return -1;
305 return [GetNSTabView() indexOfTabViewItem:selectedItem];
306}
307
308void wxNotebook::CocoaDelegate_tabView_didSelectTabViewItem(WX_NSTabViewItem tabViewItem)
309{
310 // FIXME: oldSel probably == newSel
3e97a905 311 wxBookCtrlEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, GetId(),
78480884
DE
312 [GetNSTabView() indexOfTabViewItem:tabViewItem], GetSelection());
313 event.SetEventObject(this);
ff9f3526 314 GetEventHandler()->ProcessEvent(event);
78480884
DE
315}
316
317bool wxNotebook::CocoaDelegate_tabView_shouldSelectTabViewItem(WX_NSTabViewItem tabViewItem)
318{
3e97a905 319 wxBookCtrlEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, GetId(),
78480884
DE
320 [GetNSTabView() indexOfTabViewItem:tabViewItem], GetSelection());
321 event.SetEventObject(this);
937013e0 322 return !HandleWindowEvent(event) || event.IsAllowed();
78480884
DE
323}
324
28b22ccc 325#endif // wxUSE_NOTEBOOK