Page 1 of 1

How do I improve the keyboard response for this code ?

Posted: Thu Oct 30, 2008 1:49 pm
by pureballs
It's a kind of HTML editor, just type html and hit CTRL-S and CTRL-E to switch between edit and html view.

Code: Select all

  
If OpenWindow(0, 0, 0, 600, 300, "CTRL-S for SHOW - CTRL-E for EDIT", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0))
    WebGadget(0, 0, 0, 600, 300, "")
    HideGadget(0,1)
    StringGadget(1,0,0,600,300,"",#PB_String_BorderLess | #ES_MULTILINE | #ES_AUTOHSCROLL | #ES_AUTOVSCROLL |  #ESB_DISABLE_LEFT| #ESB_DISABLE_RIGHT)
    SetActiveGadget(1)
    
    If LoadFont(1,"MS Sans Serif",12)
     SetGadgetFont(1, FontID(1))  
    EndIf
    
    AddKeyboardShortcut(0, #PB_Shortcut_Control | #PB_Shortcut_S, 1)
    AddKeyboardShortcut(0, #PB_Shortcut_Control | #PB_Shortcut_E, 2)
    
  Repeat 
  
  If WaitWindowEvent()= #PB_Event_Menu
      
      If EventMenu()=1
       HideGadget(1,1)
       HideGadget(0,0)
       SetActiveGadget(0)
          
       text$=GetGadgetText(1)
      
       new$="<html><body topmargin=0 leftmargin=0><font face='MS Sans Serif' size=3pc>"
       new$ + ReplaceString(text$,Chr(13)+Chr(10),"<br>")
       
       SetGadgetItemText(0,#PB_Web_HtmlCode,new$)
      EndIf
    
      If EventMenu()=2
        HideGadget(1,0)
        HideGadget(0,1)
        SetActiveGadget(1)
      EndIf
   
  EndIf
     
  Until WaitWindowEvent() = #PB_Event_CloseWindow
   
  EndIf : End
As you can see, you need to press 2 or 3 times to make the program respond, I tried delays at different spots, removed them as they didn't improve response at all.

Also, there's a difference between the EDIT and HTML screens, the HTML screen has (with little input disabled) vertical scrollbar, as the EDIT window has not. Hot to add the Vscrollbar for the EDIT window ?

Cheers.

Posted: Thu Oct 30, 2008 1:59 pm
by srod

Code: Select all

If OpenWindow(0, 0, 0, 600, 300, "CTRL-S for SHOW - CTRL-E for EDIT", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0)) 
    WebGadget(0, 0, 0, 600, 300, "") 
    HideGadget(0,1) 
    StringGadget(1,0,0,600,300,"",#PB_String_BorderLess | #ES_MULTILINE | #ES_AUTOHSCROLL | #ES_AUTOVSCROLL |  #ESB_DISABLE_LEFT| #ESB_DISABLE_RIGHT) 
    SetActiveGadget(1) 
    
    If LoadFont(1,"MS Sans Serif",12) 
     SetGadgetFont(1, FontID(1))  
    EndIf 
    
    AddKeyboardShortcut(0, #PB_Shortcut_Control | #PB_Shortcut_S, 1) 
    AddKeyboardShortcut(0, #PB_Shortcut_Control | #PB_Shortcut_E, 2) 
    
  Repeat 
    EventID = WaitWindowEvent()
    Select EventID
      Case #PB_Event_Menu 
        If EventMenu()=1 
          HideGadget(1,1) 
          HideGadget(0,0) 
          SetActiveGadget(0) 
          
          text$=GetGadgetText(1) 
      
          new$="<html><body topmargin=0 leftmargin=0><font face='MS Sans Serif' size=3pc>" 
          new$ + ReplaceString(text$,Chr(13)+Chr(10),"<br>") 
        
          SetGadgetItemText(0,#PB_Web_HtmlCode,new$) 
        ElseIf EventMenu()=2 
          HideGadget(1,0) 
          HideGadget(0,1) 
          SetActiveGadget(1) 
        EndIf 
    
  EndSelect 
      
  Until EventID = #PB_Event_CloseWindow 
    
  EndIf : End
Your problem was that you were using too many WaitWindowEvent()'s (a common problem!) and consequently some events were going unprocessed.

Posted: Thu Oct 30, 2008 1:59 pm
by Baldrick
Havent looked closely at ur code, but did notice you had 2 WaitwindowEvent()'s in ur loop, which is pretty much a bit of a no no.
Change ur loop to the below & see if it helps:

Code: Select all

  Repeat 
  
  Event.l= WaitWindowEvent() 
   If Event = #PB_Event_Menu 
    Select EventMenu() 
     Case 1 
      HideGadget(1,1) 
      HideGadget(0,0) 
      SetActiveGadget(0)  
      text$=GetGadgetText(1) 
      new$="<html><body topmargin=0 leftmargin=0><font face='MS Sans Serif' size=3pc>" 
      new$ + ReplaceString(text$,Chr(13)+Chr(10),"<br>")   
      SetGadgetItemText(0,#PB_Web_HtmlCode,new$)
     Case 2 
      HideGadget(1,0) 
      HideGadget(0,1) 
      SetActiveGadget(1)
    EndSelect 
   EndIf 
      
  Until Event = #PB_Event_CloseWindow 
Edit: Beaten by the srod again.. 8)

Posted: Thu Oct 30, 2008 2:00 pm
by srod
hehe!!! :)

Posted: Thu Oct 30, 2008 2:13 pm
by Kaeru Gaman
everytime I read something like
you need to press 2 or 3 times to make the program respond
I first look into the code if there is more than one (Wait)WindowEvent(),
and heck, they were there everytime.

Posted: Thu Oct 30, 2008 2:13 pm
by pureballs
Serious competition going on right here ! :D

Thanks... I knew it had to be the case select thing that I avoided from the start because I thought it would be faster !?!
What was I thinking ? :)

Now moving on to adding a Vscrollbar for the String Gadget... I have so many flags allready for that gadget, the more you have. the bigger the chance they do interact in an undesirable way.

Posted: Thu Oct 30, 2008 2:24 pm
by srod
Just add #WS_VSCROLL to your list of flags.

Posted: Thu Oct 30, 2008 2:28 pm
by pureballs
srod wrote:Just add #WS_VSCROLL to your list of flags.
thanks...

used the wrong (#PB) flag.