TGUI  v0.6.10
Container.hpp
1 //
3 // TGUI - Texus's Graphical User Interface
4 // Copyright (C) 2012-2015 Bruno Van de Velde (vdv_b@tgui.eu)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
25 
26 #ifndef TGUI_CONTAINER_HPP
27 #define TGUI_CONTAINER_HPP
28 
29 
30 #include <list>
31 
32 #include <TGUI/Widget.hpp>
33 
35 
36 namespace tgui
37 {
38  struct Callback;
39 
43  class TGUI_API Container : public Widget
44  {
45  public:
46 
48 
49 
54  Container();
55 
56 
63  Container(const Container& copy);
64 
65 
70  virtual ~Container();
71 
72 
81  Container& operator= (const Container& right);
82 
83 
94  bool setGlobalFont(const std::string& filename);
95 
96 
105  void setGlobalFont(const sf::Font& font);
106 
107 
116  const sf::Font& getGlobalFont() const;
117 
118 
125  const std::vector<Widget::Ptr>& getWidgets();
126 
127 
134  const std::vector<sf::String>& getWidgetNames();
135 
136 
151  virtual void add(const Widget::Ptr& widgetPtr, const sf::String& widgetName = "");
152 
153 
171  Widget::Ptr get(const sf::String& widgetName, bool recursive = false) const;
172 
173 
192  template <class T>
193  typename T::Ptr get(const sf::String& widgetName, bool recursive = false) const
194  {
195  return typename T::Ptr(get(widgetName, recursive));
196  }
197 
198 
215  Widget::Ptr copy(const Widget::Ptr& oldWidget, const sf::String& newWidgetName = "");
216 
217 
232  virtual void remove(const Widget::Ptr& widget);
233 
234 
241  virtual void remove(Widget* widget);
242 
243 
248  virtual void removeAllWidgets();
249 
250 
260  bool setWidgetName(const Widget::Ptr& widget, const std::string& name);
261 
262 
273  bool getWidgetName(const Widget::Ptr& widget, std::string& name) const;
274 
275 
284  void focusWidget(const Widget::Ptr& widget);
285 
286 
295  void focusWidget(Widget *const widget);
296 
297 
305  void focusNextWidget();
306 
307 
315  void focusPreviousWidget();
316 
317 
322  void unfocusWidgets();
323 
324 
329  void uncheckRadioButtons();
330 
331 
338  void moveWidgetToFront(Widget *const widget);
339 
340 
347  void moveWidgetToBack(Widget *const widget);
348 
349 
360  virtual void setTransparency(unsigned char transparency);
361 
362 
372  void bindGlobalCallback(std::function<void(const Callback&)> func);
373 
374 
385  template <typename T>
386  void bindGlobalCallback(void (T::*func)(const Callback&), T* const classPtr)
387  {
388  m_GlobalCallbackFunctions.push_back(std::bind(func, classPtr, std::placeholders::_1));
389  }
390 
391 
396  virtual void unbindGlobalCallback();
397 
398 
414  bool loadWidgetsFromFile(const std::string& filename);
415 
416 
427  bool saveWidgetsToFile(const std::string& filename);
428 
429 
437  virtual sf::Vector2f getWidgetsOffset() const;
438 
439 
442  // This function is used internally by child widget to alert there parent about a callback.
443  // If it reaches the gui, then the callback can be obtained by calling the pollCallback function of the gui.
445  virtual void addChildCallback(const Callback& callback);
446 
447 
451  virtual bool mouseOnWidget(float x, float y) = 0;
452 
456  virtual void leftMousePressed(float x, float y);
457 
461  virtual void leftMouseReleased(float x, float y);
462 
466  virtual void mouseMoved(float x, float y);
467 
471  virtual void keyPressed(const sf::Event::KeyEvent& event);
472 
476  virtual void textEntered(sf::Uint32 key);
477 
481  virtual void mouseWheelMoved(int delta, int x, int y);
482 
486  virtual void mouseNotOnWidget();
487 
491  virtual void mouseNoLongerDown();
492 
496  virtual void widgetUnfocused();
497 
498 
500  protected:
501 
503  // This function is called when the widget is added to a container.
505  virtual void initialize(Container *const container);
506 
507 
509  // This function is called every frame.
511  virtual void update();
512 
513 
515  // When this function is called then all the widgets receive the event (if there are widgets).
516  // The function returns true when the event is consumed and false when the event was ignored by all widgets.
518  bool handleEvent(sf::Event& event);
519 
520 
522  // Focuses the next widget in the container. If the last widget was focused then all widgets will be unfocused and
523  // this function will return false.
525  bool focusNextWidgetInContainer();
526 
527 
529  // When the tab key is pressed then this function is called. The focus will move to the next widget (if there is one).
530  // This function will only work when tabKeyUsageEnabled is true.
531  // The function will return true when another widget was focused.
533  bool tabKeyPressed();
534 
535 
537  // Checks above which widget the mouse is standing.
538  // If there is no widget below the mouse then this function will return a null pointer.
540  Widget::Ptr mouseOnWhichWidget(float x, float y);
541 
542 
544  // This function will call the draw function from all the widgets.
546  virtual void drawWidgetContainer(sf::RenderTarget* target, const sf::RenderStates& states = sf::RenderStates::Default) const;
547 
548 
550  protected:
551 
552  std::vector<Widget::Ptr> m_Widgets;
553  std::vector<sf::String> m_ObjName;
554 
555  // The focused widget
556  Widget* m_FocusedWidget;
557 
558  sf::Font m_GlobalFont;
559 
560  // A list that stores all functions that receive callbacks triggered by child widgets
561  std::list< std::function<void(const Callback&)> > m_GlobalCallbackFunctions;
562 
563 
565  };
566 
567 
571  class TGUI_API GuiContainer : public Container
572  {
573  public:
574 
575  typedef SharedWidgetPtr<Container> Ptr;
576 
577 
582  void unbindGlobalCallback();
583 
584 
594  virtual void setSize(float width, float height);
595 
596 
603  virtual sf::Vector2f getSize() const;
604 
605 
610  virtual GuiContainer* clone();
611 
612 
617  virtual bool mouseOnWidget(float x, float y);
618 
619 
621  private:
622 
627  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
628 
629 
631  protected:
632 
633  sf::RenderTarget* m_Window;
634 
635 
636  friend class Gui;
637 
638 
640  };
641 
642 
644 }
645 
647 
648 #endif // TGUI_CONTAINER_HPP
Namespace that contains all TGUI functions and classes.
Definition: AnimatedPicture.hpp:33
The parent class for every widget.
Definition: Widget.hpp:45
Definition: Callback.hpp:45
Definition: Gui.hpp:40
Definition: Container.hpp:571
void bindGlobalCallback(void(T::*func)(const Callback &), T *const classPtr)
Bind a function to the callbacks of all child widgets.
Definition: Container.hpp:386
Parent class for widgets that store multiple widgets.
Definition: Container.hpp:43
Definition: SharedWidgetPtr.hpp:44