Factor out OnClick dispatching in Window::HandleEditBoxKey
[openttd/fttd.git] / src / currency.cpp
blobb6326e23f2695654960b0365580eb5d2df8085f5
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file currency.cpp Support for different currencies. */
12 #include "stdafx.h"
13 #include "core/bitmath_func.hpp"
15 #include "currency.h"
16 #include "news_func.h"
17 #include "settings_type.h"
18 #include "date_func.h"
19 #include "string.h"
21 #include "table/strings.h"
23 /* exchange rate prefix symbol_pos
24 * | separator | postfix |
25 * | | Euro year | | | name
26 * | | | | | | | */
27 /** The original currency specifications. */
28 static const CurrencySpec origin_currency_specs[CURRENCY_END] = {
29 { 1, "", CF_NOEURO, "\xC2\xA3", "", 0, STR_GAME_OPTIONS_CURRENCY_GBP }, ///< british pound
30 { 2, "", CF_NOEURO, "$", "", 0, STR_GAME_OPTIONS_CURRENCY_USD }, ///< american dollar
31 { 2, "", CF_ISEURO, "\xE2\x82\xAC", "", 0, STR_GAME_OPTIONS_CURRENCY_EUR }, ///< euro
32 { 220, "", CF_NOEURO, "\xC2\xA5", "", 0, STR_GAME_OPTIONS_CURRENCY_JPY }, ///< japanese yen
33 { 27, "", 2002, "", NBSP "S.", 1, STR_GAME_OPTIONS_CURRENCY_ATS }, ///< austrian schilling
34 { 81, "", 2002, "BEF" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_BEF }, ///< belgian franc
35 { 2, "", CF_NOEURO, "CHF" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_CHF }, ///< swiss franc
36 { 41, "", CF_NOEURO, "", NBSP "K\xC4\x8D", 1, STR_GAME_OPTIONS_CURRENCY_CZK }, ///< czech koruna
37 { 4, "", 2002, "DM" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_DEM }, ///< deutsche mark
38 { 11, "", CF_NOEURO, "", NBSP "kr", 1, STR_GAME_OPTIONS_CURRENCY_DKK }, ///< danish krone
39 { 333, "", 2002, "Pts" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_ESP }, ///< spanish peseta
40 { 12, "", 2002, "", NBSP "mk", 1, STR_GAME_OPTIONS_CURRENCY_FIM }, ///< finnish markka
41 { 13, "", 2002, "FF" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_FRF }, ///< french franc
42 { 681, "", 2002, "", "Dr.", 1, STR_GAME_OPTIONS_CURRENCY_GRD }, ///< greek drachma
43 { 378, "", CF_NOEURO, "", NBSP "Ft", 1, STR_GAME_OPTIONS_CURRENCY_HUF }, ///< hungarian forint
44 { 130, "", CF_NOEURO, "", NBSP "Kr", 1, STR_GAME_OPTIONS_CURRENCY_ISK }, ///< icelandic krona
45 { 3873, "", 2002, "", NBSP "L.", 1, STR_GAME_OPTIONS_CURRENCY_ITL }, ///< italian lira
46 { 4, "", 2002, "NLG" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_NLG }, ///< dutch gulden
47 { 12, "", CF_NOEURO, "", NBSP "Kr", 1, STR_GAME_OPTIONS_CURRENCY_NOK }, ///< norwegian krone
48 { 6, "", CF_NOEURO, "", NBSP "z\xC5\x82", 1, STR_GAME_OPTIONS_CURRENCY_PLN }, ///< polish zloty
49 { 5, "", CF_NOEURO, "", NBSP "Lei", 1, STR_GAME_OPTIONS_CURRENCY_RON }, ///< romanian leu
50 { 50, "", CF_NOEURO, "", NBSP "p", 1, STR_GAME_OPTIONS_CURRENCY_RUR }, ///< russian rouble
51 { 479, "", 2007, "", NBSP "SIT", 1, STR_GAME_OPTIONS_CURRENCY_SIT }, ///< slovenian tolar
52 { 13, "", CF_NOEURO, "", NBSP "Kr", 1, STR_GAME_OPTIONS_CURRENCY_SEK }, ///< swedish krona
53 { 3, "", CF_NOEURO, "", NBSP "TL", 1, STR_GAME_OPTIONS_CURRENCY_TRY }, ///< turkish lira
54 { 60, "", 2009, "", NBSP "Sk", 1, STR_GAME_OPTIONS_CURRENCY_SKK }, ///< slovak koruna
55 { 4, "", CF_NOEURO, "R$" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_BRL }, ///< brazil real
56 { 31, "", 2011, "", NBSP "EEK", 1, STR_GAME_OPTIONS_CURRENCY_EEK }, ///< estonian krooni
57 { 4, "", 2015, "", NBSP "Lt", 1, STR_GAME_OPTIONS_CURRENCY_LTL }, ///< lithuanian litas
58 { 1850, "", CF_NOEURO, "\xE2\x82\xA9", "", 0, STR_GAME_OPTIONS_CURRENCY_KRW }, ///< south korean won
59 { 13, "", CF_NOEURO, "R" NBSP, "", 0, STR_GAME_OPTIONS_CURRENCY_ZAR }, ///< south african rand
60 { 1, "", CF_NOEURO, "", "", 2, STR_GAME_OPTIONS_CURRENCY_CUSTOM }, ///< custom currency (add further languages below)
61 { 3, "", CF_NOEURO, "", NBSP "GEL", 1, STR_GAME_OPTIONS_CURRENCY_GEL }, ///< Georgian Lari
62 { 4901, "", CF_NOEURO, "", NBSP "Rls", 1, STR_GAME_OPTIONS_CURRENCY_IRR }, ///< Iranian Rial
65 /** Array of currencies used by the system */
66 CurrencySpec _currency_specs[CURRENCY_END];
68 /**
69 * get a mask of the allowed currencies depending on the year
70 * @return mask of currencies
72 uint64 GetMaskOfAllowedCurrencies()
74 uint64 mask = 0LL;
75 uint i;
77 for (i = 0; i < CURRENCY_END; i++) {
78 Year to_euro = _currency_specs[i].to_euro;
80 if (to_euro != CF_NOEURO && to_euro != CF_ISEURO && _cur_year >= to_euro) continue;
81 if (to_euro == CF_ISEURO && _cur_year < 2000) continue;
82 SetBit(mask, i);
84 SetBit(mask, CURRENCY_CUSTOM); // always allow custom currency
85 return mask;
88 /**
89 * Verify if the currency chosen by the user is about to be converted to Euro
91 void CheckSwitchToEuro()
93 if (_currency_specs[_settings_game.locale.currency].to_euro != CF_NOEURO &&
94 _currency_specs[_settings_game.locale.currency].to_euro != CF_ISEURO &&
95 _cur_year >= _currency_specs[_settings_game.locale.currency].to_euro) {
96 _settings_game.locale.currency = 2; // this is the index of euro above.
97 AddNewsItem<NewsItem> (STR_NEWS_EURO_INTRODUCTION, NT_ECONOMY, NF_NORMAL);
102 * Will fill _currency_specs array with
103 * default values from origin_currency_specs
104 * Called only from newgrf.cpp and settings.cpp.
105 * @param preserve_custom will not reset custom currency
107 void ResetCurrencies(bool preserve_custom)
109 for (uint i = 0; i < CURRENCY_END; i++) {
110 if (preserve_custom && i == CURRENCY_CUSTOM) continue;
111 _currency_specs[i] = origin_currency_specs[i];
116 * Build a list of currency names StringIDs to use in a dropdown list
117 * @return Pointer to a (static) array of StringIDs
119 StringID *BuildCurrencyDropdown()
121 /* Allow room for all currencies, plus a terminator entry */
122 static StringID names[CURRENCY_END + 1];
123 uint i;
125 /* Add each name */
126 for (i = 0; i < CURRENCY_END; i++) {
127 names[i] = _currency_specs[i].name;
129 /* Terminate the list */
130 names[i] = INVALID_STRING_ID;
132 return names;