# music_gui.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    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
    00320   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00321   {
    00322     if (!gui_scope) return;
    00323     for (int i = 0; i < 6; i++) {
    00324       this->SetWidgetLoweredState(MTSW_ALL + i, i == _msf.playlist);
    00325     }
    00326     this->SetWidgetDisabledState(MTSW_CLEAR, _msf.playlist <= 3);
    00327     this->SetDirty();
    00328   }
    00329
    00330   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00331   {
    00332     switch (widget) {
    00333       case MTSW_PLAYLIST: {
    00334         Dimension d = {0, 0};
    00335
    00336         for (int i = 0; i < 6; i++) {
    00337           SetDParam(0, STR_MUSIC_PLAYLIST_ALL + i);
    00338           d = maxdim(d, GetStringBoundingBox(STR_PLAYLIST_PROGRAM));
    00339         }
    00340         d.width += padding.width;
    00341         d.height += padding.height;
    00342         *size = maxdim(*size, d);
    00343         break;
    00344       }
    00345
    00346       case MTSW_LIST_LEFT: case MTSW_LIST_RIGHT: {
    00347         Dimension d = {0, 0};
    00348
    00349         for (uint i = 0; i < NUM_SONGS_AVAILABLE; i++) {
    00350           const char *song_name = GetSongName(i);
    00351           if (StrEmpty(song_name)) continue;
    00352
    00353           SetDParam(0, GetTrackNumber(i));
    00354           SetDParam(1, 2);
    00355           SetDParamStr(2, GetSongName(i));
    00356           Dimension d2 = GetStringBoundingBox(STR_PLAYLIST_TRACK_NAME);
    00357           d.width = max(d.width, d2.width);
    00358           d.height += d2.height;
    00359         }
    00360         d.width += padding.width;
    00361         d.height += padding.height;
    00362         *size = maxdim(*size, d);
    00363         break;
    00364       }
    00365     }
    00366   }
    00367
    00368   virtual void DrawWidget(const Rect &r, int widget) const
    00369   {
    00370     switch (widget) {
    00371       case MTSW_LIST_LEFT: {
    00372         GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, 0);
    00373
    00374         int y = r.top + WD_FRAMERECT_TOP;
    00375         for (uint i = 0; i < NUM_SONGS_AVAILABLE; i++) {
    00376           const char *song_name = GetSongName(i);
    00377           if (StrEmpty(song_name)) continue;
    00378
    00379           SetDParam(0, GetTrackNumber(i));
    00380           SetDParam(1, 2);
    00381           SetDParamStr(2, song_name);
    00382           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_PLAYLIST_TRACK_NAME);
    00383           y += FONT_HEIGHT_SMALL;
    00384         }
    00385         break;
    00386       }
    00387
    00388       case MTSW_LIST_RIGHT: {
    00389         GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, 0);
    00390
    00391         int y = r.top + WD_FRAMERECT_TOP;
    00392         for (const byte *p = _playlists[_msf.playlist]; *p != 0; p++) {
    00393           uint i = *p - 1;
    00394           SetDParam(0, GetTrackNumber(i));
    00395           SetDParam(1, 2);
    00396           SetDParamStr(2, GetSongName(i));
    00397           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_PLAYLIST_TRACK_NAME);
    00398           y += FONT_HEIGHT_SMALL;
    00399         }
    00400         break;
    00401       }
    00402     }
    00403   }
    00404
    00405   virtual void OnClick(Point pt, int widget, int click_count)
    00406   {
    00407     switch (widget) {
    00408       case MTSW_LIST_LEFT: { // add to playlist
    00409         int y = this->GetRowFromWidget(pt.y, widget, 0, FONT_HEIGHT_SMALL);
    00410
    00411         if (_msf.playlist < 4) return;
    00412         if (!IsInsideMM(y, 0, BaseMusic::GetUsedSet()->num_available)) return;
    00413
    00414         byte *p = _playlists[_msf.playlist];
    00415         for (uint i = 0; i != NUM_SONGS_PLAYLIST - 1; i++) {
    00416           if (p[i] == 0) {
    00417             /* Find the actual song number */
    00418             for (uint j = 0; j < NUM_SONGS_AVAILABLE; j++) {
    00419               if (GetTrackNumber(j) == y + 1) {
    00420                 p[i] = j + 1;
    00421                 break;
    00422               }
    00423             }
    00424             p[i + 1] = 0;
    00425             this->SetDirty();
    00426             SelectSongToPlay();
    00427             break;
    00428           }
    00429         }
    00430         break;
    00431       }
    00432
    00433       case MTSW_LIST_RIGHT: { // remove from playlist
    00434         int y = this->GetRowFromWidget(pt.y, widget, 0, FONT_HEIGHT_SMALL);
    00435
    00436         if (_msf.playlist < 4) return;
    00437         if (!IsInsideMM(y, 0, NUM_SONGS_PLAYLIST)) return;
    00438
    00439         byte *p = _playlists[_msf.playlist];
    00440         for (uint i = y; i != NUM_SONGS_PLAYLIST - 1; i++) {
    00441           p[i] = p[i + 1];
    00442         }
    00443
    00444         this->SetDirty();
    00445         SelectSongToPlay();
    00446         break;
    00447       }
    00448
    00449       case MTSW_CLEAR: // clear
    00450         for (uint i = 0; _playlists[_msf.playlist][i] != 0; i++) _playlists[_msf.playlist][i] = 0;
    00451         this->SetDirty();
    00452         StopMusic();
    00453         SelectSongToPlay();
    00454         break;
    00455
    00456       case MTSW_ALL: case MTSW_OLD: case MTSW_NEW:
    00457       case MTSW_EZY: case MTSW_CUSTOM1: case MTSW_CUSTOM2: // set playlist
    00458         SelectPlaylist(widget - MTSW_ALL);
    00459         StopMusic();
    00460         SelectSongToPlay();
    00461         break;
    00462     }
    00463   }
    00464 };
    00465
    00466 static const NWidgetPart _nested_music_track_selection_widgets[] = {
    00467   NWidget(NWID_HORIZONTAL),
    00468     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00469     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_PLAYLIST_MUSIC_PROGRAM_SELECTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00470   EndContainer(),
    00471   NWidget(WWT_PANEL, COLOUR_GREY),
    00472     NWidget(NWID_HORIZONTAL), SetPIP(2, 4, 2),
    00473       /* Left panel. */
    00474       NWidget(NWID_VERTICAL),
    00475         NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_PLAYLIST_TRACK_INDEX, STR_NULL),
    00476         NWidget(WWT_PANEL, COLOUR_GREY, MTSW_LIST_LEFT), SetMinimalSize(180, 194), SetDataTip(0x0, STR_PLAYLIST_TOOLTIP_CLICK_TO_ADD_TRACK), EndContainer(),
    00477         NWidget(NWID_SPACER), SetMinimalSize(0, 2),
    00478       EndContainer(),
    00479       /* Middle buttons. */
    00480       NWidget(NWID_VERTICAL),
    00481         NWidget(NWID_SPACER), SetMinimalSize(60, 30), // Space above the first button from the title bar.
    00482         NWidget(WWT_TEXTBTN, COLOUR_GREY, MTSW_ALL), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_ALL, STR_MUSIC_TOOLTIP_SELECT_ALL_TRACKS_PROGRAM),
    00483         NWidget(WWT_TEXTBTN, COLOUR_GREY, MTSW_OLD), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_OLD_STYLE, STR_MUSIC_TOOLTIP_SELECT_OLD_STYLE_MUSIC),
    00484         NWidget(WWT_TEXTBTN, COLOUR_GREY, MTSW_NEW), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_NEW_STYLE, STR_MUSIC_TOOLTIP_SELECT_NEW_STYLE_MUSIC),
    00485         NWidget(WWT_TEXTBTN, COLOUR_GREY, MTSW_EZY), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_EZY_STREET, STR_MUSIC_TOOLTIP_SELECT_EZY_STREET_STYLE),
    00486         NWidget(WWT_TEXTBTN, COLOUR_GREY, MTSW_CUSTOM1), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_CUSTOM_1, STR_MUSIC_TOOLTIP_SELECT_CUSTOM_1_USER_DEFINED),
    00487         NWidget(WWT_TEXTBTN, COLOUR_GREY, MTSW_CUSTOM2), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_CUSTOM_2, STR_MUSIC_TOOLTIP_SELECT_CUSTOM_2_USER_DEFINED),
    00488         NWidget(NWID_SPACER), SetMinimalSize(0, 16), // Space above 'clear' button
    00489         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, MTSW_CLEAR), SetFill(1, 0), SetDataTip(STR_PLAYLIST_CLEAR, STR_PLAYLIST_TOOLTIP_CLEAR_CURRENT_PROGRAM_CUSTOM1),
    00490         NWidget(NWID_SPACER), SetFill(0, 1),
    00491       EndContainer(),
    00492       /* Right panel. */
    00493       NWidget(NWID_VERTICAL),
    00494         NWidget(WWT_LABEL, COLOUR_GREY, MTSW_PLAYLIST), SetDataTip(STR_PLAYLIST_PROGRAM, STR_NULL),
    00495         NWidget(WWT_PANEL, COLOUR_GREY, MTSW_LIST_RIGHT), SetMinimalSize(180, 194), SetDataTip(0x0, STR_PLAYLIST_TOOLTIP_CLICK_TO_REMOVE_TRACK), EndContainer(),
    00496         NWidget(NWID_SPACER), SetMinimalSize(0, 2),
    00497       EndContainer(),
    00498     EndContainer(),
    00499   EndContainer(),
    00500 };
    00501
    00502 static const WindowDesc _music_track_selection_desc(
    00503   WDP_AUTO, 0, 0,
    00504   WC_MUSIC_TRACK_SELECTION, WC_NONE,
    00505   WDF_UNCLICK_BUTTONS,
    00506   _nested_music_track_selection_widgets, lengthof(_nested_music_track_selection_widgets)
    00507 );
    00508
    00509 static void ShowMusicTrackSelection()
    00510 {
    00511   AllocateWindowDescFront<MusicTrackSelectionWindow>(&_music_track_selection_desc, 0);
    00512 }
    00513
    00514 enum MusicWidgets {
    00515   MW_PREV,
    00516   MW_NEXT,
    00517   MW_STOP,
    00518   MW_PLAY,
    00519   MW_SLIDERS,
    00520   MW_MUSIC_VOL,
    00521   MW_GAUGE,
    00522   MW_EFFECT_VOL,
    00523   MW_BACKGROUND,
    00524   MW_TRACK,
    00525   MW_TRACK_NR,
    00526   MW_TRACK_TITLE,
    00527   MW_TRACK_NAME,
    00528   MW_SHUFFLE,
    00529   MW_PROGRAMME,
    00530   MW_ALL,
    00531   MW_OLD,
    00532   MW_NEW,
    00533   MW_EZY,
    00534   MW_CUSTOM1,
    00535   MW_CUSTOM2,
    00536 };
    00537
    00538 struct MusicWindow : public Window {
    00539   static const int slider_width = 3;
    00540
    00541   MusicWindow(const WindowDesc *desc, WindowNumber number) : Window()
    00542   {
    00543     this->InitNested(desc, number);
    00544     this->LowerWidget(_msf.playlist + MW_ALL);
    00545     this->SetWidgetLoweredState(MW_SHUFFLE, _msf.shuffle);
    00546   }
    00547
    00548   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00549   {
    00550     switch (widget) {
    00551       /* Make sure that MW_SHUFFLE and MW_PROGRAMME have the same size.
    00552        * This can't be done by using NC_EQUALSIZE as the MW_INFO is
    00553        * between those widgets and of different size. */
    00554       case MW_SHUFFLE: case MW_PROGRAMME: {
    00555         Dimension d = maxdim(GetStringBoundingBox(STR_MUSIC_PROGRAM), GetStringBoundingBox(STR_MUSIC_SHUFFLE));
    00556         d.width += padding.width;
    00557         d.height += padding.height;
    00558         *size = maxdim(*size, d);
    00559         break;
    00560       }
    00561
    00562       case MW_TRACK_NR: {
    00563         Dimension d = GetStringBoundingBox(STR_MUSIC_TRACK_NONE);
    00564         d.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
    00565         d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00566         *size = maxdim(*size, d);
    00567         break;
    00568       }
    00569
    00570       case MW_TRACK_NAME: {
    00571         Dimension d = GetStringBoundingBox(STR_MUSIC_TITLE_NONE);
    00572         for (uint i = 0; i < NUM_SONGS_AVAILABLE; i++) {
    00573           SetDParamStr(0, GetSongName(i));
    00574           d = maxdim(d, GetStringBoundingBox(STR_MUSIC_TITLE_NAME));
    00575         }
    00576         d.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
    00577         d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00578         *size = maxdim(*size, d);
    00579         break;
    00580       }
    00581
    00582       /* Hack-ish: set the proper widget data; only needs to be done once
    00583        * per (Re)Init as that's the only time the language changes. */
    00584       case MW_PREV: this->GetWidget<NWidgetCore>(MW_PREV)->widget_data = _current_text_dir == TD_RTL ? SPR_IMG_SKIP_TO_NEXT : SPR_IMG_SKIP_TO_PREV; break;
    00585       case MW_NEXT: this->GetWidget<NWidgetCore>(MW_NEXT)->widget_data = _current_text_dir == TD_RTL ? SPR_IMG_SKIP_TO_PREV : SPR_IMG_SKIP_TO_NEXT; break;
    00586       case MW_PLAY: this->GetWidget<NWidgetCore>(MW_PLAY)->widget_data = _current_text_dir == TD_RTL ? SPR_IMG_PLAY_MUSIC_RTL : SPR_IMG_PLAY_MUSIC; break;
    00587     }
    00588   }
    00589
    00590   virtual void DrawWidget(const Rect &r, int widget) const
    00591   {
    00592     switch (widget) {
    00593       case MW_GAUGE:
    00594         GfxFillRect(r.left, r.top, r.right, r.bottom, 0);
    00595
    00596         for (uint i = 0; i != 8; i++) {
    00597           int colour = 0xD0;
    00598           if (i > 4) {
    00599             colour = 0xBF;
    00600             if (i > 6) {
    00601               colour = 0xB8;
    00602             }
    00603           }
    00604           GfxFillRect(r.left, r.bottom - i * 2, r.right, r.bottom - i * 2, colour);
    00605         }
    00606         break;
    00607
    00608       case MW_TRACK_NR: {
    00609         GfxFillRect(r.left + 1, r.top + 1, r.right, r.bottom, 0);
    00610         StringID str = STR_MUSIC_TRACK_NONE;
    00611         if (_song_is_active != 0 && _music_wnd_cursong != 0) {
    00612           SetDParam(0, GetTrackNumber(_music_wnd_cursong - 1));
    00613           SetDParam(1, 2);
    00614           str = STR_MUSIC_TRACK_DIGIT;
    00615         }
    00616         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, str);
    00617         break;
    00618       }
    00619
    00620       case MW_TRACK_NAME: {
    00621         GfxFillRect(r.left, r.top + 1, r.right - 1, r.bottom, 0);
    00622         StringID str = STR_MUSIC_TITLE_NONE;
    00623         if (_song_is_active != 0 && _music_wnd_cursong != 0) {
    00624           str = STR_MUSIC_TITLE_NAME;
    00625           SetDParamStr(0, GetSongName(_music_wnd_cursong - 1));
    00626         }
    00627         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, str, TC_FROMSTRING, SA_HOR_CENTER);
    00628         break;
    00629       }
    00630
    00631       case MW_MUSIC_VOL: case MW_EFFECT_VOL: {
    00632         DrawFrameRect(r.left, r.top + 2, r.right, r.bottom - 2, COLOUR_GREY, FR_LOWERED);
    00633         byte volume = (widget == MW_MUSIC_VOL) ? _msf.music_vol : _msf.effect_vol;
    00634         int x = (volume * (r.right - r.left) / 127);
    00635         if (_current_text_dir == TD_RTL) {
    00636           x = r.right - x;
    00637         } else {
    00638           x += r.left;
    00639         }
    00640         DrawFrameRect(x, r.top, x + slider_width, r.bottom, COLOUR_GREY, FR_NONE);
    00641         break;
    00642       }
    00643     }
    00644   }
    00645
    00651   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00652   {
    00653     if (!gui_scope) return;
    00654     for (int i = 0; i < 6; i++) {
    00655       this->SetWidgetLoweredState(MW_ALL + i, i == _msf.playlist);
    00656     }
    00657     this->SetDirty();
    00658   }
    00659
    00660   virtual void OnClick(Point pt, int widget, int click_count)
    00661   {
    00662     switch (widget) {
    00663       case MW_PREV: // skip to prev
    00664         if (!_song_is_active) return;
    00665         SkipToPrevSong();
    00666         this->SetDirty();
    00667         break;
    00668
    00669       case MW_NEXT: // skip to next
    00670         if (!_song_is_active) return;
    00671         SkipToNextSong();
    00672         this->SetDirty();
    00673         break;
    00674
    00675       case MW_STOP: // stop playing
    00676         _msf.playing = false;
    00677         break;
    00678
    00679       case MW_PLAY: // start playing
    00680         _msf.playing = true;
    00681         break;
    00682
    00683       case MW_MUSIC_VOL: case MW_EFFECT_VOL: { // volume sliders
    00684         int x = pt.x - this->GetWidget<NWidgetBase>(widget)->pos_x;
    00685
    00686         byte *vol = (widget == MW_MUSIC_VOL) ? &_msf.music_vol : &_msf.effect_vol;
    00687
    00688         byte new_vol = x * 127 / this->GetWidget<NWidgetBase>(widget)->current_x;
    00689         if (_current_text_dir == TD_RTL) new_vol = 127 - new_vol;
    00690         if (new_vol != *vol) {
    00691           *vol = new_vol;
    00692           if (widget == MW_MUSIC_VOL) MusicVolumeChanged(new_vol);
    00693           this->SetDirty();
    00694         }
    00695
    00696         _left_button_clicked = false;
    00697         break;
    00698       }
    00699
    00700       case MW_SHUFFLE: // toggle shuffle
    00701         _msf.shuffle ^= 1;
    00702         this->SetWidgetLoweredState(MW_SHUFFLE, _msf.shuffle);
    00703         this->SetWidgetDirty(MW_SHUFFLE);
    00704         StopMusic();
    00705         SelectSongToPlay();
    00706         this->SetDirty();
    00707         break;
    00708
    00709       case MW_PROGRAMME: // show track selection
    00710         ShowMusicTrackSelection();
    00711         break;
    00712
    00713       case MW_ALL: case MW_OLD: case MW_NEW:
    00714       case MW_EZY: case MW_CUSTOM1: case MW_CUSTOM2: // playlist
    00715         SelectPlaylist(widget - MW_ALL);
    00716         StopMusic();
    00717         SelectSongToPlay();
    00718         this->SetDirty();
    00719         break;
    00720     }
    00721   }
    00722 };
    00723
    00724 static const NWidgetPart _nested_music_window_widgets[] = {
    00725   NWidget(NWID_HORIZONTAL),
    00726     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00727     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_MUSIC_JAZZ_JUKEBOX_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00728   EndContainer(),
    00729
    00730   NWidget(NWID_HORIZONTAL),
    00731     NWidget(NWID_VERTICAL),
    00732       NWidget(WWT_PANEL, COLOUR_GREY, -1), SetFill(1, 1), EndContainer(),
    00733       NWidget(NWID_HORIZONTAL),
    00734         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, MW_PREV), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_SKIP_TO_PREV, STR_MUSIC_TOOLTIP_SKIP_TO_PREVIOUS_TRACK),
    00735         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),
    00736         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, MW_STOP), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_STOP_MUSIC, STR_MUSIC_TOOLTIP_STOP_PLAYING_MUSIC),
    00737         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, MW_PLAY), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_PLAY_MUSIC, STR_MUSIC_TOOLTIP_START_PLAYING_MUSIC),
    00738       EndContainer(),
    00739       NWidget(WWT_PANEL, COLOUR_GREY, -1), SetFill(1, 1), EndContainer(),
    00740     EndContainer(),
    00741     NWidget(WWT_PANEL, COLOUR_GREY, MW_SLIDERS),
    00742       NWidget(NWID_HORIZONTAL), SetPIP(20, 0, 20),
    00743         NWidget(NWID_VERTICAL),
    00744           NWidget(WWT_LABEL, COLOUR_GREY, -1), SetFill(1, 0), SetDataTip(STR_MUSIC_MUSIC_VOLUME, STR_NULL),
    00745           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),
    00746           NWidget(NWID_HORIZONTAL),
    00747             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MIN, STR_NULL),
    00748             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
    00749             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
    00750             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
    00751             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
    00752             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
    00753             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MAX, STR_NULL),
    00754           EndContainer(),
    00755         EndContainer(),
    00756         NWidget(WWT_PANEL, COLOUR_GREY, MW_GAUGE), SetMinimalSize(16, 20), SetPadding(1, 11, 1, 11), SetFill(0, 0), EndContainer(),
    00757         NWidget(NWID_VERTICAL),
    00758           NWidget(WWT_LABEL, COLOUR_GREY, -1), SetFill(1, 0), SetDataTip(STR_MUSIC_EFFECTS_VOLUME, STR_NULL),
    00759           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),
    00760           NWidget(NWID_HORIZONTAL),
    00761             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MIN, STR_NULL),
    00762             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
    00763             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
    00764             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
    00765             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
    00766             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MARKER, STR_NULL), SetFill(1, 0),
    00767             NWidget(WWT_LABEL, COLOUR_GREY, -1), SetDataTip(STR_MUSIC_RULER_MAX, STR_NULL),
    00768           EndContainer(),
    00769         EndContainer(),
    00770       EndContainer(),
    00771     EndContainer(),
    00772   EndContainer(),
    00773   NWidget(WWT_PANEL, COLOUR_GREY, MW_BACKGROUND),
    00774     NWidget(NWID_HORIZONTAL), SetPIP(6, 0, 6),
    00775       NWidget(NWID_VERTICAL),
    00776         NWidget(NWID_SPACER), SetFill(0, 1),
    00777         NWidget(WWT_TEXTBTN, COLOUR_GREY, MW_SHUFFLE), SetMinimalSize(50, 8), SetDataTip(STR_MUSIC_SHUFFLE, STR_MUSIC_TOOLTIP_TOGGLE_PROGRAM_SHUFFLE),
    00778         NWidget(NWID_SPACER), SetFill(0, 1),
    00779       EndContainer(),
    00780       NWidget(NWID_VERTICAL), SetPadding(0, 0, 3, 3),
    00781         NWidget(WWT_LABEL, COLOUR_GREY, MW_TRACK), SetFill(0, 0), SetDataTip(STR_MUSIC_TRACK, STR_NULL),
    00782         NWidget(WWT_PANEL, COLOUR_GREY, MW_TRACK_NR), EndContainer(),
    00783       EndContainer(),
    00784       NWidget(NWID_VERTICAL), SetPadding(0, 3, 3, 0),
    00785         NWidget(WWT_LABEL, COLOUR_GREY, MW_TRACK_TITLE), SetFill(1, 0), SetDataTip(STR_MUSIC_XTITLE, STR_NULL),
    00786         NWidget(WWT_PANEL, COLOUR_GREY, MW_TRACK_NAME), SetFill(1, 0), EndContainer(),
    00787       EndContainer(),
    00788       NWidget(NWID_VERTICAL),
    00789         NWidget(NWID_SPACER), SetFill(0, 1),
    00790         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, MW_PROGRAMME), SetMinimalSize(50, 8), SetDataTip(STR_MUSIC_PROGRAM, STR_MUSIC_TOOLTIP_SHOW_MUSIC_TRACK_SELECTION),
    00791         NWidget(NWID_SPACER), SetFill(0, 1),
    00792       EndContainer(),
    00793     EndContainer(),
    00794   EndContainer(),
    00795   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00796     NWidget(WWT_TEXTBTN, COLOUR_GREY, MW_ALL), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_ALL, STR_MUSIC_TOOLTIP_SELECT_ALL_TRACKS_PROGRAM),
    00797     NWidget(WWT_TEXTBTN, COLOUR_GREY, MW_OLD), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_OLD_STYLE, STR_MUSIC_TOOLTIP_SELECT_OLD_STYLE_MUSIC),
    00798     NWidget(WWT_TEXTBTN, COLOUR_GREY, MW_NEW), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_NEW_STYLE, STR_MUSIC_TOOLTIP_SELECT_NEW_STYLE_MUSIC),
    00799     NWidget(WWT_TEXTBTN, COLOUR_GREY, MW_EZY), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_EZY_STREET, STR_MUSIC_TOOLTIP_SELECT_EZY_STREET_STYLE),
    00800     NWidget(WWT_TEXTBTN, COLOUR_GREY, MW_CUSTOM1), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_CUSTOM_1, STR_MUSIC_TOOLTIP_SELECT_CUSTOM_1_USER_DEFINED),
    00801     NWidget(WWT_TEXTBTN, COLOUR_GREY, MW_CUSTOM2), SetFill(1, 0), SetDataTip(STR_MUSIC_PLAYLIST_CUSTOM_2, STR_MUSIC_TOOLTIP_SELECT_CUSTOM_2_USER_DEFINED),
    00802   EndContainer(),
    00803 };
    00804
    00805 static const WindowDesc _music_window_desc(
    00806   WDP_AUTO, 0, 0,
    00807   WC_MUSIC_WINDOW, WC_NONE,
    00808   WDF_UNCLICK_BUTTONS,
    00809   _nested_music_window_widgets, lengthof(_nested_music_window_widgets)
    00810 );
    00811
    00812 void ShowMusicWindow()
    00813 {
    00814   if (BaseMusic::GetUsedSet()->num_available == 0) ShowErrorMessage(STR_ERROR_NO_SONGS, INVALID_STRING_ID, WL_WARNING);
    00815   AllocateWindowDescFront<MusicWindow>(&_music_window_desc, 0);
    00816 }