> > Before I discuss caret behavior.
> > I have a further related issue (it's all bound up in the same widget).
>
> The Fl_Double_Window does not get keyboard focus by default. You can =
> achieve that by returning 1 for FL_FOCUS and FL_UNFOCUS events.
>
> Fl_Input_ and derived widgets and Fl_Text_Display do that for you.=
>

Apologies and thanks to Matthias for FOCUS:UNFOCUS:return 1 advice.
  I had been cutting and splicing and didnt realise its importance.

Nevertheless there is STILL a PROBLEM. Sorry but I have to show

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Text_Display.H>
#define USING_CARET
class EventWindow : public Fl_Window
{
private:
  Fl_Text_Buffer*  mBuff;
  Fl_Text_Display* mDisp;
  bool             mKeyPressed;
  unsigned long    mKey;
  int handle_keydn(int event, int key, int state);
public:
  EventWindow();
  bool getKey(unsigned long& pKey);
  int handle(int pEvent);
#ifdef USING_CARET
  void showCaret(int pPos);
#endif
};
EventWindow::EventWindow() : Fl_Window(640,480,"Display")
{
  mBuff = new Fl_Text_Buffer();
  mDisp = new Fl_Text_Display(20,20,640-40,480-40);
  mDisp->buffer(mBuff); add(*mDisp);
  mBuff->text("line 0\nline 1\nline 2\nline 3\nline 4\nline 5\n");
  mKeyPressed = false;
  mKey = 0;
}
int EventWindow::handle(int pEvent)
{
  switch (pEvent) {
    case FL_FOCUS: case FL_UNFOCUS: case FL_KEYUP:
      return 1;
    case FL_KEYBOARD:
      return handle_keydn(pEvent,Fl::event_key(),Fl::event_state());
    default:
      return Fl_Window::handle(pEvent);
  };
}
int EventWindow::handle_keydn(int pEvent, int pKey,int pState)
{
printf("KeyDN Key = %d State = %d\n",pKey,pState);fflush(stdout);
  mKey = (unsigned long)pState + (unsigned long)pKey;
  mKeyPressed = true;
  return 1;
}
bool EventWindow::getKey(unsigned long& pKey)
{
  bool lRet = false;
  if (mKeyPressed)
  {
    switch (mKey)
    {
      default:
        pKey = mKey;
        lRet = true;
        case 0xFFE3: // left ctrl
        case 0xFFE4: // righ ctrl
        case 0xFFE9: // left alt
        case 0xFFEA: // left alt
        case 0xFFE1: // left shift
        case 0xFFE2: // righ shift
        break;
    }
    mKeyPressed = false;
    mKey = 0;
  }
  return lRet;
}
#ifdef USING_CARET
void EventWindow::showCaret(int pPos)
{
  mDisp->show_cursor();  // ENABLE CURSOR
  mDisp->cursor_color(0xFF000000); // CURSOR RED
  mDisp->cursor_style(Fl_Text_Display::HEAVY_CURSOR);
  mDisp->insert_position(pPos); // POSITION
  mDisp->take_focus(); // SHOW CARET ON STARTUP (EVEN IF NOT IN FOCUS)
}
#endif
int main()
{
  EventWindow* win  = new EventWindow();
  win->show();
  int lPos = 5;
#ifdef USING_CARET
  win->showCaret(lPos);
#endif
  int lRet = 0;
  while (1)
  {
    lRet = Fl::wait();
    unsigned long lKey = 0;
    if (win->getKey(lKey))
    {
      ++lPos;
      printf(" lPos = %d\n",lPos);fflush(stdout);
#ifdef USING_CARET
      win->showCaret(lPos);
#endif
    }
    if (!lRet)
    {
      break;
    }
  }
  return Fl::run();
}

try commenting out #define USING_CARET

1) trap keys in (Fl_Window/Double_Window) using above etc, WORKS OK (all keys 
produce keydown).
2) add Fl_Text_Display to (1) WORKS OK (all keys produce keydown).
3) add caret functionality using Greg's showCaret instructions
  DOESN'T WORK (not all keys produce keydown). Is this related to take_focus()?

  What is going on ??



_______________________________________________
fltk-dev mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-dev

Reply via email to