]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/notebook.mm
If no colour name then use "BLACK"
[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];
67}
68
69- (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel
70{
71 NSSize labelSize = [super sizeOfLabel:shouldTruncateLabel];
72 if(!m_image)
73 return labelSize;
74 NSSize imageSize = [m_image size];
75 // scale image size
76 if(imageSize.height > labelSize.height)
77 {
78 imageSize.width *= labelSize.height/imageSize.height;
79 imageSize.height *= labelSize.height/imageSize.height;
80 [m_image setScalesWhenResized:YES];
81 [m_image setSize: imageSize];
82 }
83 labelSize.width += imageSize.width;
84 return labelSize;
85}
86
87- (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect
88{
89 if(m_image)
90 {
91 NSSize imageSize = [m_image size];
92 [m_image compositeToPoint:NSMakePoint(tabRect.origin.x,
93 tabRect.origin.y+imageSize.height)
94 operation:NSCompositeSourceOver];
95 tabRect.size.width -= imageSize.width;
96 tabRect.origin.x += imageSize.width;
97 }
98 [super drawLabel:shouldTruncateLabel inRect:tabRect];
99}
100
101- (NSImage*)image
102{
103 return m_image;
104}
105
106- (void)setImage:(NSImage*)image
107{
108 [image retain];
109 [m_image release];
110 m_image = image;
111 if(!m_image)
112 return;
113 [[NSPasteboard generalPasteboard]
114 declareTypes:[NSArray arrayWithObject:NSTIFFPboardType]
115 owner:nil];
116 [[NSPasteboard generalPasteboard]
117 setData:[m_image TIFFRepresentation]
118 forType:NSTIFFPboardType];
119}
120
121@end // implementation WXCTabViewImageItem : NSTabViewItem
122
123// ========================================================================
124// wxNotebookEvent
125// ========================================================================
126DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED)
127DEFINE_EVENT_TYPE(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING)
128IMPLEMENT_DYNAMIC_CLASS(wxNotebookEvent, wxNotifyEvent)
129
130// ========================================================================
131// wxNotebook
132// ========================================================================
133IMPLEMENT_DYNAMIC_CLASS(wxNotebook, wxControl)
134BEGIN_EVENT_TABLE(wxNotebook, wxNotebookBase)
135END_EVENT_TABLE()
136WX_IMPLEMENT_COCOA_OWNER(wxNotebook,NSTabView,NSView,NSView)
137
138bool wxNotebook::Create(wxWindow *parent, wxWindowID winid,
139 const wxPoint& pos,
140 const wxSize& size,
141 long style,
142 const wxString& name)
143{
144 wxAutoNSAutoreleasePool pool;
145 if(!CreateControl(parent,winid,pos,size,style,wxDefaultValidator,name))
146 return false;
147 m_cocoaNSView = NULL;
148 SetNSTabView([[NSTabView alloc] initWithFrame:MakeDefaultNSRect(size)]);
149 if(m_parent)
150 m_parent->CocoaAddChild(this);
151 SetInitialFrameRect(pos,size);
152
153 return true;
154}
155
156wxNotebook::~wxNotebook()
157{
158}
159
160void wxNotebook::SetPadding(const wxSize& padding)
161{ // Can't do
162}
163
164void wxNotebook::SetTabSize(const wxSize& sz)
165{ // Can't do
166}
167
168void wxNotebook::SetPageSize(const wxSize& size)
169{
170}
171
172
173wxNotebookPage *wxNotebook::DoRemovePage(size_t nPage)
174{
175 wxNotebookPage *page = wxNotebookBase::DoRemovePage(nPage);
176 if(!page)
177 return NULL;
178 NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage];
179 wxASSERT(tvitem);
180 [tvitem retain];
181 [GetNSTabView() removeTabViewItem:tvitem];
182 // Remove the child window as a notebook page
183 wxASSERT([tvitem view] == page->GetNSViewForSuperview());
184 [tvitem setView:nil];
185 [tvitem release];
186 // Make it back into a normal child window
187 [m_cocoaNSView addSubview: page->GetNSViewForSuperview()];
188
189 return page;
190}
191
192bool wxNotebook::DeletePage(size_t nPage)
193{
194 return wxNotebookBase::DeletePage(nPage);
195}
196
197bool wxNotebook::InsertPage( size_t pos,
198 wxNotebookPage *page, const wxString& title,
199 bool bSelect, int imageId)
200{
201 wxAutoNSAutoreleasePool pool;
202 m_pages.Insert(page,pos);
203 NSTabViewItem *tvitem = [[WXCTabViewImageItem alloc] initWithIdentifier:nil];
204 [tvitem setLabel: wxNSStringWithWxString(title)];
205 const wxBitmap *bmp = (imageId!=-1)?m_imageList->GetBitmap(imageId):NULL;
206 if(bmp)
207 [(WXCTabViewImageItem*) tvitem setImage: bmp->GetNSImage(true)];
208
209 NSView *pageNSView = page->GetNSViewForSuperview();
210 // Remove it as a normal child
211 wxASSERT(m_cocoaNSView == [pageNSView superview]);
212 [pageNSView removeFromSuperview];
213 // And make it a notebook page
214 [tvitem setView: pageNSView];
215
216 [GetNSTabView() insertTabViewItem:tvitem atIndex:pos];
217 [tvitem release];
218
219 return true;
220}
221
222bool wxNotebook::DeleteAllPages()
223{
224 while(!m_pages.IsEmpty())
225 DeletePage(0);
226 return true;
227}
228
229
230bool wxNotebook::SetPageText(size_t nPage, const wxString& title)
231{
232 NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage];
233 if(!tvitem)
234 return false;
235 [tvitem setLabel: wxNSStringWithWxString(title)];
236 return true;
237}
238
239wxString wxNotebook::GetPageText(size_t nPage) const
240{
241 return wxStringWithNSString([[GetNSTabView() tabViewItemAtIndex: nPage] label]);
242}
243
244
245int wxNotebook::GetPageImage(size_t nPage) const
246{
247 // To do this we'd need to keep track of this, which we don't!
248 return -1;
249}
250
251bool wxNotebook::SetPageImage(size_t nPage, int nImage)
252{
253 const wxBitmap *bmp = nImage!=-1?m_imageList->GetBitmap(nImage):NULL;
254 if(!bmp)
255 return false;
256 NSTabViewItem *tvitem = [GetNSTabView() tabViewItemAtIndex: nPage];
257 if(!tvitem)
258 return false;
259 [(WXCTabViewImageItem*) tvitem setImage: bmp->GetNSImage(true)];
260 return true;
261}
262
263
264int wxNotebook::SetSelection(size_t nPage)
265{
264c023f 266 wxAutoNSAutoreleasePool pool;
78480884
DE
267 [GetNSTabView() selectTabViewItemAtIndex:nPage];
268 return GetSelection();
269}
270
271int wxNotebook::GetSelection() const
272{
273 NSTabViewItem *selectedItem = [GetNSTabView() selectedTabViewItem];
274 if(!selectedItem)
275 return -1;
276 return [GetNSTabView() indexOfTabViewItem:selectedItem];
277}
278
279void wxNotebook::CocoaDelegate_tabView_didSelectTabViewItem(WX_NSTabViewItem tabViewItem)
280{
281 // FIXME: oldSel probably == newSel
282 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, GetId(),
283 [GetNSTabView() indexOfTabViewItem:tabViewItem], GetSelection());
284 event.SetEventObject(this);
285}
286
287bool wxNotebook::CocoaDelegate_tabView_shouldSelectTabViewItem(WX_NSTabViewItem tabViewItem)
288{
289 wxNotebookEvent event(wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING, GetId(),
290 [GetNSTabView() indexOfTabViewItem:tabViewItem], GetSelection());
291 event.SetEventObject(this);
292 return !GetEventHandler()->ProcessEvent(event) || event.IsAllowed();
293}
294
28b22ccc 295#endif // wxUSE_NOTEBOOK