File: FXTriStateButton.cpp

package info (click to toggle)
gogglesmm 1.2.5-6
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 16,812 kB
  • sloc: cpp: 231,960; ansic: 893; xml: 222; makefile: 33
file content (443 lines) | stat: -rw-r--r-- 12,800 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/********************************************************************************
*                                                                               *
*               T r i - S t a t e    B u t t o n    W i d g e t                 *
*                                                                               *
*********************************************************************************
* Copyright (C) 2002,2022 by Charles Warren.   All Rights Reserved.             *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify          *
* it under the terms of the GNU Lesser General Public License as published by   *
* the Free Software Foundation; either version 3 of the License, or             *
* (at your option) any later version.                                           *
*                                                                               *
* This library is distributed in the hope that it will be useful,               *
* but WITHOUT ANY WARRANTY; without even the implied warranty of                *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
* GNU Lesser General Public License for more details.                           *
*                                                                               *
* You should have received a copy of the GNU Lesser General Public License      *
* along with this program.  If not, see <http://www.gnu.org/licenses/>          *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "fxmath.h"
#include "fxkeys.h"
#include "FXArray.h"
#include "FXHash.h"
#include "FXMutex.h"
#include "FXStream.h"
#include "FXString.h"
#include "FXSize.h"
#include "FXPoint.h"
#include "FXRectangle.h"
#include "FXStringDictionary.h"
#include "FXSettings.h"
#include "FXRegistry.h"
#include "FXEvent.h"
#include "FXWindow.h"
#include "FXDCWindow.h"
#include "FXApp.h"
#include "FXIcon.h"
#include "FXToggleButton.h"
#include "FXTriStateButton.h"

/*
  Notes:

  - Small change from Charles' original code:- normal label and
    icon are now used as fallback; this means its possible for example
    to just specify three icons and use same text for all, or vice-versa
    just specify three labels and use same icons for all.
  - Likewise, help text and tip text follow similar fallback scheme.
  - Since there is no way INTO the MAYBE state except programmatically,
    there is no attempt to install a hotkey for this!
  - The fallback logic needs to be properly adhered to in the size computation.
*/

using namespace FX;

/*******************************************************************************/

namespace FX {

// Map
FXDEFMAP(FXTriStateButton) FXTriStateButtonMap[]={
  FXMAPFUNC(SEL_UPDATE,0,FXTriStateButton::onUpdate),
  FXMAPFUNC(SEL_PAINT,0,FXTriStateButton::onPaint),
  FXMAPFUNC(SEL_QUERY_TIP,0,FXTriStateButton::onQueryTip),
  FXMAPFUNC(SEL_QUERY_HELP,0,FXTriStateButton::onQueryHelp),
  FXMAPFUNC(SEL_COMMAND,FXWindow::ID_UNKNOWN,FXTriStateButton::onUnknown)
  };


// Object implementation
FXIMPLEMENT(FXTriStateButton,FXToggleButton,FXTriStateButtonMap,ARRAYNUMBER(FXTriStateButtonMap))


// Deserialization
FXTriStateButton::FXTriStateButton(){
  maybeicon=(FXIcon*)-1L;
  }


// Construct and init
FXTriStateButton::FXTriStateButton(FXComposite* p,const FXString& text1,const FXString& text2,const FXString& text3,FXIcon* icon1,FXIcon* icon2,FXIcon* icon3,FXObject* tgt,FXSelector sel,FXuint opts,FXint x,FXint y,FXint w,FXint h,FXint pl,FXint pr,FXint pt,FXint pb):FXToggleButton(p,text1,text2,icon1,icon2,tgt,sel,opts,x,y,w,h,pl,pr,pt,pb){
  maybelabel=text3.section('\t',0);
  maybetip=text3.section('\t',1);
  maybehelp=text3.section('\t',2);
  maybeicon=icon3;
  }


// Create window
void FXTriStateButton::create(){
  FXToggleButton::create();
  if(maybeicon) maybeicon->create();
  }


// Detach window
void FXTriStateButton::detach(){
  FXToggleButton::detach();
  if(maybeicon) maybeicon->detach();
  }


// Get default width
FXint FXTriStateButton::getDefaultWidth(){
  FXint tw,iw,s,w1,w2,w3;

  tw=iw=s=0;
  if(!label.empty()) tw=labelWidth(label);
  if(icon) iw=icon->getWidth();
  if(iw && tw) s=4;
  if(!(options&(ICON_AFTER_TEXT|ICON_BEFORE_TEXT))) w1=FXMAX(tw,iw); else w1=tw+iw+s;

  tw=iw=s=0;
  if(!altlabel.empty()) tw=labelWidth(altlabel);
  if(alticon) iw=alticon->getWidth();
  if(iw && tw) s=4;
  if(!(options&(ICON_AFTER_TEXT|ICON_BEFORE_TEXT))) w2=FXMAX(tw,iw); else w2=tw+iw+s;

  tw=iw=s=0;
  if(!maybelabel.empty()) tw=labelWidth(maybelabel);
  if(maybeicon) iw=maybeicon->getWidth();
  if(iw && tw) s=4;
  if(!(options&(ICON_AFTER_TEXT|ICON_BEFORE_TEXT))) w3=FXMAX(tw,iw); else w3=tw+iw+s;

  return FXMAX3(w1,w2,w3)+padleft+padright+(border<<1);
  }


// Get default height
FXint FXTriStateButton::getDefaultHeight(){
  FXint th,ih,h1,h2,h3;

  th=ih=0;
  if(!label.empty()) th=labelHeight(label);
  if(icon) ih=icon->getHeight();
  if(!(options&(ICON_ABOVE_TEXT|ICON_BELOW_TEXT))) h1=FXMAX(th,ih); else h1=th+ih;

  th=ih=0;
  if(!altlabel.empty()) th=labelHeight(altlabel);
  if(alticon) ih=alticon->getHeight();
  if(!(options&(ICON_ABOVE_TEXT|ICON_BELOW_TEXT))) h2=FXMAX(th,ih); else h2=th+ih;

  th=ih=0;
  if(!maybelabel.empty()) th=labelHeight(maybelabel);
  if(maybeicon) ih=maybeicon->getHeight();
  if(!(options&(ICON_ABOVE_TEXT|ICON_BELOW_TEXT))) h3=FXMAX(th,ih); else h3=th+ih;

  return FXMAX3(h1,h2,h3)+padtop+padbottom+(border<<1);
  }


// Check the menu button
long FXTriStateButton::onUnknown(FXObject*,FXSelector,void*){
  setState(maybe);
  return 1;
  }


// We were asked about status text
long FXTriStateButton::onQueryHelp(FXObject* sender,FXSelector sel,void* ptr){
  if(FXWindow::onQueryHelp(sender,sel,ptr)) return 1;
  if(flags&FLAG_HELP){
    if(state==true){
      if(!althelp.empty()){
        sender->handle(this,FXSEL(SEL_COMMAND,ID_SETSTRINGVALUE),(void*)&althelp);
        return 1;
        }
      }
    else if(state==maybe){
      if(!maybehelp.empty()){
        sender->handle(this,FXSEL(SEL_COMMAND,ID_SETSTRINGVALUE),(void*)&maybehelp);
        return 1;
        }
      }
    if(!help.empty()){
      if(!help.empty()){
        sender->handle(this,FXSEL(SEL_COMMAND,ID_SETSTRINGVALUE),(void*)&help);
        return 1;
        }
      }
    }
  return 0;
  }


// We were asked about tip text
long FXTriStateButton::onQueryTip(FXObject* sender,FXSelector sel,void* ptr){
  if(FXWindow::onQueryTip(sender,sel,ptr)) return 1;
  if(flags&FLAG_TIP){
    if(state==true){
      if(!alttip.empty()){
        sender->handle(this,FXSEL(SEL_COMMAND,ID_SETSTRINGVALUE),(void*)&alttip);
        return 1;
        }
      }
    else if(state==maybe){
      if(!maybetip.empty()){
        sender->handle(this,FXSEL(SEL_COMMAND,ID_SETSTRINGVALUE),(void*)&maybetip);
        return 1;
        }
      }
    if(!tip.empty()){
      sender->handle(this,FXSEL(SEL_COMMAND,ID_SETSTRINGVALUE),(void*)&tip);
      return 1;
      }
    }
  return 0;
  }


// Handle repaint
long FXTriStateButton::onPaint(FXObject*,FXSelector,void* ptr){
  FXint tw=0,th=0,iw=0,ih=0,tx,ty,ix,iy;
  FXEvent *ev=(FXEvent*)ptr;
  FXDCWindow dc(this,ev);

  // Got a border at all?
  if(options&(FRAME_RAISED|FRAME_SUNKEN)){

    // Toolbar style
    if(options&TOGGLEBUTTON_TOOLBAR){

      // Enabled and cursor inside and down
      if(down){
        dc.setForeground(hiliteColor);
        dc.fillRectangle(border,border,width-border*2,height-border*2);
        if(options&FRAME_THICK) drawDoubleSunkenRectangle(dc,0,0,width,height);
        else drawSunkenRectangle(dc,0,0,width,height);
        }

      // Enabled and cursor inside, and up
      else if(isEnabled() && underCursor()){
        dc.setForeground(backColor);
        dc.fillRectangle(border,border,width-border*2,height-border*2);
        if(options&FRAME_THICK) drawDoubleRaisedRectangle(dc,0,0,width,height);
        else drawRaisedRectangle(dc,0,0,width,height);
        }

      // Disabled or unchecked or not under cursor
      else{
        dc.setForeground(backColor);
        dc.fillRectangle(0,0,width,height);
        }
      }

    // Normal style
    else{

      // Draw sunken if pressed
      if(down){
        dc.setForeground(hiliteColor);
        dc.fillRectangle(border,border,width-border*2,height-border*2);
        if(options&FRAME_THICK) drawDoubleSunkenRectangle(dc,0,0,width,height);
        else drawSunkenRectangle(dc,0,0,width,height);
        }

      // Draw raised if not currently pressed down
      else{
        dc.setForeground(backColor);
        dc.fillRectangle(border,border,width-border*2,height-border*2);
        if(options&FRAME_THICK) drawDoubleRaisedRectangle(dc,0,0,width,height);
        else drawRaisedRectangle(dc,0,0,width,height);
        }

      }
    }

  // No borders
  else{
    dc.setForeground(backColor);
    dc.fillRectangle(0,0,width,height);
    }

  // Place text & icon
  if(state==true && !altlabel.empty()){
    tw=labelWidth(altlabel);
    th=labelHeight(altlabel);
    }
  else if(state==maybe && !maybelabel.empty()){
    tw=labelWidth(maybelabel);
    th=labelHeight(maybelabel);
    }
  else if(!label.empty()){
    tw=labelWidth(label);
    th=labelHeight(label);
    }
  if(state==true && alticon){
    iw=alticon->getWidth();
    ih=alticon->getHeight();
    }
  else if(state==maybe && maybeicon){
    iw=maybeicon->getWidth();
    ih=maybeicon->getHeight();
    }
  else if(icon){
    iw=icon->getWidth();
    ih=icon->getHeight();
    }

  just_x(tx,ix,tw,iw);
  just_y(ty,iy,th,ih);

  // Shift a bit when pressed
  if(down && (options&(FRAME_RAISED|FRAME_SUNKEN))){ ++tx; ++ty; ++ix; ++iy; }

  // Draw enabled state
  if(isEnabled()){

    // Paint the right icon
    if(state==true && alticon){
      dc.drawIcon(alticon,ix,iy);
      }
    else if(state==maybe &&maybeicon){
      dc.drawIcon(maybeicon,ix,iy);
      }
    else if(icon){
      dc.drawIcon(icon,ix,iy);
      }

    // Paint the right text
    if(state==true && !altlabel.empty()){
      dc.setFont(font);
      dc.setForeground(textColor);
      drawLabel(dc,altlabel,althotoff,tx,ty,tw,th);
      }
    else if(state==maybe && !maybelabel.empty()){
      dc.setFont(font);
      dc.setForeground(textColor);
      drawLabel(dc,maybelabel,-1,tx,ty,tw,th);
      }
    else if(!label.empty()){
      dc.setFont(font);
      dc.setForeground(textColor);
      drawLabel(dc,label,hotoff,tx,ty,tw,th);
      }
    if(hasFocus()){
      dc.drawFocusRectangle(border+1,border+1,width-2*border-2,height-2*border-2);
      }
    }

  // Draw grayed-out state
  else{

    // Paint the right icon
    if(state==true && alticon){
      dc.drawIconSunken(alticon,ix,iy);
      }
    else if(state==maybe &&maybeicon){
      dc.drawIconSunken(maybeicon,ix,iy);
      }
    else if(icon){
      dc.drawIconSunken(icon,ix,iy);
      }

    // Paint the right text
    if(state==true && !altlabel.empty()){
      dc.setFont(font);
      dc.setForeground(hiliteColor);
      drawLabel(dc,altlabel,althotoff,tx+1,ty+1,tw,th);
      dc.setForeground(shadowColor);
      drawLabel(dc,altlabel,althotoff,tx,ty,tw,th);
      }
    else if(state==maybe && !maybelabel.empty()){
      dc.setFont(font);
      dc.setForeground(hiliteColor);
      drawLabel(dc,maybelabel,-1,tx+1,ty+1,tw,th);
      dc.setForeground(shadowColor);
      drawLabel(dc,maybelabel,-1,tx,ty,tw,th);
      }
    else if(!label.empty()){
      dc.setFont(font);
      dc.setForeground(hiliteColor);
      drawLabel(dc,label,hotoff,tx+1,ty+1,tw,th);
      dc.setForeground(shadowColor);
      drawLabel(dc,label,hotoff,tx,ty,tw,th);
      }
    }
  return 1;
  }


// Change text
void FXTriStateButton::setMaybeText(const FXString& text){
  if(maybelabel!=text){
    maybelabel=text;
    recalc();
    update();
    }
  }


// Change icon
void FXTriStateButton::setMaybeIcon(FXIcon* ic){
  if(maybeicon!=ic){
    maybeicon=ic;
    recalc();
    update();
    }
  }


// Change help text
void FXTriStateButton::setMaybeHelpText(const FXString& text){
  maybehelp=text;
  }


// Change tip text
void FXTriStateButton::setMaybeTipText(const FXString& text){
  maybetip=text;
  }


// Save object to stream
void FXTriStateButton::save(FXStream& store) const {
  FXToggleButton::save(store);
  store << maybelabel;
  store << maybeicon;
  store << maybetip;
  store << maybehelp;
  }


// Load object from stream
void FXTriStateButton::load(FXStream& store){
  FXToggleButton::load(store);
  store >> maybelabel;
  store >> maybeicon;
  store >> maybetip;
  store >> maybehelp;
  }


// Destruct
FXTriStateButton::~FXTriStateButton(){
  maybeicon=(FXIcon*)-1L;
  }

}