00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "list.h"
00022 #include <vdr/i18n.h>
00023 #include <vdr/config.h>
00024 #include <vdr/osdbase.h>
00025 #include <vdr/osd.h>
00026 #include <vdr/menuitems.h>
00027 #include <vdr/interface.h>
00028 #include <assert.h>
00029
00030 using namespace SudokuPlugin;
00031
00032
00033
00034
00035
00036 ListLine::ListLine()
00037 {
00038 }
00039
00040
00041 ListLine::ListLine(const char* sudoku, const char* description) :
00042 sudoku(sudoku), description(description)
00043 {
00044 assert(sudoku != NULL);
00045 assert(description != NULL);
00046 }
00047
00048
00049 const char* ListLine::get_sudoku() const
00050 {
00051 assert((const char*)sudoku != NULL);
00052 return sudoku;
00053 }
00054
00055
00056 const char* ListLine::get_description() const
00057 {
00058 assert((const char*)description != NULL);
00059 return description;
00060 }
00061
00062
00063 void ListLine::set_description(const char* new_description)
00064 {
00065 assert(new_description != NULL);
00066 description = skipspace(new_description);
00067 }
00068
00069
00070 bool ListLine::Parse(const char* line)
00071 {
00072 const char *delim = strchr(line, ' ');
00073 if (delim)
00074 {
00075 sudoku = cString(strndup(line, delim - line), true);
00076 description = skipspace(delim);
00077 }
00078 else
00079 {
00080 sudoku = line;
00081 description = "";
00082 }
00083 assert((const char*)sudoku != NULL);
00084 assert((const char*)description != NULL);
00085 return !isempty(sudoku);
00086 }
00087
00088
00089 bool ListLine::Save(FILE* file) const
00090 {
00091 return fprintf(file, "%s %s\n", *sudoku, *description) > 0;
00092 }
00093
00094
00095
00096
00097
00098 ListMenu::ListMenu(const char* filename, const char* new_sudoku) :
00099 cOsdMenu(tr("Sudoku list")), selected(-1), save_mode(new_sudoku)
00100 {
00101 list.Load(filename);
00102 if (save_mode)
00103 list.Add(new ListLine(new_sudoku));
00104 refresh();
00105 if (save_mode)
00106 {
00107 SetCurrent(Last());
00108 AddSubMenu(new ListEdit(*list.Get(Current())));
00109 ProcessKey(kRight);
00110 }
00111 }
00112
00113
00114 const char* ListMenu::get_selected_sudoku() const
00115 {
00116 ListLine* line = list.Get(selected);
00117 if (line)
00118 return line->get_sudoku();
00119 return NULL;
00120 }
00121
00122
00123 eOSState ListMenu::ProcessKey(eKeys key)
00124 {
00125 bool had_submenu = HasSubMenu();
00126 if (key == kBlue && !HasSubMenu())
00127 key = kBack;
00128 if (key == kGreen && !HasSubMenu() && !save_mode)
00129 key = kOk;
00130 eOSState state = cOsdMenu::ProcessKey(key);
00131 if (state == osUnknown)
00132 {
00133 state = osContinue;
00134 switch (key)
00135 {
00136 case kRed:
00137 if (Current() >= 0 && Current() < list.Count())
00138 return AddSubMenu(new ListEdit(*list.Get(Current())));
00139 break;
00140 case kYellow:
00141 if (Current() >= 0 && Current() < list.Count() &&
00142 Interface->Confirm(tr("Delete the puzzle?")))
00143 list.Del(list.Get(Current()));
00144 refresh();
00145 list.Save();
00146 break;
00147 case kOk:
00148 if (Current() >= 0 && Current() < list.Count() && !save_mode)
00149 selected = Current();
00150 state = osBack;
00151 break;
00152 default:
00153 break;
00154 }
00155 }
00156 if (had_submenu && !HasSubMenu())
00157 {
00158 refresh();
00159 list.Save();
00160 }
00161 return state;
00162 }
00163
00164
00165 void ListMenu::refresh()
00166 {
00167 int current = Current();
00168 Clear();
00169 SetHasHotkeys();
00170 for (ListLine* line = list.First(); line; line = list.Next(line))
00171 Add(new cOsdItem(hk(line->get_description())));
00172 SetCurrent(Get(current));
00173 SetHelp(trVDR("Button$Edit"), save_mode ? NULL : tr("Button$Load"),
00174 trVDR("Button$Delete"), tr("Button$Back"));
00175 Display();
00176 }
00177
00178
00179
00180
00181
00182 ListEdit::ListEdit(ListLine& line) :
00183 cOsdMenu(tr("Edit sudoku list"), 15), line(line)
00184 {
00185 strn0cpy(description, line.get_description(), sizeof(description));
00186 Add(new cMenuEditStrItem(tr("Description"), description, sizeof(description)));
00187 }
00188
00189
00190 eOSState ListEdit::ProcessKey(eKeys key)
00191 {
00192 eOSState state = cOsdMenu::ProcessKey(key);
00193 if (state == osUnknown)
00194 {
00195 if (key == kOk)
00196 {
00197 line.set_description(description);
00198 state = osBack;
00199 }
00200 }
00201 return state;
00202 }