Back to index

texmacs  1.0.7.15
tm_button.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : tm_button.cpp
00004 * DESCRIPTION: Text widgets for output only
00005 * COPYRIGHT  : (C) 1999  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "boxes.hpp"
00013 #include "Boxes/construct.hpp"
00014 #include "font.hpp"
00015 #include "tm_frame.hpp"
00016 #include "message.hpp"
00017 #ifdef AQUATEXMACS
00018 #include "Cocoa/aqua_simple_widget.h"
00019 #else
00020 #ifdef QTTEXMACS
00021 #include "Qt/qt_simple_widget.hpp"
00022 #else
00023 #include "Widkit/simple_wk_widget.hpp"
00024 #endif
00025 #endif
00026 
00027 /******************************************************************************
00028 * Getting extents of a typesetted tree
00029 * Application: get window size for widget tree
00030 ******************************************************************************/
00031 
00032 #include "gui.hpp"
00033 #include "drd_std.hpp"
00034 #include "drd_info.hpp"
00035 #include "convert.hpp"
00036 #include "formatter.hpp"
00037 #include "Format/format.hpp"
00038 #include "new_style.hpp"
00039 
00040 void use_modules (tree t);
00041 
00042 edit_env
00043 get_init_environment (tree doc, drd_info& drd) {
00044   hashmap<string,tree> h1 (UNINIT), h2 (UNINIT), h3 (UNINIT), h4 (UNINIT);
00045   edit_env env (drd, "none", h1, h2, h3, h4);
00046   env->write_default_env ();
00047   bool ok;
00048   tree t, style= extract (doc, "style");
00049   hashmap<string,tree> H;
00050   style_get_cache (style, H, t, ok);
00051   if (ok) {
00052     env->patch_env (H);
00053     ok= drd->set_locals (t);
00054     drd->set_environment (H);
00055   }
00056   if (!ok) {
00057     ASSERT (is_tuple (style), "tuple expected as style");
00058     tree t (USE_PACKAGE, A (style));
00059     env->exec (t);
00060     env->read_env (H);
00061     drd->heuristic_init (H);
00062   }
00063   use_modules (env->read (THE_MODULES));
00064   // FIXME: extract (doc, "init")
00065   // env->write (PAGE_TYPE, "a5");
00066   env->update ();
00067   return env;
00068 }
00069 
00070 tree
00071 tree_extents (tree doc) {
00072   drd_info drd ("none", std_drd);
00073   edit_env env= get_init_environment (doc, drd);
00074   tree t= extract (doc, "body");
00075   lazy lz= make_lazy (env, t, path ());
00076   format vf= make_query_vstream_width (array<line_item>(), array<line_item>());
00077   format rf= lz->query (LAZY_BOX, vf);
00078   SI w= ((format_vstream) rf)->width;
00079   box b= (box) lz->produce (LAZY_BOX, make_format_width (w));
00080   SI h= b->h ();
00081   w += env->get_length (PAGE_SCREEN_LEFT);
00082   w += env->get_length (PAGE_SCREEN_RIGHT);
00083   h += env->get_length (PAGE_SCREEN_TOP);
00084   h += env->get_length (PAGE_SCREEN_BOT);
00085   return tuple (as_tree ((w / (5*PIXEL)) + 1), as_tree ((h / (5*PIXEL)) + 1));
00086 }
00087 
00088 /******************************************************************************
00089 * Typesetted boxes as widgets
00090 ******************************************************************************/
00091 
00092 class box_widget_rep: public simple_widget_rep {
00093   box   b;
00094   color bg;
00095   bool  transparent;
00096   int   sf;
00097   int   dw, dh;
00098 
00099 public:
00100   box_widget_rep (box b, color bg, bool trans, int sf, int dw, int dh);
00101   operator tree ();
00102 
00103   void handle_get_size_hint (SI& w, SI& h);
00104   void handle_repaint (SI x1, SI y1, SI x2, SI y2);
00105 };
00106 
00107 box_widget_rep::box_widget_rep
00108   (box b2, color bg2, bool trans2, int sf2, int dw2, int dh2):
00109     simple_widget_rep (), b (b2),
00110     bg (bg2), transparent (trans2), sf (sf2),
00111     dw (dw2+2*PIXEL), dh (dh2+2*PIXEL) {}
00112 
00113 box_widget_rep::operator tree () {
00114   return tree (TUPLE, "box", (tree) b);
00115 }
00116 
00117 void
00118 box_widget_rep::handle_get_size_hint (SI& w, SI& h) {
00119   SI X1= b->x1, Y1= b->y1;
00120   SI X2= b->x2, Y2= b->y2;
00121   w = ((X2- X1+ sf- 1)/sf)+ 2*dw;
00122   h = ((Y2- Y1+ sf- 1)/sf)+ 2*dh;
00123   abs_round (w, h);
00124 }
00125 
00126 void
00127 box_widget_rep::handle_repaint (SI x1, SI y1, SI x2, SI y2) {
00128   SI w, h;
00129   handle_get_size_hint (w, h);
00130   renderer ren= get_renderer (this);
00131   if (!transparent) {
00132     ren->set_background (bg);
00133     ren->set_color (bg);
00134     ren->fill (x1, y1, x2, y2);
00135   }
00136   ren->set_shrinking_factor (sf);
00137   rectangles l (rectangle (0, 0, w, h));
00138   SI x= ((sf*w-b->w())>>1) - b->x1;
00139   SI y= ((sf*h-b->h())>>1) - b->y1 - sf*h;
00140   b->redraw (ren, path(), l, x, y);
00141   ren->set_shrinking_factor (1);
00142 }
00143 
00144 /******************************************************************************
00145 * Interface
00146 ******************************************************************************/
00147 
00148 widget
00149 box_widget (box b, bool tr) {
00150   color col= light_grey;
00151   return widget (tm_new<box_widget_rep> (b, col, tr, 6, 3*PIXEL, 3*PIXEL));
00152 }
00153 
00154 widget
00155 box_widget (scheme_tree p, string s, color col, bool trans, bool ink) {
00156   string family  = "roman";
00157   string fn_class= "mr";
00158   string series  = "medium";
00159   string shape   = "normal";
00160   int    sz      = 10;
00161   int    dpi     = 600;
00162   int    n       = arity (p);
00163   if ((n >= 1) && is_atomic (p[0])) family  = as_string (p[0]);
00164   if ((n >= 2) && is_atomic (p[1])) fn_class= as_string (p[1]);
00165   if ((n >= 3) && is_atomic (p[2])) series  = as_string (p[2]);
00166   if ((n >= 4) && is_atomic (p[3])) shape   = as_string (p[3]);
00167   if ((n >= 5) && is_atomic (p[4])) sz      = as_int (p[4]);
00168   if ((n >= 6) && is_atomic (p[5])) dpi     = as_int (p[5]);
00169   font fn= find_font (family, fn_class, series, shape, sz, dpi);
00170   box  b = text_box (decorate (), 0, s, fn, col);
00171   if (ink) b= resize_box (decorate (), b, b->x3, b->y3, b->x4, b->y4, true);
00172   return box_widget (b, trans);
00173 }
00174 
00175 tree enrich_embedded_document (tree body);
00176 
00177 widget
00178 texmacs_output_widget (tree doc) {
00179   doc= enrich_embedded_document (doc);
00180   drd_info drd ("none", std_drd);
00181   edit_env env= get_init_environment (doc, drd);
00182   tree t= extract (doc, "body");
00183   lazy lz= make_lazy (env, t, path ());
00184   format vf= make_query_vstream_width (array<line_item>(), array<line_item>());
00185   format rf= lz->query (LAZY_BOX, vf);
00186   SI w= ((format_vstream) rf)->width;
00187   box b= (box) lz->produce (LAZY_BOX, make_format_width (w));
00188   //cout << (b->w()>>8) << ", " << (b->h()>>8) << "\n";
00189   //SI dw1= env->get_length (PAGE_SCREEN_LEFT);
00190   //SI dw2= env->get_length (PAGE_SCREEN_RIGHT);
00191   //SI dh1= env->get_length (PAGE_SCREEN_BOT);
00192   //SI dh2= env->get_length (PAGE_SCREEN_TOP);
00193   color col= light_grey;
00194   return widget (tm_new<box_widget_rep> (b, col, false, 5, 0, 0));
00195 }