]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/xforms_resize.C
Change glob() API to accept a dir parameter.
[lyx.git] / src / frontends / xforms / xforms_resize.C
index 0bc115733f6a2885e6e292cd60a43fc28c851d34..d84aa560be7e402de64b83151bdc353ee1a20f26 100644 (file)
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
- * \author Angus Leeming 
+ * \author Angus Leeming
  *
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
 
 #include "xforms_resize.h"
-#include "support/LAssert.h"
+
+#include <boost/assert.hpp>
+
 #include <algorithm> // std::max. Use FL_max in .c code...
-#include FORMS_H_LOCATION
+#include "lyx_forms.h"
+
+namespace lyx {
+namespace frontend {
 
 namespace {
 
-// A nasty hack for older xforms versions
-int get_tabfolder_numfolders(FL_OBJECT * folder)
+/* This is hacked straight out of the xforms source.
+   It is fl_adjust_form_size without the last few lines that do the
+   adjusting.
+ */
+double get_scaling_factor(FL_FORM * form)
 {
-#if FL_VERSION > 0 || FL_REVISION > 88
-       return fl_get_tabfolder_numfolders(folder);
-#else
-       if (folder->objclass != FL_TABFOLDER)
-               return 0;
-
-       fl_freeze_form(folder->form);
-       int const saved_folder_id = fl_get_folder_number(folder);
-
-       int num_folders = 0;
-       FL_FORM const * old_leaf = 0;
-       for (;;) {
-               int const id = num_folders + 1;
-               fl_set_folder_bynumber(folder, id);
-               FL_FORM const * const leaf = fl_get_folder(folder);
-               if (!leaf || leaf == old_leaf) {
-                       // unable to increment succesfully.
-                       break;
-               }
-               old_leaf = leaf;
-               ++num_folders;
+    FL_OBJECT *ob;
+    float xfactor, yfactor, max_factor, factor;
+    int sw, sh, osize;
+    float xm = 0.5f, ym = 0.5f;
+    int bw;
+
+//      if (fl_no_connection)
+//     return 1.0f;
+
+    max_factor = factor = 1.0f;
+    for (ob = form->first; ob; ob = ob->next)
+    {
+       if ((ob->align == FL_ALIGN_CENTER || (ob->align & FL_ALIGN_INSIDE) ||
+            ob->objclass == FL_INPUT) &&
+           !ob->is_child && *(ob->label) && ob->label[0] != '@' &&
+           ob->boxtype != FL_NO_BOX &&
+           (ob->boxtype != FL_FLAT_BOX || ob->objclass == FL_MENU))
+       {
+           fl_get_string_dimension(ob->lstyle, ob->lsize, ob->label,
+                      strlen(ob->label), &sw, &sh);
+
+           bw = (ob->boxtype == FL_UP_BOX || ob->boxtype == FL_DOWN_BOX) ?
+               FL_abs(ob->bw) : 1;
+
+           if (ob->objclass == FL_BUTTON &&
+               (ob->type == FL_RETURN_BUTTON || ob->type == FL_MENU_BUTTON))
+               sw += (int)FL_min(0.6f * ob->h, 0.6f * ob->w) - 1;
+
+           if (ob->objclass == FL_BUTTON && ob->type == FL_LIGHTBUTTON)
+               sw += FL_LIGHTBUTTON_MINSIZE + 1;
+
+           if (sw <= (ob->w - 2 * (bw + xm)) && sh <= (ob->h - 2 * (bw + ym)))
+               continue;
+
+           if ((osize = ob->w - 2 * (int)(bw + xm)) <= 0)
+               osize = 1;
+           xfactor = (float) sw / (float)osize;
+
+           if ((osize = ob->h - 2 * (int)(bw + ym)) <= 0)
+               osize = 1;
+           yfactor = (float) sh / osize;
+
+           if (ob->objclass == FL_INPUT)
+           {
+               xfactor = 1.0f;
+               yfactor = (sh + 1.6f) / osize;
+           }
+
+           if ((factor = FL_max(xfactor, yfactor)) > max_factor)
+           {
+               max_factor = factor;
+           }
        }
+    }
 
-       fl_set_folder_bynumber(folder, saved_folder_id);
-       fl_unfreeze_form(folder->form);
+    if (max_factor <= 1.0f)
+       return 1.0f;
 
-       return num_folders;
-#endif
-}
+    max_factor = 0.01f * (int) (max_factor * 100.0f);
 
+    if (max_factor > 1.25f)
+       max_factor = 1.25f;
 
-double get_scaling_factor(FL_FORM * form)
-{
-       // fl_adjust_form_size gives us the info we desire but also resizes
-       // the form, which we don't want :-(
-       double factor = fl_adjust_form_size(form);
-       if (factor > 1.001) {
-               double const inv = 1.0 / factor;
-               fl_scale_form(form, inv, inv);
-       }
-       return factor;
+    return max_factor;
 }
 
 
 double get_tabfolder_scale_to_fit(FL_OBJECT * folder)
 {
-       lyx::Assert(folder && folder->objclass == FL_TABFOLDER);
+       BOOST_ASSERT(folder && folder->objclass == FL_TABFOLDER);
 
        fl_freeze_form(folder->form);
        int const saved_folder_id = fl_get_folder_number(folder);
 
        double factor = 1.0;
-       int const size = get_tabfolder_numfolders(folder);
+       int const size = fl_get_tabfolder_numfolders(folder);
        for (int i = 0; i < size; ++i) {
                fl_set_folder_bynumber(folder, i+1);
                FL_FORM * leaf = fl_get_folder(folder);
@@ -92,12 +120,12 @@ double get_tabfolder_scale_to_fit(FL_OBJECT * folder)
 
 void scale_tabfolder_horizontally(FL_OBJECT * folder, double factor)
 {
-       lyx::Assert(folder && folder->objclass == FL_TABFOLDER);
+       BOOST_ASSERT(folder && folder->objclass == FL_TABFOLDER);
 
        fl_freeze_form(folder->form);
        int const saved_folder_id = fl_get_folder_number(folder);
 
-       int const size = get_tabfolder_numfolders(folder);
+       int const size = fl_get_tabfolder_numfolders(folder);
        for (int i = 0; i < size; ++i) {
                fl_set_folder_bynumber(folder, i+1);
                FL_FORM * leaf = fl_get_folder(folder);
@@ -113,8 +141,8 @@ void scale_tabfolder_horizontally(FL_OBJECT * folder, double factor)
 
 double get_scale_to_fit(FL_FORM * form)
 {
-       lyx::Assert(form);
-       
+       BOOST_ASSERT(form);
+
        double factor = get_scaling_factor(form);
        for (FL_OBJECT * ob = form->first; ob; ob = ob->next) {
                if (ob->objclass == FL_TABFOLDER)
@@ -127,15 +155,18 @@ double get_scale_to_fit(FL_FORM * form)
 
 void scale_form_horizontally(FL_FORM * form, double factor)
 {
-       lyx::Assert(form);
-       
+       BOOST_ASSERT(form);
+
        if (factor <= 1.0)
                return;
 
        fl_scale_form(form, factor, 1);
-       
+
        for (FL_OBJECT * ob = form->first; ob; ob = ob->next) {
                if (ob->objclass == FL_TABFOLDER)
                        scale_tabfolder_horizontally(ob, factor);
        }
 }
+
+} // namespace frontend
+} // namespace lyx