# music_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: music_gui.cpp 18608 2009-12-22 21:40:29Z rubidium $ */
    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 "fileio_func.h"
    00015 #include "base_media_base.h"
    00016 #include "music/music_driver.hpp"
    00017 #include "window_gui.h"
    00018 #include "strings_func.h"
    00019 #include "window_func.h"
    00020 #include "sound_func.h"
    00021 #include "gfx_func.h"
    00022 #include "core/random_func.hpp"
    00023 #include "gui.h"
    00024
    00025 #include "table/strings.h"
    00026 #include "table/sprites.h"
    00027
    00033 static const char *GetSongName(int index)
    00034 {
    00035   return BaseMusic::GetUsedSet()->song_name[index];
    00036 }
    00037
    00043 static int GetTrackNumber(int index)
    00044 {
    00045   return BaseMusic::GetUsedSet()->track_nr[index];
    00046 }
    00047
    00049 static byte _music_wnd_cursong = 1;
    00051 static bool _song_is_active = false;
    00052
    00054 static byte _cur_playlist[NUM_SONGS_PLAYLIST + 1];
    00055
    00057 static byte _playlist_all[NUM_SONGS_AVAILABLE + 1];
    00059 static byte _playlist_old_style[NUM_SONGS_CLASS + 1];
    00061 static byte _playlist_new_style[NUM_SONGS_CLASS + 1];
    00063 static byte _playlist_ezy_street[NUM_SONGS_CLASS + 1];
    00064
    00065 assert_compile(lengthof(msf.custom_1) == NUM_SONGS_PLAYLIST + 1);
    00066 assert_compile(lengthof(msf.custom_2) == NUM_SONGS_PLAYLIST + 1);
    00067
    00069 static byte * const _playlists[] = {
    00070   _playlist_all,
    00071   _playlist_old_style,
    00072   _playlist_new_style,
    00073   _playlist_ezy_street,
    00074   msf.custom_1,
    00075   msf.custom_2,
    00076 };
    00077
    00082 void ValidatePlaylist(byte *playlist)
    00083 {
    00084   while (*playlist != 0) {
    00085     if (*playlist <= BaseMusic::GetUsedSet()->num_available) {
    00086       playlist++;
    00087       continue;
    00088     }
    00089     for (byte *p = playlist; *p != 0; p++) {
    00090       p[0] = p[1];
    00091     }
    00092   }
    00093 }
    00094
    00096 void InitializeMusic()
    00097 {
    00098   uint j = 0;
    00099   for (uint i = 0; i < NUM_SONGS_AVAILABLE; i++) {
    00100     if (StrEmpty(GetSongName(i))) continue;
    00101     _playlist_all[j++] = i + 1;
    00102   }
    00103   /* Terminate the list */
    00104   _playlist_all[j] = 0;
    00105
    00106   /* Now make the 'styled' playlists */
    00107   for (uint k = 0; k < NUM_SONG_CLASSES; k++) {
    00108     j = 0;
    00109     for (uint i = 0; i < NUM_SONGS_CLASS; i++) {
    00110       int id = k * NUM_SONGS_CLASS + i + 1;
    00111       if (StrEmpty(GetSongName(id))) continue;
    00112       _playlists[k + 1][j++] = id + 1;
    00113     }
    00114     /* Terminate the list */
    00115     _playlists[k + 1][j] = 0;
    00116   }
    00117
    00118   ValidatePlaylist(msf.custom_1);
    00119   ValidatePlaylist(msf.custom_2);
    00120
    00121   if (BaseMusic::GetUsedSet()->num_available < _music_wnd_cursong) {
    00122     /* If there are less songs than the currently played song,
    00123      * just pause and reset to no song. */
    00124     _music_wnd_cursong = 0;
    00125     _song_is_active = false;
    00126   }
    00127 }
    00128
    00129 static void SkipToPrevSong()
    00130 {
    00131   byte *b = _cur_playlist;
    00132   byte *p = b;
    00133   byte t;
    00134
    00135   if (b[0] == 0) return; // empty playlist
    00136
    00137   do p++; while (p[0] != 0); // find the end
    00138
    00139   t = *--p; // and copy the bytes
    00140   while (p != b) {
    00141     p--;
    00142     p[1] = p[0];
    00143   }
    00144   *b = t;
    00145
    00146   _song_is_active = false;
    00147 }
    00148
    00149 static void SkipToNextSong()
    00150 {
    00151   byte *b = _cur_playlist;
    00152   byte t;
    00153
    00154   t = b[0];
    00155   if (t != 0) {
    00156     while (b[1] != 0) {
    00157       b[0] = b[1];
    00158       b++;
    00159     }
    00160     b[0] = t;
    00161   }
    00162
    00163   _song_is_active = false;
    00164 }
    00165
    00166 static void MusicVolumeChanged(byte new_vol)
    00167 {
    00168   _music_driver->SetVolume(new_vol);
    00169 }
    00170
    00171 static void DoPlaySong()
    00172 {
    00173   char filename[MAX_PATH];
    00174   FioFindFullPath(filename, lengthof(filename), GM_DIR,
    00175       BaseMusic::GetUsedSet()->files[_music_wnd_cursong - 1].filename);
    00176   _music_driver->PlaySong(filename);
    00177   SetWindowDirty(WC_MUSIC_WINDOW, 0);
    00178 }
    00179
    00180 static void DoStopMusic()
    00181 {
    00182   _music_driver->StopSong();
    00183   SetWindowDirty(WC_MUSIC_WINDOW, 0);
    00184 }
    00185
    00186 static void SelectSongToPlay()
    00187 {
    00188   uint i = 0;
    00189   uint j = 0;
    00190
    00191   memset(_cur_playlist, 0, sizeof(_cur_playlist));
    00192   do {
    00193     const char *filename = BaseMusic::GetUsedSet()->files[_playlists[msf.playlist][i] - 1].filename;
    00194     /* We are now checking for the existence of that file prior
    00195      * to add it to the list of available songs */
    00196     if (!StrEmpty(filename) && FioCheckFileExists(filename, GM_DIR)) {
    00197       _cur_playlist[j] = _playlists[msf.playlist][i];
    00198       j++;
    00199     }
    00200   } while (_playlists[msf.playlist][++i] != 0 && j < lengthof(_cur_playlist) - 1);
    00201
    00202   /* Do not shuffle when on the intro-start window, as the song to play has to be the original TTD Theme*/
    00203   if (msf.shuffle && _game_mode != GM_MENU) {
    00204     i = 500;
    00205     do {
    00206       uint32 r = InteractiveRandom();
    00207       byte *a = &_cur_playlist[GB(r, 0, 5)];
    00208       byte *b = &_cur_playlist[GB(r, 8, 5)];
    00209
    00210       if (*a != 0 && *b != 0) {
    00211         byte t = *a;
    00212         *a = *b;
    00213         *b = t;
    00214       }
    00215     } while (--i);
    00216   }
    00217 }
    00218
    00219 static void StopMusic()
    00220 {
    00221   _music_wnd_cursong = 0;
    00222   DoStopMusic();
    00223   _song_is_active = false;
    00224   SetWindowWidgetDirty(WC_MUSIC_WINDOW, 0, 9);
    00225 }
    00226
    00227 static void PlayPlaylistSong()
    00228 {
    00229   if (_cur_playlist[0] == 0) {
    00230     SelectSongToPlay();
    00231     /* if there is not songs in the playlist, it may indicate
    00232      * no file on the gm folder, or even no gm folder.
    00233      * Stop the playback, then */
    00234     if (_cur_playlist[0] == 0) {
    00235       _song_is_active = false;
    00236       _music_wnd_cursong = 0;
    00237       msf.playing = false;
    00238       return;
    00239     }
    00240   }
    00241   _music_wnd_cursong = _cur_playlist[0];
    00242   DoPlaySong();
    00243   _song_is_active = true;
    00244
    00245   SetWindowWidgetDirty(WC_MUSIC_WINDOW, 0, 9);
    00246 }
    00247
    00248 void ResetMusic()
    00249 {
    00250   _music_wnd_cursong = 1;
    00251   DoPlaySong();
    00252 }
    00253
    00254 void MusicLoop()
    00255 {
    00256   if (!msf.playing && _song_is_active) {
    00257     StopMusic();
    00258   } else if (msf.playing && !_song_is_active) {
    00259     PlayPlaylistSong();
    00260   }
    00261
    00262   if (!_song_is_active) return;
    00263
    00264   if (!_music_driver->IsSongPlaying()) {
    00265     if (_game_mode != GM_MENU) {
    00266       StopMusic();
    00267       SkipToNextSong();
    00268       PlayPlaylistSong();
    00269     } else {
    00270       ResetMusic();
    00271     }
    00272   }
    00273 }
    00274
    00275 static void SelectPlaylist(byte list)
    00276 {
    00277   msf.playlist = list;
    00278   InvalidateWindowData(WC_MUSIC_TRACK_SELECTION, 0);
    00279   InvalidateWindowData(WC_MUSIC_WINDOW, 0);
    00280 }
    00281
    00282 enum MusicTrackSelectionWidgets {
    00283   MTSW_LIST_LEFT,
    00284   MTSW_PLAYLIST,
    00285   MTSW_LIST_RIGHT,
    00286   MTSW_ALL,
    00287   MTSW_OLD,
    00288   MTSW_NEW,
    00289   MTSW_EZY,
    00290   MTSW_CUSTOM1,
    00291   MTSW_CUSTOM2,
    00292   MTSW_CLEAR,
    00293 };
    00294
    00295 struct MusicTrackSelectionWindow : public Window {
    00296   MusicTrackSelectionWindow(const WindowDesc *desc, WindowNumber number) : Window()
    00297   {
    00298     this->InitNested(desc, number);
    00299     this->LowerWidget(MTSW_LIST_LEFT);
    00300     this->LowerWidget(MTSW_LIST_RIGHT);
    00301     this->SetWidgetDisabledState(MTSW_CLEAR, msf.playlist <= 3);
    00302     this->LowerWidget(MTSW_ALL + msf.playlist);
    00303   }
    00304
    00305   virtual void SetStringParameters(int widget) const
    00306   {
    00307     switch (widget) {
    00308       case MTSW_PLAYLIST:
    00309         SetDParam(0, STR_MUSIC_PLAYLIST_ALL + msf.playlist);
    00310         break;
    00311     }
    00312   }
    00313
    00314   virtual void OnInvalidateData(int data = 0)
    00315   {
    00316     for (int i = 0; i < 6; i++) {
    00317       this->SetWidgetLoweredState(MTSW_ALL + i, i == msf.playlist);
    00318     }
    00319     this->SetWidgetDisabledState(MTSW_CLEAR, msf.playlist <= 3);
    00320     this->SetDirty();
    00321   }
    00322
    00323   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00324   {
    00325     switch (widget) {
    00326       case MTSW_PLAYLIST: {
    00327         Dimension d = {0, 0};
    00328
    00329         for (int i = 0; i < 6; i++) {
    00330           SetDParam(0, STR_MUSIC_PLAYLIST_ALL + i);
    00331           d = maxdim(d, GetStringBoundingBox(STR_PLAYLIST_PROGRAM));
    00332         }
    00333         d.width += padding.width;
    00334         d.height += padding.height;
    00335         *size = maxdim(*size, d);
    00336       } break;
    00337
    00338       case MTSW_LIST_LEFT: case MTSW_LIST_RIGHT: {
    00339         Dimension d = {0, 0};
    00340
    00341         for (uint i = 0; i < NUM_SONGS_AVAILABLE; i++) {
    00342           const char *song_name = GetSongName(i);
    00343           if (StrEmpty(song_name)) continue;
    00344
    00345           SetDParam(0, GetTrackNumber(i));
    00346           SetDParam(1, 2);
    00347           SetDParamStr(2, GetSongName(i));
    00348           Dimension d2 = GetStringBoundingBox(STR_PLAYLIST_TRACK_NAME);
    00349           d.width = max(d.width, d2.width);
    00350           d.height += d2.height;
    00351         }
    00352         d.width += padding.width;
    00353         d.height += padding.height;
    00354         *size = maxdim(*size, d);
    00355       } break;
    00356     }
    00357   }
    00358
    00359   virtual void DrawWidget(const Rect &r, int widget) const
    00360   {
    00361     switch (widget) {
    00362       case MTSW_LIST_LEFT: {
    00363         GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, 0);
    00364
    00365         int y = r.top + WD_FRAMERECT_TOP;
    00366         for (uint i = 0; i < NUM_SONGS_AVAILABLE; i++) {
    00367           const char *song_name = GetSongName(i);
    00368           if (StrEmpty(song_name)) continue;
    00369
    00370           SetDParam(0, GetTrackNumber(i));
    00371           SetDParam(1, 2);
    00372           SetDParamStr(2, song_name);
    00373           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_PLAYLIST_TRACK_NAME);
    00374           y += FONT_HEIGHT_SMALL;
    00375         }
    00376       } break;
    00377
    00378       case MTSW_LIST_RIGHT: {
    00379         GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, 0);
    00380
    00381         int y = r.top + WD_FRAMERECT_TOP;
    00382         for (const byte *p = _playlists[msf.playlist]; *p != 0; p++) {
    00383           uint i = *p - 1;
    00384           SetDParam(0, GetTrackNumber(i));
    00385           SetDParam(1, 2);
    00386           SetDParamStr(2, GetSongName(i));
    00387           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_PLAYLIST_TRACK_NAME);
    00388           y += FONT_HEIGHT_SMALL;
    00389         }
    00390       } break;
    00391     }
    00392   }
    00393
    00394   virtual void OnPaint()
    00395   {
    00396     this->DrawWidgets();
    00397   }
    00398
    00399   virtual void OnClick(Point pt, int widget)
    00400   {
    00401     switch (widget) {
    00402       case MTSW_LIST_LEFT: { // add to playlist
    00403         int y = (pt.y - this->GetWidget<NWidgetBase>(widget)->pos_y) / FONT_HEIGHT_SMALL;
    00404
    00405         if (msf.playlist < 4) return;
    00406         if (!IsInsideMM(y, 0, BaseMusic::GetUsedSet()->num_available)) return;
    00407
    00408         byte *p = _playlists[msf.playlist];
    00409         for (uint i = 0; i != NUM_SONGS_PLAYLIST - 1; i++) {
    00410           if (p[i] == 0) {
    00411             /* Find the actual song number */
    00412             for (uint j = 0; j < NUM_SONGS_AVAILABLE; j++) {
    00413               if (GetTrackNumber(j) == y + 1) {
    00414                 p[i] = j + 1;
    00415                 break;
    00416               }
    00417             }
    00418             p[i + 1] = 0;
    00419             this->SetDirty();
    00420             SelectSongToPlay();
    00421             break;
    00422           }
    00423         }
    00424       } break;
    00425
    00426       case MTSW_LIST_RIGHT: { // remove from playlist
    00427         int y = (pt.y - this->GetWidget<NWidgetBase>(widget)->pos_y) / FONT_HEIGHT_SMALL;
    00428
    00429         if (msf.playlist < 4) return;
    00430         if (!IsInsideMM(y, 0, NUM_SONGS_PLAYLIST)) return;
    00431
    00432         byte *p = _playlists[msf.playlist];
    00433         for (uint i = y; i != NUM_SONGS_PLAYLIST - 1; i++) {
    00434           p[i] = p[i + 1];
    00435         }
    00436
    00437         this->SetDirty();
    00438         SelectSongToPlay();
    00439       } break;
    00440
    00441       case MTSW_CLEAR: // clear
    00442         for (uint i = 0; _playlists[msf.playlist][i] != 0; i++) _playlists[msf.playlist][i] = 0;
    00443         this->SetDirty();
    00444         StopMusic();
    00445         SelectSongToPlay();
    00446         break;
    00447
    00448       case MTSW_ALL: case MTSW_OLD: case MTSW_NEW:
    00449       case MTSW_EZY: case MTSW_CUSTOM1: case MTSW_CUSTOM2: // set playlist
    00450         SelectPlaylist(widget - MTSW_ALL);
    00451         StopMusic();
    00452         SelectSongToPlay();
    00453         break;
    00454     }
    00455   }
    00456 };
    00457
    00458 static const NWidgetPart _nested_music_track_selection_widgets[] = {
    00459   NWidget(NWID_HORIZONTAL),
    00460     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00461     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_PLAYLIST_MUSIC_PROGRAM_SELECTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00462   EndContainer(),
    00463   NWidget(WWT_PANEL, COLOUR_GREY),
    00464     NWidget(NWID_HORIZONTAL), SetPIP(2, 4, 2),
    00465       /* Left panel. */
    00466       NWidget(NWID_VERTICAL),
    00467         NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_PLAYLIST_TRACK_INDEX, STR_NULL),
    00468         NWidget(WWT_PANEL, COLOUR_GREY, MTSW_LIST_LEFT), SetMinimalSize(180, 194), SetDataTip(0x0, STR_PLAYLIST_TOOLTIP_CLICK_TO_ADD_TRACK), EndContainer(),
    00469         NWidget(NWID_SPACER), SetMinimalSize(0, 2),
    00470       EndContainer(),
    00471       /* Middle buttons. */
    00472       NWidget(NWID_VERTICAL),
    00473         NWidget(NWID_SPACER), SetMinimalSize(60, 30), // Space above the first button from the title bar.
    00474         NWidget(WWT_TEXTBTN, COLOUR_GREY, MTSW_ALL), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_ALL, STR_MUSIC_TOOLTIP_SELECT_ALL_TRACKS_PROGRAM),
    00475         NWidget(WWT_TEXTBTN, COLOUR_GREY, MTSW_OLD), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_OLD_STYLE, STR_MUSIC_TOOLTIP_SELECT_OLD_STYLE_MUSIC),
    00476         NWidget(WWT_TEXTBTN, COLOUR_GREY, MTSW_NEW), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_NEW_STYLE, STR_MUSIC_TOOLTIP_SELECT_NEW_STYLE_MUSIC),
    00477         NWidget(WWT_TEXTBTN, COLOUR_GREY, MTSW_EZY), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_EZY_STREET, STR_MUSIC_TOOLTIP_SELECT_EZY_STREET_STYLE),
    00478         NWidget(WWT_TEXTBTN, COLOUR_GREY, MTSW_CUSTOM1), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_CUSTOM_1, STR_MUSIC_TOOLTIP_SELECT_CUSTOM_1_USER_DEFINED),
    00479         NWidget(WWT_TEXTBTN, COLOUR_GREY, MTSW_CUSTOM2), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_CUSTOM_2, STR_MUSIC_TOOLTIP_SELECT_CUSTOM_2_USER_DEFINED),
    00480         NWidget(NWID_SPACER), SetMinimalSize(0, 16), // Space above 'clear' button
    00481         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, MTSW_CLEAR), SetFill(1, 0), SetDataTip(STR_PLAYLIST_CLEAR, STR_PLAYLIST_TOOLTIP_CLEAR_CURRENT_PROGRAM_CUSTOM1),
    00482         NWidget(NWID_SPACER), SetFill(0, 1),
    00483       EndContainer(),
    00484       /* Right panel. */
    00485       NWidget(NWID_VERTICAL),
    00486         NWidget(WWT_LABEL, COLOUR_GREY, MTSW_PLAYLIST), SetDataTip(STR_PLAYLIST_PROGRAM, STR_NULL),
    00487         NWidget(WWT_PANEL, COLOUR_GREY, MTSW_LIST_RIGHT), SetMinimalSize(180, 194), SetDataTip(0x0, STR_PLAYLIST_TOOLTIP_CLICK_TO_REMOVE_TRACK), EndContainer(),
    00488         NWidget(NWID_SPACER), SetMinimalSize(0, 2),
    00489       EndContainer(),
    00490     EndContainer(),
    00491   EndContainer(),
    00492 };
    00493
    00494 static const WindowDesc _music_track_selection_desc(
    00495   WDP_AUTO, 0, 0,
    00496   WC_MUSIC_TRACK_SELECTION, WC_NONE,
    00497   WDF_UNCLICK_BUTTONS,
    00498   _nested_music_track_selection_widgets, lengthof(_nested_music_track_selection_widgets)
    00499 );
    00500
    00501 static void ShowMusicTrackSelection()
    00502 {
    00503   AllocateWindowDescFront<MusicTrackSelectionWindow>(&_music_track_selection_desc, 0);
    00504 }
    00505
    00506 enum MusicWidgets {
    00507   MW_PREV,
    00508   MW_NEXT,
    00509   MW_STOP,
    00510   MW_PLAY,
    00511   MW_SLIDERS,
    00512   MW_MUSIC_VOL,
    00513   MW_GAUGE,
    00514   MW_EFFECT_VOL,
    00515   MW_BACKGROUND,
    00516   MW_TRACK,
    00517   MW_TRACK_NR,
    00518   MW_TRACK_TITLE,
    00519   MW_TRACK_NAME,
    00520   MW_SHUFFLE,
    00521   MW_PROGRAMME,
    00522   MW_ALL,
    00523   MW_OLD,
    00524   MW_NEW,
    00525   MW_EZY,
    00526   MW_CUSTOM1,
    00527   MW_CUSTOM2,
    00528 };
    00529
    00530 struct MusicWindow : public Window {
    00531   static const int slider_width = 3;
    00532
    00533   MusicWindow(const WindowDesc *desc, WindowNumber number) : Window()
    00534   {
    00535     this->InitNested(desc, number);
    00536     this->LowerWidget(msf.playlist + MW_ALL);
    00537     this->SetWidgetLoweredState(MW_SHUFFLE, msf.shuffle);
    00538   }
    00539
    00540   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00541   {
    00542     switch (widget) {
    00543       /* Make sure that MW_SHUFFLE and MW_PROGRAMME have the same size.
    00544        * This can't be done by using NC_EQUALSIZE as the MW_INFO is
    00545        * between those widgets and of different size. */
    00546       case MW_SHUFFLE: case MW_PROGRAMME: {
    00547         Dimension d = maxdim(GetStringBoundingBox(STR_MUSIC_PROGRAM), GetStringBoundingBox(STR_MUSIC_SHUFFLE));
    00548         d.width += padding.width;
    00549         d.height += padding.height;
    00550         *size = maxdim(*size, d);
    00551       } break;
    00552
    00553       case MW_TRACK_NR: {
    00554         Dimension d = GetStringBoundingBox(STR_MUSIC_TRACK_NONE);
    00555         d.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
    00556         d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00557         *size = maxdim(*size, d);
    00558       } break;
    00559
    00560       case MW_TRACK_NAME: {
    00561         Dimension d = GetStringBoundingBox(STR_MUSIC_TITLE_NONE);
    00562         for (uint i = 0; i < NUM_SONGS_AVAILABLE; i++) {
    00563           SetDParamStr(0, GetSongName(i));
    00564           d = maxdim(d, GetStringBoundingBox(STR_MUSIC_TITLE_NAME));
    00565         }
    00566         d.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
    00567         d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00568         *size = maxdim(*size, d);
    00569       } break;
    00570
    00571       /* Hack-ish: set the proper widget data; only needs to be done once
    00572        * per (Re)Init as that's the only time the language changes. */
    00573       case MW_PREV: this->GetWidget<NWidgetCore>(MW_PREV)->widget_data = _dynlang.text_dir == TD_RTL ? SPR_IMG_SKIP_TO_NEXT : SPR_IMG_SKIP_TO_PREV; break;
    00574       case MW_NEXT: this->GetWidget<NWidgetCore>(MW_NEXT)->widget_data = _dynlang.text_dir == TD_RTL ? SPR_IMG_SKIP_TO_PREV : SPR_IMG_SKIP_TO_NEXT; break;
    00575       case MW_PLAY: this->GetWidget<NWidgetCore>(MW_PLAY)->widget_data = _dynlang.text_dir == TD_RTL ? SPR_IMG_PLAY_MUSIC_RTL : SPR_IMG_PLAY_MUSIC; break;
    00576     }
    00577   }
    00578
    00579   virtual void DrawWidget(const Rect &r, int widget) const
    00580   {
    00581     switch (widget) {
    00582       case MW_GAUGE:
    00583         GfxFillRect(r.left, r.top, r.right, r.bottom, 0);
    00584
    00585         for (uint i = 0; i != 8; i++) {
    00586           int colour = 0xD0;
    00587           if (i > 4) {
    00588             colour = 0xBF;
    00589             if (i > 6) {
    00590               colour = 0xB8;
    00591             }
    00592           }
    00593           GfxFillRect(r.left, r.bottom - i * 2, r.right, r.bottom - i * 2, colour);
    00594         }
    00595         break;
    00596
    00597       case MW_TRACK_NR: {
    00598         GfxFillRect(r.left + 1, r.top + 1, r.right, r.bottom, 0);
    00599         StringID str = STR_MUSIC_TRACK_NONE;
    00600         if (_song_is_active != 0 && _music_wnd_cursong != 0) {
    00601           SetDParam(0, GetTrackNumber(_music_wnd_cursong - 1));
    00602           SetDParam(1, 2);
    00603           str = STR_MUSIC_TRACK_DIGIT;
    00604         }
    00605         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, str);
    00606       } break;
    00607
    00608       case MW_TRACK_NAME: {
    00609         GfxFillRect(r.left, r.top + 1, r.right - 1, r.bottom, 0);
    00610         StringID str = STR_MUSIC_TITLE_NONE;
    00611         if (_song_is_active != 0 && _music_wnd_cursong != 0) {
    00612           str = STR_MUSIC_TITLE_NAME;
    00613           SetDParamStr(0, GetSongName(_music_wnd_cursong - 1));
    00614         }
    00615         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, str, TC_FROMSTRING, SA_CENTER);
    00616       } break;
    00617
    00618       case MW_MUSIC_VOL: case MW_EFFECT_VOL: {
    00619         DrawFrameRect(r.left, r.top + 2, r.right, r.bottom - 2, COLOUR_GREY, FR_LOWERED);
    00620         byte volume = (widget == MW_MUSIC_VOL) ? msf.music_vol : msf.effect_vol;
    00621         int x = (volume * (r.right - r.left) / 127);
    00622         if (_dynlang.text_dir == TD_RTL) {
    00623           x = r.right - x;
    00624         } else {
    00625           x += r.left;
    00626         }
    00627         DrawFrameRect(x, r.top, x + slider_width, r.bottom, COLOUR_GREY, FR_NONE);
    00628       } break;
    00629     }
    00630   }
    00631
    00632   virtual void OnPaint()
    00633   {
    00634     this->DrawWidgets();
    00635   }
    00636
    00637   virtual void OnInvalidateData(int data = 0)
    00638   {
    00639     for (int i = 0; i < 6; i++) {
    00640       this->SetWidgetLoweredState(MW_ALL + i, i == msf.playlist);
    00641     }
    00642     this->SetDirty();
    00643   }
    00644
    00645   virtual void OnClick(Point pt, int widget)
    00646   {
    00647     switch (widget) {
    00648       case MW_PREV: // skip to prev
    00649         if (!_song_is_active) return;
    00650         SkipToPrevSong();
    00651         this->SetDirty();
    00652         break;
    00653
    00654       case MW_NEXT: // skip to next
    00655         if (!_song_is_active) return;
    00656         SkipToNextSong();
    00657         this->SetDirty();
    00658         break;
    00659
    00660       case MW_STOP: // stop playing
    00661         msf.playing = false;
    00662         break;
    00663
    00664       case MW_PLAY: // start playing
    00665         msf.playing = true;
    00666         break;
    00667
    00668       case MW_MUSIC_VOL: case MW_EFFECT_VOL: { // volume sliders
    00669         int x = pt.x - this->GetWidget<NWidgetBase>(widget)->pos_x;
    00670
    00671         byte *vol = (widget == MW_MUSIC_VOL) ? &msf.music_vol : &msf.effect_vol;
    00672
    00673         byte new_vol = x * 127 / this->GetWidget<NWidgetBase>(widget)->current_x;
    00674         if (_dynlang.text_dir == TD_RTL) new_vol = 127 - new_vol;
    00675         if (new_vol != *vol) {
    00676           *vol = new_vol;
    00677           if (widget == MW_MUSIC_VOL) MusicVolumeChanged(new_vol);
    00678           this->SetDirty();
    00679         }
    00680
    00681         _left_button_clicked = false;
    00682       } break;
    00683
    00684       case MW_SHUFFLE: // toggle shuffle
    00685         msf.shuffle ^= 1;
    00686         this->SetWidgetLoweredState(MW_SHUFFLE, msf.shuffle);
    00687         this->SetWidgetDirty(MW_SHUFFLE);
    00688         StopMusic();
    00689         SelectSongToPlay();
    00690         this->SetDirty();
    00691         break;
    00692
    00693       case MW_PROGRAMME: // show track selection
    00694         ShowMusicTrackSelection();
    00695         break;
    00696
    00697       case MW_ALL: case MW_OLD: case MW_NEW:
    00698       case MW_EZY: case MW_CUSTOM1: case MW_CUSTOM2: // playlist
    00699         SelectPlaylist(widget - MW_ALL);
    00700         StopMusic();
    00701         SelectSongToPlay();
    00702         this->SetDirty();
    00703         break;
    00704     }
    00705   }
    00706
    00707 #if 0
    00708   virtual void OnTick()
    00709   {
    00710     this->SetWidgetDirty(MW_GAUGE);
    00711   }
    00712 #endif
    00713 };
    00714
    00715 static const NWidgetPart _nested_music_window_widgets[] = {
    00716   NWidget(NWID_HORIZONTAL),
    00717     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00718     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_MUSIC_JAZZ_JUKEBOX_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00719   EndContainer(),
    00720
    00721   NWidget(NWID_HORIZONTAL),
    00722     NWidget(NWID_VERTICAL),
    00723       NWidget(WWT_PANEL, COLOUR_GREY, -1), SetFill(1, 1), EndContainer(),
    00724       NWidget(NWID_HORIZONTAL),
    00725         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, MW_PREV), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_SKIP_TO_PREV, STR_MUSIC_TOOLTIP_SKIP_TO_PREVIOUS_TRACK),
    00726         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, MW_NEXT), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_SKIP_TO_NEXT, STR_MUSIC_TOOLTIP_SKIP_TO_NEXT_TRACK_IN_SELECTION),
    00727         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, MW_STOP), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_STOP_MUSIC, STR_MUSIC_TOOLTIP_STOP_PLAYING_MUSIC),
    00728         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, MW_PLAY), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_PLAY_MUSIC, STR_MUSIC_TOOLTIP_START_PLAYING_MUSIC),
    00729       EndContainer(),
    00730       NWidget(WWT_PANEL, COLOUR_GREY, -1), SetFill(1, 1), EndContainer(),
    00731     EndContainer(),
    00732     NWidget(WWT_PANEL, COLOUR_GREY, MW_SLIDERS),
    00733       NWidget(NWID_HORIZONTAL), SetPIP(20, 0, 20),
    00734         NWidget(NWID_VERTICAL),
    00735           NWidget(WWT_LABEL, COLOUR_GREY, -1), SetFill(1, 0), SetDataTip(STR_MUSIC_MUSIC_VOLUME, STR_NULL),
    00736           NWidget(WWT_EMPTY, COLOUR_GREY, MW_MUSIC_VOL), SetMinimalSize(67, 0), SetMinimalTextLines(1, 0), SetFill(1, 0), SetDataTip(0x0, STR_MUSIC_TOOLTIP_DRAG_SLIDERS_TO_SET_MUSIC),
    00737           NWidget(NWID_HORIZONTAL),
    00738             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MIN, STR_NULL),
    00739             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
    00740             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
    00741             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
    00742             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
    00743             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
    00744             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MAX, STR_NULL),
    00745           EndContainer(),
    00746         EndContainer(),
    00747         NWidget(WWT_PANEL, COLOUR_GREY, MW_GAUGE), SetMinimalSize(16, 20), SetPadding(1, 11, 1, 11), SetFill(0, 0), EndContainer(),
    00748         NWidget(NWID_VERTICAL),
    00749           NWidget(WWT_LABEL, COLOUR_GREY, -1), SetFill(1, 0), SetDataTip(STR_MUSIC_EFFECTS_VOLUME, STR_NULL),
    00750           NWidget(WWT_EMPTY, COLOUR_GREY, MW_EFFECT_VOL), SetMinimalSize(67, 0), SetMinimalTextLines(1, 0), SetFill(1, 0), SetDataTip(0x0, STR_MUSIC_TOOLTIP_DRAG_SLIDERS_TO_SET_MUSIC),
    00751           NWidget(NWID_HORIZONTAL),
    00752             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MIN, STR_NULL),
    00753             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
    00754             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
    00755             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
    00756             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
    00757             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
    00758             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MAX, STR_NULL),
    00759           EndContainer(),
    00760         EndContainer(),
    00761       EndContainer(),
    00762     EndContainer(),
    00763   EndContainer(),
    00764   NWidget(WWT_PANEL, COLOUR_GREY, MW_BACKGROUND),
    00765     NWidget(NWID_HORIZONTAL), SetPIP(6, 0, 6),
    00766       NWidget(NWID_VERTICAL),
    00767         NWidget(NWID_SPACER), SetFill(0, 1),
    00768         NWidget(WWT_TEXTBTN, COLOUR_GREY, MW_SHUFFLE), SetMinimalSize(50, 8), SetDataTip(STR_MUSIC_SHUFFLE, STR_MUSIC_TOOLTIP_TOGGLE_PROGRAM_SHUFFLE),
    00769         NWidget(NWID_SPACER), SetFill(0, 1),
    00770       EndContainer(),
    00771       NWidget(NWID_VERTICAL), SetPadding(0, 0, 3, 3),
    00772         NWidget(WWT_LABEL, COLOUR_GREY, MW_TRACK), SetFill(0, 0), SetDataTip(STR_MUSIC_TRACK, STR_NULL),
    00773         NWidget(WWT_PANEL, COLOUR_GREY, MW_TRACK_NR), EndContainer(),
    00774       EndContainer(),
    00775       NWidget(NWID_VERTICAL), SetPadding(0, 3, 3, 0),
    00776         NWidget(WWT_LABEL, COLOUR_GREY, MW_TRACK_TITLE), SetFill(1, 0), SetDataTip(STR_MUSIC_XTITLE, STR_NULL),
    00777         NWidget(WWT_PANEL, COLOUR_GREY, MW_TRACK_NAME), SetFill(1, 0), EndContainer(),
    00778       EndContainer(),
    00779       NWidget(NWID_VERTICAL),
    00780         NWidget(NWID_SPACER), SetFill(0, 1),
    00781         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, MW_PROGRAMME), SetMinimalSize(50, 8), SetDataTip(STR_MUSIC_PROGRAM, STR_MUSIC_TOOLTIP_SHOW_MUSIC_TRACK_SELECTION),
    00782         NWidget(NWID_SPACER), SetFill(0, 1),
    00783       EndContainer(),
    00784     EndContainer(),
    00785   EndContainer(),
    00786   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00787     NWidget(WWT_TEXTBTN, COLOUR_GREY, MW_ALL), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_ALL, STR_MUSIC_TOOLTIP_SELECT_ALL_TRACKS_PROGRAM),
    00788     NWidget(WWT_TEXTBTN, COLOUR_GREY, MW_OLD), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_OLD_STYLE, STR_MUSIC_TOOLTIP_SELECT_OLD_STYLE_MUSIC),
    00789     NWidget(WWT_TEXTBTN, COLOUR_GREY, MW_NEW), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_NEW_STYLE, STR_MUSIC_TOOLTIP_SELECT_NEW_STYLE_MUSIC),
    00790     NWidget(WWT_TEXTBTN, COLOUR_GREY, MW_EZY), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_EZY_STREET, STR_MUSIC_TOOLTIP_SELECT_EZY_STREET_STYLE),
    00791     NWidget(WWT_TEXTBTN, COLOUR_GREY, MW_CUSTOM1), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_CUSTOM_1, STR_MUSIC_TOOLTIP_SELECT_CUSTOM_1_USER_DEFINED),
    00792     NWidget(WWT_TEXTBTN, COLOUR_GREY, MW_CUSTOM2), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_CUSTOM_2, STR_MUSIC_TOOLTIP_SELECT_CUSTOM_2_USER_DEFINED),
    00793   EndContainer(),
    00794 };
    00795
    00796 static const WindowDesc _music_window_desc(
    00797   WDP_AUTO, 0, 0,
    00798   WC_MUSIC_WINDOW, WC_NONE,
    00799   WDF_UNCLICK_BUTTONS,
    00800   _nested_music_window_widgets, lengthof(_nested_music_window_widgets)
    00801 );
    00802
    00803 void ShowMusicWindow()
    00804 {
    00805   if (BaseMusic::GetUsedSet()->num_available == 0) ShowErrorMessage(STR_ERROR_NO_SONGS, INVALID_STRING_ID, 0, 0);
    00806   AllocateWindowDescFront<MusicWindow>(&_music_window_desc, 0);
    00807 }