vdr  1.7.27
menu.c
Go to the documentation of this file.
00001 /*
00002  * menu.c: A menu for still pictures
00003  *
00004  * See the README file for copyright information and how to reach the author.
00005  *
00006  * $Id: menu.c 2.0 2008/01/13 11:35:18 kls Exp $
00007  */
00008 
00009 #include "menu.h"
00010 #include <vdr/tools.h>
00011 #include "entry.h"
00012 #include "player.h"
00013 
00014 char PictureDirectory[PATH_MAX] = "";
00015 
00016 static bool PathStartsWith(const char *Path, const char *Name)
00017 {
00018   if (Path && Name) {
00019      while (*Name) {
00020            if (*Path++ != *Name++)
00021               return false;
00022            }
00023      if (*Path && *Path != '/')
00024         return false;
00025      return true;
00026      }
00027   return false;
00028 }
00029 
00030 static const char *NextLevel(const char *Path)
00031 {
00032   if (Path) {
00033      const char *p = strchr(Path, '/');
00034      return p ? p + 1 : NULL;
00035      }
00036   return Path;
00037 }
00038 
00039 cPictureEntry *cPictureMenu::pictures = NULL;
00040 
00041 cPictureMenu::cPictureMenu(const cPictureEntry *PictureEntry, const char *Path)
00042 :cOsdMenu(tr("Pictures"))
00043 {
00044   pictureEntry = PictureEntry;
00045   if (!pictureEntry)
00046      pictureEntry = pictures = new cPictureEntry(PictureDirectory, NULL, true);
00047   if (pictureEntry->Parent()) {
00048      if (!pictureEntry->Parent()->Parent())
00049         SetTitle(pictureEntry->Name()); // Year
00050      else
00051         SetTitle(cString::sprintf("%s: %s", pictureEntry->Parent()->Name(), *HandleUnderscores(pictureEntry->Name()))); // Year/Description
00052      }
00053   Set(Path);
00054 }
00055 
00056 cPictureMenu::~cPictureMenu()
00057 {
00058   if (pictures && pictureEntry && !pictureEntry->Parent())
00059      DELETENULL(pictures);
00060 }
00061 
00062 void cPictureMenu::Set(const char *Path)
00063 {
00064   Clear();
00065   const cList<cPictureEntry> *l = pictureEntry->Entries();
00066   if (l) {
00067      for (const cPictureEntry *e = l->First(); e; e = l->Next(e)) {
00068          cString Name = HandleUnderscores(e->Name());
00069          if (!e->IsDirectory())
00070             Name.Truncate(-4); // don't display the ".mpg" extension
00071          Add(new cOsdItem(HandleUnderscores(Name)), PathStartsWith(Path, e->Name()));
00072          }
00073      }
00074   SetHelp(Count() ? trVDR("Button$Play") : NULL, NULL, NULL, cPictureControl::Active() ? trVDR("Button$Stop") : NULL);
00075   if (Current() >= 0) {
00076      const char *p = NextLevel(Path);
00077      if (p)
00078         SelectItem(p);
00079      }
00080 }
00081 
00082 eOSState cPictureMenu::SelectItem(const char *Path, bool SlideShow)
00083 {
00084   cOsdItem *Item = Get(Current());
00085   if (Item) {
00086      const cList<cPictureEntry> *l = pictureEntry->Entries();
00087      if (l) {
00088         cPictureEntry *pe = l->Get(Current());
00089         if (pe) {
00090            if (SlideShow) {
00091               cControl::Launch(new cPictureControl(pictures, pe, true));
00092               pictures = NULL; // cPictureControl takes ownership
00093               return osEnd;
00094               }
00095            if (pe->IsDirectory())
00096               return AddSubMenu(new cPictureMenu(pe, Path));
00097            else if (!Path) {
00098               cControl::Launch(new cPictureControl(pictures, pe));
00099               pictures = NULL; // cPictureControl takes ownership
00100               return osEnd;
00101               }
00102            }
00103         }
00104      }
00105   return osContinue;
00106 }
00107 
00108 eOSState cPictureMenu::ProcessKey(eKeys Key)
00109 {
00110   eOSState state = cOsdMenu::ProcessKey(Key);
00111   if (state == osUnknown) {
00112      switch (Key) {
00113        case kRed:
00114        case kPlay:   return SelectItem(NULL, true);
00115        case kBlue:
00116        case kStop:   if (cPictureControl::Active())
00117                         return osStopReplay;
00118                      break;
00119        case kOk:     return SelectItem();
00120        default:      break;
00121        }
00122      }
00123   return state;
00124 }
00125 
00126 cPictureMenu *cPictureMenu::CreatePictureMenu(void)
00127 {
00128   return new cPictureMenu(NULL, cPictureControl::LastDisplayed());
00129 }