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