Help: PopUp Menu using API

Everything else that doesn't fall into one of the other PB categories.
chen
Enthusiast
Enthusiast
Posts: 338
Joined: Fri Dec 23, 2005 2:20 pm
Location: Quebec, Canada
Contact:

Help: PopUp Menu using API

Post by chen »

Hi,
Im creating a simple PopUp menu... first I tried to use only PureBasic
commands but it seems that is imposible (at least for me :wink: ), then
I decided to use win API but new in this topic, I can not make run the simple one.

The problem I have: I cant copy selected text to the clipboard.... using
#EM_GETSELTEXT

Could you give me a hand??? :cry:

Code: Select all

Enumeration ;PopUp Menu
  #PopUp_0
EndEnumeration

Enumeration ; PopUp menu items
  #popCut
  #popCopy
  #popPaste
  #popSelectAll
  #popFind
EndEnumeration

Enumeration  ; Gadget where menu works
  #ed_file
EndEnumeration

If OpenWindow(0, 200, 200, 400, 400, #PB_Window_SystemMenu, "Popup-Menu")
  
  If CreatePopupMenu(#PopUp_0)   
    MenuItem(#popCut, "Cut"+Chr(9)+"Ctrl+X")  
    MenuItem(#popCopy, "Copy"+Chr(9)+"Ctrl+C")
    MenuItem(#popPaste, "Paste"+Chr(9)+"Ctrl+V")
    MenuBar()
    MenuItem(#popSelectAll, "Select All"+Chr(9)+"Ctrl+A")
    MenuBar()
    MenuItem(#popFind, "Find"+Chr(9)+"Ctrl+F")
    
  EndIf
  If CreateGadgetList(WindowID())
    EditorGadget(#ed_file, 1, 1, 390, 390)
  EndIf

  Repeat
    Event = WaitWindowEvent()        
    
    WindowID = EventWindowID() ; In what window or
    MenuID = EventMenuID()     ; In what menu item or: 
    GadgetID = EventGadgetID() ; In what gadget.: 
    EventType = EventType()    ; Type of event  for SOME gadgets (no menus, not ALL gadgets) 
    
    
    If Event = #WM_RBUTTONDOWN   ; I want to use PureBasic.. apparently there is no way...
      DisplayPopupMenu(#PopUp_0, WindowID())  ; display popup-menu
    EndIf
    If Event = #PB_Event_Menu
      Select MenuID   
        Case #popCut 
          ;"Menu: Cut"
        Case #popCopy 
          SendMessage_(GadgetID(#ed_file),#EM_GETSELTEXT,0,text$)  ; HERE IS THE PROBLEM
          MessageRequester("ccc",text$,#PB_MessageRequester_Ok)    ; to show if Im geting the text....
          SetClipboardText(text$)
        Case #popPaste 
          Text$ = GetClipboardText()
          AddGadgetItem(#ed_file, -1, Text$ )
        Case #popSelectAll 
          ; Select All"
        Case #popFind 
          ;"Menu: Find"
      EndSelect
    EndIf
  Until Event = #PB_Event_CloseWindow
EndIf

Thanks in advance
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Code: Select all

          text$ = ""
          SendMessage_(GadgetID(#ed_file), #EM_GETSELTEXT, 0, @text$)  ; HERE IS THE PROBLEM 
          MessageRequester("ccc", text$, #PB_MessageRequester_Ok)    ; to show if Im geting the text.... 
          SetClipboardText(text$) 
Refer to this:

Code: Select all

Debug @text$
text$ = ""
Debug @text$
Until you've used text$ it's a null pointer.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

The following should work. It checks the length of the selected text and subsequently reserves enough memory to accommodate the text etc. (Only works for a max selection of 64k. If the selection is likely to exceed this then use the #EM_EXGETSEL message instead).

Code: Select all

Case #popCopy 
;Get start and end positions of the selection.
          SendMessage_(GadgetID(#ed_file), #EM_GETSEL, @startsel, @endsel) 
;Reserve enough space (including the null terminator)
          text$=Space(endsel-startsel+1)
          SendMessage_(GadgetID(#ed_file),#EM_GETSELTEXT,0,text$)  ; HERE IS THE PROBLEM 
          MessageRequester("ccc",text$,#PB_MessageRequester_Ok)    ; to show if Im geting the text.... 
          SetClipboardText(text$) 
I may look like a mule, but I'm not a complete ass.
chen
Enthusiast
Enthusiast
Posts: 338
Joined: Fri Dec 23, 2005 2:20 pm
Location: Quebec, Canada
Contact:

Post by chen »

Thanks Guys......

Its working....... with your help.
Post Reply