tkMacOSXEntry.c
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:10k
源码类别:

通讯编程

开发平台:

Visual C++

  1. /*
  2.  * tkMacOSXEntry.c --
  3.  *
  4.  * This file implements the native aqua entry widget.
  5.  *
  6.  * Copyright 2001, Apple Computer, Inc.
  7.  * Copyright (c) 2006-2007 Daniel A. Steffen <das@users.sourceforge.net>
  8.  *
  9.  * See the file "license.terms" for information on usage and redistribution of
  10.  * this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11.  *
  12.  * The following terms apply to all files originating from Apple
  13.  * Computer, Inc. ("Apple") and associated with the software
  14.  * unless explicitly disclaimed in individual files.
  15.  *
  16.  *
  17.  * Apple hereby grants permission to use, copy, modify,
  18.  * distribute, and license this software and its documentation
  19.  * for any purpose, provided that existing copyright notices are
  20.  * retained in all copies and that this notice is included
  21.  * verbatim in any distributions. No written agreement, license,
  22.  * or royalty fee is required for any of the authorized
  23.  * uses. Modifications to this software may be copyrighted by
  24.  * their authors and need not follow the licensing terms
  25.  * described here, provided that the new terms are clearly
  26.  * indicated on the first page of each file where they apply.
  27.  *
  28.  *
  29.  * IN NO EVENT SHALL APPLE, THE AUTHORS OR DISTRIBUTORS OF THE
  30.  * SOFTWARE BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
  31.  * INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
  32.  * THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF,
  33.  * EVEN IF APPLE OR THE AUTHORS HAVE BEEN ADVISED OF THE
  34.  * POSSIBILITY OF SUCH DAMAGE.  APPLE, THE AUTHORS AND
  35.  * DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
  36.  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  37.  * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS
  38.  * SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND APPLE,THE
  39.  * AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
  40.  * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  41.  *
  42.  * GOVERNMENT USE: If you are acquiring this software on behalf
  43.  * of the U.S. government, the Government shall have only
  44.  * "Restricted Rights" in the software and related documentation
  45.  * as defined in the Federal Acquisition Regulations (FARs) in
  46.  * Clause 52.227.19 (c) (2).  If you are acquiring the software
  47.  * on behalf of the Department of Defense, the software shall be
  48.  * classified as "Commercial Computer Software" and the
  49.  * Government shall have only "Restricted Rights" as defined in
  50.  * Clause 252.227-7013 (c) (1) of DFARs.  Notwithstanding the
  51.  * foregoing, the authors grant the U.S. Government and others
  52.  * acting in its behalf permission to use and distribute the
  53.  * software in accordance with the terms specified in this
  54.  * license.
  55.  *
  56.  * RCS: @(#) $Id: tkMacOSXEntry.c,v 1.2.2.13 2007/11/09 06:26:55 das Exp $
  57.  */
  58. #include "tkMacOSXPrivate.h"
  59. #include "tkMacOSXDefault.h"
  60. #include "tkEntry.h"
  61. static ThemeButtonKind ComputeIncDecParameters (int height, int *width);
  62. /*
  63.  *--------------------------------------------------------------
  64.  *
  65.  * ComputeIncDecParameters --
  66.  *
  67.  * This procedure figures out which of the kThemeIncDec
  68.  * buttons to use. It also sets width to the width of the
  69.  * IncDec button.
  70.  *
  71.  * Results:
  72.  * The ThemeButtonKind of the button we should use.
  73.  *
  74.  * Side effects:
  75.  * May draw the entry border into pixmap.
  76.  *
  77.  *--------------------------------------------------------------
  78.  */
  79. static ThemeButtonKind
  80. ComputeIncDecParameters(int height, int *width)
  81. {
  82.     ThemeButtonKind kind;
  83.     TK_IF_HI_TOOLBOX (3,
  84. if (height < 11 || height > 28) {
  85.     *width = 0;
  86.     kind = (ThemeButtonKind) 0;
  87. } else {
  88.     if (height >= 21) {
  89. *width = 13;
  90. kind = kThemeIncDecButton;
  91.     } else if (height >= 18) {
  92. *width = 12;
  93. kind = kThemeIncDecButtonSmall;
  94.     } else {
  95. *width = 11;
  96. kind = kThemeIncDecButtonMini;
  97.     }
  98. }
  99.     ) TK_ELSE_HI_TOOLBOX (3,
  100. if (height < 21 || height > 28) {
  101.     *width = 0;
  102.     kind = (ThemeButtonKind) 0;
  103. } else {
  104.     *width = 13;
  105.     kind = kThemeIncDecButton;
  106. }
  107.     ) TK_ENDIF
  108.     return kind;
  109. }
  110. /*
  111.  *--------------------------------------------------------------
  112.  *
  113.  * TkpDrawEntryBorderAndFocus --
  114.  *
  115.  * This procedure redraws the border of an entry window.
  116.  * It overrides the generic border drawing code if the
  117.  * entry widget parameters are such that the native widget
  118.  * drawing is a good fit.
  119.  * This version just returns 1, so platforms that don't
  120.  * do special native drawing don't have to implement it.
  121.  *
  122.  * Results:
  123.  * 1 if it has drawn the border, 0 if not.
  124.  *
  125.  * Side effects:
  126.  * May draw the entry border into pixmap.
  127.  *
  128.  *--------------------------------------------------------------
  129.  */
  130. int
  131. TkpDrawEntryBorderAndFocus(Entry *entryPtr, Drawable d, int isSpinbox)
  132. {
  133.     Rect bounds;
  134.     TkMacOSXDrawingContext dc;
  135.     GC bgGC;
  136.     Tk_Window tkwin = entryPtr->tkwin;
  137.     ThemeDrawState drawState;
  138.     int oldWidth = 0;
  139.     MacDrawable *macDraw = (MacDrawable *) d;
  140.     /*
  141.      * I use 6 as the borderwidth. 2 of the 5 go into the actual frame the
  142.      * 3 are because the Mac OS Entry widgets leave more space around the
  143.      * Text than Tk does on X11.
  144.      */
  145.     if (entryPtr->borderWidth != MAC_OSX_ENTRY_BORDER
  146.     || entryPtr->highlightWidth != MAC_OSX_FOCUS_WIDTH
  147.     || entryPtr->relief != MAC_OSX_ENTRY_RELIEF) {
  148. return 0;
  149.     }
  150.     /*
  151.      * For the spinbox, we have to make the entry part smaller by the size
  152.      * of the buttons. We also leave 2 pixels to the left (as per the HIG)
  153.      * and space for one pixel to the right, 'cause it makes the buttons look
  154.      * nicer.
  155.      */
  156.     if (isSpinbox) {
  157. ThemeButtonKind buttonKind;
  158. int incDecWidth;
  159. oldWidth = Tk_Width(tkwin);
  160. buttonKind = ComputeIncDecParameters(Tk_Height(tkwin)
  161. - 2 * MAC_OSX_FOCUS_WIDTH, &incDecWidth);
  162. Tk_Width(tkwin) -= incDecWidth + 1;
  163.     }
  164.    /*
  165.     * The focus ring is drawn with an Alpha at the outside
  166.     * part of the ring, so we have to draw over the edges of the
  167.     * ring before drawing the focus or the text will peep through.
  168.     */
  169.     bgGC = Tk_GCForColor(entryPtr->highlightBgColorPtr, d);
  170.     TkDrawInsetFocusHighlight(entryPtr->tkwin, bgGC, MAC_OSX_FOCUS_WIDTH, d, 0);
  171.     /*
  172.      * Inset the entry Frame by the maximum width of the focus rect,
  173.      * which is 3 according to the Carbon docs.
  174.      */
  175.     bounds.left = macDraw->xOff + MAC_OSX_FOCUS_WIDTH;
  176.     bounds.top = macDraw->yOff + MAC_OSX_FOCUS_WIDTH;
  177.     bounds.right = macDraw->xOff + Tk_Width(tkwin) - MAC_OSX_FOCUS_WIDTH;
  178.     bounds.bottom = macDraw->yOff + Tk_Height(tkwin) - MAC_OSX_FOCUS_WIDTH;
  179.     if (entryPtr->state == STATE_DISABLED) {
  180. drawState = kThemeStateInactive;
  181.     } else {
  182. drawState = kThemeStateActive;
  183.     }
  184.     if (!TkMacOSXSetupDrawingContext(d, NULL, 0, &dc)) {
  185. return 0;
  186.     }
  187.     DrawThemeEditTextFrame(&bounds, drawState);
  188.     if (entryPtr->flags & GOT_FOCUS) {
  189. /*
  190.  * Don't call this if we don't have the focus, because then it
  191.  * erases the focus rect to white, but we've already drawn the
  192.  * highlightbackground above.
  193.  */
  194. DrawThemeFocusRect(&bounds, (entryPtr->flags & GOT_FOCUS) != 0);
  195.     }
  196.     if (isSpinbox) {
  197. Tk_Width(tkwin) = oldWidth;
  198.     }
  199.     TkMacOSXRestoreDrawingContext(&dc);
  200.     return 1;
  201. }
  202. /*
  203.  *--------------------------------------------------------------
  204.  *
  205.  * TkpDrawSpinboxButtons --
  206.  *
  207.  * This procedure redraws the buttons of an spinbox widget.
  208.  * It overrides the generic button drawing code if the
  209.  * spinbox widget parameters are such that the native widget
  210.  * drawing is a good fit.
  211.  * This version just returns 0, so platforms that don't
  212.  * do special native drawing don't have to implement it.
  213.  *
  214.  * Results:
  215.  * 1 if it has drawn the border, 0 if not.
  216.  *
  217.  * Side effects:
  218.  * May draw the entry border into pixmap.
  219.  *
  220.  *--------------------------------------------------------------
  221.  */
  222. int
  223. TkpDrawSpinboxButtons(Spinbox *sbPtr, Drawable d)
  224. {
  225.     Rect inBounds;
  226.     ThemeButtonKind inKind;
  227.     ThemeButtonDrawInfo inNewInfo;
  228.     ThemeButtonDrawInfo * inPrevInfo = NULL;
  229.     ThemeEraseUPP inEraseProc = NULL;
  230.     ThemeButtonDrawUPP inLabelProc = NULL;
  231.     UInt32 inUserData = 0;
  232.     Tk_Window tkwin = sbPtr->entry.tkwin;
  233.     int height = Tk_Height(tkwin);
  234.     int buttonHeight = height - 2 * MAC_OSX_FOCUS_WIDTH;
  235.     int incDecWidth;
  236.     TkMacOSXDrawingContext dc;
  237.     XRectangle rects[1];
  238.     GC bgGC;
  239.     MacDrawable *macDraw = (MacDrawable *) d;
  240.     /*
  241.      * FIXME: RAISED really makes more sense
  242.      */
  243.     if (sbPtr->buRelief != TK_RELIEF_FLAT) {
  244. return 0;
  245.     }
  246.     /*
  247.      * The actual sizes of the IncDec button are 21 for the normal,
  248.      * 18 for the small and 15 for the mini. But the spinbox still
  249.      * looks okay if the entry is a little bigger than this, so we
  250.      * give it a little slop.
  251.      */
  252.     inKind = ComputeIncDecParameters(buttonHeight, &incDecWidth);
  253.     if (inKind == (ThemeButtonKind) 0) {
  254. return 0;
  255.     }
  256.     if (sbPtr->entry.state == STATE_DISABLED) {
  257. inNewInfo.state = kThemeStateInactive;
  258. inNewInfo.value = kThemeButtonOff;
  259.     } else if (sbPtr->selElement == SEL_BUTTONUP) {
  260. inNewInfo.state = kThemeStatePressedUp;
  261. inNewInfo.value = kThemeButtonOn;
  262.     } else if (sbPtr->selElement == SEL_BUTTONDOWN) {
  263. inNewInfo.state = kThemeStatePressedDown;
  264. inNewInfo.value = kThemeButtonOn;
  265.     } else {
  266. inNewInfo.state = kThemeStateActive;
  267. inNewInfo.value = kThemeButtonOff;
  268.     }
  269.     inNewInfo.adornment = kThemeAdornmentNone;
  270.     inBounds.left = macDraw->xOff + Tk_Width(tkwin) - incDecWidth - 1;
  271.     inBounds.right = macDraw->xOff + Tk_Width(tkwin) - 1;
  272.     inBounds.top = macDraw->yOff + MAC_OSX_FOCUS_WIDTH;
  273.     inBounds.bottom = macDraw->yOff + Tk_Height(tkwin) - MAC_OSX_FOCUS_WIDTH;
  274.     /* We had to make the entry part of the window smaller so that we
  275.      * wouldn't overdraw the spin buttons with the focus highlight. So
  276.      * now we have to draw the highlightbackground.
  277.      */
  278.     bgGC = Tk_GCForColor(sbPtr->entry.highlightBgColorPtr, d);
  279.     rects[0].x = inBounds.left;
  280.     rects[0].y = 0;
  281.     rects[0].width = Tk_Width(tkwin);
  282.     rects[0].height = Tk_Height(tkwin);
  283.     XFillRectangles(Tk_Display(tkwin), d, bgGC, rects, 1);
  284.     if (!TkMacOSXSetupDrawingContext(d, NULL, 0, &dc)) {
  285. return 0;
  286.     }
  287.     ChkErr(DrawThemeButton, &inBounds, inKind, &inNewInfo, inPrevInfo,
  288.     inEraseProc, inLabelProc, inUserData);
  289.     TkMacOSXRestoreDrawingContext(&dc);
  290.     return 1;
  291. }