2016-05-01 24 views
0

當OS X中的某個應用程序有多個窗口(許多打開的文檔,每個窗口都在自己的窗口中)時,似乎它們共享相同的系統菜單,至少在FLTK中。有沒有辦法找到最近選擇的窗口從菜單中發送一個事件給它?Mac OS X中的FLTK窗口共享相同的系統菜單

這裏是我的設置(Mac OS X 10.6.2,FLTK 1.3.3):有Shell類與系統菜單。每一次新的文檔被打開,新Shell創建:

#ifdef __APPLE__ 
void Shell::macOpen(const char *fileName) 
{ 
    // If there are empty shell, open the model in it 
    if (s_empty != 0) 
    { 
     ... 
     s_empty = 0; 
    } 
    // Otherwise, create new shell with the model 
    else 
    { 
     char *args[1]; 
     args[0] = (char *) fileName; 
     new Shell(1, args, s_conf, s_dct, fileName, 1); 
    } 
} 
#endif 

然後我跟蹤的最近選擇Shell將其保存到static Shell *Shell::s_current

int Shell::handle(int event) 
{ 
    ... 
    case FL_FOCUS: 
#ifdef __APPLE__ 
     // We just selected some shell, it is current. 
     s_current = this; 
     cout << "Select shell with address: [" << s_current << "]" << endl; 
#endif 
     return 1; 
    ... 
} 

這片似乎工作,因爲我可以看到的痕跡,每次我選擇一些Shell

Select shell with address: [0x8206db0] 
Select shell with address: [0x82375f0] 
Select shell with address: [0x5d20650] 
Select shell with address: [0x82375f0] 

現在,給出:

Shell *Shell::currentShell(Fl_Widget *w) 
{ 
    cout << "Widget address: [" << w << "]" << endl; 
    Shell *result = 0; 
    if (w != 0) 
    { 
     result = (Shell *) w->window(); 
     cout << "Widget wingow address: [" << result << "]" << endl; 
    } 
#ifdef __APPLE__ 
    else 
    { 
     result = s_current; 
     cout << "Last selected shell address: [" << result << "]" << endl; 
    } 
#endif 
    return result; 
} 

我有一些回調:

void Shell::shortcutCB(Fl_Widget *w, void *data) 
{ 
    cout << "Shortcut" << endl; 
    Shell *ref = currentShell(w); 
    if (ref != 0) 
    { 
     ... 
    } 
} 

當這個回調是從菜單中執行多Shell s爲開,我得到以下錯誤:

Bus error 

,不留痕跡,從要麼是Shell::shortcutCB要麼是Shell::currentShell。當唯一的Shell是開放的,都完美的作品。如果有更多的Shell已打開,並且我關閉了所有其中一個,則錯誤再次出現。從Shell內的某個窗口小部件調用相同的回調或從鍵盤快捷方式發送時,沒有任何問題。

回答

0

解決的問題有以下3個步驟:在OS X

  1. 聲明馬努酒吧也是靜態的(這裏是失敗):

    #ifdef __APPLE__ 
        static Fl_Sys_Menu_Bar *s_menubar; 
    #else 
        Fl_Sys_Menu_Bar *m_menubar; 
    #endif 
    
  2. 保存當前Shell::s_current不僅FL_FOCUS事件,但在任何由Shell處理的事件中,也就是每次返回1時:

    int Shell::handle(int event) 
    { 
        int result = 0; 
        switch (event) 
        { 
         // Set result = 1 when handling the event 
         ... 
        } 
    #ifdef __APPLE__ 
        if (result == 1) 
        { 
         // We just selected some shell, it is current. 
         s_current = this; 
         cout << "Select shell with address: [" << s_current << "]" << endl; 
        } 
    #endif 
        return result; 
    } 
    
  3. 使用Shell::s_current對OS X的菜單回調無論生成調用控件的:

    Shell *Shell::currentShell(Fl_Widget *w) 
    { 
        Shell *result = 0; 
    #ifdef __APPLE__ 
        result = s_current; 
        cout << "Last selected shell address: [" << result << "]" << endl; 
    #else 
        cout << "Widget address: [" << w << "]" << endl; 
        if (w != 0) 
        { 
         result = (Shell *) w->window(); 
         cout << "Widget wingow address: [" << result << "]" << endl; 
        } 
    #endif 
        return result; 
    }