2009-12-18 37 views
0

我有一個openGL GUI界面,我需要一個彈出式菜單,當我的openGL顯示中按下某個按鈕時,會彈出一個菜單。我想顯示一個菜單,類似於按下NSPopUPButton時彈出的菜單,但我不能使用Cocoa按鈕本身。看來最好的方法是使用NSPopupButtonCell。我不能在這裏發佈我的代碼,因爲我在Lisp中編寫代碼並通過客觀的C橋訪問Cocoa,但我只想看看下面的pseduo代碼是否應該顯示菜單,或者如果我的明顯缺陷邏輯:在可可OSX中編程實例化一個NSPopUpButtonCell

• Pseudo Code for function that is called when button is pressed: 
• Initialize an NSPopUpButtonCell Using initTextCell: "test" pullsDown: NO 
• allocate an NSMenu using alloc 
• add two menu items using addItem: 
• set the menu of the popUpButtonCell to be the menu that was just allocated using setMenu: 
• Next I tried two methods to try and get the menu to actually be displayed, 
    first I tried using drawWithFrame:inView: and when that did not work I also tried 
    using drawBezelWithFrame:inView: eventhough the docs said not to use but I just 
    tried it out of desperation. 
• finally, when the draw methods did not work I tried using performClick: on the 
    popupButtonCell to try and simulate the click. 

這些方法都無法成功顯示任何種類的菜單。有沒有其他的方式來編程彈出單元格中包含的菜單?

回答

3

我認爲你正在尋找NSCell支持10.5或ealier所以我不能用popUpMenuPositioningItem

[theCell trackMouse:[NSApp currentEvent] inRect:NSZeroRect ofView:theView untilMouseUp:YES]; 
+0

這樣做伎倆謝謝。 – Mike2012 2009-12-18 20:24:42

0

我認爲你會更好的使用普通的NSMenu並呼籲+[NSMenu popUpContextMenu:withEvent:forView:]。如果您只定位到10.6及更高版本,則可能還會檢出-[NSMenu popUpMenuPositioningItem:atLocation:inView:],這樣您可以更多地控制菜單的位置。只需在您的視圖中實現mouseDown:,構建一個菜單(或從nib文件加載它)並顯示它,NSMenu應該處理所有細節。只要確保設置了每個NSMenuItem的目標和操作,以便在選擇某個項目時正確調用操作方法。

+0

我目標機器的trackMouse:inRect:ofView:untilMouseUp:方法:atLocation:inView:。還有我一直在使用popUpContextMenu嘗試:withEvent:方法forView:但由於整個目標c橋我沒有一個真正的NSEvent,所以我不得不僞造一個鼠標按下事件,並由於某種原因,它忽略了我傳遞到我使用mouseEventWithType創建的事件點:位置:modifierFlags: timestamp:windowNumber:context:eventNumber:clickCount:pressure:無論我傳遞給location參數的座標如何,菜單總是顯示在底部左轉角 – Mike2012 2009-12-18 18:26:15

+0

此外,這似乎並不支持cocotron這種方法,我們也試圖以windows爲目標。 – Mike2012 2009-12-18 18:26:47

相關問題