status_t KeymapWindow::_SetKeyboardLayout(const char* path) { status_t status = B_OK; if (path != NULL && path[0] != '\0') { status = fKeyboardLayoutView->GetKeyboardLayout()->Load(path); if (status == B_OK) { // select item for (int32 i = fLayoutMenu->CountItems(); i-- > 0;) { BMenuItem* item = fLayoutMenu->ItemAt(i); BMessage* message = item->Message(); entry_ref ref; if (message->FindRef("ref", &ref) == B_OK) { BPath layoutPath(&ref); if (layoutPath == path) { item->SetMarked(true); break; } } } } } if (path == NULL || status != B_OK) { fKeyboardLayoutView->GetKeyboardLayout()->SetDefault(); fLayoutMenu->ItemAt(0)->SetMarked(true); } // Refresh currently set layout fKeyboardLayoutView->SetKeyboardLayout( fKeyboardLayoutView->GetKeyboardLayout()); return status; }
/*! Marks a keyboard layout item by iterating through the menus recursively searching for the menu item with the passed in path. This method always iterates through all menu items and unmarks them. If no item with the passed in path is found it is up to the caller to set the default keyboard layout and mark item corresponding to the default keyboard layout path. */ void KeymapWindow::_MarkKeyboardLayoutItem(const char* path, BMenu* menu) { BMenuItem* item = NULL; entry_ref ref; for (int32 i = 0; i < menu->CountItems(); i++) { item = menu->ItemAt(i); if (item == NULL) continue; // Unmark each item initially item->SetMarked(false); BMenu* submenu = item->Submenu(); if (submenu != NULL) _MarkKeyboardLayoutItem(path, submenu); else { if (item->Message()->FindRef("ref", &ref) == B_OK) { BPath layoutPath(&ref); if (path != NULL && path[0] != '\0' && layoutPath == path) { // Found it, mark the item item->SetMarked(true); } } } } }