# statusbar_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: statusbar_gui.cpp 18966 2010-01-30 18:34:48Z frosch $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "openttd.h"
    00014 #include "date_func.h"
    00015 #include "gfx_func.h"
    00016 #include "news_func.h"
    00017 #include "company_func.h"
    00018 #include "string_func.h"
    00019 #include "strings_func.h"
    00020 #include "company_base.h"
    00021 #include "tilehighlight_func.h"
    00022 #include "news_gui.h"
    00023 #include "company_gui.h"
    00024 #include "window_gui.h"
    00025 #include "variables.h"
    00026 #include "window_func.h"
    00027 #include "statusbar_gui.h"
    00028 #include "core/geometry_func.hpp"
    00029
    00030 #include "table/strings.h"
    00031 #include "table/sprites.h"
    00032
    00033 static bool DrawScrollingStatusText(const NewsItem *ni, int scroll_pos, int left, int right, int top, int bottom)
    00034 {
    00035   CopyInDParam(0, ni->params, lengthof(ni->params));
    00036   StringID str = ni->string_id;
    00037
    00038   char buf[512];
    00039   GetString(buf, str, lastof(buf));
    00040   const char *s = buf;
    00041
    00042   char buffer[256];
    00043   char *d = buffer;
    00044   const char *last = lastof(buffer);
    00045
    00046   for (;;) {
    00047     WChar c = Utf8Consume(&s);
    00048     if (c == 0) {
    00049       break;
    00050     } else if (c == '\n') {
    00051       if (d + 4 >= last) break;
    00052       d[0] = d[1] = d[2] = d[3] = ' ';
    00053       d += 4;
    00054     } else if (IsPrintable(c)) {
    00055       if (d + Utf8CharLen(c) >= last) break;
    00056       d += Utf8Encode(d, c);
    00057     }
    00058   }
    00059   *d = '\0';
    00060
    00061   DrawPixelInfo tmp_dpi;
    00062   if (!FillDrawPixelInfo(&tmp_dpi, left, top, right - left, bottom)) return true;
    00063
    00064   int width = GetStringBoundingBox(buffer).width;
    00065   int pos = (_dynlang.text_dir == TD_RTL) ? (scroll_pos - width) : (right - scroll_pos - left);
    00066
    00067   DrawPixelInfo *old_dpi = _cur_dpi;
    00068   _cur_dpi = &tmp_dpi;
    00069   DrawString(pos, INT16_MAX, 0, buffer, TC_LIGHT_BLUE, SA_LEFT | SA_FORCE);
    00070   _cur_dpi = old_dpi;
    00071
    00072   return (_dynlang.text_dir == TD_RTL) ? (pos < right - left) : (pos + width > 0);
    00073 }
    00074
    00075 enum StatusbarWidget {
    00076   SBW_LEFT,
    00077   SBW_MIDDLE,
    00078   SBW_RIGHT,
    00079 };
    00080
    00081 struct StatusBarWindow : Window {
    00082   bool saving;
    00083   int ticker_scroll;
    00084   int reminder_timeout;
    00085
    00086   enum {
    00087     TICKER_STOP    = 1640,
    00088     REMINDER_START =   91,
    00089     REMINDER_STOP  =    0,
    00090     COUNTER_STEP   =    2,
    00091   };
    00092
    00093   StatusBarWindow(const WindowDesc *desc) : Window()
    00094   {
    00095     CLRBITS(this->flags4, WF_WHITE_BORDER_MASK);
    00096     this->ticker_scroll    =   TICKER_STOP;
    00097     this->reminder_timeout = REMINDER_STOP;
    00098
    00099     this->InitNested(desc);
    00100   }
    00101
    00102   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
    00103   {
    00104     Point pt = { (_screen.width - max(sm_width, desc->default_width)) / 2, _screen.height - sm_height };
    00105     return pt;
    00106   }
    00107
    00108   virtual void OnPaint()
    00109   {
    00110     this->DrawWidgets();
    00111   }
    00112
    00113   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00114   {
    00115     Dimension d;
    00116     switch (widget) {
    00117       case SBW_LEFT:
    00118         SetDParam(0, MAX_YEAR * DAYS_IN_YEAR);
    00119         d = GetStringBoundingBox(STR_WHITE_DATE_LONG);
    00120         break;
    00121
    00122       case SBW_RIGHT: {
    00123         int64 max_money = UINT32_MAX;
    00124         const Company *c;
    00125         FOR_ALL_COMPANIES(c) max_money = max<int64>(c->money, max_money);
    00126         SetDParam(0, 100LL * max_money);
    00127         d = GetStringBoundingBox(STR_COMPANY_MONEY);
    00128       } break;
    00129
    00130       default:
    00131         return;
    00132     }
    00133
    00134     d.width += padding.width;
    00135     d.height += padding.height;
    00136     *size = maxdim(d, *size);
    00137   }
    00138
    00139   virtual void DrawWidget(const Rect &r, int widget) const
    00140   {
    00141     switch (widget) {
    00142       case SBW_LEFT:
    00143         /* Draw the date */
    00144         SetDParam(0, _date);
    00145         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, (_pause_mode || _settings_client.gui.status_long_date) ? STR_WHITE_DATE_LONG : STR_WHITE_DATE_SHORT, TC_FROMSTRING, SA_CENTER);
    00146         break;
    00147
    00148       case SBW_RIGHT: {
    00149         /* Draw company money, if any */
    00150         const Company *c = Company::GetIfValid(_local_company);
    00151         if (c != NULL) {
    00152           SetDParam(0, c->money);
    00153           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_COMPANY_MONEY, TC_FROMSTRING, SA_CENTER);
    00154         }
    00155       } break;
    00156
    00157       case SBW_MIDDLE:
    00158         /* Draw status bar */
    00159         if (this->saving) { // true when saving is active
    00160           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_STATUSBAR_SAVING_GAME, TC_FROMSTRING, SA_CENTER);
    00161         } else if (_do_autosave) {
    00162           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_STATUSBAR_AUTOSAVE, TC_FROMSTRING, SA_CENTER);
    00163         } else if (_pause_mode != PM_UNPAUSED) {
    00164           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_STATUSBAR_PAUSED, TC_FROMSTRING, SA_CENTER);
    00165         } else if (this->ticker_scroll < TICKER_STOP && FindWindowById(WC_NEWS_WINDOW, 0) == NULL && _statusbar_news_item != NULL && _statusbar_news_item->string_id != 0) {
    00166           /* Draw the scrolling news text */
    00167           if (!DrawScrollingStatusText(_statusbar_news_item, this->ticker_scroll, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom)) {
    00168             InvalidateWindowData(WC_STATUS_BAR, 0, SBI_NEWS_DELETED);
    00169             if (Company::IsValidID(_local_company)) {
    00170               /* This is the default text */
    00171               SetDParam(0, _local_company);
    00172               DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_STATUSBAR_COMPANY_NAME, TC_FROMSTRING, SA_CENTER);
    00173             }
    00174           }
    00175         } else {
    00176           if (Company::IsValidID(_local_company)) {
    00177             /* This is the default text */
    00178             SetDParam(0, _local_company);
    00179             DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_STATUSBAR_COMPANY_NAME, TC_FROMSTRING, SA_CENTER);
    00180           }
    00181         }
    00182
    00183         if (this->reminder_timeout > 0) DrawSprite(SPR_BLOT, PALETTE_TO_RED, r.right - WD_FRAMERECT_RIGHT - 10, r.top + WD_FRAMERECT_TOP + 1);
    00184         break;
    00185     }
    00186   }
    00187
    00188   virtual void OnInvalidateData(int data)
    00189   {
    00190     switch (data) {
    00191       default: NOT_REACHED();
    00192       case SBI_SAVELOAD_START:  this->saving = true;  break;
    00193       case SBI_SAVELOAD_FINISH: this->saving = false; break;
    00194       case SBI_SHOW_TICKER:     this->ticker_scroll = 0; break;
    00195       case SBI_SHOW_REMINDER:   this->reminder_timeout = REMINDER_START; break;
    00196       case SBI_NEWS_DELETED:
    00197         this->ticker_scroll    =   TICKER_STOP; // reset ticker ...
    00198         this->reminder_timeout = REMINDER_STOP; // ... and reminder
    00199         break;
    00200     }
    00201   }
    00202
    00203   virtual void OnClick(Point pt, int widget, int click_count)
    00204   {
    00205     switch (widget) {
    00206       case SBW_MIDDLE: ShowLastNewsMessage(); break;
    00207       case SBW_RIGHT:  if (_local_company != COMPANY_SPECTATOR) ShowCompanyFinances(_local_company); break;
    00208       default: ResetObjectToPlace();
    00209     }
    00210   }
    00211
    00212   virtual void OnTick()
    00213   {
    00214     if (_pause_mode != PM_UNPAUSED) return;
    00215
    00216     if (this->ticker_scroll < TICKER_STOP) { // Scrolling text
    00217       this->ticker_scroll += COUNTER_STEP;
    00218       this->SetWidgetDirty(SBW_MIDDLE);
    00219     }
    00220
    00221     if (this->reminder_timeout > REMINDER_STOP) { // Red blot to show there are new unread newsmessages
    00222       this->reminder_timeout -= COUNTER_STEP;
    00223     } else if (this->reminder_timeout < REMINDER_STOP) {
    00224       this->reminder_timeout = REMINDER_STOP;
    00225       this->SetWidgetDirty(SBW_MIDDLE);
    00226     }
    00227   }
    00228 };
    00229
    00230 static const NWidgetPart _nested_main_status_widgets[] = {
    00231   NWidget(NWID_HORIZONTAL),
    00232     NWidget(WWT_PANEL, COLOUR_GREY, SBW_LEFT), SetMinimalSize(140, 12), EndContainer(),
    00233     NWidget(WWT_PUSHBTN, COLOUR_GREY, SBW_MIDDLE), SetMinimalSize(40, 12), SetDataTip(0x0, STR_STATUSBAR_TOOLTIP_SHOW_LAST_NEWS), SetResize(1, 0),
    00234     NWidget(WWT_PUSHBTN, COLOUR_GREY, SBW_RIGHT), SetMinimalSize(140, 12),
    00235   EndContainer(),
    00236 };
    00237
    00238 static const WindowDesc _main_status_desc(
    00239   WDP_MANUAL, 640, 12,
    00240   WC_STATUS_BAR, WC_NONE,
    00241   WDF_UNCLICK_BUTTONS | WDF_NO_FOCUS,
    00242   _nested_main_status_widgets, lengthof(_nested_main_status_widgets)
    00243 );
    00244
    00248 bool IsNewsTickerShown()
    00249 {
    00250   const StatusBarWindow *w = dynamic_cast<StatusBarWindow*>(FindWindowById(WC_STATUS_BAR, 0));
    00251   return w != NULL && w->ticker_scroll < StatusBarWindow::TICKER_STOP;
    00252 }
    00253
    00254 void ShowStatusBar()
    00255 {
    00256   new StatusBarWindow(&_main_status_desc);
    00257 }