1 /////////////////////////////////////////////////////////////////////////////
 
   2 // Name:        src/cocoa/dialog.mm
 
   3 // Purpose:     wxDialog class
 
   4 // Author:      David Elliott
 
   8 // Copyright:   2002 David Elliott
 
   9 // Licence:     wxWidgets licence
 
  10 /////////////////////////////////////////////////////////////////////////////
 
  12 #include "wx/wxprec.h"
 
  14 #include "wx/dialog.h"
 
  19     #include "wx/settings.h"
 
  22 #include "wx/cocoa/autorelease.h"
 
  23 #include "wx/cocoa/string.h"
 
  25 #import <AppKit/NSPanel.h>
 
  26 #import <AppKit/NSApplication.h>
 
  27 #import <AppKit/NSEvent.h>
 
  28 #import <Foundation/NSRunLoop.h>
 
  30 // Lists to keep track of windows, so we can disable/enable them
 
  32 static wxWindowList wxModalDialogs;
 
  34 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
 
  36 BEGIN_EVENT_TABLE(wxDialog, wxDialogBase)
 
  37     EVT_BUTTON(wxID_OK, wxDialog::OnOK)
 
  38     EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
 
  39     EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
 
  40     EVT_CLOSE(wxDialog::OnCloseWindow)
 
  43 WX_IMPLEMENT_COCOA_OWNER(wxDialog,NSPanel,NSWindow,NSWindow)
 
  48     SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
 
  51 bool wxDialog::Create(wxWindow *parent, wxWindowID winid,
 
  52            const wxString& title,
 
  58     wxAutoNSAutoreleasePool pool;
 
  59     wxTopLevelWindows.Append(this);
 
  61     if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
 
  65         parent->AddChild(this);
 
  67     NSRect cocoaRect = NSMakeRect(300,300,200,200);
 
  69     unsigned int cocoaStyle = 0;
 
  70     cocoaStyle |= NSTitledWindowMask;
 
  71     cocoaStyle |= NSClosableWindowMask;
 
  72     cocoaStyle |= NSMiniaturizableWindowMask;
 
  73     cocoaStyle |= NSResizableWindowMask;
 
  75     m_cocoaNSWindow = NULL;
 
  76     SetNSPanel([[NSPanel alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
 
  77     // NOTE: SetNSWindow has retained the Cocoa object for this object.
 
  78     // Because we do not release on close, the following release matches the
 
  79     // above alloc and thus the retain count will be 1.
 
  80     [m_cocoaNSWindow release];
 
  81     wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxDialog m_cocoaNSWindow retainCount=%d"),[m_cocoaNSWindow retainCount]);
 
  82     [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
 
  83     [m_cocoaNSWindow setHidesOnDeactivate:NO];
 
  90     DisassociateNSPanel(GetNSPanel());
 
  93 void wxDialog::CocoaDelegate_windowWillClose(void)
 
  96     /* Actually, this isn't true anymore */
 
  97     wxLogTrace(wxTRACE_COCOA,wxT("Woah: Dialogs are not generally closed"));
 
 100 void wxDialog::SetModal(bool flag)
 
 102     wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") );
 
 105 bool wxDialog::Show(bool show)
 
 107     if(m_isShown == show)
 
 112         wxAutoNSAutoreleasePool pool;
 
 115         {   // ShowModal() will show the dialog
 
 123         {   // this doesn't hide the dialog, base class Show(false) does.
 
 124             wxLogTrace(wxTRACE_COCOA,wxT("abortModal"));
 
 125             [wxTheApp->GetNSApplication() abortModal];
 
 126             wxModalDialogs.DeleteObject(this);
 
 130     return wxTopLevelWindow::Show(show);
 
 133 // Shows the dialog and begins a modal event loop.  When the event loop
 
 134 // is stopped (via EndModal()) it returns the exit code.
 
 135 int wxDialog::ShowModal()
 
 137     wxCHECK_MSG(!IsModal(),GetReturnCode(),wxT("wxDialog::ShowModal called within its own modal loop"));
 
 139     // Show(true) will set m_isShown = true
 
 142     wxModalDialogs.Append(this);
 
 144     wxLogTrace(wxTRACE_COCOA,wxT("runModal"));
 
 145     NSApplication *theNSApp = wxTheApp->GetNSApplication();
 
 146     // If the app hasn't started, flush the event queue
 
 147     // If we don't do this, the Dock doesn't get the message that
 
 148     // the app has started so will refuse to activate it.
 
 149     if(![theNSApp isRunning])
 
 151         // We should only do a few iterations so one pool should be okay
 
 152         wxAutoNSAutoreleasePool pool;
 
 153         while(NSEvent *event = [theNSApp
 
 154                     nextEventMatchingMask:NSAnyEventMask
 
 155                     untilDate:[NSDate distantPast]
 
 156                     inMode:NSDefaultRunLoopMode
 
 159             [theNSApp sendEvent: event];
 
 165         wxAutoNSAutoreleasePool pool;
 
 166         [wxTheApp->GetNSApplication() runModalForWindow:m_cocoaNSWindow];
 
 168     wxLogTrace(wxTRACE_COCOA,wxT("runModal END"));
 
 170     return GetReturnCode();
 
 173 void wxDialog::EndModal(int retCode)
 
 175     wxASSERT_MSG(IsModal(), wxT("EndModal() should only be used within ShowModal()"));
 
 176     SetReturnCode(retCode);
 
 180 void wxDialog::EndDialog(int retCode)
 
 188 void wxDialog::OnCloseWindow(wxCloseEvent& event)
 
 190     // We'll send a Cancel message by default,
 
 191     // which may close the dialog.
 
 192     // Check for looping if the Cancel event handler calls Close().
 
 194     // Note that if a cancel button and handler aren't present in the dialog,
 
 195     // nothing will happen when you close the dialog via the window manager, or
 
 197     // We wouldn't want to destroy the dialog by default, since the dialog may have been
 
 198     // created on the stack.
 
 199     // However, this does mean that calling dialog->Close() won't delete the dialog
 
 200     // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
 
 201     // sure to destroy the dialog.
 
 202     // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
 
 203     // ALWAYS VETO THIS EVENT!!!!
 
 206     static wxList closing;
 
 208     if ( closing.Member(this) )
 
 210         wxLogDebug(wxT("WARNING: Attempting to recursively call Close for dialog"));
 
 214     closing.Append(this);
 
 216     wxLogTrace(wxTRACE_COCOA,wxT("Sending Cancel Event"));
 
 217     wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
 
 218     cancelEvent.SetEventObject( this );
 
 219     GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
 
 221     closing.DeleteObject(this);
 
 225 void wxDialog::OnOK(wxCommandEvent& event)
 
 227     if ( Validate() && TransferDataFromWindow() )
 
 233 void wxDialog::OnApply(wxCommandEvent& event)
 
 236         TransferDataFromWindow();
 
 237     // TODO probably need to disable the Apply button until things change again
 
 240 void wxDialog::OnCancel(wxCommandEvent& event)
 
 242     wxLogTrace(wxTRACE_COCOA,wxT("Cancelled!"));
 
 243     EndDialog(wxID_CANCEL);