Add wxMessageDialog::GetEffectiveIcon() and use it in all ports.
[wxWidgets.git] / src / cocoa / control.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/cocoa/control.mm
3 // Purpose:     wxControl class
4 // Author:      David Elliiott
5 // Modified by:
6 // Created:     2003/02/15
7 // RCS-ID:      $Id$
8 // Copyright:   (c) 2003 David Elliott
9 // Licence:     wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/control.h"
15
16 #ifndef WX_PRECOMP
17     #include "wx/log.h"
18 #endif
19
20 #include "wx/cocoa/autorelease.h"
21 #include "wx/cocoa/string.h"
22 #include "wx/cocoa/trackingrectmanager.h"
23 #include "wx/cocoa/objc/objc_uniquifying.h"
24 #include "wx/cocoa/objc/NSView.h"
25
26 #import <AppKit/NSControl.h>
27 #import <AppKit/NSCell.h>
28 #import <Foundation/NSException.h>
29
30 #include <math.h>
31
32 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
33 BEGIN_EVENT_TABLE(wxControl, wxControlBase)
34 END_EVENT_TABLE()
35 WX_IMPLEMENT_COCOA_OWNER(wxControl,NSControl,NSView,NSView)
36
37 bool wxControl::Create(wxWindow *parent, wxWindowID winid,
38             const wxPoint& pos, const wxSize& size, long style,
39             const wxValidator& validator, const wxString& name)
40 {
41     wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
42     if(!CreateControl(parent,winid,pos,size,style,validator,name))
43         return false;
44     wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId());
45     m_cocoaNSView = NULL;
46     SetNSControl([[WX_GET_OBJC_CLASS(WXNSView) alloc] initWithFrame: MakeDefaultNSRect(size)]);
47     // NOTE: YES we want to release this (to match the alloc).
48     // DoAddChild(this) will retain us again since addSubView doesn't.
49     [m_cocoaNSView release];
50
51     if(m_parent)
52         m_parent->CocoaAddChild(this);
53     SetInitialFrameRect(pos,size);
54
55     // Controls should have a viewable-area tracking rect by default
56     m_visibleTrackingRectManager = new wxCocoaTrackingRectManager(this);
57
58     return true;
59 }
60
61 wxControl::~wxControl()
62 {
63     DisassociateNSControl(GetNSControl());
64 }
65
66 wxSize wxControl::DoGetBestSize() const
67 {
68     wxAutoNSAutoreleasePool pool;
69     wxASSERT(GetNSControl());
70     /* We can ask single-celled controls for their cell and get its size */
71     NSCell *cell = nil;
72     if([GetNSControl() respondsToSelector:@selector(cell)])
73         cell = [GetNSControl() cell];
74     if(cell)
75     {
76         NSSize cellSize = [cell cellSize];
77         wxSize size((int)ceil(cellSize.width),(int)ceil(cellSize.height));
78         wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from NSCell"),this,size.x,size.y);
79         return size;
80     }
81
82     /* multi-celled control? size to fit, get the size, then set it back */
83     if([GetNSControl() respondsToSelector:@selector(sizeToFit)])
84     {
85         NSRect storedRect = [m_cocoaNSView frame];
86         [GetNSControl() sizeToFit];
87         NSRect cocoaRect = [m_cocoaNSView frame];
88         wxSize size((int)ceil(cocoaRect.size.width),(int)ceil(cocoaRect.size.height));
89         [m_cocoaNSView setFrame: storedRect];
90         wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from sizeToFit"),this,size.x,size.y);
91         return size;
92     }
93     // Cocoa can't tell us the size, probably not an NSControl.
94     wxLogDebug(wxT("Class %s (or superclass still below wxControl) should implement DoGetBestSize()"),GetClassInfo()->GetClassName());
95     return wxControlBase::DoGetBestSize();
96 }
97
98 bool wxControl::ProcessCommand(wxCommandEvent& event)
99 {
100     return HandleWindowEvent(event);
101 }
102
103 void wxControl::CocoaSetEnabled(bool enable)
104 {
105     if([GetNSControl() respondsToSelector:@selector(setEnabled:)])
106         [GetNSControl() setEnabled: enable];
107 }
108
109 /*static*/ void wxControl::CocoaSetLabelForObject(const wxString& label, struct objc_object *aView)
110 {
111     [aView setTitle:wxNSStringWithWxString(GetLabelText(label))];
112 }
113