1 /********************************************************************************
2 * *
3 * T a b O b j e c t *
4 * *
5 *********************************************************************************
6 * Copyright (C) 1997,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: FXTabBar.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 "FXRectangle.h"
35 #include "FXRegistry.h"
36 #include "FXAccelTable.h"
37 #include "FXApp.h"
38 #include "FXDCWindow.h"
39 #include "FXFont.h"
40 #include "FXIcon.h"
41 #include "FXTabBar.h"
42 #include "FXTabBook.h"
43
44
45 /*
46 Notes:
47 - Should focus go to tab items?
48 - Tab items should observe various border styles.
49 - TAB/TABTAB should go into content, arrow keys navigate between tabs.
50 */
51
52
53 #define TAB_ORIENT_MASK (TAB_TOP|TAB_LEFT|TAB_RIGHT|TAB_BOTTOM)
54 #define TABBOOK_MASK (TABBOOK_SIDEWAYS|TABBOOK_BOTTOMTABS)
55 #define REVEAL_PIXELS 20
56
57 using namespace FX;
58
59 /*******************************************************************************/
60
61 namespace FX {
62
63 // Map
64 FXDEFMAP(FXTabBar) FXTabBarMap[]={
65 FXMAPFUNC(SEL_PAINT,0,FXTabBar::onPaint),
66 FXMAPFUNC(SEL_FOCUS_NEXT,0,FXTabBar::onFocusNext),
67 FXMAPFUNC(SEL_FOCUS_PREV,0,FXTabBar::onFocusPrev),
68 FXMAPFUNC(SEL_FOCUS_UP,0,FXTabBar::onFocusUp),
69 FXMAPFUNC(SEL_FOCUS_DOWN,0,FXTabBar::onFocusDown),
70 FXMAPFUNC(SEL_FOCUS_LEFT,0,FXTabBar::onFocusLeft),
71 FXMAPFUNC(SEL_FOCUS_RIGHT,0,FXTabBar::onFocusRight),
72 FXMAPFUNC(SEL_COMMAND,FXTabBar::ID_OPEN_ITEM,FXTabBar::onCmdOpenItem),
73 FXMAPFUNC(SEL_COMMAND,FXTabBar::ID_SETVALUE,FXTabBar::onCmdSetValue),
74 FXMAPFUNC(SEL_COMMAND,FXTabBar::ID_SETINTVALUE,FXTabBar::onCmdSetIntValue),
75 FXMAPFUNC(SEL_COMMAND,FXTabBar::ID_GETINTVALUE,FXTabBar::onCmdGetIntValue),
76 FXMAPFUNCS(SEL_UPDATE,FXTabBar::ID_OPEN_FIRST,FXTabBar::ID_OPEN_LAST,FXTabBar::onUpdOpen),
77 FXMAPFUNCS(SEL_COMMAND,FXTabBar::ID_OPEN_FIRST,FXTabBar::ID_OPEN_LAST,FXTabBar::onCmdOpen),
78 };
79
80
81 // Object implementation
FXIMPLEMENT(FXTabBar,FXPacker,FXTabBarMap,ARRAYNUMBER (FXTabBarMap))82 FXIMPLEMENT(FXTabBar,FXPacker,FXTabBarMap,ARRAYNUMBER(FXTabBarMap))
83
84
85 // Make a tab bar
86 FXTabBar::FXTabBar(FXComposite* p,FXObject* tgt,FXSelector sel,FXuint opts,FXint x,FXint y,FXint w,FXint h,FXint pl,FXint pr,FXint pt,FXint pb):
87 FXPacker(p,opts,x,y,w,h,pl,pr,pt,pb,0,0){
88 flags|=FLAG_ENABLED;
89 target=tgt;
90 message=sel;
91 current=0;
92 shift=0;
93 }
94
95
96 // Get width
getDefaultWidth()97 FXint FXTabBar::getDefaultWidth(){
98 FXint w,wtabs,maxtabw,t,ntabs;
99 FXuint hints;
100 FXWindow *child;
101
102 // Left or right tabs
103 if(options&TABBOOK_SIDEWAYS){
104 wtabs=0;
105 for(child=getFirst(); child; child=child->getNext()){
106 if(child->shown()){
107 hints=child->getLayoutHints();
108 if(hints&LAYOUT_FIX_WIDTH) t=child->getWidth()-2; else t=child->getDefaultWidth()-2;
109 if(t>wtabs) wtabs=t;
110 }
111 }
112 w=wtabs;
113 }
114
115 // Top or bottom tabs
116 else{
117 wtabs=maxtabw=ntabs=0;
118 for(child=getFirst(); child; child=child->getNext()){
119 if(child->shown()){
120 hints=child->getLayoutHints();
121 if(hints&LAYOUT_FIX_WIDTH) t=child->getWidth(); else t=child->getDefaultWidth();
122 if(t>maxtabw) maxtabw=t;
123 wtabs+=t;
124 ntabs++;
125 }
126 }
127 if(options&PACK_UNIFORM_WIDTH) wtabs=ntabs*maxtabw;
128 w=wtabs+5;
129 }
130 return w+padleft+padright+(border<<1);
131 }
132
133
134 // Get height
getDefaultHeight()135 FXint FXTabBar::getDefaultHeight(){
136 FXint h,htabs,maxtabh,t,ntabs;
137 FXuint hints;
138 FXWindow *child;
139
140 // Left or right tabs
141 if(options&TABBOOK_SIDEWAYS){
142 htabs=maxtabh=ntabs=0;
143 for(child=getFirst(); child; child=child->getNext()){
144 if(child->shown()){
145 hints=child->getLayoutHints();
146 if(hints&LAYOUT_FIX_HEIGHT) t=child->getHeight(); else t=child->getDefaultHeight();
147 if(t>maxtabh) maxtabh=t;
148 htabs+=t;
149 ntabs++;
150 }
151 }
152 if(options&PACK_UNIFORM_HEIGHT) htabs=ntabs*maxtabh;
153 h=htabs+5;
154 }
155
156 // Top or bottom tabs
157 else{
158 htabs=0;
159 for(child=getFirst(); child; child=child->getNext()){
160 if(child->shown()){
161 hints=child->getLayoutHints();
162 if(hints&LAYOUT_FIX_HEIGHT) t=child->getHeight()-2; else t=child->getDefaultHeight()-2;
163 if(t>htabs) htabs=t;
164 }
165 }
166 h=htabs;
167 }
168 return h+padtop+padbottom+(border<<1);
169 }
170
171
172 // Recalculate layout
layout()173 void FXTabBar::layout(){
174 FXint i,px,py,pw,ph,x,y,xx,yy,w,h,maxtabw,maxtabh,cumw,cumh,newcurrent;
175 FXWindow *raisetab=NULL;
176 FXWindow *tab;
177 FXuint hints;
178
179 newcurrent=-1;
180
181 // Measure tabs again
182 maxtabw=maxtabh=0;
183 for(tab=getFirst(),i=0; tab; tab=tab->getNext(),i++){
184 if(tab->shown()){
185 hints=tab->getLayoutHints();
186 if(newcurrent<0 || i<=current) newcurrent=i;
187 if(hints&LAYOUT_FIX_WIDTH) w=tab->getWidth(); else w=tab->getDefaultWidth();
188 if(hints&LAYOUT_FIX_HEIGHT) h=tab->getHeight(); else h=tab->getDefaultHeight();
189 if(w>maxtabw) maxtabw=w;
190 if(h>maxtabh) maxtabh=h;
191 }
192 }
193
194 // Changes current only if old current no longer visible
195 current=newcurrent;
196
197 // Tabs on left or right
198 if(options&TABBOOK_SIDEWAYS){
199
200 // Place panel
201 py=border+padtop;
202 ph=height-padtop-padbottom-(border<<1);
203
204 // Scroll as appropriate
205 for(tab=getFirst(),cumh=i=0; tab; tab=tab->getNext(),i++){
206 if(tab->shown()){
207 hints=tab->getLayoutHints();
208 if(hints&LAYOUT_FIX_HEIGHT) h=tab->getHeight();
209 else if(options&PACK_UNIFORM_HEIGHT) h=maxtabh;
210 else h=tab->getDefaultHeight();
211 if(i==current){
212 if(tab->getNext()){
213 if(cumh+shift+h>ph-REVEAL_PIXELS-2) shift=ph-cumh-h-REVEAL_PIXELS-2;
214 }
215 else{
216 if(cumh+shift+h>ph-2) shift=ph-cumh-h-2;
217 }
218 if(tab->getPrev()){
219 if(cumh+shift<REVEAL_PIXELS+2) shift=REVEAL_PIXELS+2-cumh;
220 }
221 else{
222 if(cumh+shift<2) shift=2-cumh;
223 }
224 }
225 cumh+=h;
226 }
227 }
228
229 // Adjust shift based on space
230 if(shift<ph-cumh-2) shift=ph-cumh-2;
231 if(shift>0) shift=0;
232
233 // Place all of the children
234 for(tab=getFirst(),yy=py+shift,i=0; tab; tab=tab->getNext(),i++){
235 if(tab->shown()){
236 hints=tab->getLayoutHints();
237 if(hints&LAYOUT_FIX_WIDTH) w=tab->getWidth();
238 else if(options&PACK_UNIFORM_WIDTH) w=maxtabw;
239 else w=tab->getDefaultWidth();
240 if(hints&LAYOUT_FIX_HEIGHT) h=tab->getHeight();
241 else if(options&PACK_UNIFORM_HEIGHT) h=maxtabh;
242 else h=tab->getDefaultHeight();
243 if(i<current){
244 y=yy+2;
245 if(y+h>py+ph-2) y=py+ph-2-h;
246 if(y<py+2) y=py+2;
247 if(options&TABBOOK_BOTTOMTABS)
248 tab->position(-4,y,w,h);
249 else
250 tab->position(width-w+4,y,w,h);
251 tab->raise();
252 yy+=h;
253 }
254 else if(i>current){
255 y=yy+2;
256 if(y+h>py+ph-2) y=py+ph-h-2;
257 if(y<py+2) y=py+2;
258 if(options&TABBOOK_BOTTOMTABS)
259 tab->position(-4,y,w,h);
260 else
261 tab->position(width-w+4,y,w,h);
262 tab->lower();
263 yy+=h;
264 }
265 else{
266 y=yy;
267 if(y+h>py+ph-2) y=py+ph-h-2;
268 if(y<py) y=py;
269 if(options&TABBOOK_BOTTOMTABS)
270 tab->position(-2,y,w,h);
271 else
272 tab->position(width-w+2,y,w,h);
273 raisetab=tab;
274 yy+=h-3;
275 }
276 }
277 }
278 }
279
280 // Tabs on top or bottom
281 else{
282
283 // Place panel
284 px=border+padleft;
285 pw=width-padleft-padright-(border<<1);
286
287 // Scroll as appropriate
288 for(tab=getFirst(),cumw=i=0; tab; tab=tab->getNext(),i++){
289 if(tab->shown()){
290 hints=tab->getLayoutHints();
291 if(hints&LAYOUT_FIX_WIDTH) w=tab->getWidth();
292 else if(options&PACK_UNIFORM_WIDTH) w=maxtabw;
293 else w=tab->getDefaultWidth();
294 if(i==current){
295 if(tab->getNext()){
296 if(cumw+shift+w>pw-REVEAL_PIXELS-2) shift=pw-cumw-w-REVEAL_PIXELS-2;
297 }
298 else{
299 if(cumw+shift+w>pw-2) shift=pw-cumw-w-2;
300 }
301 if(tab->getPrev()){
302 if(cumw+shift<REVEAL_PIXELS+2) shift=REVEAL_PIXELS+2-cumw;
303 }
304 else{
305 if(cumw+shift<2) shift=2-cumw;
306 }
307 }
308 cumw+=w;
309 }
310 }
311
312 // Adjust shift based on space
313 if(shift<pw-cumw-2) shift=pw-cumw-2;
314 if(shift>0) shift=0;
315
316 // Place all of the children
317 for(tab=getFirst(),xx=px+shift,i=0; tab; tab=tab->getNext(),i++){
318 if(tab->shown()){
319 hints=tab->getLayoutHints();
320 if(hints&LAYOUT_FIX_WIDTH) w=tab->getWidth();
321 else if(options&PACK_UNIFORM_WIDTH) w=maxtabw;
322 else w=tab->getDefaultWidth();
323 if(hints&LAYOUT_FIX_HEIGHT) h=tab->getHeight();
324 else if(options&PACK_UNIFORM_HEIGHT) h=maxtabh;
325 else h=tab->getDefaultHeight();
326 if(i<current){
327 x=xx+2;
328 if(x+w>px+pw-2) x=px+pw-2-w;
329 if(x<px+2) x=px+2;
330 if(options&TABBOOK_BOTTOMTABS)
331 tab->position(x,-4,w,h);
332 else
333 tab->position(x,height-h+4,w,h);
334 tab->raise();
335 xx+=w;
336 }
337 else if(i>current){
338 x=xx+2;
339 if(x+w>px+pw-2) x=px+pw-w-2;
340 if(x<px+2) x=px+2;
341 if(options&TABBOOK_BOTTOMTABS)
342 tab->position(xx+2,-4,w,h);
343 else
344 tab->position(xx+2,height-h+4,w,h);
345 tab->lower();
346 xx+=w;
347 }
348 else{
349 x=xx;
350 if(x+w>px+pw-2) x=px+pw-w-2;
351 if(x<px) x=px;
352 if(options&TABBOOK_BOTTOMTABS)
353 tab->position(xx,-2,w,h);
354 else
355 tab->position(xx,height-h+2,w,h);
356 raisetab=tab;
357 xx+=w-3;
358 }
359 }
360 }
361 }
362
363 // Raise tab
364 if(raisetab) raisetab->raise();
365
366 flags&=~FLAG_DIRTY;
367 }
368
369
370 // Set current subwindow
setCurrent(FXint panel,FXbool notify)371 void FXTabBar::setCurrent(FXint panel,FXbool notify){
372 FXint nc=isMemberOf(&FXTabBook::metaClass)?numChildren()>>1:numChildren();
373 if(panel!=current && 0<=panel && panel<nc){
374 current=panel;
375 recalc();
376 if(notify && target){ target->tryHandle(this,FXSEL(SEL_COMMAND,message),(void*)(FXival)current); }
377 }
378 }
379
380
381 // Handle repaint
onPaint(FXObject *,FXSelector,void * ptr)382 long FXTabBar::onPaint(FXObject*,FXSelector,void* ptr){
383 FXEvent *ev=(FXEvent*)ptr;
384 FXDCWindow dc(this,ev);
385 dc.setForeground(backColor);
386 dc.fillRectangle(ev->rect.x,ev->rect.y,ev->rect.w,ev->rect.h);
387 drawFrame(dc,0,0,width,height);
388 return 1;
389 }
390
391
392 // Focus moved to next visible tab
onFocusNext(FXObject *,FXSelector,void * ptr)393 long FXTabBar::onFocusNext(FXObject*,FXSelector,void* ptr){
394 FXWindow *child=getFocus();
395 if(child) child=child->getNext(); else child=getFirst();
396 while(child && !child->shown()) child=child->getNext();
397 if(child){
398 setCurrent(indexOfChild(child),TRUE);
399 child->handle(this,FXSEL(SEL_FOCUS_SELF,0),ptr);
400 return 1;
401 }
402 return 0;
403 }
404
405
406 // Focus moved to previous visible tab
onFocusPrev(FXObject *,FXSelector,void * ptr)407 long FXTabBar::onFocusPrev(FXObject*,FXSelector,void* ptr){
408 FXWindow *child=getFocus();
409 if(child) child=child->getPrev(); else child=getLast();
410 while(child && !child->shown()) child=child->getPrev();
411 if(child){
412 setCurrent(indexOfChild(child),TRUE);
413 child->handle(this,FXSEL(SEL_FOCUS_SELF,0),ptr);
414 return 1;
415 }
416 return 0;
417 }
418
419
420 // Focus moved up
onFocusUp(FXObject *,FXSelector,void * ptr)421 long FXTabBar::onFocusUp(FXObject*,FXSelector,void* ptr){
422 if(options&TABBOOK_SIDEWAYS){
423 return handle(this,FXSEL(SEL_FOCUS_PREV,0),ptr);
424 }
425 return 0;
426 }
427
428
429 // Focus moved down
onFocusDown(FXObject *,FXSelector,void * ptr)430 long FXTabBar::onFocusDown(FXObject*,FXSelector,void* ptr){
431 if(options&TABBOOK_SIDEWAYS){
432 return handle(this,FXSEL(SEL_FOCUS_NEXT,0),ptr);
433 }
434 return 0;
435 }
436
437
438 // Focus moved left
onFocusLeft(FXObject *,FXSelector,void * ptr)439 long FXTabBar::onFocusLeft(FXObject*,FXSelector,void* ptr){
440 if(!(options&TABBOOK_SIDEWAYS)){
441 return handle(this,FXSEL(SEL_FOCUS_PREV,0),ptr);
442 }
443 return 0;
444 }
445
446
447 // Focus moved right
onFocusRight(FXObject *,FXSelector,void * ptr)448 long FXTabBar::onFocusRight(FXObject*,FXSelector,void* ptr){
449 if(!(options&TABBOOK_SIDEWAYS)){
450 return handle(this,FXSEL(SEL_FOCUS_NEXT,0),ptr);
451 }
452 return 0;
453 }
454
455
456 // Update value from a message
onCmdSetValue(FXObject *,FXSelector,void * ptr)457 long FXTabBar::onCmdSetValue(FXObject*,FXSelector,void* ptr){
458 setCurrent((FXint)(FXival)ptr);
459 return 1;
460 }
461
462
463 // Update value from a message
onCmdSetIntValue(FXObject *,FXSelector,void * ptr)464 long FXTabBar::onCmdSetIntValue(FXObject*,FXSelector,void* ptr){
465 setCurrent(*((FXint*)ptr));
466 return 1;
467 }
468
469
470 // Obtain value from text field
onCmdGetIntValue(FXObject *,FXSelector,void * ptr)471 long FXTabBar::onCmdGetIntValue(FXObject*,FXSelector,void* ptr){
472 *((FXint*)ptr)=getCurrent();
473 return 1;
474 }
475
476
477 // Open item
onCmdOpen(FXObject *,FXSelector sel,void *)478 long FXTabBar::onCmdOpen(FXObject*,FXSelector sel,void*){
479 setCurrent(FXSELID(sel)-ID_OPEN_FIRST,TRUE);
480 return 1;
481 }
482
483
484 // Update the nth button
onUpdOpen(FXObject * sender,FXSelector sel,void *)485 long FXTabBar::onUpdOpen(FXObject* sender,FXSelector sel,void*){
486 sender->handle(this,((FXSELID(sel)-ID_OPEN_FIRST)==current)?FXSEL(SEL_COMMAND,ID_CHECK):FXSEL(SEL_COMMAND,ID_UNCHECK),NULL);
487 return 1;
488 }
489
490
491 // The sender of the message is the item to open up
onCmdOpenItem(FXObject * sender,FXSelector,void *)492 long FXTabBar::onCmdOpenItem(FXObject* sender,FXSelector,void*){
493 setCurrent(indexOfChild((FXWindow*)sender),TRUE);
494 return 1;
495 }
496
497
498 // Get tab style
getTabStyle() const499 FXuint FXTabBar::getTabStyle() const {
500 return (options&TABBOOK_MASK);
501 }
502
503
504 // Set tab style
setTabStyle(FXuint style)505 void FXTabBar::setTabStyle(FXuint style){
506 FXuint opts=(options&~TABBOOK_MASK) | (style&TABBOOK_MASK);
507 if(options!=opts){
508 options=opts;
509 recalc();
510 update();
511 }
512 }
513
514
515 // Save object to stream
save(FXStream & store) const516 void FXTabBar::save(FXStream& store) const {
517 FXPacker::save(store);
518 store << current;
519 }
520
521
522 // Load object from stream
load(FXStream & store)523 void FXTabBar::load(FXStream& store){
524 FXPacker::load(store);
525 store >> current;
526 }
527
528 }
529