]>
Commit | Line | Data |
---|---|---|
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 | // ======================================================================== | |
127 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED) | |
128 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING) | |
129 | IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent) | |
130 | ||
131 | // ======================================================================== | |
132 | // wxNotebook | |
133 | // ======================================================================== | |
134 | IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl) | |
135 | BEGIN_EVENT_TABLE(wxNotebook, wxNotebookBase) | |
136 | END_EVENT_TABLE() | |
137 | WX_IMPLEMENT_COCOA_OWNER(wxNotebook,NSTabView,NSView,NSView) | |
138 | ||
139 | bool 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 | ||
157 | wxNotebook::~wxNotebook() | |
158 | { | |
159 | } | |
160 | ||
161 | void wxNotebook::SetPadding(const wxSize& padding) | |
162 | { // Can't do | |
163 | } | |
164 | ||
165 | void wxNotebook::SetTabSize(const wxSize& sz) | |
166 | { // Can't do | |
167 | } | |
168 | ||
169 | void wxNotebook::SetPageSize(const wxSize& size) | |
170 | { | |
171 | } | |
172 | ||
173 | ||
174 | wxNotebookPage *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 | ||
193 | bool wxNotebook::DeletePage(size_t nPage) | |
194 | { | |
195 | return wxNotebookBase::DeletePage(nPage); | |
196 | } | |
197 | ||
198 | bool 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 | ||
223 | bool wxNotebook::DeleteAllPages() | |
224 | { | |
225 | while(!m_pages.IsEmpty()) | |
226 | DeletePage(0); | |
227 | return true; | |
228 | } | |
229 | ||
230 | ||
231 | bool 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 | ||
240 | wxString wxNotebook::GetPageText(size_t nPage) const | |
241 | { | |
242 | return wxStringWithNSString([[GetNSTabView() tabViewItemAtIndex: nPage] label]); | |
243 | } | |
244 | ||
245 | ||
246 | int 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 | ||
252 | bool 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 | ||
264 | ||
265 | int wxNotebook::SetSelection(size_t nPage) | |
266 | { | |
264c023f | 267 | wxAutoNSAutoreleasePool pool; |
78480884 DE |
268 | [GetNSTabView() selectTabViewItemAtIndex:nPage]; |
269 | return GetSelection(); | |
270 | } | |
271 | ||
272 | int wxNotebook::GetSelection() const | |
273 | { | |
274 | NSTabViewItem *selectedItem = [GetNSTabView() selectedTabViewItem]; | |
275 | if(!selectedItem) | |
276 | return -1; | |
277 | return [GetNSTabView() indexOfTabViewItem:selectedItem]; | |
278 | } | |
279 | ||
280 | void wxNotebook::CocoaDelegate_tabView_didSelectTabViewItem(WX_NSTabViewItem tabViewItem) | |
281 | { | |
282 | // FIXME: oldSel probably == newSel | |
283 | wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, GetId(), | |
284 | [GetNSTabView() indexOfTabViewItem:tabViewItem], GetSelection()); | |
285 | event.SetEventObject(this); | |
286 | } | |
287 | ||
288 | bool wxNotebook::CocoaDelegate_tabView_shouldSelectTabViewItem(WX_NSTabViewItem tabViewItem) | |
289 | { | |
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(); | |
294 | } | |
295 | ||
28b22ccc | 296 | #endif // wxUSE_NOTEBOOK |