Kodi Development 22.0
for Binary and Script based Add-Ons
 
Loading...
Searching...
No Matches
Window.h
1/*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9#pragma once
10
11#include "../AddonBase.h"
12#include "../c-api/gui/window.h"
13#include "ListItem.h"
14#include "input/ActionIDs.h"
15
16#ifdef __cplusplus
17
18namespace kodi
19{
20namespace gui
21{
22
23//==============================================================================
33
34//==============================================================================
43using ClientHandle = KODI_GUI_CLIENT_HANDLE;
44//------------------------------------------------------------------------------
45
46class CListItem;
47
48//==============================================================================
109class ATTR_DLL_LOCAL CWindow : public CAddonGUIControlBase
110{
111public:
112 //============================================================================
126 CWindow(const std::string& xmlFilename,
127 const std::string& defaultSkin,
128 bool asDialog,
129 bool isMedia = false)
130 : CAddonGUIControlBase(nullptr)
131 {
132 m_controlHandle = m_interface->kodi_gui->window->create(
133 m_interface->kodiBase, xmlFilename.c_str(), defaultSkin.c_str(), asDialog, isMedia);
134 if (!m_controlHandle)
135 kodi::Log(ADDON_LOG_FATAL, "kodi::gui::CWindow can't create window class from Kodi !!!");
136 m_interface->kodi_gui->window->set_callbacks(m_interface->kodiBase, m_controlHandle, this,
137 CBOnInit, CBOnFocus, CBOnClick, CBOnAction,
138 CBGetContextButtons, CBOnContextButton);
139 }
140 //----------------------------------------------------------------------------
141
142 //============================================================================
146 ~CWindow() override
147 {
148 if (m_controlHandle)
149 m_interface->kodi_gui->window->destroy(m_interface->kodiBase, m_controlHandle);
150 }
151 //----------------------------------------------------------------------------
152
153 //============================================================================
170 bool Show()
171 {
172 return m_interface->kodi_gui->window->show(m_interface->kodiBase, m_controlHandle);
173 }
174 //----------------------------------------------------------------------------
175
176 //============================================================================
183 void Close() { m_interface->kodi_gui->window->close(m_interface->kodiBase, m_controlHandle); }
184 //----------------------------------------------------------------------------
185
186 //============================================================================
190 void DoModal()
191 {
192 m_interface->kodi_gui->window->do_modal(m_interface->kodiBase, m_controlHandle);
193 }
194 //----------------------------------------------------------------------------
195
196 //============================================================================
204 bool SetFocusId(int controlId)
205 {
206 return m_interface->kodi_gui->window->set_focus_id(m_interface->kodiBase, m_controlHandle,
207 controlId);
208 }
209 //----------------------------------------------------------------------------
210
211 //============================================================================
218 {
219 return m_interface->kodi_gui->window->get_focus_id(m_interface->kodiBase, m_controlHandle);
220 }
221 //----------------------------------------------------------------------------
222
223 //============================================================================
230 void SetControlLabel(int controlId, const std::string& label)
231 {
232 m_interface->kodi_gui->window->set_control_label(m_interface->kodiBase, m_controlHandle,
233 controlId, label.c_str());
234 }
235 //----------------------------------------------------------------------------
236
237 //============================================================================
244 void SetControlVisible(int controlId, bool visible)
245 {
246 m_interface->kodi_gui->window->set_control_visible(m_interface->kodiBase, m_controlHandle,
247 controlId, visible);
248 }
249 //----------------------------------------------------------------------------
250
251 //============================================================================
258 void SetControlSelected(int controlId, bool selected)
259 {
260 m_interface->kodi_gui->window->set_control_selected(m_interface->kodiBase, m_controlHandle,
261 controlId, selected);
262 }
263 //----------------------------------------------------------------------------
264
265 //============================================================================
278 void SetProperty(const std::string& key, const std::string& value)
279 {
280 m_interface->kodi_gui->window->set_property(m_interface->kodiBase, m_controlHandle, key.c_str(),
281 value.c_str());
282 }
283 //----------------------------------------------------------------------------
284
285 //============================================================================
298 std::string GetProperty(const std::string& key) const
299 {
300 std::string label;
301 char* ret = m_interface->kodi_gui->window->get_property(m_interface->kodiBase, m_controlHandle,
302 key.c_str());
303 if (ret != nullptr)
304 {
305 if (std::strlen(ret))
306 label = ret;
307 m_interface->free_string(m_interface->kodiBase, ret);
308 }
309 return label;
310 }
311 //----------------------------------------------------------------------------
312
313 //============================================================================
320 void SetPropertyInt(const std::string& key, int value)
321 {
322 m_interface->kodi_gui->window->set_property_int(m_interface->kodiBase, m_controlHandle,
323 key.c_str(), value);
324 }
325 //----------------------------------------------------------------------------
326
327 //============================================================================
334 int GetPropertyInt(const std::string& key) const
335 {
336 return m_interface->kodi_gui->window->get_property_int(m_interface->kodiBase, m_controlHandle,
337 key.c_str());
338 }
339 //----------------------------------------------------------------------------
340
341 //============================================================================
348 void SetPropertyBool(const std::string& key, bool value)
349 {
350 m_interface->kodi_gui->window->set_property_bool(m_interface->kodiBase, m_controlHandle,
351 key.c_str(), value);
352 }
353 //----------------------------------------------------------------------------
354
355 //============================================================================
362 bool GetPropertyBool(const std::string& key) const
363 {
364 return m_interface->kodi_gui->window->get_property_bool(m_interface->kodiBase, m_controlHandle,
365 key.c_str());
366 }
367 //----------------------------------------------------------------------------
368
369 //============================================================================
376 void SetPropertyDouble(const std::string& key, double value)
377 {
378 m_interface->kodi_gui->window->set_property_double(m_interface->kodiBase, m_controlHandle,
379 key.c_str(), value);
380 }
381 //----------------------------------------------------------------------------
382
383 //============================================================================
390 double GetPropertyDouble(const std::string& key) const
391 {
392 return m_interface->kodi_gui->window->get_property_double(m_interface->kodiBase,
393 m_controlHandle, key.c_str());
394 }
395 //----------------------------------------------------------------------------
396
397 //============================================================================
402 {
403 m_interface->kodi_gui->window->clear_properties(m_interface->kodiBase, m_controlHandle);
404 }
405 //----------------------------------------------------------------------------
406
407 //============================================================================
429 void ClearProperty(const std::string& key)
430 {
431 m_interface->kodi_gui->window->clear_property(m_interface->kodiBase, m_controlHandle,
432 key.c_str());
433 }
434 //----------------------------------------------------------------------------
435
437 //============================================================================
442 {
443 m_interface->kodi_gui->window->clear_item_list(m_interface->kodiBase, m_controlHandle);
444 }
445 //----------------------------------------------------------------------------
446
447 //============================================================================
454 void AddListItem(const std::shared_ptr<CListItem>& item, int itemPosition = -1)
455 {
456 m_interface->kodi_gui->window->add_list_item(m_interface->kodiBase, m_controlHandle,
457 item->m_controlHandle, itemPosition);
458 }
459 //----------------------------------------------------------------------------
460
461 //============================================================================
468 void AddListItem(const std::string& item, int itemPosition = -1)
469 {
470 m_interface->kodi_gui->window->add_list_item(
471 m_interface->kodiBase, m_controlHandle,
472 std::make_shared<kodi::gui::CListItem>(item)->m_controlHandle, itemPosition);
473 }
474 //----------------------------------------------------------------------------
475
476 //============================================================================
482 void RemoveListItem(int itemPosition)
483 {
484 m_interface->kodi_gui->window->remove_list_item_from_position(m_interface->kodiBase,
485 m_controlHandle, itemPosition);
486 }
487 //----------------------------------------------------------------------------
488
489 //============================================================================
495 void RemoveListItem(const std::shared_ptr<CListItem>& item)
496 {
497 m_interface->kodi_gui->window->remove_list_item(m_interface->kodiBase, m_controlHandle,
498 item->m_controlHandle);
499 }
500 //----------------------------------------------------------------------------
501
502 //============================================================================
511 std::shared_ptr<CListItem> GetListItem(int listPos)
512 {
513 KODI_GUI_LISTITEM_HANDLE handle = m_interface->kodi_gui->window->get_list_item(
514 m_interface->kodiBase, m_controlHandle, listPos);
515 if (!handle)
516 return std::shared_ptr<CListItem>();
517
518 return std::make_shared<kodi::gui::CListItem>(handle);
519 }
520 //----------------------------------------------------------------------------
521
522 //============================================================================
528 void SetCurrentListPosition(int listPos)
529 {
530 m_interface->kodi_gui->window->set_current_list_position(m_interface->kodiBase, m_controlHandle,
531 listPos);
532 }
533 //----------------------------------------------------------------------------
534
535 //============================================================================
542 {
543 return m_interface->kodi_gui->window->get_current_list_position(m_interface->kodiBase,
544 m_controlHandle);
545 }
546 //----------------------------------------------------------------------------
547
548 //============================================================================
555 {
556 return m_interface->kodi_gui->window->get_list_size(m_interface->kodiBase, m_controlHandle);
557 }
558 //----------------------------------------------------------------------------
559
560 //============================================================================
572 void SetContainerProperty(const std::string& key, const std::string& value)
573 {
574 m_interface->kodi_gui->window->set_container_property(m_interface->kodiBase, m_controlHandle,
575 key.c_str(), value.c_str());
576 }
577 //----------------------------------------------------------------------------
578
579 //============================================================================
611 void SetContainerContent(const std::string& value)
612 {
613 m_interface->kodi_gui->window->set_container_content(m_interface->kodiBase, m_controlHandle,
614 value.c_str());
615 }
616 //----------------------------------------------------------------------------
617
618 //============================================================================
625 {
626 return m_interface->kodi_gui->window->get_current_container_id(m_interface->kodiBase,
627 m_controlHandle);
628 }
629 //----------------------------------------------------------------------------
631
632 //============================================================================
637 {
638 return m_interface->kodi_gui->window->mark_dirty_region(m_interface->kodiBase, m_controlHandle);
639 }
640 //----------------------------------------------------------------------------
641
642 //============================================================================
652 //
653
654 //============================================================================
661 virtual bool OnInit() { return false; }
662 //----------------------------------------------------------------------------
663
664 //============================================================================
673 virtual bool OnFocus(int controlId) { return false; }
674 //----------------------------------------------------------------------------
675
676 //============================================================================
685 virtual bool OnClick(int controlId) { return false; }
686 //----------------------------------------------------------------------------
687
688 //============================================================================
740 virtual bool OnAction(ADDON_ACTION actionId)
741 {
742 switch (actionId)
743 {
746 Close();
747 return true;
748 default:
749 break;
750 }
751 return false;
752 }
753 //----------------------------------------------------------------------------
754
755 //============================================================================
763 virtual void GetContextButtons(int itemNumber,
764 std::vector<std::pair<unsigned int, std::string>>& buttons)
765 {
766 }
767 //----------------------------------------------------------------------------
768
769 //============================================================================
777 virtual bool OnContextButton(int itemNumber, unsigned int button) { return false; }
778 //----------------------------------------------------------------------------
779
780 //============================================================================
836 bool (*CBOnInit)(kodi::gui::ClientHandle cbhdl),
837 bool (*CBOnFocus)(kodi::gui::ClientHandle cbhdl, int controlId),
838 bool (*CBOnClick)(kodi::gui::ClientHandle cbhdl, int controlId),
839 bool (*CBOnAction)(kodi::gui::ClientHandle cbhdl,
840 ADDON_ACTION actionId),
841 void (*CBGetContextButtons)(kodi::gui::ClientHandle cbhdl,
842 int itemNumber,
843 gui_context_menu_pair* buttons,
844 unsigned int* size) = nullptr,
845 bool (*CBOnContextButton)(kodi::gui::ClientHandle cbhdl,
846 int itemNumber,
847 unsigned int button) = nullptr)
848 {
849 if (!cbhdl || !CBOnInit || !CBOnFocus || !CBOnClick || !CBOnAction)
850 {
851 kodi::Log(ADDON_LOG_FATAL, "kodi::gui::CWindow::%s called with nullptr !!!", __FUNCTION__);
852 return;
853 }
854
855 m_interface->kodi_gui->window->set_callbacks(m_interface->kodiBase, m_controlHandle, cbhdl,
856 CBOnInit, CBOnFocus, CBOnClick, CBOnAction,
857 CBGetContextButtons, CBOnContextButton);
858 }
859 //----------------------------------------------------------------------------
861
862private:
863 static bool CBOnInit(KODI_GUI_CLIENT_HANDLE cbhdl)
864 {
865 return static_cast<CWindow*>(cbhdl)->OnInit();
866 }
867
868 static bool CBOnFocus(KODI_GUI_CLIENT_HANDLE cbhdl, int controlId)
869 {
870 return static_cast<CWindow*>(cbhdl)->OnFocus(controlId);
871 }
872
873 static bool CBOnClick(KODI_GUI_CLIENT_HANDLE cbhdl, int controlId)
874 {
875 return static_cast<CWindow*>(cbhdl)->OnClick(controlId);
876 }
877
878 static bool CBOnAction(KODI_GUI_CLIENT_HANDLE cbhdl, ADDON_ACTION actionId)
879 {
880 return static_cast<CWindow*>(cbhdl)->OnAction(actionId);
881 }
882
883 static void CBGetContextButtons(KODI_GUI_CLIENT_HANDLE cbhdl,
884 int itemNumber,
885 gui_context_menu_pair* buttons,
886 unsigned int* size)
887 {
888 std::vector<std::pair<unsigned int, std::string>> buttonList;
889 static_cast<CWindow*>(cbhdl)->GetContextButtons(itemNumber, buttonList);
890 if (!buttonList.empty())
891 {
892 unsigned int presentSize = static_cast<unsigned int>(buttonList.size());
893 if (presentSize > *size)
894 kodi::Log(ADDON_LOG_WARNING, "GetContextButtons: More as allowed '%i' entries present!",
895 *size);
896 else
897 *size = presentSize;
898 for (unsigned int i = 0; i < *size; ++i)
899 {
900 buttons[i].id = buttonList[i].first;
901 strncpy(buttons[i].name, buttonList[i].second.c_str(), ADDON_MAX_CONTEXT_ENTRY_NAME_LENGTH);
902 }
903 }
904 }
905
906 static bool CBOnContextButton(KODI_GUI_CLIENT_HANDLE cbhdl, int itemNumber, unsigned int button)
907 {
908 return static_cast<CWindow*>(cbhdl)->OnContextButton(itemNumber, button);
909 }
910};
911
912} /* namespace gui */
913} /* namespace kodi */
914
915#endif /* __cplusplus */
Definition ListItem.h:26
Definition ListItem.h:65
Definition Window.h:110
~CWindow() override
Class destructor.
Definition Window.h:146
@ ADDON_LOG_FATAL
4 : To notify fatal unrecoverable errors, which can may also indicate upcoming crashes.
Definition addon_base.h:197
@ ADDON_LOG_WARNING
2 : To write warnings in the log file.
Definition addon_base.h:190
ADDON_ACTION
Definition action_ids.h:19
@ ADDON_ACTION_PREVIOUS_MENU
10: Previous menu.
Definition action_ids.h:54
@ ADDON_ACTION_NAV_BACK
90: Nav back.
Definition action_ids.h:286
KODI_GUI_CLIENT_HANDLE ClientHandle
Handler for addon-sided processing class If the callback functions used by the window are not used di...
Definition Window.h:43
virtual bool OnContextButton(int itemNumber, unsigned int button)
Called after selection in context menu.
Definition Window.h:777
virtual void GetContextButtons(int itemNumber, std::vector< std::pair< unsigned int, std::string > > &buttons)
Get context menu buttons for list entry.
Definition Window.h:763
virtual bool OnAction(ADDON_ACTION actionId)
OnAction method.
Definition Window.h:740
virtual bool OnInit()
OnInit method.
Definition Window.h:661
virtual bool OnFocus(int controlId)
OnFocus method.
Definition Window.h:673
void SetIndependentCallbacks(kodi::gui::ClientHandle cbhdl, bool(*CBOnInit)(kodi::gui::ClientHandle cbhdl), bool(*CBOnFocus)(kodi::gui::ClientHandle cbhdl, int controlId), bool(*CBOnClick)(kodi::gui::ClientHandle cbhdl, int controlId), bool(*CBOnAction)(kodi::gui::ClientHandle cbhdl, ADDON_ACTION actionId), void(*CBGetContextButtons)(kodi::gui::ClientHandle cbhdl, int itemNumber, gui_context_menu_pair *buttons, unsigned int *size)=nullptr, bool(*CBOnContextButton)(kodi::gui::ClientHandle cbhdl, int itemNumber, unsigned int button)=nullptr)
Set independent callbacks
Definition Window.h:835
virtual bool OnClick(int controlId)
OnClick method.
Definition Window.h:685
bool SetFocusId(int controlId)
Gives the control with the supplied focus.
Definition Window.h:204
void RemoveListItem(const std::shared_ptr< CListItem > &item)
Remove item with given control class from list.
Definition Window.h:495
int GetListSize()
To get the amount of entries in the list.
Definition Window.h:554
CWindow(const std::string &xmlFilename, const std::string &defaultSkin, bool asDialog, bool isMedia=false)
Class constructor with needed values for window / dialog.
Definition Window.h:126
void ClearList()
Function delete all entries in integrated list.
Definition Window.h:441
void SetContainerContent(const std::string &value)
Sets the content type of the container.
Definition Window.h:611
void SetPropertyDouble(const std::string &key, double value)
Sets a window property with double value.
Definition Window.h:376
void SetPropertyInt(const std::string &key, int value)
Sets a window property with integer value.
Definition Window.h:320
std::shared_ptr< CListItem > GetListItem(int listPos)
To get list item control class on wanted position.
Definition Window.h:511
void SetControlLabel(int controlId, const std::string &label)
To set the used label on given control id.
Definition Window.h:230
void MarkDirtyRegion()
To inform Kodi that it need to render region new.
Definition Window.h:636
void SetControlVisible(int controlId, bool visible)
To set the visibility on given control id.
Definition Window.h:244
void SetControlSelected(int controlId, bool selected)
To set the selection on given control id.
Definition Window.h:258
void SetProperty(const std::string &key, const std::string &value)
Sets a window property, similar to an infolabel.
Definition Window.h:278
void Close()
Closes this window.
Definition Window.h:183
int GetCurrentListPosition()
To get current selected position in list.
Definition Window.h:541
void DoModal()
Display this window until close() is called.
Definition Window.h:190
void ClearProperty(const std::string &key)
Clears the specific window property.
Definition Window.h:429
void SetPropertyBool(const std::string &key, bool value)
Sets a window property with boolean value.
Definition Window.h:348
std::string GetProperty(const std::string &key) const
Returns a window property as a string, similar to an infolabel.
Definition Window.h:298
bool GetPropertyBool(const std::string &key) const
Returns a window property with boolean value.
Definition Window.h:362
bool Show()
Show this window.
Definition Window.h:170
void SetContainerProperty(const std::string &key, const std::string &value)
Sets a container property, similar to an infolabel.
Definition Window.h:572
int GetPropertyInt(const std::string &key) const
Returns a window property with integer value.
Definition Window.h:334
void SetCurrentListPosition(int listPos)
To set position of selected part in list.
Definition Window.h:528
void AddListItem(const std::shared_ptr< CListItem > &item, int itemPosition=-1)
To add a list item in the on window integrated list.
Definition Window.h:454
void ClearProperties()
Remove all present properties from window.
Definition Window.h:401
void RemoveListItem(int itemPosition)
Remove list item on position.
Definition Window.h:482
int GetCurrentContainerId()
Get the id of the currently visible container.
Definition Window.h:624
void AddListItem(const std::string &item, int itemPosition=-1)
To add a list item based upon string in the on window integrated list.
Definition Window.h:468
double GetPropertyDouble(const std::string &key) const
Returns a window property with double value.
Definition Window.h:390
int GetFocusId()
Returns the id of the control which is focused.
Definition Window.h:217
void ATTR_DLL_LOCAL Log(const ADDON_LOG loglevel, const char *format,...)
Add a message to Kodi's log.
Definition AddonBase.h:1938
Definition window.h:26