I have an application with custom widgets and custom event handling model (I'm rendering in OpenGL). I would like to implement a text edit view taking advantage of Cocoa text input structures, but I don't know how to generate NSEvent objects to pass to NSTextInputContext. In particular, I'm having problems providing a window number, graphic context and mouse cursor coordinates (since i have to provide them in the window's coordinate system). Probably graphic context isn't needed but mouse coordinates are necessary to handle mouse selection events.
Is there any way I can solve this?
Related
What function I need to override to draw custom focus ring around NSTableView(or I need to subclass NSScrollView what contains table)? May be there are some examples of custom focus ring drawing?
I want to build a simple forms designer in Cocoa and need to move controls around on a form using the mouse (click, hold, move around, release).
Do I need to inherit new classes from all control classes to intercept those events ? Is there a way to implement it generically for any control?
One way might be to have a single large custom view that fills all the space the controls will be in. Implement the necessary methods to implement mouse events in this view, doing hit detection on the control views and moving them around. This approach requires only 1 custom subclass of NSView, and you can use any views or controls you want to move around without subclassing them.
Write a custom view to contain the controls. Override -hitTest: to ignore the controls and return self instead. Then when you receive mouse events, figure out which control they apply to and move as appropriate.
This is a design question regarding an MVC implementation. I am creating a 2D graphic app using QT and OpenGL but I do not think the technology matters.
So my view is an openGL widget, whatever is to be drawn is stored n the model and the controller should modify the model and have the OpenGL widget redraw the scene.
The view should capture the following mouse events, MouseRelease, MouseDown and MouseMove and then transfer them to the controller to make the proper decision on what to do when the user clicks or drags the mouse.
I am debating between 2 approaches, incapsulate the mouse handling inside the OpenGL widget and just report the click and drag back to the controller?
Or transfer the mouse events as is to the controller and let it handle all the logic to determine the clicks and drags.
Any advise is very apreciated.
Thank you
I think the widget is going to be getting mouse coordinates in the viewport/"view space" coordinate system, which may not make much sense to the controller. I think your widget should convert the co-ordinates of any clicks and drags to world space, then pass them to the controller.
Why is this good? Because it avoids your controller needing any special knowledge of the viewport/widget, and so preserves encapulation. If you add more viewports/widgets or maybe even a console or a script that also wants to feed the controller, they can all pass their "instructions" in world space and the controller will function quite happily. Your viewport is already "aware" of "world space" and "view space" or it couldn't have rendered your model.
I want to use a free form shape (e.g. A partially transparent image) as a window backgound without the standard close and maximise buttons. Like the widgets do. Is that possible in OSX? I could not find any info on that or an app that uses this.
Thanks
Yes. You can do this by subclassing NSWindow to make it borderless and transparent. You'll also subclass NSView to draw the visible custom shape, then use an instance of this view as the window's content view. The result will be a window whose only visible parts will be the shape your content view draws.
Here's a good article with an example.
Is there a simple way to create a selectable NSRect in Cocoa? In need a rectangle that can be selected and stays selected after a mouse click.
Thanks.
NSRect is just a struct with a position and size. It's not an object that can actually do anything or have any properties other than a width and height. It sounds like what you want is to create an NSView that can be selected. (Here's Apple's Guide on the subject.)
Though not as immediate as you would like, you may be interested in the management of tracking rectangles and tracking areas performed by NSView class.
This mechanism allows you to define specific areas of your custom view. Then, an event is generated whenever the cursor enters or leaves the area, or a mouse button is pressed in this area (-mouseEntered:, -mouseExited:, -mouseDown:, -mouseUp:, -mouseDragged:, ... of NSResponder class). This up to you to define what you want your application do in response to these events (set the rectangle as selected and display it accordingly).
For an example implementation of this, take a look at the Sketch example included with the Apple developer tools (look in /Developer/Examples/AppKit). Sketch allows the user to create new graphics (including rectangles, but also ovals, lines, and text), select them, move them around in the document, etc. In particular, you'll probably want to look at the SKTGraphic class, which represents a single graphic object in the document, and the SKTGraphicView class, which is an NSView subclass that perform the actual layout and drawing, handling mouse events for dragging views around, etc.