Author: spitzak
Date: 2007-06-08 14:19:39 -0400 (Fri, 08 Jun 2007)
New Revision: 5896
Log:
Numerous changes so that setting align(0) on a widget with an image will
produce the text nicely centered under the image. In particular if you
set align(0) on the top-level items in a MenuBar, it makes a rather
standard-looking "toolbar" with labels below icons. Swapping the labels
to the tooltips can be used to make a "no text toolbar" with the icons
only.


Modified:
   trunk/src/Item.cxx
   trunk/src/MenuBar.cxx
   trunk/src/Menu_add.cxx
   trunk/src/Widget_draw.cxx

Modified: trunk/src/Item.cxx
===================================================================
--- trunk/src/Item.cxx  2007-06-08 18:17:53 UTC (rev 5895)
+++ trunk/src/Item.cxx  2007-06-08 18:19:39 UTC (rev 5896)
@@ -163,16 +163,25 @@
   setfont(textfont(), textsize());
   int w = 250, h = 250;
   measure(label(), w, h);
-  if (w) {w += 6+int(textsize())/2;}
-  if (type()) w += 15;
+  if (w) {
+    w += int(textsize())/2; // put 1 space between menubar items
+    h += int(leading());
+  }
   if (image()) {
     int W,H;
     image()->measure(W, H);
-    if (H > h) h = H;
-    w += W;
+    if (flag(ALIGN_LEFT|ALIGN_RIGHT)) {
+      if (H > h) h = H;
+      w += W;
+    } else {
+      if (w) h += H; else h = H;
+      if (W > w) w = W;
+    }
+  } else {
+    w += 6; // further adjustment to match Windows menubars
   }
   this->w(w);
-  this->h(h+int(leading()));
+  this->h(h);
   Widget::layout();
 }
 
@@ -249,15 +258,25 @@
   setfont(textfont(), textsize());
   int w = 250, h = 250;
   measure(label(), w, h);
-  if (w) {w += 6+int(textsize())/2;}
+  if (w) {
+    w += int(textsize())/2; // put 1 space between menubar items
+    h += int(leading());
+  }
   if (image()) {
     int W,H;
     image()->measure(W, H);
-    if (H > h) h = H;
-    w += W;
+    if (flag(ALIGN_LEFT|ALIGN_RIGHT)) {
+      if (H > h) h = H;
+      w += W;
+    } else {
+      if (w) h += H; else h = H;
+      if (W > w) w = W;
+    }
+  } else {
+    w += 6; // further adjustment to match Windows menubars
   }
   this->w(w);
-  this->h(h+int(leading()));
+  this->h(h);
   Widget::layout();
 }
 

Modified: trunk/src/MenuBar.cxx
===================================================================
--- trunk/src/MenuBar.cxx       2007-06-08 18:17:53 UTC (rev 5895)
+++ trunk/src/MenuBar.cxx       2007-06-08 18:19:39 UTC (rev 5896)
@@ -99,7 +99,7 @@
     // Test against the shortcut() of any item in any submenu:
     if (handle_shortcut()) return 1;
     // Check against the &x of top-level items:
-    if (event_state(ACCELERATOR)) {
+    if (shortcut() && event_state(ACCELERATOR)) {
       for (i = 0; i < children; i++) {
        Widget* w = child(i);
        if (w->active() && w->test_label_shortcut()) {

Modified: trunk/src/Menu_add.cxx
===================================================================
--- trunk/src/Menu_add.cxx      2007-06-08 18:17:53 UTC (rev 5895)
+++ trunk/src/Menu_add.cxx      2007-06-08 18:19:39 UTC (rev 5896)
@@ -222,6 +222,7 @@
   item->shortcut(shortcut);
   if (callback) item->callback(callback);
   item->user_data(data);
+  item->w(0); item->h(0); // force re-measure
   top->relayout();
   return item;
 }

Modified: trunk/src/Widget_draw.cxx
===================================================================
--- trunk/src/Widget_draw.cxx   2007-06-08 18:17:53 UTC (rev 5895)
+++ trunk/src/Widget_draw.cxx   2007-06-08 18:19:39 UTC (rev 5896)
@@ -183,18 +183,23 @@
     // in the button:
     if (label_ && !(flags&0x3f) && !(label_[0]=='@' && label_[1]==';')) {
       int d = (r.h()-int(h+labelsize()+leading()+.5))>>1;
-      if (d >= 0) {
-       // put the image atop the text
-       r.move_y(d); flags |= ALIGN_TOP|ALIGN_INSIDE;
-      } else if (w < r.w()) {
-       int text_w = r.w(); int text_h = r.h();
-       measure(label_, text_w, text_h, flags);
-       int d = (r.w()-(w+text_w))>>1;
-       if (d > 0) {
-         r.move_x(d);
-         flags |= ALIGN_LEFT|ALIGN_INSIDE;
-       }
+      if (d < 0) {
+        if (w < r.w()) {
+          // try to put the text to the right
+          int text_w = r.w()-w; int text_h = r.h();
+          measure(label_, text_w, text_h, flags);
+          int d = (r.w()-w-text_w)>>1;
+          if (d >= 0) {
+            r.move_x(d);
+            flags |= ALIGN_LEFT|ALIGN_INSIDE;
+            goto OK;
+          }
+        }
+        d = 0;
       }
+      // put the image atop the text
+      r.move_y(d); flags |= ALIGN_TOP|ALIGN_INSIDE;
+    OK:;
     }
 
     // STR 1547: ALIGN_CENTER forces the image to center and not effect label
@@ -202,13 +207,13 @@
       Rectangle ir(r, w, h, 0);
       img->draw(ir);
     } else {
-      Rectangle ir(r, w, h, flags);
-      img->draw(ir);
-      // figure out the rectangle that remains for text:
-      if (flags & ALIGN_TOP) r.set_y(ir.b());
-      else if (flags & ALIGN_BOTTOM) r.set_b(ir.y());
-      else if (flags & ALIGN_LEFT) r.set_x(ir.r());
-      else if (flags & ALIGN_RIGHT) r.set_r(ir.x());
+    Rectangle ir(r, w, h, flags);
+    img->draw(ir);
+    // figure out the rectangle that remains for text:
+    if (flags & ALIGN_TOP) r.set_y(ir.b());
+    else if (flags & ALIGN_BOTTOM) r.set_b(ir.y());
+    else if (flags & ALIGN_LEFT) r.set_x(ir.r());
+    else if (flags & ALIGN_RIGHT) r.set_r(ir.x());
       else r.set_y(ir.b());
     }
   }

_______________________________________________
fltk-commit mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-commit

Reply via email to