ui_node_abstractscrollable.c

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 2002-2010 UFO: Alien Invasion.
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018 See the GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 
00024 */
00025 
00026 #include "../ui_main.h"
00027 #include "../ui_parse.h"
00028 #include "../ui_font.h"
00029 #include "../ui_render.h"
00030 #include "../ui_actions.h"
00031 #include "ui_node_abstractnode.h"
00032 #include "ui_node_abstractscrollable.h"
00033 
00034 #include "../../client.h" /* gettext _() */
00035 
00036 #define EXTRADATA_TYPE abstractScrollableExtraData_t
00037 #define EXTRADATA(node) UI_EXTRADATA(node, EXTRADATA_TYPE)
00038 
00042 qboolean UI_AbstractScrollableNodeIsSizeChange (uiNode_t *node)
00043 {
00044     assert(UI_NodeInstanceOf(node, "abstractscrollable"));
00045 
00046     if (node->size[0] != EXTRADATA(node).cacheSize[0]
00047         || node->size[1] != EXTRADATA(node).cacheSize[1])
00048     {
00049         EXTRADATA(node).cacheSize[0] = node->size[0];
00050         EXTRADATA(node).cacheSize[1] = node->size[1];
00051         return qtrue;
00052     }
00053     return qfalse;
00054 }
00055 
00064 qboolean UI_SetScroll (uiScroll_t *scroll, int viewPos, int viewSize, int fullSize)
00065 {
00066     qboolean updated = qfalse;
00067 
00068     /* default values */
00069     if (viewPos == -1)
00070         viewPos = scroll->viewPos;
00071     if (viewSize == -1)
00072         viewSize = scroll->viewSize;
00073     if (fullSize == -1)
00074         fullSize = scroll->fullSize;
00075 
00076     /* fix limits */
00077     if (viewSize < 0)
00078         viewSize = 0;
00079     if (fullSize < 0)
00080         fullSize = 0;
00081     if (viewPos < 0)
00082         viewPos = 0;
00083 
00084     /* clap position */
00085     if (viewSize >= fullSize)
00086         viewPos = 0;
00087     else if (viewPos > fullSize - viewSize)
00088         viewPos = fullSize - viewSize;
00089 
00090     /* update */
00091     if (scroll->viewPos != viewPos) {
00092         scroll->viewPos = viewPos;
00093         updated = qtrue;
00094     }
00095     if (scroll->viewSize != viewSize) {
00096         scroll->viewSize = viewSize;
00097         updated = qtrue;
00098     }
00099     if (scroll->fullSize != fullSize) {
00100         scroll->fullSize = fullSize;
00101         updated = qtrue;
00102     }
00103 
00104     return updated;
00105 }
00106 
00115 qboolean UI_AbstractScrollableNodeSetY (uiNode_t *node, int viewPos, int viewSize, int fullSize)
00116 {
00117     qboolean updated;
00118     assert(UI_NodeInstanceOf(node, "abstractscrollable"));
00119 
00120     updated = UI_SetScroll(&EXTRADATA(node).scrollY, viewPos, viewSize, fullSize);
00121 
00122     if (updated && EXTRADATA(node).onViewChange)
00123         UI_ExecuteEventActions(node, EXTRADATA(node).onViewChange);
00124 
00125     return updated;
00126 }
00127 
00131 static void UI_AbstractScrollableNodePageUp (uiNode_t *node, const uiCallContext_t *context) {
00132     const int pos = EXTRADATA(node).scrollY.viewPos - 10;
00133     UI_AbstractScrollableNodeSetY(node, (pos >= 0)?pos:0, -1, -1);
00134 }
00135 
00136 static void UI_AbstractScrollableNodePageDown (uiNode_t *node, const uiCallContext_t *context) {
00137     UI_AbstractScrollableNodeSetY(node, EXTRADATA(node).scrollY.viewPos + 10, -1, -1);
00138 }
00139 
00140 static void UI_AbstractScrollableNodeMoveUp (uiNode_t *node, const uiCallContext_t *context) {
00141     UI_AbstractScrollableNodeSetY(node, EXTRADATA(node).scrollY.viewPos - 1, -1, -1);
00142 }
00143 
00144 static void UI_AbstractScrollableNodeMoveDown (uiNode_t *node, const uiCallContext_t *context) {
00145     UI_AbstractScrollableNodeSetY(node, EXTRADATA(node).scrollY.viewPos + 1, -1, -1);
00146 }
00147 
00148 static void UI_AbstractScrollableNodeMoveHome (uiNode_t *node, const uiCallContext_t *context) {
00149     UI_AbstractScrollableNodeSetY(node, 0, -1, -1);
00150 }
00151 
00155 static void UI_AbstractScrollableNodeMoveEnd (uiNode_t *node, const uiCallContext_t *context) {
00156     UI_AbstractScrollableNodeSetY(node, EXTRADATA(node).scrollY.fullSize, -1, -1);
00157 }
00158 
00163 qboolean UI_AbstractScrollableNodeScrollY (uiNode_t *node, int offset)
00164 {
00165     assert(UI_NodeInstanceOf(node, "abstractscrollable"));
00166     return UI_AbstractScrollableNodeSetY(node, EXTRADATA(node).scrollY.viewPos + offset, -1, -1);
00167 }
00168 
00169 static const value_t properties[] = {
00170     /* position of the vertical view (into the full number of elements the node contain) */
00171     {"viewpos", V_INT, UI_EXTRADATA_OFFSETOF(EXTRADATA_TYPE, scrollY.viewPos),  MEMBER_SIZEOF(EXTRADATA_TYPE, scrollY.viewPos)},
00172     /* size of the vertical view (proportional to the number of elements the node can display without moving) */
00173     {"viewsize", V_INT, UI_EXTRADATA_OFFSETOF(EXTRADATA_TYPE, scrollY.viewSize),  MEMBER_SIZEOF(EXTRADATA_TYPE, scrollY.viewSize)},
00174     /* full vertical size (proportional to the number of elements the node contain) */
00175     {"fullsize", V_INT, UI_EXTRADATA_OFFSETOF(EXTRADATA_TYPE, scrollY.fullSize),  MEMBER_SIZEOF(EXTRADATA_TYPE, scrollY.fullSize)},
00176     /* Called when one of the properties viewpos/viewsize/fullsize change */
00177     {"onviewchange", V_UI_ACTION, UI_EXTRADATA_OFFSETOF(EXTRADATA_TYPE, onViewChange), MEMBER_SIZEOF(EXTRADATA_TYPE, onViewChange)},
00178 
00179     /* Call it to vertically scroll the document up */
00180     {"pageup", V_UI_NODEMETHOD, ((size_t) UI_AbstractScrollableNodePageUp), 0},
00181     /* Call it to vertically scroll the document down */
00182     {"pagedown", V_UI_NODEMETHOD, ((size_t) UI_AbstractScrollableNodePageDown), 0},
00183     /* Call it to vertically scroll the document up */
00184     {"moveup", V_UI_NODEMETHOD, ((size_t) UI_AbstractScrollableNodeMoveUp), 0},
00185     /* Call it to vertically scroll the document down */
00186     {"movedown", V_UI_NODEMETHOD, ((size_t) UI_AbstractScrollableNodeMoveDown), 0},
00187     /* Call it to vertically reset the scroll position to 0 */
00188     {"movehome", V_UI_NODEMETHOD, ((size_t) UI_AbstractScrollableNodeMoveHome), 0},
00189     /* Call it to vertically move the scroll to the end of the document */
00190     {"moveend", V_UI_NODEMETHOD, ((size_t) UI_AbstractScrollableNodeMoveEnd), 0},
00191 
00192     {NULL, V_NULL, 0, 0}
00193 };
00194 
00195 void UI_RegisterAbstractScrollableNode (uiBehaviour_t *behaviour)
00196 {
00197     behaviour->name = "abstractscrollable";
00198     behaviour->isAbstract = qtrue;
00199     behaviour->properties = properties;
00200     behaviour->extraDataSize = sizeof(EXTRADATA_TYPE);
00201 }

Generated by  doxygen 1.6.2