+}
+
+static void HandleTypeNameExt(boolean initialize, Key key)
+{
+ static struct TextPosInfo pos_name = { 0 };
+ static char name[MAX_PLAYER_NAME_LEN + 1] = { 0 };
+ static int xpos = 0;
+ struct TextPosInfo *pos = &pos_name;
+ char key_char = getValidConfigValueChar(getCharFromKey(key));
+ boolean is_valid_key_char = (key_char != 0 && (key_char != ' ' || xpos > 0));
+ boolean active = TRUE;
+
+ if (initialize)
+ {
+ getTypeNameValues(name, pos, &xpos);
+
+ int sx = mSX + ALIGNED_TEXT_XPOS(pos);
+ int sy = mSY + ALIGNED_TEXT_YPOS(pos);
+
+ StartTextInput(sx, sy, pos->width, pos->height);
+ }
+ else if (is_valid_key_char && xpos < MAX_PLAYER_NAME_LEN)
+ {
+ name[xpos] = key_char;
+ name[xpos + 1] = 0;
+
+ xpos++;
+ }
+ else if ((key == KSYM_Delete || key == KSYM_BackSpace) && xpos > 0)
+ {
+ xpos--;
+
+ name[xpos] = 0;
+ }
+ else if (key == KSYM_Return)
+ {
+ setTypeNameValues(name, pos, TRUE);
+
+ active = FALSE;
+ }
+ else if (key == KSYM_Escape)
+ {
+ setTypeNameValues(name, pos, FALSE);
+
+ active = FALSE;
+ }
+
+ drawTypeNameText(name, pos, active);
+
+ if (!active)
+ {
+ StopTextInput();
+
+ SetGameStatus(game_status_last_screen);
+
+ if (game_status == GAME_MODE_MAIN)
+ InitializeMainControls();
+ }
+}
+
+static void DrawTypeName(void)
+{
+ HandleTypeNameExt(TRUE, 0);
+}
+
+void HandleTypeName(Key key)
+{
+ HandleTypeNameExt(FALSE, key);
+}
+
+
+// ============================================================================
+// tree menu functions
+// ============================================================================
+
+static int getAlignXOffsetFromTreeInfo(TreeInfo *ti)
+{
+ if (game_status != GAME_MODE_SETUP ||
+ DRAW_MODE_SETUP(setup_mode) != SETUP_MODE_CHOOSE_OTHER)
+ return 0;
+
+ int max_text_size = 0;
+ TreeInfo *node;
+
+ for (node = getTreeInfoFirstGroupEntry(ti); node != NULL; node = node->next)
+ max_text_size = MAX(max_text_size, strlen(node->name));
+
+ int num_entries = numTreeInfoInGroup(ti);
+ boolean scrollbar_needed = (num_entries > NUM_MENU_ENTRIES_ON_SCREEN);
+ int text_width = max_text_size * getFontWidth(FONT_TEXT_1);
+ int button_width = SC_MENUBUTTON_XSIZE;
+ int scrollbar_xpos = SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset;
+ int screen_width = (scrollbar_needed ? scrollbar_xpos : SXSIZE);
+ int align = menu.list_setup[SETUP_MODE_CHOOSE_OTHER].align;
+ int x = ALIGNED_XPOS(0, screen_width, align) * -1;
+ int align_xoffset_raw = ALIGNED_XPOS(x, button_width + text_width, align);
+ int align_xoffset = MAX(0, align_xoffset_raw);
+
+ return align_xoffset;
+}
+
+static int getAlignYOffsetFromTreeInfo(TreeInfo *ti)
+{
+ if (game_status != GAME_MODE_SETUP ||
+ DRAW_MODE_SETUP(setup_mode) != SETUP_MODE_CHOOSE_OTHER)
+ return 0;
+
+ int num_entries = numTreeInfoInGroup(ti);
+ int num_page_entries = MIN(num_entries, NUM_MENU_ENTRIES_ON_SCREEN);
+ int font_height = getFontHeight(FONT_TEXT_1);
+ int text_height = font_height * num_page_entries;
+ int page_height = font_height * NUM_MENU_ENTRIES_ON_SCREEN;
+ int align = menu.list_setup[SETUP_MODE_CHOOSE_OTHER].valign;
+ int y = ALIGNED_YPOS(0, page_height, align) * -1;
+ int align_yoffset_raw = ALIGNED_YPOS(y, text_height, align);
+ int align_yoffset = MAX(0, align_yoffset_raw);
+
+ return align_yoffset;
+}
+
+static void DrawChooseTree(TreeInfo **ti_ptr)
+{
+ int fade_mask = REDRAW_FIELD;
+
+ if (CheckFadeAll())
+ fade_mask = REDRAW_ALL;
+
+ if (strEqual((*ti_ptr)->subdir, STRING_TOP_DIRECTORY))
+ {
+ SetGameStatus(GAME_MODE_MAIN);
+
+ DrawMainMenu();
+
+ return;
+ }
+
+ UnmapAllGadgets();
+
+ FreeScreenGadgets();
+ CreateScreenGadgets();
+
+ FadeOut(fade_mask);
+
+ // needed if different viewport properties defined for choosing level (set)
+ ChangeViewportPropertiesIfNeeded();
+
+ if (game_status == GAME_MODE_NAMES)
+ SetMainBackgroundImage(IMG_BACKGROUND_NAMES);
+ else if (game_status == GAME_MODE_LEVELNR)
+ SetMainBackgroundImage(IMG_BACKGROUND_LEVELNR);
+ else if (game_status == GAME_MODE_LEVELS)
+ SetMainBackgroundImage(IMG_BACKGROUND_LEVELS);
+
+ ClearField();
+
+ OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
+
+ MapScreenTreeGadgets(*ti_ptr);
+ HandleChooseTree(0, 0, 0, 0, MB_MENU_INITIALIZE, ti_ptr);
+
+ DrawMaskedBorder(fade_mask);
+
+ FadeIn(fade_mask);
+}
+
+static void drawChooseTreeList(int first_entry, int num_page_entries,
+ TreeInfo *ti)
+{
+ int num_entries = numTreeInfoInGroup(ti);
+ boolean scrollbar_needed = (num_entries > NUM_MENU_ENTRIES_ON_SCREEN);
+ int scrollbar_xpos = SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset;
+ int screen_width = (scrollbar_needed ? scrollbar_xpos : SXSIZE);
+ int font_nr = FONT_TEXT_1;
+ int font_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
+ int i;
+ char *title_string = NULL;
+ int yoffset_sets = MENU_TITLE1_YPOS;
+ int yoffset_setup = 16;
+ int yoffset = (ti->type == TREE_TYPE_LEVEL_DIR ||
+ ti->type == TREE_TYPE_LEVEL_NR ? yoffset_sets : yoffset_setup);
+
+ title_string = ti->infotext;
+
+ DrawTextSCentered(mSY - SY + yoffset, FONT_TITLE_1, title_string);
+
+ clearMenuListArea();
+
+ for (i = 0; i < num_page_entries; i++)
+ {
+ TreeInfo *node, *node_first;
+ int entry_pos = first_entry + i;
+ int xpos = MENU_SCREEN_START_XPOS;
+ int ypos = MENU_SCREEN_START_YPOS + i;
+ int startx = amSX + xpos * 32;
+ int starty = amSY + ypos * 32;
+ int startx_text = startx + font_xoffset;
+ int endx_text = amSX + screen_width;
+ int max_text_size = endx_text - startx_text;
+ int max_buffer_len = max_text_size / getFontWidth(font_nr);
+ char buffer[max_buffer_len + 1];
+
+ node_first = getTreeInfoFirstGroupEntry(ti);
+ node = getTreeInfoFromPos(node_first, entry_pos);
+
+ strncpy(buffer, node->name, max_buffer_len);
+ buffer[max_buffer_len] = '\0';
+
+ DrawText(startx, starty, buffer, font_nr + node->color);
+
+ if (node->parent_link)
+ initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
+ else if (node->level_group)
+ initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
+ else
+ initCursor(i, IMG_MENU_BUTTON);
+
+ if (game_status == GAME_MODE_NAMES)
+ drawChooseTreeEdit(i, FALSE);
+ }
+
+ redraw_mask |= REDRAW_FIELD;