Back to index

texmacs  1.0.7.15
popup_widget.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : popup_widget.cpp
00004 * DESCRIPTION: These widgets are decorated as popup windows.
00005 *              A pointer grab is assumed to take place when they popup.
00006 *              They disappear (=>ungrab) when the mouse leaves the window
00007 *              at the 'quit' direction.
00008 * COPYRIGHT  : (C) 1999  Joris van der Hoeven
00009 *******************************************************************************
00010 * This software falls under the GNU general public license version 3 or later.
00011 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00012 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00013 ******************************************************************************/
00014 
00015 #include "window.hpp"
00016 #include "Widkit/layout.hpp"
00017 #include "Widkit/basic_widget.hpp"
00018 #include "Widkit/Event/attribute_event.hpp"
00019 
00020 /******************************************************************************
00021 * Popup widgets
00022 ******************************************************************************/
00023 
00024 class popup_widget_rep: public basic_widget_rep {
00025   gravity quit;
00026   bool    grabbed;
00027   bool    stick;
00028   bool    freeze;
00029 
00030 public:
00031   popup_widget_rep (wk_widget w, gravity quit);
00032   operator tree ();
00033 
00034   void handle_get_size (get_size_event ev);
00035   void handle_position (position_event ev);
00036   void handle_repaint (repaint_event ev);
00037   void handle_mouse_grab (mouse_grab_event ev);
00038   void handle_mouse (mouse_event ev);
00039   void handle_set_integer (set_integer_event ev);
00040   bool handle (event ev);
00041 };
00042 
00043 /******************************************************************************
00044 * Routines for popup widgets
00045 ******************************************************************************/
00046 
00047 popup_widget_rep::popup_widget_rep (wk_widget w, gravity quit2):
00048   basic_widget_rep (1), quit (quit2),
00049   grabbed (false), stick (false), freeze (false)
00050 {
00051   a[0]= w;
00052 }
00053 
00054 popup_widget_rep::operator tree () {
00055   return tree (TUPLE, "popup", (tree) a[0]);
00056 }
00057 
00058 void
00059 popup_widget_rep::handle_get_size (get_size_event ev) {
00060   a[0] << ev;
00061   ev->w += 6*PIXEL;
00062   ev->h += 6*PIXEL;
00063 }
00064 
00065 void
00066 popup_widget_rep::handle_position (position_event ev) {
00067   (void) ev;
00068   a[0] << emit_position (3*PIXEL, -3*PIXEL, w- 6*PIXEL, h- 6*PIXEL);
00069 }
00070 
00071 void
00072 popup_widget_rep::handle_repaint (repaint_event ev) { (void) ev;
00073   renderer ren= win->get_renderer ();
00074   layout_higher (ren, 0, -h, w, 0);
00075   layout_default (ren, PIXEL, PIXEL-h,
00076                 w-PIXEL, -PIXEL);
00077   layout_default (ren, 2*PIXEL, 2*PIXEL-h,
00078                 w-2*PIXEL, -2*PIXEL);
00079 }
00080 
00081 /*
00082 extern string pritty (tree t); // from x_display.cpp
00083 */
00084 
00085 void
00086 popup_widget_rep::handle_mouse_grab (mouse_grab_event ev) {
00087   if (ev->flag) {
00088     grabbed= true;
00089     freeze = false;
00090   }
00091 }
00092 
00093 void
00094 popup_widget_rep::handle_mouse (mouse_event ev) {
00095   string type= ev->type;
00096   SI     x= ev->x, y= ev->y;
00097 
00098   bool leaving=
00099     ((quit == north_west) && (y>0)) ||
00100     ((quit == north) && (y>0)) ||
00101     ((quit == west) && (x<0));
00102   bool pressed=
00103     ev->pressed ("left") || ev->pressed ("right");
00104 
00105   /*
00106   if (type != "move") {
00107     cout << "-> " << (grabbed? "grabbed": "non grabbed")
00108         << ", "  << (stick? "sticked": "not sticked")
00109         << ", "  << (freeze? "frozen": "not frozen")
00110         << ", "  << (pressed? "pressed": "non pressed") << "\n";
00111     cout << "   " << ((event) ev) << "\n";
00112     cout << "   " << pritty ((wk_widget) this) << "\n";
00113     cout << "   " << ((int) ((void*) this)) << "\n";
00114   }
00115   */
00116 
00117   if (freeze) { freeze= pressed; stick= !pressed; }
00118   stick = stick && (!pressed);
00119   if (grabbed) a[0] << emit_mouse (ev);
00120   if ((type != "leave") && (!stick) && (!freeze)) {
00121     if (wk_has_pointer_grab (this)) {
00122       grabbed= pressed && (!leaving);
00123       if (!grabbed) wk_ungrab_pointer (this);
00124     }
00125   }
00126 
00127   /*
00128   if (type != "move") {
00129     cout << "=> " << (grabbed? "grabbed": "non grabbed")
00130         << ", "  << (stick? "sticked": "not sticked")
00131         << ", "  << (freeze? "frozen": "not frozen")
00132         << ", "  << (pressed? "pressed": "non pressed") << "\n";
00133     cout << "   " << ((event) ev) << "\n";
00134     cout << "   " << pritty ((wk_widget) this) << "\n";
00135     cout << "   " << ((int) ((void*) this)) << "\n";
00136   }
00137   */
00138 }
00139 
00140 void
00141 popup_widget_rep::handle_set_integer (set_integer_event ev) {
00142   if (ev->which == "stick") { stick= (ev->i != 0); }
00143   else if (ev->which == "freeze") { freeze= (ev->i != 0); }
00144   else WK_FAILED ("could not set integer attribute " * ev->which);
00145 }
00146 
00147 bool
00148 popup_widget_rep::handle (event ev) {
00149   switch (ev->type) {
00150   case GET_SIZE_EVENT:
00151   case ATTACH_WINDOW_EVENT:
00152   case POSITION_EVENT:
00153   case UPDATE_EVENT:
00154   case INVALIDATE_EVENT:
00155   case MOUSE_EVENT:
00156   case REPAINT_EVENT:
00157     return basic_widget_rep::handle (ev);
00158   case MOUSE_GRAB_EVENT:
00159     handle_mouse_grab (ev);
00160     return true;
00161   case SET_INTEGER_EVENT:
00162     handle_set_integer (ev);
00163     return true;
00164   default:
00165     a[0] << ev;
00166     return true;
00167   }
00168 }
00169 
00170 /******************************************************************************
00171 * Interface
00172 ******************************************************************************/
00173 
00174 wk_widget
00175 popup_widget (wk_widget w, gravity quit) {
00176   return tm_new<popup_widget_rep> (w, quit);
00177 }