Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

button_default.cpp

Go to the documentation of this file.
00001 /*
00002         ClanGUI, copyrights by various people. Have a look in the CREDITS file.
00003         
00004         This sourcecode is distributed using the Library GNU Public Licence,
00005         version 2 or (at your option) any later version. Please read LICENSE
00006         for details.
00007 */
00008 
00009 #include "precomp.h"
00010 #include <stdio.h>
00011 #include "API/Display/Display/surface.h"
00012 #include "API/Display/Font/font.h"
00013 #include "API/Core/Resources/resource_manager.h"
00014 #include "button_default.h"
00015 
00016 CL_Button_Default::CL_Button_Default(
00017         CL_Button *_button,
00018         const CL_ComponentOptions &options,
00019         CL_StyleManager_Default *_style)
00020 :
00021         CL_ComponentStyle(_button),
00022         button(_button),
00023         style(_style)
00024 {
00025         resources = style->get_resources();
00026 
00027         font = CL_Font::load("Button/font", resources);
00028         font_disabled = CL_Font::load("Button/font_disabled", resources);
00029 
00030         if(options.exists("surface_up"))
00031                 button->set_surface_up(CL_Surface::load(options.get_value("surface_up").c_str(), resources));
00032 
00033         if(options.exists("surface_down"))
00034                 button->set_surface_down(CL_Surface::load(options.get_value("surface_down").c_str(), resources));
00035 
00036         if(options.exists("surface_disabled"))
00037                 button->set_surface_disabled(CL_Surface::load(options.get_value("surface_disabled").c_str(), resources));
00038 
00039         if(options.exists("surface_highlighted"))
00040                 button->set_surface_highlighted(CL_Surface::load(options.get_value("surface_highlighted").c_str(), resources));
00041 
00042         CL_Rect position = button->get_position();
00043         if(position.x2 == 0 || position.y2 == 0)
00044         {
00045                 if(position.x2 == 0)
00046                 {
00047                         if(button->get_surface_up())
00048                                 position.x2 = position.x1 + button->get_surface_up()->get_width();
00049                         else
00050                                 position.x2 = position.x1 + (4 + font->get_text_width(button->get_text().c_str()));
00051                 }
00052                 if(position.y2 == 0)
00053                 {
00054                         if(button->get_surface_up())
00055                                 position.y2 = position.y1 + button->get_surface_up()->get_height();
00056                         else
00057                                 position.y2 = position.y1 + (4 + font->get_height());
00058                 }
00059                 button->set_position(position);
00060         }
00061 
00062         slot_paint = button->sig_paint().connect(CL_CreateSlot(this, &CL_Button_Default::on_paint));
00063 }
00064 
00065 CL_Button_Default::~CL_Button_Default()
00066 {
00067 //      delete font;
00068 }
00069 
00070 void CL_Button_Default::on_paint()
00071 {
00072         int text_width = font->get_text_width(button->get_text().c_str());
00073         int button_width = button->get_width();
00074         int text_height = font->get_height();
00075         int button_height = button->get_height();
00076 
00077         int x_pos = (button_width - text_width) / 2;
00078         int y_pos = (button_height - text_height) / 2;
00079 
00080         int width = button->get_width();
00081         int height = button->get_height();
00082 
00083         if(button->is_enabled() == false)
00084         {
00085                 CL_Surface *surface = button->get_surface_disabled();
00086                 if(surface)
00087                         surface->put_screen(0, 0);
00088                 else
00089                 {
00090                         // Fill
00091                         style->fill_rect(2, 2, width - 2, height - 2, GUICOLOR_BUTTON_DISABLED);
00092 
00093                         // Outline
00094                         style->draw_rect(0, 0, width, height, GUICOLOR_DARK_OUTLINE_DISABLED);
00095 
00096                         // Shade box
00097                         style->draw_box(1, 1, width - 1 , height - 1, GUICOLOR_BRIGHT_SHADE_DISABLED, GUICOLOR_DARK_SHADE_DISABLED);
00098                 }
00099 
00100                 font_disabled->print_left(x_pos, y_pos, button->get_text().c_str());
00101         }
00102         else 
00103         {
00104                 if(button->is_down())
00105                 {
00106                         CL_Surface *surface = button->get_surface_down();
00107                         if(surface)
00108                                 surface->put_screen(0, 0);
00109                         else
00110                         {
00111                                 // Fill
00112                                 style->fill_rect(2, 2, width - 2, height - 2, GUICOLOR_BUTTON_TOGGLED);
00113 
00114                                 // Outline
00115                                 style->draw_rect(0, 0, width, height, GUICOLOR_DARK_OUTLINE);
00116 
00117                                 // Shade box
00118                                 style->draw_box(1, 1, width - 1 , height - 1, GUICOLOR_DARK_SHADE, GUICOLOR_BRIGHT_SHADE);
00119                         }
00120 
00121                         font->print_left(x_pos + 1, y_pos + 1, button->get_text().c_str());
00122                 }
00123                 else
00124                 {
00125                         CL_Surface *surface = button->get_surface_up();
00126                         if(surface)
00127                                 surface->put_screen(0, 0);
00128                         else
00129                         {
00130                                 // Fill
00131                                 style->fill_rect(2, 2, width - 2, height - 2, GUICOLOR_BUTTON);
00132 
00133                                 // Outline
00134                                 style->draw_rect(0, 0, width, height, GUICOLOR_DARK_OUTLINE);
00135 
00136                                 // Shade box
00137                                 style->draw_box(1, 1, width - 1 , height - 1, GUICOLOR_BRIGHT_SHADE, GUICOLOR_DARK_SHADE);
00138                         }
00139 
00140                         font->print_left(x_pos, y_pos, button->get_text().c_str());
00141                 }
00142         }
00143 }
00144 

Generated at Wed Apr 4 19:53:59 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001