Fixed f-keys

This commit is contained in:
Daniel Collin 2016-05-08 11:00:50 +02:00
parent 3c2451c2ec
commit d3fcfd4bf0
3 changed files with 40 additions and 7 deletions

View file

@ -529,6 +529,10 @@ impl Menu {
self.0.add_sub_menu(name, &menu.0) self.0.add_sub_menu(name, &menu.0)
} }
pub fn add_separator(&mut self) {
self.add_menu_item(&MenuItem { id: MENU_ID_SEPARATOR, ..MenuItem::default() });
}
#[inline] #[inline]
pub fn add_menu_item(&mut self, item: &MenuItem) -> MenuItemHandle { pub fn add_menu_item(&mut self, item: &MenuItem) -> MenuItemHandle {
self.0.add_menu_item(item) self.0.add_menu_item(item)

View file

@ -581,6 +581,14 @@ void build_submenu(NSMenu* menu, MenuDesc* desc)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static NSString* get_string_for_key(uint32_t t) {
unichar c = (unichar)t;
NSString* key = [NSString stringWithCharacters:&c length:1];
return key;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
uint64_t mfb_add_menu_item( uint64_t mfb_add_menu_item(
void* in_menu, void* in_menu,
int32_t menu_id, int32_t menu_id,
@ -601,10 +609,13 @@ uint64_t mfb_add_menu_item(
} }
else else
{ {
NSString* key_string = 0;
int mask = 0; int mask = 0;
NSMenuItem* newItem = [[NSMenuItem alloc] initWithTitle:name action:@selector(onMenuPress:) keyEquivalent:@""]; NSMenuItem* newItem = [[NSMenuItem alloc] initWithTitle:name action:@selector(onMenuPress:) keyEquivalent:@""];
[newItem setTag:menu_id]; [newItem setTag:menu_id];
printf("set menu id %d\n", menu_id);
// This code may look a bit weird but is here for a reason: // This code may look a bit weird but is here for a reason:
// //
// In order to make it easier to bulid cross-platform apps Ctrl is often used as // In order to make it easier to bulid cross-platform apps Ctrl is often used as
@ -628,15 +639,30 @@ uint64_t mfb_add_menu_item(
mask |= NSAlternateKeyMask; mask |= NSAlternateKeyMask;
} }
if (key != 0x7f) { switch (key) {
NSString* key_string = convert_key_code_to_string(key); case 0x7a: { key_string = get_string_for_key(NSF1FunctionKey); break; } // F1
case 0x78: { key_string = get_string_for_key(NSF2FunctionKey); break; } // F2
if (key_string) { case 0x63: { key_string = get_string_for_key(NSF3FunctionKey); break; } // F3
[newItem setKeyEquivalentModifierMask: mask]; case 0x76: { key_string = get_string_for_key(NSF4FunctionKey); break; } // F4
[newItem setKeyEquivalent:key_string]; case 0x60: { key_string = get_string_for_key(NSF5FunctionKey); break; } // F5
case 0x61: { key_string = get_string_for_key(NSF6FunctionKey); break; } // F6
case 0x62: { key_string = get_string_for_key(NSF7FunctionKey); break; } // F7
case 0x64: { key_string = get_string_for_key(NSF8FunctionKey); break; } // F8
case 0x65: { key_string = get_string_for_key(NSF9FunctionKey); break; } // F9
case 0x6d: { key_string = get_string_for_key(NSF10FunctionKey); break; } // F10
case 0x67: { key_string = get_string_for_key(NSF11FunctionKey); break; } // F11
case 0x6f: { key_string = get_string_for_key(NSF12FunctionKey); break; } // F12
case 0x7f: break;
default: {
key_string = convert_key_code_to_string(key);
} }
} }
if (key_string) {
[newItem setKeyEquivalentModifierMask: mask];
[newItem setKeyEquivalent:key_string];
}
if (enabled) { if (enabled) {
[newItem setEnabled:YES]; [newItem setEnabled:YES];
} else { } else {

View file

@ -173,6 +173,7 @@ extern "C" {
fn mfb_is_active(window: *mut c_void) -> u32; fn mfb_is_active(window: *mut c_void) -> u32;
fn mfb_add_menu(window: *mut c_void, menu: *mut c_void); fn mfb_add_menu(window: *mut c_void, menu: *mut c_void);
fn mfb_add_sub_menu(parent_menu: *mut c_void, name: *const c_char, menu: *mut c_void); fn mfb_add_sub_menu(parent_menu: *mut c_void, name: *const c_char, menu: *mut c_void);
fn mfb_active_menu(window: *mut c_void) -> i32;
fn mfb_create_menu(name: *const c_char) -> *mut c_void; fn mfb_create_menu(name: *const c_char) -> *mut c_void;
// fn mfb_destroy_menu(menu_item: *mut c_void); // fn mfb_destroy_menu(menu_item: *mut c_void);
@ -382,7 +383,7 @@ impl Window {
} }
pub fn is_menu_pressed(&mut self) -> Option<usize> { pub fn is_menu_pressed(&mut self) -> Option<usize> {
let menu_id = 0; //unsafe { mfb_active_menu(self.window_handle) }; let menu_id = unsafe { mfb_active_menu(self.window_handle) };
if menu_id < 0 { if menu_id < 0 {
None None
@ -578,6 +579,8 @@ impl Menu {
let item_name = CString::new(item.label.as_str()).unwrap(); let item_name = CString::new(item.label.as_str()).unwrap();
let conv_key = Self::map_key_to_menu_key(item.key); let conv_key = Self::map_key_to_menu_key(item.key);
println!("menu id {}", item.id);
MenuItemHandle(mfb_add_menu_item(self.menu_handle, MenuItemHandle(mfb_add_menu_item(self.menu_handle,
item.id as i32, item.id as i32,
item_name.as_ptr(), item_name.as_ptr(),