~ubuntu-branches/ubuntu/oneiric/nux/oneiric

« back to all changes in this revision

Viewing changes to Nux/WindowThread.h

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2010-11-18 19:17:32 UTC
  • Revision ID: james.westby@ubuntu.com-20101118191732-rn35790vekj6o4my
Tags: upstream-0.9.4
ImportĀ upstreamĀ versionĀ 0.9.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2010 Inalogic Inc.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU Lesser General Public License version 3, as
 
6
 * published by the  Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 
10
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 
11
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 
12
 * License for more details.
 
13
 *
 
14
 * You should have received a copy of both the GNU Lesser General Public
 
15
 * License version 3 along with this program.  If not, see
 
16
 * <http://www.gnu.org/licenses/>
 
17
 *
 
18
 * Authored by: Jay Taoko <jay.taoko_AT_gmail_DOT_com>
 
19
 *
 
20
 */
 
21
 
 
22
 
 
23
#ifndef WINDOWTHREAD_H
 
24
#define WINDOWTHREAD_H
 
25
 
 
26
#include "TimerProc.h"
 
27
 
 
28
namespace nux
 
29
{
 
30
 
 
31
  class WindowThread;
 
32
  class Layout;
 
33
  class HLayout;
 
34
  class GLWindowImpl;
 
35
  class ClientArea;
 
36
  class WindowCompositor;
 
37
  class AbstractThread;
 
38
  class SystemThread;
 
39
  class UXTheme;
 
40
  class TimerHandler;
 
41
  struct ClientAreaDraw;
 
42
 
 
43
#if (defined(NUX_OS_LINUX) || defined(NUX_USE_GLIB_LOOP_ON_WINDOWS)) && (!defined(NUX_DISABLE_GLIB_LOOP))
 
44
  gboolean nux_event_dispatch (GSource *source, GSourceFunc callback, gpointer user_data);
 
45
  gboolean nux_timeout_dispatch (gpointer user_data);
 
46
#endif
 
47
 
 
48
  class WindowThread: public AbstractThread
 
49
  {
 
50
    NUX_DECLARE_OBJECT_TYPE (WindowThread, AbstractThread);
 
51
  public:
 
52
    WindowThread (const TCHAR *WindowTitle, unsigned int width, unsigned int height, AbstractThread *Parent, bool Modal);
 
53
    ~WindowThread();
 
54
 
 
55
    //! Set the main layout for this window user interface.
 
56
    /*!
 
57
        @param layout The layout of the user interface.
 
58
    */
 
59
    void SetLayout (Layout *layout);
 
60
 
 
61
    //! Compute the layout of this window.
 
62
    /*!
 
63
        Reconfigure the layout of this window. Start by setting the size of the layout to the size of this window.
 
64
    */
 
65
    void ReconfigureLayout();
 
66
 
 
67
    void TerminateThread();
 
68
 
 
69
    //! Start running the user interface
 
70
    void RunUserInterface();
 
71
 
 
72
    // Event, Drawing
 
73
    virtual long ProcessEvent (IEvent &ievent, long TraverseInfo, long ProcessEventInfo);
 
74
    void ProcessDraw (GraphicsEngine &GfxContext, bool force_draw);
 
75
    void SetWindowTitle (const TCHAR *WindowTitle)
 
76
    {
 
77
      m_WindowTitle = WindowTitle;
 
78
    }
 
79
 
 
80
    GLWindowImpl &GetWindow() const
 
81
    {
 
82
      return *m_GLWindow;
 
83
    }
 
84
    GraphicsEngine &GetGraphicsEngine() const
 
85
    {
 
86
      return *m_GLWindow->GetGraphicsEngine();
 
87
    }
 
88
    BasePainter &GetPainter() const
 
89
    {
 
90
      return *m_Painter;
 
91
    }
 
92
    TimerHandler &GetTimerHandler() const
 
93
    {
 
94
      return *m_TimerHandler;
 
95
    }
 
96
    UXTheme &GetTheme() const
 
97
    {
 
98
      return *m_Theme;
 
99
    }
 
100
 
 
101
    void SetWindowSize (int width, int height);
 
102
 
 
103
    void SetWindowBackgroundPaintLayer (AbstractPaintLayer *bkg);
 
104
 
 
105
    void RequestRedraw();
 
106
    
 
107
    void ClearRedrawFlag()
 
108
    {
 
109
      m_RedrawRequested = false;
 
110
    }
 
111
    bool IsRedrawNeeded() const
 
112
    {
 
113
      return m_RedrawRequested;
 
114
    }
 
115
 
 
116
    /*!
 
117
      Returns the main layout, a BaseWindow object or 0 as the parent of the area.
 
118
      A BaseWindow has no parent so 0 is returned. Also for objects that have not been
 
119
      added to the rendering tree, 0 is returned.
 
120
    */
 
121
    Area* GetTopRenderingParent(Area* area);
 
122
 
 
123
    void AddToDrawList (View *view);
 
124
    
 
125
    void ClearDrawList ();
 
126
  
 
127
    std::vector<Geometry> GetDrawList ();
 
128
 
 
129
    // Layout
 
130
 
 
131
    //! This list contains the layout that need to be recomputed following the resizing of one of the sub element.
 
132
    /*!
 
133
        This list contains the layout that need to be recomputed following the resizing of one of the sub element.
 
134
    */
 
135
    void AddObjectToRefreshList (Area *bo);
 
136
    void RemoveObjectFromRefreshList (Area *bo);
 
137
 
 
138
    //! Empty the list that contains the layout that need to be recomputed following the resizing of one of the sub element.
 
139
    /*!
 
140
        Empty the list that contains the layout that need to be recomputed following the resizing of one of the sub element.
 
141
    */
 
142
    void EmptyLayoutRefreshList();
 
143
 
 
144
    void RefreshLayout();
 
145
 
 
146
    //! Return true if we are computing any layout that is part of this window.
 
147
    /*!
 
148
        Return true if we are computing any layout that is part of this window.
 
149
    */
 
150
    bool IsComputingLayout() const
 
151
    {
 
152
      return m_IsComputingMainLayout;
 
153
    }
 
154
    void SetComputingLayout (bool b)
 
155
    {
 
156
      m_IsComputingMainLayout = b;
 
157
    }
 
158
 
 
159
    bool IsMainLayoutDrawDirty() const;
 
160
 
 
161
    //! Compute the layout of a specific element
 
162
    void ComputeElementLayout(Area* bo, bool RecurseToTopLevelLayout = false);
 
163
 
 
164
    bool IsWaitingforModalWindow()
 
165
    {
 
166
      return m_bWaitForModalWindow;
 
167
    }
 
168
    
 
169
    bool IsModalWindow()
 
170
    {
 
171
      return m_bWaitForModalWindow;
 
172
    }
 
173
 
 
174
    void SetWindowStyle(WindowStyle wstyle)
 
175
    {
 
176
      m_WindowStyle = wstyle;
 
177
    }
 
178
    
 
179
    WindowStyle GetWindowStyle() const
 
180
    {
 
181
      return m_WindowStyle;
 
182
    }
 
183
 
 
184
    virtual ThreadState Start( void* arg = NULL );
 
185
 
 
186
    WindowCompositor& GetWindowCompositor()
 
187
    {
 
188
      return *m_window_compositor;
 
189
    }
 
190
    // should be private
 
191
 
 
192
    float GetFrameRate() const;
 
193
    t_u32 GetFrameCounter() const;
 
194
    t_u32 GetFramePeriodeCounter() const;
 
195
 
 
196
    bool IsEmbeddedWindow();
 
197
 
 
198
#if defined(NUX_OS_WINDOWS)
 
199
    bool ProcessForeignEvent (HWND hWnd, MSG msg, WPARAM wParam, LPARAM lParam, void *data);
 
200
#elif defined(NUX_OS_LINUX)
 
201
    bool ProcessForeignEvent (XEvent *event, void *data);
 
202
#endif
 
203
 
 
204
    void RenderInterfaceFromForeignCmd();
 
205
 
 
206
  public:
 
207
    virtual unsigned int Run (void *);
 
208
 
 
209
    /*!
 
210
        Constructor-like function for the thread.
 
211
        Will be called by EntryPoint before executing the thread body.
 
212
        For the main window, ThreadCtor is called in nux::CreateMainWindow.
 
213
        ThreadCtor creates and initialize the following elements:
 
214
            - Graphics Window
 
215
            - Timer
 
216
            - Painter
 
217
            - Compositor
 
218
            - Theme engine
 
219
        After ThreadCtor is called, m_ThreadCtorCalled is set to true;
 
220
    */
 
221
    virtual bool ThreadCtor();
 
222
 
 
223
#if defined(NUX_OS_WINDOWS)
 
224
    /*!
 
225
        Constructor-like function for the thread.
 
226
        Will be called by EntryPoint before executing the thread body.
 
227
        For the main window, ThreadCtor is called in nux::CreateMainWindow.
 
228
        ThreadCtor creates and initialize the following elements:
 
229
            - Graphics Window (from the externally created window)
 
230
            - Timer
 
231
            - Painter
 
232
            - Compositor
 
233
            - Theme engine
 
234
        After ThreadCtor is called, m_ThreadCtorCalled is set to true;
 
235
 
 
236
 
 
237
    */
 
238
    virtual bool ThreadCtor (HWND WindowHandle, HDC WindowDCHandle, HGLRC OpenGLRenderingContext);
 
239
#elif defined(NUX_OS_LINUX)
 
240
    /*!
 
241
        Constructor-like function for the thread.
 
242
        Will be called by EntryPoint before executing the thread body.
 
243
        For the main window, ThreadCtor is called in nux::CreateMainWindow.
 
244
        ThreadCtor creates and initialize the following elements:
 
245
            - Graphics Window (from the externally created window)
 
246
            - Timer
 
247
            - Painter
 
248
            - Compositor
 
249
            - Theme engine
 
250
        After ThreadCtor is called, m_ThreadCtorCalled is set to true;
 
251
 
 
252
 
 
253
    */
 
254
    virtual bool ThreadCtor (Display *X11Display, Window X11Window, GLXContext OpenGLContext);
 
255
#endif
 
256
 
 
257
    /*!
 
258
        Destructor-like function for the thread.
 
259
        Will be called by EntryPoint after executing the thread body.
 
260
        After ThreadDtor is called, m_ThreadDtorCalled is set to true.
 
261
        ThreadDtor is also called in the destructor of the WindowThread but is protected by m_ThreadDtorCalled so it is not called twice.
 
262
        In the case of the main window, ThreadDtor is called in the destructor of WindowThread.
 
263
    */
 
264
    virtual bool ThreadDtor();
 
265
 
 
266
    //! Get the main layout of this thread.
 
267
    /*!
 
268
      @return The main layout of this thread.
 
269
    */
 
270
    Layout* GetMainLayout();
 
271
 
 
272
    /*!
 
273
        This pointer maybe set by the user in ThreadInitFunc and reused in ThreadExitFunc
 
274
    */
 
275
    void *m_InitData;
 
276
    void *m_ExitData;
 
277
 
 
278
    sigc::signal<void> RedrawRequested;
 
279
 
 
280
    bool _inside_main_loop;
 
281
    bool _inside_timer_loop;
 
282
    bool _pending_wake_up_timer;
 
283
    
 
284
    TimerFunctor *_async_wake_up_functor;
 
285
    TimerHandle _async_wake_up_timer;  
 
286
    
 
287
  protected:
 
288
 
 
289
    void AsyncWakeUpCallback (void*);
 
290
    //void SetModalWindow(bool b) {m_bIsModal = b;}
 
291
 
 
292
    /*!
 
293
        Suspend Win32 Mouse and Keyboard inputs for this window thread and its child thread that are also window (not SystemThread).
 
294
    */
 
295
    void EnableMouseKeyboardInput();
 
296
 
 
297
    /*!
 
298
        Enable Win32 Mouse and Keyboard inputs for this window thread and its child thread that are also window (not SystemThread).
 
299
    */
 
300
    void DisableMouseKeyboardInput();
 
301
 
 
302
#if (defined(NUX_OS_LINUX) || defined(NUX_USE_GLIB_LOOP_ON_WINDOWS)) && (!defined(NUX_DISABLE_GLIB_LOOP))
 
303
    t_u32 ExecutionLoop (t_u32 timer_id);
 
304
#else
 
305
    t_u32 ExecutionLoop();
 
306
#endif
 
307
 
 
308
    virtual ThreadState StartChildThread (NThread *thread, bool Modal);
 
309
    virtual void AddChildThread (NThread *);
 
310
    virtual void RemoveChildThread (NThread *);
 
311
    virtual void ChildHasFinished (NThread *app);
 
312
    virtual void TerminateAllChildThread();
 
313
    virtual ThreadState SuspendChildGraphics (WindowThread *app);
 
314
 
 
315
    bool m_bWaitForModalWindow;
 
316
    WindowThread *m_ModalWindowThread;
 
317
 
 
318
    //typedef Loki::Functor< void, LOKI_TYPELIST_1(void*)   > ChildThreadExitCallback;
 
319
 
 
320
    typedef struct
 
321
    {
 
322
      NThread *thread;
 
323
      std::list< sigc::signal<void, void *> > ChildThreadExitCallback;
 
324
    } ThreadInfo;
 
325
 
 
326
    //std::list<NThread*> m_ChildThread;
 
327
    std::list< ThreadInfo * > m_ChildThreadInfo;
 
328
 
 
329
  private:
 
330
 
 
331
    WindowThread (const WindowThread &);
 
332
    // Does not make sense for a singleton. This is a self assignment.
 
333
    WindowThread &operator= (const WindowThread &);
 
334
    // Declare operator address-of as private
 
335
    WindowThread *operator &();
 
336
 
 
337
  private:
 
338
    //! This list contains the layout that need to be recomputed following the resizing of one of the sub element.
 
339
    /*!
 
340
        This list contains the layout that need to be recomputed following the resizing of one of the sub element.
 
341
    */
 
342
    std::list<Area *> m_LayoutRefreshList;
 
343
    std::vector<Geometry> m_dirty_areas;
 
344
 
 
345
    //! This variable is true while we are computing the layout the starting from the outmost layout (the Main Layout);
 
346
    bool m_IsComputingMainLayout;
 
347
 
 
348
  private:
 
349
    float m_FrameRate;
 
350
    t_u32 m_FrameCounter;
 
351
    t_u32 m_FramePeriodeCounter;
 
352
    float m_PeriodeTime;
 
353
 
 
354
    bool m_bFirstDrawPass;
 
355
    unsigned int m_StartupWidth;
 
356
    unsigned int m_StartupHeight;
 
357
    NString m_WindowTitle;
 
358
 
 
359
    bool m_RedrawRequested;
 
360
    Layout *m_AppLayout;
 
361
 
 
362
    UXTheme         *m_Theme;
 
363
    BasePainter     *m_Painter;
 
364
    TimerHandler    *m_TimerHandler;
 
365
 
 
366
    GLWindowImpl *m_GLWindow;
 
367
    GraphicsEngine *m_GraphicsContext;
 
368
    WindowCompositor *m_window_compositor;
 
369
    std::list<NThread *> m_ThreadList;
 
370
    bool m_WidgetInitialized;
 
371
    WindowStyle m_WindowStyle;
 
372
    bool m_bIsModal;
 
373
 
 
374
    bool m_ThreadCtorCalled;    //!< True is the thread constructor has been called.
 
375
 
 
376
    bool m_ThreadDtorCalled;    //!< True is the thread destructor has been called.
 
377
 
 
378
    bool m_embedded_window;             //!< Flag to run the interface in embedded mode.
 
379
 
 
380
    /*!
 
381
        Record if there was a configuration nux_event (NUX_SIZE_CONFIGURATION) that requires a full redraw.
 
382
        Used in the case where event processing and rendering are decoupled (with foreign windows).
 
383
    */
 
384
    bool m_size_configuration_event;
 
385
 
 
386
    bool m_force_redraw;
 
387
 
 
388
    friend class BasePainter;
 
389
    friend class SystemThread;
 
390
 
 
391
#if (defined(NUX_OS_LINUX) || defined(NUX_USE_GLIB_LOOP_ON_WINDOWS)) && (!defined(NUX_DISABLE_GLIB_LOOP))
 
392
    GMainLoop *m_GLibLoop;
 
393
    GMainContext *m_GLibContext;
 
394
    friend gboolean nux_event_dispatch (GSource *source, GSourceFunc callback, gpointer user_data);
 
395
    friend gboolean nux_timeout_dispatch (gpointer user_data);
 
396
 
 
397
    void InitGlibLoop();
 
398
    t_u32 AddGLibTimeout (t_u32 duration);
 
399
#endif
 
400
    friend class TimerHandler;
 
401
 
 
402
    friend WindowThread *CreateGUIThread (const TCHAR *WindowTitle,
 
403
                                          UINT width,
 
404
                                          UINT height,
 
405
                                          WindowThread *Parent,
 
406
                                          ThreadUserInitFunc UserInitFunc,
 
407
                                          void *InitData);
 
408
 
 
409
    friend WindowThread *CreateWindowThread (WindowStyle WndStyle,
 
410
        const TCHAR *WindowTitle,
 
411
        UINT width,
 
412
        UINT height,
 
413
        WindowThread *Parent,
 
414
        ThreadUserInitFunc UserInitFunc,
 
415
        void *InitData);
 
416
 
 
417
    friend WindowThread *CreateModalWindowThread (WindowStyle WndStyle,
 
418
        const TCHAR *WindowTitle,
 
419
        UINT width,
 
420
        UINT height,
 
421
        WindowThread *Parent,
 
422
        ThreadUserInitFunc UserInitFunc,
 
423
        void *InitData);
 
424
 
 
425
#if defined(NUX_OS_WINDOWS)
 
426
    friend WindowThread *CreateFromForeignWindow (HWND WindowHandle, HDC WindowDCHandle, HGLRC OpenGLRenderingContext,
 
427
        ThreadUserInitFunc UserInitFunc,
 
428
        void *InitData);
 
429
#elif defined(NUX_OS_LINUX)
 
430
    friend WindowThread *CreateFromForeignWindow (Window X11Window, GLXContext OpenGLContext,
 
431
        ThreadUserInitFunc UserInitFunc,
 
432
        void *InitData);
 
433
#endif
 
434
 
 
435
    SystemThread *CreateSimpleThread (AbstractThread *Parent, ThreadUserInitFunc UserInitFunc, void *InitData);
 
436
 
 
437
  };
 
438
 
 
439
}
 
440
 
 
441
#endif // WINDOWTHREAD_H