network_content_gui.cpp

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