Originally, I wanted to create a custom dialog box control with an input box. I started with the idea of inheriting cakndilog, but later I found that the CBA of the dialog box was not good when I did this, although it can be hidden by means, when CBA is hidden at that time, the buttons in the dialog box are not easy to handle, so I thought of a replacement method.
Control inherits from ccoecontrol. It also inherits from makneditingstateindicator and makneditingstateindicator to get the current input status of the editing box. Check the SDK for specific functions. Not much nonsense. Post code first and talk about ideas.
. H file
/*
========================================================== ==========================================
Name: newdilog. h
Author:
Version: Barrett
Copyright: Your copyright notice
Description: cnewdilog Declaration
========================================================== ==========================================
*/
# Ifndef _ newdilog_h __
# DEFINE _ newdilog_h __
// Des
# Include <e32std. h>
# Include <eikedwin. h>
# Include <akneditstateindicator. h> // For makneditingstateindicator
# Include "meventobserver. H"
// Class declaration
Class cclienteventexample;
/**
* Cnewdilog
*
*/
Class cnewdilog: Public ccoecontrol, public makneditingstateindicator
{
Public: // constructors and destructor
Enum einputmode // custom enumeration, used to save the input mode for easy identification
{
Eunknown,
Eabc,
Eabc,
Eabc,
E123,
Epinyin,
Ebihua
};
/**
* Destructor.
*/
~ Cnewdilog ();
/**
* Two-phased constructor.
*/
Static cnewdilog * newl (minputdilogobserver & aobserver, ccoecontrol * aparent );
/**
* Two-phased constructor.
*/
Static cnewdilog * newlc (minputdilogobserver & aobserver, ccoecontrol * aparent );
Tkeyresponse offerkeyeventl (const tkeyevent & akeyevent, teventcode Atype );
PRIVATE:
/**
* Constructor for Ming 1st Stage Construction
*/
Cnewdilog (minputdilogobserver & aobserver );
/**
* EPOC default constructor for faster Ming 2nd Stage Construction
*/
Void constructl (ccoecontrol * aparent );
Void draw (const trect & arect) const;
Void setstate (takneditingstate astate); // This function and the following function are two pure virtual methods of makneditingstateindicator.
Caknindicatorcontainer * indicatorcontainer ();
Ceikedwin * ieditor;
Tint countcomponentcontrols () const;
Ccoecontrol * componentcontrol (tint aindex) const;
PRIVATE:
Trect irect;
Trect ieditorrect;
Minputdilogobserver & iobserver;
// Save the editor status
Tbool istatus;
Ccoecontrol * iparent;
// Input status
Einputmode icurinputmode;
// Name of the input state
Tbuf <8> icurmodename;
// Obtain the input status
Void getinputmode ();
Public:
Void makevisible (tbool avisible );
Void setdilogrect (trect arect, trect aeditorrect );
Tbool isfocus ();
Void convgbk2uni (tdesc8 & original, TDES & res );
};
Struct s_uid: Public tuid
{
S_uid (int I)
{
Iuid = I;
}
};
# Endif/_ newdilog_h __
Control Observer meventobserver. h file
# Ifndef _ meventobserver_h __
# DEFINE _ meventobserver_h __
/*************************************** ********************
* Observer in the input dialog box
**************************************** *******************/
Class minputdilogobserver
{
Public:
Virtual void getcontent (tdesc & atext, tbool AStatus) = 0;
};
# Endif
// End of File
. Cpp File
/*
========================================================== ==========================================
Name: newdilog. cpp
Author:
Version: Barrett
Copyright: Your copyright notice
Description: cnewdilog implementation
========================================================== ==========================================
*/
# Include "newdilog. H"
# Include <eikenv. h>
# Include <aknenv. h>
# Include <aknindicatorcontainer. h>
# Include <avkon. HRH>
# Include <charconv. h>
# Include "inputdilog. H"
Cnewdilog: cnewdilog (minputdilogobserver & aobserver): iobserver (aobserver)
{
// No implementation required
Istatus = etrue;
}
Cnewdilog ::~ Cnewdilog ()
{
Delete ieditor;
}
Cnewdilog * cnewdilog: newlc (minputdilogobserver & aobserver, ccoecontrol * aparent)
{
Cnewdilog * Self = new (eleave) cnewdilog (aobserver );
Cleanupstack: pushl (Self );
Self-> constructl (aparent );
Return self;
}
Cnewdilog * cnewdilog: newl (minputdilogobserver & aobserver, ccoecontrol * aparent)
{
Cnewdilog * Self = cnewdilog: newlc (aobserver, aparent );
Cleanupstack: Pop (); // self;
Return self;
}
Void cnewdilog: constructl (ccoecontrol * aparent)
{
// Irect = trect (tpoint (0,160), tsize (240,160 ));
Icurmodename. Copy (_ L ("ABC "));
Iparent = aparent;
// Ceikedwin: constructl (eakneditorflagdefault | eeikedwinnoautoselection | eeikedwinjustautocurend, 0, 10, 1); // call the second-segment constructor of the system control.
}
Tkeyresponse cnewdilog: offerkeyeventl (const tkeyevent & akeyevent, teventcode Atype)
{
Getinputmode ();
// If (Atype = eeventkey & akeyevent. iscancode = estdkeydevice0)
//{
// Tbuf <100> textbuf;
// Ieditor-> gettext (textbuf );
// Iobserver. getcontent (textbuf, etrue );
//}
// Else if (Atype = eeventkey & akeyevent. iscancode = estdkeydevice1)
//{
// Tbuf <100> textbuf;
// Ieditor-> gettext (textbuf );
// Iobserver. getcontent (textbuf, efalse );
//}
// Else
//{
Ieditor-> offerkeyeventl (akeyevent, Atype );
//}
Return ieditor-> offerkeyeventl (akeyevent, Atype );
}
Void cnewdilog: Draw (const trect & arect) const
{
Cwindowgc & GC = systemgc ();
If (istatus) // whether to draw controls
{
GC. setbrushstyle (cgraphicscontext: esolidbrush );
GC. setbrushcolor (krgbgreen );
GC. drawrect (irect); // background color
GC. setbrushcolor (krgbwhite );
GC. drawrect (trect (tpoint (ieditorrect. ITL. the iX-1, ieditorrect. ITL. iY-1), tsize (ieditorrect. width () + 2, ieditorrect. height () + 2); // draw an editor rectangle
GC. setbrushstyle (cgraphicscontext: enullbrush );
Const cfont * font = ceikonenv: static ()-> densefont ();
GC. usefont (font );
GC. setpencolor (krgbblack );
GC. drawtext (icurmodename, tpoint (ieditorrect. ITL. IX, ieditorrect. ITL. Iy + font-> heightinpixels () + ieditorrect. Height ()));
GC. discardfont ();
Ieditor-> textview ()-> drawl (ieditorrect); // when the control is refreshed, it is possible that none of the preceding items are displayed,
}
}
Void cnewdilog: makevisible (tbool avisible)
{
Istatus = avisible;
Ieditor-> setfocus (avisible );
Ieditor-> makevisible (avisible );
}
Void cnewdilog: setdilogrect (trect arect, trect aeditorrect)
{
Irect = arect;
Ieditorrect = aeditorrect;
Ieditor = new (eleave) ceikedwin;
Ieditor-> setcontainer1_wl (* iparent );
Ieditor-> constructl (eakneditorflagdefault | eeikedwinnoautoselection | eeikedwinjustautocurend, 0,100, 1); // call the second-segment constructor of the system control
Ieditor-> setextent (ieditorrect. ITL, ieditorrect. Size ());
Ieditor-> makevisible (efalse );
}
Tbool cnewdilog: isfocus ()
{
Return istatus;
}
Void cnewdilog: setstate (takneditingstate astate)
{
}
Caknindicatorcontainer * cnewdilog: indicatorcontainer ()
{
Return NULL;
}
Void cnewdilog: getinputmode ()
{
Makneditingstateindicator * Ei = caknenv: static ()-> editingstateindicator ();
If (! EI) return;
Caknindicatorcontainer * Ic = EI-> indicatorcontainer ();
If (! Ic) return;
Icurmodename. Zero ();
If (IC-> indicatorstate (s_uid (eaknnavipaneeditorindicatorpinyin )))
{
Icurinputmode = epinyin;
Tbuf8 <20> msg8;
Msg8.format (_ L8 ("pinyin "));
Convgbk2uni (msg8, icurmodename );
}
If (IC-> indicatorstate (s_uid (eaknnavipaneeditorindicatorstroke )))
{
Icurinputmode = ebihua;
Tbuf8 <20> msg8;
Msg8.format (_ L8 ("strokes "));
Convgbk2uni (msg8, icurmodename );
}
Else if (IC-> indicatorstate (s_uid (eaknnavipaneeditorindicatorlowercase )))
{
Icurinputmode = eabc;
Icurmodename. Copy (_ L ("ABC "));
}
Else if (IC-> indicatorstate (s_uid (eaknnavipaneeditorindicatoruppercase )))
{
Icurinputmode = eabc;
Icurmodename. Copy (_ L ("ABC "));
}
Else if (IC-> indicatorstate (s_uid (eaknnavipaneeditorindicatortextcase )))
{
Icurinputmode = eabc;
Icurmodename. Copy (_ L ("ABC "));
}
Else if (IC-> indicatorstate (s_uid (eaknnavipaneeditorindicatornumbercase )))
{
Icurinputmode = e123;
Icurmodename. Copy (_ L ("123 "));
}
Drawnow ();
// Ceikedwin: drawnow ();
}
Void cnewdilog: convgbk2uni (tdesc8 & original, TDES & res)
{
// RFS afileserversession = ceikonenv: static ()-> fssession ();
RFS afileserversession;
Afileserversession. Connect ();
// Cleanupstack: Pop ();
Ccnvcharactersetconverter * converter = ccnvcharactersetconverter: newlc ();
If (converter-> preparetoconverttoorfroml (kcharactersetidentifiergbk, afileserversession )! = Ccnvcharactersetconverter: eavailable)
User: Leave (kerrnotsupported );
Tint state = ccnvcharactersetconverter: kstatedefault;
Tptrc8 STR (original );
Hbufc * iinfotext = hbufc: newl (Str. Length ());
Tptr16 PTR = iinfotext-> des ();
If (ccnvcharactersetconverter: eerrorillformedinput = converter-> converttounicode (PTR, STR, State ))
User: Leave (kerrargument );
Res. Zero ();
Res. Copy (PTR );
Afileserversession. Close ();
Cleanupstack: popanddestroy ();
Delete iinfotext;
}
Tint cnewdilog: countcomponentcontrols () const
{
// Return number of controls inside this container
Return 1;
// Return 0;
}
Ccoecontrol * cnewdilog: componentcontrol (tint aindex) const
{
Switch (aindex)
{
Case 0:
Return ieditor;
}
Return NULL;
}
Use in container:
1. In container. H, the iner must inherit the following observer code:
Class ccontainer: minputdilogobserver
2. Implement the virtual method of minputdilogobserver
Void getcontent (tdesc & atext, tbool AStatus );
At this time, we can directly process the value and status returned by the returned control in this function.
3. Define a cnewdilog * inewdilog; Member
4. Initialization
Inewdilog = cnewdilog: newl (* This, this );
Inewdilog-> setcontainer1_wl (* This );
Inewdilog-> setdilogrect (trect (tpoint (0,160), tsize (240,160), trect (tpoint (50,235), tsize )));
Inewdilog-> setextent (tpoint (0, 0), tsize (240,320 ));
Inewdilog-> makevisible (etrue );
5. countcomponentcontrols plus 1 and componentcontrol return control pointer
6. Call the control's offerkeyeventl function in the offerkeyeventl function to pass the key value
7. Write the release function in the destructor
8. Compile and run the program.
Conclusion: The makevisible function can be used to display and hide controls. At the same time, when you click confirm or cancel, the status and data are returned to the iner using the control through the observer iobserver. The idea is still relatively simple. However, there are indeed many problems ~~
Topic questions:
1. Obtain the input method. ABC and ABC status are not recognized. At that time, the tint ntemp = akneditorcurrentinputmode () method of ceikedwin was used directly.
This method returns 1 in the ABC, ABC, and ABC statuses.
2. Click Cancel when selecting Chinese text during the Chinese input method. If this is not the case, close the control or cancel the selection of Chinese characters. After a long attempt, it is found that when you click to cancel the selection of Chinese characters, the control's offerkeyeventl function will go only once, that is, only eeventkeydown, therefore, you can determine whether to disable the control or select Chinese characters. For example, you can use the Atype = eeventkey event in the code to determine whether to disable the control.
4. ieditor-> textview ()-> drawl (ieditorrect); use of this function. It is used to solve the problem that the content entered earlier may not be displayed when the control is refreshed.
5. The function of the composite control is similar to that of a container. When adding a control to a control, if the container is the same, you need to write the countcomponentcontrols and componentcontrol functions. Constructl of a composite control also has a window or a window with a shared iner, just like a common Composite Control. In this article, we use a window that shares the iner.
I did not test it on my mobile phone yesterday. I tested it in a few days and found that the Pinyin and strokes cannot be displayed. Today I am modifying the code to change the conditions for determining the Pinyin and strokes to the following code.
If (IC-> indicatorstate (s_uid (eaknnavipaneeditorindicatorpinyin) | IC-> indicatorstate (s_uid (eaknnavipaneeditorindicatorpinyinphrase )))
{
Icurinputmode = epinyin;
Tbuf8 <20> msg8;
Msg8.format (_ L8 ("pinyin "));
Convgbk2uni (msg8, icurmodename );
// Icurmodename. Copy (_ L ("pinyin "));
}
If (IC-> indicatorstate (s_uid (eaknnavipaneeditorindicatorstroke) | IC-> indicatorstate (s_uid (eaknnavipaneeditorindicatorstrokephrase )))
{
Icurinputmode = ebihua;
Tbuf8 <20> msg8;
Msg8.format (_ L8 ("strokes "));
Convgbk2uni (msg8, icurmodename );
// Icurmodename. Copy (_ L ("bihua "));
}