5 typedef struct W_TabView
{
9 struct W_TabViewItem
**items
;
11 int maxItems
; /* size of items array */
20 WMTabViewDelegate
*delegate
;
26 WMReliefType relief
:4;
27 WMTitlePosition titlePosition
:4;
28 WMTabViewTypes type
:2;
31 unsigned allowsTruncatedLabels
:1;
37 #define DEFAULT_WIDTH 40
38 #define DEFAULT_HEIGHT 40
41 static void destroyTabView(TabView
*tPtr
);
42 static void paintTabView(TabView
*tPtr
);
45 static void W_SetTabViewItemParent(WMTabViewItem
*item
, WMTabView
*parent
);
47 static void W_DrawLabel(WMTabViewItem
*item
, Drawable d
, WMRect rect
);
49 static void W_UnmapTabViewItem(WMTabViewItem
*item
);
51 static void W_MapTabViewItem(WMTabViewItem
*item
);
53 static WMView
*W_TabViewItemView(WMTabViewItem
*item
);
55 static void recalcTabWidth(TabView
*tPtr
);
59 handleEvents(XEvent
*event
, void *data
)
61 TabView
*tPtr
= (TabView
*)data
;
63 CHECK_CLASS(data
, WC_TabView
);
65 switch (event
->type
) {
67 if (event
->xexpose
.count
!=0)
74 WMTabViewItem
*item
= WMTabViewItemAtPoint(tPtr
,
78 WMSelectTabViewItem(tPtr
, item
);
92 WMCreateTabView(WMWidget
*parent
)
95 WMScreen
*scr
= WMWidgetScreen(parent
);
97 tPtr
= wmalloc(sizeof(TabView
));
98 memset(tPtr
, 0, sizeof(TabView
));
100 tPtr
->widgetClass
= WC_TabView
;
102 tPtr
->view
= W_CreateView(W_VIEW(parent
));
107 tPtr
->view
->self
= tPtr
;
109 tPtr
->lightGray
= WMCreateRGBColor(scr
, 0xd9d9, 0xd9d9, 0xd9d9, False
);
110 tPtr
->tabColor
= WMCreateRGBColor(scr
, 0x8420, 0x8420, 0x8420, False
);
112 tPtr
->font
= WMRetainFont(scr
->normalFont
);
114 WMCreateEventHandler(tPtr
->view
, ExposureMask
|StructureNotifyMask
115 |ButtonPressMask
, handleEvents
, tPtr
);
117 WMResizeWidget(tPtr
, DEFAULT_WIDTH
, DEFAULT_HEIGHT
);
119 tPtr
->tabHeight
= WMFontHeight(tPtr
->font
) + 3;
126 WMSetTabViewDelegate(WMTabView
*tPtr
, WMTabViewDelegate
*delegate
)
128 tPtr
->delegate
= delegate
;
133 WMAddItemInTabView(WMTabView
*tPtr
, WMTabViewItem
*item
)
135 WMInsertItemInTabView(tPtr
, tPtr
->itemCount
, item
);
140 WMInsertItemInTabView(WMTabView
*tPtr
, int index
, WMTabViewItem
*item
)
142 wassertr(W_TabViewItemView(item
) != NULL
);
144 if (tPtr
->maxItems
== tPtr
->itemCount
) {
145 WMTabViewItem
**items
;
147 items
= wrealloc(tPtr
->items
,
148 sizeof(WMTabViewItem
*) * (tPtr
->maxItems
+ 10));
150 wwarning("out of memory allocating memory for tabview");
153 memset(&items
[tPtr
->maxItems
], 0, sizeof(WMTabViewItem
*) * 10);
155 tPtr
->maxItems
+= 10;
158 if (index
> tPtr
->itemCount
)
159 index
= tPtr
->itemCount
;
161 if (index
== 0 && tPtr
->items
[0]) {
162 W_UnmapTabViewItem(tPtr
->items
[0]);
165 if (index
< tPtr
->itemCount
) {
166 memmove(&tPtr
->items
[index
+ 1], &tPtr
->items
[index
],
167 tPtr
->itemCount
- index
);
170 tPtr
->items
[index
] = item
;
174 recalcTabWidth(tPtr
);
176 W_SetTabViewItemParent(item
, tPtr
);
178 W_UnmapTabViewItem(item
);
180 W_ReparentView(W_TabViewItemView(item
), tPtr
->view
, 1,
181 tPtr
->tabHeight
+ 1);
183 W_ResizeView(W_TabViewItemView(item
), tPtr
->view
->size
.width
- 3,
184 tPtr
->view
->size
.height
- tPtr
->tabHeight
- 3);
187 W_MapTabViewItem(item
);
189 if (tPtr
->delegate
&& tPtr
->delegate
->didChangeNumberOfItems
)
190 (*tPtr
->delegate
->didChangeNumberOfItems
)(tPtr
->delegate
, tPtr
);
195 WMRemoveTabViewItem(WMTabView
*tPtr
, WMTabViewItem
*item
)
199 for (i
= 0; i
< tPtr
->itemCount
; i
++) {
200 if (tPtr
->items
[i
] == item
) {
201 if (i
< tPtr
->itemCount
- 1)
202 memmove(&tPtr
->items
[i
], &tPtr
->items
[i
+ 1],
203 tPtr
->itemCount
- i
- 1);
205 tPtr
->items
[i
] = NULL
;
207 W_SetTabViewItemParent(item
, NULL
);
213 if (tPtr
->delegate
&& tPtr
->delegate
->didChangeNumberOfItems
)
214 (*tPtr
->delegate
->didChangeNumberOfItems
)(tPtr
->delegate
, tPtr
);
220 isInside(int x
, int y
, int width
, int height
, int px
, int py
)
222 if (py
>= y
+ height
- 3 && py
<= y
+ height
223 && px
>= x
+ py
- (y
+ height
- 3)
224 && px
<= x
+ width
- (py
- (y
+ height
- 3))) {
228 if (py
>= y
+ 3 && py
< y
+ height
- 3
229 && px
>= x
+ 3 + ((y
+ 3) - py
)*3/7
230 && px
<= x
+ width
- 3 - ((y
+ 3) - py
)*3/7) {
234 if (py
>= y
&& py
< y
+ 3
235 && px
>= x
+ 7 + py
- y
236 && px
<= x
+ width
- 7 - (py
- y
)) {
245 WMTabViewItemAtPoint(WMTabView
*tPtr
, int x
, int y
)
249 i
= tPtr
->selectedItem
;
250 if (isInside(8 + (tPtr
->tabWidth
-10)*i
, 0, tPtr
->tabWidth
,
251 tPtr
->tabHeight
, x
, y
)) {
252 return tPtr
->items
[i
];
255 for (i
= 0; i
< tPtr
->itemCount
; i
++) {
256 if (isInside(8 + (tPtr
->tabWidth
-10)*i
, 0, tPtr
->tabWidth
,
257 tPtr
->tabHeight
, x
, y
)) {
258 return tPtr
->items
[i
];
266 WMSelectFirstTabViewItem(WMTabView
*tPtr
)
268 WMSelectTabViewItemAtIndex(tPtr
, 0);
273 WMSelectLastTabViewItem(WMTabView
*tPtr
)
275 WMSelectTabViewItemAtIndex(tPtr
, tPtr
->itemCount
);
280 WMSelectNextTabViewItem(WMTabView
*tPtr
)
282 WMSelectTabViewItemAtIndex(tPtr
, tPtr
->selectedItem
+ 1);
287 WMSelectPreviousTabViewItem(WMTabView
*tPtr
)
289 WMSelectTabViewItemAtIndex(tPtr
, tPtr
->selectedItem
- 1);
294 WMGetSelectedTabViewItem(WMTabView
*tPtr
)
296 return tPtr
->items
[tPtr
->selectedItem
];
301 WMSelectTabViewItem(WMTabView
*tPtr
, WMTabViewItem
*item
)
305 for (i
= 0; i
< tPtr
->itemCount
; i
++) {
306 if (tPtr
->items
[i
] == item
) {
307 WMSelectTabViewItemAtIndex(tPtr
, i
);
315 WMSelectTabViewItemAtIndex(WMTabView
*tPtr
, int index
)
319 if (index
== tPtr
->selectedItem
)
324 else if (index
>= tPtr
->itemCount
)
325 index
= tPtr
->itemCount
- 1;
327 item
= tPtr
->items
[tPtr
->selectedItem
];
329 if (tPtr
->delegate
&& tPtr
->delegate
->shouldSelectItem
)
330 if (!(*tPtr
->delegate
->shouldSelectItem
)(tPtr
->delegate
, tPtr
,
334 if (tPtr
->delegate
&& tPtr
->delegate
->willSelectItem
)
335 (*tPtr
->delegate
->willSelectItem
)(tPtr
->delegate
, tPtr
,
338 W_UnmapTabViewItem(item
);
341 item
= tPtr
->items
[index
];
343 W_MapTabViewItem(item
);
345 tPtr
->selectedItem
= index
;
347 if (tPtr
->delegate
&& tPtr
->delegate
->didSelectItem
)
348 (*tPtr
->delegate
->didSelectItem
)(tPtr
->delegate
, tPtr
,
354 recalcTabWidth(TabView
*tPtr
)
357 int twidth
= W_VIEW(tPtr
)->size
.width
;
361 for (i
= 0; i
< tPtr
->itemCount
; i
++) {
362 char *str
= WMGetTabViewItemLabel(tPtr
->items
[i
]);
365 width
= WMWidthOfString(tPtr
->font
, str
, strlen(str
));
366 if (width
> tPtr
->tabWidth
)
367 tPtr
->tabWidth
= width
;
370 tPtr
->tabWidth
+= 30;
371 if (tPtr
->tabWidth
> twidth
- 10 * tPtr
->itemCount
372 && tPtr
->tabWidth
< twidth
- 16)
373 tPtr
->tabWidth
= (twidth
- 16) / tPtr
->itemCount
;
378 drawRelief(W_Screen
*scr
, Drawable d
, int x
, int y
, unsigned int width
,
381 Display
*dpy
= scr
->display
;
382 GC bgc
= WMColorGC(scr
->black
);
383 GC wgc
= WMColorGC(scr
->white
);
384 GC dgc
= WMColorGC(scr
->darkGray
);
386 XDrawLine(dpy
, d
, wgc
, x
, y
, x
, y
+height
-1);
388 XDrawLine(dpy
, d
, bgc
, x
, y
+height
-1, x
+width
-1, y
+height
-1);
389 XDrawLine(dpy
, d
, dgc
, x
+1, y
+height
-2, x
+width
-2, y
+height
-2);
391 XDrawLine(dpy
, d
, bgc
, x
+width
-1, y
, x
+width
-1, y
+height
-1);
392 XDrawLine(dpy
, d
, dgc
, x
+width
-2, y
+1, x
+width
-2, y
+height
-2);
397 drawTab(TabView
*tPtr
, Drawable d
, int x
, int y
,
398 unsigned width
, unsigned height
, Bool selected
)
400 WMScreen
*scr
= W_VIEW(tPtr
)->screen
;
401 Display
*dpy
= scr
->display
;
402 GC white
= WMColorGC(selected
? scr
->white
: tPtr
->lightGray
);
403 GC black
= WMColorGC(scr
->black
);
404 GC dark
= WMColorGC(scr
->darkGray
);
405 GC light
= WMColorGC(scr
->gray
);
408 trap
[0].x
= x
+ (selected
? 0 : 1);
409 trap
[0].y
= y
+ height
- (selected
? 0 : 1);
412 trap
[1].y
= y
+ height
- 3;
414 trap
[2].x
= x
+ 10 - 3;
420 trap
[4].x
= x
+ width
- 10;
423 trap
[5].x
= x
+ width
- 10 + 3;
426 trap
[6].x
= x
+ width
- 3;
427 trap
[6].y
= y
+ height
- 3;
429 trap
[7].x
= x
+ width
- (selected
? 0 : 1);
430 trap
[7].y
= y
+ height
- (selected
? 0 : 1);
432 XFillPolygon(dpy
, d
, selected
? light
: WMColorGC(tPtr
->tabColor
), trap
, 8,
433 Convex
, CoordModeOrigin
);
435 XDrawLine(dpy
, d
, white
, trap
[0].x
, trap
[0].y
, trap
[1].x
, trap
[1].y
);
436 XDrawLine(dpy
, d
, white
, trap
[1].x
, trap
[1].y
, trap
[2].x
, trap
[2].y
);
437 XDrawLine(dpy
, d
, white
, trap
[2].x
, trap
[2].y
, trap
[3].x
, trap
[3].y
);
438 XDrawLine(dpy
, d
, white
, trap
[3].x
, trap
[3].y
, trap
[4].x
, trap
[4].y
);
439 XDrawLine(dpy
, d
, dark
, trap
[4].x
, trap
[4].y
, trap
[5].x
, trap
[5].y
);
440 XDrawLine(dpy
, d
, black
, trap
[5].x
, trap
[5].y
, trap
[6].x
, trap
[6].y
);
441 XDrawLine(dpy
, d
, black
, trap
[6].x
, trap
[6].y
, trap
[7].x
, trap
[7].y
);
443 XDrawLine(dpy
, d
, selected
? light
: WMColorGC(scr
->white
),
444 trap
[0].x
, trap
[0].y
, trap
[7].x
, trap
[7].y
);
449 paintTabView(TabView
*tPtr
)
452 WMScreen
*scr
= W_VIEW(tPtr
)->screen
;
453 Display
*dpy
= scr
->display
;
454 GC white
= WMColorGC(scr
->white
);
458 if (tPtr
->flags
.type
== WTTopTabsBevelBorder
) {
459 buffer
= XCreatePixmap(dpy
, W_VIEW(tPtr
)->window
,
460 W_VIEW(tPtr
)->size
.width
, tPtr
->tabHeight
,
461 W_VIEW(tPtr
)->screen
->depth
);
463 XFillRectangle(dpy
, buffer
, WMColorGC(W_VIEW(tPtr
)->backColor
),
464 0, 0, W_VIEW(tPtr
)->size
.width
, tPtr
->tabHeight
);
468 rect
.size
.width
= tPtr
->tabWidth
;
469 rect
.size
.height
= tPtr
->tabHeight
;
471 for (i
= tPtr
->itemCount
- 1; i
>= 0; i
--) {
472 if (i
!= tPtr
->selectedItem
) {
473 drawTab(tPtr
, buffer
, 8 + (rect
.size
.width
-10)*i
, 0,
474 rect
.size
.width
, rect
.size
.height
, False
);
477 drawTab(tPtr
, buffer
, 8 + (rect
.size
.width
-10) * tPtr
->selectedItem
,
478 0, rect
.size
.width
, rect
.size
.height
, True
);
480 XDrawLine(dpy
, buffer
, white
, 0, tPtr
->tabHeight
- 1,
481 8, tPtr
->tabHeight
- 1);
483 XDrawLine(dpy
, buffer
, white
,
484 8 + 10 + (rect
.size
.width
-10) * tPtr
->itemCount
,
485 tPtr
->tabHeight
- 1, W_VIEW(tPtr
)->size
.width
- 1,
486 tPtr
->tabHeight
- 1);
489 for (i
= 0; i
< tPtr
->itemCount
; i
++) {
490 W_DrawLabel(tPtr
->items
[i
], buffer
, rect
);
492 rect
.pos
.x
+= rect
.size
.width
- 10;
495 XCopyArea(dpy
, buffer
, W_VIEW(tPtr
)->window
, scr
->copyGC
, 0, 0,
496 W_VIEW(tPtr
)->size
.width
, tPtr
->tabHeight
, 0, 0);
498 XFreePixmap(dpy
, buffer
);
500 switch (tPtr
->flags
.type
) {
501 case WTTopTabsBevelBorder
:
502 drawRelief(scr
, W_VIEW(tPtr
)->window
, 0, tPtr
->tabHeight
- 1,
503 W_VIEW(tPtr
)->size
.width
,
504 W_VIEW(tPtr
)->size
.height
- tPtr
->tabHeight
+ 1);
507 case WTNoTabsBevelBorder
:
508 W_DrawRelief(scr
, W_VIEW(tPtr
)->window
, 0, 0, W_VIEW(tPtr
)->size
.width
,
509 W_VIEW(tPtr
)->size
.height
, WRRaised
);
512 case WTNoTabsLineBorder
:
513 W_DrawRelief(scr
, W_VIEW(tPtr
)->window
, 0, 0, W_VIEW(tPtr
)->size
.width
,
514 W_VIEW(tPtr
)->size
.height
, WRSimple
);
517 case WTNoTabsNoBorder
:
524 destroyTabView(TabView
*tPtr
)
528 for (i
= 0; i
< tPtr
->itemCount
; i
++) {
529 WMSetTabViewItemView(tPtr
->items
[i
], NULL
);
530 WMDestroyTabViewItem(tPtr
->items
[i
]);
534 WMReleaseColor(tPtr
->lightGray
);
535 WMReleaseColor(tPtr
->tabColor
);
536 WMReleaseFont(tPtr
->font
);
541 /******************************************************************/
544 typedef struct W_TabViewItem
{
560 W_SetTabViewItemParent(WMTabViewItem
*item
, WMTabView
*parent
)
562 item
->tabView
= parent
;
567 W_DrawLabel(WMTabViewItem
*item
, Drawable d
, WMRect rect
)
569 WMScreen
*scr
= W_VIEW(item
->tabView
)->screen
;
574 WMDrawString(scr
, d
, WMColorGC(scr
->black
), item
->tabView
->font
,
575 rect
.pos
.x
, rect
.pos
.y
, item
->label
, strlen(item
->label
));
580 W_UnmapTabViewItem(WMTabViewItem
*item
)
582 wassertr(item
->view
);
584 W_UnmapView(item
->view
);
586 item
->flags
.visible
= 0;
591 W_MapTabViewItem(WMTabViewItem
*item
)
593 wassertr(item
->view
);
595 W_MapView(item
->view
);
597 item
->flags
.visible
= 1;
602 W_TabViewItemView(WMTabViewItem
*item
)
609 WMCreateTabViewItemWithIdentifier(int identifier
)
613 item
= wmalloc(sizeof(WMTabViewItem
));
614 memset(item
, 0, sizeof(WMTabViewItem
));
616 item
->identifier
= identifier
;
623 WMGetTabViewItemIdentifier(WMTabViewItem
*item
)
625 return item
->identifier
;
630 WMSetTabViewFont(WMTabView
*tPtr
, WMFont
*font
)
633 WMReleaseFont(tPtr
->font
);
635 tPtr
->font
= WMRetainFont(font
);
636 tPtr
->tabHeight
= WMFontHeight(tPtr
->font
) + 3;
637 recalcTabWidth(tPtr
);
642 WMSetTabViewItemLabel(WMTabViewItem
*item
, char *label
)
647 item
->label
= wstrdup(label
);
650 recalcTabWidth(item
->tabView
);
655 WMGetTabViewItemLabel(WMTabViewItem
*item
)
662 WMSetTabViewItemView(WMTabViewItem
*item
, WMView
*view
)
669 WMGetTabViewItemView(WMTabViewItem
*item
)
676 WMDestroyTabViewItem(WMTabViewItem
*item
)
682 W_DestroyView(item
->view
);