Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / wtabview.c
blob3ae4728321d241bb5de107d0f692b1c605e5588d
2 #include "WINGsP.h"
4 typedef struct W_TabView {
5 W_Class widgetClass;
6 W_View *view;
8 struct W_TabViewItem **items;
9 int itemCount;
10 int maxItems; /* size of items array, can be increased */
12 int selectedItem;
13 int firstVisible;
15 int visibleTabs;
17 WMFont *font;
19 WMColor *lightGray;
20 WMColor *tabColor;
22 WMTabViewDelegate *delegate;
24 short tabHeight;
26 struct {
27 WMReliefType relief:4;
28 WMTitlePosition titlePosition:4;
29 WMTabViewType type:2;
31 unsigned enabled:1;
32 unsigned tabbed:1;
33 unsigned dontFitAll:1;
34 unsigned bordered:1;
35 unsigned uniformTabs:1;
36 } flags;
37 } TabView;
39 typedef struct W_TabViewItem {
40 WMTabView *tabView;
42 W_View *view;
44 char *label;
46 short tabWidth;
47 int identifier;
49 struct {
50 unsigned visible:1;
51 unsigned enabled:1;
52 } flags;
53 } W_TabViewItem;
55 #define DEFAULT_WIDTH 40
56 #define DEFAULT_HEIGHT 40
58 #define NORMAL_SIDE_OFFSET 8
59 #define BUTTONED_SIDE_OFFSET 15
61 static void destroyTabView(TabView * tPtr);
62 static void paintTabView(TabView * tPtr);
64 static void W_SetTabViewItemParent(WMTabViewItem * item, WMTabView * parent);
66 static void W_DrawLabel(WMTabViewItem * item, Drawable d, WMRect rect, Bool enabled);
68 static void W_UnmapTabViewItem(WMTabViewItem * item);
70 static void W_MapTabViewItem(WMTabViewItem * item);
72 static WMView *W_TabViewItemView(WMTabViewItem * item);
74 static int W_TabViewItemTabWidth(WMTabViewItem * item);
76 static void W_SetTabViewItemTabWidth(WMTabViewItem * item, int width);
78 static void recalcTabWidth(TabView * tPtr);
79 static void rearrange(TabView * tPtr);
81 static void didResize(struct W_ViewDelegate *, WMView *);
83 static W_ViewDelegate delegate = {
84 NULL,
85 NULL,
86 didResize,
87 NULL,
88 NULL
89 };
91 static int positionOfTab(WMTabView * tabView, int tab)
92 {
93 int i;
94 int offs;
96 if (tab < tabView->firstVisible)
97 return -1;
99 if (tab > tabView->firstVisible + tabView->visibleTabs)
100 return -1;
102 if (tabView->flags.dontFitAll)
103 offs = BUTTONED_SIDE_OFFSET;
104 else
105 offs = NORMAL_SIDE_OFFSET;
107 for (i = tabView->firstVisible; i < tab; i++)
108 offs += W_TabViewItemTabWidth(tabView->items[i]) - 10;
110 return offs;
113 static int countVisibleTabs(TabView * tPtr, int first)
115 int i;
116 int width;
118 if (first < 0) {
119 width = W_VIEW_WIDTH(tPtr->view) - 2 * NORMAL_SIDE_OFFSET;
120 first = 0;
121 } else {
122 width = W_VIEW_WIDTH(tPtr->view) - 2 * BUTTONED_SIDE_OFFSET;
125 for (i = first; i < tPtr->itemCount; i++) {
126 width -= W_TabViewItemTabWidth(tPtr->items[i]) - 10;
127 if (width <= 0) {
128 return i - first;
131 return i - first;
134 static void handleEvents(XEvent * event, void *data)
136 TabView *tPtr = (TabView *) data;
138 CHECK_CLASS(data, WC_TabView);
140 switch (event->type) {
141 case Expose:
142 if (event->xexpose.count != 0)
143 break;
144 paintTabView(tPtr);
145 break;
147 case ButtonPress:
148 if (tPtr->flags.enabled) {
149 WMTabViewItem *item = WMTabViewItemAtPoint(tPtr,
150 event->xbutton.x,
151 event->xbutton.y);
152 /*if (item && !item->flags.enabled)
153 break; */
155 if (item && item->flags.enabled) {
156 WMSelectTabViewItem(tPtr, item);
157 } else if (tPtr->flags.dontFitAll) {
158 int redraw = 0;
159 int lastVisible = tPtr->firstVisible + tPtr->visibleTabs - 1;
161 if (event->xbutton.x < BUTTONED_SIDE_OFFSET) {
162 if (tPtr->firstVisible > 0) {
163 redraw = 1;
164 tPtr->firstVisible--;
166 } else if (event->xbutton.x > positionOfTab(tPtr, lastVisible)) {
168 if (lastVisible < tPtr->itemCount - 1) {
169 redraw = 1;
170 tPtr->firstVisible++;
173 tPtr->visibleTabs = countVisibleTabs(tPtr, tPtr->firstVisible);
174 if (redraw) {
175 paintTabView(tPtr);
179 break;
181 case DestroyNotify:
182 destroyTabView(tPtr);
183 break;
187 WMTabView *WMCreateTabView(WMWidget * parent)
189 TabView *tPtr;
190 WMScreen *scr = WMWidgetScreen(parent);
192 tPtr = wmalloc(sizeof(TabView));
193 memset(tPtr, 0, sizeof(TabView));
195 tPtr->widgetClass = WC_TabView;
197 tPtr->view = W_CreateView(W_VIEW(parent));
198 if (!tPtr->view) {
199 wfree(tPtr);
200 return NULL;
202 tPtr->view->self = tPtr;
203 tPtr->view->delegate = &delegate;
205 tPtr->lightGray = WMCreateRGBColor(scr, 0xd9d9, 0xd9d9, 0xd9d9, False);
206 tPtr->tabColor = WMCreateRGBColor(scr, 0x8420, 0x8420, 0x8420, False);
208 tPtr->font = WMRetainFont(scr->normalFont);
210 tPtr->flags.type = WTTopTabsBevelBorder;
211 tPtr->flags.bordered = 1;
212 tPtr->flags.uniformTabs = 0;
213 tPtr->flags.enabled = 1;
215 WMCreateEventHandler(tPtr->view, ExposureMask | StructureNotifyMask | ButtonPressMask, handleEvents, tPtr);
217 WMResizeWidget(tPtr, DEFAULT_WIDTH, DEFAULT_HEIGHT);
219 tPtr->tabHeight = WMFontHeight(tPtr->font) + 3;
221 return tPtr;
224 void WMSetTabViewDelegate(WMTabView * tPtr, WMTabViewDelegate * delegate)
226 tPtr->delegate = delegate;
229 WMTabViewItem *WMAddTabViewItemWithView(WMTabView * tPtr, WMView * view, int identifier, char *label)
231 WMTabViewItem *item;
233 item = WMCreateTabViewItemWithIdentifier(identifier);
234 WMSetTabViewItemView(item, view);
235 WMAddItemInTabView(tPtr, item);
236 WMSetTabViewItemLabel(item, label);
238 return item;
241 void WMAddItemInTabView(WMTabView * tPtr, WMTabViewItem * item)
243 WMInsertItemInTabView(tPtr, tPtr->itemCount, item);
246 void WMSetTabViewEnabled(WMTabView * tPtr, Bool flag)
248 tPtr->flags.enabled = ((flag == 0) ? 0 : 1);
249 if (W_VIEW_REALIZED(tPtr->view))
250 paintTabView(tPtr);
253 void WMInsertItemInTabView(WMTabView * tPtr, int index, WMTabViewItem * item)
255 wassertr(W_TabViewItemView(item) != NULL);
257 if (tPtr->maxItems == tPtr->itemCount) {
258 WMTabViewItem **items;
260 items = wrealloc(tPtr->items, sizeof(WMTabViewItem *) * (tPtr->maxItems + 10));
261 memset(&items[tPtr->maxItems], 0, sizeof(WMTabViewItem *) * 10);
262 tPtr->items = items;
263 tPtr->maxItems += 10;
266 if (index > tPtr->itemCount)
267 index = tPtr->itemCount;
269 if (index == 0 && tPtr->items[0]) {
270 W_UnmapTabViewItem(tPtr->items[0]);
273 if (index < tPtr->itemCount) {
274 memmove(tPtr->items + index + 1, tPtr->items + index,
275 (tPtr->itemCount - index) * sizeof(WMTabViewItem *));
278 tPtr->items[index] = item;
280 tPtr->itemCount++;
282 recalcTabWidth(tPtr);
284 W_SetTabViewItemParent(item, tPtr);
286 W_UnmapTabViewItem(item);
288 if (tPtr->flags.bordered) {
289 W_ReparentView(W_TabViewItemView(item), tPtr->view, 1, tPtr->tabHeight + 1);
291 W_ResizeView(W_TabViewItemView(item), tPtr->view->size.width - 3,
292 tPtr->view->size.height - tPtr->tabHeight - 3);
293 } else {
294 W_ReparentView(W_TabViewItemView(item), tPtr->view, 0, tPtr->tabHeight);
296 W_ResizeView(W_TabViewItemView(item), tPtr->view->size.width,
297 tPtr->view->size.height - tPtr->tabHeight);
300 if (index == 0) {
301 W_MapTabViewItem(item);
303 if (tPtr->delegate && tPtr->delegate->didChangeNumberOfItems)
304 (*tPtr->delegate->didChangeNumberOfItems) (tPtr->delegate, tPtr);
306 if (W_VIEW_REALIZED(tPtr->view))
307 paintTabView(tPtr);
310 void WMRemoveTabViewItem(WMTabView * tPtr, WMTabViewItem * item)
312 int i;
314 for (i = 0; i < tPtr->itemCount; i++) {
315 if (tPtr->items[i] == item) {
316 if (i < tPtr->itemCount - 1)
317 memmove(&tPtr->items[i], &tPtr->items[i + 1], tPtr->itemCount - i - 1);
318 else
319 tPtr->items[i] = NULL;
321 W_SetTabViewItemParent(item, NULL);
323 tPtr->itemCount--;
324 break;
327 if (tPtr->delegate && tPtr->delegate->didChangeNumberOfItems)
328 (*tPtr->delegate->didChangeNumberOfItems) (tPtr->delegate, tPtr);
331 static Bool isInside(int x, int y, int width, int height, int px, int py)
333 if (py >= y + height - 3 && py <= y + height && px >= x + py - (y + height - 3)
334 && px <= x + width - (py - (y + height - 3))) {
336 return True;
338 if (py >= y + 3 && py < y + height - 3
339 && px >= x + 3 + ((y + 3) - py) * 3 / 7 && px <= x + width - 3 - ((y + 3) - py) * 3 / 7) {
341 return True;
343 if (py >= y && py < y + 3 && px >= x + 7 + py - y && px <= x + width - 7 - (py - y)) {
345 return True;
347 return False;
350 WMTabViewItem *WMTabViewItemAtPoint(WMTabView * tPtr, int x, int y)
352 int i;
353 int count = tPtr->visibleTabs;
354 int first = tPtr->firstVisible;
356 if (tPtr->flags.dontFitAll) {
357 i = tPtr->selectedItem - tPtr->firstVisible;
358 if (i >= 0 && i < tPtr->visibleTabs
359 && isInside(positionOfTab(tPtr, tPtr->selectedItem), 0,
360 W_TabViewItemTabWidth(tPtr->items[tPtr->selectedItem]), tPtr->tabHeight, x, y)) {
361 return tPtr->items[tPtr->selectedItem];
363 } else {
364 i = tPtr->selectedItem;
365 if (isInside(positionOfTab(tPtr, i), 0,
366 W_TabViewItemTabWidth(tPtr->items[i]), tPtr->tabHeight, x, y)) {
367 return tPtr->items[i];
371 for (i = first; i < first + count; i++) {
372 int pos;
374 pos = positionOfTab(tPtr, i);
375 if (isInside(pos, 0, W_TabViewItemTabWidth(tPtr->items[i]), tPtr->tabHeight, x, y)) {
376 return tPtr->items[i];
379 return NULL;
382 void WMSetTabViewType(WMTabView * tPtr, WMTabViewType type)
384 tPtr->flags.type = type;
386 if (type != WTTopTabsBevelBorder)
387 tPtr->tabHeight = 0;
388 else
389 tPtr->tabHeight = WMFontHeight(tPtr->font) + 3;
391 if (type == WTNoTabsNoBorder)
392 tPtr->flags.bordered = 0;
393 else
394 tPtr->flags.bordered = 1;
396 rearrange(tPtr);
399 void WMSelectFirstTabViewItem(WMTabView * tPtr)
401 WMSelectTabViewItemAtIndex(tPtr, 0);
404 void WMSelectLastTabViewItem(WMTabView * tPtr)
406 WMSelectTabViewItemAtIndex(tPtr, tPtr->itemCount);
409 void WMSelectNextTabViewItem(WMTabView * tPtr)
411 WMSelectTabViewItemAtIndex(tPtr, tPtr->selectedItem + 1);
414 void WMSelectPreviousTabViewItem(WMTabView * tPtr)
416 WMSelectTabViewItemAtIndex(tPtr, tPtr->selectedItem - 1);
419 WMTabViewItem *WMGetSelectedTabViewItem(WMTabView * tPtr)
421 return tPtr->items[tPtr->selectedItem];
424 void WMSelectTabViewItem(WMTabView * tPtr, WMTabViewItem * item)
426 int i;
428 for (i = 0; i < tPtr->itemCount; i++) {
429 if (tPtr->items[i] == item) {
430 WMSelectTabViewItemAtIndex(tPtr, i);
431 break;
436 void WMSelectTabViewItemAtIndex(WMTabView * tPtr, int index)
438 WMTabViewItem *item;
440 if (index == tPtr->selectedItem) {
441 return;
444 if (index < 0)
445 index = 0;
446 else if (index >= tPtr->itemCount)
447 index = tPtr->itemCount - 1;
449 item = tPtr->items[tPtr->selectedItem];
451 if (tPtr->delegate && tPtr->delegate->shouldSelectItem)
452 if (!(*tPtr->delegate->shouldSelectItem) (tPtr->delegate, tPtr, tPtr->items[index]))
453 return;
455 if (tPtr->delegate && tPtr->delegate->willSelectItem)
456 (*tPtr->delegate->willSelectItem) (tPtr->delegate, tPtr, tPtr->items[index]);
458 W_UnmapTabViewItem(item);
460 item = tPtr->items[index];
462 W_MapTabViewItem(item);
464 tPtr->selectedItem = index;
466 if (tPtr->delegate && tPtr->delegate->didSelectItem)
467 (*tPtr->delegate->didSelectItem) (tPtr->delegate, tPtr, tPtr->items[index]);
469 paintTabView(tPtr);
472 static void recalcTabWidth(TabView * tPtr)
474 int i;
475 /*int twidth = W_VIEW(tPtr)->size.width; */
476 int width;
478 if (tPtr->flags.uniformTabs) {
479 int tabWidth;
481 tabWidth = 0;
483 for (i = 0; i < tPtr->itemCount; i++) {
484 char *str = WMGetTabViewItemLabel(tPtr->items[i]);
486 if (str) {
487 width = WMWidthOfString(tPtr->font, str, strlen(str));
488 if (width > tabWidth)
489 tabWidth = width;
493 tabWidth = tabWidth + 30;
495 for (i = 0; i < tPtr->itemCount; i++)
496 W_SetTabViewItemTabWidth(tPtr->items[i], tabWidth);
498 tPtr->firstVisible = 0;
499 tPtr->visibleTabs = countVisibleTabs(tPtr, -1);
500 if (tPtr->visibleTabs < tPtr->itemCount)
501 tPtr->flags.dontFitAll = 1;
502 else
503 tPtr->flags.dontFitAll = 0;
504 } else {
505 for (i = 0; i < tPtr->itemCount; i++) {
506 char *str = WMGetTabViewItemLabel(tPtr->items[i]);
507 if (!str)
508 continue;
510 width = WMWidthOfString(tPtr->font, str, strlen(str)) + 30;
512 W_SetTabViewItemTabWidth(tPtr->items[i], width);
515 if (countVisibleTabs(tPtr, -1) < tPtr->itemCount) {
516 tPtr->flags.dontFitAll = 1;
517 tPtr->firstVisible = 0;
518 tPtr->visibleTabs = countVisibleTabs(tPtr, tPtr->firstVisible);
519 } else {
520 tPtr->flags.dontFitAll = 0;
521 tPtr->firstVisible = 0;
522 tPtr->visibleTabs = tPtr->itemCount;
527 static void drawRelief(W_Screen * scr, Drawable d, int x, int y, unsigned int width, unsigned int height)
529 Display *dpy = scr->display;
530 GC bgc = WMColorGC(scr->black);
531 GC wgc = WMColorGC(scr->white);
532 GC dgc = WMColorGC(scr->darkGray);
534 XDrawLine(dpy, d, wgc, x, y, x, y + height - 1);
536 XDrawLine(dpy, d, bgc, x, y + height - 1, x + width - 1, y + height - 1);
537 XDrawLine(dpy, d, dgc, x + 1, y + height - 2, x + width - 2, y + height - 2);
539 XDrawLine(dpy, d, bgc, x + width - 1, y, x + width - 1, y + height - 1);
540 XDrawLine(dpy, d, dgc, x + width - 2, y + 1, x + width - 2, y + height - 2);
543 static void drawTab(TabView * tPtr, Drawable d, int x, int y, unsigned width, unsigned height, Bool selected)
545 WMScreen *scr = W_VIEW(tPtr)->screen;
546 Display *dpy = scr->display;
547 GC white = WMColorGC(selected ? scr->white : tPtr->lightGray);
548 GC black = WMColorGC(scr->black);
549 GC dark = WMColorGC(scr->darkGray);
550 GC light = WMColorGC(scr->gray);
551 XPoint trap[8];
553 trap[0].x = x + (selected ? 0 : 1);
554 trap[0].y = y + height - (selected ? 0 : 1);
556 trap[1].x = x + 3;
557 trap[1].y = y + height - 3;
559 trap[2].x = x + 10 - 3;
560 trap[2].y = y + 3;
562 trap[3].x = x + 10;
563 trap[3].y = y;
565 trap[4].x = x + width - 10;
566 trap[4].y = y;
568 trap[5].x = x + width - 10 + 3;
569 trap[5].y = y + 3;
571 trap[6].x = x + width - 3;
572 trap[6].y = y + height - 3;
574 trap[7].x = x + width - (selected ? 0 : 1);
575 trap[7].y = y + height - (selected ? 0 : 1);
577 XFillPolygon(dpy, d, selected ? light : WMColorGC(tPtr->tabColor), trap, 8, Convex, CoordModeOrigin);
579 XDrawLine(dpy, d, white, trap[0].x, trap[0].y, trap[1].x, trap[1].y);
580 XDrawLine(dpy, d, white, trap[1].x, trap[1].y, trap[2].x, trap[2].y);
581 XDrawLine(dpy, d, white, trap[2].x, trap[2].y, trap[3].x, trap[3].y);
582 XDrawLine(dpy, d, white, trap[3].x, trap[3].y, trap[4].x, trap[4].y);
583 XDrawLine(dpy, d, dark, trap[4].x, trap[4].y, trap[5].x, trap[5].y);
584 XDrawLine(dpy, d, black, trap[5].x, trap[5].y, trap[6].x, trap[6].y);
585 XDrawLine(dpy, d, black, trap[6].x, trap[6].y, trap[7].x, trap[7].y);
587 XDrawLine(dpy, d, selected ? light : WMColorGC(scr->white), trap[0].x, trap[0].y, trap[7].x, trap[7].y);
590 static void paintDot(TabView * tPtr, Drawable d, int x, int y)
592 WMScreen *scr = W_VIEW(tPtr)->screen;
593 Display *dpy = scr->display;
594 GC white = WMColorGC(scr->white);
595 GC black = WMColorGC(scr->black);
597 XFillRectangle(dpy, d, black, x, y, 2, 2);
598 XDrawPoint(dpy, d, white, x, y);
601 static void paintTabView(TabView * tPtr)
603 Pixmap buffer;
604 WMScreen *scr = W_VIEW(tPtr)->screen;
605 Display *dpy = scr->display;
606 GC white = WMColorGC(scr->white);
607 int i;
609 if (tPtr->flags.type == WTTopTabsBevelBorder) {
610 int count = tPtr->visibleTabs;
611 int first = tPtr->firstVisible;
612 int moreAtLeft;
613 int moreAtRight;
614 int selectedIsVisible;
615 int ty;
616 int twidth, theight;
618 ty = 2;
619 theight = tPtr->tabHeight;
621 buffer = XCreatePixmap(dpy, W_VIEW(tPtr)->window,
622 W_VIEW(tPtr)->size.width, theight, W_VIEW(tPtr)->screen->depth);
624 XFillRectangle(dpy, buffer, WMColorGC(W_VIEW(tPtr)->backColor),
625 0, 0, W_VIEW(tPtr)->size.width, tPtr->tabHeight);
627 if (tPtr->flags.dontFitAll) {
628 moreAtLeft = first > 0;
629 moreAtRight = (first + count) < tPtr->itemCount;
630 if (tPtr->selectedItem >= first && tPtr->selectedItem < first + count)
631 selectedIsVisible = 1;
632 else
633 selectedIsVisible = 0;
634 } else {
635 moreAtLeft = 0;
636 moreAtRight = 0;
637 selectedIsVisible = 1;
640 if (moreAtRight) {
641 drawTab(tPtr, buffer, positionOfTab(tPtr, first + count), 0,
642 W_VIEW_WIDTH(tPtr->view), theight, False);
644 for (i = first + count - 1; i >= first; i--) {
645 if (!selectedIsVisible || i != tPtr->selectedItem) {
646 twidth = W_TabViewItemTabWidth(tPtr->items[i]);
648 drawTab(tPtr, buffer, positionOfTab(tPtr, i), 0, twidth, theight, False);
651 if (moreAtLeft) {
652 drawTab(tPtr, buffer, positionOfTab(tPtr, 0) - 2 * BUTTONED_SIDE_OFFSET,
653 0, BUTTONED_SIDE_OFFSET * 4, theight, False);
656 if (selectedIsVisible) {
657 int idx = tPtr->selectedItem;
659 drawTab(tPtr, buffer, positionOfTab(tPtr, idx),
660 0, W_TabViewItemTabWidth(tPtr->items[idx]), theight, True);
662 XDrawLine(dpy, buffer, white, 0, theight - 1, positionOfTab(tPtr, idx), theight - 1);
664 XDrawLine(dpy, buffer, white,
665 positionOfTab(tPtr, idx) + W_TabViewItemTabWidth(tPtr->items[idx]),
666 tPtr->tabHeight - 1, W_VIEW_WIDTH(tPtr->view) - 1, tPtr->tabHeight - 1);
667 } else {
668 XDrawLine(dpy, buffer, white, 0, theight - 1, W_VIEW_WIDTH(tPtr->view), theight - 1);
671 for (i = 0; i < count; i++) {
672 WMRect rect;
674 rect.pos.x = 15 + positionOfTab(tPtr, first + i);
675 rect.pos.y = ty;
676 rect.size.width = W_TabViewItemTabWidth(tPtr->items[first + i]);
677 rect.size.height = theight;
678 W_DrawLabel(tPtr->items[first + i], buffer, rect,
679 tPtr->flags.enabled && tPtr->items[first + i]->flags.enabled);
682 if (moreAtLeft) {
683 paintDot(tPtr, buffer, 4, 10);
684 paintDot(tPtr, buffer, 7, 10);
685 paintDot(tPtr, buffer, 10, 10);
687 if (moreAtRight) {
688 int x;
690 x = positionOfTab(tPtr, tPtr->firstVisible + tPtr->visibleTabs);
692 x = x + (W_VIEW_WIDTH(tPtr->view) - x) / 2;
693 paintDot(tPtr, buffer, x + 5, 10);
694 paintDot(tPtr, buffer, x + 8, 10);
695 paintDot(tPtr, buffer, x + 11, 10);
698 XCopyArea(dpy, buffer, W_VIEW(tPtr)->window, scr->copyGC, 0, 0,
699 W_VIEW_WIDTH(tPtr->view), theight, 0, 0);
701 XFreePixmap(dpy, buffer);
703 switch (tPtr->flags.type) {
704 case WTTopTabsBevelBorder:
705 drawRelief(scr, W_VIEW(tPtr)->window, 0, tPtr->tabHeight - 1,
706 W_VIEW(tPtr)->size.width, W_VIEW(tPtr)->size.height - tPtr->tabHeight + 1);
707 break;
709 case WTNoTabsBevelBorder:
710 W_DrawRelief(scr, W_VIEW(tPtr)->window, 0, 0, W_VIEW(tPtr)->size.width,
711 W_VIEW(tPtr)->size.height, WRRaised);
712 break;
714 case WTNoTabsLineBorder:
715 W_DrawRelief(scr, W_VIEW(tPtr)->window, 0, 0, W_VIEW(tPtr)->size.width,
716 W_VIEW(tPtr)->size.height, WRSimple);
717 break;
719 case WTNoTabsNoBorder:
720 break;
724 static void rearrange(TabView * tPtr)
726 int i;
727 int width, height;
728 int bordered = tPtr->flags.bordered;
730 recalcTabWidth(tPtr);
732 width = tPtr->view->size.width - (bordered ? 3 : 0);
733 height = tPtr->view->size.height - tPtr->tabHeight - (bordered ? 3 : 0);
735 for (i = 0; i < tPtr->itemCount; i++) {
736 W_MoveView(W_TabViewItemView(tPtr->items[i]), 1 * bordered, tPtr->tabHeight + 1 * bordered);
737 W_ResizeView(W_TabViewItemView(tPtr->items[i]), width, height);
739 if (W_VIEW_MAPPED(tPtr->view) && W_VIEW_REALIZED(tPtr->view))
740 paintTabView(tPtr);
743 static void didResize(struct W_ViewDelegate *deleg, WMView * view)
745 rearrange(view->self);
748 static void destroyTabView(TabView * tPtr)
750 int i;
752 for (i = 0; i < tPtr->itemCount; i++) {
753 WMSetTabViewItemView(tPtr->items[i], NULL);
754 WMDestroyTabViewItem(tPtr->items[i]);
756 wfree(tPtr->items);
758 WMReleaseColor(tPtr->lightGray);
759 WMReleaseColor(tPtr->tabColor);
760 WMReleaseFont(tPtr->font);
762 wfree(tPtr);
765 /******************************************************************/
767 static void W_SetTabViewItemParent(WMTabViewItem * item, WMTabView * parent)
769 item->tabView = parent;
772 static void W_DrawLabel(WMTabViewItem * item, Drawable d, WMRect rect, Bool enabled)
774 WMScreen *scr = W_VIEW(item->tabView)->screen;
776 if (!item->label)
777 return;
779 WMDrawString(scr, d, enabled ? scr->black : scr->darkGray,
780 item->tabView->font, rect.pos.x, rect.pos.y, item->label, strlen(item->label));
783 static void W_UnmapTabViewItem(WMTabViewItem * item)
785 wassertr(item->view);
787 W_UnmapView(item->view);
789 item->flags.visible = 0;
792 static void W_MapTabViewItem(WMTabViewItem * item)
794 wassertr(item->view);
796 W_MapView(item->view);
797 W_RaiseView(item->view);
799 item->flags.visible = 1;
802 static WMView *W_TabViewItemView(WMTabViewItem * item)
804 return item->view;
807 static int W_TabViewItemTabWidth(WMTabViewItem * item)
809 return item->tabWidth;
812 static void W_SetTabViewItemTabWidth(WMTabViewItem * item, int width)
814 item->tabWidth = width;
817 WMTabViewItem *WMCreateTabViewItemWithIdentifier(int identifier)
819 WMTabViewItem *item;
821 item = wmalloc(sizeof(WMTabViewItem));
822 memset(item, 0, sizeof(WMTabViewItem));
824 item->identifier = identifier;
826 item->flags.enabled = 1;
828 return item;
831 WMTabViewItem *WMCreateTabViewItem(int identifier, char *label)
833 WMTabViewItem *item;
835 item = wmalloc(sizeof(WMTabViewItem));
836 memset(item, 0, sizeof(WMTabViewItem));
838 item->identifier = identifier;
840 item->flags.enabled = 1;
842 WMSetTabViewItemLabel(item, label);
844 return item;
847 void WMSetTabViewItemEnabled(WMTabViewItem * tPtr, Bool flag)
849 tPtr->flags.enabled = ((flag == 0) ? 0 : 1);
850 if (tPtr->tabView && W_VIEW_REALIZED(tPtr->tabView->view))
851 paintTabView(tPtr->tabView);
854 int WMGetTabViewItemIdentifier(WMTabViewItem * item)
856 return item->identifier;
859 void WMSetTabViewFont(WMTabView * tPtr, WMFont * font)
861 if (tPtr->font)
862 WMReleaseFont(tPtr->font);
864 tPtr->font = WMRetainFont(font);
865 tPtr->tabHeight = WMFontHeight(tPtr->font) + 3;
866 recalcTabWidth(tPtr);
869 void WMSetTabViewItemLabel(WMTabViewItem * item, char *label)
871 if (item->label)
872 wfree(item->label);
874 if (label)
875 item->label = wstrdup(label);
876 else
877 item->label = NULL;
879 if (item->tabView)
880 recalcTabWidth(item->tabView);
883 char *WMGetTabViewItemLabel(WMTabViewItem * item)
885 return item->label;
888 void WMSetTabViewItemView(WMTabViewItem * item, WMView * view)
890 item->view = view;
893 WMView *WMGetTabViewItemView(WMTabViewItem * item)
895 return item->view;
898 void WMDestroyTabViewItem(WMTabViewItem * item)
900 if (item->label)
901 wfree(item->label);
903 if (item->view)
904 W_DestroyView(item->view);
906 wfree(item);