MorePalmOS
A development library for developing Palm OS applications

MoreField.c

Go to the documentation of this file.
00001 /******************************************************************************
00002  * MorePalmOS
00003  * Copyright (c) 2004 Steven Fisher
00004  *
00005  * Distributed under the Boost Software License, Version 1.0. See accompanying
00006  * license file License.txt or <http://www.boost.org/LICENSE_1_0.txt>.
00007  *
00008  * http://morepalmos.sourceforge.net
00009  *
00010  *   MorePalmOS is the humble begining of an attempt to provide a library for
00011  *   Palm OS development which works around system bugs, illustrate how to
00012  *   use system calls, and provide "glue code" for programming Palm OS
00013  *   applications in a more straightforward way while minimally impacting
00014  *   code size.
00015  *****************************************************************************/
00016 
00017 #include "MorePrefix.h"
00018 #include "MoreField.h"
00019 #include "MoreForm.h"
00020 #include "MoreMemoryMgr.h"
00021 #include "MoreStringMgr.h"
00022 #include "MorePrivate.h"
00023 
00024 #include <Field.h>
00025 #include <MemoryMgr.h>
00026 #include <StringMgr.h>
00027 
00028 
00029 //-----------------------------------------------------------------------------
00030 
00031 #if !MOREPALMOS_MACRO_SIMPLE_FUNCS
00032 
00033 FieldType* MFldGetByID( FormType* formP, FrmObjectID_t id )
00034 {
00035         return ( FieldType* )MFrmGetObjPtrByID( formP, id );
00036 }
00037 
00038 FieldType* MFldGetByIdx( FormType* formP, FrmObjectIdx_t idx )
00039 {
00040         return ( FieldType* )MFrmGetObjPtrByIdx( formP, idx );
00041 }
00042 
00043 FieldType* MFldGetByPtr( FrmObject_t* objectP )
00044 {
00045         return ( FieldType* )( objectP );
00046 }
00047 
00048 #endif
00049 
00050 //-----------------------------------------------------------------------------
00051 
00054 static _rinline void PrvMFldAbandonHandle( MemHandle oldH, MoreSetTextFlags flags )
00055 {
00056         if ( oldH && ( !( flags & mstLeakOld ) ) )
00057         {
00058                 MemHandleFree( oldH );
00059         }
00060 }
00061 
00064 static _rinline void PrvMFldRedraw( FieldType* fieldP, MoreSetTextFlags flags )
00065 {
00066         if ( flags & mstRedraw )
00067         {
00068                 FldDrawField( fieldP );
00069         }
00070 }
00071 
00072 //-----------------------------------------------------------------------------
00073 
00074 Err MFldSetTextFromChars( FieldType* fieldP, const char *s,
00075                 UInt32 len, MoreSetTextFlags flags )
00076 {
00077         Err err;
00078         UInt32 size = len + 1;
00079         MemHandle oldH = FldGetTextHandle( fieldP );
00080         MemHandle newH = NULL;
00081         char* p;
00082 
00083         // We've been asked to reuse the old hadnel. Try to do so. 
00084         if ( ( mstReuseOld & flags ) && ( oldH ) )
00085         {
00086                 // Sever handle from field before changing it. 
00087                 FldSetTextHandle( fieldP, NULL );
00088                 err = MemHandleResize( oldH, size );
00089                 if ( errNone != err )
00090                 {
00091                         /* Couldn't resize handle. Reattach it; we'll create a new
00092                            one instead. */
00093                         FldSetTextHandle( fieldP, oldH );
00094                 }
00095                 else
00096                 {
00097                         newH = oldH;
00098                 }
00099         }
00100 
00101         // Try to allocate a new handle for the text. 
00102         if ( !newH )
00103         {
00104                 newH = MemHandleNew( size );
00105         }
00106 
00107         // If we still don't have a handle, fail. 
00108         if ( !newH )
00109         {
00110                 return memErrNotEnoughSpace;
00111         }
00112 
00113         // Copy stirng into handle. 
00114         p = ( char * ) MemHandleLock( newH );
00115         MemMove( p, s, len );
00116         p [ len ] = 0;
00117         MemHandleUnlock( newH );
00118 
00119         /* Dispose of the old handle. We do this after copying in case the old
00120            handle contains the text we're copying. */
00121         if ( oldH != newH )
00122                 PrvMFldAbandonHandle( oldH, flags );
00123 
00124         // Assign handle to field. 
00125         FldSetTextHandle( fieldP, newH );
00126 
00127         // Redraw the field if so asked.
00128         PrvMFldRedraw( fieldP, flags );
00129 
00130         return errNone;
00131 }
00132 
00133 Err MFldSetTextFromCharsByID( FormType* formP, FrmObjectID_t fieldID,
00134                 const Char *s, UInt32 len, MoreSetTextFlags flags )
00135 {
00136         _validate_object_id( formP, fieldID );
00137         return MFldSetTextFromChars( MFldGetByID( formP, fieldID ), s,
00138                         len, flags );
00139 }
00140 
00141 Err MFldSetTextFromCharsByIdx( FormType* formP, FrmObjectIdx_t fieldIdx,
00142                 const Char *s, UInt32 len, MoreSetTextFlags flags )
00143 {
00144         _validate_object_idx( formP, fieldIdx );
00145         return MFldSetTextFromChars( MFldGetByIdx( formP, fieldIdx ), s,
00146                         len, flags );
00147 }
00148 
00149 //-----------------------------------------------------------------------------
00150 
00151 Err MFldSetTextFromStr( FieldType* fieldP, const char *s,
00152                 MoreSetTextFlags flags )
00153 {
00154         return MFldSetTextFromChars( fieldP, s, MStrLen( s ), flags );
00155 }
00156 
00157 Err MFldSetTextFromStrByID( FormType* formP, FrmObjectID_t fieldID,
00158                 const Char *s, MoreSetTextFlags flags )
00159 {
00160         _validate_object_id( formP, fieldID );
00161         return MFldSetTextFromStr( MFldGetByID( formP, fieldID ), s,
00162                         flags );
00163 }
00164 
00165 Err MFldSetTextFromStrByIdx( FormType* formP, FrmObjectIdx_t fieldIdx,
00166                 const Char *s, MoreSetTextFlags flags )
00167 {
00168         _validate_object_idx( formP, fieldIdx );
00169         return MFldSetTextFromStr( MFldGetByIdx( formP, fieldIdx ), s,
00170                         flags );
00171 }
00172 
00173 //-----------------------------------------------------------------------------
00174 
00175 Err MFldClearText( FieldType* fieldP, MoreSetTextFlags flags )
00176 {
00177         _check( fieldP );
00178         return MFldSetTextFromChars( fieldP, NULL, 0, flags );
00179 }
00180 
00181 Err MFldClearTextByID( FormType* formP, FrmObjectID_t fieldID,
00182                 MoreSetTextFlags flags )
00183 {
00184         _validate_object_id( formP, fieldID );
00185         return MFldClearText( MFldGetByID( formP, fieldID ), flags );
00186 }
00187 
00188 Err MFldClearTextByIdx( FormType* formP, FrmObjectIdx_t fieldIdx,
00189                 MoreSetTextFlags flags )
00190 {
00191         _validate_object_idx( formP, fieldIdx );
00192         return MFldClearText( MFldGetByIdx( formP, fieldIdx ), flags );
00193 }
00194 //-----------------------------------------------------------------------------
00195 
00196 Err MFldSetTextFromHandle( FieldPtr fieldP, MemHandle text,
00197                 MoreSetTextFlags flags )
00198 {
00199         Err err;
00200         if ( mstReuseOld & flags )
00201         {
00202                 /* We were asked to reuse the old handle, so lock down this handle
00203                    and pass the text to MFldSetTextFromChars. */
00204                 MemPtr textP;
00205                 UInt16 textSize;
00206                 _validate_field_ptr( fieldP );
00207                 _validate_memhandle( text );
00208                 textSize = MemHandleSize( text );
00209                 textP = MemHandleLock( text );
00210                 err = MFldSetTextFromChars( fieldP, (char*)textP, textSize, flags );
00211                 /* Whether or not MFldSetTextFromChars() fails, we still need to
00212                    unlock the text handle. */
00213                 MemHandleUnlock( text );
00214                 return err;
00215         }
00216         else
00217         {
00218                 /* We weren't asked to reuse the old handle, so assign the new handle
00219                    to the field. */
00220                 MemHandle oldH = FldGetTextHandle( fieldP );
00221                 if ( oldH != text ) /* Is this actually a new handle? */
00222                 {
00223                         FldSetTextHandle( fieldP, text );
00224                         PrvMFldAbandonHandle( oldH, flags );
00225                         // Redraw the field if so asked. 
00226                         PrvMFldRedraw( fieldP, flags );
00227                 }
00228                 return errNone;
00229         }
00230 }
00231 
00232 Err MFldSetTextFromHandleByID( FormType* formP, FrmObjectID_t fieldID,
00233                 MemHandle text, MoreSetTextFlags flags )
00234 {
00235         _validate_object_id( formP, fieldID );
00236         return MFldSetTextFromHandle( MFldGetByID( formP, fieldID ), text,
00237                         flags );
00238 }
00239 
00240 Err MFldSetTextFromHandleByIdx( FormType* formP, FrmObjectIdx_t fieldIdx,
00241                 MemHandle text, MoreSetTextFlags flags )
00242 {
00243         _validate_object_idx( formP, fieldIdx );
00244         return MFldSetTextFromHandle( MFldGetByIdx( formP, fieldIdx ), text,
00245                         flags );
00246 }
00247 
00248 //---------------------------------------------------------------------------

SourceForge.net Logo