1 /*
2  * Author:      William Chia-Wei Cheng (bill.cheng@acm.org)
3  *
4  * Copyright (C) 2001-2009, William Chia-Wei Cheng.
5  *
6  * This file may be distributed under the terms of the Q Public License
7  * as defined by Trolltech AS of Norway and appearing in the file
8  * LICENSE.QPL included in the packaging of this file.
9  *
10  * THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING
11  * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
12  * PURPOSE.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
13  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
14  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
16  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  * @(#)$Header: /mm2/home/cvs/bc-src/tgif/button.c,v 1.6 2011/05/16 16:21:56 william Exp $
19  */
20 
21 #define _INCLUDE_FROM_BUTTON_C_
22 
23 #include "tgifdefs.h"
24 
25 #include "auxtext.e"
26 #include "box.e"
27 #include "button.e"
28 #include "cursor.e"
29 #include "file.e"
30 #include "font.e"
31 #include "mainloop.e"
32 #include "mainmenu.e"
33 #include "menu.e"
34 #include "msg.e"
35 #include "raster.e"
36 #include "rect.e"
37 #include "setup.e"
38 
39 #define BTN_DEFAULT_FONT         0
40 #define BTN_MSG_FONT             1
41 #define BTN_BOLD_MSG_FONT        2
42 #define BTN_ITALIC_MSG_FONT      3
43 #define BTN_BOLD_ITALIC_MSG_FONT 4
44 
45 static
GetButtonFontStyle(pButtonFontInfo)46 int GetButtonFontStyle(pButtonFontInfo)
47    ButtonFontInfo *pButtonFontInfo;
48 {
49    int font_style=INVALID;
50 
51    if (pButtonFontInfo == NULL) {
52       if (boldMsgFontSet == NULL && boldMsgFontPtr == NULL) {
53          font_style = BTN_DEFAULT_FONT;
54       } else {
55          font_style = BTN_BOLD_MSG_FONT;
56       }
57    } else {
58       switch (pButtonFontInfo->font_style) {
59       case STYLE_NR:
60          font_style = BTN_MSG_FONT;
61          break;
62       case STYLE_BR:
63          if (boldMsgFontSet == NULL && boldMsgFontPtr == NULL) {
64             font_style = BTN_MSG_FONT;
65          } else {
66             font_style = BTN_BOLD_MSG_FONT;
67          }
68          break;
69       case STYLE_NI:
70          if (italicMsgFontSet == NULL && italicMsgFontPtr == NULL) {
71             font_style = BTN_MSG_FONT;
72          } else {
73             font_style = BTN_ITALIC_MSG_FONT;
74          }
75          break;
76       case STYLE_BI:
77          if (boldItalicMsgFontSet == NULL && boldItalicMsgFontPtr == NULL) {
78             font_style = BTN_MSG_FONT;
79          } else {
80             font_style = BTN_BOLD_ITALIC_MSG_FONT;
81          }
82          break;
83       default: break;
84       }
85    }
86    return font_style;
87 }
88 
ButtonWidth(Str,MinLen,pButtonFontInfo)89 int ButtonWidth(Str, MinLen, pButtonFontInfo)
90    char *Str;
91    int MinLen;
92    ButtonFontInfo *pButtonFontInfo;
93 {
94    int len=strlen(Str);
95 
96    if (pButtonFontInfo != NULL) {
97 #ifdef _TGIF_DBG /* debug, do not translate */
98       fprintf(stderr, "ButtonWidth() is called with pButtonFontInfo != NULL\n");
99 #endif /* _TGIF_DBG */
100    }
101    if (boldMsgFontSet == NULL && boldMsgFontPtr == NULL) {
102       return (defaultFontWidth * max(len+2, MinLen));
103    } else if (len > MinLen-2) {
104       char buf[40];
105       int len1=0, len2=0;
106 
107       /* do not translate -- the string is used to measure things */
108       sprintf(buf, "  %s  ", Str);
109       len1 = 4+BoldMsgTextWidth(boldMsgFontPtr, buf, len+4);
110       len2 = 4+BoldMsgTextWidth(boldMsgFontPtr, "  CANCEL  ", 10);
111 
112       return max(len1,len2);
113    } else {
114       /* do not translate -- the string is used to measure things */
115       return 4+BoldMsgTextWidth(boldMsgFontPtr, "  CANCEL  ", 10);
116    }
117 }
118 
119 #define BTN_Y_MARGIN 2
120 
DisplayButtonInBBox(Win,Str,Len,BBox,Normal,HighLight,Width,pButtonFontInfo)121 void DisplayButtonInBBox(Win, Str, Len, BBox, Normal, HighLight, Width,
122       pButtonFontInfo)
123    Window Win;
124    char *Str;
125    int Len, HighLight, Width;
126    struct BBRec *BBox;
127    int Normal;
128    ButtonFontInfo *pButtonFontInfo;
129    /* Display a button in Win at location ((*BBox).ltx,(*BBox).lty), Str is */
130    /*    centered in the button.  The width of the button is given in BBox. */
131    /* Len must be strlen(Str) */
132    /* If HighLight is TRUE, Width is used to draw an outline around the box. */
133 {
134    int button_w=0, button_h=0, left=0, top=0, text_w=0;
135    int bg_pixel=(threeDLook ? myLtGryPixel : myBgPixel);
136    int btn_font_style=INVALID;
137 
138    button_w = BBox->rbx - BBox->ltx;
139    button_h = BBox->rby - BBox->lty;
140 
141    btn_font_style = GetButtonFontStyle(pButtonFontInfo);
142    switch (btn_font_style) {
143    case BTN_DEFAULT_FONT:
144       text_w = defaultFontWidth * Len;
145       top = ((button_h - defaultFontHeight)>>1);
146       break;
147    case BTN_MSG_FONT:
148       text_w = BoldMsgTextWidth(msgFontPtr, Str, Len);
149       top = ((button_h - msgFontHeight)>>1);
150       break;
151    case BTN_BOLD_MSG_FONT:
152       text_w = BoldMsgTextWidth(boldMsgFontPtr, Str, Len);
153       top = ((button_h - boldMsgFontHeight)>>1);
154       break;
155    case BTN_ITALIC_MSG_FONT:
156       text_w = BoldMsgTextWidth(italicMsgFontPtr, Str, Len);
157       top = ((button_h - italicMsgFontHeight)>>1);
158       break;
159    case BTN_BOLD_ITALIC_MSG_FONT:
160       text_w = BoldMsgTextWidth(boldItalicMsgFontPtr, Str, Len);
161       top = ((button_h - boldItalicMsgFontHeight)>>1);
162       break;
163    }
164    left = ((button_w - text_w)>>1);
165 
166    XSetForeground(mainDisplay, defaultGC, (Normal ? bg_pixel : myFgPixel));
167    XFillRectangle(mainDisplay, Win, defaultGC, BBox->ltx, BBox->lty,
168          button_w, button_h);
169    XSetForeground(mainDisplay, defaultGC, myFgPixel);
170    if (threeDLook) {
171       struct BBRec bbox;
172 
173       SetBBRec(&bbox, BBox->ltx, BBox->lty-windowPadding, BBox->ltx+button_w,
174             BBox->lty+button_h+windowPadding);
175       TgDrawThreeDButton(mainDisplay, Win, textMenuGC, &bbox,
176             (Normal ? TGBS_RAISED : TGBS_LOWRED), 2, Normal);
177    } else {
178       XDrawRectangle(mainDisplay, Win, defaultGC, BBox->ltx, BBox->lty,
179             button_w, button_h);
180    }
181    if (!Normal) {
182       XSetForeground(mainDisplay, defaultGC, myBgPixel);
183    }
184    if (pButtonFontInfo == NULL) {
185       if (boldMsgFontSet == NULL && boldMsgFontPtr == NULL) {
186          DrawBoldMsgString(mainDisplay, Win, defaultGC, BBox->ltx+left,
187                BBox->lty+defaultFontAsc+BTN_Y_MARGIN, Str, Len);
188       } else {
189          if (boldMsgFontPtr != NULL) {
190             XSetFont(mainDisplay, defaultGC, boldMsgFontPtr->fid);
191          }
192          DrawBoldMsgString(mainDisplay, Win, defaultGC, BBox->ltx+left,
193                BBox->lty+boldMsgFontAsc+BTN_Y_MARGIN, Str, Len);
194          XSetFont(mainDisplay, defaultGC, defaultFontPtr->fid);
195       }
196    } else {
197       switch (btn_font_style) {
198       case BTN_DEFAULT_FONT:
199          if (defaultFontPtr != NULL) {
200             XSetFont(mainDisplay, defaultGC, defaultFontPtr->fid);
201          }
202          DrawMsgString(mainDisplay, Win, defaultGC, BBox->ltx+left,
203                BBox->lty+defaultFontAsc+BTN_Y_MARGIN, Str, Len);
204          break;
205       case BTN_MSG_FONT:
206          if (msgFontPtr != NULL) {
207             XSetFont(mainDisplay, defaultGC, msgFontPtr->fid);
208          }
209          DrawMsgString(mainDisplay, Win, defaultGC, BBox->ltx+left,
210                BBox->lty+msgFontAsc+BTN_Y_MARGIN, Str, Len);
211          break;
212       case BTN_BOLD_MSG_FONT:
213          if (boldMsgFontPtr != NULL) {
214             XSetFont(mainDisplay, defaultGC, boldMsgFontPtr->fid);
215          }
216          DrawBoldMsgString(mainDisplay, Win, defaultGC, BBox->ltx+left,
217                BBox->lty+boldMsgFontAsc+BTN_Y_MARGIN, Str, Len);
218          break;
219       case BTN_ITALIC_MSG_FONT:
220          if (italicMsgFontPtr != NULL) {
221             XSetFont(mainDisplay, defaultGC, italicMsgFontPtr->fid);
222          }
223          DrawItalicMsgString(mainDisplay, Win, defaultGC, BBox->ltx+left,
224                BBox->lty+italicMsgFontAsc+BTN_Y_MARGIN, Str, Len);
225          break;
226       case BTN_BOLD_ITALIC_MSG_FONT:
227          if (boldItalicMsgFontPtr != NULL) {
228             XSetFont(mainDisplay, defaultGC, boldItalicMsgFontPtr->fid);
229          }
230          DrawBoldItalicMsgString(mainDisplay, Win, defaultGC, BBox->ltx+left,
231                BBox->lty+boldItalicMsgFontAsc+BTN_Y_MARGIN, Str, Len);
232          break;
233       }
234       XSetFont(mainDisplay, defaultGC, defaultFontPtr->fid);
235    }
236    if (!Normal) {
237       XSetForeground(mainDisplay, defaultGC, myFgPixel);
238    }
239    if (HighLight) {
240       if (threeDLook) {
241          XSetForeground(mainDisplay, defaultGC, myBorderPixel);
242          XDrawRectangle(mainDisplay, Win, defaultGC, BBox->ltx-1,
243                BBox->lty-windowPadding-1, button_w+1,
244                button_h+(windowPadding<<1)+1);
245          XSetForeground(mainDisplay, defaultGC, myFgPixel);
246       } else {
247          XDrawRectangle(mainDisplay, Win, defaultGC, BBox->ltx-Width,
248                BBox->lty-Width, button_w+(Width<<1), button_h+(Width<<1));
249       }
250    }
251 }
252