guit  0.1
 All Classes Functions Variables Typedefs Enumerations Friends
gsdl.hpp
1 //
2 // Native SDL Layer
3 // guit GUI Toolkit
4 // Copyright © 2019 Eric Lecolinet. All rights reserved.
5 // http://www.telecom-paristech.fr/~elc
6 //
7 
8 #ifndef Guit_NativeSDL_hpp
9 #define Guit_NativeSDL_hpp
10 #include <functional>
11 #include <SDL.h>
12 #include <gevent.hpp>
13 #include <gcursor.hpp>
14 #ifdef __APPLE__
15 #include <objc/objc-runtime.h>
16 #endif
17 
18 struct SDL_SysWMinfo;
19 struct SDL_Surface;
20 
21 namespace guit {
22 
23 class GNatFont;
24 class GFontResource;
25 class GLContext;
26 class GIconValue;
27 
28 class GNatContext {
29 public:
30  GNatContext(class GControlImpl&);
31  ~GNatContext();
32 
33  void mainLoop();
34  void subLoop(const bool& quit);
35 
36  void setMainLoopDelay(unsigned int ms_delay);
37  void setSwapInterval(int interval);
38 
39  static uint32_t elapsedTime();
40  GDim const& screenSize() const {return screensize_;}
41 
42  GPoint cursorPos() const;
43  void moveCursor(GPoint const&);
44  void setCursorShape(GCursor::Shape);
45  void showCursor(bool state);
46  void freezeCursor(bool state);
47  void grabCursor(bool state);
48 
49  bool getClipboard(GString& text) const;
50  bool setClipboard(GString const& text);
51 
52  bool startTimer(GTimer&, GTime delay);
53  bool stopTimer(GTimer&);
54  void pushUserEvent(GFunction* fun);
55 
56  static bool loadFont(GFontResource&);
57  static void deleteFont(GFontResource&);
58  static bool getFontInfo(GString const& path, GString& family, GString& style);
59  static GTexture createFontTexture(GString const& text, GFontResource&);
60 
61  static GDim textSize(GFontResource&, GString const& text);
62  static float textWidth(GFontResource&, GString const& text);
63  static float fontHeight(GFontResource&);
64  static float fontAscent(GFontResource&);
65  static float fontDescent(GFontResource&);
66 
67  static void readTexture(const char** xpm, GIconValue&);
68  static void readTexture(GString const& filename, GIconValue&);
69  static SDL_Surface* readSurface(const char** xpm);
70  static SDL_Surface* readSurface(GString const& filename);
71  static void deleteSurface(SDL_Surface*);
72 
75  int waitEvent(SDL_Event*);
76  void processEvent(SDL_Event const&);
77  void processWindowEvent(SDL_Event const&, GWindow&);
79 
80 private:
81  static GTexture surfaceToTexture(SDL_Surface*); // deletes surf!
82  class GControlImpl& ctrl_;
83  Uint32 mainloop_delay_{10}, focus_winid_{}, focus_time_{};
84  GDim screensize_;
85 };
86 
87 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
88 
89 class GNatWindow {
90 public:
91  GNatWindow(GWindow*, bool with_border, bool resizable);
92  ~GNatWindow();
93 
94  Uint32 winID() const {return winid_;}
95  void setClip(float x, float y, float w, float h);
96  GLContext* glcontext() {return (GLContext*)glcontext_;}
97  GLContext* createGLContext();
98  void makeCurrent();
99  void makeCurrent(GLContext*);
100  void swapBuffers();
101 
102  void show(bool state);
103  void paint();
104  void raise();
105  void minimize();
106  void maximize();
107  void restore();
108  void move(float x, float y);
109  void resize(float w, float h);
110 
111  bool setIcon(GString const& filename);
112  void setTitle(GString const& title);
113  bool setOpacity(float opacity);
114  void setResizable(bool resizable);
115  void setMinSize(float w, float h);
116  void setMaxSize(float w, float h);
117  void setBordered(bool bordered);
118 
119  // MacOSX specific
120 #ifdef __APPLE__
121  void stayOnTop();
122  void stayOnTopOf(GNatWindow&);
123  void joinAllSpaces();
124  void setTitlebarTransparent(bool);
125  void hideCloseButton(bool);
126  void hideMiniaturizeButton(bool);
127  void hideZoomButton(bool);
128  void disallowFullScreen();
129  id cocoaWindow();
130 #endif
131  void createDelegate();
132  void deleteDelegate();
133  void resizeCB(uint32_t time, int w, int h);
134  GWindow* window_{};
135  SDL_Window* sdlwin_{};
136  SDL_GLContext glcontext_{};
137  Uint32 winid_{};
138  bool usermove_{}, userresize_{};
139 #ifdef __APPLE__
140  id delegate_{};
141 #endif
142 };
143 
144 }
145 #endif