]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/notebook.mm
added enabled and hidden attributes to radio box items in XRC
[wxWidgets.git] / src / cocoa / notebook.mm
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
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_NOTEBOOK
15
16 #ifndef WX_PRECOMP
17 #include "wx/app.h"
18 #endif //WX_PRECOMP
19 #include "wx/notebook.h"
20 #include "wx/imaglist.h"
21
22 #include "wx/cocoa/autorelease.h"
23 #include "wx/cocoa/string.h"
24 #include "wx/cocoa/objc/objc_uniquifying.h"
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
52 WX_DECLARE_GET_OBJC_CLASS(WXCTabViewImageItem,NSTabViewItem)
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];
69 [super dealloc];
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
125 WX_IMPLEMENT_GET_OBJC_CLASS(WXCTabViewImageItem,NSTabViewItem)
126
127 // ========================================================================
128 // wxBookCtrlEvent
129 // ========================================================================
130 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
131 DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
132
133 // ========================================================================
134 // wxNotebook
135 // ========================================================================
136 IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxBookCtrlBase)
137 BEGIN_EVENT_TABLE(wxNotebook, wxNotebookBase)
138 END_EVENT_TABLE()
139 WX_IMPLEMENT_COCOA_OWNER(wxNotebook,NSTabView,NSView,NSView)
140
141 bool wxNotebook::Create(wxWindow *parent, wxWindowID winid,
142 const wxPoint& pos,
143 const wxSize& size,
144 long style,
145 const wxString& name)
146 {
147 wxAutoNSAutoreleasePool pool;
148 if(!CreateControl(parent,winid,pos,size,style,wxDefaultValidator,name))
149 return false;
150 m_cocoaNSView = NULL;
151 SetNSTabView([[NSTabView alloc] initWithFrame:MakeDefaultNSRect(size)]);
152
153 do
154 {
155 NSTabViewType tabViewType;
156 if(style & wxNB_TOP)
157 tabViewType = NSTopTabsBezelBorder;
158 else if(style & wxNB_LEFT)
159 tabViewType = NSLeftTabsBezelBorder;
160 else if(style & wxNB_RIGHT)
161 tabViewType = NSRightTabsBezelBorder;
162 else if(style & wxNB_BOTTOM)
163 tabViewType = NSBottomTabsBezelBorder;
164 else
165 break;
166 [GetNSTabView() setTabViewType:tabViewType];
167 } while(0);
168
169 if(m_parent)
170 m_parent->CocoaAddChild(this);
171 SetInitialFrameRect(pos,size);
172
173 return true;
174 }
175
176 wxNotebook::~wxNotebook()
177 {
178 }
179
180 void wxNotebook::SetPadding(const wxSize& padding)
181 { // Can't do
182 }
183
184 void wxNotebook::SetTabSize(const wxSize& sz)
185 { // Can't do
186 }
187
188 void wxNotebook::SetPageSize(const wxSize& size)
189 {
190 }
191
192
193 wxNotebookPage *wxNotebook::DoRemovePage(size_t nPage)
194 {
195 wxNotebookPage *page = wxNotebookBase::DoRemovePage(nPage);
196 if(!page)
197 return NULL;
198 NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage];
199 wxASSERT(tvitem);
200 [tvitem retain];
201 [GetNSTabView() removeTabViewItem:tvitem];
202 // Remove the child window as a notebook page
203 wxASSERT(static_cast<NSView*>([tvitem view]) == page->GetNSViewForSuperview());
204 [tvitem setView:nil];
205 [tvitem release];
206 // Make it back into a normal child window
207 [m_cocoaNSView addSubview: page->GetNSViewForSuperview()];
208
209 return page;
210 }
211
212 bool wxNotebook::DeletePage(size_t nPage)
213 {
214 return wxNotebookBase::DeletePage(nPage);
215 }
216
217 bool wxNotebook::InsertPage( size_t pos,
218 wxNotebookPage *page, const wxString& title,
219 bool bSelect, int imageId)
220 {
221 wxAutoNSAutoreleasePool pool;
222 m_pages.Insert(page,pos);
223 NSTabViewItem *tvitem = [[WX_GET_OBJC_CLASS(WXCTabViewImageItem) alloc] initWithIdentifier:nil];
224 [tvitem setLabel: wxNSStringWithWxString(title)];
225 const wxBitmap *bmp = (imageId!=-1)?m_imageList->GetBitmapPtr(imageId):NULL;
226 if(bmp)
227 [(WXCTabViewImageItem*) tvitem setImage: bmp->GetNSImage(true)];
228
229 NSView *pageNSView = page->GetNSViewForSuperview();
230 // Remove it as a normal child
231 wxASSERT(m_cocoaNSView == [pageNSView superview]);
232 [pageNSView removeFromSuperview];
233 // And make it a notebook page
234 [tvitem setView: pageNSView];
235
236 [GetNSTabView() insertTabViewItem:tvitem atIndex:pos];
237 [tvitem release];
238
239 return true;
240 }
241
242 bool wxNotebook::DeleteAllPages()
243 {
244 while(!m_pages.IsEmpty())
245 DeletePage(0);
246 return true;
247 }
248
249
250 bool wxNotebook::SetPageText(size_t nPage, const wxString& title)
251 {
252 NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage];
253 if(!tvitem)
254 return false;
255 [tvitem setLabel: wxNSStringWithWxString(title)];
256 return true;
257 }
258
259 wxString wxNotebook::GetPageText(size_t nPage) const
260 {
261 return wxStringWithNSString([[GetNSTabView() tabViewItemAtIndex: nPage] label]);
262 }
263
264
265 int wxNotebook::GetPageImage(size_t nPage) const
266 {
267 // To do this we'd need to keep track of this, which we don't!
268 return -1;
269 }
270
271 bool wxNotebook::SetPageImage(size_t nPage, int nImage)
272 {
273 const wxBitmap *bmp = nImage!=-1?m_imageList->GetBitmapPtr(nImage):NULL;
274 if(!bmp)
275 return false;
276 NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage];
277 if(!tvitem)
278 return false;
279 [(WXCTabViewImageItem*) tvitem setImage: bmp->GetNSImage(true)];
280 return true;
281 }
282
283 int wxNotebook::SetSelection(size_t nPage)
284 {
285 const int pageOld = GetSelection();
286
287 if ( !SendPageChangingEvent(nPage) )
288 return pageOld;
289
290 int page = ChangeSelection(nPage);
291 if ( page != wxNOT_FOUND )
292 {
293 SendPageChangedEvent(pageOld);
294 }
295
296 return page;
297 }
298
299 int wxNotebook::ChangeSelection(size_t nPage)
300 {
301 wxAutoNSAutoreleasePool pool;
302 [GetNSTabView() selectTabViewItemAtIndex:nPage];
303 return GetSelection();
304 }
305
306 int wxNotebook::GetSelection() const
307 {
308 NSTabViewItem *selectedItem = [GetNSTabView() selectedTabViewItem];
309 if(!selectedItem)
310 return -1;
311 return [GetNSTabView() indexOfTabViewItem:selectedItem];
312 }
313
314 void wxNotebook::CocoaDelegate_tabView_didSelectTabViewItem(WX_NSTabViewItem tabViewItem)
315 {
316 // FIXME: oldSel probably == newSel
317 wxBookCtrlEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, GetId(),
318 [GetNSTabView() indexOfTabViewItem:tabViewItem], GetSelection());
319 event.SetEventObject(this);
320 GetEventHandler()->ProcessEvent(event);
321 }
322
323 bool wxNotebook::CocoaDelegate_tabView_shouldSelectTabViewItem(WX_NSTabViewItem tabViewItem)
324 {
325 wxBookCtrlEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, GetId(),
326 [GetNSTabView() indexOfTabViewItem:tabViewItem], GetSelection());
327 event.SetEventObject(this);
328 return !HandleWindowEvent(event) || event.IsAllowed();
329 }
330
331 #endif // wxUSE_NOTEBOOK