diff mde/gui/WidgetManager.d @ 85:56c0ddd90193

Intermediate commit (not stable). Changes to init system.
author Diggory Hardy <diggory.hardy@gmail.com>
date Thu, 11 Sep 2008 11:33:51 +0100
parents e0f1ec7fe73a
children 79d816b3e2d2
line wrap: on
line diff
--- a/mde/gui/WidgetManager.d	Sun Aug 31 15:59:17 2008 +0100
+++ b/mde/gui/WidgetManager.d	Thu Sep 11 11:33:51 2008 +0100
@@ -29,6 +29,7 @@
 import imde = mde.imde;
 import mde.input.Input;
 import mde.scheduler.Scheduler;
+import mde.setup.Screen;
 
 import tango.core.sync.Mutex;
 import tango.util.log.Log : Log, Logger;
@@ -36,13 +37,8 @@
 private Logger logger;
 static this () {
     logger = Log.getLogger ("mde.gui.WidgetManager");
-
-    gui = new WidgetManager ("gui");
 }
 
-WidgetManager gui;
-
-
 /*************************************************************************************************
  * The widget manager.
  * 
@@ -54,7 +50,7 @@
  * 
  * Aside from the IWidgetManager methods, this class should be thread-safe.
  *************************************************************************************************/
-class WidgetManager : WidgetLoader {
+class WidgetManager : WidgetLoader, Screen.Drawable {
     /** Construct a new widget manager.
      * 
      * params:
@@ -62,11 +58,14 @@
      */
     this (char[] file) {
         super(file);
+        
+        Screen.addDrawable (this);
     }
     
     // NOTE - temporarily here to allow CTOR to run safely during static this
     // called during init
     void init () {
+        // Doesn't need a lock - cannot conflict with other class functions.
         // Events we want to know about:
         imde.input.addMouseClickCallback(&clickEvent);
         imde.input.addMouseMotionCallback(&motionEvent);
@@ -75,8 +74,10 @@
     
     /** Draw the gui. */
     void draw() {
+        debug logger.trace ("drawing; w,h = {},{}",w,h);
         synchronized(mutex)
-            child.draw;
+            if (child)
+                child.draw;
     }
     
     
@@ -88,6 +89,7 @@
             logger.warn ("clickEvent: failed!");
         mutex.lock;
         scope(exit) mutex.unlock;
+        if (child is null) return;
         
         // NOTE: buttons receive the up-event even when drag-callbacks are in place.
         foreach (dg; clickCallbacks)
@@ -95,21 +97,10 @@
             if (dg (cast(wdabs)cx, cast(wdabs)cy, b, state)) return;
         
         // NOTE: do we need to test if the click was on the gui (and thus child)?
+        // FIXME: yes, unless we can guarantee this!
         IChildWidget widg = child.getWidget (cast(wdabs)cx,cast(wdabs)cy);
         if (widg !is null)
             widg.clickEvent (cast(wdabs)cx,cast(wdabs)cy,b,state);
-        /+ FIXME: remove
-        foreach (i,w; windows) {
-            IWidget widg = w.getWidget (cast(wdabs)cx,cast(wdabs)cy);
-            if (widg !is null) {
-                // Bring to front
-                windows = w ~ windows[0..i] ~ windows[i+1..$];
-                
-                widg.clickEvent (cast(wdabs)cx,cast(wdabs)cy,b,state);
-                requestRedraw;	// in case we've only moved to front
-                return;     // only pass to first window
-            }
-        }+/
     }
     
     /** For mouse motion events.
@@ -126,15 +117,18 @@
     }
     
     
-    void setSize (int x, int y) {
+    void sizeEvent (int nw, int nh) {   // Drawable function
         mutex.lock;
         scope(exit) mutex.unlock;
         
-        w = cast(wdim) x;
-        h = cast(wdim) y;
+        w = cast(wdim) nw;
+        h = cast(wdim) nh;
         
-        if (child is null)
-            return;     // May not have been created before this is first run.
+        debug logger.trace ("Resize to: {},{}", nw, nh);
+        if (w < mw || h < mh)
+            logger.warn ("Minimal dimensions ({},{}) not met: ({},{}), but I cannot resize myself!",mw,mh,w,h);
+        
+        if (!child) return;     // if not created yet.
         child.setWidth  (w, -1);
         child.setHeight (h, -1);
         child.setPosition (0,0);
@@ -165,7 +159,8 @@
     //END IWidgetManager methods
     
 protected:
-    /* Second stage of widget loading. */
+    /* Second stage of widget loading.
+     * Note: sizeEvent should be called with window size before this. */
     void createRootWidget () {
         // The renderer needs to be created on the first load, but not after this.
         if (rend is null)
@@ -173,6 +168,12 @@
         
         child = makeWidget ("root");
         
+        mw = child.minWidth;
+        mh = child.minHeight;
+        
+        if (w < mw || h < mh)
+            logger.warn ("Minimal dimensions ({},{}) not met: ({},{}), but I cannot resize myself!",mw,mh,w,h);
+        
         child.setWidth  (w, -1);
         child.setHeight (h, -1);
         child.setPosition (0,0);
@@ -184,6 +185,7 @@
     void delegate(wdabs cx, wdabs cy) [void*] motionCallbacks;
     IRenderer rend;
     wdim w,h;       // area available to the widgets
+    wdim mw,mh;     // minimal area available to the widgets
 }
 
 
@@ -212,9 +214,6 @@
         mutex = new Mutex;  // Used on functions intended to be called from outside the gui package.
         fileName = file;
     }
-    ~this () {
-        save;
-    }
     
     /* Load the widgets' data from the file specified to the CTOR.
     * 
@@ -380,7 +379,7 @@
     
     /** Create a widget by ID. */
     IChildWidget makeWidget (widgetID id, IParentWidget parent = null) {
-        debug logger.trace ("Creating widget \""~id~'"');
+        debug (mdeWidgets) logger.trace ("Creating widget \""~id~'"');
         return createWidget (this, curData[id], parent);
     }