src/network/network_gui.cpp
changeset 9365 95e9cbc0257e
parent 9354 845e07db4549
child 9381 3a8c7f0221bb
equal deleted inserted replaced
9364:5476e1a21517 9365:95e9cbc0257e
   188 		this->resize.step_height = NET_PRC__SIZE_OF_ROW;
   188 		this->resize.step_height = NET_PRC__SIZE_OF_ROW;
   189 
   189 
   190 		this->field = NGWW_PLAYER;
   190 		this->field = NGWW_PLAYER;
   191 		this->server = NULL;
   191 		this->server = NULL;
   192 
   192 
   193 		this->servers.sort_list = NULL;
       
   194 		this->servers.flags = VL_REBUILD | (_ng_sorting.order ? VL_DESC : VL_NONE);
   193 		this->servers.flags = VL_REBUILD | (_ng_sorting.order ? VL_DESC : VL_NONE);
   195 		this->servers.sort_type = _ng_sorting.criteria;
   194 		this->servers.sort_type = _ng_sorting.criteria;
   196 
   195 
   197 		this->FindWindowPlacementAndResize(desc);
   196 		this->FindWindowPlacementAndResize(desc);
   198 	}
   197 	}
   199 
   198 
   200 	~NetworkGameWindow()
   199 	~NetworkGameWindow()
   201 	{
   200 	{
   202 		free(this->servers.sort_list);
       
   203 	}
   201 	}
   204 
   202 
   205 	/**
   203 	/**
   206 	 * (Re)build the network game list as its amount has changed because
   204 	 * (Re)build the network game list as its amount has changed because
   207 	 * an item has been added or deleted for example
   205 	 * an item has been added or deleted for example
   208 	 */
   206 	 */
   209 	void BuildNetworkGameList()
   207 	void BuildNetworkGameList()
   210 	{
   208 	{
   211 		NetworkGameList *ngl_temp;
       
   212 		uint n = 0;
       
   213 
       
   214 		if (!(this->servers.flags & VL_REBUILD)) return;
   209 		if (!(this->servers.flags & VL_REBUILD)) return;
   215 
   210 
   216 		/* Count the number of games in the list */
       
   217 		for (ngl_temp = _network_game_list; ngl_temp != NULL; ngl_temp = ngl_temp->next) n++;
       
   218 		if (n == 0) return;
       
   219 
       
   220 		/* Create temporary array of games to use for listing */
   211 		/* Create temporary array of games to use for listing */
   221 		this->servers.sort_list = ReallocT(this->servers.sort_list, n);
   212 		this->servers.Clear();
   222 		this->servers.list_length = n;
   213 
   223 
   214 		for (NetworkGameList *ngl = _network_game_list; ngl != NULL; ngl = ngl->next) {
   224 		for (n = 0, ngl_temp = _network_game_list; ngl_temp != NULL; ngl_temp = ngl_temp->next) {
   215 			*this->servers.Append() = ngl;
   225 			this->servers.sort_list[n++] = ngl_temp;
   216 		}
   226 		}
   217 
       
   218 		this->servers.Compact();
   227 
   219 
   228 		/* Force resort */
   220 		/* Force resort */
   229 		this->servers.flags &= ~VL_REBUILD;
   221 		this->servers.flags &= ~VL_REBUILD;
   230 		this->servers.flags |= VL_RESORT;
   222 		this->servers.flags |= VL_RESORT;
   231 	}
   223 	}
   240 
   232 
   241 		NetworkGameList *item;
   233 		NetworkGameList *item;
   242 		uint i;
   234 		uint i;
   243 
   235 
   244 		if (!(this->servers.flags & VL_RESORT)) return;
   236 		if (!(this->servers.flags & VL_RESORT)) return;
   245 		if (this->servers.list_length == 0) return;
   237 		if (this->servers.Length() == 0) return;
   246 
   238 
   247 		_internal_sort_order = !!(this->servers.flags & VL_DESC);
   239 		_internal_sort_order = !!(this->servers.flags & VL_DESC);
   248 		qsort(this->servers.sort_list, this->servers.list_length, sizeof(this->servers.sort_list[0]), ngame_sorter[this->servers.sort_type]);
   240 		qsort(this->servers.Begin(), this->servers.Length(), sizeof(this->servers.Begin()), ngame_sorter[this->servers.sort_type]);
   249 
   241 
   250 		/* After sorting ngl->sort_list contains the sorted items. Put these back
   242 		/* After sorting ngl->sort_list contains the sorted items. Put these back
   251 		 * into the original list. Basically nothing has changed, we are only
   243 		 * into the original list. Basically nothing has changed, we are only
   252 		 * shuffling the ->next pointers */
   244 		 * shuffling the ->next pointers */
   253 		_network_game_list = this->servers.sort_list[0];
   245 		_network_game_list = this->servers[0];
   254 		for (item = _network_game_list, i = 1; i != this->servers.list_length; i++) {
   246 		for (item = _network_game_list, i = 1; i != this->servers.Length(); i++) {
   255 			item->next = this->servers.sort_list[i];
   247 			item->next = this->servers[i];
   256 			item = item->next;
   248 			item = item->next;
   257 		}
   249 		}
   258 		item->next = NULL;
   250 		item->next = NULL;
   259 
   251 
   260 		this->servers.flags &= ~VL_RESORT;
   252 		this->servers.flags &= ~VL_RESORT;
   298 		const NetworkGameList *sel = this->server;
   290 		const NetworkGameList *sel = this->server;
   299 		const SortButtonState arrow = (this->servers.flags & VL_DESC) ? SBS_DOWN : SBS_UP;
   291 		const SortButtonState arrow = (this->servers.flags & VL_DESC) ? SBS_DOWN : SBS_UP;
   300 
   292 
   301 		if (this->servers.flags & VL_REBUILD) {
   293 		if (this->servers.flags & VL_REBUILD) {
   302 			this->BuildNetworkGameList();
   294 			this->BuildNetworkGameList();
   303 			SetVScrollCount(this, this->servers.list_length);
   295 			SetVScrollCount(this, this->servers.Length());
   304 		}
   296 		}
   305 		if (this->servers.flags & VL_RESORT) this->SortNetworkGameList();
   297 		if (this->servers.flags & VL_RESORT) this->SortNetworkGameList();
   306 
   298 
   307 		/* 'Refresh' button invisible if no server selected */
   299 		/* 'Refresh' button invisible if no server selected */
   308 		this->SetWidgetDisabledState(NGWW_REFRESH, sel == NULL);
   300 		this->SetWidgetDisabledState(NGWW_REFRESH, sel == NULL);
   580 	{
   572 	{
   581 		this->vscroll.cap += delta.y / (int)this->resize.step_height;
   573 		this->vscroll.cap += delta.y / (int)this->resize.step_height;
   582 
   574 
   583 		this->widget[NGWW_MATRIX].data = (this->vscroll.cap << 8) + 1;
   575 		this->widget[NGWW_MATRIX].data = (this->vscroll.cap << 8) + 1;
   584 
   576 
   585 		SetVScrollCount(this, this->servers.list_length);
   577 		SetVScrollCount(this, this->servers.Length());
   586 
   578 
   587 		int widget_width = this->widget[NGWW_FIND].right - this->widget[NGWW_FIND].left;
   579 		int widget_width = this->widget[NGWW_FIND].right - this->widget[NGWW_FIND].left;
   588 		int space = (this->width - 4 * widget_width - 25) / 3;
   580 		int space = (this->width - 4 * widget_width - 25) / 3;
   589 
   581 
   590 		int offset = 10;
   582 		int offset = 10;