Back to index

texmacs  1.0.7.15
refresh_widget.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : refresh_widget.cpp
00004 * DESCRIPTION: Widgets which are capable of being refreshed
00005 * COPYRIGHT  : (C) 2012  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 "Widkit/attribute_widget.hpp"
00013 #include "Widkit/layout.hpp"
00014 #include "scheme.hpp"
00015 
00016 widget make_menu_widget (object wid);
00017 
00018 /******************************************************************************
00019 * Refresh widgets
00020 ******************************************************************************/
00021 
00022 class refresh_widget_rep: public basic_widget_rep {
00023   string tmwid;
00024   object curobj;
00025   widget cur;
00026   hashmap<object,widget> cache;
00027 public:
00028   refresh_widget_rep (string tmwid);
00029   operator tree ();
00030   bool recompute ();
00031   void handle_refresh (refresh_event ev);
00032 };
00033 
00034 refresh_widget_rep::refresh_widget_rep (string tmwid2):
00035   basic_widget_rep (1), tmwid (tmwid2),
00036   curobj (false), cur (), cache (widget ()) {
00037     (void) recompute ();
00038     a[0]= concrete (cur); }
00039 
00040 refresh_widget_rep::operator tree () {
00041   return tree (TUPLE, "refresh", tmwid);
00042 }
00043 
00044 bool
00045 refresh_widget_rep::recompute () {
00046   string s= "'(vertical (link " * tmwid * "))";
00047   eval ("(lazy-initialize-force)");
00048   //cout << "Recompute " << tmwid << "\n";
00049   object xwid= call ("menu-expand", eval (s));
00050   //cout << "xwid= " << xwid << "\n";
00051   if (cache->contains (xwid)) {
00052     //if (curobj == xwid) cout << "Same " << s << "\n";
00053     if (curobj == xwid) return false;
00054     curobj= xwid;
00055     cur   = cache [xwid];
00056     //cout << "Cached " << s << "\n";
00057     return true;
00058   }
00059   else {
00060     curobj= xwid;
00061     //cout << "Compute " << s << "\n";
00062     object uwid= eval (s);
00063     //cout << "uwid= " << uwid << "\n";
00064     cur= make_menu_widget (uwid);
00065     //cout << "cur= " << cur << "\n";
00066     cache (xwid)= cur;
00067     return true;
00068   }
00069 }
00070 
00071 void
00072 refresh_widget_rep::handle_refresh (refresh_event ev) { (void) ev;
00073   if (recompute ()) {
00074     SI ww1= a[0]->w, hh1= a[0]->h;
00075     SI ww2= a[0]->w, hh2= a[0]->h;
00076     a[0] << get_size (ww1, hh1);
00077     a[0]= concrete (cur);
00078     a[0] << get_size (ww1, hh1);
00079     if (attached ()) {
00080       if (!a[0]->attached () || a[0]->win != win)
00081         a[0] << emit_attach_window (win);
00082       if (ww1 == ww2 && hh1 == hh2) this << emit_update ();
00083       else concrete (this->win->get_widget ()) << emit_update ();
00084     }
00085   }
00086 }
00087 
00088 /******************************************************************************
00089 * Refresh widget
00090 ******************************************************************************/
00091 
00092 wk_widget
00093 refresh_wk_widget (string tmwid) {
00094   return tm_new<refresh_widget_rep> (tmwid);
00095 }