# music_gui.cpp

Go to the documentation of this file.

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