Ocean Empire GUI user code and third party controls. All code posted here is in the public domain.

Controls

Multiline Label

A Label class, that can do automatic word wrapping, made by Brian Bull.

   1 class WrappedLabel (Label):
   2     def __init__ (self, text, width):
   3         self.max_width = width
   4         Label.__init__ (self,"")
   5         self.set_text (text)
   6 
   7     def set_text (self, text):
   8         self.wrapped_text = ""
   9         current_line = ""
  10         for word in text.split (" "):
  11             if word.find ("\n")>0:
  12                 word_before_return = word[:word.find ("\n")]
  13                 word = word[word.find ("\n")+1:]
  14                 temp_label = Label (current_line + word_before_return + " ")
  15                 temp_label.update ()
  16                 if temp_label.width > self.max_width:
  17                     self.wrapped_text = self.wrapped_text + current_line + "\n" + word_before_return + "\n"
  18                     current_line = ""
  19                 else:
  20                     self.wrapped_text = self.wrapped_text + current_line + word_before_return + "\n"
  21                     current_line = ""
  22             temp_label = Label (current_line + word + " ")
  23             temp_label.update ()
  24             if temp_label.width > self.max_width:
  25                 self.wrapped_text = self.wrapped_text + current_line + "\n"
  26                 current_line = word + " "
  27             else:
  28                 current_line = current_line + word + " "
  29         if current_line <> "":
  30             self.wrapped_text = self.wrapped_text + current_line
  31         self.set_multiline (True)
  32         Label.set_text (self, self.wrapped_text)

A tooltip that follows mouse when over a button

Code from http://ocemp.sourceforge.net/manual/misc_widgets.html#widgets_tooltipwindow

   1 # TooltipWindow examples
   2 import pygame.mouse
   3 from ocempgui.widgets import *
   4 from ocempgui.widgets.Constants import *
   5 
   6 __tooltip = None
   7 
   8 def _make_tooltip (tip, renderer):
   9     global __tooltip
  10 
  11     # Destroy the tooltip, if it exists.
  12     if __tooltip:
  13         __tooltip.destroy ()
  14 
  15     # Create a new tooltip.
  16     __tooltip = TooltipWindow (tip)
  17     _update_tooltip(None, tip, renderer)
  18     __tooltip.depth = 99 # Make it the topmost widget.
  19 
  20     renderer.add_widget (__tooltip)
  21 
  22 def _update_tooltip (val, tip, renderer):
  23     global __tooltip
  24     if __tooltip:
  25         x, y = pygame.mouse.get_pos ()
  26         __tooltip.topleft = x + 8, y - 5
  27 
  28 
  29 def _destroy_tooltip ():
  30     # Destroy the tooltip, if it exists.
  31     global __tooltip
  32     if __tooltip:
  33         __tooltip.destroy ()
  34         __tooltip = None
  35 
  36 def create_tooltip_view (renderer):
  37 
  38     frame = VFrame ()
  39     frame.border = BORDER_NONE
  40     frame.spacing = 20
  41 
  42     button1 = Button ("Move the mouse over me")
  43     button1.tooltip = "An enhanced description"
  44     button1.connect_signal (SIG_ENTER, _make_tooltip, button1.tooltip, renderer)
  45     button1.connect_signal (SIG_MOUSEMOVE, _update_tooltip, button1.tooltip, renderer)
  46     button1.connect_signal (SIG_LEAVE, _destroy_tooltip)
  47 
  48     button2 = Button ("And over me, too")
  49     button2.tooltip = "Another description"
  50     button2.connect_signal (SIG_ENTER, _make_tooltip, button2.tooltip, renderer)
  51     button2.connect_signal (SIG_MOUSEMOVE, _update_tooltip, button2.tooltip, renderer)
  52     button2.connect_signal (SIG_LEAVE, _destroy_tooltip)
  53 
  54     frame.children = button1, button2
  55 
  56     return frame
  57 
  58 if __name__ == "__main__":
  59     # Initialize the drawing window.
  60     re = Renderer ()
  61     re.create_screen (200, 150)
  62     re.title = "TooltipWindow examples"
  63     re.color = (234, 228, 223)
  64     re.add_widget (create_tooltip_view (re))
  65     # Start the main rendering loop.
  66     re.start ()

UserCode (last edited 2007-12-02 09:29:50 by Marcusva)

SourceForge.net Logo