Greg Ercolano wrote:
> Jeff Schiller wrote:
>> can someone point me to a quick example of how one might implement
>> the standard right-click context menu on a text widget (input or static text)
>> with the ability to copy/paste the selected text into the system clipboard?
> 
>       Here's one of probably many ways using FLTK 1.x.x:
>       http://seriss.com/people/erco/fltk/#PopupCopyPasteMenu
> 
>       I don't use FLTK2; if you need an example for that,
>       someone else here can probably point you at an example for that.

   BTW, here's another approach that's less code.

   Not sure though if this is 'legal', since it overlaps widgets
   by making an invisible Fl_Menu_Button over the Fl_Input.

   Maybe other folks can weigh in on the below approach.
   I *think* this is an intentional use of Fl_Menu_Button,
   as it is a technique shown in the test/menubar.cxx example.

   But I think the above approach on the website is maybe better,
   because it explicitly handles the events, without overlapping widgets.
   A little more code, but maybe cleaner design-wise.


#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Menu_Button.H>
#include <stdio.h>              // printf
//
// Approach #2: How to implement a copy/paste menu for Fl_Input -- erco 02/04/09
//
class MyInput : public Fl_Input {
    static void Copy_CB(Fl_Widget*, void *userdata) {
        printf("*** COPY ***\n");
        MyInput *in = (MyInput*)userdata;
        in->copy(0);    // text selection clipboard
        in->copy(1);    // copy/paste clipboard
    }
    static void Paste_CB(Fl_Widget*, void *userdata) {
        printf("*** PASTE ***\n");
        MyInput *in = (MyInput*)userdata;
        Fl::paste(*in);
    }
public:
    MyInput(int X,int Y,int W,int H,const char*L=0):Fl_Input(X,Y,W,H,L) {
        // Make an invisible menu button "over" the Fl_Input
        Fl_Menu_Button *mb = new Fl_Menu_Button(X,Y,W,H);
        mb->type(Fl_Menu_Button::POPUP3);               // enable right click 
to bring up menu
        mb->box(FL_NO_BOX);                             // make menu button 
invisible
        mb->add("Copy", 0, Copy_CB,  (void*)this);
        mb->add("Paste",0, Paste_CB, (void*)this);
    }
};
int main() {
    Fl_Window win(200,45,"Test");
    MyInput input(50,10,120,25,"Text:");
    win.show();
    return(Fl::run());
}


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

Reply via email to