1 /********************************************************************************
2 *                                                                               *
3 *                          T a b   B a r   W i d g e t                          *
4 *                                                                               *
5 *********************************************************************************
6 * Copyright (C) 1997,2021 by Jeroen van der Zijp.   All Rights Reserved.        *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify          *
9 * it under the terms of the GNU Lesser General Public License as published by   *
10 * the Free Software Foundation; either version 3 of the License, or             *
11 * (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                 *
16 * GNU Lesser General Public License for more details.                           *
17 *                                                                               *
18 * You should have received a copy of the GNU Lesser General Public License      *
19 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
20 ********************************************************************************/
21 #include "xincs.h"
22 #include "fxver.h"
23 #include "fxdefs.h"
24 #include "fxmath.h"
25 #include "fxkeys.h"
26 #include "FXArray.h"
27 #include "FXHash.h"
28 #include "FXMutex.h"
29 #include "FXStream.h"
30 #include "FXString.h"
31 #include "FXSize.h"
32 #include "FXPoint.h"
33 #include "FXRectangle.h"
34 #include "FXStringDictionary.h"
35 #include "FXSettings.h"
36 #include "FXRegistry.h"
37 #include "FXAccelTable.h"
38 #include "FXEvent.h"
39 #include "FXFont.h"
40 #include "FXWindow.h"
41 #include "FXDCWindow.h"
42 #include "FXApp.h"
43 #include "FXIcon.h"
44 #include "FXTabBar.h"
45 
46 
47 /*
48   Notes:
49   - Should focus go to tab items?
50   - Tab items should observe various border styles.
51   - TAB/TABTAB should go into content, arrow keys navigate between tabs.
52 */
53 
54 
55 #define TAB_ORIENT_MASK    (TAB_TOP|TAB_LEFT|TAB_RIGHT|TAB_BOTTOM)
56 #define TABBOOK_MASK       (TABBOOK_SIDEWAYS|TABBOOK_BOTTOMTABS)
57 #define REVEAL_PIXELS      20
58 
59 using namespace FX;
60 
61 /*******************************************************************************/
62 
63 namespace FX {
64 
65 // Map
66 FXDEFMAP(FXTabBar) FXTabBarMap[]={
67   FXMAPFUNC(SEL_PAINT,0,FXTabBar::onPaint),
68   FXMAPFUNC(SEL_FOCUS_NEXT,0,FXTabBar::onFocusNext),
69   FXMAPFUNC(SEL_FOCUS_PREV,0,FXTabBar::onFocusPrev),
70   FXMAPFUNC(SEL_FOCUS_UP,0,FXTabBar::onFocusUp),
71   FXMAPFUNC(SEL_FOCUS_DOWN,0,FXTabBar::onFocusDown),
72   FXMAPFUNC(SEL_FOCUS_LEFT,0,FXTabBar::onFocusLeft),
73   FXMAPFUNC(SEL_FOCUS_RIGHT,0,FXTabBar::onFocusRight),
74   FXMAPFUNC(SEL_COMMAND,FXTabBar::ID_OPEN_ITEM,FXTabBar::onCmdOpenItem),
75   FXMAPFUNC(SEL_COMMAND,FXTabBar::ID_SETVALUE,FXTabBar::onCmdSetValue),
76   FXMAPFUNC(SEL_COMMAND,FXTabBar::ID_SETINTVALUE,FXTabBar::onCmdSetIntValue),
77   FXMAPFUNC(SEL_COMMAND,FXTabBar::ID_GETINTVALUE,FXTabBar::onCmdGetIntValue),
78   FXMAPFUNCS(SEL_UPDATE,FXTabBar::ID_OPEN_FIRST,FXTabBar::ID_OPEN_LAST,FXTabBar::onUpdOpen),
79   FXMAPFUNCS(SEL_COMMAND,FXTabBar::ID_OPEN_FIRST,FXTabBar::ID_OPEN_LAST,FXTabBar::onCmdOpen),
80   };
81 
82 
83 // Object implementation
FXIMPLEMENT(FXTabBar,FXPacker,FXTabBarMap,ARRAYNUMBER (FXTabBarMap))84 FXIMPLEMENT(FXTabBar,FXPacker,FXTabBarMap,ARRAYNUMBER(FXTabBarMap))
85 
86 
87 // Make a tab bar
88 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):FXPacker(p,opts,x,y,w,h,pl,pr,pt,pb,0,0){
89   flags|=FLAG_ENABLED;
90   target=tgt;
91   message=sel;
92   current=0;
93   shift=0;
94   }
95 
96 
97 // Get width
getDefaultWidth()98 FXint FXTabBar::getDefaultWidth(){
99   FXint w,wtabs,maxtabw,t,ntabs;
100   FXuint hints;
101   FXWindow *child;
102 
103   // Left or right tabs
104   if(options&TABBOOK_SIDEWAYS){
105     wtabs=0;
106     for(child=getFirst(); child; child=child->getNext()){
107       if(child->shown()){
108         hints=child->getLayoutHints();
109         if(hints&LAYOUT_FIX_WIDTH) t=child->getWidth()-2; else t=child->getDefaultWidth()-2;
110         if(t>wtabs) wtabs=t;
111         }
112       }
113     w=wtabs;
114     }
115 
116   // Top or bottom tabs
117   else{
118     wtabs=maxtabw=ntabs=0;
119     for(child=getFirst(); child; child=child->getNext()){
120       if(child->shown()){
121         hints=child->getLayoutHints();
122         if(hints&LAYOUT_FIX_WIDTH) t=child->getWidth(); else t=child->getDefaultWidth();
123         if(t>maxtabw) maxtabw=t;
124         wtabs+=t;
125         ntabs++;
126         }
127       }
128     if(options&PACK_UNIFORM_WIDTH) wtabs=ntabs*maxtabw;
129     w=wtabs+5;
130     }
131   return w+padleft+padright+(border<<1);
132   }
133 
134 
135 // Get height
getDefaultHeight()136 FXint FXTabBar::getDefaultHeight(){
137   FXint h,htabs,maxtabh,t,ntabs;
138   FXuint hints;
139   FXWindow *child;
140 
141   // Left or right tabs
142   if(options&TABBOOK_SIDEWAYS){
143     htabs=maxtabh=ntabs=0;
144     for(child=getFirst(); child; child=child->getNext()){
145       if(child->shown()){
146         hints=child->getLayoutHints();
147         if(hints&LAYOUT_FIX_HEIGHT) t=child->getHeight(); else t=child->getDefaultHeight();
148         if(t>maxtabh) maxtabh=t;
149         htabs+=t;
150         ntabs++;
151         }
152       }
153     if(options&PACK_UNIFORM_HEIGHT) htabs=ntabs*maxtabh;
154     h=htabs+5;
155     }
156 
157   // Top or bottom tabs
158   else{
159     htabs=0;
160     for(child=getFirst(); child; child=child->getNext()){
161       if(child->shown()){
162         hints=child->getLayoutHints();
163         if(hints&LAYOUT_FIX_HEIGHT) t=child->getHeight()-2; else t=child->getDefaultHeight()-2;
164         if(t>htabs) htabs=t;
165         }
166       }
167     h=htabs;
168     }
169   return h+padtop+padbottom+(border<<1);
170   }
171 
172 
173 // Recalculate layout
layout()174 void FXTabBar::layout(){
175   FXint i,px,py,pw,ph,x,y,xx,yy,w,h,maxtabw,maxtabh,cumw,cumh,newcurrent;
176   FXWindow *raisetab=NULL;
177   FXWindow *tab;
178   FXuint hints;
179 
180   newcurrent=-1;
181 
182   // Measure tabs again
183   maxtabw=maxtabh=0;
184   for(tab=getFirst(),i=0; tab; tab=tab->getNext(),i++){
185     if(tab->shown()){
186       hints=tab->getLayoutHints();
187       if(newcurrent<0 || i<=current) newcurrent=i;
188       if(hints&LAYOUT_FIX_WIDTH) w=tab->getWidth(); else w=tab->getDefaultWidth();
189       if(hints&LAYOUT_FIX_HEIGHT) h=tab->getHeight(); else h=tab->getDefaultHeight();
190       if(w>maxtabw) maxtabw=w;
191       if(h>maxtabh) maxtabh=h;
192       }
193     }
194 
195   // Changes current only if old current no longer visible
196   current=newcurrent;
197 
198   // Tabs on left or right
199   if(options&TABBOOK_SIDEWAYS){
200 
201     // Place panel
202     py=border+padtop;
203     ph=height-padtop-padbottom-(border<<1);
204 
205     // Scroll as appropriate
206     for(tab=getFirst(),cumh=i=0; tab; tab=tab->getNext(),i++){
207       if(tab->shown()){
208         hints=tab->getLayoutHints();
209         if(hints&LAYOUT_FIX_HEIGHT) h=tab->getHeight();
210         else if(options&PACK_UNIFORM_HEIGHT) h=maxtabh;
211         else h=tab->getDefaultHeight();
212         if(i==current){
213           if(tab->getNext()){
214             if(cumh+shift+h>ph-REVEAL_PIXELS-2) shift=ph-cumh-h-REVEAL_PIXELS-2;
215             }
216           else{
217             if(cumh+shift+h>ph-2) shift=ph-cumh-h-2;
218             }
219           if(tab->getPrev()){
220             if(cumh+shift<REVEAL_PIXELS+2) shift=REVEAL_PIXELS+2-cumh;
221             }
222           else{
223             if(cumh+shift<2) shift=2-cumh;
224             }
225           }
226         cumh+=h;
227         }
228       }
229 
230     // Adjust shift based on space
231     if(shift<ph-cumh-2) shift=ph-cumh-2;
232     if(shift>0) shift=0;
233 
234     // Place all of the children
235     for(tab=getFirst(),yy=py+shift,i=0; tab; tab=tab->getNext(),i++){
236       if(tab->shown()){
237         hints=tab->getLayoutHints();
238         if(hints&LAYOUT_FIX_WIDTH) w=tab->getWidth();
239         else if(options&PACK_UNIFORM_WIDTH) w=maxtabw;
240         else w=tab->getDefaultWidth();
241         if(hints&LAYOUT_FIX_HEIGHT) h=tab->getHeight();
242         else if(options&PACK_UNIFORM_HEIGHT) h=maxtabh;
243         else h=tab->getDefaultHeight();
244         if(i<current){
245           y=yy+2;
246           if(y+h>py+ph-2) y=py+ph-2-h;
247           if(y<py+2) y=py+2;
248           if(options&TABBOOK_BOTTOMTABS)
249             tab->position(-4,y,w,h);
250           else
251             tab->position(width-w+4,y,w,h);
252           tab->raise();
253           yy+=h;
254           }
255         else if(i>current){
256           y=yy+2;
257           if(y+h>py+ph-2) y=py+ph-h-2;
258           if(y<py+2) y=py+2;
259           if(options&TABBOOK_BOTTOMTABS)
260             tab->position(-4,y,w,h);
261           else
262             tab->position(width-w+4,y,w,h);
263           tab->lower();
264           yy+=h;
265           }
266         else{
267           y=yy;
268           if(y+h>py+ph-2) y=py+ph-h-2;
269           if(y<py) y=py;
270           if(options&TABBOOK_BOTTOMTABS)
271             tab->position(-2,y,w,h);
272           else
273             tab->position(width-w+2,y,w,h);
274           raisetab=tab;
275           yy+=h-3;
276           }
277         }
278       }
279     }
280 
281   // Tabs on top or bottom
282   else{
283 
284     // Place panel
285     px=border+padleft;
286     pw=width-padleft-padright-(border<<1);
287 
288     // Scroll as appropriate
289     for(tab=getFirst(),cumw=i=0; tab; tab=tab->getNext(),i++){
290       if(tab->shown()){
291         hints=tab->getLayoutHints();
292         if(hints&LAYOUT_FIX_WIDTH) w=tab->getWidth();
293         else if(options&PACK_UNIFORM_WIDTH) w=maxtabw;
294         else w=tab->getDefaultWidth();
295         if(i==current){
296           if(tab->getNext()){
297             if(cumw+shift+w>pw-REVEAL_PIXELS-2) shift=pw-cumw-w-REVEAL_PIXELS-2;
298             }
299           else{
300             if(cumw+shift+w>pw-2) shift=pw-cumw-w-2;
301             }
302           if(tab->getPrev()){
303             if(cumw+shift<REVEAL_PIXELS+2) shift=REVEAL_PIXELS+2-cumw;
304             }
305           else{
306             if(cumw+shift<2) shift=2-cumw;
307             }
308           }
309         cumw+=w;
310         }
311       }
312 
313     // Adjust shift based on space
314     if(shift<pw-cumw-2) shift=pw-cumw-2;
315     if(shift>0) shift=0;
316 
317     // Place all of the children
318     for(tab=getFirst(),xx=px+shift,i=0; tab; tab=tab->getNext(),i++){
319       if(tab->shown()){
320         hints=tab->getLayoutHints();
321         if(hints&LAYOUT_FIX_WIDTH) w=tab->getWidth();
322         else if(options&PACK_UNIFORM_WIDTH) w=maxtabw;
323         else w=tab->getDefaultWidth();
324         if(hints&LAYOUT_FIX_HEIGHT) h=tab->getHeight();
325         else if(options&PACK_UNIFORM_HEIGHT) h=maxtabh;
326         else h=tab->getDefaultHeight();
327         if(i<current){
328           x=xx+2;
329           if(x+w>px+pw-2) x=px+pw-2-w;
330           if(x<px+2) x=px+2;
331           if(options&TABBOOK_BOTTOMTABS)
332             tab->position(x,-4,w,h);
333           else
334             tab->position(x,height-h+4,w,h);
335           tab->raise();
336           xx+=w;
337           }
338         else if(i>current){
339           x=xx+2;
340           if(x+w>px+pw-2) x=px+pw-w-2;
341           if(x<px+2) x=px+2;
342           if(options&TABBOOK_BOTTOMTABS)
343             tab->position(x+2,-4,w,h);
344           else
345             tab->position(x+2,height-h+4,w,h);
346           tab->lower();
347           xx+=w;
348           }
349         else{
350           x=xx;
351           if(x+w>px+pw-2) x=px+pw-w-2;
352           if(x<px) x=px;
353           if(options&TABBOOK_BOTTOMTABS)
354             tab->position(x,-2,w,h);
355           else
356             tab->position(x,height-h+2,w,h);
357           raisetab=tab;
358           xx+=w-3;
359           }
360         }
361       }
362     }
363 
364   // Raise tab
365   if(raisetab) raisetab->raise();
366 
367   flags&=~FLAG_DIRTY;
368   }
369 
370 
371 // Set current subwindow
setCurrent(FXint index,FXbool notify)372 void FXTabBar::setCurrent(FXint index,FXbool notify){
373   if(index!=current && 0<=index && index<numChildren()){
374     current=index;
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