network_content_gui.cpp

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