1 /********************************************************************************
2 *                                                                               *
3 *                         C o l o r   L i s t   W i d g e t                     *
4 *                                                                               *
5 *********************************************************************************
6 * Copyright (C) 2005,2006 by Jeroen van der Zijp.   All Rights Reserved.        *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or                 *
9 * modify it under the terms of the GNU Lesser General Public                    *
10 * License as published by the Free Software Foundation; either                  *
11 * version 2.1 of the License, or (at your option) any later version.            *
12 *                                                                               *
13 * This library is distributed in the hope that it will be useful,               *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             *
16 * Lesser General Public License for more details.                               *
17 *                                                                               *
18 * You should have received a copy of the GNU Lesser General Public              *
19 * License along with this library; if not, write to the Free Software           *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
21 *********************************************************************************
22 * $Id: FXColorList.cpp 4937 2019-03-10 19:59:30Z arthurcnorman $                      *
23 ********************************************************************************/
24 #include "xincs.h"
25 #include "fxver.h"
26 #include "fxdefs.h"
27 #include "fxkeys.h"
28 #include "FXHash.h"
29 #include "FXThread.h"
30 #include "FXStream.h"
31 #include "FXString.h"
32 #include "FXSize.h"
33 #include "FXPoint.h"
34 #include "FXObjectList.h"
35 #include "FXRectangle.h"
36 #include "FXRegistry.h"
37 #include "FXApp.h"
38 #include "FXDCWindow.h"
39 #include "FXFont.h"
40 #include "FXIcon.h"
41 #include "FXScrollBar.h"
42 #include "FXColorList.h"
43 
44 
45 
46 /*
47 
48   Notes:
49 */
50 
51 
52 #define ICON_SPACING             4    // Spacing between icon and label
53 #define SIDE_SPACING             6    // Left or right spacing between items
54 #define LINE_SPACING             4    // Line spacing between items
55 
56 #define SWATCH_WIDTH            24    // Swatch size
57 #define SWATCH_HEIGHT           12
58 
59 using namespace FX;
60 
61 
62 /*******************************************************************************/
63 
64 namespace FX {
65 
66 
67 // Object implementation
68 FXIMPLEMENT(FXColorItem,FXListItem,NULL,0)
69 
70 
71 // Draw item
draw(const FXList * list,FXDC & dc,FXint xx,FXint yy,FXint ww,FXint hh)72 void FXColorItem::draw(const FXList* list,FXDC& dc,FXint xx,FXint yy,FXint ww,FXint hh){
73   FXFont *font=list->getFont();
74   FXint th=0;
75   if(!label.empty()) th=font->getFontHeight();
76   if(isSelected())
77     dc.setForeground(list->getSelBackColor());
78   else
79     dc.setForeground(list->getBackColor());
80   dc.fillRectangle(xx,yy,ww,hh);
81   if(hasFocus()){
82     dc.drawFocusRectangle(xx+1,yy+1,ww-2,hh-2);
83     }
84   xx+=SIDE_SPACING/2;
85   dc.setForeground(color);
86   dc.fillRectangle(xx,yy+(hh-SWATCH_HEIGHT)/2,SWATCH_WIDTH,SWATCH_HEIGHT);
87   dc.setForeground(FXRGB(0,0,0));
88   dc.drawRectangle(xx,yy+(hh-SWATCH_HEIGHT)/2,SWATCH_WIDTH,SWATCH_HEIGHT);
89   xx+=ICON_SPACING+SWATCH_WIDTH;
90   if(!label.empty()){
91     dc.setFont(font);
92     if(!isEnabled())
93       dc.setForeground(makeShadowColor(list->getBackColor()));
94     else if(isSelected())
95       dc.setForeground(list->getSelTextColor());
96     else
97       dc.setForeground(list->getTextColor());
98     dc.drawText(xx,yy+(hh-th)/2+font->getFontAscent(),label);
99     }
100   }
101 
102 
103 // See if item got hit, and where: 0 is outside, 1 is icon, 2 is text
hitItem(const FXList * list,FXint xx,FXint yy) const104 FXint FXColorItem::hitItem(const FXList* list,FXint xx,FXint yy) const {
105   FXint tw=0,th=0,ix,iy,tx,ty,h;
106   FXFont *font=list->getFont();
107   if(!label.empty()){
108     tw=4+font->getTextWidth(label);
109     th=4+font->getFontHeight();
110     }
111   h=LINE_SPACING+FXMAX(th,SWATCH_HEIGHT);
112   ix=SIDE_SPACING/2;
113   tx=SIDE_SPACING/2+SWATCH_WIDTH+ICON_SPACING;
114   iy=(h-SWATCH_HEIGHT)/2;
115   ty=(h-th)/2;
116 
117   // In icon?
118   if(ix<=xx && iy<=yy && xx<ix+SWATCH_WIDTH && yy<iy+SWATCH_HEIGHT) return 1;
119 
120   // In text?
121   if(tx<=xx && ty<=yy && xx<tx+tw && yy<ty+th) return 2;
122 
123   // Outside
124   return 0;
125   }
126 
127 
128 // Get width of item
getWidth(const FXList * list) const129 FXint FXColorItem::getWidth(const FXList* list) const {
130   FXFont *font=list->getFont();
131   FXint w=SWATCH_WIDTH;
132   if(!label.empty()) w+=ICON_SPACING+font->getTextWidth(label);
133   return SIDE_SPACING+w;
134   }
135 
136 
137 // Get height of item
getHeight(const FXList * list) const138 FXint FXColorItem::getHeight(const FXList* list) const {
139   FXFont *font=list->getFont();
140   FXint h=0;
141   if(!label.empty()) h=font->getFontHeight();
142   return LINE_SPACING+FXMAX(h,SWATCH_HEIGHT);
143   }
144 
145 
146 /*******************************************************************************/
147 
148 
149 // Object implementation
150 FXIMPLEMENT(FXColorList,FXList,NULL,0)
151 
152 
153 // List
FXColorList(FXComposite * p,FXObject * tgt,FXSelector sel,FXuint opts,FXint x,FXint y,FXint w,FXint h)154 FXColorList::FXColorList(FXComposite *p,FXObject* tgt,FXSelector sel,FXuint opts,FXint x,FXint y,FXint w,FXint h):FXList(p,tgt,sel,opts,x,y,w,h){
155   }
156 
157 
158 // Create custom item
createItem(const FXString & text,FXIcon *,void * ptr)159 FXListItem *FXColorList::createItem(const FXString& text,FXIcon*,void* ptr){
160   return new FXColorItem(text,FXRGB(0,0,0),ptr);
161   }
162 
163 
164 // Fill list by appending color items from array of strings and array of colors
fillItems(const FXchar ** strings,FXColor * colors,void * ptr,FXbool notify)165 FXint FXColorList::fillItems(const FXchar** strings,FXColor *colors,void* ptr,FXbool notify){
166   FXint n=0;
167   if(strings){
168     while(strings[n]){
169       appendItem(strings[n],colors[n],ptr,notify);
170       n++;
171       }
172     }
173   return n;
174   }
175 
176 
177 // Insert item at index with given text, color, and user-data pointer
insertItem(FXint index,const FXString & text,FXColor color,void * ptr,FXbool notify)178 FXint FXColorList::insertItem(FXint index,const FXString& text,FXColor color,void* ptr,FXbool notify){
179   FXint pos=FXList::insertItem(index,text,NULL,ptr,notify);
180   setItemColor(pos,color);
181   return pos;
182   }
183 
184 
185 // Append new item with given text, color, and user-data pointer
appendItem(const FXString & text,FXColor color,void * ptr,FXbool notify)186 FXint FXColorList::appendItem(const FXString& text,FXColor color,void* ptr,FXbool notify){
187   FXint pos=FXList::appendItem(text,NULL,ptr,notify);
188   setItemColor(pos,color);
189   return pos;
190   }
191 
192 // Prepend new item with given text, color, and user-data pointer
prependItem(const FXString & text,FXColor color,void * ptr,FXbool notify)193 FXint FXColorList::prependItem(const FXString& text,FXColor color,void* ptr,FXbool notify){
194   FXint pos=FXList::prependItem(text,NULL,ptr,notify);
195   setItemColor(pos,color);
196   return pos;
197   }
198 
199 
200 // Change item color
setItemColor(FXint index,FXColor color)201 void FXColorList::setItemColor(FXint index,FXColor color){
202   if(index<0 || items.no()<=index){ fxerror("%s::setItemData: index out of range.\n",getClassName()); }
203   ((FXColorItem*)items[index])->setColor(color);
204   }
205 
206 
207 // Return item color
getItemColor(FXint index) const208 FXColor FXColorList::getItemColor(FXint index) const {
209   if(index<0 || items.no()<=index){ fxerror("%s::getItemData: index out of range.\n",getClassName()); }
210   return ((FXColorItem*)items[index])->getColor();
211   }
212 
213 }
214 
215