# network_content_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: network_content_gui.cpp 18809 2010-01-15 16:41:15Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #if defined(ENABLE_NETWORK)
    00013 #include "../stdafx.h"
    00014 #include "../strings_func.h"
    00015 #include "../gfx_func.h"
    00016 #include "../window_func.h"
    00017 #include "../gui.h"
    00018 #include "../ai/ai.hpp"
    00019 #include "../base_media_base.h"
    00020 #include "../sortlist_type.h"
    00021 #include "../querystring_gui.h"
    00022 #include "../core/geometry_func.hpp"
    00023 #include  "network_content.h"
    00024
    00025 #include "table/strings.h"
    00026 #include "../table/sprites.h"
    00027
    00029 enum DownloadStatusWindowWidgets {
    00030   NCDSWW_BACKGROUND,
    00031   NCDSWW_CANCELOK,
    00032 };
    00033
    00035 static const NWidgetPart _nested_network_content_download_status_window_widgets[] = {
    00036   NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_CONTENT_DOWNLOAD_TITLE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00037   NWidget(WWT_PANEL, COLOUR_GREY, NCDSWW_BACKGROUND),
    00038     NWidget(NWID_SPACER), SetMinimalSize(350, 0), SetMinimalTextLines(3, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 30),
    00039     NWidget(NWID_HORIZONTAL),
    00040       NWidget(NWID_SPACER), SetMinimalSize(125, 0),
    00041       NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCDSWW_CANCELOK), SetMinimalSize(101, 12), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
    00042       NWidget(NWID_SPACER), SetFill(1, 0),
    00043     EndContainer(),
    00044     NWidget(NWID_SPACER), SetMinimalSize(0, 4),
    00045   EndContainer(),
    00046 };
    00047
    00049 static const WindowDesc _network_content_download_status_window_desc(
    00050   WDP_CENTER, 0, 0,
    00051   WC_NETWORK_STATUS_WINDOW, WC_NONE,
    00052   WDF_MODAL,
    00053   _nested_network_content_download_status_window_widgets, lengthof(_nested_network_content_download_status_window_widgets)
    00054 );
    00055
    00057 struct NetworkContentDownloadStatusWindow : public Window, ContentCallback {
    00058 private:
    00059   ClientNetworkContentSocketHandler *connection;
    00060   SmallVector<ContentType, 4> receivedTypes;
    00061
    00062   uint total_files;
    00063   uint downloaded_files;
    00064   uint total_bytes;
    00065   uint downloaded_bytes;
    00066
    00067   uint32 cur_id;
    00068   char name[48];
    00069
    00070 public:
    00076   NetworkContentDownloadStatusWindow() :
    00077     cur_id(UINT32_MAX)
    00078   {
    00079     this->parent = FindWindowById(WC_NETWORK_WINDOW, 1);
    00080
    00081     _network_content_client.AddCallback(this);
    00082     _network_content_client.DownloadSelectedContent(this->total_files, this->total_bytes);
    00083
    00084     this->InitNested(&_network_content_download_status_window_desc, 0);
    00085   }
    00086
    00088   ~NetworkContentDownloadStatusWindow()
    00089   {
    00090     /* Tell all the backends about what we've downloaded */
    00091     for (ContentType *iter = this->receivedTypes.Begin(); iter != this->receivedTypes.End(); iter++) {
    00092       switch (*iter) {
    00093         case CONTENT_TYPE_AI:
    00094         case CONTENT_TYPE_AI_LIBRARY:
    00095           AI::Rescan();
    00096           SetWindowClassesDirty(WC_AI_DEBUG);
    00097           break;
    00098
    00099         case CONTENT_TYPE_BASE_GRAPHICS:
    00100           BaseGraphics::FindSets();
    00101           SetWindowDirty(WC_GAME_OPTIONS, 0);
    00102           break;
    00103
    00104         case CONTENT_TYPE_BASE_SOUNDS:
    00105           BaseSounds::FindSets();
    00106           SetWindowDirty(WC_GAME_OPTIONS, 0);
    00107           break;
    00108
    00109         case CONTENT_TYPE_NEWGRF:
    00110           ScanNewGRFFiles();
    00111           /* Yes... these are the NewGRF windows */
    00112           InvalidateWindowClassesData(WC_SAVELOAD);
    00113           InvalidateWindowData(WC_GAME_OPTIONS, 0, 1);
    00114           break;
    00115
    00116         case CONTENT_TYPE_SCENARIO:
    00117         case CONTENT_TYPE_HEIGHTMAP:
    00118           extern void ScanScenarios();
    00119           ScanScenarios();
    00120           InvalidateWindowData(WC_SAVELOAD, 0, 0);
    00121           break;
    00122
    00123         default:
    00124           break;
    00125       }
    00126     }
    00127
    00128     /* Always invalidate the download window; tell it we are going to be gone */
    00129     InvalidateWindowData(WC_NETWORK_WINDOW, 1, 2);
    00130     _network_content_client.RemoveCallback(this);
    00131   }
    00132
    00133   virtual void OnPaint()
    00134   {
    00135     this->DrawWidgets();
    00136   }
    00137
    00138   virtual void DrawWidget(const Rect &r, int widget) const
    00139   {
    00140     if (widget != NCDSWW_BACKGROUND) return;
    00141
    00142     /* Draw nice progress bar :) */
    00143     DrawFrameRect(r.left + 20, r.top + 4, r.left + 20 + (int)((this->width - 40LL) * this->downloaded_bytes / this->total_bytes), r.top + 14, COLOUR_MAUVE, FR_NONE);
    00144
    00145     int y = r.top + 20;
    00146     SetDParam(0, this->downloaded_bytes);
    00147     SetDParam(1, this->total_bytes);
    00148     SetDParam(2, this->downloaded_bytes * 100LL / this->total_bytes);
    00149     DrawString(r.left + 2, r.right - 2, y, STR_CONTENT_DOWNLOAD_PROGRESS_SIZE, TC_FROMSTRING, SA_CENTER);
    00150
    00151     StringID str;
    00152     if (this->downloaded_bytes == this->total_bytes) {
    00153       str = STR_CONTENT_DOWNLOAD_COMPLETE;
    00154     } else if (!StrEmpty(this->name)) {
    00155       SetDParamStr(0, this->name);
    00156       SetDParam(1, this->downloaded_files);
    00157       SetDParam(2, this->total_files);
    00158       str = STR_CONTENT_DOWNLOAD_FILE;
    00159     } else {
    00160       str = STR_CONTENT_DOWNLOAD_INITIALISE;
    00161     }
    00162
    00163     y += FONT_HEIGHT_NORMAL + 5;
    00164     DrawStringMultiLine(r.left + 2, r.right - 2, y, y + FONT_HEIGHT_NORMAL * 2, str, TC_FROMSTRING, SA_CENTER);
    00165   }
    00166
    00167   virtual void OnClick(Point pt, int widget)
    00168   {
    00169     if (widget == NCDSWW_CANCELOK) {
    00170       if (this->downloaded_bytes != this->total_bytes) _network_content_client.Close();
    00171       delete this;
    00172     }
    00173   }
    00174
    00175   virtual void OnDownloadProgress(const ContentInfo *ci, uint bytes)
    00176   {
    00177     if (ci->id != this->cur_id) {
    00178       strecpy(this->name, ci->filename, lastof(this->name));
    00179       this->cur_id = ci->id;
    00180       this->downloaded_files++;
    00181       this->receivedTypes.Include(ci->type);
    00182     }
    00183     this->downloaded_bytes += bytes;
    00184
    00185     /* When downloading is finished change cancel in ok */
    00186     if (this->downloaded_bytes == this->total_bytes) {
    00187       this->GetWidget<NWidgetCore>(NCDSWW_CANCELOK)->widget_data = STR_BUTTON_OK;
    00188     }
    00189
    00190     this->SetDirty();
    00191   }
    00192 };
    00193
    00195 enum NetworkContentListWindowWidgets {
    00196   NCLWW_BACKGROUND,
    00197
    00198   NCLWW_FILTER_CAPT,
    00199   NCLWW_FILTER,
    00200
    00201   NCLWW_CHECKBOX,
    00202   NCLWW_TYPE,
    00203   NCLWW_NAME,
    00204
    00205   NCLWW_MATRIX,
    00206   NCLWW_SCROLLBAR,
    00207
    00208   NCLWW_DETAILS,
    00209
    00210   NCLWW_SELECT_ALL,
    00211   NCLWW_SELECT_UPDATE,
    00212   NCLWW_UNSELECT,
    00213   NCLWW_CANCEL,
    00214   NCLWW_DOWNLOAD,
    00215
    00216   NCLWW_SEL_ALL_UPDATE,
    00217 };
    00218
    00220 class NetworkContentListWindow : public QueryStringBaseWindow, ContentCallback {
    00221   typedef GUIList<const ContentInfo*> GUIContentList;
    00222
    00223   enum {
    00224     EDITBOX_MAX_SIZE = 50,
    00225     EDITBOX_MAX_LENGTH = 300,
    00226   };
    00227
    00229   static Listing last_sorting;
    00230   static Filtering last_filtering;
    00232   static GUIContentList::SortFunction * const sorter_funcs[];
    00233   static GUIContentList::FilterFunction * const filter_funcs[];
    00234   GUIContentList content;
    00235
    00236   const ContentInfo *selected;
    00237   int list_pos;
    00238   uint filesize_sum;
    00239
    00244   void BuildContentList()
    00245   {
    00246     if (!this->content.NeedRebuild()) return;
    00247
    00248     /* Create temporary array of games to use for listing */
    00249     this->content.Clear();
    00250
    00251     for (ConstContentIterator iter = _network_content_client.Begin(); iter != _network_content_client.End(); iter++) {
    00252       *this->content.Append() = *iter;
    00253     }
    00254
    00255     this->FilterContentList();
    00256     this->content.Compact();
    00257     this->content.RebuildDone();
    00258     this->SortContentList();
    00259
    00260     this->vscroll.SetCount(this->content.Length()); // Update the scrollbar
    00261     this->ScrollToSelected();
    00262   }
    00263
    00265   static int CDECL NameSorter(const ContentInfo * const *a, const ContentInfo * const *b)
    00266   {
    00267     return strcasecmp((*a)->name, (*b)->name);
    00268   }
    00269
    00271   static int CDECL TypeSorter(const ContentInfo * const *a, const ContentInfo * const *b)
    00272   {
    00273     int r = 0;
    00274     if ((*a)->type != (*b)->type) {
    00275       char a_str[64];
    00276       char b_str[64];
    00277       GetString(a_str, STR_CONTENT_TYPE_BASE_GRAPHICS + (*a)->type - CONTENT_TYPE_BASE_GRAPHICS, lastof(a_str));
    00278       GetString(b_str, STR_CONTENT_TYPE_BASE_GRAPHICS + (*b)->type - CONTENT_TYPE_BASE_GRAPHICS, lastof(b_str));
    00279       r = strcasecmp(a_str, b_str);
    00280     }
    00281     if (r == 0) r = NameSorter(a, b);
    00282     return r;
    00283   }
    00284
    00286   static int CDECL StateSorter(const ContentInfo * const *a, const ContentInfo * const *b)
    00287   {
    00288     int r = (*a)->state - (*b)->state;
    00289     if (r == 0) r = TypeSorter(a, b);
    00290     return r;
    00291   }
    00292
    00294   void SortContentList()
    00295   {
    00296     if (!this->content.Sort()) return;
    00297
    00298     for (ConstContentIterator iter = this->content.Begin(); iter != this->content.End(); iter++) {
    00299       if (*iter == this->selected) {
    00300         this->list_pos = iter - this->content.Begin();
    00301         break;
    00302       }
    00303     }
    00304   }
    00305
    00307   static bool CDECL TagNameFilter(const ContentInfo * const *a, const char *filter_string)
    00308   {
    00309     for (int i = 0; i < (*a)->tag_count; i++) {
    00310       if (strcasestr((*a)->tags[i], filter_string) != NULL) return true;
    00311     }
    00312     return strcasestr((*a)->name, filter_string) != NULL;
    00313   }
    00314
    00316   void FilterContentList()
    00317   {
    00318     if (!this->content.Filter(this->edit_str_buf)) return;
    00319
    00320     /* update list position */
    00321     for (ConstContentIterator iter = this->content.Begin(); iter != this->content.End(); iter++) {
    00322       if (*iter == this->selected) {
    00323         this->list_pos = iter - this->content.Begin();
    00324         return;
    00325       }
    00326     }
    00327
    00328     /* previously selected item not in list anymore */
    00329     this->selected = NULL;
    00330     this->list_pos = 0;
    00331   }
    00332
    00334   void ScrollToSelected()
    00335   {
    00336     if (this->selected == NULL) return;
    00337
    00338     this->vscroll.ScrollTowards(this->list_pos);
    00339   }
    00340
    00341 public:
    00346   NetworkContentListWindow(const WindowDesc *desc, bool select_all) :
    00347       QueryStringBaseWindow(EDITBOX_MAX_SIZE),
    00348       selected(NULL),
    00349       list_pos(0)
    00350   {
    00351     this->InitNested(desc, 1);
    00352
    00353     this->GetWidget<NWidgetStacked>(NCLWW_SEL_ALL_UPDATE)->SetDisplayedPlane(select_all);
    00354
    00355     this->afilter = CS_ALPHANUMERAL;
    00356     InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, EDITBOX_MAX_LENGTH);
    00357     this->SetFocusedWidget(NCLWW_FILTER);
    00358
    00359     _network_content_client.AddCallback(this);
    00360     this->content.SetListing(this->last_sorting);
    00361     this->content.SetFiltering(this->last_filtering);
    00362     this->content.SetSortFuncs(this->sorter_funcs);
    00363     this->content.SetFilterFuncs(this->filter_funcs);
    00364     this->content.ForceRebuild();
    00365     this->FilterContentList();
    00366     this->SortContentList();
    00367     this->InvalidateData();
    00368   }
    00369
    00371   ~NetworkContentListWindow()
    00372   {
    00373     _network_content_client.RemoveCallback(this);
    00374   }
    00375
    00376   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00377   {
    00378     switch (widget) {
    00379       case NCLWW_FILTER_CAPT:
    00380         *size = maxdim(*size, GetStringBoundingBox(STR_CONTENT_FILTER_TITLE));
    00381         break;
    00382
    00383       case NCLWW_TYPE: {
    00384         Dimension d = *size;
    00385         for (int i = CONTENT_TYPE_BEGIN; i < CONTENT_TYPE_END; i++) {
    00386           d = maxdim(d, GetStringBoundingBox(STR_CONTENT_TYPE_BASE_GRAPHICS + i - CONTENT_TYPE_BASE_GRAPHICS));
    00387         }
    00388         size->width = d.width + WD_MATRIX_RIGHT + WD_MATRIX_LEFT;
    00389         break;
    00390       }
    00391
    00392       case NCLWW_MATRIX:
    00393         resize->height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
    00394         size->height = 10 * resize->height;
    00395         break;
    00396     }
    00397   }
    00398
    00399
    00400   virtual void DrawWidget(const Rect &r, int widget) const
    00401   {
    00402     switch (widget) {
    00403       case NCLWW_FILTER_CAPT:
    00404         DrawString(r.left, r.right, r.top, STR_CONTENT_FILTER_TITLE, TC_FROMSTRING, SA_RIGHT);
    00405         break;
    00406
    00407       case NCLWW_DETAILS:
    00408         this->DrawDetails(r);
    00409         break;
    00410
    00411       case NCLWW_MATRIX:
    00412         this->DrawMatrix(r);
    00413         break;
    00414     }
    00415   }
    00416
    00417   virtual void OnPaint()
    00418   {
    00419     const SortButtonState arrow = this->content.IsDescSortOrder() ? SBS_DOWN : SBS_UP;
    00420
    00421     if (this->content.NeedRebuild()) {
    00422       this->BuildContentList();
    00423     }
    00424
    00425     this->DrawWidgets();
    00426
    00427     /* Edit box to filter for keywords */
    00428     this->DrawEditBox(NCLWW_FILTER);
    00429
    00430     switch (this->content.SortType()) {
    00431       case NCLWW_CHECKBOX - NCLWW_CHECKBOX: this->DrawSortButtonState(NCLWW_CHECKBOX, arrow); break;
    00432       case NCLWW_TYPE     - NCLWW_CHECKBOX: this->DrawSortButtonState(NCLWW_TYPE,     arrow); break;
    00433       case NCLWW_NAME     - NCLWW_CHECKBOX: this->DrawSortButtonState(NCLWW_NAME,     arrow); break;
    00434     }
    00435   }
    00436
    00437   void DrawMatrix(const Rect &r) const
    00438   {
    00439     const NWidgetBase *nwi_checkbox = this->GetWidget<NWidgetBase>(NCLWW_CHECKBOX);
    00440     const NWidgetBase *nwi_name = this->GetWidget<NWidgetBase>(NCLWW_NAME);
    00441     const NWidgetBase *nwi_type = this->GetWidget<NWidgetBase>(NCLWW_TYPE);
    00442
    00443
    00444     /* Fill the matrix with the information */
    00445     int sprite_y_offset = WD_MATRIX_TOP + (FONT_HEIGHT_NORMAL - 10) / 2;
    00446     uint y = r.top;
    00447     int cnt = 0;
    00448     for (ConstContentIterator iter = this->content.Get(this->vscroll.GetPosition()); iter != this->content.End() && cnt < this->vscroll.GetCapacity(); iter++, cnt++) {
    00449       const ContentInfo *ci = *iter;
    00450
    00451       if (ci == this->selected) GfxFillRect(r.left + 1, y + 1, r.right - 1, y + this->resize.step_height - 1, 10);
    00452
    00453       SpriteID sprite;
    00454       SpriteID pal = PAL_NONE;
    00455       switch (ci->state) {
    00456         case ContentInfo::UNSELECTED:     sprite = SPR_BOX_EMPTY;   break;
    00457         case ContentInfo::SELECTED:       sprite = SPR_BOX_CHECKED; break;
    00458         case ContentInfo::AUTOSELECTED:   sprite = SPR_BOX_CHECKED; break;
    00459         case ContentInfo::ALREADY_HERE:   sprite = SPR_BLOT; pal = PALETTE_TO_GREEN; break;
    00460         case ContentInfo::DOES_NOT_EXIST: sprite = SPR_BLOT; pal = PALETTE_TO_RED;   break;
    00461         default: NOT_REACHED();
    00462       }
    00463       DrawSprite(sprite, pal, nwi_checkbox->pos_x + (pal == PAL_NONE ? 2 : 3), y + sprite_y_offset + (pal == PAL_NONE ? 1 : 0));
    00464
    00465       StringID str = STR_CONTENT_TYPE_BASE_GRAPHICS + ci->type - CONTENT_TYPE_BASE_GRAPHICS;
    00466       DrawString(nwi_type->pos_x, nwi_type->pos_x + nwi_type->current_x - 1, y + WD_MATRIX_TOP, str, TC_BLACK, SA_CENTER);
    00467
    00468       DrawString(nwi_name->pos_x + WD_FRAMERECT_LEFT, nwi_name->pos_x + nwi_name->current_x - WD_FRAMERECT_RIGHT, y + WD_MATRIX_TOP, ci->name, TC_BLACK);
    00469       y += this->resize.step_height;
    00470     }
    00471   }
    00472
    00477   void DrawDetails(const Rect &r) const
    00478   {
    00479     static const int DETAIL_LEFT         =  5;
    00480     static const int DETAIL_RIGHT        =  5;
    00481     static const int DETAIL_TOP          =  5;
    00482
    00483     /* Height for the title banner */
    00484     int DETAIL_TITLE_HEIGHT = 5 * FONT_HEIGHT_NORMAL;
    00485
    00486     /* Create the nice grayish rectangle at the details top */
    00487     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.top + DETAIL_TITLE_HEIGHT, 157);
    00488     DrawString(r.left + WD_INSET_LEFT, r.right - WD_INSET_RIGHT, r.top + FONT_HEIGHT_NORMAL + WD_INSET_TOP, STR_CONTENT_DETAIL_TITLE, TC_FROMSTRING, SA_CENTER);
    00489
    00490     /* Draw the total download size */
    00491     SetDParam(0, this->filesize_sum);
    00492     DrawString(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, r.bottom - FONT_HEIGHT_NORMAL - WD_PAR_VSEP_NORMAL, STR_CONTENT_TOTAL_DOWNLOAD_SIZE);
    00493
    00494     if (this->selected == NULL) return;
    00495
    00496     /* And fill the rest of the details when there's information to place there */
    00497     DrawStringMultiLine(r.left + WD_INSET_LEFT, r.right - WD_INSET_RIGHT, r.top + DETAIL_TITLE_HEIGHT / 2, r.top + DETAIL_TITLE_HEIGHT, STR_CONTENT_DETAIL_SUBTITLE_UNSELECTED + this->selected->state, TC_FROMSTRING, SA_CENTER);
    00498
    00499     /* Also show the total download size, so keep some space from the bottom */
    00500     const uint max_y = r.bottom - FONT_HEIGHT_NORMAL - WD_PAR_VSEP_WIDE;
    00501     int y = r.top + DETAIL_TITLE_HEIGHT + DETAIL_TOP;
    00502
    00503     if (this->selected->upgrade) {
    00504       SetDParam(0, STR_CONTENT_TYPE_BASE_GRAPHICS + this->selected->type - CONTENT_TYPE_BASE_GRAPHICS);
    00505       y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_UPDATE);
    00506       y += WD_PAR_VSEP_WIDE;
    00507     }
    00508
    00509     SetDParamStr(0, this->selected->name);
    00510     y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_NAME);
    00511
    00512     if (!StrEmpty(this->selected->version)) {
    00513       SetDParamStr(0, this->selected->version);
    00514       y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_VERSION);
    00515     }
    00516
    00517     if (!StrEmpty(this->selected->description)) {
    00518       SetDParamStr(0, this->selected->description);
    00519       y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_DESCRIPTION);
    00520     }
    00521
    00522     if (!StrEmpty(this->selected->url)) {
    00523       SetDParamStr(0, this->selected->url);
    00524       y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_URL);
    00525     }
    00526
    00527     SetDParam(0, STR_CONTENT_TYPE_BASE_GRAPHICS + this->selected->type - CONTENT_TYPE_BASE_GRAPHICS);
    00528     y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_TYPE);
    00529
    00530     y += WD_PAR_VSEP_WIDE;
    00531     SetDParam(0, this->selected->filesize);
    00532     y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_FILESIZE);
    00533
    00534     if (this->selected->dependency_count != 0) {
    00535       /* List dependencies */
    00536       char buf[DRAW_STRING_BUFFER] = "";
    00537       char *p = buf;
    00538       for (uint i = 0; i < this->selected->dependency_count; i++) {
    00539         ContentID cid = this->selected->dependencies[i];
    00540
    00541         /* Try to find the dependency */
    00542         ConstContentIterator iter = _network_content_client.Begin();
    00543         for (; iter != _network_content_client.End(); iter++) {
    00544           const ContentInfo *ci = *iter;
    00545           if (ci->id != cid) continue;
    00546
    00547           p += seprintf(p, lastof(buf), p == buf ? "%s" : ", %s", (*iter)->name);
    00548           break;
    00549         }
    00550       }
    00551       SetDParamStr(0, buf);
    00552       y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_DEPENDENCIES);
    00553     }
    00554
    00555     if (this->selected->tag_count != 0) {
    00556       /* List all tags */
    00557       char buf[DRAW_STRING_BUFFER] = "";
    00558       char *p = buf;
    00559       for (uint i = 0; i < this->selected->tag_count; i++) {
    00560         p += seprintf(p, lastof(buf), i == 0 ? "%s" : ", %s", this->selected->tags[i]);
    00561       }
    00562       SetDParamStr(0, buf);
    00563       y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_TAGS);
    00564     }
    00565
    00566     if (this->selected->IsSelected()) {
    00567       /* When selected show all manually selected content that depends on this */
    00568       ConstContentVector tree;
    00569       _network_content_client.ReverseLookupTreeDependency(tree, this->selected);
    00570
    00571       char buf[DRAW_STRING_BUFFER] = "";
    00572       char *p = buf;
    00573       for (ConstContentIterator iter = tree.Begin(); iter != tree.End(); iter++) {
    00574         const ContentInfo *ci = *iter;
    00575         if (ci == this->selected || ci->state != ContentInfo::SELECTED) continue;
    00576
    00577         p += seprintf(p, lastof(buf), buf == p ? "%s" : ", %s", ci->name);
    00578       }
    00579       if (p != buf) {
    00580         SetDParamStr(0, buf);
    00581         y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_SELECTED_BECAUSE_OF);
    00582       }
    00583     }
    00584   }
    00585
    00586   virtual void OnDoubleClick(Point pt, int widget)
    00587   {
    00588     /* Double clicking on a line in the matrix toggles the state of the checkbox */
    00589     if (widget != NCLWW_MATRIX) return;
    00590
    00591     pt.x = this->GetWidget<NWidgetBase>(NCLWW_CHECKBOX)->pos_x;
    00592     this->OnClick(pt, widget);
    00593   }
    00594
    00595   virtual void OnClick(Point pt, int widget)
    00596   {
    00597     switch (widget) {
    00598       case NCLWW_MATRIX: {
    00599         uint32 id_v = (pt.y - this->GetWidget<NWidgetBase>(NCLWW_MATRIX)->pos_y) / this->resize.step_height;
    00600
    00601         if (id_v >= this->vscroll.GetCapacity()) return; // click out of bounds
    00602         id_v += this->vscroll.GetPosition();
    00603
    00604         if (id_v >= this->content.Length()) return; // click out of bounds
    00605
    00606         this->selected = *this->content.Get(id_v);
    00607         this->list_pos = id_v;
    00608
    00609         if (pt.x <= (int)(this->GetWidget<NWidgetBase>(NCLWW_CHECKBOX)->pos_y + this->GetWidget<NWidgetBase>(NCLWW_CHECKBOX)->current_y)) {
    00610           _network_content_client.ToggleSelectedState(this->selected);
    00611           this->content.ForceResort();
    00612         }
    00613
    00614         this->InvalidateData();
    00615       } break;
    00616
    00617       case NCLWW_CHECKBOX:
    00618       case NCLWW_TYPE:
    00619       case NCLWW_NAME:
    00620         if (this->content.SortType() == widget - NCLWW_CHECKBOX) {
    00621           this->content.ToggleSortOrder();
    00622           this->list_pos = this->content.Length() - this->list_pos - 1;
    00623         } else {
    00624           this->content.SetSortType(widget - NCLWW_CHECKBOX);
    00625           this->content.ForceResort();
    00626           this->SortContentList();
    00627         }
    00628         this->ScrollToSelected();
    00629         this->InvalidateData();
    00630         break;
    00631
    00632       case NCLWW_SELECT_ALL:
    00633         _network_content_client.SelectAll();
    00634         this->InvalidateData();
    00635         break;
    00636
    00637       case NCLWW_SELECT_UPDATE:
    00638         _network_content_client.SelectUpgrade();
    00639         this->InvalidateData();
    00640         break;
    00641
    00642       case NCLWW_UNSELECT:
    00643         _network_content_client.UnselectAll();
    00644         this->InvalidateData();
    00645         break;
    00646
    00647       case NCLWW_CANCEL:
    00648         delete this;
    00649         break;
    00650
    00651       case NCLWW_DOWNLOAD:
    00652         if (BringWindowToFrontById(WC_NETWORK_STATUS_WINDOW, 0) == NULL) new NetworkContentDownloadStatusWindow();
    00653         break;
    00654     }
    00655   }
    00656
    00657   virtual void OnMouseLoop()
    00658   {
    00659     this->HandleEditBox(NCLWW_FILTER);
    00660   }
    00661
    00662   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    00663   {
    00664     switch (keycode) {
    00665       case WKC_UP:
    00666         /* scroll up by one */
    00667         if (this->list_pos > 0) this->list_pos--;
    00668         break;
    00669       case WKC_DOWN:
    00670         /* scroll down by one */
    00671         if (this->list_pos < (int)this->content.Length() - 1) this->list_pos++;
    00672         break;
    00673       case WKC_PAGEUP:
    00674         /* scroll up a page */
    00675         this->list_pos = (this->list_pos < this->vscroll.GetCapacity()) ? 0 : this->list_pos - this->vscroll.GetCapacity();
    00676         break;
    00677       case WKC_PAGEDOWN:
    00678         /* scroll down a page */
    00679         this->list_pos = min(this->list_pos + this->vscroll.GetCapacity(), (int)this->content.Length() - 1);
    00680         break;
    00681       case WKC_HOME:
    00682         /* jump to beginning */
    00683         this->list_pos = 0;
    00684         break;
    00685       case WKC_END:
    00686         /* jump to end */
    00687         this->list_pos = this->content.Length() - 1;
    00688         break;
    00689
    00690       case WKC_SPACE:
    00691       case WKC_RETURN:
    00692         if (keycode == WKC_RETURN || !IsWidgetFocused(NCLWW_FILTER)) {
    00693           if (this->selected != NULL) {
    00694             _network_content_client.ToggleSelectedState(this->selected);
    00695             this->content.ForceResort();
    00696             this->InvalidateData();
    00697           }
    00698           return ES_HANDLED;
    00699         }
    00700         /* Fall through when pressing space is pressed and filter isn't focused */
    00701
    00702       default: {
    00703         /* Handle editbox input */
    00704         EventState state = ES_NOT_HANDLED;
    00705         if (this->HandleEditBoxKey(NCLWW_FILTER, key, keycode, state) == HEBR_EDITING) {
    00706           this->OnOSKInput(NCLWW_FILTER);
    00707         }
    00708
    00709         return state;
    00710       }
    00711     }
    00712
    00713     if (_network_content_client.Length() == 0) return ES_HANDLED;
    00714
    00715     this->selected = *this->content.Get(this->list_pos);
    00716
    00717     /* scroll to the new server if it is outside the current range */
    00718     this->ScrollToSelected();
    00719
    00720     /* redraw window */
    00721     this->InvalidateData();
    00722     return ES_HANDLED;
    00723   }
    00724
    00725   virtual void OnOSKInput(int wid)
    00726   {
    00727     this->content.SetFilterState(!StrEmpty(this->edit_str_buf));
    00728     this->content.ForceRebuild();
    00729     this->InvalidateData();
    00730   }
    00731
    00732   virtual void OnResize()
    00733   {
    00734     this->vscroll.SetCapacityFromWidget(this, NCLWW_MATRIX);
    00735     this->GetWidget<NWidgetCore>(NCLWW_MATRIX)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00736   }
    00737
    00738   virtual void OnReceiveContentInfo(const ContentInfo *rci)
    00739   {
    00740     this->content.ForceRebuild();
    00741     this->InvalidateData();
    00742   }
    00743
    00744   virtual void OnDownloadComplete(ContentID cid)
    00745   {
    00746     this->content.ForceResort();
    00747     this->InvalidateData();
    00748   }
    00749
    00750   virtual void OnConnect(bool success)
    00751   {
    00752     if (!success) {
    00753       ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_CONNECT, INVALID_STRING_ID, 0, 0);
    00754       delete this;
    00755       return;
    00756     }
    00757
    00758     this->InvalidateData();
    00759   }
    00760
    00761   virtual void OnInvalidateData(int data)
    00762   {
    00763     if (this->content.NeedRebuild()) this->BuildContentList();
    00764
    00765     /* To sum all the bytes we intend to download */
    00766     this->filesize_sum = 0;
    00767     bool show_select_all = false;
    00768     bool show_select_upgrade = false;
    00769     for (ConstContentIterator iter = this->content.Begin(); iter != this->content.End(); iter++) {
    00770       const ContentInfo *ci = *iter;
    00771       switch (ci->state) {
    00772         case ContentInfo::SELECTED:
    00773         case ContentInfo::AUTOSELECTED:
    00774           this->filesize_sum += ci->filesize;
    00775           break;
    00776
    00777         case ContentInfo::UNSELECTED:
    00778           show_select_all = true;
    00779           show_select_upgrade |= ci->upgrade;
    00780           break;
    00781
    00782         default:
    00783           break;
    00784       }
    00785     }
    00786
    00787     /* If data == 2 then the status window caused this OnInvalidate */
    00788     this->SetWidgetDisabledState(NCLWW_DOWNLOAD, this->filesize_sum == 0 || (FindWindowById(WC_NETWORK_STATUS_WINDOW, 0) != NULL && data != 2));
    00789     this->SetWidgetDisabledState(NCLWW_UNSELECT, this->filesize_sum == 0);
    00790     this->SetWidgetDisabledState(NCLWW_SELECT_ALL, !show_select_all);
    00791     this->SetWidgetDisabledState(NCLWW_SELECT_UPDATE, !show_select_upgrade);
    00792
    00793     this->GetWidget<NWidgetCore>(NCLWW_CANCEL)->widget_data = this->filesize_sum == 0 ? STR_AI_SETTINGS_CLOSE : STR_AI_LIST_CANCEL;
    00794   }
    00795 };
    00796
    00797 Listing NetworkContentListWindow::last_sorting = {false, 1};
    00798 Filtering NetworkContentListWindow::last_filtering = {false, 0};
    00799
    00800 NetworkContentListWindow::GUIContentList::SortFunction * const NetworkContentListWindow::sorter_funcs[] = {
    00801   &StateSorter,
    00802   &TypeSorter,
    00803   &NameSorter,
    00804 };
    00805
    00806 NetworkContentListWindow::GUIContentList::FilterFunction * const NetworkContentListWindow::filter_funcs[] = {
    00807   &TagNameFilter,
    00808 };
    00809
    00810 static const NWidgetPart _nested_network_content_list_widgets[] = {
    00811   NWidget(NWID_HORIZONTAL),
    00812     NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
    00813     NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_CONTENT_TITLE, STR_NULL),
    00814   EndContainer(),
    00815   NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, NCLWW_BACKGROUND),
    00816     NWidget(NWID_SPACER), SetMinimalSize(0, 7), SetResize(1, 0),
    00817     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(8, 8, 8),
    00818       /* Top */
    00819       NWidget(WWT_EMPTY, COLOUR_LIGHT_BLUE, NCLWW_FILTER_CAPT), SetFill(1, 0), SetResize(1, 0),
    00820       NWidget(WWT_EDITBOX, COLOUR_LIGHT_BLUE, NCLWW_FILTER), SetFill(1, 0), SetResize(1, 0),
    00821             SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
    00822     EndContainer(),
    00823     NWidget(NWID_SPACER), SetMinimalSize(0, 7), SetResize(1, 0),
    00824     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(8, 8, 8),
    00825       /* Left side. */
    00826       NWidget(NWID_VERTICAL),
    00827         NWidget(NWID_HORIZONTAL),
    00828           NWidget(NWID_VERTICAL),
    00829             NWidget(NWID_HORIZONTAL),
    00830               NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCLWW_CHECKBOX), SetMinimalSize(13, 1), SetDataTip(STR_EMPTY, STR_NULL),
    00831               NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCLWW_TYPE),
    00832                       SetDataTip(STR_CONTENT_TYPE_CAPTION, STR_CONTENT_TYPE_CAPTION_TOOLTIP),
    00833               NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCLWW_NAME), SetResize(1, 0), SetFill(1, 0),
    00834                       SetDataTip(STR_CONTENT_NAME_CAPTION, STR_CONTENT_NAME_CAPTION_TOOLTIP),
    00835             EndContainer(),
    00836             NWidget(WWT_MATRIX, COLOUR_LIGHT_BLUE, NCLWW_MATRIX), SetResize(1, 14), SetFill(1, 1),
    00837           EndContainer(),
    00838           NWidget(WWT_SCROLLBAR, COLOUR_LIGHT_BLUE, NCLWW_SCROLLBAR),
    00839         EndContainer(),
    00840       EndContainer(),
    00841       /* Right side. */
    00842       NWidget(NWID_VERTICAL),
    00843         NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, NCLWW_DETAILS), SetResize(1, 1), SetFill(1, 1), EndContainer(),
    00844       EndContainer(),
    00845     EndContainer(),
    00846     NWidget(NWID_SPACER), SetMinimalSize(0, 7), SetResize(1, 0),
    00847     /* Bottom. */
    00848     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(8, 8, 8),
    00849       NWidget(NWID_SELECTION, INVALID_COLOUR, NCLWW_SEL_ALL_UPDATE), SetResize(1, 0), SetFill(1, 0),
    00850         NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCLWW_SELECT_UPDATE), SetResize(1, 0), SetFill(1, 0),
    00851                     SetDataTip(STR_CONTENT_SELECT_UPDATES_CAPTION, STR_CONTENT_SELECT_UPDATES_CAPTION_TOOLTIP),
    00852         NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCLWW_SELECT_ALL), SetResize(1, 0), SetFill(1, 0),
    00853                     SetDataTip(STR_CONTENT_SELECT_ALL_CAPTION, STR_CONTENT_SELECT_ALL_CAPTION_TOOLTIP),
    00854       EndContainer(),
    00855       NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCLWW_UNSELECT), SetResize(1, 0), SetFill(1, 0),
    00856                     SetDataTip(STR_CONTENT_UNSELECT_ALL_CAPTION, STR_CONTENT_UNSELECT_ALL_CAPTION_TOOLTIP),
    00857       NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCLWW_CANCEL), SetResize(1, 0), SetFill(1, 0),
    00858                     SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
    00859       NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCLWW_DOWNLOAD), SetResize(1, 0), SetFill(1, 0),
    00860                     SetDataTip(STR_CONTENT_DOWNLOAD_CAPTION, STR_CONTENT_DOWNLOAD_CAPTION_TOOLTIP),
    00861     EndContainer(),
    00862     NWidget(NWID_SPACER), SetMinimalSize(0, 2), SetResize(1, 0),
    00863     /* Resize button. */
    00864     NWidget(NWID_HORIZONTAL),
    00865       NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
    00866       NWidget(WWT_RESIZEBOX, COLOUR_LIGHT_BLUE),
    00867     EndContainer(),
    00868   EndContainer(),
    00869 };
    00870
    00872 static const WindowDesc _network_content_list_desc(
    00873   WDP_CENTER, 630, 460,
    00874   WC_NETWORK_WINDOW, WC_NONE,
    00875   WDF_UNCLICK_BUTTONS,
    00876   _nested_network_content_list_widgets, lengthof(_nested_network_content_list_widgets)
    00877 );
    00878
    00884 void ShowNetworkContentListWindow(ContentVector *cv, ContentType type)
    00885 {
    00886 #if defined(WITH_ZLIB)
    00887   _network_content_client.Clear();
    00888   if (cv == NULL) {
    00889     _network_content_client.RequestContentList(type);
    00890   } else {
    00891     _network_content_client.RequestContentList(cv, true);
    00892   }
    00893
    00894   DeleteWindowById(WC_NETWORK_WINDOW, 1);
    00895   new NetworkContentListWindow(&_network_content_list_desc, cv != NULL);
    00896 #else
    00897   ShowErrorMessage(STR_CONTENT_NO_ZLIB, STR_CONTENT_NO_ZLIB_SUB, 0, 0);
    00898   /* Connection failed... clean up the mess */
    00899   if (cv != NULL) {
    00900     for (ContentIterator iter = cv->Begin(); iter != cv->End(); iter++) delete *iter;
    00901   }
    00902 #endif /* WITH_ZLIB */
    00903 }
    00904
    00905 #endif /* ENABLE_NETWORK */