Handling mouse events on any FLTK Widget - user-interface

It is very easy to handle event with FLTK Button (Fl_Button). This is done using callback() method inherited from Widget class. But I tried using with other widgets like Fl_Box (widget that displays image or text) but not worked and there is no compile error because Fl_Box is also subclass of Widget so something like flbox_pointer->callback(mycallback) is valid.
In Java you can use mouse event on any component like JButton, JPanel, JLabel etc, but in C++ FLTK seems very difficult.
So, somebody can help me with piece of code to do this. I have already checked their docs but all examples of callbacks deal with Fl_Button, not other Widgets. The method that looks like it works is virtual int handle(int) but it forces to create class, extending then overriding it (something that I don't like for now!).

Related

How can I replace the SearchBar icon in a Xamarin.Forms custom renderer for GTK?

I am working on a Xamarin project that includes a build for GTK. I am attempting to create a custom renderer for many of the Controls, but am having trouble finding, accessing and changing the properties for the control. For example, I would like to replace the "magnifying glass" icon for the SearchBar control with something more similar to the default icon on the Android platform.
I've created the custom renderer:
namespace MyProject.GTK.CustomRenderers
{
public class CustomSearchBarRenderer : Xamarin.Forms.Platform.GTK.Renderers.SearchBarRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
{
base.OnElementChanged(e);
var searchBar = Control;
// How do I replace the image?
}
}
}
but from there I am at a loss as there are practically no resources on custom renderers for GTK. I've tried looking at the GTK.Renderers.SearchBarRenderer to see if the class its derived from contains any useful properties or methods, as well as trying to find something meaningful in the GTK documentation and the repository for the Xamarin.Forms.GTK package, to no avail. I'm just not really sure how to understand the inner workings of the controls in this build so I can't figure out what I should even be looking for. Any pointers or resources for this or any GTK specific custom renderer work would be much appreciated.
You can check Xamarin Forms GTK
SearchBar is implmented by the use of element called SearchEntry which uses ImageButton and the icon is set by below code
_searchButton.ImageWidget.Pixbuf = RenderIcon("gtk-find", IconSize.SmallToolbar, null); // Search icon
Refer
SearchEntry.GTK
SearchBar.GTK
This should help you begin modifying, if you can get access to SearchEntry in your custom renderer you can change icon, otherwise you will have to create your own search bar, which takes lot of effort.

How to draw a Cocoa focus ring without subclassing?

I'd like to draw a proper, modern animated focus ring around a control, which according to Q&A 1785, should be a simple matter of overriding the -drawFocusRingMask and -focusRingMaskBounds methods.
Trouble is, for this project I have to use Xojo, which can declare and invoke Cocoa methods, but doesn't give me any opportunity to actually create my own view subclass.
So, is there any way to get a proper focus ring without making an actual subclass? Some other methods, perhaps introduced after this 10.7 tech note, that get the job done? Or some sneaky way to inject a method into an existing class at runtime?
As one comment suggested, class_addMethod() would be right if you want to add an optional protocol method. The public macoslib project has some code that shows how to do that, just search for that name.
However, if the function is already implemented, then you cannot add another. In that case method swizzling is the solution. It's a common method to replace a selector'd function address with another, and then call the original one.
I don't seem to have an example in Xojo for that at hand, though.
Update
For standard Cocoa controls the simplest solution is to set the NSView property focusRingType accordingly (available in macoslib). Implementing drawFocusRingMask is only necessary for custom controls.

(NSWindowButton) NSWindow.standardWindowButton button not highlighting

When i use the following to create a button
NSWindow.standardWindowButton(NSWindowButton.ZoomButton, forStyleMask: 0)
I get a button that doesn't react to mouse hover events. I can augment this by creating a container view with a NSTrackingArea and manually triggering the highlight method but it produces a clicked state.
Is there a way to somehow force the button into the expected behavior state without the darkened background?
I have been trying to avoid subclassing NSButton, but it seems like i may have to, and I'm just in a state of denial.
I could not get this working for the life of me so my solution was to write my own custom buttons. This works fine for me because my NSWindow is already custom so there wont be a clash of style.
https://gist.github.com/icodeforlove/a334884e59784b4c2567
Another viable solution is to use https://github.com/indragiek/INAppStoreWindow as #xhruso00 mentioned, but it seemed like overkill.

Cocoa OSX custom widgets/controls

I want to know how can I create custom widgets/controls in Cocoa.
Not a full tutorial, but some guidance on what to start looking into. I'm confused by custom views, Core Animation, etc. I feel lost.
I see a lot of cool looking controls, like in Reeder App, or Sparrow etc. For example:
The left side is a collapsable menu that includes animations etc. How can I achieve something similar? I thought of using a WebView + HTML + JavaScript, but that doesn't seem like a very optimized solution.
Controls are views, so if custom views confuse you, you'll need to get that figured out before moving on to custom controls. Although you should really read the entire View Programming Guide, the section called Creating a Custom View will get you started on creating your own views. Try creating a simple view that draws a circle, for example, or the time.
When you've got views figured out, move on to custom controls. Most controls work about the same way. The user touches them, and the control responds by: a) tracking the user's input, b) changing its value, c) sending its action message to its target, and d) giving the user some feedback by redrawing itself. To get started, first make sure that you know how to use controls. Reading Control and Cell Programming Topics should help, and the section titled Subclassing NSControl covers (obviously) creating your own subclasses.
The example you provided is pretty clearly Apple's Mail.app. The view on the left side of the window might be an instance of NSOutlineView, or it might be a custom class. Either way, NSOutlineView would be a good starting point if you want to duplicate that functionality. NSOutlineView is a subclass of NSTableView, which in turn is a subclass of NSControl, which in turn is a subclass of NSView. Read Outline View Programming Topics for help getting started -- tables and outlines are extremely useful, but also more complicated to use than basic controls like buttons and text fields.
I know it's only a part of the UI, but I've recently coded something similar to the sidebar. If you look though the source-code it may give you some help on learning how to use custom controls and cells.
You can check it out on Github:
https://github.com/iluuu1994/ITSidebar

Global Mouse Moved Events in Cocoa

Is there a way to register for global mouse moved events in Cocoa? I was able to register for the events using Carbon's InstallEventHandler(), but would prefer a Cocoa equivalent. I have looked for NSNotificationCenter events, but there doesn't seem to be any public event names (are there private ones?)
Alternatively, is there a way to use NSTrackingArea for views with a clearColor background?
The app is Snow Leopard only.
In SnowLeopard there is a new class method on NSEvent which does exactly what you want: + (id)addGlobalMonitorForEventsMatchingMask:(NSEventMask)mask handler:(void (^)(NSEvent*))block. You’ll want mask = NSMouseMovedMask.
A similar question was already asked on StackOverflow:
How to make a transparent NSView subclass handle mouse events?
To summarize, the tansparent view method didn't work. Quartz Event Taps seem to be the best answer.
Here are some hints on working with taps:
Create the tap with CGEventTapCreate.
a) For the location (first) parameter you'll probably want to use kCGSessionEventTap.
b) For the placement (second) parameter you'll probably want kCGHeadInsertEventTap.
c) For the event mask parameter, try (1 << kCGEventMouseMoved).
Create a run loop source with CFMachPortCreateRunLoopSource, passing the event tap as the second parameter.
Add the run loop source to your run loop. Assuming you want it added to the main run loop, do:
CFRunLoopAddSource(CFRunLoopGetMain(), sourceFromStep2, kCFRunLoopDefaultMode);
Enable the event tap with CGEventTapEnable
If you want to track the mouse no matter where it is, you want a CGEventTap. There is no Cocoa equivalent. If you just want to track it in your application then you should explain why you're finding yourself unable to do so a little more thoroughly.

Resources