GUI Fixes
[tpg/acess2.git] / Usermode / Applications / axwin2_src / WM / wm.c
index b91c756..ad441b3 100644 (file)
@@ -6,14 +6,16 @@
  */
 #include "common.h"
 #include <stdlib.h>
-#include <strings.h>
+#include <string.h>
 #include "wm.h"
 
 // === IMPORTS ===
 extern void    Decorator_RenderWidget(tElement *Element);
+extern void    Video_GetTextDims(tFont *Font, const char *Text, int *W, int *H);
 
 // === PROTOTYPES ===
-tElement       *WM_CreateElement(tElement *Parent, int Type, int Flags);
+tElement       *WM_CreateElement(tElement *Parent, int Type, int Flags, const char *DebugName);
+void   WM_UpdateMinDims(tElement *Element);
 void   WM_SetFlags(tElement *Element, int Flags);
 void   WM_SetSize(tElement *Element, int Size);
 void   WM_SetText(tElement *Element, char *Text);
@@ -23,7 +25,9 @@ void  WM_RenderWidget(tElement *Element);
 void   WM_Update(void);
 
 // === GLOBALS ===
-tElement       gWM_RootElement;
+tElement       gWM_RootElement = {
+       DebugName: "ROOT"
+};
 struct {
        void    (*Init)(tElement *This);
        void    (*UpdateFlags)(tElement *This);
@@ -36,22 +40,24 @@ struct {
 
 // === CODE ===
 // --- Widget Creation and Control ---
-tElement *WM_CreateElement(tElement *Parent, int Type, int Flags)
+tElement *WM_CreateElement(tElement *Parent, int Type, int Flags, const char *DebugName)
 {
        tElement        *ret;
+       const char      *dbgName = DebugName ? DebugName : "";
        
-       if(Type < 0 || Type > NUM_ELETYPES)     return NULL;
-       
-       ret = calloc(sizeof(tElement), 1);
+       ret = calloc(sizeof(tElement)+strlen(dbgName)+1, 1);
        if(!ret)        return NULL;
        
        // Prepare
        ret->Type = Type;
+       strcpy(ret->DebugName, dbgName);
        if(Parent == NULL)      Parent = &gWM_RootElement;
        ret->Parent = Parent;
+       ret->Flags = Flags;
        
        // Append to parent's list
-       ret->NextSibling = Parent->LastChild;
+       if(Parent->LastChild)
+               Parent->LastChild->NextSibling = ret;
        Parent->LastChild = ret;
        if(!Parent->FirstChild) Parent->FirstChild = ret;
        
@@ -60,9 +66,17 @@ tElement *WM_CreateElement(tElement *Parent, int Type, int Flags)
        ret->PaddingT = 2;
        ret->PaddingB = 2;
        
+       if( gaWM_WidgetTypes[Type].Init )
+               gaWM_WidgetTypes[Type].Init(ret);
+       
+       WM_UpdateMinDims(ret->Parent);
+       
        return ret;
 }
 
+/**
+ * \brief Alter an element's flags 
+ */
 void WM_SetFlags(tElement *Element, int Flags)
 {
        // Permissions are handled in the message handler
@@ -78,10 +92,14 @@ void WM_SetFlags(tElement *Element, int Flags)
 void WM_SetSize(tElement *Element, int Size)
 {
        if(!Element)    return ;
-       Element->Size = Size;
+       Element->FixedWith = Size;
        return ;
 }
 
+/**
+ * \brief Set the text field of an element
+ * \note Used for the image path on ELETYPE_IMAGE
+ */
 void WM_SetText(tElement *Element, char *Text)
 {
        if(!Element)    return ;
@@ -92,143 +110,51 @@ void WM_SetText(tElement *Element, char *Text)
        {
        case ELETYPE_IMAGE:
                if(Element->Data)       free(Element->Data);
-               Element->Data = LoadImage( Element->Text );
+               Element->Data = Image_Load( Element->Text );
                if(!Element->Data) {
                        Element->Flags &= ~ELEFLAG_FIXEDSIZE;
                        return ;
                }
                
-               Element->Flags |= ELEFLAG_FIXEDSIZE;
+               //Element->Flags |= ELEFLAG_FIXEDSIZE;
                Element->CachedW = ((tImage*)Element->Data)->Width;
                Element->CachedH = ((tImage*)Element->Data)->Height;
                
-               if(Element->Parent && Element->Parent->Flags & ELEFLAG_VERTICAL)
-                       Element->Size = Element->CachedH;
-               else
-                       Element->Size = Element->CachedW;
-               break;
-       }
-       
-       return ;
-}
-
-// --- Pre-Rendering ---
-#if 0
-void WM_UpdateDimensions(tElement *Element, int Pass)
-{
-       // Pass zero intialises
-       if( Pass == 0 )
-       {
-               // If not a fixed size element, initialise the sizes
-               if( !(Element->Flags & ELEFLAG_FIXEDSIZE) )
-               {
-                       Element->CachedH = 0;
-                       Element->CachedW = 0;
-                       if( Element->Size )
-                       {
-                               if( Element->Parent->Flags & ELEFLAG_VERTICAL )
-                                       Element->CachedH = Element->Size;
-                               else
-                                       Element->CachedW = Element->Size;
-                       }
+               if(Element->Parent && Element->Parent->Flags & ELEFLAG_VERTICAL) {
+                       Element->MinCross = ((tImage*)Element->Data)->Width;
+                       Element->MinWith = ((tImage*)Element->Data)->Height;
                }
-       }
-
-        int    fixedSize = 0, maxCross = 0;
-        int    nFixed = 0, nChildren = 0;
-       for( child = Element->FirstChild; child; child = child->NextSibling )
-       {
-               WM_UpdateDimensions( child, 0 );
-               
-               if( Element->Flags & ELEFLAG_VERTICAL )
-               {
-                       if( child->CachedH ) {
-                               fixedSize += child->CachedH;
-                               nFixed ++;
-                       }
-                       if( maxCross < child->CachedW )
-                               maxCross = child->CachedW;
+               else {
+                       Element->MinWith = ((tImage*)Element->Data)->Width;
+                       Element->MinCross = ((tImage*)Element->Data)->Height;
                }
-               else
+               break;
+       
+       case ELETYPE_TEXT:
                {
-                       if( child->CachedW ) {
-                               fixedSize += child->CachedW;
-                               nFixed ++;
-                       }
-                       if( maxCross < child->CachedH )
-                               maxCross = child->CachedH;
+                int    w=0, h=0;
+               Video_GetTextDims(NULL, Element->Text, &w, &h);
+               if(Element->Parent && Element->Parent->Flags & ELEFLAG_VERTICAL) {
+                       Element->MinCross = w;
+                       Element->MinWith = h;
                }
-               nChildren ++;
-       }
-
-
-       // If we don't have our dimensions, get the child dimensions
-       if( Element->CachedW == 0 || Element->CachedH == 0 )
-       {       
-               if( Element->Flags & ELEFLAG_VERTICAL )
-               {
-                       if( Element->CachedW == 0 && maxCross )
-                               Element->CachedW = Element->PaddingL
-                                       + Element->PaddingR + maxCross;
-                       
-                       if( Element->CachedH == 0 && nFixed == nChildren )
-                               Element->CachedH = Element->PaddingT
-                                       + Element->PaddingB + fixedSize
-                                       + nChildren * Element->GapSize;
+               else {
+                       Element->MinWith = w;
+                       Element->MinCross = h;
                }
-               else
-               {
-                       if( maxCross )
-                               Element->CachedH = Element->PaddingT
-                                       + Element->PaddingB + maxCross;
-                       
-                       if( Element->CachedW == 0 && nFixed == nChildren )
-                               Element->CachedW = Element->PaddingL
-                                       + Element->PaddingR + fixedSize
-                                       + nChildren * Element->GapSize;
                }
+               break;
        }
        
-       // Now, if we have the "length" of the widget, we can size the children
-       if( (Element->Flags & ELEFLAG_VERTICAL && Element->CachedH > 0)
-        || (!(Element->Flags & ELEFLAG_VERTICAL) && Element->CachedW > 0) )
-       {
-                int    dynSize;
-               
-               // Calculate the size of dynamically sized elements
-               if( Element->Flags & ELEFLAG_VERTICAL )
-                       dynSize = Element->CachedH - Element->PaddingT
-                                - Element->PaddingB - fixedSize;
-               else
-                       dynSize = Element->CachedW - Element->PaddingL
-                                - Element->PaddingR - fixedSize;
-               dynSize /= nChildren - nFixed;
-               
-               // Itterate children again
-               for( child = Element->FirstChild; child; child = child->NextSibling )
-               {
-                        int    tmp;
-                       
-                       // Get the size of them
-                       if(child->Size)
-                               tmp = child->Size;
-                       else if(dynSize < Element->MinSize)
-                               tmp = child->MinSize;
-                       else
-                               tmp = dynSize;
-                       
-                       if( Element->Flags & ELEFLAG_VERTICAL )
-                               child->CachedH = tmp;
-                       else
-                               child->CachedW = tmp;
-                       
-                       WM_UpdateDimensions(child, 1);
-               }
-       }
+       return ;
 }
 
-#endif
-
+// --- Pre-Rendering ---
+/**
+ * \name Pre-Rendering
+ * \brief Updates the element positions and sizes
+ * \{
+ */
 /**
  * \brief Updates the dimensions of an element
  * 
@@ -239,168 +165,104 @@ void WM_UpdateDimensions(tElement *Element, int Pass)
 void WM_UpdateDimensions(tElement *Element, int Pass)
 {
        tElement        *child;
-        int    fixedChildSize = 0;
-        int    dynamicSize;
         int    nChildren = 0;
         int    nFixed = 0;
+        int    maxCross = 0;
+        int    fixedSize = 0;
+        int    fullCross, dynWith;
        
-       _SysDebug("WM_UpdateDimensions: (Element=%p{Flags:0x%x}, Pass=%i)",
-               Element, Element->Flags,
-               Pass);
+       _SysDebug("WM_UpdateDimensions %p'%s'", Element, Element->DebugName);
+       _SysDebug(" -> Flags = 0x%x", Element->Flags);
+       _SysDebug(" ->CachedH = %i, ->PaddingT = %i, ->PaddingB = %i",
+               Element->CachedH, Element->PaddingT, Element->PaddingB
+               );
+       _SysDebug(" ->CachedW = %i, ->PaddingL = %i, ->PaddingR = %i",
+               Element->CachedW, Element->PaddingL, Element->PaddingR
+               );
        
-       if( Pass == 0 )
+       // Pass 1
+       for( child = Element->FirstChild; child; child = child->NextSibling )
        {
-               if( Element->Flags & ELEFLAG_NORENDER ) return ;
+               if( child->Flags & ELEFLAG_ABSOLUTEPOS )
+                       continue ;
                
-               if( !(Element->Flags & ELEFLAG_ABSOLUTEPOS) ) {
-                       Element->CachedX = 0;
-                       Element->CachedY = 0;
-               }
-               if( !(Element->Flags & ELEFLAG_FIXEDSIZE) ) {
-                       Element->CachedW = 0;
-                       Element->CachedH = 0;
+               _SysDebug(" > %p'%s' ->FixedWith = %i", child, child->DebugName, child->FixedWith);
+               if( child->FixedWith )
+               {
+                       nFixed ++;
+                       fixedSize += child->FixedWith;
                }
+               
+               if( child->FixedCross && maxCross < child->FixedCross )
+                       maxCross = child->FixedCross;
+               if( child->MinCross && maxCross < child->MinCross )
+                       maxCross = child->MinCross;
+               nChildren ++;
        }
        
-       if( !(Element->Flags & ELEFLAG_FIXEDSIZE) ) {
-               // If the element is sized, fix its dimension(s)
-               if(Element->Size)
-               {
-                       if(Element->Flags & ELEFLAG_NOEXPAND)
-                       {
-                               Element->CachedW = Element->Size;
-                               Element->CachedH = Element->Size;
-                       }
-                       else {
-                               if( Element->Parent->Flags & ELEFLAG_VERTICAL ) {
-                                       Element->CachedH = Element->Size;
-                                       Element->CachedW = Element->Parent->CachedW;
-                                       if(Element->CachedW)
-                                               Element->CachedW -= (Element->Parent->PaddingL + Element->Parent->PaddingR);
-                               }
-                               else {
-                                       Element->CachedW = Element->Size;
-                                       Element->CachedH = Element->Parent->CachedH;
-                                       if(Element->CachedH)
-                                               Element->CachedH -= (Element->Parent->PaddingT + Element->Parent->PaddingB);
-                               }
-                       }
-               }
-               else {
-                       // Ok, so now we need to calculate the size of all child elements
-                       // However, if ELEFLAG_NOEXPAND is not set, we can still set one
-                       // dimension
-                       if( !(Element->Flags & ELEFLAG_NOEXPAND) ) {
-                               if( Element->Parent->Flags & ELEFLAG_VERTICAL ) {
-                                       Element->CachedW = Element->Parent->CachedW;
-                                       if(Element->CachedW)
-                                               Element->CachedW -= (Element->Parent->PaddingL + Element->Parent->PaddingR);
-                               }
-                               else {
-                                       Element->CachedH = Element->Parent->CachedH;
-                                       if(Element->CachedH)
-                                               Element->CachedH -= (Element->Parent->PaddingT + Element->Parent->PaddingB);
-                               }
-                       }
-               }
+       _SysDebug(" - nChildren = %i, nFixed = %i", Element, nChildren, nFixed);
+       if( nChildren > nFixed ) {
+               if( Element->Flags & ELEFLAG_VERTICAL )
+                       dynWith = Element->CachedH - Element->PaddingT
+                               - Element->PaddingB;
+               else
+                       dynWith = Element->CachedW - Element->PaddingL
+                               - Element->PaddingR;
+               dynWith -= fixedSize;
+               if( dynWith < 0 )       return ;
+               dynWith /= nChildren - nFixed;
+               _SysDebug(" - dynWith = %i", dynWith);
        }
        
-       // Process Children (first pass)
+       if( Element->Flags & ELEFLAG_VERTICAL )
+               fullCross = Element->CachedW - Element->PaddingL - Element->PaddingR;
+       else
+               fullCross = Element->CachedH - Element->PaddingT - Element->PaddingB;
+       
+       _SysDebug(" - fullCross = %i", Element, fullCross);
+       
+       // Pass 2 - Set sizes and recurse
        for( child = Element->FirstChild; child; child = child->NextSibling )
        {
-               if( child->Flags & ELEFLAG_NORENDER )   continue;
-               WM_UpdateDimensions(child, 0);
+                int    cross, with;
                
-               // Children that don't inherit positions are ignored
-               if( child->Flags & ELEFLAG_ABSOLUTEPOS )        continue;
+               _SysDebug(" > %p'%s' ->MinCross = %i", child, child->DebugName, child->MinCross);
+
                
-               fixedChildSize += child->Size;
-               if(child->Size > 0)
-                       nFixed ++;
-               nChildren ++;
+               // --- Cross Size ---
+               if( child->FixedCross )
+                       cross = child->FixedCross;
+               // Expand to fill?
+               // TODO: Extra flag so options are (Expand, Equal, Wrap)
+               else if( child->Flags & ELEFLAG_NOEXPAND )
+                       cross = child->MinCross;
+               else
+                       cross = fullCross;
+               _SysDebug(" > %p'%s' - cross = %i", child, child->DebugName, cross);
+               if( Element->Flags & ELEFLAG_VERTICAL )
+                       child->CachedW = cross;
+               else
+                       child->CachedH = cross;
                
-               // If we are wrapping the children, get the largest cross size
-               if( !(Element->Flags & ELEFLAG_FIXEDSIZE)
-                && Element->Flags & ELEFLAG_NOEXPAND
-                && Element->Size == 0 )
-               {
-                       if( Element->Flags & ELEFLAG_VERTICAL ) {
-                               if( Element->CachedW < child->CachedW )
-                                       Element->CachedW = child->CachedW;
-                       }
-                       else {
-                               if( Element->CachedH < child->CachedH )
-                                       Element->CachedH = child->CachedH;
-                       }
-               }
-       }
-       
-       // Let's avoid a #DIV0 shall we?
-       if( nChildren > 0 )
-       {
-               // Calculate the size of dynamically sized children
-               if( Element->Flags & ELEFLAG_VERTICAL ) {
-                       if( Element->CachedH == 0 ) {
-                               if( nFixed == nChildren )
-                                       Element->CachedH = fixedChildSize;
-                               else
-                                       return ;
-                       }
-                       dynamicSize = (Element->CachedH - (Element->PaddingT + Element->PaddingB)  - fixedChildSize) / nChildren;
-               }
-               else {
-                       if( Element->CachedW == 0 ) {
-                               if( nFixed == nChildren )
-                                       Element->CachedW = fixedChildSize;
-                               else
-                                       return ;
-                       }
-                       dynamicSize = (Element->CachedW - (Element->PaddingL + Element->PaddingR) - fixedChildSize) / nChildren;
-               }
+               // --- With Size ---
+               if( child->FixedWith)
+                       with = child->FixedWith;
+               else if( child->Flags & ELEFLAG_NOSTRETCH )
+                       with = child->MinWith;
+               else
+                       with = dynWith;
+               _SysDebug(" > %p'%s' - with = %i", child, child->DebugName, with);
+               if( Element->Flags & ELEFLAG_VERTICAL )
+                       child->CachedH = with;
+               else
+                       child->CachedW = with;
                
-               // Process Children (second pass)
-               for( child = Element->FirstChild; child; child = child->NextSibling )
-               {
-                       if( child->Flags & ELEFLAG_NORENDER )   continue;
-                       // Children that don't inherit positions are ignored
-                       if( child->Flags & ELEFLAG_ABSOLUTEPOS )        continue;
-                       
-                       if(!child->Size) {
-                               if(child->Flags & ELEFLAG_VERTICAL)
-                                       child->CachedH = dynamicSize;
-                               else
-                                       child->CachedW = dynamicSize;
-                       }
-                       
-                       WM_UpdateDimensions(child, 1);
-                       
-                       // If we are wrapping the children, get the largest cross size
-                       if( Element->Flags & ELEFLAG_NOEXPAND ) {
-                               if( Element->Flags & ELEFLAG_VERTICAL ) {
-                                       if( Element->CachedW < child->CachedW )
-                                               Element->CachedW = child->CachedW;
-                               }
-                               else {
-                                       if( Element->CachedH < child->CachedH )
-                                               Element->CachedH = child->CachedH;
-                               }
-                       }
-               }
+               WM_UpdateDimensions(child, 0);
        }
        
-       // Add the padding
-       //Element->CachedW += Element->PaddingL + Element->PaddingR;
-       //Element->CachedH += Element->PaddingT + Element->PaddingB;
-       
-       _SysDebug("Pass %i, Element %p %ix%i",
-               Pass, Element, Element->CachedW, Element->CachedH
-               );
-       
-       // We should be done
-       // Next function will do the coordinates
+       _SysDebug("%p'%s' Done", Element, Element->DebugName);
 }
 
-
 /**
  * \brief Updates the position of an element
  * 
@@ -410,36 +272,56 @@ void WM_UpdatePosition(tElement *Element)
 {
        tElement        *child;
         int    x, y;
+       static int      depth = 0;
+       char    indent[depth+1];
        
        if( Element->Flags & ELEFLAG_NORENDER ) return ;
        
-       _SysDebug("Element=%p{PaddingL:%i, PaddingT:%i}",
-               Element, Element->PaddingL, Element->PaddingT);
+       memset(indent, ' ', depth);
+       indent[depth] = '\0';
+       depth ++;
+       
+       _SysDebug("%sWM_UpdatePosition %p'%s'{PaddingL:%i, PaddingT:%i}",
+               indent, Element, Element->DebugName, Element->PaddingL, Element->PaddingT);
        
        // Initialise
        x = Element->CachedX + Element->PaddingL;
        y = Element->CachedY + Element->PaddingT;
        
+       _SysDebug("%s- Alignment = %s", indent,
+               (Element->Flags & ELEFLAG_VERTICAL) ? "vertical" : "horizontal");
+
        // Update each child
        for(child = Element->FirstChild; child; child = child->NextSibling)
        {
+               _SysDebug("%s- x = %i, y = %i", indent, x, y);
                child->CachedX = x;
                child->CachedY = y;
                
                // Set Alignment
                if( Element->Flags & ELEFLAG_ALIGN_CENTER ) {
-                       if(Element->Flags & ELEFLAG_VERTICAL )
+                       _SysDebug("%sChild being aligned to center", indent);
+                       if(Element->Flags & ELEFLAG_VERTICAL)
                                child->CachedX += Element->CachedW/2 - child->CachedW/2;
                        else
                                child->CachedY += Element->CachedH/2 - child->CachedH/2;
                }
-               else if( Element->Flags & ELEFLAG_ALIGN_END ) {
+               else if( Element->Flags & ELEFLAG_ALIGN_END) {
+                       _SysDebug("%sChild being aligned to end", indent);
                        if(Element->Flags & ELEFLAG_VERTICAL )
-                               child->CachedX += Element->CachedW - child->CachedW;
+                               child->CachedX += Element->CachedW
+                                       - Element->PaddingL - Element->PaddingR
+                                       - child->CachedW;
                        else
-                               child->CachedY += Element->CachedH - child->CachedH;
+                               child->CachedY += Element->CachedH
+                                       - Element->PaddingT
+                                       - Element->PaddingB
+                                       - child->CachedH;
                }
                
+               _SysDebug("%s> %p'%s' at (%i,%i)", indent, child, child->DebugName,
+                       child->CachedX, child->CachedY);
+       
                // Update child's children positions
                WM_UpdatePosition(child);
                
@@ -452,11 +334,60 @@ void WM_UpdatePosition(tElement *Element)
                }
        }
        
-       _SysDebug("Element %p (%i,%i)",
-                Element, Element->CachedX, Element->CachedY
+       _SysDebug("%sElement %p'%s' (%i,%i)",
+               indent, Element, Element->DebugName, Element->CachedX, Element->CachedY
                );
+       depth --;
 }
 
+/**
+ * \brief Update the minimum dimensions of the element
+ * \note Called after a child's minimum dimensions have changed
+ */
+void WM_UpdateMinDims(tElement *Element)
+{
+       tElement        *child;
+       
+       if(!Element)    return;
+       
+       Element->MinCross = 0;
+       Element->MinWith = 0;
+       
+       for(child = Element->FirstChild; child; child = child->NextSibling)
+       {
+               if( Element->Parent &&
+                       (Element->Flags & ELEFLAG_VERTICAL) == (Element->Parent->Flags & ELEFLAG_VERTICAL)
+                       )
+               {
+                       if(child->FixedCross)
+                               Element->MinCross += child->FixedCross;
+                       else
+                               Element->MinCross += child->MinCross;
+                       if(child->FixedWith)
+                               Element->MinWith += child->FixedWith;
+                       else
+                               Element->MinWith += child->MinWith;
+               }
+               else
+               {
+                       if(child->FixedCross)
+                               Element->MinWith += child->FixedCross;
+                       else
+                               Element->MinWith += child->MinCross;
+                       if(child->FixedWith)
+                               Element->MinCross += child->FixedWith;
+                       else
+                               Element->MinCross += child->MinWith;
+               }
+       }
+       
+       // Recurse upwards
+       WM_UpdateMinDims(Element->Parent);
+}
+/**
+ * \}
+ */
+
 // --- Render ---
 void WM_RenderWidget(tElement *Element)
 {

UCC git Repository :: git.ucc.asn.au