<commit_before>"""

This module defines the following constants:

*Widget options*
  * BGUI_NONE = 0
  * BGUI_CENTERX = 1
  * BGUI_CENTERY = 2
  * BGUI_NORMALIZED = 4
  * BGUI_THEMED = 8
  * BGUI_NO_FOCUS = 16
  * BGUI_CACHE = 32

  * BGUI_DEFAULT = BGUI_NORMALIZED | BGUI_THEMED
  * BGUI_CENTERED = BGUI_CENTERX | BGUI_CENTERY

*Widget overflow*
  * BGUI_OVERFLOW_NONE = 0
  * BGUI_OVERFLOW_HIDDEN = 1
  * BGUI_OVERFLOW_REPLACE = 2
  * BGUI_OVERFLOW_CALLBACK = 3

*Mouse event states*
  * BGUI_MOUSE_NONE = 0
  * BGUI_MOUSE_CLICK = 1
  * BGUI_MOUSE_RELEASE = 2
  * BGUI_MOUSE_ACTIVE = 4
"""

from .key_defs import *
from collections import OrderedDict

# Widget options
BGUI_NONE = 0
BGUI_CENTERX = 1
BGUI_CENTERY = 2
BGUI_NORMALIZED = 4
BGUI_THEMED = 8
BGUI_NO_FOCUS = 16
BGUI_CACHE = 32

BGUI_DEFAULT = BGUI_NORMALIZED | BGUI_THEMED
BGUI_CENTERED = BGUI_CENTERX | BGUI_CENTERY

# Widget overflow
BGUI_OVERFLOW_NONE = 0
BGUI_OVERFLOW_HIDDEN = 1
BGUI_OVERFLOW_REPLACE = 2
BGUI_OVERFLOW_CALLBACK = 3

# Mouse event states
BGUI_MOUSE_NONE = 0
BGUI_MOUSE_CLICK = 1
BGUI_MOUSE_RELEASE = 2
BGUI_MOUSE_ACTIVE = 4

class Widget:
	"""The base widget class"""
	
	theme_section = 'Widget'
	theme_options = {}

	def __init__(self, parent, name, aspect=None, size=[0, 0], pos=[0, 0], sub_theme='',
			options=BGUI_DEFAULT):
		"""The Widget constructor

		Arguments:

		parent -- the widget's parent
		name -- the name of the widget
		aspect -- constrain the widget size to a specified aspect ratio
		size -- a tuple containing the wdith and height
		pos -- a tuple containing the x and y position
		options -- various other options

		"""
		
		self.name = name
		self.options = options
		
		# Store the system so children can access theming data
		self.system = parent.system
		
		if self.system.theme and options & BGUI_THEMED and self.theme_section != Widget.theme_section:
			if sub_theme:
				self.theme_section += ':'+sub_theme
		
			if self.system.theme.supports(self):
				self.theme = self.system.theme
			else:
				print("Theming is enabled, but the current theme does not support", self.theme_section)
				self.theme = None
		else:
			self.theme = None
		
		self._hover = False
		self.frozen = False
		
		# The widget is visible by default
		self.visible = True
		
		# Event callbacks
		self.on_click = None
		self.on_release = None
		self.on_hover = None
		self.on_active = None

		# Setup the parent
		parent._attach_widget(self)
		self.parent = parent

		# A dictionary to store children widgets
		self.children = OrderedDict()

		# Setup the widget's position
		self._position = [None]*4
		self._update_position(size, pos)	
		
		if aspect:
			size = [self.size[1]*aspect, self.size[1]]
			if self.options & BGUI_NORMALIZED:
				size = [size[0]/self.parent.size[0], size[1]/self.parent.size[1]]
			self._update_position(size, self._base_pos)
	
	def __del__(self):
		self._cleanup()
		
	def _cleanup(self):
		"""Override this if needed"""
		for child in self.children:
			self.children[child]._cleanup()

	def _update_position(self, size, pos):
		self._base_size = size[:]
		self._base_pos = pos[:]

		if self.options & BGUI_NORMALIZED:
			pos[0] *= self.parent.size[0]
			pos[1] *= self.parent.size[1]

			size[0] *= self.parent.size[0]
			size[1] *= self.parent.size[1]

		if self.options & BGUI_CENTERX:
			pos[0] = self.parent.size[0]/2 - size[0]/2

		if self.options & BGUI_CENTERY:
			pos[1] = self.parent.size[1]/2 - size[1]/2

		x = pos[0] + self.parent.position[0]
		y = pos[1] + self.parent.position[1]
		width = size[0]
		height = size[1]
		self._size = [width, height]
		# The "private" position returned by setter
		self._position = [x, y]
		
		# OpenGL starts at the bottom left and goes counter clockwise
		self.gl_position = [
					[x, y],
					[x+width, y],
					[x+width, y+height],
					[x, y+height]
				]
				
		# Update any children
		for widget in self.children.values():
			widget._update_position(widget._base_size, widget._base_pos)
				
	@property
	def position(self):
		"""The text to display"""
		return self._position
		
	@position.setter
	def position(self, value):
		self._update_position(self._base_size, value)
		
	@property
	def size(self):
		"""The text to display"""
		return self._size
		
	@size.setter
	def size(self, value):
		self._update_position(value, self._base_pos)

	def _handle_mouse(self, pos, event):<commit_msg>Fixing the doc strings for Widget.position and Widget.size. I'm also adding a note to not use Widget directly, but to subclass it instead.<commit_after>"""

This module defines the following constants:

*Widget options*
  * BGUI_NONE = 0
  * BGUI_CENTERX = 1
  * BGUI_CENTERY = 2
  * BGUI_NORMALIZED = 4
  * BGUI_THEMED = 8
  * BGUI_NO_FOCUS = 16
  * BGUI_CACHE = 32

  * BGUI_DEFAULT = BGUI_NORMALIZED | BGUI_THEMED
  * BGUI_CENTERED = BGUI_CENTERX | BGUI_CENTERY

*Widget overflow*
  * BGUI_OVERFLOW_NONE = 0
  * BGUI_OVERFLOW_HIDDEN = 1
  * BGUI_OVERFLOW_REPLACE = 2
  * BGUI_OVERFLOW_CALLBACK = 3

*Mouse event states*
  * BGUI_MOUSE_NONE = 0
  * BGUI_MOUSE_CLICK = 1
  * BGUI_MOUSE_RELEASE = 2
  * BGUI_MOUSE_ACTIVE = 4
  
.. note::

	The Widget class should not be used directly in a gui, but should instead
	be subclassed to create other widgets.

"""

from .key_defs import *
from collections import OrderedDict

# Widget options
BGUI_NONE = 0
BGUI_CENTERX = 1
BGUI_CENTERY = 2
BGUI_NORMALIZED = 4
BGUI_THEMED = 8
BGUI_NO_FOCUS = 16
BGUI_CACHE = 32

BGUI_DEFAULT = BGUI_NORMALIZED | BGUI_THEMED
BGUI_CENTERED = BGUI_CENTERX | BGUI_CENTERY

# Widget overflow
BGUI_OVERFLOW_NONE = 0
BGUI_OVERFLOW_HIDDEN = 1
BGUI_OVERFLOW_REPLACE = 2
BGUI_OVERFLOW_CALLBACK = 3

# Mouse event states
BGUI_MOUSE_NONE = 0
BGUI_MOUSE_CLICK = 1
BGUI_MOUSE_RELEASE = 2
BGUI_MOUSE_ACTIVE = 4

class Widget:
	"""The base widget class"""
	
	theme_section = 'Widget'
	theme_options = {}

	def __init__(self, parent, name, aspect=None, size=[0, 0], pos=[0, 0], sub_theme='',
			options=BGUI_DEFAULT):
		"""The Widget constructor

		Arguments:

		parent -- the widget's parent
		name -- the name of the widget
		aspect -- constrain the widget size to a specified aspect ratio
		size -- a tuple containing the wdith and height
		pos -- a tuple containing the x and y position
		options -- various other options

		"""
		
		self.name = name
		self.options = options
		
		# Store the system so children can access theming data
		self.system = parent.system
		
		if self.system.theme and options & BGUI_THEMED and self.theme_section != Widget.theme_section:
			if sub_theme:
				self.theme_section += ':'+sub_theme
		
			if self.system.theme.supports(self):
				self.theme = self.system.theme
			else:
				print("Theming is enabled, but the current theme does not support", self.theme_section)
				self.theme = None
		else:
			self.theme = None
		
		self._hover = False
		self.frozen = False
		
		# The widget is visible by default
		self.visible = True
		
		# Event callbacks
		self.on_click = None
		self.on_release = None
		self.on_hover = None
		self.on_active = None

		# Setup the parent
		parent._attach_widget(self)
		self.parent = parent

		# A dictionary to store children widgets
		self.children = OrderedDict()

		# Setup the widget's position
		self._position = [None]*4
		self._update_position(size, pos)	
		
		if aspect:
			size = [self.size[1]*aspect, self.size[1]]
			if self.options & BGUI_NORMALIZED:
				size = [size[0]/self.parent.size[0], size[1]/self.parent.size[1]]
			self._update_position(size, self._base_pos)
	
	def __del__(self):
		self._cleanup()
		
	def _cleanup(self):
		"""Override this if needed"""
		for child in self.children:
			self.children[child]._cleanup()

	def _update_position(self, size, pos):
		self._base_size = size[:]
		self._base_pos = pos[:]

		if self.options & BGUI_NORMALIZED:
			pos[0] *= self.parent.size[0]
			pos[1] *= self.parent.size[1]

			size[0] *= self.parent.size[0]
			size[1] *= self.parent.size[1]

		if self.options & BGUI_CENTERX:
			pos[0] = self.parent.size[0]/2 - size[0]/2

		if self.options & BGUI_CENTERY:
			pos[1] = self.parent.size[1]/2 - size[1]/2

		x = pos[0] + self.parent.position[0]
		y = pos[1] + self.parent.position[1]
		width = size[0]
		height = size[1]
		self._size = [width, height]
		# The "private" position returned by setter
		self._position = [x, y]
		
		# OpenGL starts at the bottom left and goes counter clockwise
		self.gl_position = [
					[x, y],
					[x+width, y],
					[x+width, y+height],
					[x, y+height]
				]
				
		# Update any children
		for widget in self.children.values():
			widget._update_position(widget._base_size, widget._base_pos)
				
	@property
	def position(self):
		"""The widget's position"""
		return self._position
		
	@position.setter
	def position(self, value):
		self._update_position(self._base_size, value)
		
	@property
	def size(self):
		"""The widget's size"""
		return self._size
		
	@size.setter
	def size(self, value):
		self._update_position(value, self._base_pos)

	def _handle_mouse(self, pos, event):
{'fraction_non_alphanumeric': 0.10800579000111346, 'fraction_numerical': 0.015365772185725421, 'mean_word_length': 3.538655886811521, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2430725', 'n_tokens_mistral': 3687, 'n_tokens_neox': 3252, 'n_words': 838}