Example #1
0
/*
Function: wz_send_event

Sends an event to a widget, and to any of its focused children, propagating down the tree.
The first widget that processes the event breaks the process.
Note that the first widget gets the event whether it is focused or not.

Returns:
1 if the event was handled by some widget, 0 if it was not
*/
int wz_send_event(WZ_WIDGET* wgt, ALLEGRO_EVENT* event)
{
	WZ_WIDGET* child = wgt->first_child;
	if(wgt->proc(wgt, event))
		return 1;
		
	while(child)
	{
		if ((child->flags & WZ_STATE_HAS_FOCUS) && wz_send_event(child, event))
			return 1;
		child = child->next_sib;
	}
	
	/*
	See if the unfocused ones would like some too
	*/
	child = wgt->first_child;
	while(child)
	{
		if (wz_send_event(child, event))
			return 1;
		child = child->next_sib;
	}
	
	return 0;
}
Example #2
0
/*
Function: wz_ask_parent_for_focus

Asks the parent to defocus everyone but the widget that calls this function.

Returns:
1 if the widget already has focus, or succesfully obtained the focus

0 if the widget cannot be focused
*/
int wz_ask_parent_for_focus(WZ_WIDGET* wgt)
{
	if(wgt->flags & WZ_STATE_HAS_FOCUS)
		return 1;

	if(wgt->flags & WZ_STATE_NOTWANT_FOCUS)
		return 0;

	if(wgt->flags & WZ_STATE_DISABLED)
		return 0;

	if(wgt->flags & WZ_STATE_HIDDEN)
		return 0;

	if(wgt->parent == 0)
	{
		ALLEGRO_EVENT event;
		wz_craft_event(&event, WZ_TAKE_FOCUS, wgt, 0);
		wz_send_event(wgt, &event);
	}
	else
	{
		ALLEGRO_EVENT event;

		if(!(wgt->parent->flags & WZ_STATE_HAS_FOCUS))
		{
			wz_ask_parent_for_focus(wgt->parent);
		}

		wz_craft_event(&event, WZ_WANT_FOCUS, wgt, 0);
		wz_send_event(wgt->parent, &event);
	}

	return 1;
}
Example #3
0
/*
Title: Widget

Section: Internal

Function: wz_widget_proc

Callback function that handles the operations of a general widget.
All widget procs call this function as their default handler.

Returns:

1 if the event was handled by the widget, 0 otherwise
*/
int wz_widget_proc(WZ_WIDGET* wgt, ALLEGRO_EVENT* event)
{
	int ret = 1;
	switch (event->type)
	{
		case WZ_HIDE:
		{
			wgt->flags |= WZ_STATE_HIDDEN;
			break;
		}
		case WZ_SHOW:
		{
			wgt->flags &= ~WZ_STATE_HIDDEN;
			break;
		}
		case WZ_DISABLE:
		{
			wgt->flags |= WZ_STATE_DISABLED;
			break;
		}
		case WZ_ENABLE:
		{
			wgt->flags &= ~WZ_STATE_DISABLED;
			break;
		}
		case WZ_UPDATE_POSITION:
		{
			if (wgt->parent)
			{
				wgt->local_x = wgt->parent->local_x + wgt->x;
				wgt->local_y = wgt->parent->local_y + wgt->y;
			}
			else
			{
				wgt->local_x = wgt->x;
				wgt->local_y = wgt->y;
			}
			break;
		}
		case WZ_DESTROY:
		{
			al_destroy_user_event_source(wgt->source);
			free(wgt->source);
			free(wgt);
			break;
		}
		case WZ_LOSE_FOCUS:
		{
			wgt->flags &= ~WZ_STATE_HAS_FOCUS;
			break;
		}
		case WZ_TAKE_FOCUS:
		{
			wz_focus(wgt, 0);
			wgt->flags |= WZ_STATE_HAS_FOCUS;
			if (wgt->first_child)
				wz_focus(wgt->first_child, 1);
			break;
		}
		case WZ_WANT_FOCUS:
		{
			WZ_WIDGET* child = wgt->first_child;
			while (child)
			{
				wz_focus(child, 0);
				child = child->next_sib;
			}
			{
			ALLEGRO_EVENT ev;
			wz_craft_event(&ev, WZ_TAKE_FOCUS, wgt, 0);
			
			child = wgt->first_child;
			wz_send_event((WZ_WIDGET*)event->user.data2, &ev);
			}
			break;
		}
		case ALLEGRO_EVENT_MOUSE_AXES:
		{
			if (wgt->flags & WZ_STATE_DISABLED)
			{
				ret = 0;
			}
			else if (event->mouse.dz != 0 && wgt->first_child == 0 && wgt->parent != 0)
			{
				if (event->mouse.dz > 0)
				{
					wz_ask_parent_to_focus_prev(wgt);
				}
				else
				{
					wz_ask_parent_to_focus_next(wgt);
				}
			}
			else
			{
				ret = 0;
			}
			break;
		}
		/* Switch through elements on Touch:
		case ALLEGRO_EVENT_TOUCH_BEGIN:
		{
			if (wgt->flags & WZ_STATE_DISABLED)
			{
				ret = 0;
			}
			else if (wgt->first_child == 0 && wgt->parent != 0)
			{
				wz_ask_parent_to_focus_next(wgt);
			}
			else
			{
				ret = 0;
			}
			break;
		}
		*/
		case ALLEGRO_EVENT_KEY_CHAR:
		{
			if(event->keyboard.keycode == wgt->shortcut.keycode && ((event->keyboard.modifiers & wgt->shortcut.modifiers) || wgt->shortcut.modifiers == 0))
			{
				ALLEGRO_EVENT ev;
				wz_craft_event(&ev, WZ_HANDLE_SHORTCUT, wgt, 0);
				wz_send_event(wgt, &ev);
			}
			else
			{
				switch (event->keyboard.keycode)
				{
					case ALLEGRO_KEY_TAB:
					{
						if (wgt->first_child != 0)
						{
							ret = 0;
						}
						else if (event->keyboard.modifiers & 1)
						{
							wz_ask_parent_to_focus_prev(wgt);
						}
						else
						{
							wz_ask_parent_to_focus_next(wgt);
						}
						break;
					}
					case ALLEGRO_KEY_UP:
					{
						if (wgt->first_child != 0)
						{
							ret = 0;
						}
						else if (wgt->parent != 0)
						{
							wz_ask_parent_for_focus(wz_get_widget_dir(wgt, 0));
						}
						else
							ret = 0;
						break;
					}
					case ALLEGRO_KEY_RIGHT:
					{
						if (wgt->first_child != 0)
						{
							ret = 0;
						}
						else if (wgt->parent != 0)
						{
							wz_ask_parent_for_focus(wz_get_widget_dir(wgt, 1));
						}
						else
							ret = 0;
						break;
					}
					case ALLEGRO_KEY_DOWN:
					{
						if (wgt->first_child != 0)
						{
							ret = 0;
						}
						else if (wgt->parent != 0)
						{
							wz_ask_parent_for_focus(wz_get_widget_dir(wgt, 2));
						}
						else
							ret = 0;
						break;
					}
					case ALLEGRO_KEY_LEFT:
					{
						if (wgt->first_child != 0)
						{
							ret = 0;
						}
						else if (wgt->parent != 0)
						{
							wz_ask_parent_for_focus(wz_get_widget_dir(wgt, 3));
						}
						else
							ret = 0;
						break;
					}
					default:
						ret = 0;
				}
			}
			break;
		}
		default:
			ret = 0;
	}
	return ret;
}
Example #4
0
/*
Function: wz_set_cursor_pos

Sets the cursor position
*/
void wz_set_cursor_pos(WZ_WIDGET* wgt, int pos)
{
	ALLEGRO_EVENT event;
	wz_craft_event(&event, WZ_SET_CURSOR_POS, 0, pos);
	wz_send_event(wgt, &event);
}
Example #5
0
/*
Function: wz_set_scroll_pos

Sets the scroll position

Parameters:
max - Pass 1 to say that pos is actually the max position, 0 otherwise
*/
void wz_set_scroll_pos(WZ_WIDGET* wgt, int pos, int max)
{
	ALLEGRO_EVENT event;
	wz_craft_event(&event, max ? WZ_SET_SCROLL_MAX_POS : WZ_SET_SCROLL_POS, 0, pos);
	wz_send_event(wgt, &event);
}
Example #6
0
/*
Function: wz_set_text

Sets the text of a widget. The widget makes a local copy of the text.
*/
void wz_set_text(WZ_WIDGET* wgt, ALLEGRO_USTR* text)
{
	ALLEGRO_EVENT event;
	wz_craft_event(&event, WZ_SET_TEXT, 0, (intptr_t)text);
	wz_send_event(wgt, &event);
}
Example #7
0
/*
Function: wz_trigger

Triggers a widget. What the widget does depends on the widget. Generally, it will send
an event that is characteristic of the events that it usually sends. For example,
triggering a button will simulate an impression of the button, causing it to send <WZ_BUTTON_PRESSED>
event.
*/
void wz_trigger(WZ_WIDGET* wgt)
{
	ALLEGRO_EVENT event;
	wz_craft_event(&event, WZ_TRIGGER, 0, 0);
	wz_send_event(wgt, &event);
}