]> git.saurik.com Git - wxWidgets.git/blobdiff - src/osx/carbon/fontdlgosx.mm
use correct scale when drawing
[wxWidgets.git] / src / osx / carbon / fontdlgosx.mm
index 7b771bd1e4eaf3623ba31bbf7d3da2cd593ec166..0ac8bd7d55b8cab2b823a4f8f2738e836c351813 100644 (file)
@@ -1,5 +1,5 @@
 /////////////////////////////////////////////////////////////////////////////
 /////////////////////////////////////////////////////////////////////////////
-// Name:        src/mac/carbon/fontdlgosx.cpp
+// Name:        src/osx/carbon/fontdlgosx.mm
 // Purpose:     wxFontDialog class.
 // Author:      Ryan Norton
 // Modified by:
 // Purpose:     wxFontDialog class.
 // Author:      Ryan Norton
 // Modified by:
@@ -28,6 +28,7 @@
 #endif
 
 #include "wx/fontutil.h"
 #endif
 
 #include "wx/fontutil.h"
+#include "wx/modalhook.h"
 
 // ============================================================================
 // implementation
 
 // ============================================================================
 // implementation
@@ -42,7 +43,7 @@
 #import <Foundation/Foundation.h>
 #import <AppKit/AppKit.h>
 
 #import <Foundation/Foundation.h>
 #import <AppKit/AppKit.h>
 
-#include "wx/osx/uma.h"
+#include "wx/osx/private.h"
 
 @interface wxMacFontPanelAccView : NSView
 {
 
 @interface wxMacFontPanelAccView : NSView
 {
@@ -68,8 +69,8 @@
     wxCFStringRef cfOkString( wxT("OK"), wxLocale::GetSystemEncoding() );
     wxCFStringRef cfCancelString( wxT("Cancel"), wxLocale::GetSystemEncoding() );
 
     wxCFStringRef cfOkString( wxT("OK"), wxLocale::GetSystemEncoding() );
     wxCFStringRef cfCancelString( wxT("Cancel"), wxLocale::GetSystemEncoding() );
 
-    NSRect rectCancel = NSMakeRect( 10.0 , 10.0 , 82  , 24 );
-    NSRect rectOK = NSMakeRect( 100.0 , 10.0 , 82  , 24 );
+    NSRect rectCancel = NSMakeRect( (CGFloat) 10.0 , (CGFloat)10.0 , (CGFloat)82  , (CGFloat)24 );
+    NSRect rectOK = NSMakeRect( (CGFloat)100.0 , (CGFloat)10.0 , (CGFloat)82  , (CGFloat)24 );
 
     NSButton* cancelButton = [[NSButton alloc] initWithFrame:rectCancel];
     [cancelButton setTitle:(NSString*)wxCFRetain((CFStringRef)cfCancelString)];
 
     NSButton* cancelButton = [[NSButton alloc] initWithFrame:rectCancel];
     [cancelButton setTitle:(NSString*)wxCFRetain((CFStringRef)cfCancelString)];
 
 extern "C" int RunMixedFontDialog(wxFontDialog* dialog) ;
 
 
 extern "C" int RunMixedFontDialog(wxFontDialog* dialog) ;
 
-int RunMixedFontDialog(wxFontDialog* WXUNUSED(dialog))
+int RunMixedFontDialog(wxFontDialog* dialog)
 {
 {
+#if wxOSX_USE_COCOA
+    wxFontData& fontdata= ((wxFontDialog*)dialog)->GetFontData() ;
+#else
+    wxUnusedVar(dialog);
+#endif
     int retval = wxID_CANCEL ;
 
     int retval = wxID_CANCEL ;
 
-    if ( !NSApplicationLoad() )
-    {
-        wxFAIL_MSG("Couldn't load Cocoa in Carbon Environment");
-    }
-
     wxAutoNSAutoreleasePool pool;
 
     // setting up the ok/cancel buttons
     NSFontPanel* fontPanel = [NSFontPanel sharedFontPanel] ;
 
     // adjust modality for carbon environment
     wxAutoNSAutoreleasePool pool;
 
     // setting up the ok/cancel buttons
     NSFontPanel* fontPanel = [NSFontPanel sharedFontPanel] ;
 
     // adjust modality for carbon environment
+#if wxOSX_USE_CARBON
     WindowRef carbonWindowRef = (WindowRef)[fontPanel windowRef] ;
     SetWindowModality(carbonWindowRef, kWindowModalityAppModal , 0) ;
     SetWindowGroup(carbonWindowRef , GetWindowGroupOfClass(kMovableModalWindowClass));
     WindowRef carbonWindowRef = (WindowRef)[fontPanel windowRef] ;
     SetWindowModality(carbonWindowRef, kWindowModalityAppModal , 0) ;
     SetWindowGroup(carbonWindowRef , GetWindowGroupOfClass(kMovableModalWindowClass));
+#endif
 
     [fontPanel setFloatingPanel:NO] ;
     [[fontPanel standardWindowButton:NSWindowCloseButton] setEnabled:NO] ;
 
     [fontPanel setFloatingPanel:NO] ;
     [[fontPanel standardWindowButton:NSWindowCloseButton] setEnabled:NO] ;
@@ -165,28 +168,60 @@ int RunMixedFontDialog(wxFontDialog* WXUNUSED(dialog))
         NSRect rectBox = NSMakeRect( 0 , 0 , 192 , 40 );
         accessoryView = [[wxMacFontPanelAccView alloc] initWithFrame:rectBox];
         [fontPanel setAccessoryView:accessoryView];
         NSRect rectBox = NSMakeRect( 0 , 0 , 192 , 40 );
         accessoryView = [[wxMacFontPanelAccView alloc] initWithFrame:rectBox];
         [fontPanel setAccessoryView:accessoryView];
+        [accessoryView release];
 
         [fontPanel setDefaultButtonCell:[[accessoryView okButton] cell]] ;
     }
 
     [accessoryView resetFlags];
 
         [fontPanel setDefaultButtonCell:[[accessoryView okButton] cell]] ;
     }
 
     [accessoryView resetFlags];
+#if wxOSX_USE_COCOA
+    wxFont font = *wxNORMAL_FONT ;
+    if ( fontdata.m_initialFont.IsOk() )
+    {
+        font = fontdata.m_initialFont ;
+    }
 
 
-    NSModalSession session = [NSApp beginModalSessionForWindow:fontPanel];
-
-    [NSApp runModalSession:session];
-
-    [NSApp endModalSession:session];
+    [[NSFontPanel sharedFontPanel] setPanelFont: font.OSXGetNSFont() isMultiple:NO];
 
 
+    if(fontdata.m_fontColour.IsOk())
+        [[NSColorPanel sharedColorPanel] setColor:
+            [NSColor colorWithCalibratedRed:fontdata.m_fontColour.Red() / 255.0
+                                        green:fontdata.m_fontColour.Green() / 255.0
+                                        blue:fontdata.m_fontColour.Blue() / 255.0
+                                        alpha:1.0]
+        ];
+    else
+        [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
+#endif
+    
+    [NSApp runModalForWindow:fontPanel];
+    
     // if we don't reenable it, FPShowHideFontPanel does not work
     [[fontPanel standardWindowButton:NSWindowCloseButton] setEnabled:YES] ;
     // if we don't reenable it, FPShowHideFontPanel does not work
     [[fontPanel standardWindowButton:NSWindowCloseButton] setEnabled:YES] ;
+#if wxOSX_USE_CARBON
     if( FPIsFontPanelVisible())
         FPShowHideFontPanel() ;
     if( FPIsFontPanelVisible())
         FPShowHideFontPanel() ;
+#else
+    [fontPanel close];
+#endif
 
     if ( [accessoryView closedWithOk])
     {
 
     if ( [accessoryView closedWithOk])
     {
+#if wxOSX_USE_COCOA
+        NSFont* theFont = [fontPanel panelConvertFont:[NSFont userFontOfSize:0]];
+
+        fontdata.m_chosenFont = wxFont( theFont );
+
+        //Get the shared color panel along with the chosen color and set the chosen color
+        NSColor* theColor = [[[NSColorPanel sharedColorPanel] color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
+
+        fontdata.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
+                                    (unsigned char) ([theColor greenComponent] * 255.0),
+                                    (unsigned char) ([theColor blueComponent] * 255.0));
+#endif
         retval = wxID_OK ;
     }
         retval = wxID_OK ;
     }
-
+    [fontPanel setAccessoryView:nil];
 
     return retval ;
 }
 
     return retval ;
 }
@@ -317,6 +352,11 @@ wxFontDialog::wxFontDialog()
 {
 }
 
 {
 }
 
+wxFontDialog::wxFontDialog(wxWindow *parent)
+{
+    Create(parent);
+}
+
 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData&  data)
 {
     Create(parent, data);
 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData&  data)
 {
     Create(parent, data);
@@ -326,22 +366,20 @@ wxFontDialog::~wxFontDialog()
 {
 }
 
 {
 }
 
+bool wxFontDialog::Create(wxWindow *parent)
+{
+    return Create(parent);
+}
+
 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
 {
     m_fontData = data;
 
 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
 {
     m_fontData = data;
 
-    //
-    //  This is the key call - this initializes
-    //  events and window stuff for cocoa for carbon
-    //  applications.
-    //
-    //  This is also the only call here that is
-    //  10.2+ specific (the rest is OSX only),
-    //  which, ironically, the carbon font
-    //  panel requires.
-    //
-    bool bOK = NSApplicationLoad();
+    return Create(parent);
+}
 
 
+bool wxFontDialog::Create(wxWindow *parent)
+{
     //autorelease pool - req'd for carbon
     NSAutoreleasePool *thePool;
     thePool = [[NSAutoreleasePool alloc] init];
     //autorelease pool - req'd for carbon
     NSAutoreleasePool *thePool;
     thePool = [[NSAutoreleasePool alloc] init];
@@ -351,7 +389,7 @@ bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
 
     //if the font is valid set the default (selected) font of the
     //NSFontDialog to that font
 
     //if the font is valid set the default (selected) font of the
     //NSFontDialog to that font
-    if (thewxfont.Ok())
+    if (thewxfont.IsOk())
     {
         NSFontTraitMask theMask = 0;
 
     {
         NSFontTraitMask theMask = 0;
 
@@ -379,7 +417,7 @@ bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
 
     }
 
 
     }
 
-    if(m_fontData.m_fontColour.Ok())
+    if(m_fontData.m_fontColour.IsOk())
         [[NSColorPanel sharedColorPanel] setColor:
             [NSColor colorWithCalibratedRed:m_fontData.m_fontColour.Red() / 255.0
                                         green:m_fontData.m_fontColour.Green() / 255.0
         [[NSColorPanel sharedColorPanel] setColor:
             [NSColor colorWithCalibratedRed:m_fontData.m_fontColour.Red() / 255.0
                                         green:m_fontData.m_fontColour.Green() / 255.0
@@ -392,11 +430,13 @@ bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
     //We're done - free up the pool
     [thePool release];
 
     //We're done - free up the pool
     [thePool release];
 
-    return bOK;
+    return true;
 }
 
 int wxFontDialog::ShowModal()
 {
 }
 
 int wxFontDialog::ShowModal()
 {
+    WX_HOOK_MODAL_DIALOG();
+
     //Start the pool.  Required for carbon interaction
     //(For those curious, the only thing that happens
     //if you don't do this is a bunch of error
     //Start the pool.  Required for carbon interaction
     //(For those curious, the only thing that happens
     //if you don't do this is a bunch of error
@@ -431,6 +471,7 @@ int wxFontDialog::ShowModal()
     //  the color panel until the color panel closes, switching
     //  back to the font panel modal loop once it does close.
     //
     //  the color panel until the color panel closes, switching
     //  back to the font panel modal loop once it does close.
     //
+    wxDialog::OSXBeginModalDialog();
     do
     {
         //
     do
     {
         //
@@ -469,7 +510,8 @@ int wxFontDialog::ShowModal()
         //out of its modal loop because the color panel was
         //opened) return the font panel modal loop
     }while([theFPDelegate isClosed] == NO);
         //out of its modal loop because the color panel was
         //opened) return the font panel modal loop
     }while([theFPDelegate isClosed] == NO);
-
+    wxDialog::OSXEndModalDialog();
+    
     //free up the memory for the delegates - we don't need them anymore
     [theFPDelegate release];
     [theCPDelegate release];
     //free up the memory for the delegates - we don't need them anymore
     [theFPDelegate release];
     [theCPDelegate release];