1
0
mirror of https://github.com/VDR4Arch/vdr.git synced 2023-10-10 13:36:52 +02:00
vdr/osdbase.c

577 lines
14 KiB
C
Raw Permalink Normal View History

/*
* osdbase.c: Basic interface to the On Screen Display
*
* See the main source file 'vdr.c' for copyright information and
* how to reach the author.
*
* $Id: osdbase.c 4.4 2018/03/06 10:38:18 kls Exp $
*/
#include "osdbase.h"
2004-05-16 10:35:36 +02:00
#include <string.h>
#include "device.h"
#include "i18n.h"
#include "menuitems.h"
2004-05-16 10:35:36 +02:00
#include "remote.h"
#include "status.h"
2004-05-16 10:35:36 +02:00
// --- cOsdItem --------------------------------------------------------------
2004-05-16 10:35:36 +02:00
cOsdItem::cOsdItem(eOSState State)
{
2004-05-16 10:35:36 +02:00
text = NULL;
state = State;
selectable = true;
fresh = true;
}
cOsdItem::cOsdItem(const char *Text, eOSState State, bool Selectable)
{
2004-05-16 10:35:36 +02:00
text = NULL;
state = State;
selectable = Selectable;
2004-05-16 10:35:36 +02:00
fresh = true;
SetText(Text);
}
2004-05-16 10:35:36 +02:00
cOsdItem::~cOsdItem()
{
2004-05-16 10:35:36 +02:00
free(text);
}
2004-05-16 10:35:36 +02:00
void cOsdItem::SetText(const char *Text, bool Copy)
{
2004-05-16 10:35:36 +02:00
free(text);
text = Copy ? strdup(Text ? Text : "") : (char *)Text; // text assumes ownership!
}
2004-05-16 10:35:36 +02:00
void cOsdItem::SetSelectable(bool Selectable)
{
2004-05-16 10:35:36 +02:00
selectable = Selectable;
}
2004-05-16 10:35:36 +02:00
void cOsdItem::SetFresh(bool Fresh)
{
2004-05-16 10:35:36 +02:00
fresh = Fresh;
}
void cOsdItem::SetMenuItem(cSkinDisplayMenu *DisplayMenu, int Index, bool Current, bool Selectable)
{
DisplayMenu->SetItem(Text(), Index, Current, Selectable);
}
2004-05-16 10:35:36 +02:00
eOSState cOsdItem::ProcessKey(eKeys Key)
{
2004-05-16 10:35:36 +02:00
return Key == kOk ? state : osUnknown;
}
// --- cOsdObject ------------------------------------------------------------
void cOsdObject::Show(void)
{
if (isMenu)
((cOsdMenu *)this)->Display();
}
2004-05-16 10:35:36 +02:00
// --- cOsdMenu --------------------------------------------------------------
2004-05-16 10:35:36 +02:00
cSkinDisplayMenu *cOsdMenu::displayMenu = NULL;
int cOsdMenu::displayMenuCount = 0;
int cOsdMenu::osdState = 0;
2004-05-16 10:35:36 +02:00
cOsdMenu::cOsdMenu(const char *Title, int c0, int c1, int c2, int c3, int c4)
{
2004-05-16 10:35:36 +02:00
isMenu = true;
digit = 0;
hasHotkeys = false;
displayMenuItems = 0;
2004-05-16 10:35:36 +02:00
title = NULL;
menuCategory = mcUnknown;
menuSortMode = msmUnknown;
menuOrientation = moVertical;
2004-05-16 10:35:36 +02:00
SetTitle(Title);
SetCols(c0, c1, c2, c3, c4);
2004-05-16 10:35:36 +02:00
first = 0;
lastOffset = -1;
2004-05-16 10:35:36 +02:00
current = marked = -1;
subMenu = NULL;
helpRed = helpGreen = helpYellow = helpBlue = NULL;
helpDisplayed = false;
2004-05-16 10:35:36 +02:00
status = NULL;
if (!displayMenuCount++) {
cOsdProvider::OsdSizeChanged(osdState); // to get the current state
SetDisplayMenu();
}
}
2004-05-16 10:35:36 +02:00
cOsdMenu::~cOsdMenu()
{
2004-05-16 10:35:36 +02:00
free(title);
delete subMenu;
free(status);
displayMenu->Clear();
cStatus::MsgOsdClear();
if (!--displayMenuCount)
DELETENULL(displayMenu);
}
void cOsdMenu::SetMenuCategory(eMenuCategory MenuCategory)
{
menuCategory = MenuCategory;
}
void cOsdMenu::SetMenuSortMode(eMenuSortMode MenuSortMode)
{
menuSortMode = MenuSortMode;
}
void cOsdMenu::SetDisplayMenu(void)
{
if (displayMenu) {
displayMenu->Clear();
delete displayMenu;
}
displayMenu = Skins.Current()->DisplayMenu();
}
2004-05-16 10:35:36 +02:00
const char *cOsdMenu::hk(const char *s)
{
static cString buffer;
2004-05-16 10:35:36 +02:00
if (s && hasHotkeys) {
if (digit == 0 && '1' <= *s && *s <= '9' && *(s + 1) == ' ')
digit = -1; // prevents automatic hotkeys - input already has them
if (digit >= 0) {
digit++;
buffer = cString::sprintf(" %c %s", (digit < 10) ? '0' + digit : ' ' , s);
2004-05-16 10:35:36 +02:00
s = buffer;
}
}
2004-05-16 10:35:36 +02:00
return s;
}
void cOsdMenu::SetCols(int c0, int c1, int c2, int c3, int c4)
{
cols[0] = c0;
cols[1] = c1;
cols[2] = c2;
cols[3] = c3;
cols[4] = c4;
}
void cOsdMenu::SetHasHotkeys(bool HasHotkeys)
{
hasHotkeys = HasHotkeys;
2004-05-16 10:35:36 +02:00
digit = 0;
}
2004-05-16 10:35:36 +02:00
void cOsdMenu::SetStatus(const char *s)
{
2004-05-16 10:35:36 +02:00
free(status);
status = s ? strdup(s) : NULL;
displayMenu->SetMessage(mtStatus, s);
}
2004-05-16 10:35:36 +02:00
void cOsdMenu::SetTitle(const char *Title)
{
2004-05-16 10:35:36 +02:00
free(title);
title = strdup(Title);
}
void cOsdMenu::DisplayHelp(bool Force)
{
if (!helpDisplayed || Force) {
displayMenu->SetButtons(helpRed, helpGreen, helpYellow, helpBlue);
cStatus::MsgOsdHelpKeys(helpRed, helpGreen, helpYellow, helpBlue);
helpDisplayed = true;
}
}
2004-05-16 10:35:36 +02:00
void cOsdMenu::SetHelp(const char *Red, const char *Green, const char *Yellow, const char *Blue)
{
2004-05-16 10:35:36 +02:00
// strings are NOT copied - must be constants!!!
helpRed = Red;
helpGreen = Green;
helpYellow = Yellow;
helpBlue = Blue;
DisplayHelp(true);
}
2004-05-16 10:35:36 +02:00
void cOsdMenu::Del(int Index)
{
2004-05-16 10:35:36 +02:00
cList<cOsdItem>::Del(Get(Index));
int count = Count();
2006-01-08 11:44:37 +01:00
while (current < count && !SelectableItem(current))
current++;
if (current == count) {
2006-01-08 11:44:37 +01:00
while (current > 0 && !SelectableItem(current))
current--;
}
2004-05-16 10:35:36 +02:00
if (Index == first && first > 0)
first--;
}
2004-05-16 10:35:36 +02:00
void cOsdMenu::Add(cOsdItem *Item, bool Current, cOsdItem *After)
{
2004-05-16 10:35:36 +02:00
cList<cOsdItem>::Add(Item, After);
if (Current)
current = Item->Index();
}
2004-05-16 10:35:36 +02:00
void cOsdMenu::Ins(cOsdItem *Item, bool Current, cOsdItem *Before)
{
2004-05-16 10:35:36 +02:00
cList<cOsdItem>::Ins(Item, Before);
if (Current)
current = Item->Index();
}
2004-05-16 10:35:36 +02:00
void cOsdMenu::Display(void)
{
2004-05-16 10:35:36 +02:00
if (subMenu) {
subMenu->Display();
return;
}
if (cOsdProvider::OsdSizeChanged(osdState))
SetDisplayMenu();
displayMenu->SetMessage(mtStatus, NULL);
2004-05-16 10:35:36 +02:00
displayMenu->Clear();
cStatus::MsgOsdClear();
if (menuCategory != displayMenu->MenuCategory())
displayMenu->SetMenuCategory(menuCategory);
displayMenu->SetMenuSortMode(menuSortMode);
menuOrientation = displayMenu->MenuOrientation();
displayMenuItems = displayMenu->MaxItems();
2004-05-16 10:35:36 +02:00
displayMenu->SetTabs(cols[0], cols[1], cols[2], cols[3], cols[4]);//XXX
displayMenu->SetTitle(title);
cStatus::MsgOsdTitle(title);
DisplayHelp(true);
2004-05-16 10:35:36 +02:00
int count = Count();
if (count > 0) {
int ni = 0;
for (cOsdItem *item = First(); item; item = Next(item)) {
2004-05-16 10:35:36 +02:00
cStatus::MsgOsdItem(item->Text(), ni++);
if (current < 0 && item->Selectable())
current = item->Index();
}
2004-05-16 10:35:36 +02:00
if (current < 0)
current = 0; // just for safety - there HAS to be a current item!
first = max(0, min(first, max(0, count - displayMenuItems))); // in case the menu size has changed
2004-05-16 10:35:36 +02:00
if (current - first >= displayMenuItems || current < first) {
first = current - displayMenuItems / 2;
if (first + displayMenuItems > count)
first = count - displayMenuItems;
if (first < 0)
first = 0;
}
int i = first;
int n = 0;
for (cOsdItem *item = Get(first); item; item = Next(item)) {
bool CurrentSelectable = (i == current) && item->Selectable();
item->SetMenuItem(displayMenu, i - first, CurrentSelectable, item->Selectable());
if (CurrentSelectable)
2004-05-16 10:35:36 +02:00
cStatus::MsgOsdCurrentItem(item->Text());
if (++n == displayMenuItems)
break;
i++;
}
}
2008-02-17 12:31:16 +01:00
displayMenu->SetScrollbar(count, first);
2004-05-16 10:35:36 +02:00
if (!isempty(status))
displayMenu->SetMessage(mtStatus, status);
}
2004-05-16 10:35:36 +02:00
void cOsdMenu::SetCurrent(cOsdItem *Item)
{
2004-05-16 10:35:36 +02:00
current = Item ? Item->Index() : -1;
if (current >= 0 && lastOffset >= 0)
first = max(0, current - lastOffset);
lastOffset = -1;
}
2004-05-16 10:35:36 +02:00
void cOsdMenu::RefreshCurrent(void)
{
2004-05-16 10:35:36 +02:00
cOsdItem *item = Get(current);
if (item)
item->Set();
}
2004-05-16 10:35:36 +02:00
void cOsdMenu::DisplayCurrent(bool Current)
{
2004-05-16 10:35:36 +02:00
cOsdItem *item = Get(current);
if (item) {
item->SetMenuItem(displayMenu, current - first, Current && item->Selectable(), item->Selectable());
if (Current && item->Selectable())
2004-05-16 10:35:36 +02:00
cStatus::MsgOsdCurrentItem(item->Text());
if (!Current)
item->SetFresh(true); // leaving the current item resets 'fresh'
if (cMenuEditItem *MenuEditItem = dynamic_cast<cMenuEditItem *>(item)) {
if (!MenuEditItem->DisplayHelp(Current))
DisplayHelp();
else
helpDisplayed = false;
}
}
}
void cOsdMenu::DisplayItem(cOsdItem *Item)
{
if (Item) {
int Index = Item->Index();
int Offset = Index - first;
if (Offset >= 0 && Offset < first + displayMenuItems) {
bool Current = Index == current;
Item->SetMenuItem(displayMenu, Offset, Current && Item->Selectable(), Item->Selectable());
if (Current && Item->Selectable())
cStatus::MsgOsdCurrentItem(Item->Text());
}
}
}
2004-05-16 10:35:36 +02:00
void cOsdMenu::Clear(void)
{
if (marked >= 0)
SetStatus(NULL);
lastOffset = (current > first) ? current - first : 0;
2004-05-16 10:35:36 +02:00
first = 0;
current = marked = -1;
cList<cOsdItem>::Clear();
}
2004-05-16 10:35:36 +02:00
bool cOsdMenu::SelectableItem(int idx)
{
2004-05-16 10:35:36 +02:00
cOsdItem *item = Get(idx);
return item && item->Selectable();
}
2004-05-16 10:35:36 +02:00
void cOsdMenu::CursorUp(void)
{
int tmpCurrent = current;
int lastOnScreen = first + displayMenuItems - 1;
int last = Count() - 1;
if (last < 0)
return;
while (--tmpCurrent != current) {
if (tmpCurrent < 0) {
if (first > 0) {
// make non-selectable items at the beginning visible:
first = 0;
Display();
return;
}
if (Setup.MenuScrollWrap)
tmpCurrent = last + 1;
else
return;
}
else if (SelectableItem(tmpCurrent))
break;
2004-05-16 10:35:36 +02:00
}
if (first <= tmpCurrent && tmpCurrent <= lastOnScreen)
DisplayCurrent(false);
current = tmpCurrent;
if (current < first) {
first = Setup.MenuScrollPage ? max(0, current - displayMenuItems + 1) : current;
Display();
}
else if (current > lastOnScreen) {
first = max(0, current - displayMenuItems + 1);
Display();
2004-05-16 10:35:36 +02:00
}
else
DisplayCurrent(true);
}
2004-05-16 10:35:36 +02:00
void cOsdMenu::CursorDown(void)
{
int tmpCurrent = current;
int lastOnScreen = first + displayMenuItems - 1;
2004-05-16 10:35:36 +02:00
int last = Count() - 1;
if (last < 0)
return;
while (++tmpCurrent != current) {
if (tmpCurrent > last) {
if (first < last - displayMenuItems) {
// make non-selectable items at the end visible:
first = last - displayMenuItems + 1;
Display();
return;
}
if (Setup.MenuScrollWrap)
tmpCurrent = -1;
else
return;
}
else if (SelectableItem(tmpCurrent))
break;
}
if (first <= tmpCurrent && tmpCurrent <= lastOnScreen)
DisplayCurrent(false);
current = tmpCurrent;
if (current > lastOnScreen) {
first = Setup.MenuScrollPage ? current : max(0, current - displayMenuItems + 1);
if (first + displayMenuItems > last)
first = max(0, last - displayMenuItems + 1);
Display();
}
else if (current < first) {
first = current;
Display();
}
else
DisplayCurrent(true);
}
2004-05-16 10:35:36 +02:00
void cOsdMenu::PageUp(void)
{
int oldCurrent = current;
int oldFirst = first;
2004-05-16 10:35:36 +02:00
current -= displayMenuItems;
first -= displayMenuItems;
int last = Count() - 1;
if (current < 0)
current = 0;
2004-05-16 10:35:36 +02:00
if (first < 0)
first = 0;
int tmpCurrent = current;
while (!SelectableItem(tmpCurrent) && --tmpCurrent >= 0)
;
if (tmpCurrent < 0) {
tmpCurrent = current;
while (++tmpCurrent <= last && !SelectableItem(tmpCurrent))
;
}
current = tmpCurrent <= last ? tmpCurrent : -1;
if (current >= 0) {
if (current < first)
first = current;
else if (current - first >= displayMenuItems)
first = current - displayMenuItems + 1;
}
if (current != oldCurrent || first != oldFirst) {
Display();
DisplayCurrent(true);
}
else if (Setup.MenuScrollWrap)
CursorUp();
}
2006-01-08 11:44:37 +01:00
void cOsdMenu::PageDown(void)
{
int oldCurrent = current;
int oldFirst = first;
2004-05-16 10:35:36 +02:00
current += displayMenuItems;
first += displayMenuItems;
int last = Count() - 1;
if (current > last)
current = last;
if (first + displayMenuItems > last)
first = max(0, last - displayMenuItems + 1);
int tmpCurrent = current;
while (!SelectableItem(tmpCurrent) && ++tmpCurrent <= last)
;
if (tmpCurrent > last) {
tmpCurrent = current;
while (--tmpCurrent >= 0 && !SelectableItem(tmpCurrent))
;
}
current = tmpCurrent > 0 ? tmpCurrent : -1;
if (current >= 0) {
if (current < first)
first = current;
else if (current - first >= displayMenuItems)
first = current - displayMenuItems + 1;
}
if (current != oldCurrent || first != oldFirst) {
Display();
DisplayCurrent(true);
}
else if (Setup.MenuScrollWrap)
CursorDown();
}
2004-05-16 10:35:36 +02:00
void cOsdMenu::Mark(void)
{
2004-05-16 10:35:36 +02:00
if (Count() && marked < 0) {
marked = current;
SetStatus(tr("Up/Dn for new location - OK to move"));
}
}
2004-05-16 10:35:36 +02:00
eOSState cOsdMenu::HotKey(eKeys Key)
{
2004-05-16 10:35:36 +02:00
for (cOsdItem *item = First(); item; item = Next(item)) {
const char *s = item->Text();
if (s && (s = skipspace(s)) != NULL) {
if (*s == Key - k1 + '1') {
current = item->Index();
RefreshCurrent();
Display();
2004-05-16 10:35:36 +02:00
cRemote::Put(kOk, true);
break;
}
}
}
return osContinue;
}
2004-05-16 10:35:36 +02:00
eOSState cOsdMenu::AddSubMenu(cOsdMenu *SubMenu)
{
2004-05-16 10:35:36 +02:00
delete subMenu;
subMenu = SubMenu;
subMenu->Display();
return osContinue; // convenience return value
}
eOSState cOsdMenu::CloseSubMenu(bool ReDisplay)
{
2004-05-16 10:35:36 +02:00
delete subMenu;
subMenu = NULL;
if (ReDisplay) {
RefreshCurrent();
Display();
}
2004-05-16 10:35:36 +02:00
return osContinue; // convenience return value
}
2004-05-16 10:35:36 +02:00
eOSState cOsdMenu::ProcessKey(eKeys Key)
{
2004-05-16 10:35:36 +02:00
if (subMenu) {
eOSState state = subMenu->ProcessKey(Key);
if (state == osBack)
return CloseSubMenu();
return state;
}
2004-05-16 10:35:36 +02:00
cOsdItem *item = Get(current);
if (marked < 0 && item) {
eOSState state = item->ProcessKey(Key);
if (state != osUnknown) {
DisplayCurrent(true);
return state;
}
}
switch (int(Key)) {
case k0: return osUnknown;
2004-05-16 10:35:36 +02:00
case k1...k9: return hasHotkeys ? HotKey(Key) : osUnknown;
case kUp|k_Repeat:
case kUp: if (menuOrientation == moHorizontal) PageUp(); else CursorUp(); break;
2004-05-16 10:35:36 +02:00
case kDown|k_Repeat:
case kDown: if (menuOrientation == moHorizontal) PageDown(); else CursorDown(); break;
2004-05-16 10:35:36 +02:00
case kLeft|k_Repeat:
case kLeft: if (menuOrientation == moHorizontal) CursorUp(); else PageUp(); break;
2004-05-16 10:35:36 +02:00
case kRight|k_Repeat:
case kRight: if (menuOrientation == moHorizontal) CursorDown(); else PageDown(); break;
2004-05-16 10:35:36 +02:00
case kBack: return osBack;
case kOk: if (marked >= 0) {
SetStatus(NULL);
if (marked != current)
Move(marked, current);
marked = -1;
break;
}
// else run into default
default: if (marked < 0)
return osUnknown;
}
return osContinue;
}