mGNCS API Reference  v1.2.0
A new control set and a new framework for MiniGUI apps
medit.h
Go to the documentation of this file.
1 
42 #ifndef _MGUI_NCSCTRL_EDIT_H
43 #define _MGUI_NCSCTRL_EDIT_H
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif /* __cplusplus */
48 
49 /* structure to represent a text string */
50 typedef struct strbuffer_s
51 {
52  unsigned char *string; /* text string content */
53  unsigned int txtlen; /* text len or buffer used size */
54  unsigned int blocksize; /* block size, a block is an allocate unit */
55  unsigned int buffsize; /* buffer size */
56 } StrBuffer;
57 
58 /* ------------------------- memory and struct alloc ------------------------ */
59 
60 #define te_alloc malloc
61 #define te_free free
62 #define te_realloc(ptr, size) realloc(ptr, size)
63 
64 /* alloc with a unit of blocksize */
65 static inline void* testr_alloc (StrBuffer *txtbuff, size_t len, size_t blocksize)
66 {
67  txtbuff->buffsize = (len + 1) + (blocksize - (len + 1)%blocksize);
68  txtbuff->string = (unsigned char *)te_alloc (txtbuff->buffsize);
69  txtbuff->blocksize = blocksize;
70  //txtbuff->txtlen = len;
71  return txtbuff->string;
72 }
73 
74 static inline void* testr_realloc (StrBuffer *txtbuff, size_t len)
75 {
76  if (len + 1 > (size_t)txtbuff->buffsize ||
77  len + 1 < (size_t)(txtbuff->buffsize - txtbuff->blocksize)) {
78  /* really realloc */
79  txtbuff->buffsize = (len + 1) + (txtbuff->blocksize -
80  (len + 1)%txtbuff->blocksize);
81  txtbuff->string = (unsigned char *)te_realloc (txtbuff->string, txtbuff->buffsize);
82  }
83 
84  return txtbuff->string;
85 }
86 
87 static inline void testr_free (StrBuffer *txtbuff)
88 {
89  if (txtbuff) {
90  te_free (txtbuff->string);
91  }
92 }
93 
94 static inline void testr_setstr (StrBuffer *txtbuff, const char *newstring, unsigned int len)
95 {
96  int datalen;
97 
98  if (len < 0 && !newstring) return;
99 
100  datalen = MIN(len, txtbuff->buffsize-1);
101  memcpy (txtbuff->string, newstring, datalen);
102  txtbuff->string[datalen] = '\0';
103  txtbuff->txtlen = datalen;
104 }
105 
106 /* copy a string buffer */
107 static inline StrBuffer* testr_copy (StrBuffer *des, StrBuffer *src)
108 {
109  if (!des || !src)
110  return NULL;
111 
112  testr_free (des);
113  testr_alloc (des, src->txtlen, des->blocksize);
114  testr_setstr (des, (const char *)src->string, src->txtlen);
115  return des;
116 }
117 
118 /* duplicates a string buffer */
119 static inline StrBuffer* testr_dup (StrBuffer *s1)
120 {
121  StrBuffer *s2;
122 
123  if (!s1)
124  return NULL;
125 
126  s2 = (StrBuffer *)malloc (sizeof(s1));
127  if (s2) {
128  testr_alloc (s2, s1->txtlen, s1->blocksize);
129  testr_setstr (s2, (const char *)s1->string, s1->txtlen);
130  }
131  return s2;
132 }
133 
134 /* duplicates a string buffer */
135 static inline StrBuffer* testr_dup2 (StrBuffer *s2, StrBuffer *s1)
136 {
137  if (!s1 || !s2)
138  return NULL;
139 
140  testr_alloc (s2, s1->txtlen, s1->blocksize);
141  testr_setstr (s2, (const char *)s1->string, s1->txtlen);
142  return s2;
143 }
144 
145 /* -------------------------------------------------------------------------- */
146 
147 
153 /*
154  * \def NCSCTRL_EDIT
155  * \brief the name of edit control
156 */
157 #define NCSCTRL_EDIT NCSCLASSNAME("edit")
158 
159 #ifdef _MGNCS_USE_DBEDIT
160 #define DOUBLE_DC HDC db_dc;
161 #else
162 #define DOUBLE_DC
163 #endif
164 
165 typedef struct _mEdit mEdit;
166 typedef struct _mEditClass mEditClass;
167 typedef struct _mEditRenderer mEditRenderer;
168 
169 #define mEditHeader(Class) \
170  mScrollViewHeader(Class) \
171  DOUBLE_DC
172 
179 struct _mEdit
180 {
181  mEditHeader(mEdit)
182 };
183 
184 
185 typedef struct _TextAttribute
186 {
187  struct _TextAttribute* (*clone)(void);
188  void (*destroy)(void);
189 }TextAttribute;
190 
191 typedef struct _PasteInfo {
192  char *str;
193  int len;
194  TextAttribute *attr;
195  void *user_info;
196  struct _PasteInfo *next;
197 }PasteInfo;
198 
199 typedef struct _TextCopyPaste
200 {
201  void (*addCopyInfo)(const char* str, int len, TextAttribute *atrr, void *user_info);
202  void (*endCopyInfo)(void);
203  BOOL (*isEmpty)(void);
204  PasteInfo* (*getPasteInfo)(void);
205 }TextCopyPaste;
206 
207 typedef struct _UndoRedoInfo
208 {
209  char *text;
210  int start;
211  int end;
212  TextAttribute *attr;
213  void *user_data;
214  struct _UndoRedoInfo * next;
215 } UndoRedoInfo;
216 
217 typedef struct _TextUndoRedoInfo
218 {
219  BOOL (*pushText)(const char* text, int start, int end, TextAttribute *attr, void *user_data);
220  BOOL (*endPush)(void);
221 
222  UndoRedoInfo* (*getUndoTop)(void);
223  UndoRedoInfo* (*getRedoTop)(void);
224 
225  void (*popUndo)(void);
226  void (*popRedo)(void);
227 
228  BOOL (*isUndoEmpty)(void);
229  BOOL (*isRedoEmpty)(void);
230 
231  void (*empty)(void);
232 }TextUndoRedoInfo;
233 
234 #define mEditClassHeader(clsName, parentClass) \
235  mScrollViewClassHeader(clsName, parentClass) \
236  int (*onChar)(clsName* self, int asciiCode, DWORD keyFlags); \
237  int (*setContent)(clsName *self, const char* str, int start, int len); \
238  void (*replaceText)(clsName *self, const char* str, int start, int len, int replaceStart, int replaceEnd); \
239  void (*insert)(clsName *self, const char* str, int start, int len, int insertStart); \
240  void (*append)(clsName *self, const char* str, int start, int len); \
241  int (*getTextLength)(clsName *self); \
242  int (*getContent)(clsName *self, char *strBuff, int bufLen, int start, int end); \
243  int (*setTextAttr)(clsName *self, int start, int end, TextAttribute *attr); \
244  TextAttribute* (*getTextAttr)(clsName *self, int start, int end, int *pEnd); \
245  int (*setSel)(clsName *self, int start, int end); \
246  int (*getSel)(clsName *self, int *pStart, int *pEnd); \
247  void (*setMargin)(clsName *self, int left, int top, int right, int bottom); \
248  void (*copy)(clsName *self); \
249  void (*cut)(clsName *self); \
250  void (*paste)(clsName *self); \
251  TextCopyPaste * (*setCopyPaste)(clsName *self, TextCopyPaste* cp); \
252  void (*undo)(clsName *self); \
253  void (*redo)(clsName *self); \
254  TextUndoRedoInfo * (*setUndoRedo)(clsName *self, TextUndoRedoInfo* ur); \
255  BOOL (*makevisible)(clsName *self, int pos);
256 
360 struct _mEditClass
361 {
362  mEditClassHeader(mEdit, mScrollView)
363 };
364 
371 MGNCS_EXPORT extern mEditClass g_stmEditCls;
372 
373 
374 #define mEditRendererHeader(clsName, parentClass) \
375  mScrollViewRendererHeader(clsName, parentClass)
376 
383 struct _mEditRenderer
384 {
385  mEditRendererHeader(mEdit, mScrollView)
386 };
387 
392 #define NCSS_EDIT_LEFT (0x0000L<<NCSS_SCRLV_SHIFT)
393 
398 #define NCSS_EDIT_CENTER (0x0001L<<NCSS_SCRLV_SHIFT)
399 
404 #define NCSS_EDIT_RIGHT (0x0002L<<NCSS_SCRLV_SHIFT)
405 
410 #define NCSS_EDIT_UPPERCASE (0x0004L<<NCSS_SCRLV_SHIFT)
411 
416 #define NCSS_EDIT_LOWERCASE (0x0008L<<NCSS_SCRLV_SHIFT)
417 
422 #define NCSS_EDIT_NOHIDESEL (0x0010L<<NCSS_SCRLV_SHIFT)
423 
428 #define NCSS_EDIT_READONLY (0x0020L<<NCSS_SCRLV_SHIFT)
429 
434 #define NCSS_EDIT_BASELINE (0x0040L<<NCSS_SCRLV_SHIFT)
435 
436 #define NCSS_EDIT_SHIFT (NCSS_SCRLV_SHIFT+8)
437 
438 
439 
445 {
460  NCSP_EDIT_MAX
461 };
462 
463 
466 {
474  NCSN_EDIT_MAX
475 };
476 
477 #ifdef _MGNCS_USE_DBEDIT
478 static inline HDC mEdit_getDC(mEdit *self)
479 {
480  if (self->db_dc) {
481  SelectClipRect(self->db_dc, NULL);
482  SelectFont(self->db_dc, GetWindowFont(self->hwnd));
483  return self->db_dc;
484  }
485  else
486  return GetClientDC(self->hwnd);
487 }
488 
489 static inline void mEdit_releaseDC(mEdit *self, HDC hdc)
490 {
491  if (self->db_dc == 0 || self->db_dc != hdc)
492  ReleaseDC(hdc);
493 }
494 #else
495 static inline HDC mEdit_getDC(mEdit *self)
496 {
497  return GetClientDC(self->hwnd);
498 }
499 
500 static inline void mEdit_releaseDC(mEdit *self, HDC hdc)
501 {
502  ReleaseDC(hdc);
503 }
504 #endif
505 #define GETDC(self) mEdit_getDC((mEdit*)self)
506 #define RELEASEDC(self, hdc) mEdit_releaseDC((mEdit*)self, hdc)
507 
510 #ifdef __cplusplus
511 }
512 #endif /* __cplusplus */
513 
514 #endif /* _MGUI_NCSCTRL_EDIT_H */
515 
516 
The structure of mScrollView control, which derived from mItemView.
the edit struct of edit control, derived from mScrollView.
Edit class&#39;s Renderer interface, derived from mScrollViewRenderer.
mEditProp
the Property of edit
Definition: medit.h:444
mEditNotify
Definition: medit.h:465
the VTable of mEdit, derived from mScrollViewClass.
Get/Set the position of charactor.
Definition: medit.h:459
MGNCS_EXPORT mEditClass g_stmEditCls
the instance of mEditClass
Get/Set the maximum length of text.
Definition: medit.h:452