]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/XMiniBuffer.C
get rid of broken_header.h and some unneeded tests
[lyx.git] / src / frontends / xforms / XMiniBuffer.C
index e9e1f4f4dc1688b733c563e3f800c84b56660365..a6b228aa998282939e94cb690ed968520edf594f 100644 (file)
-// -*- C++ -*-
 /**
  * \file XMiniBuffer.C
- * Copyright 1995-2002 the LyX Team
- * Read the file COPYING
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
  * \author Lars
- * \author Asger and Juergen
+ * \author Asger and Jürgen
+ *
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
+#include "XMiniBuffer.h"
+#include "XFormsView.h"
+
+#include "freebrowser.h"
+#include "xforms_helpers.h"
+
+#include "controllers/ControlCommandBuffer.h"
 
-#include "frontends/xforms/DropDown.h"
-#include "frontends/xforms/XFormsView.h"
-#include "frontends/controllers/ControlCommandBuffer.h"
 #include "frontends/Timeout.h"
 
-#include "XMiniBuffer.h"
 #include "gettext.h"
-#include "debug.h"
-#include "bufferview_funcs.h"
 
 #include <boost/bind.hpp>
 
-#include <vector>
-
-#ifndef CXX_GLOBAL_CSTD
-using std::isprint;
-#endif
+using boost::shared_ptr;
 
-using std::endl;
 using std::vector;
+using std::string;
+
+namespace lyx {
+namespace frontend {
+
+namespace {
+
+/// This creates the input widget for the minibuffer
+FL_OBJECT * create_input_box(void * parent, int type);
 
+FL_FREEBROWSER * create_freebrowser(void * parent);
 
-XMiniBuffer::XMiniBuffer(XFormsView * v, ControlCommandBuffer & control,
-       FL_Coord x, FL_Coord y, FL_Coord h, FL_Coord w)
-       : controller_(control), view_(v),
-       info_suffix_shown_(false)
+} // namespace anon
+
+
+XMiniBuffer::XMiniBuffer(XFormsView & owner,
+                        ControlCommandBuffer & control)
+       : controller_(control),
+         info_shown_(false)
 {
-       input_obj_ = create_input_box(FL_NORMAL_INPUT, x, y, h, w);
-       suffix_timer_.reset(new Timeout(1500));
+       input_ = create_input_box(this, FL_NORMAL_INPUT);
+       freebrowser_.reset(create_freebrowser(this), fl_free_freebrowser);
+
+       // The minibuffer is 25 pixels high and is embedded inside a
+       // 2 pixel deep frame.
+       int const air = 2;
+
+       BoxList & boxlist = owner.getBox(XFormsView::Bottom)->children();
+       minibuffer_ = boxlist.push_back(Box(0,0));
+       shared_ptr<Box> center = embed(input_, minibuffer_->children(), widgets_, air);
+       center->set(Box::Expand);
+       center->setMinimumDimensions(0, 25);
+
+       owner.metricsUpdated.connect(boost::bind(&WidgetMap::updateMetrics,
+                                                &widgets_));
+
+       info_timer_.reset(new Timeout(1500));
        idle_timer_.reset(new Timeout(6000));
-       suffix_timer_->timeout.connect(boost::bind(&XMiniBuffer::suffix_timeout, this));
-       idle_timer_->timeout.connect(boost::bind(&XMiniBuffer::idle_timeout, this));
-       idle_timer_->start(); 
+       info_con = info_timer_->timeout.connect(boost::bind(&XMiniBuffer::info_timeout, this));
+       idle_con = idle_timer_->timeout.connect(boost::bind(&XMiniBuffer::idle_timeout, this));
+       idle_timer_->start();
        messageMode();
 }
 
 
-// thanks for nothing, xforms (recursive creation not allowed)
-void XMiniBuffer::dd_init()
-{
-       dropdown_.reset(new DropDown(the_buffer_));
-       dropdown_->result.connect(boost::bind(&XMiniBuffer::set_complete_input, this, _1));
-       dropdown_->keypress.connect(boost::bind(&XMiniBuffer::append_char, this, _1));
-}
+// This is here so that scoped ptr will not require a complete type.
+XMiniBuffer::~XMiniBuffer()
+{}
 
 
-XMiniBuffer::~XMiniBuffer()
+void XMiniBuffer::freebrowserCB(int action)
 {
+       if (action < 0 || action > 1)
+               // unrecognized action
+               return;
+
+       if (action == 0)
+               // The freebrowser has been hidden
+               return;
+
+       if (freebrowser_->last_printable) {
+               // Append this char to the current input contents
+               string input = getString(input_);
+               input += freebrowser_->last_printable;
+               fl_set_input(input_, input.c_str());
+
+       } else {
+               // Fill the input widget with the selected
+               // browser entry.
+               FL_OBJECT * browser = freebrowser_->browser;
+               string const str = getString(browser);
+
+               if (!str.empty()) {
+                       // add a space so the user can type
+                       // an argument immediately
+                       set_input(str + ' ');
+               }
+       }
 }
 
 
 int XMiniBuffer::peek_event(FL_OBJECT * ob, int event,
                            int key, XEvent * /*xev*/)
 {
-#if 0
-       if (dropdown_->isVisible()) {
-               return dropdown_->peek(xev);
-       }
-#endif
-
        switch (event) {
        case FL_FOCUS:
                messageMode(false);
@@ -84,11 +122,11 @@ int XMiniBuffer::peek_event(FL_OBJECT * ob, int event,
        case FL_KEYBOARD:
        {
                string input;
-               if (info_suffix_shown_) {
-                       suffix_timer_->stop();
-                       suffix_timeout();
+               if (info_shown_) {
+                       info_timer_->stop();
+                       info_timeout();
                }
+
                char const * tmp = fl_get_input(ob);
                input = tmp ? tmp : "";
 
@@ -100,13 +138,13 @@ int XMiniBuffer::peek_event(FL_OBJECT * ob, int event,
                {
                        string const h(controller_.historyDown());
                        if (h.empty()) {
-                               show_info_suffix(_("[End of history]"), input);
+                               show_info(_("[End of history]"), input, false);
                        } else {
                                set_input(h);
                        }
                        return 1;
                }
+
                case XK_Up:
 #ifdef XK_KP_Up
                case XK_KP_Up:
@@ -114,42 +152,56 @@ int XMiniBuffer::peek_event(FL_OBJECT * ob, int event,
                {
                        string const h(controller_.historyUp());
                        if (h.empty()) {
-                               show_info_suffix(_("[Beginning of history]"), input);
+                               show_info(_("[Beginning of history]"), input, false);
                        } else {
                                set_input(h);
                        }
                        return 1;
                }
+
                case 9:
                case XK_Tab:
                {
                        string new_input;
                        vector<string> comp = controller_.completions(input, new_input);
-                        
+
                        if (comp.empty() && new_input == input) {
-                               show_info_suffix(_("[no match]"), input);
+                               show_info(_("[no match]"), input);
                                break;
                        }
 
                        if (comp.empty()) {
                                set_input(new_input);
-                               show_info_suffix(("[only completion]"), new_input + " ");
+                               show_info(_("[only completion]"), new_input + ' ');
                                break;
                        }
-                                
+
                        set_input(new_input);
 
+                       // Fill freebrowser_'s browser with the list of
+                       // available completions
+                       FL_OBJECT * browser = freebrowser_->browser;
+                       fl_clear_browser(browser);
+                       vector<string>::const_iterator cit = comp.begin();
+                       vector<string>::const_iterator end = comp.end();
+                       for (; cit != end; ++cit) {
+                               fl_add_browser_line(browser, cit->c_str());
+                       }
+                       fl_select_browser_line(browser, 1);
+
+                       // Set the position of the freebrowser and display it.
                        int x,y,w,h;
-                       fl_get_wingeometry(fl_get_real_object_window(the_buffer_),
+                       fl_get_wingeometry(fl_get_real_object_window(input_),
                                           &x, &y, &w, &h);
 
                        // asynchronous completion
-                       int const air = the_buffer_->x;
+                       int const air = input_->x;
                        x += air;
-                       y += h - (the_buffer_->h + air);
-                       w = the_buffer_->w;
-                       dropdown_->select(comp, x, y, w);
+                       y += h - (input_->h + air);
+                       w = input_->w;
+                       h = 100;
+
+                       fl_show_freebrowser(freebrowser_.get(), x, y-h, w, h);
                        return 1;
                }
                case 27:
@@ -162,27 +214,9 @@ int XMiniBuffer::peek_event(FL_OBJECT * ob, int event,
                case XK_KP_Enter:
 #endif
                {
-#if 0
-                       // This will go in again in a little while
-                       // we need to be able to declare what types
-                       // of argumetns LFUN's should have first. (Lgb)
-                       // First check for match
-                       vector<string>::const_iterator cit =
-                               find(completion_.begin(),
-                                    completion_.end(),
-                                    input);
-                       if (cit == completion_.end()) {
-                               // no such func/item
-                               string const tmp = input + _(" [no match]");
-                               show_info_suffix(tmp, input);
-                       } else {
-#endif
                        messageMode();
                        redraw();
                        controller_.dispatch(input);
-# if 0
-                       }
-#endif
                        return 1;
                }
                default:
@@ -197,74 +231,42 @@ int XMiniBuffer::peek_event(FL_OBJECT * ob, int event,
 }
 
 
-extern "C" {
-
-       static
-       int C_XMiniBuffer_peek_event(FL_OBJECT * ob, int event,
-                                    FL_Coord, FL_Coord,
-                                    int key, void * xev)
-       {
-               XMiniBuffer * mini = static_cast<XMiniBuffer*>(ob->u_vdata);
-               return mini->peek_event(ob, event, key,
-                                       static_cast<XEvent *>(xev));
-       }
-}
-
-
-FL_OBJECT * XMiniBuffer::create_input_box(int type, FL_Coord x, FL_Coord y,
-                                         FL_Coord w, FL_Coord h)
-{
-       FL_OBJECT * obj;
-
-       the_buffer_ = obj = fl_add_input(type, x, y, w, h, "");
-       fl_set_object_boxtype(obj, FL_DOWN_BOX);
-       fl_set_object_resize(obj, FL_RESIZE_ALL);
-       fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
-       fl_set_object_color(obj, FL_MCOL, FL_MCOL);
-       fl_set_object_lsize(obj, FL_NORMAL_SIZE);
-
-       // To intercept Up, Down, Table for history
-       fl_set_object_prehandler(obj, C_XMiniBuffer_peek_event);
-       obj->u_vdata = this;
-       obj->wantkey = FL_KEY_TAB;
-
-       return obj;
-}
-
-
 void XMiniBuffer::freeze()
 {
        // we must prevent peek_event, or we get an unfocus() when the
        // containing form gets destroyed
-       fl_set_object_prehandler(input_obj_, 0);
+       fl_set_object_prehandler(input_, 0);
 }
 
-void XMiniBuffer::show_info_suffix(string const & suffix, string const & input)
+
+void XMiniBuffer::show_info(string const & info, string const & input, bool append)
 {
        stored_input_ = input;
-       info_suffix_shown_ = true;
-       set_input(input + " " + suffix); 
-       suffix_timer_->start();
+       info_shown_ = true;
+       if (append)
+               set_input(input + ' ' + info);
+       else
+               set_input(info);
+       info_timer_->start();
 }
+
 
 void XMiniBuffer::idle_timeout()
 {
-       set_input(currentState(view_->view()));
+       set_input(controller_.getCurrentState());
 }
 
-void XMiniBuffer::suffix_timeout()
+
+void XMiniBuffer::info_timeout()
 {
-       info_suffix_shown_ = false;
+       info_shown_ = false;
        set_input(stored_input_);
 }
 
+
 bool XMiniBuffer::isEditingMode() const
 {
-       return the_buffer_->focus;
+       return input_->focus;
 }
 
 
@@ -272,14 +274,14 @@ void XMiniBuffer::messageMode(bool on)
 {
        set_input("");
        if (!on) {
-               fl_activate_object(the_buffer_);
-               fl_set_focus_object(view_->getForm(), the_buffer_);
+               fl_activate_object(input_);
+               fl_set_focus_object(input_->form, input_);
                redraw();
                idle_timer_->stop();
        } else {
                if (isEditingMode()) {
                        // focus back to the workarea
-                       fl_set_focus_object(view_->getForm(), 0);
+                       fl_set_focus_object(input_->form, 0);
                        idle_timer_->start();
                }
        }
@@ -288,43 +290,74 @@ void XMiniBuffer::messageMode(bool on)
 
 void XMiniBuffer::redraw()
 {
-       fl_redraw_object(the_buffer_);
+       fl_redraw_object(input_);
        XFlush(fl_display);
 }
 
 
-void XMiniBuffer::append_char(char c)
+void XMiniBuffer::message(string const & str)
 {
-       if (!c || !isprint(c))
-               return;
+       if (!isEditingMode())
+               set_input(str);
+}
+
+
+void XMiniBuffer::set_input(string const & str)
+{
+       fl_set_input(input_, str.c_str());
+}
 
-       char const * tmp = fl_get_input(the_buffer_);
-       string str = tmp ? tmp : "";
 
-       str += c;
+namespace {
 
-       fl_set_input(the_buffer_, str.c_str());
+extern "C"
+int C_XMiniBuffer_peek_event(FL_OBJECT * ob, int event,
+                            FL_Coord, FL_Coord,
+                            int key, void * xev)
+{
+       XMiniBuffer * mini = static_cast<XMiniBuffer*>(ob->u_vdata);
+       return mini->peek_event(ob, event, key, static_cast<XEvent *>(xev));
 }
-void XMiniBuffer::set_complete_input(string const & str)
+
+
+extern "C"
+void C_freebrowserCB(FL_FREEBROWSER * fb, int action)
 {
-       if (!str.empty()) {
-               // add a space so the user can type
-               // an argument immediately
-               set_input(str + " ");
-       }
-} 
+       if (!fb || !fb->parent)
+               return;
 
-void XMiniBuffer::message(string const & str)
+       XMiniBuffer * ptr = static_cast<XMiniBuffer *>(fb->parent);
+       ptr->freebrowserCB(action);
+}
+
+
+FL_OBJECT * create_input_box(void * parent, int type)
 {
-       if (!isEditingMode())
-               set_input(str);
+       FL_OBJECT * obj = fl_add_input(type, 0, 0, 0, 0, "");
+       fl_set_object_boxtype(obj, FL_DOWN_BOX);
+       fl_set_object_resize(obj, FL_RESIZE_ALL);
+       fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
+       fl_set_object_color(obj, FL_MCOL, FL_MCOL);
+       fl_set_object_lsize(obj, FL_NORMAL_SIZE);
+
+       // To intercept Up, Down, Table for history
+       fl_set_object_prehandler(obj, C_XMiniBuffer_peek_event);
+       obj->u_vdata = parent;
+       obj->wantkey = FL_KEY_TAB;
+
+       return obj;
 }
 
-void XMiniBuffer::set_input(string const & str)
+
+FL_FREEBROWSER * create_freebrowser(void * parent)
 {
-       fl_set_input(the_buffer_, str.c_str());
+       FL_FREEBROWSER * fb = fl_create_freebrowser(parent);
+       fb->want_printable = 1;
+       fb->callback = C_freebrowserCB;
+       return fb;
 }
+
+} // namespace anon
+
+} // namespace frontend
+} // namespace lyx