1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
  
     | 
    
      
/*
Copyright (C) 2007 ezQuake team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
$Id: ez_scrollpane.c,v 1.78 2007/10/27 14:51:15 cokeman1982 Exp $
*/
#include "quakedef.h"
#include "keys.h"
#include "utils.h"
#include "common_draw.h"
#include "ez_controls.h"
#include "ez_scrollpane.h"
#ifdef _MSC_VER
#pragma warning( disable : 4189 )
#endif
// =========================================================================================
// Scrollpane
// =========================================================================================
//
// Scrollpane - Adjusts the scrollpanes target to fit in between the scrollbars.
//
static void EZ_scrollpane_AdjustTargetSize(ez_scrollpane_t *scrollpane)
{
	ez_control_t *scrollpane_ctrl	= (ez_control_t *)scrollpane;
	qbool show_v	= (scrollpane->ext_flags & always_h_scrollbar) || (scrollpane->int_flags & show_v_scrollbar);
	qbool show_h	= (scrollpane->ext_flags & always_v_scrollbar) || (scrollpane->int_flags & show_h_scrollbar);
	int rh_size_v	= (scrollpane_ctrl->ext_flags & control_resize_v ? scrollpane_ctrl->resize_handle_thickness : 0); 
	int rh_size_h	= (scrollpane_ctrl->ext_flags & control_resize_h ? scrollpane_ctrl->resize_handle_thickness : 0); 
	// Make sure we don't listen to the targets OnResize event when resizing it ourselves
	// or we'll get a stack overflow due to an infinite loop :S
	scrollpane->int_flags |= resizing_target;
	
	EZ_control_SetSize(scrollpane->target, 
						(scrollpane_ctrl->width  - (show_v ? scrollpane->scrollbar_thickness : 0) - rh_size_h),
						(scrollpane_ctrl->height - (show_h ? scrollpane->scrollbar_thickness : 0) - rh_size_v));
	scrollpane->int_flags &= ~resizing_target;
}
//
// Scrollpane - Resize the scrollbars based on the targets state.
//
static void EZ_scrollpane_ResizeScrollbars(ez_scrollpane_t *scrollpane)
{
	ez_control_t *scrollpane_ctrl	= (ez_control_t *)scrollpane;
	ez_control_t *v_scroll_ctrl		= (ez_control_t *)scrollpane->v_scrollbar;
	ez_control_t *h_scroll_ctrl		= (ez_control_t *)scrollpane->h_scrollbar;
//	qbool size_changed	= false;
	qbool show_v		= (scrollpane->ext_flags & always_h_scrollbar) || (scrollpane->int_flags & show_v_scrollbar);
	qbool show_h		= (scrollpane->ext_flags & always_v_scrollbar) || (scrollpane->int_flags & show_h_scrollbar);
	int rh_size_v		= (scrollpane_ctrl->ext_flags & control_resize_v ? scrollpane_ctrl->resize_handle_thickness : 0); 
	int rh_size_h		= (scrollpane_ctrl->ext_flags & control_resize_h ? scrollpane_ctrl->resize_handle_thickness : 0); 
	EZ_control_SetVisible((ez_control_t *)scrollpane->v_scrollbar, show_v);
	EZ_control_SetVisible((ez_control_t *)scrollpane->h_scrollbar, show_h);
	// Resize the scrollbars depending on if both are shown or not.
	EZ_control_SetSize(v_scroll_ctrl,
						scrollpane->scrollbar_thickness, 
						scrollpane_ctrl->height - (show_h ? scrollpane->scrollbar_thickness : 0) - (2 * rh_size_v));
	EZ_control_SetSize(h_scroll_ctrl,
						scrollpane_ctrl->width - (show_v ? scrollpane->scrollbar_thickness : 0) - (2 * rh_size_h), 
						scrollpane->scrollbar_thickness);
	// Resize the target control so that it doesn't overlap with the scrollbars.
	if (scrollpane->target)
	{
		// Since this resize operation is going to raise a new OnResize event on the target control
		// which in turn calls this function again we only change the size of the target to fit
		// within the scrollbars when the scrollbars actually changed size. Otherwise we'd get an infinite recursion.
		EZ_scrollpane_AdjustTargetSize(scrollpane);
	}
}
//
// Scrollpane - Determine if the scrollbars should be visible based on the target controls state.
//
static void EZ_scrollpane_DetermineScrollbarVisibility(ez_scrollpane_t *scrollpane)
{
	SET_FLAG(scrollpane->int_flags, show_v_scrollbar, (scrollpane->target->height <= scrollpane->target->virtual_height_min));
	SET_FLAG(scrollpane->int_flags, show_h_scrollbar, (scrollpane->target->width <= scrollpane->target->virtual_width_min));
}
// TODO : Add an event handler for handling when the target control changes it's min virtual size, we might need to show/hide the scrollbars if that happens.
//
// Scrollpane - Target changed it's virtual size.
//
static int EZ_scrollpane_OnTargetVirtualResize(ez_control_t *self, void *payload, void *ext_event_info)
{
	ez_scrollpane_t *scrollpane = NULL;
	if (!self->parent || (self->parent->CLASS_ID != EZ_SCROLLPANE_ID))
	{
		Sys_Error("EZ_scrollpane_OnTargetVirtualResize(): Target parent is not a scrollpane!.\n");
	}
	scrollpane = (ez_scrollpane_t *)self->parent;
	// Check if we should show/hide the scrollbars.
	EZ_scrollpane_DetermineScrollbarVisibility(scrollpane);
	return 0;
}
//
// Scrollpane - Creates a new Scrollpane and initializes it.
//
ez_scrollpane_t *EZ_scrollpane_Create(ez_tree_t *tree, ez_control_t *parent,
							  char *name, char *description,
							  int x, int y, int width, int height,
							  ez_control_flags_t flags)
{
	ez_scrollpane_t *scrollpane = NULL;
	// We have to have a tree to add the control to.
	if (!tree)
	{
		return NULL;
	}
	scrollpane = (ez_scrollpane_t *)Q_malloc(sizeof(ez_scrollpane_t));
	memset(scrollpane, 0, sizeof(ez_scrollpane_t));
	EZ_scrollpane_Init(scrollpane, tree, parent, name, description, x, y, width, height, flags);
	
	return scrollpane;
}
//
// Scrollpane - Initializes a Scrollpane.
//
void EZ_scrollpane_Init(ez_scrollpane_t *scrollpane, ez_tree_t *tree, ez_control_t *parent,
							  char *name, char *description,
							  int x, int y, int width, int height,
							  ez_control_flags_t flags)
{
	int rh_size_h = 0;
	int rh_size_v = 0;
	ez_control_t *scrollpane_ctrl = (ez_control_t *)scrollpane;
	// Initialize the inherited class first.
	EZ_control_Init(&scrollpane->super, tree, parent, name, description, x, y, width, height, flags);
	// Initilize the button specific stuff.
	((ez_control_t *)scrollpane)->CLASS_ID	= EZ_SCROLLPANE_ID;
	((ez_control_t *)scrollpane)->ext_flags	|= (flags | control_focusable | control_contained | control_resizeable);
	// Override control events.
	CONTROL_REGISTER_EVENT(scrollpane, EZ_scrollpane_OnResize, OnResize, ez_control_t);
	// Scrollpane specific events.
	CONTROL_REGISTER_EVENT(scrollpane, EZ_scrollpane_OnTargetChanged, OnTargetChanged, ez_scrollpane_t);
	CONTROL_REGISTER_EVENT(scrollpane, EZ_scrollpane_OnScrollbarThicknessChanged, OnScrollbarThicknessChanged, ez_scrollpane_t);
	scrollpane->scrollbar_thickness = 10;
	rh_size_h = (scrollpane_ctrl->ext_flags & control_resize_h) ? scrollpane_ctrl->resize_handle_thickness : 0;
	rh_size_v = (scrollpane_ctrl->ext_flags & control_resize_v) ? scrollpane_ctrl->resize_handle_thickness : 0;
	EZ_control_SetMinVirtualSize(scrollpane_ctrl, 1, 1);
	scrollpane->int_flags |= (show_h_scrollbar | show_v_scrollbar);
	// Create vertical scrollbar.
	{
		scrollpane->v_scrollbar = EZ_scrollbar_Create(tree, scrollpane_ctrl, "Vertical scrollbar", "", 0, 0, 10, 10, control_anchor_viewport);
		EZ_control_SetVisible((ez_control_t *)scrollpane->v_scrollbar, true);
		EZ_control_SetPosition((ez_control_t *)scrollpane->v_scrollbar, -rh_size_h, rh_size_v);
		EZ_control_SetAnchor((ez_control_t *)scrollpane->v_scrollbar, (anchor_top | anchor_bottom | anchor_right));
		EZ_scrollbar_SetTargetIsParent(scrollpane->v_scrollbar, false);
 
		EZ_control_AddChild(scrollpane_ctrl, (ez_control_t *)scrollpane->v_scrollbar);
	}
	// Create horizontal scrollbar.
	{
		scrollpane->h_scrollbar = EZ_scrollbar_Create(tree, (ez_control_t *)scrollpane, "Horizontal scrollbar", "", 0, 0, 10, 10, control_anchor_viewport);
	
		EZ_control_SetVisible((ez_control_t *)scrollpane->h_scrollbar, true);
		EZ_control_SetPosition((ez_control_t *)scrollpane->h_scrollbar, rh_size_h, -rh_size_v);
		EZ_control_SetAnchor((ez_control_t *)scrollpane->h_scrollbar, (anchor_left | anchor_bottom | anchor_right));
		EZ_scrollbar_SetTargetIsParent(scrollpane->h_scrollbar, false);
		EZ_scrollbar_SetIsVertical(scrollpane->h_scrollbar, false);
		EZ_control_AddChild(scrollpane_ctrl, (ez_control_t *)scrollpane->h_scrollbar);
	}
	EZ_scrollpane_ResizeScrollbars(scrollpane);
}
//
// Scrollpane - Destroys the scrollpane.
//
int EZ_scrollpane_Destroy(ez_control_t *self, qbool destroy_children)
{
	ez_scrollpane_t *scrollpane = (ez_scrollpane_t *)self;
	CONTROL_EVENT_HANDLER_CALL(NULL, self, ez_control_t, OnDestroy, destroy_children);
	EZ_eventhandler_Remove(scrollpane->event_handlers.OnScrollbarThicknessChanged, NULL, true);
	EZ_eventhandler_Remove(scrollpane->event_handlers.OnTargetChanged, NULL, true);
	EZ_control_Destroy(self, destroy_children);
	return 0;
}
//
// Scrollpane - Always show the vertical scrollbar.
//
void EZ_scrollpane_SetAlwaysShowVerticalScrollbar(ez_scrollpane_t *scrollpane, qbool always_show)
{
	SET_FLAG(scrollpane->ext_flags, always_v_scrollbar, always_show);
}
//
// Scrollpane - Always show the horizontal scrollbar.
//
void EZ_scrollpane_SetAlwaysShowHorizontalScrollbar(ez_scrollpane_t *scrollpane, qbool always_show)
{
	SET_FLAG(scrollpane->ext_flags, always_h_scrollbar, always_show);
}
//
// Scrollpane - Set the target control of the scrollpane (the one to be scrolled).
//
void EZ_scrollpane_SetTarget(ez_scrollpane_t *scrollpane, ez_control_t *target)
{
	scrollpane->prev_target = scrollpane->target;
	scrollpane->target = target;
	CONTROL_RAISE_EVENT(NULL, scrollpane, ez_scrollpane_t, OnTargetChanged, NULL);
}
//
// Scrollpane - Set the thickness of the scrollbar controls.
//
void EZ_scrollpane_SetScrollbarThickness(ez_scrollpane_t *scrollpane, int scrollbar_thickness)
{
	scrollpane->scrollbar_thickness = scrollbar_thickness;
	CONTROL_RAISE_EVENT(NULL, scrollpane, ez_scrollpane_t, OnScrollbarThicknessChanged, NULL);
}
//
// Scrollpane - OnResize event.
//
int EZ_scrollpane_OnResize(ez_control_t *self, void *ext_event_info)
{
	ez_scrollpane_t *scrollpane = (ez_scrollpane_t *)self;
	EZ_control_OnResize(self, NULL);
	// Make sure we don't listen to resizing events of the target that we raised
	// ourselves, it causes an infinite loop.
	if (!(scrollpane->int_flags & resizing_target))
	{
		// Resize the scrollbars and target based on the state of the target.
		EZ_scrollpane_ResizeScrollbars(scrollpane);
	}
	
	CONTROL_EVENT_HANDLER_CALL(NULL, self, ez_control_t, OnResize, NULL);
	return 0;
}
//
// Scrollpane - The target control changed.
//
int EZ_scrollpane_OnTargetChanged(ez_control_t *self, void *ext_event_info)
{
	ez_scrollpane_t *scrollpane = (ez_scrollpane_t *)self;
	// Clean up the old target.
	if (scrollpane->prev_target)
	{
		EZ_eventhandler_Remove(scrollpane->prev_target->event_handlers.OnVirtualResize, EZ_scrollpane_OnTargetVirtualResize, false);
		EZ_control_RemoveChild(self, scrollpane->target);
		EZ_control_SetMovesParent(scrollpane->prev_target, false);
	}
	// Set the new target for the scrollbars so they know what to scroll.
	EZ_scrollbar_SetTarget(scrollpane->v_scrollbar, scrollpane->target);
	EZ_scrollbar_SetTarget(scrollpane->h_scrollbar, scrollpane->target);
	if (scrollpane->target)
	{
		// Subscribe to the targets resize and scroll events.
		EZ_control_AddOnVirtualResize(scrollpane->target, EZ_scrollpane_OnTargetVirtualResize, scrollpane);
		EZ_control_AddChild(self, scrollpane->target);
		EZ_control_SetScrollable(scrollpane->target, true);
		EZ_control_SetMovable(scrollpane->target, true);
		EZ_control_SetResizeableBoth(scrollpane->target, true);
		EZ_control_SetResizeable(scrollpane->target, true);
		// Reposition the target inside the scrollpane.
		EZ_control_SetPosition(scrollpane->target, 0, 0);
		EZ_control_SetSize(scrollpane->target, (self->width - scrollpane->scrollbar_thickness), (self->height - scrollpane->scrollbar_thickness));
		EZ_control_SetAnchor(scrollpane->target, (anchor_left | anchor_right | anchor_top | anchor_bottom));
		// Make sure the target is drawn infront of the scrollpane.
		EZ_control_SetDrawOrder(scrollpane->target, ((ez_control_t *)scrollpane)->draw_order + 1, true);
		// When moving the target move the scrollpane with it
		// and don't allow moving the target inside of the scrollpane.
		EZ_control_SetMovesParent(scrollpane->target, true);
		// Resize the scrollbars / target to fit properly.
		EZ_scrollpane_DetermineScrollbarVisibility(scrollpane);
		EZ_scrollpane_ResizeScrollbars(scrollpane);
		EZ_scrollpane_AdjustTargetSize(scrollpane);
		CONTROL_RAISE_EVENT(NULL, (ez_control_t *)scrollpane->target, ez_control_t, OnResize, NULL);
		CONTROL_RAISE_EVENT(NULL, (ez_control_t *)scrollpane->target, ez_control_t, OnMove, NULL);
	}
	CONTROL_EVENT_HANDLER_CALL(NULL, scrollpane, ez_scrollpane_t, OnTargetChanged, NULL);
	return 0;
}
//
// Scrollpane - The scrollbar thickness changed.
//
int EZ_scrollpane_OnScrollbarThicknessChanged(ez_control_t *self, void *ext_event_info)
{
	ez_scrollpane_t *scrollpane = (ez_scrollpane_t *)self;
	// Resize the scrollbars and target with the new scrollbar thickness.
	EZ_scrollpane_ResizeScrollbars(scrollpane);
	CONTROL_EVENT_HANDLER_CALL(NULL, scrollpane, ez_scrollpane_t, OnScrollbarThicknessChanged, NULL);
	return 0;
}
 
     |