1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
/*
* Sudoku: A plug-in for the Video Disk Recorder
*
* Copyright (C) 2008-2010, Thomas Günther <tom@toms-cafe.de>
*
* This program 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; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "list.h"
#include <vdr/i18n.h>
#include <vdr/config.h>
#include <vdr/osdbase.h>
#include <vdr/osd.h>
#include <vdr/menuitems.h>
#include <vdr/interface.h>
#include <assert.h>
using namespace SudokuPlugin;
//--- class SudokuPlugin::ListLine ---------------------------------------------
/** Default constructor for cConfig<ListLine> */
ListLine::ListLine()
{
}
/** Constructor */
ListLine::ListLine(const char* sudoku, const char* description) :
sudoku(sudoku), description(description)
{
assert(sudoku != NULL);
assert(description != NULL);
}
/** Get the sudoku dump. */
const char* ListLine::get_sudoku() const
{
assert((const char*)sudoku != NULL);
return sudoku;
}
/** Get the description. */
const char* ListLine::get_description() const
{
assert((const char*)description != NULL);
return description;
}
/** Set the description. */
void ListLine::set_description(const char* new_description)
{
assert(new_description != NULL);
description = skipspace(new_description);
}
/** Parse a line of the sudoku list. */
bool ListLine::Parse(const char* line)
{
const char *delim = strchr(line, ' ');
if (delim)
{
sudoku = cString(strndup(line, delim - line), true);
description = skipspace(delim);
}
else
{
sudoku = line;
description = "";
}
assert((const char*)sudoku != NULL);
assert((const char*)description != NULL);
return !isempty(sudoku);
}
/** Save a line of the sudoku list. */
bool ListLine::Save(FILE* file) const
{
return fprintf(file, "%s %s\n", *sudoku, *description) > 0;
}
//--- class SudokuPlugin::ListMenu ---------------------------------------------
/** Constructor */
ListMenu::ListMenu(const char* filename, const char* new_sudoku) :
cOsdMenu(tr("Sudoku list")), selected(-1), save_mode(new_sudoku)
{
list.Load(filename);
if (save_mode)
list.Add(new ListLine(new_sudoku));
refresh();
if (save_mode)
{
SetCurrent(Last());
AddSubMenu(new ListEdit(*list.Get(Current())));
ProcessKey(kRight); // enter edit mode
}
}
/** Get the selected sudoku. */
const char* ListMenu::get_selected_sudoku() const
{
ListLine* line = list.Get(selected);
if (line)
return line->get_sudoku();
return NULL;
}
/** Process user events. */
eOSState ListMenu::ProcessKey(eKeys key)
{
bool had_submenu = HasSubMenu();
if (key == kBlue && !HasSubMenu())
key = kBack;
if (key == kGreen && !HasSubMenu() && !save_mode)
key = kOk;
eOSState state = cOsdMenu::ProcessKey(key);
if (state == osUnknown)
{
state = osContinue;
switch (key)
{
case kRed:
if (Current() >= 0 && Current() < list.Count())
return AddSubMenu(new ListEdit(*list.Get(Current())));
break;
case kYellow:
if (Current() >= 0 && Current() < list.Count() &&
Interface->Confirm(tr("Delete the puzzle?")))
list.Del(list.Get(Current()));
refresh();
list.Save();
break;
case kOk:
if (Current() >= 0 && Current() < list.Count() && !save_mode)
selected = Current();
state = osBack;
break;
default:
break;
}
}
if (had_submenu && !HasSubMenu())
{
refresh();
list.Save();
}
return state;
}
/** Refresh the list menu. */
void ListMenu::refresh()
{
int current = Current();
Clear();
SetHasHotkeys();
for (ListLine* line = list.First(); line; line = list.Next(line))
Add(new cOsdItem(hk(line->get_description())));
SetCurrent(Get(current));
SetHelp(trVDR("Button$Edit"), save_mode ? NULL : tr("Button$Load"),
trVDR("Button$Delete"), tr("Button$Back"));
Display();
}
//--- class SudokuPlugin::ListEdit ---------------------------------------------
/** Constructor */
ListEdit::ListEdit(ListLine& line) :
cOsdMenu(tr("Edit sudoku list"), 15), line(line)
{
strn0cpy(description, line.get_description(), sizeof(description));
Add(new cMenuEditStrItem(tr("Description"), description, sizeof(description)));
}
/** Process user events. */
eOSState ListEdit::ProcessKey(eKeys key)
{
eOSState state = cOsdMenu::ProcessKey(key);
if (state == osUnknown)
{
if (key == kOk)
{
line.set_description(description);
state = osBack;
}
}
return state;
}
|