2020-03-05 06:44:53 +00:00
|
|
|
#include <Array.h>
|
2020-06-21 14:15:17 +00:00
|
|
|
#include "ruamoko/qwaq/ui/event.h"
|
|
|
|
#include "ruamoko/qwaq/ui/draw.h"
|
|
|
|
#include "ruamoko/qwaq/ui/garray.h"
|
|
|
|
#include "ruamoko/qwaq/ui/group.h"
|
|
|
|
#include "ruamoko/qwaq/ui/view.h"
|
2020-03-05 06:44:53 +00:00
|
|
|
|
|
|
|
@implementation Group
|
2020-03-14 10:45:07 +00:00
|
|
|
|
2020-03-30 07:30:58 +00:00
|
|
|
+(Group *)withContext:(id<TextContext>)context owner:(View *)owner
|
|
|
|
{
|
|
|
|
return [[[self alloc] initWithContext:context owner:owner] autorelease];
|
|
|
|
}
|
|
|
|
|
2020-03-19 07:27:30 +00:00
|
|
|
-initWithContext: (id<TextContext>) context owner: (View *) owner
|
2020-03-05 06:44:53 +00:00
|
|
|
{
|
|
|
|
if (!(self = [super init])) {
|
|
|
|
return nil;
|
|
|
|
}
|
2020-03-19 07:27:30 +00:00
|
|
|
self.owner = owner;
|
2020-03-19 02:32:44 +00:00
|
|
|
self.context = context;
|
2020-03-19 07:27:30 +00:00
|
|
|
focused = -1;
|
2020-03-05 15:32:09 +00:00
|
|
|
views = [[Array array] retain];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2020-03-05 06:44:53 +00:00
|
|
|
-(void)dealloc
|
|
|
|
{
|
|
|
|
[views release];
|
2020-03-30 07:30:58 +00:00
|
|
|
[super dealloc];
|
2020-03-05 06:44:53 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 05:46:48 +00:00
|
|
|
-(id<TextContext>)context
|
|
|
|
{
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
-setContext: (id<TextContext>) context
|
|
|
|
{
|
|
|
|
self.context = context;
|
2020-03-30 13:01:17 +00:00
|
|
|
[views makeObjectsPerformSelector:@selector(setContext:)
|
|
|
|
withObject:context];
|
2020-03-26 05:46:48 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2020-03-05 06:44:53 +00:00
|
|
|
-insert: (View *) view
|
|
|
|
{
|
2020-03-06 02:53:40 +00:00
|
|
|
[views addObject: view];
|
2020-03-19 10:00:03 +00:00
|
|
|
[view setOwner: self];
|
2020-03-19 02:32:44 +00:00
|
|
|
[view setContext: context];
|
2020-03-05 06:44:53 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2020-03-24 04:33:28 +00:00
|
|
|
-insertDrawn: (View *) view
|
|
|
|
{
|
|
|
|
[self insert: view];
|
|
|
|
[view draw];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
-insertSelected: (View *) view
|
|
|
|
{
|
|
|
|
[self insertDrawn: view];
|
|
|
|
[self selectView: view];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2020-03-05 06:44:53 +00:00
|
|
|
-remove: (View *) view
|
|
|
|
{
|
|
|
|
int index = [views indexOfObject: view];
|
|
|
|
if (index != NotFound) {
|
|
|
|
if (focused == index) {
|
2020-03-24 04:33:28 +00:00
|
|
|
[self selectPrev];
|
|
|
|
if (focused == index) {
|
|
|
|
focused = -1;
|
|
|
|
}
|
2020-03-05 06:44:53 +00:00
|
|
|
} else if (focused > index) {
|
|
|
|
focused--;
|
|
|
|
}
|
|
|
|
[views removeObjectAtIndex: index];
|
2020-03-24 04:33:28 +00:00
|
|
|
if (mouse_within == view) {
|
|
|
|
mouse_within = nil;
|
|
|
|
}
|
|
|
|
if (mouse_grabbed == view) {
|
|
|
|
[self releaseMouse];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
makeFirst (Group *self, int viewIndex)
|
|
|
|
{
|
|
|
|
View *view = [self.views objectAtIndex: viewIndex];
|
|
|
|
|
2020-03-30 07:30:58 +00:00
|
|
|
// add before remove to avoid freeing view
|
2020-03-24 04:33:28 +00:00
|
|
|
[self.views addObject: view];
|
|
|
|
[self.views removeObjectAtIndex: viewIndex];
|
2020-03-26 02:18:00 +00:00
|
|
|
[view raise];
|
2020-03-24 04:33:28 +00:00
|
|
|
return [self.views count] - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
trySetFocus (Group *self, int viewIndex)
|
|
|
|
{
|
|
|
|
View *view = [self.views objectAtIndex:viewIndex];
|
|
|
|
if (([view state] & (sfDrawn | sfDisabled)) == sfDrawn
|
|
|
|
&& [view options] & ofCanFocus) {
|
2020-03-26 09:27:02 +00:00
|
|
|
if (!self.owner || ([self.owner state] & sfInFocus)) {
|
2020-03-24 04:33:28 +00:00
|
|
|
if (self.focused >= 0) {
|
|
|
|
[[self.views objectAtIndex: self.focused] loseFocus];
|
|
|
|
}
|
|
|
|
self.focused = viewIndex;
|
|
|
|
if ([view options] & ofMakeFirst) {
|
|
|
|
self.focused = makeFirst (self, viewIndex);
|
|
|
|
}
|
|
|
|
[view takeFocus];
|
2020-03-26 09:27:02 +00:00
|
|
|
} else {
|
|
|
|
self.focused = viewIndex;
|
2020-03-24 04:33:28 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
-takeFocus
|
|
|
|
{
|
|
|
|
if (focused >= 0) {
|
|
|
|
[[views objectAtIndex:focused] takeFocus];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
-loseFocus
|
|
|
|
{
|
|
|
|
if (focused >= 0) {
|
|
|
|
[[views objectAtIndex:focused] loseFocus];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
-selectNext
|
|
|
|
{
|
|
|
|
for (int i = focused + 1; i < [views count]; i++) {
|
|
|
|
if (trySetFocus (self, i)) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// hit end, start again at the beginning
|
|
|
|
for (int i = 0; i < focused; i++) {
|
|
|
|
if (trySetFocus (self, i)) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// did not find another view that can be focused
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
-selectPrev
|
|
|
|
{
|
|
|
|
for (int i = focused - 1; i >= 0; i--) {
|
|
|
|
if (trySetFocus (self, i)) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// hit end, start again at the beginning
|
|
|
|
for (int i = [views count] - 1; i > focused; i++) {
|
|
|
|
if (trySetFocus (self, i)) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// did not find another view that can be focused
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
-selectView:(View *)view
|
|
|
|
{
|
|
|
|
int index = [views indexOfObject: view];
|
|
|
|
if (index != NotFound) {
|
|
|
|
trySetFocus (self, index);
|
2020-03-05 06:44:53 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2021-06-07 06:56:20 +00:00
|
|
|
-(View *) owner
|
|
|
|
{
|
|
|
|
return owner;
|
|
|
|
}
|
|
|
|
|
2020-03-19 12:28:55 +00:00
|
|
|
-(Rect) rect
|
|
|
|
{
|
|
|
|
if (owner) {
|
|
|
|
return [owner rect];
|
|
|
|
}
|
|
|
|
return {[self origin], [self size]};
|
|
|
|
}
|
|
|
|
|
2021-06-01 03:59:01 +00:00
|
|
|
-(Rect) absRect
|
|
|
|
{
|
|
|
|
if (owner) {
|
|
|
|
return [owner absRect];
|
|
|
|
}
|
|
|
|
return {[self origin], [self size]};
|
|
|
|
}
|
|
|
|
|
2020-03-19 12:28:55 +00:00
|
|
|
-(Point) origin
|
|
|
|
{
|
|
|
|
if (owner) {
|
|
|
|
return [owner origin];
|
|
|
|
}
|
|
|
|
return {0, 0};
|
|
|
|
}
|
|
|
|
|
|
|
|
-(Extent) size
|
|
|
|
{
|
|
|
|
if (owner) {
|
|
|
|
return [owner size];
|
|
|
|
}
|
|
|
|
return [context size];
|
|
|
|
}
|
|
|
|
|
2020-03-06 02:53:40 +00:00
|
|
|
static BOOL
|
|
|
|
not_dont_draw (id aView, void *aGroup)
|
|
|
|
{
|
|
|
|
View *view = aView;
|
|
|
|
Group *group = (Group *) aGroup;
|
2020-03-14 10:45:07 +00:00
|
|
|
|
2020-03-19 02:32:44 +00:00
|
|
|
return !([view options] & ofDontDraw);
|
2020-03-06 02:53:40 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 06:44:53 +00:00
|
|
|
-draw
|
|
|
|
{
|
2020-03-06 02:53:40 +00:00
|
|
|
[views makeObjectsPerformSelector: @selector(draw)
|
|
|
|
if: not_dont_draw
|
|
|
|
with: self];
|
2020-03-05 06:44:53 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2020-03-19 02:32:44 +00:00
|
|
|
-redraw
|
|
|
|
{
|
2020-03-19 09:39:11 +00:00
|
|
|
if (owner) {
|
|
|
|
[owner redraw];
|
|
|
|
} else {
|
|
|
|
[self draw];
|
|
|
|
if (__obj_responds_to (context, @selector(refresh))) {
|
|
|
|
[(id)context refresh];
|
|
|
|
}
|
|
|
|
}
|
2020-03-19 02:32:44 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2021-06-01 03:59:01 +00:00
|
|
|
-updateAbsPos: (Point) absPos
|
|
|
|
{
|
|
|
|
for (int i = [views count]; i-- > 0; ) {
|
|
|
|
[[views objectAtIndex: i] updateAbsPos: absPos];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2020-03-23 11:14:32 +00:00
|
|
|
-resize: (Extent) delta
|
|
|
|
{
|
|
|
|
for (int i = [views count]; i-- > 0; ) {
|
|
|
|
[[views objectAtIndex: i] grow: delta];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2020-03-19 06:55:57 +00:00
|
|
|
static View *
|
|
|
|
find_mouse_view(Group *group, Point pos)
|
|
|
|
{
|
|
|
|
for (int i = [group.views count]; i--; ) {
|
|
|
|
View *v = [group.views objectAtIndex: i];
|
|
|
|
if ([v containsPoint: pos]) {
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2020-03-26 08:28:38 +00:00
|
|
|
static void
|
|
|
|
handlePositionalEvent (qwaq_event_t *event, View *view)
|
|
|
|
{
|
|
|
|
Point pos = [view origin];
|
|
|
|
int options = [view options];
|
|
|
|
|
|
|
|
if (options & ofRelativeEvents) {
|
|
|
|
event.mouse.x -= pos.x;
|
|
|
|
event.mouse.y -= pos.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
[view handleEvent: event];
|
|
|
|
|
|
|
|
if (options & ofRelativeEvents) {
|
|
|
|
event.mouse.x += pos.x;
|
|
|
|
event.mouse.y += pos.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 06:44:53 +00:00
|
|
|
-handleEvent: (qwaq_event_t *) event
|
|
|
|
{
|
|
|
|
if (event.what & qe_focused) {
|
|
|
|
if (focused >= 0) {
|
|
|
|
[[views objectAtIndex:focused] handleEvent: event];
|
|
|
|
}
|
|
|
|
} else if (event.what & qe_positional) {
|
2020-03-19 06:55:57 +00:00
|
|
|
if (mouse_grabbed) {
|
2020-03-26 08:28:38 +00:00
|
|
|
handlePositionalEvent (event, mouse_grabbed);
|
2020-03-19 06:55:57 +00:00
|
|
|
} else {
|
2020-03-23 14:59:54 +00:00
|
|
|
Point pos = {event.mouse.x, event.mouse.y};
|
|
|
|
View *mouse_view = find_mouse_view (self, pos);
|
2020-03-23 13:35:30 +00:00
|
|
|
if (mouse_within != mouse_view) {
|
2020-03-19 06:55:57 +00:00
|
|
|
[mouse_within onMouseLeave: pos];
|
2020-03-23 13:35:30 +00:00
|
|
|
[mouse_view onMouseEnter: pos];
|
|
|
|
mouse_within = mouse_view;
|
2020-03-19 06:55:57 +00:00
|
|
|
}
|
|
|
|
if (mouse_within) {
|
2020-03-26 08:28:38 +00:00
|
|
|
handlePositionalEvent (event, mouse_within);
|
2020-03-19 06:55:57 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-05 06:44:53 +00:00
|
|
|
} else {
|
|
|
|
// broadcast
|
|
|
|
[views makeObjectsPerformSelector: @selector(draw) withObject: event];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
2020-03-19 07:27:30 +00:00
|
|
|
|
|
|
|
-(void) grabMouse
|
|
|
|
{
|
|
|
|
mouse_grabbed = mouse_within;
|
|
|
|
[owner grabMouse];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) releaseMouse
|
|
|
|
{
|
2020-03-19 10:00:03 +00:00
|
|
|
mouse_grabbed = nil;
|
|
|
|
[owner releaseMouse];
|
2020-03-19 07:27:30 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 06:44:53 +00:00
|
|
|
@end
|