]> git.saurik.com Git - wxWidgets.git/blobdiff - src/mac/carbon/app.cpp
making tool enable consistent (was using mix of enable and activate controls before)
[wxWidgets.git] / src / mac / carbon / app.cpp
index af8dafdaf1a3af05b871652aaa3e1c55e3fe0a9d..f610756a2a282f456a2a3ef295a83e1fcb725941 100644 (file)
@@ -304,7 +304,37 @@ void wxApp::MacNewFile()
 
 void wxApp::MacReopenApp()
 {
-    // eventually check for open docs, if none, call MacNewFile
+    // HIG says :
+    // if there is no open window -> create a new one
+    // if all windows are hidden -> show the first
+    // if some windows are not hidden -> do nothing
+    
+    wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
+    if ( node == NULL )
+    {  
+        MacNewFile() ;
+    }
+    else
+    {
+        wxTopLevelWindow* firstIconized = NULL ;
+        while (node)
+        {
+            wxTopLevelWindow* win = (wxTopLevelWindow*) node->GetData();
+            if ( win->IsIconized() == false )
+            {
+                firstIconized = NULL ;
+                break ;
+            }
+            else
+            {
+                if ( firstIconized == NULL )
+                    firstIconized = win ;
+            }
+            node = node->GetNext();
+        }
+        if ( firstIconized )
+            firstIconized->Iconize( false ) ;
+    }
 }
 
 //----------------------------------------------------------------------
@@ -336,62 +366,52 @@ static const EventTypeSpec eventList[] =
 
 static pascal OSStatus
 wxMacAppMenuEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
-{
-    wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar();
+{    
+    wxMacCarbonEvent cEvent( event ) ;
+    MenuRef menuRef = cEvent.GetParameter<MenuRef>(kEventParamDirectObject) ;
+    wxMenu* menu = wxFindMenuFromMacMenu( menuRef ) ;
     
-    if ( mbar )
+    if ( menu )
     {
-        wxFrame* win = mbar->GetFrame();
-        if ( win )
+        wxEventType type=0;        
+        MenuCommand cmd=0;
+        switch (GetEventKind(event))
         {
-            EventRef formerEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
-            EventHandlerCallRef formerEventHandlerCallRef = (EventHandlerCallRef) wxTheApp->MacGetCurrentEventHandlerCallRef() ;
-            wxTheApp->MacSetCurrentEvent( event , handler ) ;
-            
-            // VZ: we could find the menu from its handle here by examining all
-            //     the menus in the menu bar recursively but knowing that neither
-            //     wxMSW nor wxGTK do it why bother...
- #if 0
-            MenuRef menuRef;
-
-            GetEventParameter(event,
-                              kEventParamDirectObject,
-                              typeMenuRef, NULL,
-                              sizeof(menuRef), NULL,
-                              &menuRef);
- #endif // 0
-
-            wxEventType type=0;        
-            MenuCommand cmd=0;
-            switch (GetEventKind(event))
-            {
-                case kEventMenuOpening:
-                    type = wxEVT_MENU_OPEN;
-                    break;
-                case kEventMenuClosed:
-                    type = wxEVT_MENU_CLOSE;
-                    break;
-                case kEventMenuTargetItem:
+            case kEventMenuOpening:
+                type = wxEVT_MENU_OPEN;
+                break;
+            case kEventMenuClosed:
+                type = wxEVT_MENU_CLOSE;
+                break;
+            case kEventMenuTargetItem:
+                cmd = cEvent.GetParameter<MenuCommand>(kEventParamMenuCommand,typeMenuCommand) ;
+                if (cmd != 0) 
                     type = wxEVT_MENU_HIGHLIGHT;
-                    GetEventParameter(event, kEventParamMenuCommand,
-                                      typeMenuCommand, NULL,
-                                      sizeof(cmd), NULL, &cmd);
-                    if (cmd == 0) return eventNotHandledErr;
-                    break;
-                default:
-                    wxFAIL_MSG(wxT("Unexpected menu event kind"));
-                    break;
-            }
+                break;
+            default:
+                wxFAIL_MSG(wxT("Unexpected menu event kind"));
+                break;
+        }
 
+        if ( type )
+        {
             wxMenuEvent wxevent(type, cmd);
-            wxevent.SetEventObject(win);
+            wxevent.SetEventObject(menu);
 
-            (void)win->GetEventHandler()->ProcessEvent(wxevent);
-            
-            wxTheApp->MacSetCurrentEvent( formerEvent, formerEventHandlerCallRef ) ;
-        }
+            wxEvtHandler* handler = menu->GetEventHandler();
+            if (handler && handler->ProcessEvent(wxevent))
+            {
+                // handled
+            }
+            else
+            {
+                wxWindow *win = menu->GetInvokingWindow();
+                if (win) 
+                    win->GetEventHandler()->ProcessEvent(wxevent);
+                }
+            }
     }
-
+    
     return eventNotHandledErr;
 }
 
@@ -401,32 +421,31 @@ static pascal OSStatus wxMacAppCommandEventHandler( EventHandlerCallRef handler
 
     HICommand command ;
 
-    GetEventParameter( event, kEventParamDirectObject, typeHICommand, NULL,
-        sizeof( HICommand ), NULL, &command );
-
+    wxMacCarbonEvent cEvent( event ) ;
+    cEvent.GetParameter<HICommand>(kEventParamDirectObject,typeHICommand,&command) ;
+        
+    wxMenuItem* item = NULL ;
     MenuCommand id = command.commandID ;
+    // for items we don't really control
     if ( id == kHICommandPreferences )
-        id = wxApp::s_macPreferencesMenuItemId ;
-
-    wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
-    wxMenu* menu = NULL ;
-    wxMenuItem* item = NULL ;
-
-    if ( mbar )
     {
-        item = mbar->FindItem( id , &menu ) ;
-        // it is not 100 % sure that an menu of id 0 is really ours, safety check
-        if ( id == 0 && menu != NULL && menu->GetHMenu() != command.menu.menuRef )
+        id = wxApp::s_macPreferencesMenuItemId ;
+        
+        wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
+        if ( mbar )
         {
-            item = NULL ;
-            menu = NULL ;
+            wxMenu* menu = NULL ;
+            item = mbar->FindItem( id , &menu ) ;
         }
     }
-
-    if ( item == NULL || menu == NULL || mbar == NULL )
-        return result ;
-
-       switch( GetEventKind( event ) )
+    else if ( id != 0 && command.menu.menuRef != 0 && command.menu.menuItemIndex != 0 )
+    {
+        GetMenuItemRefCon( command.menu.menuRef , command.menu.menuItemIndex , (UInt32*) &item ) ;
+    }
+    
+    if ( item )
+    {
+       switch( cEvent.GetKind() )
        {
            case kEventProcessCommand :
            {
@@ -435,18 +454,18 @@ static pascal OSStatus wxMacAppCommandEventHandler( EventHandlerCallRef handler
                     item->Check( !item->IsChecked() ) ;
                 }
 
-                menu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
+                item->GetMenu()->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
                 result = noErr ;
             }
-               break ;
+            break ;
         case kEventCommandUpdateStatus:
             // eventually trigger an updateui round
             result = noErr ;
             break ;
-           default :
-               break ;
-       }
-
+        default :
+            break ;
+        }
+    }
     return result ;
 }
 
@@ -477,6 +496,10 @@ static pascal OSStatus wxMacAppApplicationEventHandler( EventHandlerCallRef hand
 
 pascal OSStatus wxMacAppEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
 {
+    EventRef formerEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
+    EventHandlerCallRef formerEventHandlerCallRef = (EventHandlerCallRef) wxTheApp->MacGetCurrentEventHandlerCallRef() ;
+    wxTheApp->MacSetCurrentEvent( event , handler ) ;
+
     OSStatus result = eventNotHandledErr ;
     switch( GetEventClass( event ) )
     {
@@ -503,6 +526,8 @@ pascal OSStatus wxMacAppEventHandler( EventHandlerCallRef handler , EventRef eve
             break ;
     }
 
+    wxTheApp->MacSetCurrentEvent( formerEvent, formerEventHandlerCallRef ) ;
+
     return result ;
 }
 
@@ -605,6 +630,20 @@ bool wxApp::Initialize(int& argc, wxChar **argv)
     wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
 #endif
 
+#if TARGET_API_MAC_OSX
+    // these might be the startup dirs, set them to the 'usual' dir containing the app bundle
+    wxString startupCwd = wxGetCwd() ;
+    if ( startupCwd == wxT("/") || startupCwd.Right(15) == wxT("/Contents/MacOS") )
+    {
+        CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle() ) ;
+        CFURLRef urlParent = CFURLCreateCopyDeletingLastPathComponent( kCFAllocatorDefault , url ) ;
+        CFRelease( url ) ;
+        CFStringRef path = CFURLCopyFileSystemPath ( urlParent , kCFURLPOSIXPathStyle ) ;
+        CFRelease( urlParent ) ;
+        wxString cwd = wxMacCFStringHolder(path).AsString(wxLocale::GetSystemEncoding());       
+        wxSetWorkingDirectory( cwd ) ;
+    }
+#endif
 
     wxMacCreateNotifierTable() ;