2002-06-24 22:24:57 +00:00
|
|
|
// -*- C++ -*-
|
|
|
|
/**
|
|
|
|
* \file XMiniBuffer.C
|
2002-09-05 15:14:23 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-06-24 22:24:57 +00:00
|
|
|
*
|
|
|
|
* \author Lars
|
|
|
|
* \author Asger and Juergen
|
2002-09-05 14:10:50 +00:00
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS
|
2002-06-24 22:24:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "XMiniBuffer.h"
|
2003-04-02 09:23:24 +00:00
|
|
|
#include "ControlCommandBuffer.h"
|
2003-04-02 14:20:30 +00:00
|
|
|
#include "freebrowser.h"
|
|
|
|
#include "xforms_helpers.h"
|
2003-04-02 09:23:24 +00:00
|
|
|
|
2002-06-24 22:24:57 +00:00
|
|
|
#include "gettext.h"
|
2003-04-02 09:23:24 +00:00
|
|
|
|
|
|
|
#include "frontends/Timeout.h"
|
2002-06-24 22:24:57 +00:00
|
|
|
|
2003-04-02 14:20:30 +00:00
|
|
|
#include "support/lstrings.h"
|
|
|
|
|
2002-06-24 22:24:57 +00:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#ifndef CXX_GLOBAL_CSTD
|
|
|
|
using std::isprint;
|
|
|
|
#endif
|
|
|
|
|
2002-07-19 20:56:31 +00:00
|
|
|
using std::endl;
|
2002-06-24 22:24:57 +00:00
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
|
2003-04-02 09:23:24 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// This creates the input widget for the minibuffer
|
|
|
|
FL_OBJECT * create_input_box(void * parent, int type,
|
|
|
|
FL_Coord, FL_Coord, FL_Coord, FL_Coord);
|
|
|
|
|
2003-04-02 14:20:30 +00:00
|
|
|
FL_FREEBROWSER * create_freebrowser(void * parent);
|
|
|
|
|
2003-04-02 09:23:24 +00:00
|
|
|
} // namespace anon
|
|
|
|
|
|
|
|
|
|
|
|
XMiniBuffer::XMiniBuffer(ControlCommandBuffer & control,
|
|
|
|
FL_Coord x, FL_Coord y, FL_Coord h, FL_Coord w)
|
|
|
|
: controller_(control),
|
|
|
|
info_shown_(false)
|
2002-06-24 22:24:57 +00:00
|
|
|
{
|
2003-04-02 09:23:24 +00:00
|
|
|
input_ = create_input_box(this, FL_NORMAL_INPUT, x, y, h, w);
|
2003-04-02 14:20:30 +00:00
|
|
|
freebrowser_.reset(create_freebrowser(this), fl_free_freebrowser);
|
|
|
|
|
2002-07-20 17:27:03 +00:00
|
|
|
info_timer_.reset(new Timeout(1500));
|
2002-07-19 20:56:31 +00:00
|
|
|
idle_timer_.reset(new Timeout(6000));
|
2002-07-22 20:57:58 +00:00
|
|
|
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();
|
2002-06-24 22:24:57 +00:00
|
|
|
messageMode();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-22 20:57:58 +00:00
|
|
|
// This is here so that scoped ptr will not require a complete type.
|
|
|
|
XMiniBuffer::~XMiniBuffer()
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2003-04-02 14:20:30 +00:00
|
|
|
void XMiniBuffer::freebrowserCB(int action)
|
2002-06-24 22:24:57 +00:00
|
|
|
{
|
2003-04-02 14:20:30 +00:00
|
|
|
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.
|
2003-04-03 09:48:03 +00:00
|
|
|
FL_OBJECT * browser = freebrowser_->browser;
|
2003-04-02 14:20:30 +00:00
|
|
|
string const str = getString(browser);
|
|
|
|
|
|
|
|
if (!str.empty()) {
|
|
|
|
// add a space so the user can type
|
|
|
|
// an argument immediately
|
|
|
|
set_input(str + ' ');
|
|
|
|
}
|
|
|
|
}
|
2002-06-24 22:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-04 17:45:35 +00:00
|
|
|
int XMiniBuffer::peek_event(FL_OBJECT * ob, int event,
|
|
|
|
int key, XEvent * /*xev*/)
|
2002-06-24 22:24:57 +00:00
|
|
|
{
|
|
|
|
switch (event) {
|
2002-07-19 20:56:31 +00:00
|
|
|
case FL_FOCUS:
|
|
|
|
messageMode(false);
|
|
|
|
break;
|
2002-06-24 22:24:57 +00:00
|
|
|
case FL_UNFOCUS:
|
|
|
|
messageMode();
|
|
|
|
break;
|
|
|
|
case FL_KEYBOARD:
|
|
|
|
{
|
|
|
|
string input;
|
2002-07-20 17:27:03 +00:00
|
|
|
if (info_shown_) {
|
|
|
|
info_timer_->stop();
|
|
|
|
info_timeout();
|
2002-06-24 22:24:57 +00:00
|
|
|
}
|
2002-07-22 20:57:58 +00:00
|
|
|
|
2002-07-19 20:56:31 +00:00
|
|
|
char const * tmp = fl_get_input(ob);
|
|
|
|
input = tmp ? tmp : "";
|
2002-06-24 22:24:57 +00:00
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
case XK_Down:
|
2002-07-10 15:03:25 +00:00
|
|
|
#ifdef XK_KP_Down
|
|
|
|
case XK_KP_Down:
|
|
|
|
#endif
|
2002-07-19 20:56:31 +00:00
|
|
|
{
|
|
|
|
string const h(controller_.historyDown());
|
|
|
|
if (h.empty()) {
|
2002-07-20 17:27:03 +00:00
|
|
|
show_info(_("[End of history]"), input, false);
|
2002-06-24 22:24:57 +00:00
|
|
|
} else {
|
2002-07-19 20:56:31 +00:00
|
|
|
set_input(h);
|
2002-06-24 22:24:57 +00:00
|
|
|
}
|
|
|
|
return 1;
|
2002-07-19 20:56:31 +00:00
|
|
|
}
|
2002-07-22 20:57:58 +00:00
|
|
|
|
2002-06-24 22:24:57 +00:00
|
|
|
case XK_Up:
|
2002-07-10 15:03:25 +00:00
|
|
|
#ifdef XK_KP_Up
|
|
|
|
case XK_KP_Up:
|
|
|
|
#endif
|
2002-07-19 20:56:31 +00:00
|
|
|
{
|
|
|
|
string const h(controller_.historyUp());
|
|
|
|
if (h.empty()) {
|
2002-07-20 17:27:03 +00:00
|
|
|
show_info(_("[Beginning of history]"), input, false);
|
2002-06-24 22:24:57 +00:00
|
|
|
} else {
|
2002-07-19 20:56:31 +00:00
|
|
|
set_input(h);
|
2002-06-24 22:24:57 +00:00
|
|
|
}
|
|
|
|
return 1;
|
2002-07-19 20:56:31 +00:00
|
|
|
}
|
2002-07-22 20:57:58 +00:00
|
|
|
|
2002-06-24 22:24:57 +00:00
|
|
|
case 9:
|
|
|
|
case XK_Tab:
|
|
|
|
{
|
2002-07-19 20:56:31 +00:00
|
|
|
string new_input;
|
|
|
|
vector<string> comp = controller_.completions(input, new_input);
|
2002-07-22 20:57:58 +00:00
|
|
|
|
2002-07-19 20:56:31 +00:00
|
|
|
if (comp.empty() && new_input == input) {
|
2002-07-20 17:27:03 +00:00
|
|
|
show_info(_("[no match]"), input);
|
2002-07-19 20:56:31 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-06-24 22:24:57 +00:00
|
|
|
|
|
|
|
if (comp.empty()) {
|
2002-07-19 20:56:31 +00:00
|
|
|
set_input(new_input);
|
2002-11-27 10:30:28 +00:00
|
|
|
show_info(_("[only completion]"), new_input + ' ');
|
2002-07-19 20:56:31 +00:00
|
|
|
break;
|
2002-06-24 22:24:57 +00:00
|
|
|
}
|
2002-07-22 20:57:58 +00:00
|
|
|
|
2002-07-19 20:56:31 +00:00
|
|
|
set_input(new_input);
|
|
|
|
|
2003-04-02 14:20:30 +00:00
|
|
|
// Fill freebrowser_'s browser with the list of
|
|
|
|
// available completions
|
2003-04-03 09:48:03 +00:00
|
|
|
FL_OBJECT * browser = freebrowser_->browser;
|
2003-04-02 14:20:30 +00:00
|
|
|
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.
|
2002-07-19 20:56:31 +00:00
|
|
|
int x,y,w,h;
|
2003-04-02 09:23:24 +00:00
|
|
|
fl_get_wingeometry(fl_get_real_object_window(input_),
|
2002-07-19 20:56:31 +00:00
|
|
|
&x, &y, &w, &h);
|
|
|
|
|
|
|
|
// asynchronous completion
|
2003-04-02 09:23:24 +00:00
|
|
|
int const air = input_->x;
|
2002-07-19 20:56:31 +00:00
|
|
|
x += air;
|
2003-04-02 09:23:24 +00:00
|
|
|
y += h - (input_->h + air);
|
|
|
|
w = input_->w;
|
2003-04-02 14:20:30 +00:00
|
|
|
h = 100;
|
|
|
|
|
|
|
|
fl_show_freebrowser(freebrowser_.get(), x, y-h, w, h);
|
2002-06-24 22:24:57 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
case 27:
|
|
|
|
case XK_Escape:
|
|
|
|
messageMode();
|
|
|
|
return 1;
|
|
|
|
case 13:
|
|
|
|
case XK_Return:
|
2002-07-10 15:03:25 +00:00
|
|
|
#ifdef XK_KP_Enter
|
|
|
|
case XK_KP_Enter:
|
|
|
|
#endif
|
2002-06-24 22:24:57 +00:00
|
|
|
{
|
2002-07-19 20:56:31 +00:00
|
|
|
messageMode();
|
|
|
|
redraw();
|
|
|
|
controller_.dispatch(input);
|
2002-06-24 22:24:57 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-19 20:56:31 +00:00
|
|
|
void XMiniBuffer::freeze()
|
|
|
|
{
|
|
|
|
// we must prevent peek_event, or we get an unfocus() when the
|
|
|
|
// containing form gets destroyed
|
2003-04-02 09:23:24 +00:00
|
|
|
fl_set_object_prehandler(input_, 0);
|
2002-07-19 20:56:31 +00:00
|
|
|
}
|
|
|
|
|
2002-07-22 20:57:58 +00:00
|
|
|
|
2002-07-20 17:27:03 +00:00
|
|
|
void XMiniBuffer::show_info(string const & info, string const & input, bool append)
|
2002-07-19 20:56:31 +00:00
|
|
|
{
|
|
|
|
stored_input_ = input;
|
2002-07-20 17:27:03 +00:00
|
|
|
info_shown_ = true;
|
|
|
|
if (append)
|
2002-11-27 10:30:28 +00:00
|
|
|
set_input(input + ' ' + info);
|
2002-07-20 17:27:03 +00:00
|
|
|
else
|
|
|
|
set_input(info);
|
|
|
|
info_timer_->start();
|
2002-07-19 20:56:31 +00:00
|
|
|
}
|
2002-07-22 20:57:58 +00:00
|
|
|
|
2002-07-19 20:56:31 +00:00
|
|
|
|
|
|
|
void XMiniBuffer::idle_timeout()
|
2002-06-24 22:24:57 +00:00
|
|
|
{
|
2003-04-02 09:23:24 +00:00
|
|
|
set_input(controller_.getCurrentState());
|
2002-06-24 22:24:57 +00:00
|
|
|
}
|
|
|
|
|
2002-07-22 20:57:58 +00:00
|
|
|
|
2002-07-20 17:27:03 +00:00
|
|
|
void XMiniBuffer::info_timeout()
|
2002-07-19 20:56:31 +00:00
|
|
|
{
|
2002-07-20 17:27:03 +00:00
|
|
|
info_shown_ = false;
|
2002-07-19 20:56:31 +00:00
|
|
|
set_input(stored_input_);
|
|
|
|
}
|
2002-06-24 22:24:57 +00:00
|
|
|
|
2002-07-22 20:57:58 +00:00
|
|
|
|
2002-07-19 20:56:31 +00:00
|
|
|
bool XMiniBuffer::isEditingMode() const
|
2002-06-24 22:24:57 +00:00
|
|
|
{
|
2003-04-02 09:23:24 +00:00
|
|
|
return input_->focus;
|
2002-06-24 22:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-19 20:56:31 +00:00
|
|
|
void XMiniBuffer::messageMode(bool on)
|
2002-06-24 22:24:57 +00:00
|
|
|
{
|
2002-07-19 20:56:31 +00:00
|
|
|
set_input("");
|
|
|
|
if (!on) {
|
2003-04-02 09:23:24 +00:00
|
|
|
fl_activate_object(input_);
|
|
|
|
fl_set_focus_object(input_->form, input_);
|
2002-07-19 20:56:31 +00:00
|
|
|
redraw();
|
|
|
|
idle_timer_->stop();
|
|
|
|
} else {
|
|
|
|
if (isEditingMode()) {
|
|
|
|
// focus back to the workarea
|
2003-04-02 09:23:24 +00:00
|
|
|
fl_set_focus_object(input_->form, 0);
|
2002-07-19 20:56:31 +00:00
|
|
|
idle_timer_->start();
|
|
|
|
}
|
|
|
|
}
|
2002-06-24 22:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void XMiniBuffer::redraw()
|
|
|
|
{
|
2003-04-02 09:23:24 +00:00
|
|
|
fl_redraw_object(input_);
|
2002-06-24 22:24:57 +00:00
|
|
|
XFlush(fl_display);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-19 20:56:31 +00:00
|
|
|
void XMiniBuffer::message(string const & str)
|
|
|
|
{
|
|
|
|
if (!isEditingMode())
|
|
|
|
set_input(str);
|
|
|
|
}
|
2002-06-24 22:24:57 +00:00
|
|
|
|
2002-07-22 20:57:58 +00:00
|
|
|
|
2002-06-24 22:24:57 +00:00
|
|
|
void XMiniBuffer::set_input(string const & str)
|
|
|
|
{
|
2003-04-02 09:23:24 +00:00
|
|
|
fl_set_input(input_, str.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-02 14:20:30 +00:00
|
|
|
extern "C"
|
|
|
|
void C_freebrowserCB(FL_FREEBROWSER * fb, int action)
|
|
|
|
{
|
|
|
|
if (!fb || !fb->parent)
|
|
|
|
return;
|
|
|
|
|
|
|
|
XMiniBuffer * ptr = static_cast<XMiniBuffer *>(fb->parent);
|
|
|
|
ptr->freebrowserCB(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-02 09:23:24 +00:00
|
|
|
FL_OBJECT * create_input_box(void * parent, int type,
|
|
|
|
FL_Coord x, FL_Coord y, FL_Coord w, FL_Coord h)
|
|
|
|
{
|
|
|
|
FL_OBJECT * 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 = parent;
|
|
|
|
obj->wantkey = FL_KEY_TAB;
|
|
|
|
|
|
|
|
return obj;
|
2002-06-24 22:24:57 +00:00
|
|
|
}
|
2003-04-02 09:23:24 +00:00
|
|
|
|
2003-04-02 14:20:30 +00:00
|
|
|
|
|
|
|
FL_FREEBROWSER * create_freebrowser(void * parent)
|
|
|
|
{
|
|
|
|
FL_FREEBROWSER * fb = fl_create_freebrowser(parent);
|
|
|
|
fb->want_printable = 1;
|
|
|
|
fb->callback = C_freebrowserCB;
|
|
|
|
return fb;
|
|
|
|
}
|
|
|
|
|
2003-04-02 09:23:24 +00:00
|
|
|
} // namespace anon
|