mGNCS API Reference  v1.5.0
A new control set and a new framework for MiniGUI apps
medit.h
Go to the documentation of this file.
1 //
3 // IMPORTANT NOTICE
4 //
5 // The following open source license statement does not apply to any
6 // entity in the Exception List published by FMSoft.
7 //
8 // For more information, please visit:
9 //
10 // https://www.fmsoft.cn/exception-list
11 //
13 
54 #ifndef _MGUI_NCSCTRL_EDIT_H
55 #define _MGUI_NCSCTRL_EDIT_H
56 
57 #ifdef __cplusplus
58 extern "C" {
59 #endif /* __cplusplus */
60 
61 /* structure to represent a text string */
62 typedef struct strbuffer_s
63 {
64  unsigned char *string; /* text string content */
65  unsigned int txtlen; /* text len or buffer used size */
66  unsigned int blocksize; /* block size, a block is an allocate unit */
67  unsigned int buffsize; /* buffer size */
68 } StrBuffer;
69 
70 /* ------------------------- memory and struct alloc ------------------------ */
71 
72 #define te_alloc malloc
73 #define te_free free
74 #define te_realloc(ptr, size) realloc(ptr, size)
75 
76 /* alloc with a unit of blocksize */
77 static inline void* testr_alloc (StrBuffer *txtbuff, size_t len, size_t blocksize)
78 {
79  txtbuff->buffsize = (len + 1) + (blocksize - (len + 1)%blocksize);
80  txtbuff->string = (unsigned char *)te_alloc (txtbuff->buffsize);
81  txtbuff->blocksize = blocksize;
82  //txtbuff->txtlen = len;
83  return txtbuff->string;
84 }
85 
86 static inline void* testr_realloc (StrBuffer *txtbuff, size_t len)
87 {
88  if (len + 1 > (size_t)txtbuff->buffsize ||
89  len + 1 < (size_t)(txtbuff->buffsize - txtbuff->blocksize)) {
90  /* really realloc */
91  txtbuff->buffsize = (len + 1) + (txtbuff->blocksize -
92  (len + 1)%txtbuff->blocksize);
93  txtbuff->string = (unsigned char *)te_realloc (txtbuff->string, txtbuff->buffsize);
94  }
95 
96  return txtbuff->string;
97 }
98 
99 static inline void testr_free (StrBuffer *txtbuff)
100 {
101  if (txtbuff) {
102  te_free (txtbuff->string);
103  }
104 }
105 
106 static inline void testr_setstr (StrBuffer *txtbuff, const char *newstring, unsigned int len)
107 {
108  int datalen;
109 
110  if (len < 0 && !newstring) return;
111 
112  datalen = MIN(len, txtbuff->buffsize-1);
113  memcpy (txtbuff->string, newstring, datalen);
114  txtbuff->string[datalen] = '\0';
115  txtbuff->txtlen = datalen;
116 }
117 
118 /* copy a string buffer */
119 static inline StrBuffer* testr_copy (StrBuffer *des, StrBuffer *src)
120 {
121  if (!des || !src)
122  return NULL;
123 
124  testr_free (des);
125  testr_alloc (des, src->txtlen, des->blocksize);
126  testr_setstr (des, (const char *)src->string, src->txtlen);
127  return des;
128 }
129 
130 /* duplicates a string buffer */
131 static inline StrBuffer* testr_dup (StrBuffer *s1)
132 {
133  StrBuffer *s2;
134 
135  if (!s1)
136  return NULL;
137 
138  s2 = (StrBuffer *)malloc (sizeof(s1));
139  if (s2) {
140  testr_alloc (s2, s1->txtlen, s1->blocksize);
141  testr_setstr (s2, (const char *)s1->string, s1->txtlen);
142  }
143  return s2;
144 }
145 
146 /* duplicates a string buffer */
147 static inline StrBuffer* testr_dup2 (StrBuffer *s2, StrBuffer *s1)
148 {
149  if (!s1 || !s2)
150  return NULL;
151 
152  testr_alloc (s2, s1->txtlen, s1->blocksize);
153  testr_setstr (s2, (const char *)s1->string, s1->txtlen);
154  return s2;
155 }
156 
157 /* -------------------------------------------------------------------------- */
158 
159 
165 /*
166  * \def NCSCTRL_EDIT
167  * \brief the name of edit control
168 */
169 #define NCSCTRL_EDIT NCSCLASSNAME("edit")
170 
171 #ifdef _MGNCS_USE_DBEDIT
172 #define DOUBLE_DC HDC db_dc;
173 #else
174 #define DOUBLE_DC
175 #endif
176 
177 typedef struct _mEdit mEdit;
178 typedef struct _mEditClass mEditClass;
179 typedef struct _mEditRenderer mEditRenderer;
180 
181 #define mEditHeader(Class) \
182  mScrollViewHeader(Class) \
183  DOUBLE_DC
184 
191 struct _mEdit
192 {
193  mEditHeader(mEdit)
194 };
195 
196 
197 typedef struct _TextAttribute
198 {
199  struct _TextAttribute* (*clone)(void);
200  void (*destroy)(void);
201 }TextAttribute;
202 
203 typedef struct _PasteInfo {
204  char *str;
205  int len;
206  TextAttribute *attr;
207  void *user_info;
208  struct _PasteInfo *next;
209 }PasteInfo;
210 
211 typedef struct _TextCopyPaste
212 {
213  void (*addCopyInfo)(const char* str, int len, TextAttribute *atrr, void *user_info);
214  void (*endCopyInfo)(void);
215  BOOL (*isEmpty)(void);
216  PasteInfo* (*getPasteInfo)(void);
217 }TextCopyPaste;
218 
219 typedef struct _UndoRedoInfo
220 {
221  char *text;
222  int start;
223  int end;
224  TextAttribute *attr;
225  void *user_data;
226  struct _UndoRedoInfo * next;
227 } UndoRedoInfo;
228 
229 typedef struct _TextUndoRedoInfo
230 {
231  BOOL (*pushText)(const char* text, int start, int end, TextAttribute *attr, void *user_data);
232  BOOL (*endPush)(void);
233 
234  UndoRedoInfo* (*getUndoTop)(void);
235  UndoRedoInfo* (*getRedoTop)(void);
236 
237  void (*popUndo)(void);
238  void (*popRedo)(void);
239 
240  BOOL (*isUndoEmpty)(void);
241  BOOL (*isRedoEmpty)(void);
242 
243  void (*empty)(void);
244 }TextUndoRedoInfo;
245 
246 #define mEditClassHeader(clsName, parentClass) \
247  mScrollViewClassHeader(clsName, parentClass) \
248  int (*onChar)(clsName* self, int asciiCode, DWORD keyFlags); \
249  int (*setContent)(clsName *self, const char* str, int start, int len); \
250  void (*replaceText)(clsName *self, const char* str, int start, int len, int replaceStart, int replaceEnd); \
251  void (*insert)(clsName *self, const char* str, int start, int len, int insertStart); \
252  void (*append)(clsName *self, const char* str, int start, int len); \
253  int (*getTextLength)(clsName *self); \
254  int (*getContent)(clsName *self, char *strBuff, int bufLen, int start, int end); \
255  int (*setTextAttr)(clsName *self, int start, int end, TextAttribute *attr); \
256  TextAttribute* (*getTextAttr)(clsName *self, int start, int end, int *pEnd); \
257  int (*setSel)(clsName *self, int start, int end); \
258  int (*getSel)(clsName *self, int *pStart, int *pEnd); \
259  void (*setMargin)(clsName *self, int left, int top, int right, int bottom); \
260  void (*copy)(clsName *self); \
261  void (*cut)(clsName *self); \
262  void (*paste)(clsName *self); \
263  TextCopyPaste * (*setCopyPaste)(clsName *self, TextCopyPaste* cp); \
264  void (*undo)(clsName *self); \
265  void (*redo)(clsName *self); \
266  TextUndoRedoInfo * (*setUndoRedo)(clsName *self, TextUndoRedoInfo* ur); \
267  BOOL (*makevisible)(clsName *self, int pos);
268 
372 struct _mEditClass
373 {
374  mEditClassHeader(mEdit, mScrollView)
375 };
376 
383 MGNCS_EXPORT extern mEditClass g_stmEditCls;
384 
385 
386 #define mEditRendererHeader(clsName, parentClass) \
387  mScrollViewRendererHeader(clsName, parentClass)
388 
395 struct _mEditRenderer
396 {
397  mEditRendererHeader(mEdit, mScrollView)
398 };
399 
404 #define NCSS_EDIT_LEFT (0x0000L<<NCSS_SCRLV_SHIFT)
405 
410 #define NCSS_EDIT_CENTER (0x0001L<<NCSS_SCRLV_SHIFT)
411 
416 #define NCSS_EDIT_RIGHT (0x0002L<<NCSS_SCRLV_SHIFT)
417 
422 #define NCSS_EDIT_UPPERCASE (0x0004L<<NCSS_SCRLV_SHIFT)
423 
428 #define NCSS_EDIT_LOWERCASE (0x0008L<<NCSS_SCRLV_SHIFT)
429 
434 #define NCSS_EDIT_NOHIDESEL (0x0010L<<NCSS_SCRLV_SHIFT)
435 
440 #define NCSS_EDIT_READONLY (0x0020L<<NCSS_SCRLV_SHIFT)
441 
446 #define NCSS_EDIT_BASELINE (0x0040L<<NCSS_SCRLV_SHIFT)
447 
448 #define NCSS_EDIT_SHIFT (NCSS_SCRLV_SHIFT+8)
449 
450 
451 
457 {
472  NCSP_EDIT_MAX
473 };
474 
475 
478 {
486  NCSN_EDIT_MAX
487 };
488 
489 #ifdef _MGNCS_USE_DBEDIT
490 static inline HDC mEdit_getDC(mEdit *self)
491 {
492  if (self->db_dc) {
493  SelectClipRect(self->db_dc, NULL);
494  SelectFont(self->db_dc, GetWindowFont(self->hwnd));
495  return self->db_dc;
496  }
497  else
498  return GetClientDC(self->hwnd);
499 }
500 
501 static inline void mEdit_releaseDC(mEdit *self, HDC hdc)
502 {
503  if (self->db_dc == 0 || self->db_dc != hdc)
504  ReleaseDC(hdc);
505 }
506 #else
507 static inline HDC mEdit_getDC(mEdit *self)
508 {
509  return GetClientDC(self->hwnd);
510 }
511 
512 static inline void mEdit_releaseDC(mEdit *self, HDC hdc)
513 {
514  ReleaseDC(hdc);
515 }
516 #endif
517 #define GETDC(self) mEdit_getDC((mEdit*)self)
518 #define RELEASEDC(self, hdc) mEdit_releaseDC((mEdit*)self, hdc)
519 
522 #ifdef __cplusplus
523 }
524 #endif /* __cplusplus */
525 
526 #endif /* _MGUI_NCSCTRL_EDIT_H */
527 
528 
NCSN_EDIT_UPDATE
@ NCSN_EDIT_UPDATE
Definition: medit.h:483
g_stmEditCls
MGNCS_EXPORT mEditClass g_stmEditCls
the instance of mEditClass
NCSP_SCRLV_MAX
@ NCSP_SCRLV_MAX
Definition: mscrollview.h:166
mEditClass
the VTable of mEdit, derived from mScrollViewClass.
mScrollView
The structure of mScrollView control, which derived from mItemView.
NCSN_ITEMV_SETFOCUS
@ NCSN_ITEMV_SETFOCUS
Definition: mitemview.h:605
NCSN_EDIT_SELCHANGED
@ NCSN_EDIT_SELCHANGED
Definition: medit.h:484
NCSP_EDIT_LIMITTEXT
@ NCSP_EDIT_LIMITTEXT
Get/Set the maximum length of text.
Definition: medit.h:464
NCSP_EDIT_CARETPOS
@ NCSP_EDIT_CARETPOS
Get/Set the position of charactor.
Definition: medit.h:471
mEditProp
mEditProp
the Property of edit
Definition: medit.h:456
NCSN_EDIT_KILLFOCUS
@ NCSN_EDIT_KILLFOCUS
Definition: medit.h:480
mEditRenderer
Edit class's Renderer interface, derived from mScrollViewRenderer.
mEditNotify
mEditNotify
Definition: medit.h:477
NCSN_EDIT_CONTCHANGED
@ NCSN_EDIT_CONTCHANGED
Definition: medit.h:482
NCSN_EDIT_CHANGE
@ NCSN_EDIT_CHANGE
Definition: medit.h:481
NCSN_SCRLV_MAX
@ NCSN_SCRLV_MAX
Definition: mscrollview.h:184
NCSN_EDIT_MAXTEXT
@ NCSN_EDIT_MAXTEXT
Definition: medit.h:485
NCSN_ITEMV_KILLFOCUS
@ NCSN_ITEMV_KILLFOCUS
Definition: mitemview.h:610
NCSN_EDIT_SETFOCUS
@ NCSN_EDIT_SETFOCUS
Definition: medit.h:479
mEdit
the edit struct of edit control, derived from mScrollView.