FLTK logo

[Library] r9006 - in branches/branch-3.0: include/fltk3 src/core

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.commit  ]
 
Previous Message ]Next Message ]

[Library] r9006 - in branches/branch-3.0: include/fltk3 src/core fltk-dev Aug 24, 2011  
 
Author: matt
Date: 2011-08-24 17:39:45 -0700 (Wed, 24 Aug 2011)
New Revision: 9006
Log:
fltk3::TextDisplay, added cut/copy/paste popup menu.

Modified:
   branches/branch-3.0/include/fltk3/TextDisplay.h
   branches/branch-3.0/include/fltk3/TextEditor.h
   branches/branch-3.0/src/core/Fl_Input_.cxx
   branches/branch-3.0/src/core/Fl_Text_Display.cxx

Modified: branches/branch-3.0/include/fltk3/TextDisplay.h
===================================================================
--- branches/branch-3.0/include/fltk3/TextDisplay.h	2011-08-24 23:55:27 UTC (rev 9005)
+++ branches/branch-3.0/include/fltk3/TextDisplay.h	2011-08-25 00:39:45 UTC (rev 9006)
@@ -121,6 +121,10 @@
   TextDisplay(int X, int Y, int W, int H, const char *l = 0);
   ~TextDisplay();
   
+  virtual int readonly() { return 1; }
+  
+  void handle_menu_event();
+  
   virtual int handle(int e);
   
   void buffer(fltk3::TextBuffer* buf);

Modified: branches/branch-3.0/include/fltk3/TextEditor.h
===================================================================
--- branches/branch-3.0/include/fltk3/TextEditor.h	2011-08-24 23:55:27 UTC (rev 9005)
+++ branches/branch-3.0/include/fltk3/TextEditor.h	2011-08-25 00:39:45 UTC (rev 9006)
@@ -70,6 +70,7 @@
     
     TextEditor(int X, int Y, int W, int H, const char* l = 0);
     ~TextEditor() { remove_all_key_bindings(); }
+    virtual int readonly() { return 0; }
     virtual int handle(int e);
     /**
      Sets the current insert mode; if non-zero, new text

Modified: branches/branch-3.0/src/core/Fl_Input_.cxx
===================================================================
--- branches/branch-3.0/src/core/Fl_Input_.cxx	2011-08-24 23:55:27 UTC (rev 9005)
+++ branches/branch-3.0/src/core/Fl_Input_.cxx	2011-08-25 00:39:45 UTC (rev 9006)
@@ -519,6 +519,12 @@
  Handles right mouse button clicks.
  */
 void fltk3::Input_::handle_menu_event() {
+
+  if (fltk3::focus() != this) {
+    fltk3::focus(this);
+    handle(fltk3::FOCUS);
+  }
+
   int m = mark(), p = position();
   if (m<p) { int x=p; p=m; m=x; }
   handle_mouse(x()+fltk3::box_dx(box()), y()+fltk3::box_dy(box()), 0, 0, 0);
@@ -528,13 +534,14 @@
     m = word_end(position());
   }
   position(p, m);
-  if (p!=m && (input_type()!=fltk3::SECRET_INPUT)) {
+  if (p!=m && (input_type()!=fltk3::SECRET_INPUT) && !readonly())
     ccp_menu[0].activate();
+  else
+    ccp_menu[0].deactivate();
+  if (p!=m && (input_type()!=fltk3::SECRET_INPUT))
     ccp_menu[1].activate();
-  } else {
-    ccp_menu[0].deactivate();
+  else
     ccp_menu[1].deactivate();
-  }
   if (!readonly() /*&& paste_buffer && *paste_buffer*/ ) // TODO: provide a function that can check if data is in the paste buffer
     ccp_menu[2].activate();
   else 

Modified: branches/branch-3.0/src/core/Fl_Text_Display.cxx
===================================================================
--- branches/branch-3.0/src/core/Fl_Text_Display.cxx	2011-08-24 23:55:27 UTC (rev 9005)
+++ branches/branch-3.0/src/core/Fl_Text_Display.cxx	2011-08-25 00:39:45 UTC (rev 9006)
@@ -37,6 +37,7 @@
 #include <fltk3/run.h>
 #include <fltk3/TextBuffer.h>
 #include <fltk3/TextDisplay.h>
+#include <fltk3/MenuItem.h>
 #include <fltk3/Window.h>
 #include <fltk3/Printer.h>
 
@@ -3542,8 +3543,66 @@
 }
 
 
+static fltk3::MenuItem ccp_menu[] = {
+  { "Cut", fltk3::COMMAND|'x', 0, (void*)1 },
+  { "Copy", fltk3::COMMAND|'c', 0, (void*)2 },
+  { "Paste", fltk3::COMMAND|'v', 0, (void*)3 },
+  { 0 }
+};
 
 /**
+ Handles right mouse button clicks.
+ */
+void fltk3::TextDisplay::handle_menu_event() {
+  fltk3::TextBuffer *buf = mBuffer;
+  if (!buf) return;
+  int m = buf->primary_selection()->end(), p = buf->primary_selection()->start();
+  int pc = xy_to_position(fltk3::event_x(), fltk3::event_y(), CHARACTER_POS);
+  if (!buf->primary_selection()->selected()) { p = m = insert_position(); }
+  if ( ( (p==m && p!=pc) || pc<p || pc>m )) {
+    p = word_start(pc); 
+    m = word_end(pc);
+    buf->primary_selection()->set(p, m);
+    insert_position(pc);
+  }
+  if (p!=m && !readonly())
+    ccp_menu[0].activate();
+  else
+    ccp_menu[0].deactivate();
+  if (p!=m)
+    ccp_menu[1].activate();
+  else
+    ccp_menu[1].deactivate();
+  if (!readonly() /*&& paste_buffer && *paste_buffer*/ ) // TODO: how do we know that this is read-only? provide a function that can check if data is in the paste buffer
+    ccp_menu[2].activate();
+  else 
+    ccp_menu[2].deactivate();
+  redraw();
+  fltk3::flush();
+  const fltk3::MenuItem *mi = ccp_menu->popup(fltk3::event_x(), fltk3::event_y());
+  if (mi) {
+    const char *copy;
+    switch (mi->argument()) {
+      case 1:
+        copy = buf->selection_text();
+        if (*copy) fltk3::copy(copy, strlen(copy), 1);
+        free((void*)copy);
+        buf->remove_selection();
+        break;
+      case 2: 
+        copy = buf->selection_text();
+        if (*copy) fltk3::copy(copy, strlen(copy), 1);
+        free((void*)copy);
+        break;
+      case 3: 
+        paste(*this, 1); 
+        break;
+    }
+  }
+}
+
+
+/**
  \brief Event handling.
  */
 int fltk3::TextDisplay::handle(int event) {
@@ -3590,6 +3649,10 @@
         fltk3::focus(this);
         handle(fltk3::FOCUS);
       }
+      if (((unsigned)fltk3::event_buttons())==fltk3::BUTTON3) {
+        handle_menu_event();
+        return 1;
+      }
       if (Group::handle(event)) return 1;
       if (fltk3::event_state()&fltk3::SHIFT) return handle(fltk3::DRAG);
       dragging = 1;

Direct Link to Message ]
 
     
Previous Message ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.