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 "../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_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) _network_content_client.Close();
00201       delete this;
00202     }
00203   }
00204 
00205   virtual void OnDownloadProgress(const ContentInfo *ci, int bytes)
00206   {
00207     BaseNetworkContentDownloadStatusWindow::OnDownloadProgress(ci, bytes);
00208     this->receivedTypes.Include(ci->type);
00209 
00210     /* When downloading is finished change cancel in ok */
00211     if (this->downloaded_bytes == this->total_bytes) {
00212       this->GetWidget<NWidgetCore>(NCDSWW_CANCELOK)->widget_data = STR_BUTTON_OK;
00213     }
00214   }
00215 };
00216 
00218 enum NetworkContentListWindowWidgets {
00219   NCLWW_BACKGROUND,    
00220 
00221   NCLWW_FILTER_CAPT,   
00222   NCLWW_FILTER,        
00223 
00224   NCLWW_CHECKBOX,      
00225   NCLWW_TYPE,          
00226   NCLWW_NAME,          
00227 
00228   NCLWW_MATRIX,        
00229   NCLWW_SCROLLBAR,     
00230 
00231   NCLWW_DETAILS,       
00232 
00233   NCLWW_SELECT_ALL,    
00234   NCLWW_SELECT_UPDATE, 
00235   NCLWW_UNSELECT,      
00236   NCLWW_CANCEL,        
00237   NCLWW_DOWNLOAD,      
00238 
00239   NCLWW_SEL_ALL_UPDATE, 
00240 };
00241 
00243 class NetworkContentListWindow : public QueryStringBaseWindow, ContentCallback {
00245   typedef GUIList<const ContentInfo*> GUIContentList;
00246 
00247   static const uint EDITBOX_MAX_SIZE   =  50; 
00248   static const uint EDITBOX_MAX_LENGTH = 300; 
00249 
00250   static Listing last_sorting;     
00251   static Filtering last_filtering; 
00252   static GUIContentList::SortFunction * const sorter_funcs[];   
00253   static GUIContentList::FilterFunction * const filter_funcs[]; 
00254   GUIContentList content;      
00255 
00256   const ContentInfo *selected; 
00257   int list_pos;                
00258   uint filesize_sum;           
00259   Scrollbar *vscroll;          
00260 
00265   void BuildContentList()
00266   {
00267     if (!this->content.NeedRebuild()) return;
00268 
00269     /* Create temporary array of games to use for listing */
00270     this->content.Clear();
00271 
00272     for (ConstContentIterator iter = _network_content_client.Begin(); iter != _network_content_client.End(); iter++) {
00273       *this->content.Append() = *iter;
00274     }
00275 
00276     this->FilterContentList();
00277     this->content.Compact();
00278     this->content.RebuildDone();
00279     this->SortContentList();
00280 
00281     this->vscroll->SetCount(this->content.Length()); // Update the scrollbar
00282     this->ScrollToSelected();
00283   }
00284 
00286   static int CDECL NameSorter(const ContentInfo * const *a, const ContentInfo * const *b)
00287   {
00288     return strnatcmp((*a)->name, (*b)->name); // Sort by name (natural sorting).
00289   }
00290 
00292   static int CDECL TypeSorter(const ContentInfo * const *a, const ContentInfo * const *b)
00293   {
00294     int r = 0;
00295     if ((*a)->type != (*b)->type) {
00296       char a_str[64];
00297       char b_str[64];
00298       GetString(a_str, STR_CONTENT_TYPE_BASE_GRAPHICS + (*a)->type - CONTENT_TYPE_BASE_GRAPHICS, lastof(a_str));
00299       GetString(b_str, STR_CONTENT_TYPE_BASE_GRAPHICS + (*b)->type - CONTENT_TYPE_BASE_GRAPHICS, lastof(b_str));
00300       r = strnatcmp(a_str, b_str);
00301     }
00302     if (r == 0) r = NameSorter(a, b);
00303     return r;
00304   }
00305 
00307   static int CDECL StateSorter(const ContentInfo * const *a, const ContentInfo * const *b)
00308   {
00309     int r = (*a)->state - (*b)->state;
00310     if (r == 0) r = TypeSorter(a, b);
00311     return r;
00312   }
00313 
00315   void SortContentList()
00316   {
00317     if (!this->content.Sort()) return;
00318 
00319     for (ConstContentIterator iter = this->content.Begin(); iter != this->content.End(); iter++) {
00320       if (*iter == this->selected) {
00321         this->list_pos = iter - this->content.Begin();
00322         break;
00323       }
00324     }
00325   }
00326 
00328   static bool CDECL TagNameFilter(const ContentInfo * const *a, const char *filter_string)
00329   {
00330     for (int i = 0; i < (*a)->tag_count; i++) {
00331       if (strcasestr((*a)->tags[i], filter_string) != NULL) return true;
00332     }
00333     return strcasestr((*a)->name, filter_string) != NULL;
00334   }
00335 
00337   void FilterContentList()
00338   {
00339     if (!this->content.Filter(this->edit_str_buf)) return;
00340 
00341     /* update list position */
00342     for (ConstContentIterator iter = this->content.Begin(); iter != this->content.End(); iter++) {
00343       if (*iter == this->selected) {
00344         this->list_pos = iter - this->content.Begin();
00345         return;
00346       }
00347     }
00348 
00349     /* previously selected item not in list anymore */
00350     this->selected = NULL;
00351     this->list_pos = 0;
00352   }
00353 
00355   void ScrollToSelected()
00356   {
00357     if (this->selected == NULL) return;
00358 
00359     this->vscroll->ScrollTowards(this->list_pos);
00360   }
00361 
00362 public:
00368   NetworkContentListWindow(const WindowDesc *desc, bool select_all) :
00369       QueryStringBaseWindow(EDITBOX_MAX_SIZE),
00370       selected(NULL),
00371       list_pos(0)
00372   {
00373     this->CreateNestedTree(desc);
00374     this->vscroll = this->GetScrollbar(NCLWW_SCROLLBAR);
00375     this->FinishInitNested(desc, 1);
00376 
00377     this->GetWidget<NWidgetStacked>(NCLWW_SEL_ALL_UPDATE)->SetDisplayedPlane(select_all);
00378 
00379     this->afilter = CS_ALPHANUMERAL;
00380     InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, EDITBOX_MAX_LENGTH);
00381     this->SetFocusedWidget(NCLWW_FILTER);
00382 
00383     _network_content_client.AddCallback(this);
00384     this->content.SetListing(this->last_sorting);
00385     this->content.SetFiltering(this->last_filtering);
00386     this->content.SetSortFuncs(this->sorter_funcs);
00387     this->content.SetFilterFuncs(this->filter_funcs);
00388     this->content.ForceRebuild();
00389     this->FilterContentList();
00390     this->SortContentList();
00391     this->InvalidateData();
00392   }
00393 
00395   ~NetworkContentListWindow()
00396   {
00397     _network_content_client.RemoveCallback(this);
00398   }
00399 
00400   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00401   {
00402     switch (widget) {
00403       case NCLWW_FILTER_CAPT:
00404         *size = maxdim(*size, GetStringBoundingBox(STR_CONTENT_FILTER_TITLE));
00405         break;
00406 
00407       case NCLWW_TYPE: {
00408         Dimension d = *size;
00409         for (int i = CONTENT_TYPE_BEGIN; i < CONTENT_TYPE_END; i++) {
00410           d = maxdim(d, GetStringBoundingBox(STR_CONTENT_TYPE_BASE_GRAPHICS + i - CONTENT_TYPE_BASE_GRAPHICS));
00411         }
00412         size->width = d.width + WD_MATRIX_RIGHT + WD_MATRIX_LEFT;
00413         break;
00414       }
00415 
00416       case NCLWW_MATRIX:
00417         resize->height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
00418         size->height = 10 * resize->height;
00419         break;
00420     }
00421   }
00422 
00423 
00424   virtual void DrawWidget(const Rect &r, int widget) const
00425   {
00426     switch (widget) {
00427       case NCLWW_FILTER_CAPT:
00428         DrawString(r.left, r.right, r.top, STR_CONTENT_FILTER_TITLE, TC_FROMSTRING, SA_RIGHT);
00429         break;
00430 
00431       case NCLWW_DETAILS:
00432         this->DrawDetails(r);
00433         break;
00434 
00435       case NCLWW_MATRIX:
00436         this->DrawMatrix(r);
00437         break;
00438     }
00439   }
00440 
00441   virtual void OnPaint()
00442   {
00443     const SortButtonState arrow = this->content.IsDescSortOrder() ? SBS_DOWN : SBS_UP;
00444 
00445     if (this->content.NeedRebuild()) {
00446       this->BuildContentList();
00447     }
00448 
00449     this->DrawWidgets();
00450 
00451     /* Edit box to filter for keywords */
00452     this->DrawEditBox(NCLWW_FILTER);
00453 
00454     switch (this->content.SortType()) {
00455       case NCLWW_CHECKBOX - NCLWW_CHECKBOX: this->DrawSortButtonState(NCLWW_CHECKBOX, arrow); break;
00456       case NCLWW_TYPE     - NCLWW_CHECKBOX: this->DrawSortButtonState(NCLWW_TYPE,     arrow); break;
00457       case NCLWW_NAME     - NCLWW_CHECKBOX: this->DrawSortButtonState(NCLWW_NAME,     arrow); break;
00458     }
00459   }
00460 
00465   void DrawMatrix(const Rect &r) const
00466   {
00467     const NWidgetBase *nwi_checkbox = this->GetWidget<NWidgetBase>(NCLWW_CHECKBOX);
00468     const NWidgetBase *nwi_name = this->GetWidget<NWidgetBase>(NCLWW_NAME);
00469     const NWidgetBase *nwi_type = this->GetWidget<NWidgetBase>(NCLWW_TYPE);
00470 
00471 
00472     /* Fill the matrix with the information */
00473     int sprite_y_offset = WD_MATRIX_TOP + (FONT_HEIGHT_NORMAL - 10) / 2;
00474     uint y = r.top;
00475     int cnt = 0;
00476     for (ConstContentIterator iter = this->content.Get(this->vscroll->GetPosition()); iter != this->content.End() && cnt < this->vscroll->GetCapacity(); iter++, cnt++) {
00477       const ContentInfo *ci = *iter;
00478 
00479       if (ci == this->selected) GfxFillRect(r.left + 1, y + 1, r.right - 1, y + this->resize.step_height - 1, PC_GREY);
00480 
00481       SpriteID sprite;
00482       SpriteID pal = PAL_NONE;
00483       switch (ci->state) {
00484         case ContentInfo::UNSELECTED:     sprite = SPR_BOX_EMPTY;   break;
00485         case ContentInfo::SELECTED:       sprite = SPR_BOX_CHECKED; break;
00486         case ContentInfo::AUTOSELECTED:   sprite = SPR_BOX_CHECKED; break;
00487         case ContentInfo::ALREADY_HERE:   sprite = SPR_BLOT; pal = PALETTE_TO_GREEN; break;
00488         case ContentInfo::DOES_NOT_EXIST: sprite = SPR_BLOT; pal = PALETTE_TO_RED;   break;
00489         default: NOT_REACHED();
00490       }
00491       DrawSprite(sprite, pal, nwi_checkbox->pos_x + (pal == PAL_NONE ? 2 : 3), y + sprite_y_offset + (pal == PAL_NONE ? 1 : 0));
00492 
00493       StringID str = STR_CONTENT_TYPE_BASE_GRAPHICS + ci->type - CONTENT_TYPE_BASE_GRAPHICS;
00494       DrawString(nwi_type->pos_x, nwi_type->pos_x + nwi_type->current_x - 1, y + WD_MATRIX_TOP, str, TC_BLACK, SA_HOR_CENTER);
00495 
00496       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);
00497       y += this->resize.step_height;
00498     }
00499   }
00500 
00505   void DrawDetails(const Rect &r) const
00506   {
00507     static const int DETAIL_LEFT         =  5; 
00508     static const int DETAIL_RIGHT        =  5; 
00509     static const int DETAIL_TOP          =  5; 
00510 
00511     /* Height for the title banner */
00512     int DETAIL_TITLE_HEIGHT = 5 * FONT_HEIGHT_NORMAL;
00513 
00514     /* Create the nice grayish rectangle at the details top */
00515     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.top + DETAIL_TITLE_HEIGHT, PC_DARK_BLUE);
00516     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);
00517 
00518     /* Draw the total download size */
00519     SetDParam(0, this->filesize_sum);
00520     DrawString(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, r.bottom - FONT_HEIGHT_NORMAL - WD_PAR_VSEP_NORMAL, STR_CONTENT_TOTAL_DOWNLOAD_SIZE);
00521 
00522     if (this->selected == NULL) return;
00523 
00524     /* And fill the rest of the details when there's information to place there */
00525     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);
00526 
00527     /* Also show the total download size, so keep some space from the bottom */
00528     const uint max_y = r.bottom - FONT_HEIGHT_NORMAL - WD_PAR_VSEP_WIDE;
00529     int y = r.top + DETAIL_TITLE_HEIGHT + DETAIL_TOP;
00530 
00531     if (this->selected->upgrade) {
00532       SetDParam(0, STR_CONTENT_TYPE_BASE_GRAPHICS + this->selected->type - CONTENT_TYPE_BASE_GRAPHICS);
00533       y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_UPDATE);
00534       y += WD_PAR_VSEP_WIDE;
00535     }
00536 
00537     SetDParamStr(0, this->selected->name);
00538     y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_NAME);
00539 
00540     if (!StrEmpty(this->selected->version)) {
00541       SetDParamStr(0, this->selected->version);
00542       y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_VERSION);
00543     }
00544 
00545     if (!StrEmpty(this->selected->description)) {
00546       SetDParamStr(0, this->selected->description);
00547       y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_DESCRIPTION);
00548     }
00549 
00550     if (!StrEmpty(this->selected->url)) {
00551       SetDParamStr(0, this->selected->url);
00552       y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_URL);
00553     }
00554 
00555     SetDParam(0, STR_CONTENT_TYPE_BASE_GRAPHICS + this->selected->type - CONTENT_TYPE_BASE_GRAPHICS);
00556     y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_TYPE);
00557 
00558     y += WD_PAR_VSEP_WIDE;
00559     SetDParam(0, this->selected->filesize);
00560     y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_FILESIZE);
00561 
00562     if (this->selected->dependency_count != 0) {
00563       /* List dependencies */
00564       char buf[DRAW_STRING_BUFFER] = "";
00565       char *p = buf;
00566       for (uint i = 0; i < this->selected->dependency_count; i++) {
00567         ContentID cid = this->selected->dependencies[i];
00568 
00569         /* Try to find the dependency */
00570         ConstContentIterator iter = _network_content_client.Begin();
00571         for (; iter != _network_content_client.End(); iter++) {
00572           const ContentInfo *ci = *iter;
00573           if (ci->id != cid) continue;
00574 
00575           p += seprintf(p, lastof(buf), p == buf ? "%s" : ", %s", (*iter)->name);
00576           break;
00577         }
00578       }
00579       SetDParamStr(0, buf);
00580       y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_DEPENDENCIES);
00581     }
00582 
00583     if (this->selected->tag_count != 0) {
00584       /* List all tags */
00585       char buf[DRAW_STRING_BUFFER] = "";
00586       char *p = buf;
00587       for (uint i = 0; i < this->selected->tag_count; i++) {
00588         p += seprintf(p, lastof(buf), i == 0 ? "%s" : ", %s", this->selected->tags[i]);
00589       }
00590       SetDParamStr(0, buf);
00591       y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_TAGS);
00592     }
00593 
00594     if (this->selected->IsSelected()) {
00595       /* When selected show all manually selected content that depends on this */
00596       ConstContentVector tree;
00597       _network_content_client.ReverseLookupTreeDependency(tree, this->selected);
00598 
00599       char buf[DRAW_STRING_BUFFER] = "";
00600       char *p = buf;
00601       for (ConstContentIterator iter = tree.Begin(); iter != tree.End(); iter++) {
00602         const ContentInfo *ci = *iter;
00603         if (ci == this->selected || ci->state != ContentInfo::SELECTED) continue;
00604 
00605         p += seprintf(p, lastof(buf), buf == p ? "%s" : ", %s", ci->name);
00606       }
00607       if (p != buf) {
00608         SetDParamStr(0, buf);
00609         y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_SELECTED_BECAUSE_OF);
00610       }
00611     }
00612   }
00613 
00614   virtual void OnClick(Point pt, int widget, int click_count)
00615   {
00616     switch (widget) {
00617       case NCLWW_MATRIX: {
00618         uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, NCLWW_MATRIX);
00619         if (id_v >= this->content.Length()) return; // click out of bounds
00620 
00621         this->selected = *this->content.Get(id_v);
00622         this->list_pos = id_v;
00623 
00624         const NWidgetBase *checkbox = this->GetWidget<NWidgetBase>(NCLWW_CHECKBOX);
00625         if (click_count > 1 || IsInsideBS(pt.x, checkbox->pos_x, checkbox->current_x)) {
00626           _network_content_client.ToggleSelectedState(this->selected);
00627           this->content.ForceResort();
00628         }
00629 
00630         this->InvalidateData();
00631         break;
00632       }
00633 
00634       case NCLWW_CHECKBOX:
00635       case NCLWW_TYPE:
00636       case NCLWW_NAME:
00637         if (this->content.SortType() == widget - NCLWW_CHECKBOX) {
00638           this->content.ToggleSortOrder();
00639           this->list_pos = this->content.Length() - this->list_pos - 1;
00640         } else {
00641           this->content.SetSortType(widget - NCLWW_CHECKBOX);
00642           this->content.ForceResort();
00643           this->SortContentList();
00644         }
00645         this->ScrollToSelected();
00646         this->InvalidateData();
00647         break;
00648 
00649       case NCLWW_SELECT_ALL:
00650         _network_content_client.SelectAll();
00651         this->InvalidateData();
00652         break;
00653 
00654       case NCLWW_SELECT_UPDATE:
00655         _network_content_client.SelectUpgrade();
00656         this->InvalidateData();
00657         break;
00658 
00659       case NCLWW_UNSELECT:
00660         _network_content_client.UnselectAll();
00661         this->InvalidateData();
00662         break;
00663 
00664       case NCLWW_CANCEL:
00665         delete this;
00666         break;
00667 
00668       case NCLWW_DOWNLOAD:
00669         if (BringWindowToFrontById(WC_NETWORK_STATUS_WINDOW, 0) == NULL) new NetworkContentDownloadStatusWindow();
00670         break;
00671     }
00672   }
00673 
00674   virtual void OnMouseLoop()
00675   {
00676     this->HandleEditBox(NCLWW_FILTER);
00677   }
00678 
00679   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
00680   {
00681     switch (keycode) {
00682       case WKC_UP:
00683         /* scroll up by one */
00684         if (this->list_pos > 0) this->list_pos--;
00685         break;
00686       case WKC_DOWN:
00687         /* scroll down by one */
00688         if (this->list_pos < (int)this->content.Length() - 1) this->list_pos++;
00689         break;
00690       case WKC_PAGEUP:
00691         /* scroll up a page */
00692         this->list_pos = (this->list_pos < this->vscroll->GetCapacity()) ? 0 : this->list_pos - this->vscroll->GetCapacity();
00693         break;
00694       case WKC_PAGEDOWN:
00695         /* scroll down a page */
00696         this->list_pos = min(this->list_pos + this->vscroll->GetCapacity(), (int)this->content.Length() - 1);
00697         break;
00698       case WKC_HOME:
00699         /* jump to beginning */
00700         this->list_pos = 0;
00701         break;
00702       case WKC_END:
00703         /* jump to end */
00704         this->list_pos = this->content.Length() - 1;
00705         break;
00706 
00707       case WKC_SPACE:
00708       case WKC_RETURN:
00709         if (keycode == WKC_RETURN || !IsWidgetFocused(NCLWW_FILTER)) {
00710           if (this->selected != NULL) {
00711             _network_content_client.ToggleSelectedState(this->selected);
00712             this->content.ForceResort();
00713             this->InvalidateData();
00714           }
00715           return ES_HANDLED;
00716         }
00717         /* FALL THROUGH, space is pressed and filter isn't focused. */
00718 
00719       default: {
00720         /* Handle editbox input */
00721         EventState state = ES_NOT_HANDLED;
00722         if (this->HandleEditBoxKey(NCLWW_FILTER, key, keycode, state) == HEBR_EDITING) {
00723           this->OnOSKInput(NCLWW_FILTER);
00724         }
00725 
00726         return state;
00727       }
00728     }
00729 
00730     if (_network_content_client.Length() == 0) return ES_HANDLED;
00731 
00732     this->selected = *this->content.Get(this->list_pos);
00733 
00734     /* scroll to the new server if it is outside the current range */
00735     this->ScrollToSelected();
00736 
00737     /* redraw window */
00738     this->InvalidateData();
00739     return ES_HANDLED;
00740   }
00741 
00742   virtual void OnOSKInput(int wid)
00743   {
00744     this->content.SetFilterState(!StrEmpty(this->edit_str_buf));
00745     this->content.ForceRebuild();
00746     this->InvalidateData();
00747   }
00748 
00749   virtual void OnResize()
00750   {
00751     this->vscroll->SetCapacityFromWidget(this, NCLWW_MATRIX);
00752     this->GetWidget<NWidgetCore>(NCLWW_MATRIX)->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00753   }
00754 
00755   virtual void OnReceiveContentInfo(const ContentInfo *rci)
00756   {
00757     this->content.ForceRebuild();
00758     this->InvalidateData();
00759   }
00760 
00761   virtual void OnDownloadComplete(ContentID cid)
00762   {
00763     this->content.ForceResort();
00764     this->InvalidateData();
00765   }
00766 
00767   virtual void OnConnect(bool success)
00768   {
00769     if (!success) {
00770       ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_CONNECT, INVALID_STRING_ID, WL_ERROR);
00771       delete this;
00772       return;
00773     }
00774 
00775     this->InvalidateData();
00776   }
00777 
00783   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00784   {
00785     if (!gui_scope) return;
00786     if (this->content.NeedRebuild()) this->BuildContentList();
00787 
00788     /* To sum all the bytes we intend to download */
00789     this->filesize_sum = 0;
00790     bool show_select_all = false;
00791     bool show_select_upgrade = false;
00792     for (ConstContentIterator iter = this->content.Begin(); iter != this->content.End(); iter++) {
00793       const ContentInfo *ci = *iter;
00794       switch (ci->state) {
00795         case ContentInfo::SELECTED:
00796         case ContentInfo::AUTOSELECTED:
00797           this->filesize_sum += ci->filesize;
00798           break;
00799 
00800         case ContentInfo::UNSELECTED:
00801           show_select_all = true;
00802           show_select_upgrade |= ci->upgrade;
00803           break;
00804 
00805         default:
00806           break;
00807       }
00808     }
00809 
00810     /* If data == 2 then the status window caused this OnInvalidate */
00811     this->SetWidgetDisabledState(NCLWW_DOWNLOAD, this->filesize_sum == 0 || (FindWindowById(WC_NETWORK_STATUS_WINDOW, 0) != NULL && data != 2));
00812     this->SetWidgetDisabledState(NCLWW_UNSELECT, this->filesize_sum == 0);
00813     this->SetWidgetDisabledState(NCLWW_SELECT_ALL, !show_select_all);
00814     this->SetWidgetDisabledState(NCLWW_SELECT_UPDATE, !show_select_upgrade);
00815 
00816     this->GetWidget<NWidgetCore>(NCLWW_CANCEL)->widget_data = this->filesize_sum == 0 ? STR_AI_SETTINGS_CLOSE : STR_AI_LIST_CANCEL;
00817   }
00818 };
00819 
00820 Listing NetworkContentListWindow::last_sorting = {false, 1};
00821 Filtering NetworkContentListWindow::last_filtering = {false, 0};
00822 
00823 NetworkContentListWindow::GUIContentList::SortFunction * const NetworkContentListWindow::sorter_funcs[] = {
00824   &StateSorter,
00825   &TypeSorter,
00826   &NameSorter,
00827 };
00828 
00829 NetworkContentListWindow::GUIContentList::FilterFunction * const NetworkContentListWindow::filter_funcs[] = {
00830   &TagNameFilter,
00831 };
00832 
00834 static const NWidgetPart _nested_network_content_list_widgets[] = {
00835   NWidget(NWID_HORIZONTAL),
00836     NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
00837     NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_CONTENT_TITLE, STR_NULL),
00838   EndContainer(),
00839   NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, NCLWW_BACKGROUND),
00840     NWidget(NWID_SPACER), SetMinimalSize(0, 7), SetResize(1, 0),
00841     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(8, 8, 8),
00842       /* Top */
00843       NWidget(WWT_EMPTY, COLOUR_LIGHT_BLUE, NCLWW_FILTER_CAPT), SetFill(1, 0), SetResize(1, 0),
00844       NWidget(WWT_EDITBOX, COLOUR_LIGHT_BLUE, NCLWW_FILTER), SetFill(1, 0), SetResize(1, 0),
00845             SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
00846     EndContainer(),
00847     NWidget(NWID_SPACER), SetMinimalSize(0, 7), SetResize(1, 0),
00848     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(8, 8, 8),
00849       /* Left side. */
00850       NWidget(NWID_VERTICAL),
00851         NWidget(NWID_HORIZONTAL),
00852           NWidget(NWID_VERTICAL),
00853             NWidget(NWID_HORIZONTAL),
00854               NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCLWW_CHECKBOX), SetMinimalSize(13, 1), SetDataTip(STR_EMPTY, STR_NULL),
00855               NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCLWW_TYPE),
00856                       SetDataTip(STR_CONTENT_TYPE_CAPTION, STR_CONTENT_TYPE_CAPTION_TOOLTIP),
00857               NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCLWW_NAME), SetResize(1, 0), SetFill(1, 0),
00858                       SetDataTip(STR_CONTENT_NAME_CAPTION, STR_CONTENT_NAME_CAPTION_TOOLTIP),
00859             EndContainer(),
00860             NWidget(WWT_MATRIX, COLOUR_LIGHT_BLUE, NCLWW_MATRIX), SetResize(1, 14), SetFill(1, 1), SetScrollbar(NCLWW_SCROLLBAR), SetDataTip(STR_NULL, STR_CONTENT_MATRIX_TOOLTIP),
00861           EndContainer(),
00862           NWidget(NWID_VSCROLLBAR, COLOUR_LIGHT_BLUE, NCLWW_SCROLLBAR),
00863         EndContainer(),
00864       EndContainer(),
00865       /* Right side. */
00866       NWidget(NWID_VERTICAL),
00867         NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, NCLWW_DETAILS), SetResize(1, 1), SetFill(1, 1), EndContainer(),
00868       EndContainer(),
00869     EndContainer(),
00870     NWidget(NWID_SPACER), SetMinimalSize(0, 7), SetResize(1, 0),
00871     /* Bottom. */
00872     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(8, 8, 8),
00873       NWidget(NWID_SELECTION, INVALID_COLOUR, NCLWW_SEL_ALL_UPDATE), SetResize(1, 0), SetFill(1, 0),
00874         NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCLWW_SELECT_UPDATE), SetResize(1, 0), SetFill(1, 0),
00875                     SetDataTip(STR_CONTENT_SELECT_UPDATES_CAPTION, STR_CONTENT_SELECT_UPDATES_CAPTION_TOOLTIP),
00876         NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCLWW_SELECT_ALL), SetResize(1, 0), SetFill(1, 0),
00877                     SetDataTip(STR_CONTENT_SELECT_ALL_CAPTION, STR_CONTENT_SELECT_ALL_CAPTION_TOOLTIP),
00878       EndContainer(),
00879       NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCLWW_UNSELECT), SetResize(1, 0), SetFill(1, 0),
00880                     SetDataTip(STR_CONTENT_UNSELECT_ALL_CAPTION, STR_CONTENT_UNSELECT_ALL_CAPTION_TOOLTIP),
00881       NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCLWW_CANCEL), SetResize(1, 0), SetFill(1, 0),
00882                     SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
00883       NWidget(WWT_PUSHTXTBTN, COLOUR_WHITE, NCLWW_DOWNLOAD), SetResize(1, 0), SetFill(1, 0),
00884                     SetDataTip(STR_CONTENT_DOWNLOAD_CAPTION, STR_CONTENT_DOWNLOAD_CAPTION_TOOLTIP),
00885     EndContainer(),
00886     NWidget(NWID_SPACER), SetMinimalSize(0, 2), SetResize(1, 0),
00887     /* Resize button. */
00888     NWidget(NWID_HORIZONTAL),
00889       NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
00890       NWidget(WWT_RESIZEBOX, COLOUR_LIGHT_BLUE),
00891     EndContainer(),
00892   EndContainer(),
00893 };
00894 
00896 static const WindowDesc _network_content_list_desc(
00897   WDP_CENTER, 630, 460,
00898   WC_NETWORK_WINDOW, WC_NONE,
00899   WDF_UNCLICK_BUTTONS,
00900   _nested_network_content_list_widgets, lengthof(_nested_network_content_list_widgets)
00901 );
00902 
00908 void ShowNetworkContentListWindow(ContentVector *cv, ContentType type)
00909 {
00910 #if defined(WITH_ZLIB)
00911   _network_content_client.Clear();
00912   if (cv == NULL) {
00913     _network_content_client.RequestContentList(type);
00914   } else {
00915     _network_content_client.RequestContentList(cv, true);
00916   }
00917 
00918   DeleteWindowById(WC_NETWORK_WINDOW, 1);
00919   new NetworkContentListWindow(&_network_content_list_desc, cv != NULL);
00920 #else
00921   ShowErrorMessage(STR_CONTENT_NO_ZLIB, STR_CONTENT_NO_ZLIB_SUB, WL_ERROR);
00922   /* Connection failed... clean up the mess */
00923   if (cv != NULL) {
00924     for (ContentIterator iter = cv->Begin(); iter != cv->End(); iter++) delete *iter;
00925   }
00926 #endif /* WITH_ZLIB */
00927 }
00928 
00929 #endif /* ENABLE_NETWORK */