From 45ee58ac8d9d5c30ab39b54ea4154878fcea16ab Mon Sep 17 00:00:00 2001 From: "simon.kagstrom" Date: Mon, 2 Nov 2009 06:36:25 +0000 Subject: [PATCH] (Untested) refactor menu code a bit - move stuff to menu.c instead --- Src/Network.cpp | 10 +--------- Src/menu.cpp | 33 +++++++++++++++++++++++++++++++++ Src/menu.h | 1 + 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/Src/Network.cpp b/Src/Network.cpp index 571137a..c3a527e 100644 --- a/Src/Network.cpp +++ b/Src/Network.cpp @@ -1072,12 +1072,6 @@ network_connection_error_t Network::WaitForPeerList() return SERVER_GARBAGE_ERROR; pi = (NetworkUpdateListPeers *)this->ud->data; - msgs = (const char**)calloc(pi->n_peers + 2, sizeof(const char*)); - - msgs[0] = "None (wait for peer to connect)"; - printf("Got %d peers\n", pi->n_peers); - for (int i = 0; i < pi->n_peers; i++) { - msgs[i + 1] = (const char*)pi->peers[i].name; #if 0 if (pi->peers[i].version != FRODO_NETWORK_PROTOCOL_VERSION) { @@ -1085,9 +1079,7 @@ network_connection_error_t Network::WaitForPeerList() return VERSION_ERROR; } #endif - } - int sel = menu_select(msgs, NULL); - free(msgs); + int sel = menu_select_peer(pi->peers, pi->n_peers); /* FIXME! What to do here??? */ if (sel < 0) diff --git a/Src/menu.cpp b/Src/menu.cpp index 464c5f5..81cea2a 100644 --- a/Src/menu.cpp +++ b/Src/menu.cpp @@ -18,6 +18,7 @@ #include "sysdeps.h" #include "Display.h" +#include "Network.h" #include "menu.h" #include "menutexts.h" @@ -45,6 +46,9 @@ typedef struct int cur_sel; /* Main selection */ int start_entry_visible; int n_entries; + + NetworkUpdatePeerInfo *peers; + int n_peers; } menu_t; #define IS_SUBMENU(p_msg) ( (p_msg)[0] == '^' ) @@ -606,6 +610,8 @@ static void menu_init(menu_t *p_menu, const char *title, TTF_Font *p_font, const p_menu->text_w = 0; p_menu->n_submenus = 0; + p_menu->peers = NULL; + p_menu->n_peers = 0; strcpy(p_menu->title, title); for (p_menu->n_entries = 0; p_menu->pp_msgs[p_menu->n_entries]; p_menu->n_entries++) @@ -878,6 +884,33 @@ int menu_select(const char **msgs, int *submenus) return menu_select("", msgs, submenus); } +int menu_select_peer(NetworkUpdatePeerInfo *peers, int n_peers) +{ + menu_t menu; + int out; + const char **msgs; + + msgs = (const char**)calloc(n_peers + 2, sizeof(const char*)); + + msgs[0] = "None (wait for peer to connect)"; + printf("Got %d peers\n", n_peers); + for (int i = 0; i < n_peers; i++) + msgs[i + 1] = (const char*)peers[i].name; + + menu_init(&menu, "", menu_font, msgs, + 32, 32, FULL_DISPLAY_X-32, FULL_DISPLAY_Y-64); + menu.peers = peers; + menu.n_peers = n_peers; + + out = menu_select_internal(real_screen, &menu, NULL, 0, + NULL, NULL); + + menu_fini(&menu); + free(msgs); + + return out; +} + extern "C" const char **DirD64(const char *FileName); static void d64_list_cb(menu_t *p, void *data) diff --git a/Src/menu.h b/Src/menu.h index 63c8c06..8191067 100644 --- a/Src/menu.h +++ b/Src/menu.h @@ -36,6 +36,7 @@ int menu_select_sized(char *title, const char **msgs, int *submenus, int x, int y, int w, int h); const char *menu_select_file(const char *dir_path); const char *menu_select_file_start(const char *dir_path, const char **d64_name); +int menu_select_peer(NetworkUpdatePeerInfo *peers, int n_peers); uint32_t menu_wait_key_press(void);