Merge pull request #22816 from CastagnaIT/fix_tx3g
[xbmc.git] / xbmc / dialogs / GUIDialogTextViewer.cpp
blob363f1020a2e1bd5d16edf8721fb275d25e595757
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "GUIDialogTextViewer.h"
11 #include "GUIUserMessages.h"
12 #include "ServiceBroker.h"
13 #include "filesystem/File.h"
14 #include "guilib/GUIComponent.h"
15 #include "guilib/GUIWindowManager.h"
16 #include "input/actions/Action.h"
17 #include "input/actions/ActionIDs.h"
18 #include "utils/URIUtils.h"
19 #include "utils/log.h"
21 using namespace XFILE;
23 #define CONTROL_HEADING 1
24 #define CONTROL_TEXTAREA 5
26 CGUIDialogTextViewer::CGUIDialogTextViewer(void)
27 : CGUIDialog(WINDOW_DIALOG_TEXT_VIEWER, "DialogTextViewer.xml")
29 m_loadType = KEEP_IN_MEMORY;
32 CGUIDialogTextViewer::~CGUIDialogTextViewer(void) = default;
34 bool CGUIDialogTextViewer::OnAction(const CAction &action)
36 if (action.GetID() == ACTION_TOGGLE_FONT)
38 UseMonoFont(!m_mono);
39 return true;
42 return CGUIDialog::OnAction(action);
45 bool CGUIDialogTextViewer::OnMessage(CGUIMessage& message)
47 switch ( message.GetMessage() )
49 case GUI_MSG_WINDOW_INIT:
51 CGUIDialog::OnMessage(message);
52 SetHeading();
53 SetText();
54 UseMonoFont(m_mono);
55 return true;
57 break;
58 case GUI_MSG_NOTIFY_ALL:
60 if (message.GetParam1() == GUI_MSG_UPDATE)
62 SetText();
63 SetHeading();
64 return true;
67 break;
68 default:
69 break;
71 return CGUIDialog::OnMessage(message);
74 void CGUIDialogTextViewer::SetText()
76 CGUIMessage msg(GUI_MSG_LABEL_SET, GetID(), CONTROL_TEXTAREA);
77 msg.SetLabel(m_strText);
78 OnMessage(msg);
81 void CGUIDialogTextViewer::SetHeading()
83 CGUIMessage msg(GUI_MSG_LABEL_SET, GetID(), CONTROL_HEADING);
84 msg.SetLabel(m_strHeading);
85 OnMessage(msg);
88 void CGUIDialogTextViewer::UseMonoFont(bool use)
90 m_mono = use;
91 CGUIMessage msg(GUI_MSG_SET_TYPE, GetID(), CONTROL_TEXTAREA, use ? 1 : 0);
92 OnMessage(msg);
95 void CGUIDialogTextViewer::OnDeinitWindow(int nextWindowID)
97 CGUIDialog::OnDeinitWindow(nextWindowID);
99 // reset text area
100 CGUIMessage msgReset(GUI_MSG_LABEL_RESET, GetID(), CONTROL_TEXTAREA);
101 OnMessage(msgReset);
103 // reset heading
104 SET_CONTROL_LABEL(CONTROL_HEADING, "");
107 void CGUIDialogTextViewer::ShowForFile(const std::string& path, bool useMonoFont)
109 CFile file;
110 if (file.Open(path))
112 std::string data;
115 data.resize(file.GetLength()+1);
116 file.Read(&data[0], file.GetLength());
117 CGUIDialogTextViewer* pDialog = CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogTextViewer>(WINDOW_DIALOG_TEXT_VIEWER);
118 pDialog->SetHeading(URIUtils::GetFileName(path));
119 pDialog->SetText(data);
120 pDialog->UseMonoFont(useMonoFont);
121 pDialog->Open();
123 catch(const std::bad_alloc&)
125 CLog::Log(LOGERROR, "Not enough memory to load text file {}", path);
127 catch(...)
129 CLog::Log(LOGERROR, "Exception while trying to view text file {}", path);