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.
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
)
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
);
58 case GUI_MSG_NOTIFY_ALL
:
60 if (message
.GetParam1() == GUI_MSG_UPDATE
)
71 return CGUIDialog::OnMessage(message
);
74 void CGUIDialogTextViewer::SetText()
76 CGUIMessage
msg(GUI_MSG_LABEL_SET
, GetID(), CONTROL_TEXTAREA
);
77 msg
.SetLabel(m_strText
);
81 void CGUIDialogTextViewer::SetHeading()
83 CGUIMessage
msg(GUI_MSG_LABEL_SET
, GetID(), CONTROL_HEADING
);
84 msg
.SetLabel(m_strHeading
);
88 void CGUIDialogTextViewer::UseMonoFont(bool use
)
91 CGUIMessage
msg(GUI_MSG_SET_TYPE
, GetID(), CONTROL_TEXTAREA
, use
? 1 : 0);
95 void CGUIDialogTextViewer::OnDeinitWindow(int nextWindowID
)
97 CGUIDialog::OnDeinitWindow(nextWindowID
);
100 CGUIMessage
msgReset(GUI_MSG_LABEL_RESET
, GetID(), CONTROL_TEXTAREA
);
104 SET_CONTROL_LABEL(CONTROL_HEADING
, "");
107 void CGUIDialogTextViewer::ShowForFile(const std::string
& path
, bool useMonoFont
)
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
);
123 catch(const std::bad_alloc
&)
125 CLog::Log(LOGERROR
, "Not enough memory to load text file {}", path
);
129 CLog::Log(LOGERROR
, "Exception while trying to view text file {}", path
);