# linkgraph_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: linkgraph_gui.cpp 25912 2013-10-23 19:42:17Z fonsinchen $ */
    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 #include "../stdafx.h"
    00013 #include "../window_gui.h"
    00014 #include "../window_func.h"
    00015 #include "../company_base.h"
    00016 #include "../company_gui.h"
    00017 #include "../date_func.h"
    00018 #include "../viewport_func.h"
    00019 #include "../smallmap_gui.h"
    00020 #include "../core/geometry_func.hpp"
    00021 #include "../widgets/link_graph_legend_widget.h"
    00022
    00023 #include "table/strings.h"
    00024
    00029 const uint8 LinkGraphOverlay::LINK_COLOURS[] = {
    00030   0x0f, 0xd1, 0xd0, 0x57,
    00031   0x55, 0x53, 0xbf, 0xbd,
    00032   0xba, 0xb9, 0xb7, 0xb5
    00033 };
    00034
    00039 void LinkGraphOverlay::GetWidgetDpi(DrawPixelInfo *dpi) const
    00040 {
    00041   const NWidgetBase *wi = this->window->GetWidget<NWidgetBase>(this->widget_id);
    00042   dpi->left = dpi->top = 0;
    00043   dpi->width = wi->current_x;
    00044   dpi->height = wi->current_y;
    00045 }
    00046
    00050 void LinkGraphOverlay::RebuildCache()
    00051 {
    00052   this->cached_links.clear();
    00053   this->cached_stations.clear();
    00054   if (this->company_mask == 0) return;
    00055
    00056   DrawPixelInfo dpi;
    00057   this->GetWidgetDpi(&dpi);
    00058
    00059   const Station *sta;
    00060   FOR_ALL_STATIONS(sta) {
    00061     if (sta->rect.IsEmpty()) continue;
    00062
    00063     Point pta = this->GetStationMiddle(sta);
    00064
    00065     StationID from = sta->index;
    00066     StationLinkMap &seen_links = this->cached_links[from];
    00067
    00068     uint supply = 0;
    00069     CargoID c;
    00070     FOR_EACH_SET_CARGO_ID(c, this->cargo_mask) {
    00071       if (!CargoSpec::Get(c)->IsValid()) continue;
    00072       if (!LinkGraph::IsValidID(sta->goods[c].link_graph)) continue;
    00073       const LinkGraph &lg = *LinkGraph::Get(sta->goods[c].link_graph);
    00074
    00075       ConstNode from_node = lg[sta->goods[c].node];
    00076       supply += lg.Monthly(from_node.Supply());
    00077       for (ConstEdgeIterator i = from_node.Begin(); i != from_node.End(); ++i) {
    00078         StationID to = lg[i->first].Station();
    00079         assert(from != to);
    00080         if (!Station::IsValidID(to) || seen_links.find(to) != seen_links.end()) {
    00081           continue;
    00082         }
    00083         const Station *stb = Station::Get(to);
    00084         assert(sta != stb);
    00085
    00086         /* Show links between stations of selected companies or "neutral" ones like oilrigs. */
    00087         if (stb->owner != OWNER_NONE && sta->owner != OWNER_NONE && !HasBit(this->company_mask, stb->owner)) continue;
    00088         if (stb->rect.IsEmpty()) continue;
    00089
    00090         if (!this->IsLinkVisible(pta, this->GetStationMiddle(stb), &dpi)) continue;
    00091
    00092         this->AddLinks(sta, stb);
    00093         seen_links[to]; // make sure it is created and marked as seen
    00094       }
    00095     }
    00096     if (this->IsPointVisible(pta, &dpi)) {
    00097       this->cached_stations.push_back(std::make_pair(from, supply));
    00098     }
    00099   }
    00100 }
    00101
    00109 inline bool LinkGraphOverlay::IsPointVisible(Point pt, const DrawPixelInfo *dpi, int padding) const
    00110 {
    00111   return pt.x > dpi->left - padding && pt.y > dpi->top - padding &&
    00112       pt.x < dpi->left + dpi->width + padding &&
    00113       pt.y < dpi->top + dpi->height + padding;
    00114 }
    00115
    00124 inline bool LinkGraphOverlay::IsLinkVisible(Point pta, Point ptb, const DrawPixelInfo *dpi, int padding) const
    00125 {
    00126   return !((pta.x < dpi->left - padding && ptb.x < dpi->left - padding) ||
    00127       (pta.y < dpi->top - padding && ptb.y < dpi->top - padding) ||
    00128       (pta.x > dpi->left + dpi->width + padding &&
    00129           ptb.x > dpi->left + dpi->width + padding) ||
    00130       (pta.y > dpi->top + dpi->height + padding &&
    00131           ptb.y > dpi->top + dpi->height + padding));
    00132 }
    00133
    00139 void LinkGraphOverlay::AddLinks(const Station *from, const Station *to)
    00140 {
    00141   CargoID c;
    00142   FOR_EACH_SET_CARGO_ID(c, this->cargo_mask) {
    00143     if (!CargoSpec::Get(c)->IsValid()) continue;
    00144     const GoodsEntry &ge = from->goods[c];
    00145     if (!LinkGraph::IsValidID(ge.link_graph) ||
    00146         ge.link_graph != to->goods[c].link_graph) {
    00147       continue;
    00148     }
    00149     const LinkGraph &lg = *LinkGraph::Get(ge.link_graph);
    00150     ConstEdge edge = lg[ge.node][to->goods[c].node];
    00151     if (edge.Capacity() > 0) {
    00152       this->AddStats(lg.Monthly(edge.Capacity()), lg.Monthly(edge.Usage()),
    00153           ge.GetSumFlowVia(to->index), from->owner == OWNER_NONE || to->owner == OWNER_NONE,
    00154           this->cached_links[from->index][to->index]);
    00155     }
    00156   }
    00157 }
    00158
    00169 /* static */ void LinkGraphOverlay::AddStats(uint new_cap, uint new_usg, uint new_plan, bool new_shared, LinkProperties &cargo)
    00170 {
    00171   /* multiply the numbers by 32 in order to avoid comparing to 0 too often. */
    00172   if (cargo.capacity == 0 ||
    00173       max(cargo.usage, cargo.planned) * 32 / (cargo.capacity + 1) < max(new_usg, new_plan) * 32 / (new_cap + 1)) {
    00174     cargo.capacity = new_cap;
    00175     cargo.usage = new_usg;
    00176     cargo.planned = new_plan;
    00177   }
    00178   if (new_shared) cargo.shared = true;
    00179 }
    00180
    00185 void LinkGraphOverlay::Draw(const DrawPixelInfo *dpi) const
    00186 {
    00187   this->DrawLinks(dpi);
    00188   this->DrawStationDots(dpi);
    00189 }
    00190
    00195 void LinkGraphOverlay::DrawLinks(const DrawPixelInfo *dpi) const
    00196 {
    00197   for (LinkMap::const_iterator i(this->cached_links.begin()); i != this->cached_links.end(); ++i) {
    00198     if (!Station::IsValidID(i->first)) continue;
    00199     Point pta = this->GetStationMiddle(Station::Get(i->first));
    00200     for (StationLinkMap::const_iterator j(i->second.begin()); j != i->second.end(); ++j) {
    00201       if (!Station::IsValidID(j->first)) continue;
    00202       Point ptb = this->GetStationMiddle(Station::Get(j->first));
    00203       if (!this->IsLinkVisible(pta, ptb, dpi, this->scale + 2)) continue;
    00204       this->DrawContent(pta, ptb, j->second);
    00205     }
    00206   }
    00207 }
    00208
    00215 void LinkGraphOverlay::DrawContent(Point pta, Point ptb, const LinkProperties &cargo) const
    00216 {
    00217   uint usage_or_plan = min(cargo.capacity * 2 + 1, max(cargo.usage, cargo.planned));
    00218   int colour = LinkGraphOverlay::LINK_COLOURS[usage_or_plan * lengthof(LinkGraphOverlay::LINK_COLOURS) / (cargo.capacity * 2 + 2)];
    00219   int dash = cargo.shared ? this->scale * 4 : 0;
    00220
    00221   /* Move line a bit 90° against its dominant direction to prevent it from
    00222    * being hidden below the grey line. */
    00223   if (abs(pta.x - ptb.x) < abs(pta.y - ptb.y)) {
    00224     int offset_x = (pta.y > ptb.y ? 1 : -1) * this->scale;
    00225     GfxDrawLine(pta.x + offset_x, pta.y, ptb.x + offset_x, ptb.y, colour, this->scale, dash);
    00226   } else {
    00227     int offset_y = (pta.x < ptb.x ? 1 : -1) * this->scale;
    00228     GfxDrawLine(pta.x, pta.y + offset_y, ptb.x, ptb.y + offset_y, colour, this->scale, dash);
    00229   }
    00230
    00231   GfxDrawLine(pta.x, pta.y, ptb.x, ptb.y, _colour_gradient[COLOUR_GREY][1], this->scale);
    00232 }
    00233
    00238 void LinkGraphOverlay::DrawStationDots(const DrawPixelInfo *dpi) const
    00239 {
    00240   for (StationSupplyList::const_iterator i(this->cached_stations.begin()); i != this->cached_stations.end(); ++i) {
    00241     const Station *st = Station::GetIfValid(i->first);
    00242     if (st == NULL) continue;
    00243     Point pt = this->GetStationMiddle(st);
    00244     if (!this->IsPointVisible(pt, dpi, 3 * this->scale)) continue;
    00245
    00246     uint r = this->scale * 2 + this->scale * 2 * min(200, i->second) / 200;
    00247
    00248     LinkGraphOverlay::DrawVertex(pt.x, pt.y, r,
    00249         _colour_gradient[st->owner != OWNER_NONE ?
    00250             (Colours)Company::Get(st->owner)->colour : COLOUR_GREY][5],
    00251         _colour_gradient[COLOUR_GREY][1]);
    00252   }
    00253 }
    00254
    00263 /* static */ void LinkGraphOverlay::DrawVertex(int x, int y, int size, int colour, int border_colour)
    00264 {
    00265   size--;
    00266   int w1 = size / 2;
    00267   int w2 = size / 2 + size % 2;
    00268
    00269   GfxFillRect(x - w1, y - w1, x + w2, y + w2, colour);
    00270
    00271   w1++;
    00272   w2++;
    00273   GfxDrawLine(x - w1, y - w1, x + w2, y - w1, border_colour);
    00274   GfxDrawLine(x - w1, y + w2, x + w2, y + w2, border_colour);
    00275   GfxDrawLine(x - w1, y - w1, x - w1, y + w2, border_colour);
    00276   GfxDrawLine(x + w2, y - w1, x + w2, y + w2, border_colour);
    00277 }
    00278
    00284 Point LinkGraphOverlay::GetStationMiddle(const Station *st) const
    00285 {
    00286   if (this->window->viewport != NULL) {
    00287     return GetViewportStationMiddle(this->window->viewport, st);
    00288   } else {
    00289     /* assume this is a smallmap */
    00290     return static_cast<const SmallMapWindow *>(this->window)->GetStationMiddle(st);
    00291   }
    00292 }
    00293
    00298 void LinkGraphOverlay::SetCargoMask(uint32 cargo_mask)
    00299 {
    00300   this->cargo_mask = cargo_mask;
    00301   this->RebuildCache();
    00302   this->window->GetWidget<NWidgetBase>(this->widget_id)->SetDirty(this->window);
    00303 }
    00304
    00309 void LinkGraphOverlay::SetCompanyMask(uint32 company_mask)
    00310 {
    00311   this->company_mask = company_mask;
    00312   this->RebuildCache();
    00313   this->window->GetWidget<NWidgetBase>(this->widget_id)->SetDirty(this->window);
    00314 }
    00315
    00317 NWidgetBase *MakeCompanyButtonRowsLinkGraphGUI(int *biggest_index)
    00318 {
    00319   return MakeCompanyButtonRows(biggest_index, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST, 3, STR_LINKGRAPH_LEGEND_SELECT_COMPANIES);
    00320 }
    00321
    00322 NWidgetBase *MakeSaturationLegendLinkGraphGUI(int *biggest_index)
    00323 {
    00324   NWidgetVertical *panel = new NWidgetVertical(NC_EQUALSIZE);
    00325   for (uint i = 0; i < lengthof(LinkGraphOverlay::LINK_COLOURS); ++i) {
    00326     NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_DARK_GREEN, i + WID_LGL_SATURATION_FIRST);
    00327     wid->SetMinimalSize(50, FONT_HEIGHT_SMALL);
    00328     wid->SetFill(1, 1);
    00329     wid->SetResize(0, 0);
    00330     panel->Add(wid);
    00331   }
    00332   *biggest_index = WID_LGL_SATURATION_LAST;
    00333   return panel;
    00334 }
    00335
    00336 NWidgetBase *MakeCargoesLegendLinkGraphGUI(int *biggest_index)
    00337 {
    00338   static const uint ENTRIES_PER_ROW = CeilDiv(NUM_CARGO, 5);
    00339   NWidgetVertical *panel = new NWidgetVertical(NC_EQUALSIZE);
    00340   NWidgetHorizontal *row = NULL;
    00341   for (uint i = 0; i < NUM_CARGO; ++i) {
    00342     if (i % ENTRIES_PER_ROW == 0) {
    00343       if (row) panel->Add(row);
    00344       row = new NWidgetHorizontal(NC_EQUALSIZE);
    00345     }
    00346     NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, i + WID_LGL_CARGO_FIRST);
    00347     wid->SetMinimalSize(25, FONT_HEIGHT_SMALL);
    00348     wid->SetFill(1, 1);
    00349     wid->SetResize(0, 0);
    00350     row->Add(wid);
    00351   }
    00352   /* Fill up last row */
    00353   for (uint i = 0; i < 4 - (NUM_CARGO - 1) % 5; ++i) {
    00354     NWidgetSpacer *spc = new NWidgetSpacer(25, FONT_HEIGHT_SMALL);
    00355     spc->SetFill(1, 1);
    00356     spc->SetResize(0, 0);
    00357     row->Add(spc);
    00358   }
    00359   panel->Add(row);
    00360   *biggest_index = WID_LGL_CARGO_LAST;
    00361   return panel;
    00362 }
    00363
    00364
    00365 static const NWidgetPart _nested_linkgraph_legend_widgets[] = {
    00366   NWidget(NWID_HORIZONTAL),
    00367     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    00368     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_LGL_CAPTION), SetDataTip(STR_LINKGRAPH_LEGEND_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00369     NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
    00370     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
    00371   EndContainer(),
    00372   NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
    00373     NWidget(NWID_HORIZONTAL),
    00374       NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_SATURATION),
    00375         SetPadding(WD_FRAMERECT_TOP, 0, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
    00376         NWidgetFunction(MakeSaturationLegendLinkGraphGUI),
    00377       EndContainer(),
    00378       NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_COMPANIES),
    00379         SetPadding(WD_FRAMERECT_TOP, 0, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
    00380         NWidget(NWID_VERTICAL, NC_EQUALSIZE),
    00381           NWidgetFunction(MakeCompanyButtonRowsLinkGraphGUI),
    00382           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_COMPANIES_ALL), SetDataTip(STR_LINKGRAPH_LEGEND_ALL, STR_NULL),
    00383           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_COMPANIES_NONE), SetDataTip(STR_LINKGRAPH_LEGEND_NONE, STR_NULL),
    00384         EndContainer(),
    00385       EndContainer(),
    00386       NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_LGL_CARGOES),
    00387         SetPadding(WD_FRAMERECT_TOP, WD_FRAMERECT_RIGHT, WD_FRAMERECT_BOTTOM, WD_CAPTIONTEXT_LEFT),
    00388         NWidget(NWID_VERTICAL, NC_EQUALSIZE),
    00389           NWidgetFunction(MakeCargoesLegendLinkGraphGUI),
    00390           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_CARGOES_ALL), SetDataTip(STR_LINKGRAPH_LEGEND_ALL, STR_NULL),
    00391           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_LGL_CARGOES_NONE), SetDataTip(STR_LINKGRAPH_LEGEND_NONE, STR_NULL),
    00392         EndContainer(),
    00393       EndContainer(),
    00394     EndContainer(),
    00395   EndContainer()
    00396 };
    00397
    00398 assert_compile(WID_LGL_SATURATION_LAST - WID_LGL_SATURATION_FIRST ==
    00399     lengthof(LinkGraphOverlay::LINK_COLOURS) - 1);
    00400
    00401 static WindowDesc _linkgraph_legend_desc(
    00402   WDP_AUTO, "toolbar_linkgraph", 0, 0,
    00403   WC_LINKGRAPH_LEGEND, WC_NONE,
    00404   0,
    00405   _nested_linkgraph_legend_widgets, lengthof(_nested_linkgraph_legend_widgets)
    00406 );
    00407
    00411 void ShowLinkGraphLegend()
    00412 {
    00413   AllocateWindowDescFront<LinkGraphLegendWindow>(&_linkgraph_legend_desc, 0);
    00414 }
    00415
    00416 LinkGraphLegendWindow::LinkGraphLegendWindow(WindowDesc *desc, int window_number) : Window(desc)
    00417 {
    00418   this->InitNested(window_number);
    00419   this->InvalidateData(0);
    00420   this->SetOverlay(FindWindowById(WC_MAIN_WINDOW, 0)->viewport->overlay);
    00421 }
    00422
    00427 void LinkGraphLegendWindow::SetOverlay(LinkGraphOverlay *overlay) {
    00428   this->overlay = overlay;
    00429   uint32 companies = this->overlay->GetCompanyMask();
    00430   for (uint c = 0; c < MAX_COMPANIES; c++) {
    00431     if (!this->IsWidgetDisabled(WID_LGL_COMPANY_FIRST + c)) {
    00432       this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST + c, HasBit(companies, c));
    00433     }
    00434   }
    00435   uint32 cargoes = this->overlay->GetCargoMask();
    00436   for (uint c = 0; c < NUM_CARGO; c++) {
    00437     if (!this->IsWidgetDisabled(WID_LGL_CARGO_FIRST + c)) {
    00438       this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, HasBit(cargoes, c));
    00439     }
    00440   }
    00441 }
    00442
    00443 void LinkGraphLegendWindow::UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00444 {
    00445   if (IsInsideMM(widget, WID_LGL_SATURATION_FIRST, WID_LGL_SATURATION_LAST + 1)) {
    00446     StringID str = STR_NULL;
    00447     if (widget == WID_LGL_SATURATION_FIRST) {
    00448       str = STR_LINKGRAPH_LEGEND_UNUSED;
    00449     } else if (widget == WID_LGL_SATURATION_LAST) {
    00450       str = STR_LINKGRAPH_LEGEND_OVERLOADED;
    00451     } else if (widget == (WID_LGL_SATURATION_LAST + WID_LGL_SATURATION_FIRST) / 2) {
    00452       str = STR_LINKGRAPH_LEGEND_SATURATED;
    00453     }
    00454     if (str != STR_NULL) {
    00455       Dimension dim = GetStringBoundingBox(str);
    00456       dim.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
    00457       dim.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00458       *size = maxdim(*size, dim);
    00459     }
    00460   }
    00461   if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
    00462     CargoSpec *cargo = CargoSpec::Get(widget - WID_LGL_CARGO_FIRST);
    00463     if (cargo->IsValid()) {
    00464       Dimension dim = GetStringBoundingBox(cargo->abbrev);
    00465       dim.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
    00466       dim.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00467       *size = maxdim(*size, dim);
    00468     }
    00469   }
    00470 }
    00471
    00472 void LinkGraphLegendWindow::DrawWidget(const Rect &r, int widget) const
    00473 {
    00474   if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
    00475     if (this->IsWidgetDisabled(widget)) return;
    00476     CompanyID cid = (CompanyID)(widget - WID_LGL_COMPANY_FIRST);
    00477     Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
    00478     DrawCompanyIcon(cid, (r.left + r.right + 1 - sprite_size.width) / 2, (r.top + r.bottom + 1 - sprite_size.height) / 2);
    00479   }
    00480   if (IsInsideMM(widget, WID_LGL_SATURATION_FIRST, WID_LGL_SATURATION_LAST + 1)) {
    00481     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, LinkGraphOverlay::LINK_COLOURS[widget - WID_LGL_SATURATION_FIRST]);
    00482     StringID str = STR_NULL;
    00483     if (widget == WID_LGL_SATURATION_FIRST) {
    00484       str = STR_LINKGRAPH_LEGEND_UNUSED;
    00485     } else if (widget == WID_LGL_SATURATION_LAST) {
    00486       str = STR_LINKGRAPH_LEGEND_OVERLOADED;
    00487     } else if (widget == (WID_LGL_SATURATION_LAST + WID_LGL_SATURATION_FIRST) / 2) {
    00488       str = STR_LINKGRAPH_LEGEND_SATURATED;
    00489     }
    00490     if (str != STR_NULL) DrawString(r.left, r.right, (r.top + r.bottom + 1 - FONT_HEIGHT_SMALL) / 2, str, TC_FROMSTRING, SA_HOR_CENTER);
    00491   }
    00492   if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
    00493     if (this->IsWidgetDisabled(widget)) return;
    00494     CargoSpec *cargo = CargoSpec::Get(widget - WID_LGL_CARGO_FIRST);
    00495     GfxFillRect(r.left + 2, r.top + 2, r.right - 2, r.bottom - 2, cargo->legend_colour);
    00496     DrawString(r.left, r.right, (r.top + r.bottom + 1 - FONT_HEIGHT_SMALL) / 2, cargo->abbrev, TC_BLACK, SA_HOR_CENTER);
    00497   }
    00498 }
    00499
    00503 void LinkGraphLegendWindow::UpdateOverlayCompanies()
    00504 {
    00505   uint32 mask = 0;
    00506   for (uint c = 0; c < MAX_COMPANIES; c++) {
    00507     if (this->IsWidgetDisabled(c + WID_LGL_COMPANY_FIRST)) continue;
    00508     if (!this->IsWidgetLowered(c + WID_LGL_COMPANY_FIRST)) continue;
    00509     SetBit(mask, c);
    00510   }
    00511   this->overlay->SetCompanyMask(mask);
    00512 }
    00513
    00517 void LinkGraphLegendWindow::UpdateOverlayCargoes()
    00518 {
    00519   uint32 mask = 0;
    00520   for (uint c = 0; c < NUM_CARGO; c++) {
    00521     if (this->IsWidgetDisabled(c + WID_LGL_CARGO_FIRST)) continue;
    00522     if (!this->IsWidgetLowered(c + WID_LGL_CARGO_FIRST)) continue;
    00523     SetBit(mask, c);
    00524   }
    00525   this->overlay->SetCargoMask(mask);
    00526 }
    00527
    00528 void LinkGraphLegendWindow::OnClick(Point pt, int widget, int click_count)
    00529 {
    00530   /* Check which button is clicked */
    00531   if (IsInsideMM(widget, WID_LGL_COMPANY_FIRST, WID_LGL_COMPANY_LAST + 1)) {
    00532     if (!this->IsWidgetDisabled(widget)) {
    00533       this->ToggleWidgetLoweredState(widget);
    00534       this->UpdateOverlayCompanies();
    00535     }
    00536   } else if (widget == WID_LGL_COMPANIES_ALL || widget == WID_LGL_COMPANIES_NONE) {
    00537     for (uint c = 0; c < MAX_COMPANIES; c++) {
    00538       if (this->IsWidgetDisabled(c + WID_LGL_COMPANY_FIRST)) continue;
    00539       this->SetWidgetLoweredState(WID_LGL_COMPANY_FIRST + c, widget == WID_LGL_COMPANIES_ALL);
    00540     }
    00541     this->UpdateOverlayCompanies();
    00542     this->SetDirty();
    00543   } else if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
    00544     if (!this->IsWidgetDisabled(widget)) {
    00545       this->ToggleWidgetLoweredState(widget);
    00546       this->UpdateOverlayCargoes();
    00547     }
    00548   } else if (widget == WID_LGL_CARGOES_ALL || widget == WID_LGL_CARGOES_NONE) {
    00549     for (uint c = 0; c < NUM_CARGO; c++) {
    00550       if (this->IsWidgetDisabled(c + WID_LGL_CARGO_FIRST)) continue;
    00551       this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, widget == WID_LGL_CARGOES_ALL);
    00552     }
    00553     this->UpdateOverlayCargoes();
    00554   }
    00555   this->SetDirty();
    00556 }
    00557
    00563 void LinkGraphLegendWindow::OnInvalidateData(int data, bool gui_scope)
    00564 {
    00565   /* Disable the companies who are not active */
    00566   for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
    00567     this->SetWidgetDisabledState(i + WID_LGL_COMPANY_FIRST, !Company::IsValidID(i));
    00568   }
    00569   for (CargoID i = 0; i < NUM_CARGO; i++) {
    00570     this->SetWidgetDisabledState(i + WID_LGL_CARGO_FIRST, !CargoSpec::Get(i)->IsValid());
    00571   }
    00572 }