1. -- 
  2. -- Copyright (c) 2012 Kevin Wellwood 
  3. -- All rights reserved. 
  4. -- 
  5. -- This source code is distributed under the Modified BSD License. For terms and 
  6. -- conditions, see license.txt. 
  7. -- 
  8.  
  9. with Allegro.Bitmaps;                   use Allegro.Bitmaps; 
  10. with Allegro.Color;                     use Allegro.Color; 
  11. with Fonts;                             use Fonts; 
  12. with Objects;                           use Objects; 
  13. with Tiles;                             use Tiles; 
  14.  
  15. private with Ada.Containers.Indefinite_Ordered_Maps; 
  16.  
  17. package Themes is 
  18.  
  19.     -- enumeration for text/icon alignment 
  20.     type Align_Type is ( Align_Left, Align_Center, Align_Right ); 
  21.  
  22.     -- enumeration of border styles 
  23.     type Border_Type is ( Border_None, Border_Raised, Border_Sunk, Border_Line ); 
  24.  
  25.     -- enumeration of the different purposes a widget has for user-defined 
  26.     -- colors. not all widgets use colors for every purpose. 
  27.     type Color_Purpose is ( Background, 
  28.                             Foreground, 
  29.                             Selected, 
  30.                             Selected_Text, 
  31.                             Text ); 
  32.  
  33.     -- an array of colors, one for each potential purpose in a widget 
  34.     type Colors_Array is array (Color_Purpose) of Allegro_Color; 
  35.  
  36.     DEFAULT_FONT_NAME : constant String := "standard";    -- default gui font 
  37.     DEFAULT_FONT_SIZE : constant Positive := 12;          -- default font size 
  38.  
  39.     ---------------------------------------------------------------------------- 
  40.  
  41.     -- A Theme is an abstract class used by widgets to provide the GUI's look 
  42.     -- and feel. The Theme is a singleton object. 
  43.     type Theme is abstract new Limited_Object with private; 
  44.     type A_Theme is access all Theme'Class; 
  45.  
  46.     -- Returns the width in pixels of borders drawn by the Theme. 
  47.     function Border_Width( this : access Theme ) return Natural; 
  48.  
  49.     -- Returns the Theme's default color for a 'purpose'. 
  50.     function Color( this    : access Theme; 
  51.                     purpose : Color_Purpose ) return Allegro_Color; 
  52.  
  53.     -- Draws a rectangular border from x1, y1 to x2, y2 using 'border' as the 
  54.     -- border type and 'color' as the border's dominant color. 
  55.     procedure Draw_Border( this   : access Theme; 
  56.                            border : Border_Type; 
  57.                            x1, y1, 
  58.                            x2, y2 : Integer; 
  59.                            color  : Allegro_Color ); 
  60.  
  61.     -- Draws a filled rectangle of 'color' (unless 'color' is transparent) from 
  62.     -- x1, y1 to x2, y2 and a border of type 'border' with 'color' as the 
  63.     -- border's dominant color. 
  64.     procedure Draw_Box( this   : access Theme; 
  65.                         x1, y1, 
  66.                         x2, y2 : Integer; 
  67.                         color  : Allegro_Color; 
  68.                         border : Border_Type := Border_None ); 
  69.  
  70.     -- Draws a label composed of the given icon, some horizontal spacing, and 
  71.     -- the given text. If the icon or the text is ommitted, the spacing that 
  72.     -- would separate the two will also be ommitted. The 'align' argument 
  73.     -- determines the alignment for the icon and text as a group within the 
  74.     -- width of the area given for the label by x1 and x2. The label will be 
  75.     -- centered vertically between y1 and y2. 
  76.     procedure Draw_Label( this     : access Theme; 
  77.                           x1, y1, 
  78.                           x2, y2   : Integer; 
  79.                           icon     : A_Allegro_Bitmap; 
  80.                           text     : String; 
  81.                           fontName : String; 
  82.                           fontSize : Positive; 
  83.                           color    : Allegro_Color; 
  84.                           align    : Align_Type ); 
  85.     pragma Precondition( fontName'Length > 0 ); 
  86.  
  87.     -- Synchronously retrieves a bitmap by tile id. 
  88.     function Get_Bitmap( this : access Theme; 
  89.                          id   : Natural ) return A_Allegro_Bitmap is abstract; 
  90.  
  91.     -- Returns a reference to a previously loaded font. If the font by this name 
  92.     -- and in this size has not been loaded previously, it will be reloaded in 
  93.     -- the new size 'size'. If the font 'name' is unknown or it can't be 
  94.     -- reloaded in the requested size, an exception will be raised. 
  95.     function Get_Font( this : not null access Theme'Class; 
  96.                        name : String; 
  97.                        size : Positive ) return A_Font; 
  98.     pragma Precondition( name'Length > 0 ); 
  99.  
  100.     -- Returns the id number of a tile referenced by name in the theme's tile 
  101.     -- library. 
  102.     function Get_ID( this : access Theme; name : String ) return Natural is abstract; 
  103.  
  104.     -- Returns the height in pixels of any text drawn in the given font. Zero is 
  105.     -- returned if the font hasn't been loaded. 
  106.     function Get_Text_Height( this     : not null access Theme'Class; 
  107.                               fontName : String; 
  108.                               fontSize : Positive ) return Natural; 
  109.     pragma Precondition( fontName'Length > 0 ); 
  110.  
  111.     -- Returns the width in pixels of 'text' in the given font. Zero is returned 
  112.     -- if the font hasn't been loaded. 
  113.     function Get_Text_Width( this     : not null access Theme'Class; 
  114.                              text     : String; 
  115.                              fontName : String; 
  116.                              fontSize : Positive ) return Natural; 
  117.     pragma Precondition( fontName'Length > 0 ); 
  118.  
  119.     -- Synchronously retrieves a tile from the theme's tile library, or null if 
  120.     -- the tile does not exist. 
  121.     function Get_Tile( this : access Theme; id : Natural ) return A_Tile is abstract; 
  122.  
  123.     -- Returns the minimum height in pixels required to draw a label with the 
  124.     -- given icon and text. If Draw_Label is overridden, this function should 
  125.     -- also be appropriately overridden to return the correct height. 
  126.     function Label_Height( this     : access Theme; 
  127.                            icon     : A_Allegro_Bitmap; 
  128.                            text     : String; 
  129.                            fontName : String; 
  130.                            fontSize : Positive ) return Natural; 
  131.     pragma Precondition( fontName'Length > 0 ); 
  132.  
  133.     -- Returns the minimum width in pixels required to draw a label with the 
  134.     -- given icon and text. If Draw_Label is overridden, this function should 
  135.     -- also be appropriately overridden to return the correct width. 
  136.     function Label_Width( this     : access Theme; 
  137.                           icon     : A_Allegro_Bitmap; 
  138.                           text     : String; 
  139.                           fontName : String; 
  140.                           fontSize : Positive ) return Natural; 
  141.     pragma Precondition( fontName'Length > 0 ); 
  142.  
  143.     -- Registers a font file with a font name. The font will be loaded first at 
  144.     -- size DEFAULT_FONT_SIZE. If the file at 'path' can't be loaded, a 
  145.     -- Resource_Error exception will be raised. 
  146.     procedure Register_Font( this : not null access Theme'Class; 
  147.                              path : String; 
  148.                              name : String ); 
  149.  
  150.     ---------------------------------------------------------------------------- 
  151.  
  152.     -- Creates the global theme instance. Raises an exception on error. 
  153.     procedure Create_Theme; 
  154.  
  155.     -- Deletes the global theme instance. 
  156.     procedure Delete_Theme; 
  157.  
  158.     -- Returns a reference to the global theme or null, if it has not been 
  159.     -- created. 
  160.     function Get return A_Theme; 
  161.  
  162. private 
  163.  
  164.     package Font_Maps is new Ada.Containers.Indefinite_Ordered_Maps( String, 
  165.                                                                      A_Font, 
  166.                                                                      "<", 
  167.                                                                      "=" ); 
  168.  
  169.     package Font_Paths is new Ada.Containers.Indefinite_Ordered_Maps( String, 
  170.                                                                       String, 
  171.                                                                       "<", 
  172.                                                                       "=" ); 
  173.  
  174.     type Theme is abstract new Limited_Object with 
  175.         record 
  176.             colors    : Colors_Array; 
  177.             fonts     : Font_Maps.Map; 
  178.             fontPaths : Font_Paths.Map; 
  179.         end record; 
  180.  
  181.     procedure Construct( this : access Theme ); 
  182.  
  183.     procedure Delete( this : in out Theme ); 
  184.  
  185.     -- Deletes the Theme. 
  186.     procedure Delete( this : in out A_Theme ); 
  187.     pragma Postcondition( this = null ); 
  188.  
  189.     ---------------------------------------------------------------------------- 
  190.  
  191.     -- An allocator function for creating Theme instances. 
  192.     type Allocator is access function return A_Theme; 
  193.  
  194.     -- Registers the allocator that will be used to create the global Theme 
  195.     -- instance. This should be called at elaboration time. 
  196.     procedure Register_Allocator( allocate : not null Allocator ); 
  197.  
  198. end Themes;