# network_content_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: network_content_gui.cpp 18966 2010-01-30 18:34:48Z frosch $ */
    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, int click_count)
    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 OnClick(Point pt, int widget, int click_count)
    00587   {
    00588     switch (widget) {
    00589       case NCLWW_MATRIX: {
    00590         uint32 id_v = (pt.y - this->GetWidget<NWidgetBase>(NCLWW_MATRIX)->pos_y) / this->resize.step_height;
    00591
    00592         if (id_v >= this->vscroll.GetCapacity()) return; // click out of bounds
    00593         id_v += this->vscroll.GetPosition();
    00594
    00595         if (id_v >= this->content.Length()) return; // click out of bounds
    00596
    00597         this->selected = *this->content.Get(id_v);
    00598         this->list_pos = id_v;
    00599
    00600         const NWidgetBase *checkbox = this->GetWidget<NWidgetBase>(NCLWW_CHECKBOX);
    00601         if (click_count > 1 || IsInsideBS(pt.x, checkbox->pos_x, checkbox->current_x)) {
    00602           _network_content_client.ToggleSelectedState(this->selected);
    00603           this->content.ForceResort();
    00604         }
    00605
    00606         this->InvalidateData();
    00607       } break;
    00608
    00609       case NCLWW_CHECKBOX:
    00610       case NCLWW_TYPE:
    00611       case NCLWW_NAME:
    00612         if (this->content.SortType() == widget - NCLWW_CHECKBOX) {
    00613           this->content.ToggleSortOrder();
    00614           this->list_pos = this->content.Length() - this->list_pos - 1;
    00615         } else {
    00616           this->content.SetSortType(widget - NCLWW_CHECKBOX);
    00617           this->content.ForceResort();
    00618           this->SortContentList();
    00619         }
    00620         this->ScrollToSelected();
    00621         this->InvalidateData();
    00622         break;
    00623
    00624       case NCLWW_SELECT_ALL:
    00625         _network_content_client.SelectAll();
    00626         this->InvalidateData();
    00627         break;
    00628
    00629       case NCLWW_SELECT_UPDATE:
    00630         _network_content_client.SelectUpgrade();
    00631         this->InvalidateData();
    00632         break;
    00633
    00634       case NCLWW_UNSELECT:
    00635         _network_content_client.UnselectAll();
    00636         this->InvalidateData();
    00637         break;
    00638
    00639       case NCLWW_CANCEL:
    00640         delete this;
    00641         break;
    00642
    00643       case NCLWW_DOWNLOAD:
    00644         if (BringWindowToFrontById(WC_NETWORK_STATUS_WINDOW, 0) == NULL) new NetworkContentDownloadStatusWindow();
    00645         break;
    00646     }
    00647   }
    00648
    00649   virtual void OnMouseLoop()
    00650   {
    00651     this->HandleEditBox(NCLWW_FILTER);
    00652   }
    00653
    00654   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    00655   {
    00656     switch (keycode) {
    00657       case WKC_UP:
    00658         /* scroll up by one */
    00659         if (this->list_pos > 0) this->list_pos--;
    00660         break;
    00661       case WKC_DOWN:
    00662         /* scroll down by one */
    00663         if (this->list_pos < (int)this->content.Length() - 1) this->list_pos++;
    00664         break;
    00665       case WKC_PAGEUP:
    00666         /* scroll up a page */
    00667         this->list_pos = (this->list_pos < this->vscroll.GetCapacity()) ? 0 : this->list_pos - this->vscroll.GetCapacity();
    00668         break;
    00669       case WKC_PAGEDOWN:
    00670         /* scroll down a page */
    00671         this->list_pos = min(this->list_pos + this->vscroll.GetCapacity(), (int)this->content.Length() - 1);
    00672         break;
    00673       case WKC_HOME:
    00674         /* jump to beginning */
    00675         this->list_pos = 0;
    00676         break;
    00677       case WKC_END:
    00678         /* jump to end */
    00679         this->list_pos = this->content.Length() - 1;
    00680         break;
    00681
    00682       case WKC_SPACE:
    00683       case WKC_RETURN:
    00684         if (keycode == WKC_RETURN || !IsWidgetFocused(NCLWW_FILTER)) {
    00685           if (this->selected != NULL) {
    00686             _network_content_client.ToggleSelectedState(this->selected);
    00687             this->content.ForceResort();
    00688             this->InvalidateData();
    00689           }
    00690           return ES_HANDLED;
    00691         }
    00692         /* Fall through when pressing space is pressed and filter isn't focused */
    00693
    00694       default: {
    00695         /* Handle editbox input */
    00696         EventState state = ES_NOT_HANDLED;
    00697         if (this->HandleEditBoxKey(NCLWW_FILTER, key, keycode, state) == HEBR_EDITING) {
    00698           this->OnOSKInput(NCLWW_FILTER);
    00699         }
    00700
    00701         return state;
    00702       }
    00703     }
    00704
    00705     if (_network_content_client.Length() == 0) return ES_HANDLED;
    00706
    00707     this->selected = *this->content.Get(this->list_pos);
    00708
    00709     /* scroll to the new server if it is outside the current range */
    00710     this->ScrollToSelected();
    00711
    00712     /* redraw window */
    00713     this->InvalidateData();
    00714     return ES_HANDLED;
    00715   }
    00716
    00717   virtual void OnOSKInput(int wid)
    00718   {
    00719     this->content.SetFilterState(!StrEmpty(this->edit_str_buf));
    00720     this->content.ForceRebuild();
    00721     this->InvalidateData();
    00722   }
    00723
    00724   virtual void OnResize()
    00725   {
    00726     this->vscroll.SetCapacityFromWidget(this, NCLWW_MATRIX);
    00727     this->GetWidget<NWidgetCore>(NCLWW_MATRIX)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00728   }
    00729
    00730   virtual void OnReceiveContentInfo(const ContentInfo *rci)
    00731   {
    00732     this->content.ForceRebuild();
    00733     this->InvalidateData();
    00734   }
    00735
    00736   virtual void OnDownloadComplete(ContentID cid)
    00737   {
    00738     this->content.ForceResort();
    00739     this->InvalidateData();
    00740   }
    00741
    00742   virtual void OnConnect(bool success)
    00743   {
    00744     if (!success) {
    00745       ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_CONNECT, INVALID_STRING_ID, 0, 0);
    00746       delete this;
    00747       return;
    00748     }
    00749
    00750     this->InvalidateData();
    00751   }
    00752
    00753   virtual void OnInvalidateData(int data)
    00754   {
    00755     if (this->content.NeedRebuild()) this->BuildContentList();
    00756
    00757     /* To sum all the bytes we intend to download */
    00758     this->filesize_sum = 0;
    00759     bool show_select_all = false;
    00760     bool show_select_upgrade = false;
    00761     for (ConstContentIterator iter = this->content.Begin(); iter != this->content.End(); iter++) {
    00762       const ContentInfo *ci = *iter;
    00763       switch (ci->state) {
    00764         case ContentInfo::SELECTED:
    00765         case ContentInfo::AUTOSELECTED:
    00766           this->filesize_sum += ci->filesize;
    00767           break;
    00768
    00769         case ContentInfo::UNSELECTED:
    00770           show_select_all = true;
    00771           show_select_upgrade |= ci->upgrade;
    00772           break;
    00773
    00774         default:
    00775           break;
    00776       }
    00777     }
    00778
    00779     /* If data == 2 then the status window caused this OnInvalidate */
    00780     this->SetWidgetDisabledState(NCLWW_DOWNLOAD, this->filesize_sum == 0 || (FindWindowById(WC_NETWORK_STATUS_WINDOW, 0) != NULL && data != 2));
    00781     this->SetWidgetDisabledState(NCLWW_UNSELECT, this->filesize_sum == 0);
    00782     this->SetWidgetDisabledState(NCLWW_SELECT_ALL, !show_select_all);
    00783     this->SetWidgetDisabledState(NCLWW_SELECT_UPDATE, !show_select_upgrade);
    00784
    00785     this->GetWidget<NWidgetCore>(NCLWW_CANCEL)->widget_data = this->filesize_sum == 0 ? STR_AI_SETTINGS_CLOSE : STR_AI_LIST_CANCEL;
    00786   }
    00787 };
    00788
    00789 Listing NetworkContentListWindow::last_sorting = {false, 1};
    00790 Filtering NetworkContentListWindow::last_filtering = {false, 0};
    00791
    00792 NetworkContentListWindow::GUIContentList::SortFunction * const NetworkContentListWindow::sorter_funcs[] = {
    00793   &StateSorter,
    00794   &TypeSorter,
    00795   &NameSorter,
    00796 };
    00797
    00798 NetworkContentListWindow::GUIContentList::FilterFunction * const NetworkContentListWindow::filter_funcs[] = {
    00799   &TagNameFilter,
    00800 };
    00801
    00802 static const NWidgetPart _nested_network_content_list_widgets[] = {
    00803   NWidget(NWID_HORIZONTAL),
    00804     NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
    00805     NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_CONTENT_TITLE, STR_NULL),
    00806   EndContainer(),
    00807   NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, NCLWW_BACKGROUND),
    00808     NWidget(NWID_SPACER), SetMinimalSize(0, 7), SetResize(1, 0),
    00809     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(8, 8, 8),
    00810       /* Top */
    00811       NWidget(WWT_EMPTY, COLOUR_LIGHT_BLUE, NCLWW_FILTER_CAPT), SetFill(1, 0), SetResize(1, 0),
    00812       NWidget(WWT_EDITBOX, COLOUR_LIGHT_BLUE, NCLWW_FILTER), SetFill(1, 0), SetResize(1, 0),
    00813             SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
    00814     EndContainer(),
    00815     NWidget(NWID_SPACER), SetMinimalSize(0, 7), SetResize(1, 0),
    00816     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(8, 8, 8),
    00817       /* Left side. */
    00818       NWidget(NWID_VERTICAL),
    00819         NWidget(NWID_HORIZONTAL),
    00820           NWidget(NWID_VERTICAL),
    00821             NWidget(NWID_HORIZONTAL),
    00822               NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCLWW_CHECKBOX), SetMinimalSize(13, 1), SetDataTip(STR_EMPTY, STR_NULL),
    00823               NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCLWW_TYPE),
    00824                       SetDataTip(STR_CONTENT_TYPE_CAPTION, STR_CONTENT_TYPE_CAPTION_TOOLTIP),
    00825               NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCLWW_NAME), SetResize(1, 0), SetFill(1, 0),
    00826                       SetDataTip(STR_CONTENT_NAME_CAPTION, STR_CONTENT_NAME_CAPTION_TOOLTIP),
    00827             EndContainer(),
    00828             NWidget(WWT_MATRIX, COLOUR_LIGHT_BLUE, NCLWW_MATRIX), SetResize(1, 14), SetFill(1, 1),
    00829           EndContainer(),
    00830           NWidget(WWT_SCROLLBAR, COLOUR_LIGHT_BLUE, NCLWW_SCROLLBAR),
    00831         EndContainer(),
    00832       EndContainer(),
    00833       /* Right side. */
    00834       NWidget(NWID_VERTICAL),
    00835         NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, NCLWW_DETAILS), SetResize(1, 1), SetFill(1, 1), EndContainer(),
    00836       EndContainer(),
    00837     EndContainer(),
    00838     NWidget(NWID_SPACER), SetMinimalSize(0, 7), SetResize(1, 0),
    00839     /* Bottom. */
    00840     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(8, 8, 8),
    00841       NWidget(NWID_SELECTION, INVALID_COLOUR, NCLWW_SEL_ALL_UPDATE), SetResize(1, 0), SetFill(1, 0),
    00842         NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCLWW_SELECT_UPDATE), SetResize(1, 0), SetFill(1, 0),
    00843                     SetDataTip(STR_CONTENT_SELECT_UPDATES_CAPTION, STR_CONTENT_SELECT_UPDATES_CAPTION_TOOLTIP),
    00844         NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCLWW_SELECT_ALL), SetResize(1, 0), SetFill(1, 0),
    00845                     SetDataTip(STR_CONTENT_SELECT_ALL_CAPTION, STR_CONTENT_SELECT_ALL_CAPTION_TOOLTIP),
    00846       EndContainer(),
    00847       NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCLWW_UNSELECT), SetResize(1, 0), SetFill(1, 0),
    00848                     SetDataTip(STR_CONTENT_UNSELECT_ALL_CAPTION, STR_CONTENT_UNSELECT_ALL_CAPTION_TOOLTIP),
    00849       NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCLWW_CANCEL), SetResize(1, 0), SetFill(1, 0),
    00850                     SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
    00851       NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCLWW_DOWNLOAD), SetResize(1, 0), SetFill(1, 0),
    00852                     SetDataTip(STR_CONTENT_DOWNLOAD_CAPTION, STR_CONTENT_DOWNLOAD_CAPTION_TOOLTIP),
    00853     EndContainer(),
    00854     NWidget(NWID_SPACER), SetMinimalSize(0, 2), SetResize(1, 0),
    00855     /* Resize button. */
    00856     NWidget(NWID_HORIZONTAL),
    00857       NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
    00858       NWidget(WWT_RESIZEBOX, COLOUR_LIGHT_BLUE),
    00859     EndContainer(),
    00860   EndContainer(),
    00861 };
    00862
    00864 static const WindowDesc _network_content_list_desc(
    00865   WDP_CENTER, 630, 460,
    00866   WC_NETWORK_WINDOW, WC_NONE,
    00867   WDF_UNCLICK_BUTTONS,
    00868   _nested_network_content_list_widgets, lengthof(_nested_network_content_list_widgets)
    00869 );
    00870
    00876 void ShowNetworkContentListWindow(ContentVector *cv, ContentType type)
    00877 {
    00878 #if defined(WITH_ZLIB)
    00879   _network_content_client.Clear();
    00880   if (cv == NULL) {
    00881     _network_content_client.RequestContentList(type);
    00882   } else {
    00883     _network_content_client.RequestContentList(cv, true);
    00884   }
    00885
    00886   DeleteWindowById(WC_NETWORK_WINDOW, 1);
    00887   new NetworkContentListWindow(&_network_content_list_desc, cv != NULL);
    00888 #else
    00889   ShowErrorMessage(STR_CONTENT_NO_ZLIB, STR_CONTENT_NO_ZLIB_SUB, 0, 0);
    00890   /* Connection failed... clean up the mess */
    00891   if (cv != NULL) {
    00892     for (ContentIterator iter = cv->Begin(); iter != cv->End(); iter++) delete *iter;
    00893   }
    00894 #endif /* WITH_ZLIB */
    00895 }
    00896
    00897 #endif /* ENABLE_NETWORK */