From: Stefan Csomor Date: Fri, 10 Mar 2006 16:11:29 +0000 (+0000) Subject: adding experimental font dialog version - hacking the font panel ... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/509b4eccfeb7e13a30d3192c4cb1a42b2aed3f70 adding experimental font dialog version - hacking the font panel ... git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@37961 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/mac/carbon/fontdlg.h b/include/wx/mac/carbon/fontdlg.h index 6eaabc4f73..29bf4742b9 100644 --- a/include/wx/mac/carbon/fontdlg.h +++ b/include/wx/mac/carbon/fontdlg.h @@ -15,6 +15,38 @@ #include "wx/dialog.h" #include "wx/cmndata.h" +/* + * Font dialog + */ + +#ifndef wxMAC_USE_EXPERIMENTAL_FONTDIALOG +#define wxMAC_USE_EXPERIMENTAL_FONTDIALOG 0 +#endif + +#if wxMAC_USE_EXPERIMENTAL_FONTDIALOG + +class WXDLLEXPORT wxFontDialog : public wxDialog +{ +public: + wxFontDialog(); + wxFontDialog(wxWindow *parent, const wxFontData& data); + ~wxFontDialog(); + + bool Create(wxWindow *parent, const wxFontData& data); + + int ShowModal(); + wxFontData& GetFontData() { return m_fontData; } + +protected: + wxFontData m_fontData; + + DECLARE_DYNAMIC_CLASS_NO_COPY(wxFontDialog) +}; + +extern "C" int RunMixedFontDialog(wxFontDialog* dialog) ; + +#else // wxMAC_USE_EXPERIMENTAL_FONTDIALOG + #if !USE_NATIVE_FONT_DIALOG_FOR_MACOSX /*! @@ -46,10 +78,6 @@ class WXDLLEXPORT wxCheckBox; #endif // !USE_NATIVE_FONT_DIALOG_FOR_MACOSX -/* - * Font dialog - */ - class WXDLLEXPORT wxFontDialog: public wxDialog { DECLARE_DYNAMIC_CLASS(wxFontDialog) @@ -132,6 +160,8 @@ protected: void* m_pEventHandlerRef; }; +#endif + #endif // _WX_FONTDLG_H_ diff --git a/src/mac/carbon/fontdlg.cpp b/src/mac/carbon/fontdlg.cpp index 67c5ff37a5..b268f07129 100644 --- a/src/mac/carbon/fontdlg.cpp +++ b/src/mac/carbon/fontdlg.cpp @@ -30,6 +30,173 @@ #include "wx/fontdlg.h" +#if wxMAC_USE_EXPERIMENTAL_FONTDIALOG + +IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog) + +#include "wx/mac/private.h" + +// --------------------------------------------------------------------------- +// wxFontDialog +// --------------------------------------------------------------------------- + +static const EventTypeSpec eventList[] = +{ + { kEventClassFont, kEventFontSelection } , +} ; + + +pascal OSStatus wxMacCarbonFontPanelHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData) +{ + OSStatus result = eventNotHandledErr ; + wxFontDialog *fontdialog = (wxFontDialog*) userData ; + wxFontData& fontdata= fontdialog->GetFontData() ; + + wxMacCarbonEvent cEvent( event ); + switch(cEvent.GetKind()) + { + case kEventFontSelection : + { + FMFont fontId = 0 ; + if ( cEvent.GetParameter(kEventParamATSUFontID, &fontId) == noErr ) + { + FMFontStyle fontStyle = cEvent.GetParameter(kEventParamFMFontStyle); + FMFontSize fontSize = cEvent.GetParameter(kEventParamFMFontSize); + + ByteCount actualLength = 0; + CFStringRef cfName = NULL; + char *c = NULL; + OSStatus err = ATSUFindFontName(fontId , kFontFamilyName, kFontUnicodePlatform, kFontNoScriptCode, + kFontNoLanguageCode , 0 , NULL , &actualLength , NULL ); + if ( err == noErr) + { + actualLength += 1 ; + char *c = (char*)malloc( actualLength ); + err = ATSUFindFontName(fontId, kFontFamilyName, kFontUnicodePlatform, kFontNoScriptCode, + kFontNoLanguageCode, actualLength, c , NULL, NULL); + cfName = CFStringCreateWithCharacters(NULL, (UniChar*) c, (actualLength-1) >> 1); + } + else + { + err = ATSUFindFontName(fontId , kFontFamilyName, kFontNoPlatformCode, kFontNoScriptCode, + kFontNoLanguageCode , 0 , NULL , &actualLength , NULL ); + if ( err == noErr ) + { + actualLength += 1 ; + c = (char*)malloc(actualLength); + err = ATSUFindFontName(fontId, kFontFamilyName, kFontNoPlatformCode, kFontNoScriptCode, + kFontNoLanguageCode, actualLength, c , NULL, NULL); + c[actualLength-1] = 0; + cfName = CFStringCreateWithCString(NULL, c, kCFStringEncodingMacRoman ); + } + } + if ( c!=NULL ) + free(c); + + if ( cfName!=NULL ) + { + fontdata.m_chosenFont.SetFaceName(wxMacCFStringHolder(cfName).AsString(wxLocale::GetSystemEncoding())); + fontdata.m_chosenFont.SetPointSize(fontSize); + fontdata.m_chosenFont.SetStyle(fontStyle & italic ? wxFONTSTYLE_ITALIC : 0); + fontdata.m_chosenFont.SetUnderlined(fontStyle & underline ? wxFONTSTYLE_ITALIC : 0); + fontdata.m_chosenFont.SetWeight(fontStyle & bold ? wxBOLD : wxNORMAL); + } + } + + RGBColor fontColor ; + if ( cEvent.GetParameter(kEventParamFontColor, &fontColor) == noErr ) + fontdata.m_fontColour.Set((WXCOLORREF*) &fontColor); + else + { + CFDictionaryRef dict ; + if ( cEvent.GetParameter(kEventParamDictionary, &dict) == noErr ) + { + CFDictionaryRef attributesDict ; + if ( CFDictionaryGetValueIfPresent(dict, kFontPanelAttributesKey, (const void **)&attributesDict) ) + { + CFDataRef tagsData; + CFDataRef sizesData; + CFDataRef valuesData; + if ( CFDictionaryGetValueIfPresent(attributesDict, kFontPanelAttributeTagsKey, (const void **)&tagsData) && + CFDictionaryGetValueIfPresent(attributesDict, kFontPanelAttributeSizesKey, (const void **)&sizesData) && + CFDictionaryGetValueIfPresent(attributesDict, kFontPanelAttributeValuesKey, (const void **)&valuesData) ) + { + ItemCount count = CFDataGetLength(tagsData)/sizeof(ATSUAttributeTag); + ATSUAttributeTag *tagPtr = (ATSUAttributeTag *)CFDataGetBytePtr(tagsData); + ByteCount *sizePtr = (ByteCount *)CFDataGetBytePtr(sizesData); + UInt32 *bytePtr = (UInt32*)CFDataGetBytePtr(valuesData); + ATSUAttributeValuePtr valuesPtr = bytePtr ; + for ( int i = 0 ; i < count ; ++i) + { + if ( tagPtr[i] == kATSUColorTag && sizePtr[i] == sizeof(RGBColor)) + { + fontdata.m_fontColour.Set((WXCOLORREF*) valuesPtr); + break ; + } + bytePtr = (UInt32*)( (UInt8*)bytePtr + sizePtr[i]); + } + } + } + } + } + } + break ; + } ; + + return result ; +} + +DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacCarbonFontPanelHandler ) + +wxFontDialog::wxFontDialog() +{ +} + +wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data) +{ + Create(parent, data); +} + +wxFontDialog::~wxFontDialog() +{ +} + +bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data) +{ + m_fontData = data; + return true ; +} + +int wxFontDialog::ShowModal() +{ + OSStatus err ; + wxFont font = *wxNORMAL_FONT ; + if ( m_fontData.m_initialFont.Ok() ) + { + font = m_fontData.m_initialFont ; + } + + ATSUStyle style = (ATSUStyle)font.MacGetATSUStyle(); + err = SetFontInfoForSelection (kFontSelectionATSUIType,1, &style , NULL); + // just clicking on ENTER will not send us any font setting event, therefore we have to make sure + // that field is already correct + m_fontData.m_chosenFont = font ; + + EventHandlerRef handler ; + + err = InstallApplicationEventHandler( GetwxMacCarbonFontPanelHandlerUPP(), GetEventTypeCount(eventList), eventList, this , &handler ); + + if ( !FPIsFontPanelVisible() ) + FPShowHideFontPanel(); + + int retval = RunMixedFontDialog(this); + + ::RemoveEventHandler(handler); + + return retval ; +} + +#else #if !USE_NATIVE_FONT_DIALOG_FOR_MACOSX @@ -597,3 +764,4 @@ int FontFamilyStringToInt(const wxChar *family) #endif // !USE_NATIVE_FONT_DIALOG_FOR_MACOSX +#endif diff --git a/src/mac/carbon/fontdlgosx.mm b/src/mac/carbon/fontdlgosx.mm index 968d740727..b3fdb9d80e 100644 --- a/src/mac/carbon/fontdlgosx.mm +++ b/src/mac/carbon/fontdlgosx.mm @@ -26,14 +26,165 @@ // implementation // ============================================================================ -//Mac OSX 10.2+ only + +#include "wx/cocoa/autorelease.h" +#include "wx/cocoa/string.h" + +#if wxMAC_USE_EXPERIMENTAL_FONTDIALOG + +#import +#import + +#include "wx/mac/uma.h" + +@interface wxMacFontPanelAccView : NSView +{ + BOOL m_okPressed ; + BOOL m_shouldClose ; + NSButton* m_cancelButton ; + NSButton* m_okButton ; +} + +- (IBAction)cancelPressed:(id)sender; +- (IBAction)okPressed:(id)sender; +- (void)resetFlags; +- (BOOL)closedWithOk; +- (BOOL)shouldCloseCarbon; +- (NSButton*)okButton; +@end + +@implementation wxMacFontPanelAccView : NSView +- (id)initWithFrame:(NSRect)rectBox +{ + [super initWithFrame:rectBox]; + + wxMacCFStringHolder cfOkString( wxT("OK"), wxLocale::GetSystemEncoding() ); + wxMacCFStringHolder cfCancelString( wxT("Cancel"), wxLocale::GetSystemEncoding() ); + + NSRect rectCancel = NSMakeRect( 10.0 , 10.0 , 82 , 24 ); + NSRect rectOK = NSMakeRect( 100.0 , 10.0 , 82 , 24 ); + NSView* panel = [[NSView alloc] initWithFrame:rectBox]; + + NSButton* cancelButton = [[NSButton alloc] initWithFrame:rectCancel]; + [cancelButton setTitle:(NSString*)cfCancelString.Detach()]; + [cancelButton setBezelStyle:NSRoundedBezelStyle]; + [cancelButton setButtonType:NSMomentaryPushInButton]; + [cancelButton setAction:@selector(cancelPressed:)]; + [cancelButton setTarget:self]; + m_cancelButton = cancelButton ; + + NSButton* okButton = [[NSButton alloc] initWithFrame:rectOK]; + [okButton setTitle:(NSString*)cfOkString.Detach()]; + [okButton setBezelStyle:NSRoundedBezelStyle]; + [okButton setButtonType:NSMomentaryPushInButton]; + [okButton setAction:@selector(okPressed:)]; + [okButton setTarget:self]; + // doesn't help either, the button is not highlighted after a color dialog has been used + // [okButton setKeyEquivalent:@"\r"]; + m_okButton = okButton ; + + + [self addSubview:cancelButton]; + [self addSubview:okButton]; + + [self resetFlags]; + return self; +} + +- (void)resetFlags +{ + m_okPressed = NO ; + m_shouldClose = NO ; +} + +- (IBAction)cancelPressed:(id)sender +{ + m_shouldClose = YES ; + [NSApp stopModal]; +} + +- (IBAction)okPressed:(id)sender +{ + m_okPressed = YES ; + m_shouldClose = YES ; + [NSApp stopModal]; +} + +-(BOOL)closedWithOk +{ + return m_okPressed ; +} + +-(BOOL)shouldCloseCarbon +{ + return m_shouldClose ; +} + +-(NSButton*)okButton +{ + return m_okButton ; +} +@end + + +extern "C" int RunMixedFontDialog(wxFontDialog* dialog) ; + +int RunMixedFontDialog(wxFontDialog* dialog) +{ + int retval = wxID_CANCEL ; + + bool cocoaLoaded = NSApplicationLoad(); + wxASSERT_MSG(cocoaLoaded,wxT("Couldn't load Cocoa in Carbon Environment")) ; + + wxAutoNSAutoreleasePool pool; + + // setting up the ok/cancel buttons + NSFontPanel* fontPanel = [NSFontPanel sharedFontPanel] ; + + // adjust modality for carbon environment + WindowRef carbonWindowRef = (WindowRef)[fontPanel windowRef] ; + SetWindowModality(carbonWindowRef, kWindowModalityAppModal , 0) ; + SetWindowGroup(carbonWindowRef , GetWindowGroupOfClass(kMovableModalWindowClass)); + + [fontPanel setFloatingPanel:NO] ; + + wxMacFontPanelAccView* accessoryView = (wxMacFontPanelAccView*) [fontPanel accessoryView] ; + if ( accessoryView == nil) + { + NSRect rectBox = NSMakeRect( 0 , 0 , 192 , 40 ); + accessoryView = [[wxMacFontPanelAccView alloc] initWithFrame:rectBox]; + [fontPanel setAccessoryView:accessoryView]; + + [fontPanel setDefaultButtonCell:[[accessoryView okButton] cell]] ; + } + + [accessoryView resetFlags]; + + NSModalSession session = [NSApp beginModalSessionForWindow:fontPanel]; + + [NSApp runModalSession:session]; + + [NSApp endModalSession:session]; + + if( FPIsFontPanelVisible()) + FPShowHideFontPanel() ; + + if ( [accessoryView closedWithOk]) + { + retval = wxID_OK ; + } + + + return retval ; +} + +#else + #if USE_NATIVE_FONT_DIALOG_FOR_MACOSX IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog) // Cocoa headers -#include "wx/cocoa/autorelease.h" -#include "wx/cocoa/string.h" #import #import @@ -367,3 +518,5 @@ bool wxFontDialog::IsShown() const } #endif // 10.2+ + +#endif