]> git.lyx.org Git - lyx.git/blobdiff - src/minibuffer.C
bug 183
[lyx.git] / src / minibuffer.C
index a347acec8e23893e275cfded86bd2d4c6a34c7dc..7df08768eb12a69c3e495282c5bdf19e6bf79c9d 100644 (file)
 #pragma implementation
 #endif
 
+// FIXME: temporary 
+#include "frontends/xforms/DropDown.h"
 #include "minibuffer.h"
 
 #include "support/lyxalgo.h"
 #include "support/filetools.h"
+#include "support/lstrings.h"
 #include "LyXView.h"
 #include "XFormsView.h"
 #include "gettext.h"
@@ -61,12 +65,26 @@ MiniBuffer::MiniBuffer(LyXView * o, FL_Coord x, FL_Coord y,
        deactivate();
 }
 
+// thanks for nothing, xforms (recursive creation not allowed) 
+void MiniBuffer::dd_init()
+{
+       dropdown_ = new DropDown(owner_, the_buffer);
+       dropdown_->result.connect(slot(this, &MiniBuffer::set_complete_input));
+}
+
+
+MiniBuffer::~MiniBuffer()
+{
+       delete dropdown_;
+}
 
 void MiniBuffer::stored_slot() 
 {
        if (stored_) {
                stored_ = false;
-               fl_set_input(the_buffer, stored_input.c_str());
+               set_input(stored_input);
        }
 }
 
@@ -82,6 +100,9 @@ void MiniBuffer::stored_set(string const & str)
 int MiniBuffer::peek_event(FL_OBJECT * ob, int event, int key)
 {
        switch (event) {
+       case FL_UNFOCUS:
+               deactivate();
+               break;
        case FL_KEYBOARD:
        {
                char const * tmp = fl_get_input(ob);
@@ -89,7 +110,7 @@ int MiniBuffer::peek_event(FL_OBJECT * ob, int event, int key)
                if (stored_) {
                        stored_timer.stop();
                        input = stored_input;
-                       fl_set_input(ob, input.c_str());
+                       set_input(input);
                        stored_ = false;
                }
                
@@ -101,19 +122,19 @@ int MiniBuffer::peek_event(FL_OBJECT * ob, int event, int key)
                        if (hist_iter == history_->end()) {
                                // no further history
                                stored_set(input);
-                               fl_set_input(ob, _("[End of history]"));
+                               set_input(_("[End of history]"));
                        } else {
-                               fl_set_input(ob, hist_iter->c_str());
+                               set_input((*hist_iter));
                        }
                        return 1; 
                case XK_Up:
                        if (hist_iter == history_->begin()) {
                                // no further history
                                stored_set(input);
-                               fl_set_input(ob, _("[Beginning of history]"));
+                               set_input(_("[Beginning of history]"));
                        } else {
                                --hist_iter;
-                               fl_set_input(ob, hist_iter->c_str());
+                               set_input((*hist_iter));
                        }
                        return 1; 
                case 9:
@@ -130,19 +151,20 @@ int MiniBuffer::peek_event(FL_OBJECT * ob, int event, int key)
                                // No matches
                                string const tmp = input + _(" [no match]");
                                stored_set(input);
-                               fl_set_input(ob, tmp.c_str());
+                               set_input(tmp);
                        } else if (comp.size() == 1) {
                                // Perfect match
                                string const tmp =
                                        comp[0] + _(" [sole completion]");
-                               stored_set(comp[0]);
-                               fl_set_input(ob, tmp.c_str());
+                               stored_set(comp[0] + " ");
+                               set_input(tmp);
                        } else {
                                // More that one match
                                // Find maximal avaliable prefix
                                string const tmp = comp[0];
                                string test(input);
-                               test += tmp[test.length()];
+                               if (tmp.length() > test.length())
+                                       test += tmp[test.length()];
                                while (test.length() < tmp.length()) {
                                        vector<string> vtmp;
                                        lyx::copy_if(comp.begin(),
@@ -155,12 +177,14 @@ int MiniBuffer::peek_event(FL_OBJECT * ob, int event, int key)
                                        }
                                        test += tmp[test.length()];
                                }
-                               fl_set_input(ob, test.c_str());
+                               set_input(test);
                                
-                               // How should the possible matches
-                               // be visualized?
-                               std::copy(comp.begin(), comp.end(),
-                                         std::ostream_iterator<string>(std::cerr, "\n"));
+                               int x,y,w,h;
+                               fl_get_wingeometry(fl_get_real_object_window(the_buffer),
+                                       &x, &y, &w, &h);
+                               // asynchronous completion 
+                               dropdown_->select(comp, x, y + h, w);
                        }
                        return 1; 
                }
@@ -188,13 +212,15 @@ int MiniBuffer::peek_event(FL_OBJECT * ob, int event, int key)
                                // no such func/item
                                stored_set(input);
                                string const tmp = input + _(" [no match]");
-                               fl_set_input(ob, tmp.c_str());
+                               set_input(tmp);
                        } else {
 #endif
                                // Return the inputted string
                                deactivate();
                                owner_->view()->focus(true);
-                               history_->push_back(input);
+                               if (!input.empty()) {
+                                       history_->push_back(input);
+                               }
                                stringReady.emit(input);
 # if 0
                        }
@@ -212,7 +238,7 @@ int MiniBuffer::peek_event(FL_OBJECT * ob, int event, int key)
                        {
                                stored_set(input);
                                string const tmp = input + _(" [no match]");
-                               fl_set_input(ob, tmp.c_str());
+                               set_input(tmp);
                                return 1;
                        }
                        }
@@ -232,20 +258,24 @@ int MiniBuffer::peek_event(FL_OBJECT * ob, int event, int key)
 }
 
 
-extern "C"
-int C_MiniBuffer_peek_event(FL_OBJECT * ob, int event, 
-                           FL_Coord, FL_Coord,
-                           int key, void * /*xev*/)
-{
-       MiniBuffer * mini = static_cast<MiniBuffer*>(ob->u_vdata);
-       return mini->peek_event(ob, event, key);
+extern "C" {
+       
+       static
+       int C_MiniBuffer_peek_event(FL_OBJECT * ob, int event, 
+                                   FL_Coord, FL_Coord,
+                                   int key, void * /*xev*/)
+       {
+               MiniBuffer * mini = static_cast<MiniBuffer*>(ob->u_vdata);
+               return mini->peek_event(ob, event, key);
+       }
+       
 }
 
 
 void MiniBuffer::prepare()
 {
        text.erase();
-       fl_set_input(the_buffer, "");
+       set_input("");
        activate();
        fl_set_focus_object(static_cast<XFormsView *>(owner_)->getForm(),
                            the_buffer);
@@ -269,7 +299,7 @@ FL_OBJECT * MiniBuffer::add(int type, FL_Coord x, FL_Coord y,
         obj->u_vdata = this;
         obj->wantkey = FL_KEY_TAB;
 
-       fl_set_input(the_buffer, text.c_str());
+       set_input(text);
        
        return obj;
 }
@@ -280,7 +310,7 @@ void MiniBuffer::message(string const & str)
        timer.restart();
        string const ntext = strip(str);
        if (!the_buffer->focus) {
-               fl_set_input(the_buffer, ntext.c_str());
+               set_input(ntext);
                text = ntext;
        }
 }
@@ -335,18 +365,37 @@ void MiniBuffer::init()
 void MiniBuffer::activate()
 {
        fl_activate_object(the_buffer);
-       fl_redraw_object(the_buffer);
+       redraw();
 }
 
 
 void MiniBuffer::deactivate()
 {
-       fl_redraw_object(the_buffer);
+       redraw();
        fl_deactivate_object(the_buffer);
+       XFlush(fl_display);
 }
 
 
 void MiniBuffer::redraw() 
 {
        fl_redraw_object(the_buffer);
+       XFlush(fl_display);
+}
+
+
+void MiniBuffer::set_complete_input(string const & str)
+{
+       if (!str.empty()) {
+               // add a space so the user can type
+               // an argument immediately
+               set_input(str + " ");
+       }
+}
+
+void MiniBuffer::set_input(string const & str)
+{
+       fl_set_input(the_buffer, str.c_str());
+       XFlush(fl_display);
 }