[fxruby-users] Draw on top of a widget?

Jannis Pohlmann info at sten-net.de
Fri Apr 22 07:12:44 EDT 2005


David Peoples schrieb:

> Is there a way to draw an arbitrary text string "on top of" an FXRuby 
> widget, in this case an FXListBox? I want to overwrite a portion of 
> the text in the selection box with that same text but drawn as if 
> "selected". Can this be done in FXRuby?
>
> What am I trying to do? I'm making a version of the FXListBox widget 
> with a keyboard interface. As you type characters, the first item in 
> the list that matches the typed character(s) is selected. I have the 
> key capturing and list matching routine down, and the correct item is 
> getting displayed in the selection box. But I want to visually 
> indicate what portion of that item has matched the typed text, and 
> thus I want to draw just a portion of the item in the "selected" 
> state. Since the selection box portion of the FXListBox widget is an 
> FXButton under the skin in Fox and FXButtons don't have any way to 
> display partial selection like say an FXTextField would, I don't see 
> any way to do this directly in the FXListBox itself. Thus my idea of 
> this "draw on top" kludge.
>
> I know a much cleaner approach would be to make a Fox FXButton widget 
> that implements and displays subselection of the caption, make a new 
> Fox "MyFXListBox" that uses that altered FXButton, all in C++, and 
> make the glue code that imports that new widget into FXRuby. But my 
> C++ is really weak, and I don't know thing one about SWIG. If it turns 
> out that the "draw on top" kludge can't work, I'd appreciate seeing 
> the source for any  custom Fox C++ widgets that have been merged into 
> FXRuby, or at least pointers for getting started with SWIG.
>
> Thanks for your time.
>
> David Peoples

Hi.

That's very simple.

Subclass the widget you want to draw upon and overload onPaint(sender, 
sel, event) this way:

    def onPaint(sender, sel, event)
       super // Draws the widget
       dc = FXDCWindow.new(self, event)
       font = FXFont.new(getApp, "Verdana", 10) // whatever you like
       font.create
       dc.font = font
       dc.drawText(event.rect.x, event.rect.y, "Hello World") // choords 
have to be changed if you don't want to draw in the upper left corner
    end

Maybe you will have to add Responder functionality to your widget:

class MyList
    include Responder

    def initialize(...)
       FXMAPFUNC(SEL_PAINT, 0, "onPaint")
    end

    ...
end
      
Is this what you wanted to do?

Regards,
Jannis


More information about the fxruby-users mailing list