GLICT/scrollbar.cpp

00001 /*
00002         GLICT - Graphics Library Interface Creation Toolkit
00003         Copyright (C) 2006-2007 OBJECT Networks
00004 
00005         This library is free software; you can redistribute it and/or
00006         modify it under the terms of the GNU Library General Public
00007         License as published by the Free Software Foundation; either
00008         version 2 of the License, or (at your option) any later version.
00009 
00010         This library is distributed in the hope that it will be useful,
00011         but WITHOUT ANY WARRANTY; without even the implied warranty of
00012         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013         Library General Public License for more details.
00014 
00015         You should have received a copy of the GNU Library General Public
00016         License along with this library; if not, write to the Free
00017         Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 #include <stdlib.h>
00021 #include <stdio.h>
00022 #include "scrollbar.h"
00023 #include "globals.h"
00024 #include "fonts.h"
00025 glictScrollbar::glictScrollbar() {
00026 
00027         this->containeroffsetx = 0;
00028         this->containeroffsety = 0;
00029 
00030 
00031         this->bgcolor.r = 0.75;
00032         this->bgcolor.g = 0.75;
00033         this->bgcolor.b = 0.75;
00034         this->bgcolor.a = 1.0;
00035         strcpy(this->objtype, "Scrollbar");
00036 
00037         this->parent = NULL;
00038 
00039         this->focusable = true;
00040 
00041         this->SetHeight(100);
00042         this->SetWidth(10);
00043 
00044         this->SetPos(0,0);
00045 
00046         this->highlightdn = false;
00047         this->highlightup = false;
00048 
00049         this->min = 0;
00050         this->max = 100;
00051         this->value = 0;
00052         this->step = 1;
00053 
00054 
00055 }
00056 glictScrollbar::~glictScrollbar() {
00057         //this->glictWindow::RemoveObject(&panel);
00058 }
00059 void glictScrollbar::Paint() {
00060         if (!GetVisible()) return;
00061 
00062         // upper "button" //
00063         glictColor col;
00064         if (!highlightup) {
00065                 col = bgcolor;
00066         } else {
00067                 col.r = this->bgcolor.r < .5 ? (float)this->bgcolor.r * 1.5 : (float)this->bgcolor.r / 1.5;
00068                 col.g = this->bgcolor.g < .5 ? (float)this->bgcolor.g * 1.5 : (float)this->bgcolor.g / 1.5;
00069                 col.b = this->bgcolor.b < .5 ? (float)this->bgcolor.b * 1.5 : (float)this->bgcolor.b / 1.5;
00070                 col.a = this->bgcolor.a < .5 ? (float)this->bgcolor.a * 1.5 : (float)this->bgcolor.a / 1.5;
00071         }
00072         glictGlobals.PaintRect(this->x+glictGlobals.translation.x,this->x+this->width+glictGlobals.translation.x,
00073                                                    this->y+glictGlobals.translation.y, this->y+this->width+glictGlobals.translation.y,
00074                                                    col);
00075 
00076         // lower "button" //
00077         if (!highlightdn) {
00078                 col = bgcolor;
00079         } else {
00080                 col.r = this->bgcolor.r < .5 ? (float)this->bgcolor.r * 1.5 : (float)this->bgcolor.r / 1.5;
00081                 col.g = this->bgcolor.g < .5 ? (float)this->bgcolor.g * 1.5 : (float)this->bgcolor.g / 1.5;
00082                 col.b = this->bgcolor.b < .5 ? (float)this->bgcolor.b * 1.5 : (float)this->bgcolor.b / 1.5;
00083                 col.a = this->bgcolor.a < .5 ? (float)this->bgcolor.a * 1.5 : (float)this->bgcolor.a / 1.5;
00084         }
00085         glictGlobals.PaintRect(this->x+glictGlobals.translation.x,this->x+this->width+glictGlobals.translation.x,
00086                                                         this->y+this->height-this->width+glictGlobals.translation.y,this->y+this->height+glictGlobals.translation.y,
00087                                                         col);
00088 
00089         // back panel
00090         col.r = bgcolor.r * 0.7;
00091         col.g = bgcolor.g * 0.7;
00092         col.b = bgcolor.b * 0.7;
00093         col.a = bgcolor.a;
00094         glictGlobals.PaintRect(this->x+glictGlobals.translation.x, this->x+this->width+glictGlobals.translation.x,
00095                                                         this->y+this->width+glictGlobals.translation.y,this->y+this->height-this->width+glictGlobals.translation.y, col);
00096 
00097 
00098         // scroller chip
00099         col.r = bgcolor.r * 0.8;
00100         col.g = bgcolor.g * 0.8;
00101         col.b = bgcolor.b * 0.8;
00102         col.a = bgcolor.a;
00103 
00104 
00105 /*
00106 
00107         glVertex2f(
00108                 this->x,
00109                 this->y + // normal beginning coord of the object
00110                 this->width + // increased by height of top button
00111                 ((float)(this->value-this->min) / (float)(this->max - this->min)) // at this percent
00112                 * (float)(this->height - this->width*2 - this->width) // which should be a height, reduced by top and bottom button's height, but also by scroller's height
00113         );
00114         glVertex2f(this->x + this->width, this->y + this->width + ((float)(this->value-this->min) / (float)(this->max - this->min)) * (float)(this->height - this->width*2 - this->width));
00115         glVertex2f(this->x + this->width, this->y + this->width + ((float)(this->value-this->min) / (float)(this->max - this->min)) * (float)(this->height - this->width*2 - this->width) + this->width);
00116         glVertex2f(this->x, this->y + this->width + ((float)(this->value-this->min) / (float)(this->max - this->min)) * (float)(this->height - this->width*2 - this->width) + this->width);
00117 
00118 */
00119 
00120 
00121         glictGlobals.PaintRect(
00122                 this->x+glictGlobals.translation.x,
00123                 this->x + this->width +glictGlobals.translation.x,
00124 
00125                 this->y +glictGlobals.translation.y + // normal beginning coord of the object
00126                 this->width + // increased by height of top button
00127                 ((float)(this->value-this->min) / (float)(this->max - this->min)) // at this percent
00128                 * (float)(this->height - this->width*2 - this->width), // which should be a height, reduced by top and bottom button's height, but also by scroller's height
00129 
00130                 this->y +glictGlobals.translation.y + // normal beginning coord of the object
00131                 this->width + // increased by height of top button
00132                 ((float)(this->value-this->min) / (float)(this->max - this->min)) // at this percent
00133                 * (float)(this->height - this->width*2 - this->width) // which should be a height, reduced by top and bottom button's height, but also by scroller's height
00134                 + this->width // this is bottom, add some more
00135 
00136                 ,col
00137         );
00138         this->CPaint();
00139 
00140         // this is here so that scissoring resumes properly
00141         this->SetScissor();
00142 
00143 
00144         glictGlobals.SetColor(1., 1., 1., 1.);
00145 
00146 
00147 
00148         glictFontRender("^","system",
00149                 this->x + (this->width / 2 - glictFontSize("^", "system") / 2 +glictGlobals.translation.x) ,
00150                 this->y - 9 + width / 2 + 9/2 +glictGlobals.translation.y);
00151 
00152         glictFontRender("V","system",
00153                 this->x + (this->width / 2 - glictFontSize("V", "system") / 2 +glictGlobals.translation.x) ,
00154                 this->y + this->height - width / 2 - 9 / 2 +glictGlobals.translation.y);
00155 
00156 
00157 }
00158 
00159 void glictScrollbar::SetBGColor(float r, float g, float b, float a) {
00160         this->bgcolor.r = r;
00161         this->bgcolor.g = g;
00162         this->bgcolor.b = b;
00163         this->bgcolor.a = a;
00164         //this->panel.SetBGColor(r,g,b,a);
00165 }
00166 
00167 bool glictScrollbar::CastEvent(glictEvents evt, void* wparam, long lparam, void* returnvalue) {
00168         //printf("Event of type %s passing through %s (%s)\n", EvtTypeDescriptor(evt), objtype, parent ? parent->objtype : "NULL");
00169         if (!GetVisible()) return false;
00170         int oldx = this->x, oldy = this->y;
00171         if (evt == GLICT_MOUSECLICK || evt == GLICT_MOUSEDOWN || evt == GLICT_MOUSEUP) {
00172                 if (((glictPos*)wparam)->x > this->clipleft &&
00173                         ((glictPos*)wparam)->x < this->clipright &&
00174                         ((glictPos*)wparam)->y > this->cliptop &&
00175                         ((glictPos*)wparam)->y < this->clipbottom) {
00176 
00177 
00178 
00179 
00180                         if (evt == GLICT_MOUSECLICK) {
00181                                 this->Focus(this);
00182                                 if (((glictPos*)wparam)->y - this->top < this->width) { // mousedown within upper button?
00183                                         if (this->value > this->min) this->value -= this->step;
00184                                         if (this->value < this->min) this->value = this->min;
00185                                 }
00186                                 if (((glictPos*)wparam)->y - this->top > this->height - this->width) { // mousedown within upper button?
00187                                         if (this->value < this->max) this->value += this->step;
00188                                         if (this->value > this->max) this->value = this->max;
00189                                 }
00190                         }
00191                         if (evt == GLICT_MOUSEDOWN) {
00192                                 if (((glictPos*)wparam)->y - this->top < this->width) { // mousedown within upper button?
00193                                         this->highlightup = true;
00194                                 }
00195                                 if (((glictPos*)wparam)->y - this->top > this->height - this->width) { // mousedown within upper button?
00196                                         this->highlightdn = true;
00197                                 }
00198                         }
00199                         if (evt == GLICT_MOUSEUP) {
00200                                 this->highlightdn = false;
00201                                 this->highlightup = false;
00202                         }
00203 
00204                 }
00205                 return DefaultCastEvent(evt, wparam, lparam, returnvalue);
00206         }
00207 
00208         return false;
00209 }
00210 
00211 void glictScrollbar::SetValue(int val) {
00212         this->value = val;
00213 }
00214 int glictScrollbar::GetValue() {
00215         return this->value;
00216 }
00217 void glictScrollbar::SetStep (unsigned int newstep) {
00218         this->step = newstep;
00219 }
00220 unsigned int glictScrollbar::GetStep() {
00221         return this->step;
00222 }
00223 void glictScrollbar::SetMin (int newmin) {
00224         this->min = newmin;
00225 }
00226 int glictScrollbar::GetMin() {
00227         return min;
00228 }
00229 void glictScrollbar::SetMax (int newmax) {
00230         this->max = newmax;
00231 }
00232 int glictScrollbar::GetMax() {
00233         return this->max;
00234 }
00235 
00236 
00237 

SourceForge.net Logo
generated with doxygen 1.5.3 on Mon Oct 29 18:09:26 2007