Back to index

texmacs  1.0.7.15
qt_widget.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : qt_widget.cpp
00004 * DESCRIPTION: QT widget class
00005 * COPYRIGHT  : (C) 2008  Massimiliano Gubinelli
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 "qt_widget.hpp"
00013 #include "qt_simple_widget.hpp"
00014 #include "qt_tm_widget.hpp"
00015 #include "qt_utilities.hpp"
00016 
00017 #include "QTMMenuHelper.hpp"
00018 #include "qt_renderer.hpp"
00019 
00020 widget the_keyboard_focus (NULL);
00021 
00022 
00023 /******************************************************************************
00024  * base widget
00025  ******************************************************************************/
00026 
00027 widget
00028 qt_widget_rep::plain_window_widget (string s, command ) {
00029   (void) s;
00030   return widget ();
00031 }
00032 
00033 widget
00034 qt_widget_rep::make_popup_widget () {
00035   return this;
00036 }
00037 
00038 widget
00039 qt_widget_rep::popup_window_widget (string s) {
00040   (void) s;
00041   return widget ();
00042 }
00043 
00044 
00045 /******************************************************************************
00046  * glue widget
00047  ******************************************************************************/
00048 
00049 class qt_glue_widget_rep: public qt_widget_rep {
00050 public:
00051   
00052   tree col;
00053   bool hx, vx;
00054   SI w,h;
00055   
00056   
00057   qt_glue_widget_rep (tree _col, bool _hx, bool _vx, SI _w, SI _h)
00058   : col(_col), hx(_hx), vx(_vx), w(_w), h(_h) 
00059   {}
00060   
00061   qt_glue_widget_rep () {};
00062   
00063   QPixmap render ();
00064   
00065   virtual QAction *as_qaction();
00066   virtual QWidget *as_qwidget();
00067 };
00068 
00069 
00070 QPixmap 
00071 qt_glue_widget_rep::render () {
00072   QSize s = QSize (w/PIXEL, h/PIXEL);
00073   QPixmap pxm(s);
00074   //cout << "glue (" << s.width() << "," << s.height() << ")\n";
00075   pxm.fill (Qt::transparent);
00076   QPaintDevice *pd = static_cast<QPaintDevice*>(&pxm);
00077 
00078   if (pd && !pxm.isNull()) {
00079     qt_renderer_rep *ren = the_qt_renderer();
00080     ren->begin (pd);
00081     rectangle r = rectangle (0, 0, s.width(), s.height());
00082     ren->set_origin(0,0);
00083     ren->encode (r->x1, r->y1);
00084     ren->encode (r->x2, r->y2);
00085     ren->set_clipping (r->x1, r->y2, r->x2, r->y1);
00086     
00087     if (col == "") {
00088       // do nothing
00089     } else {
00090       if (is_atomic (col)) {
00091         color c= named_color (col->label);
00092         ren->set_background (c);
00093         ren->set_color (c);
00094         ren->fill (r->x1, r->y2, r->x2, r->y1);
00095       } else {
00096         ren->set_shrinking_factor (5);
00097         int  old_a;
00098         tree old_bg= ren->get_background_pattern (old_a);
00099         ren->set_background_pattern (col);
00100         ren->clear_pattern (5*r->x1, 5*r->y2, 5*r->x2, 5*r->y1);
00101         ren->set_background_pattern (old_bg, old_a);
00102         ren->set_shrinking_factor (1);
00103       }
00104     }
00105     ren->end();
00106   }
00107 
00108   return pxm;  
00109 }
00110 
00111 QAction *
00112 qt_glue_widget_rep::as_qaction() {
00113   QAction *a= new QTMAction();
00114   a->setText(to_qstring(as_string(col)));
00115   QIcon icon;
00116 #if 0
00117   tree old_col = col;
00118   icon.addPixmap(render(), QIcon::Active, QIcon::On);
00119   col = "";
00120   icon.addPixmap(render(), QIcon::Normal, QIcon::On);
00121   col = old_col;
00122 #else
00123   icon.addPixmap (render ());
00124 #endif
00125   a->setIcon (icon);  
00126   a->setEnabled(false);
00127   return a;
00128 }
00129 
00130 QWidget *
00131 qt_glue_widget_rep::as_qwidget() {
00132   QLabel *w= new QLabel();
00133   w->setText(to_qstring(as_string(col)));
00134   QIcon icon;
00135   w->setPixmap (render ());  
00136 //  w->setEnabled(false);
00137   return w;
00138 }
00139 
00140 /******************************************************************************
00141 * Global functions we export for the creation of widgets by TeXmacs
00142 ******************************************************************************/
00143 
00144 // Window widgets creating functions
00145 
00149 widget
00150 plain_window_widget (widget w, string s, command q) {
00151   return concrete(w)->plain_window_widget (s, q);
00152 }
00153 
00154 widget
00155 popup_window_widget (widget w, string s) {
00156   // creates an undecorated window with name s and contents w
00157   return concrete(w)->popup_window_widget (s);
00158 }
00159 
00160 void
00161 destroy_window_widget (widget w) {
00162   // FIXME: Handle correcly
00163   // destroys a window as created by the above routines
00164   (void) w;
00165 
00166   // In the QT implementation explicitly destroying window widgets should not be necessary
00167   // since the widget itself destroy the Qt widget as soon as its destructor is called.
00168   // No memory leak should be caused by this trivial implementation.
00169 }
00170 
00171 /******************************************************************************
00172 * Top-level widgets (??)
00173 * See also message.hpp for specific messages for these widgets
00174 ******************************************************************************/
00175 
00182 widget
00183 texmacs_widget (int mask, command quit) {
00184   (void) mask; (void) quit; // FIXME: handle correctly mask and quit
00185   widget w= tm_new<qt_tm_widget_rep> (mask, quit);
00186   return w;
00187 }
00188 
00197 widget
00198 popup_widget (widget w) {
00199   return concrete(w)->make_popup_widget();
00200 }
00201 
00202 #include "qt_ui_element.hpp"
00203 
00204 widget
00205 glue_widget (bool hx, bool vx, SI w, SI h) {
00206   
00207   // an empty widget of minimal width w and height h and which is horizontally
00208   // resp. vertically extensible if hx resp. vx is true
00209   
00210   // glue_widget is used when detaching a canvas from the texmacs window
00211   // in view of attaching another one, e.g. when changing buffer.
00212   //return glue_widget("", hx, vx, w, h);
00213   // Bypass qt_glue_widget and use QSpacers instead:
00214   return qt_ui_element_rep::create (qt_ui_element_rep::glue_widget,
00215                                     hx, vx, w/PIXEL, h/PIXEL);
00216 }
00217 
00218 widget
00219 glue_widget (tree col, bool hx, bool vx, SI w, SI h) {
00220   return tm_new<qt_glue_widget_rep> (col, hx, vx, w, h);
00221 }
00222 
00223 /******************************************************************************
00224 *  Widgets which are not strictly required by TeXmacs
00225 *  their implementation is void
00226 ******************************************************************************/
00227 
00228 widget
00229 empty_widget () {
00230   // an empty widget of size zero
00231   NOT_IMPLEMENTED;
00232   return widget();
00233 }
00234 
00235 widget
00236 extend (widget w, array<widget> a) {
00237   (void) a;
00238   return w;
00239 }
00240 
00241 widget
00242 wait_widget (SI width, SI height, string message) {
00243   // a widget of a specified width and height, displaying a wait message
00244   // this widget is only needed when using the X11 plugin
00245   (void) width; (void) height; (void) message;
00246   return widget();
00247 }