TRichEdit

Post Reply
User avatar
ryugarai27
Posts: 65
Joined: Fri Feb 13, 2009 12:03 pm
Location: Manila, Philippines
Contact:

TRichEdit

Post by ryugarai27 »

Hi Fivewin Experts,

In TRichEdit object, Is there a function or a way to determine if a character or word ( where the cursor at or selected ) is in subscript or superscript mode?

Im trying to read an RTF file and I want to get only the letters or word with font effects ( such as subscript, superscript, italic, etc.. )

Hope somebody could help me figure out..

Thanks. More power!


ryugarai27
StefanHaupt
Posts: 824
Joined: Thu Oct 13, 2005 7:39 am
Location: Germany

Re: TRichEdit

Post by StefanHaupt »

Hi ryugarai27,

to get the format of the selected text

Code: Select all | Expand



aFormat := REGetCharFormat2 (hWnd)
lSubScript := aFormat[1] > 0
lSuperScript := aFormat[2] > 0
lCapital := aFormat[3] > 0

#pragma BEGINDUMP

#include <Windows.h>
#include <HbApi.h>
#include <Richedit.h>

HB_FUNC ( REGETCHARFORMAT2)

{
  CHARFORMAT2 cf;
  //DWORD lResult;

  cf.cbSize = sizeof (cf);
  cf.dwMask = CFM_SUBSCRIPT | CFM_SUPERSCRIPT | CFM_ALLCAPS;
  cf.dwEffects = CFE_SUBSCRIPT | CFE_SUPERSCRIPT | CFE_ALLCAPS;

  SendMessage( ( HWND ) hb_parnl( 1 ), EM_GETCHARFORMAT, SCF_SELECTION, ( LPARAM ) &cf );

  hb_reta (3);

  hb_storni (cf.dwEffects & CFE_SUBSCRIPT, -1, 1);
  hb_storni (cf.dwEffects & CFE_SUPERSCRIPT, -1, 2);
  hb_storni (cf.dwEffects & CFE_ALLCAPS, -1, 3);

}
#pragma ENDDUMP
 
kind regards
Stefan
User avatar
ryugarai27
Posts: 65
Joined: Fri Feb 13, 2009 12:03 pm
Location: Manila, Philippines
Contact:

Re: TRichEdit

Post by ryugarai27 »

Hi Stefan,

Thanks for your help.

I tried your code using the sample "testrich.prg" included in FWH\samples directory. However, REGetFormat2() always return the same result
no matter if the selection has font effects (sup/sub) or has no font effects. Could it be that I'm using it incorrectly?

Below is the source code I tested: ( Also, how can I get other font effects like italic, bold, underline? ).

// FWH and FW++ RichEdit sample

#include "FiveWin.ch"
#include "Constant.ch"
#include "WColors.ch"
#include "RichEdit.ch"

//----------------------------------------------------------------------------//

function Main()

local oDlg, oRich
local hRichDLL := LoadLibrary( "riched20.dll" )
local lSyntaxHL := .f.

DEFINE DIALOG oDlg NAME "Test"

oRich = TRichEdit():Redefine( 100, { || "" }, oDlg )
oRich:lHighLight = .f.

REDEFINE BUTTON ID 110 ;
ACTION oRich:SetText( MemoRead( "TestRich.prg" ) )

REDEFINE CHECKBOX lSyntaxHL ID 115 OF oDlg ;
ON CHANGE ( oRich:lHighLight := lSyntaxHL,;
oRich:SetText( oRich:GetText() ) )

REDEFINE BUTTON ID 120 ;
ACTION oRich:LoadFromRTFFile( cGetFile( "RTF file (*.rtf) | *.rtf" ) )

REDEFINE BUTTON ID 130 ;
ACTION oRich:SaveToRTFFile( cGetFile( "RTF file (*.rtf) | *.rtf",;
"Please write a filename", "test" ) )

REDEFINE BUTTON ID 998 ACTION ( Test( oRich, oDlg ) )

ACTIVATE DIALOG oDlg CENTERED ;
// ON INIT oRich:AutoURLDetect( .t. )

FreeLibrary( hRichDLL )



return nil

//----------------------------------------------------------------------------//

function Test( oRich, oDlg )
LOCAL cStr, aSel := {}, nC
local aFormat
local lSubScript
local lSuperScript
local lCapital

IF oRich:IsSelection()
aFormat := REGetCharFormat2 (oDlg)
lSubScript := aFormat[1] > 0
lSuperScript := aFormat[2] > 0
lCapital := aFormat[3] > 0
xBrowse( aFormat )
ELSE
Msginfo( 'Nothing selected! ')
END

RETURN ( nil )

//----------------------------------------------------------------------------//

#pragma BEGINDUMP

#include <Windows.h>
#include <HbApi.h>
#include <Richedit.h>

HB_FUNC ( REGETCHARFORMAT2)

{
CHARFORMAT2 cf;
//DWORD lResult;

cf.cbSize = sizeof (cf);
cf.dwMask = CFM_SUBSCRIPT | CFM_SUPERSCRIPT | CFM_ALLCAPS;
cf.dwEffects = CFE_SUBSCRIPT | CFE_SUPERSCRIPT | CFE_ALLCAPS;

SendMessage( ( HWND ) hb_parnl( 1 ), EM_GETCHARFORMAT, SCF_SELECTION, ( LPARAM ) &cf );

hb_reta (3);

hb_storni (cf.dwEffects & CFE_SUBSCRIPT, -1, 1);
hb_storni (cf.dwEffects & CFE_SUPERSCRIPT, -1, 2);
hb_storni (cf.dwEffects & CFE_ALLCAPS, -1, 3);

}
#pragma ENDDUMP




Thanks...

Kind regards,
ryugarai27
StefanHaupt
Posts: 824
Joined: Thu Oct 13, 2005 7:39 am
Location: Germany

Re: TRichEdit

Post by StefanHaupt »

Hi ryugarai27,

I cannot test your sample, your .rc file is missing
kind regards
Stefan
User avatar
ryugarai27
Posts: 65
Joined: Fri Feb 13, 2009 12:03 pm
Location: Manila, Philippines
Contact:

Re: TRichEdit

Post by ryugarai27 »

Hi Stefan,

Sorry, I forgot to include the file. Below is the .rc file I used during testing:

#include "..\include\winapi.ch"

#define IDC_EDIT1 101
Test DIALOG 41, 64, 409, 199
STYLE DS_MODALFRAME | 0x4L | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "Testing the RichEdit control"
FONT 8, "MS Sans Serif"
{
CONTROL "", 100, "RichEdit20A", 4100 | WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | WS_TABSTOP, 6, 12, 398, 163
PUSHBUTTON "&Load text file", 110, 5, 179, 50, 16
CHECKBOX "Activate syntax highlight", 115, 65, 182, 92, 11, BS_AUTOCHECKBOX | WS_TABSTOP
PUSHBUTTON "L&oad RTF file", 120, 162, 179, 50, 16
PUSHBUTTON "&Save RTF file", 130, 226, 179, 50, 16
PUSHBUTTON "&End", IDCANCEL, 290, 179, 50, 16
PUSHBUTTON "&Test", 998, 354, 179, 50, 16
}

I also tried calling the funciton as :
aFormat := REGetCharFormat2 (oDlg:hWnd)

but same result was returned, aFormat having values:
aFormat[1] := 65536
aFormat[2] := 131072
aFormat[3] := 128

regardless if the selection has or has no font effects...


Kind regards,
ryugarai27
StefanHaupt
Posts: 824
Joined: Thu Oct 13, 2005 7:39 am
Location: Germany

Re: TRichEdit

Post by StefanHaupt »

Hi ryugarai27,

change your code i this way:

aFormat := REGetCharFormat2 (oRich:hWnd)

If you click on the words, the format in the msgbar ist updated

Updated sample (see GetFormat () to check bold and underline)

Code: Select all | Expand

// FWH and FW++ RichEdit sample

#include "FiveWin.ch"
#include "Constant.ch"
#include "WColors.ch"
#include "RichEdit.ch"

STATIC oMsg, oLen, oRow, oCol

//----------------------------------------------------------------------------//

function Main()

local oDlg, oRich
local hRichDLL := LoadLibrary( "riched20.dll" )
local lSyntaxHL := .f.
local aF, aF2

DEFINE DIALOG oDlg NAME "Test"

oRich = TRichEdit():Redefine( 100, { || "" }, oDlg )
oRich:lHighLight = .f.

REDEFINE BUTTON ID 110 ;
ACTION oRich:SetText( MemoRead( "TestRich.prg" ) )

REDEFINE CHECKBOX lSyntaxHL ID 115 OF oDlg ;
ON CHANGE ( oRich:lHighLight := lSyntaxHL,;
oRich:SetText( oRich:GetText() ) )

REDEFINE BUTTON ID 120 ;
ACTION oRich:LoadFromRTFFile( cGetFile( "RTF file (*.rtf) | *.rtf" ) )

REDEFINE BUTTON ID 130 ;
ACTION oRich:SaveToRTFFile( cGetFile( "RTF file (*.rtf) | *.rtf",;
"Please write a filename", "test" ) )

REDEFINE BUTTON ID 998 ACTION ( Test( oRich, oDlg ) )


 oRich:bChange := { || aF2 := REGetCharFormat2 (oRich:hWnd), ;
                       oLen:SetText( GetFormat2(aF2) )  }


ACTIVATE DIALOG oDlg CENTERED ON INIT BuildBar(oDlg);
// ON INIT oRich:AutoURLDetect( .t. )

FreeLibrary( hRichDLL )



return nil

//----------------------------------------------------------------------------//

function Test( oRich, oDlg )
LOCAL cStr, aSel := {}, nC
local aFormat
local lSubScript
local lSuperScript
local lCapital

IF oRich:IsSelection()
aFormat := REGetCharFormat2 (oRich:hWnd) // <--------
lSubScript := aFormat[1] > 0
lSuperScript := aFormat[2] > 0
lCapital := aFormat[3] > 0
xBrowse( aFormat )
ELSE
Msginfo( 'Nothing selected! ')
END

RETURN ( nil )

//----------------------------------------------------------------------------//
FUNCTION GetFormat2 (aF2)

LOCAL lSubScript := aF2[1] > 0
LOCAL lSuperScript := aF2[2] > 0
LOCAL lCapital := aF2[3] > 0
LOCAL cT := ""
DO CASE
CASE lSubScript
  cT := "SubScript"
CASE lSuperScript
  cT := "SuperScript"
CASE lCapital
  cT := "Capital"
OTHERWISE
  cT := "normal"
ENDCASE

RETURN (cT)

//----------------------------------------------------------------------------//
FUNCTION GetFormat (aF)

   LOCAL lBold      := (aF[5] == 700)  // Bold -> 700
   LOCAL lItalic    := aF[6]
   LOCAL lUnderline := aF[7]

RETURN (nil)

//----------------------------------------------------------------------------//
function BuildBar (oDlg)

LOCAL oMsgBar

   oMsgBar:=oDlg:oMsgBar := TMsgBar():New( oDlg, "TestRtf", .F., .F., .F., .F., , ,,, .t.)

   DEFINE MSGITEM oLen PROMPT "Format: " SIZE 80 OF oMsgBar
   DEFINE MSGITEM oRow PROMPT "Lin: 0" SIZE 60 OF oMsgBar
   DEFINE MSGITEM oCol PROMPT "Col: 0" SIZE 60 OF oMsgBar
     
RETURN (oDlg:oMsgBar)


//----------------------------------------------------------------------------//

#pragma BEGINDUMP

#include <Windows.h>
#include <HbApi.h>
#include <Richedit.h>

HB_FUNC ( REGETCHARFORMAT2)

{
CHARFORMAT2 cf;
//DWORD lResult;

cf.cbSize = sizeof (cf);
cf.dwMask = CFM_SUBSCRIPT | CFM_SUPERSCRIPT | CFM_ALLCAPS;
cf.dwEffects = CFE_SUBSCRIPT | CFE_SUPERSCRIPT | CFE_ALLCAPS;

SendMessage( ( HWND ) hb_parnl( 1 ), EM_GETCHARFORMAT, SCF_SELECTION, ( LPARAM ) &cf );

hb_reta (3);

hb_storni (cf.dwEffects & CFE_SUBSCRIPT, -1, 1);
hb_storni (cf.dwEffects & CFE_SUPERSCRIPT, -1, 2);
hb_storni (cf.dwEffects & CFE_ALLCAPS, -1, 3);

}
#pragma ENDDUMP
 
kind regards
Stefan
User avatar
ryugarai27
Posts: 65
Joined: Fri Feb 13, 2009 12:03 pm
Location: Manila, Philippines
Contact:

Re: TRichEdit

Post by ryugarai27 »

Hi Stefan,

Works great now! Thanks a lot!

Kind regards,
ryugarai27
Post Reply