Cross-platform EditorGadget missing procedures

Just starting out? Need help? Post your questions and find answers here.
User avatar
Erich
User
User
Posts: 49
Joined: Thu Sep 30, 2010 9:21 pm

Cross-platform EditorGadget missing procedures

Post by Erich »

Hi folks,

I'm porting a small application to OS X (on a somewhat painful virtual machine setup) and was wondering whether someone knows how to fill in the some of the missing parts in the code below to make the EditorGadget selection handling more cross-platform. Unfortunately, I have no clue about Cocoa. :| I'm posting the incomplete code (assembled from various posts here) because perhaps someone else might it useful.

Code: Select all

; ====================
; Editor Gadget Routines
; ====================


Procedure.i EditorGadgetGetSelectionStart(editorNum)
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      Define *buffer.GtkTextBuffer, startIter.GtkTextIter, endIter.GtkTextIter
      *buffer = gtk_text_view_get_buffer_(GadgetID(editorNum))
      If gtk_text_buffer_get_selection_bounds_(*buffer, @startIter, @endIter)
        Define startPos.i
        startPos=gtk_text_iter_get_offset_(@startIter)
        ProcedureReturn startPos
      Else
        ProcedureReturn -1
      EndIf
    CompilerCase #PB_OS_Windows
       Define startpos, endpos
       SendMessage_(GadgetID(editorNum), #EM_GETSEL, @startpos, @endpos)
      ProcedureReturn startpos
    CompilerCase #PB_OS_MacOS
      Debug "EditorGadgetGetSelectionStart() not implemented on Mac OS"
      ProcedureReturn -1
  CompilerEndSelect
EndProcedure

Procedure.i EditorGadgetGetSelectionEnd(editorNum)
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      Define *buffer.GtkTextBuffer, startIter.GtkTextIter, endIter.GtkTextIter
      *buffer = gtk_text_view_get_buffer_(GadgetID(editorNum))
      If gtk_text_buffer_get_selection_bounds_(*buffer, @startIter, @endIter)
        Define endPos.i
        endPos=gtk_text_iter_get_offset_(@endIter)
        ProcedureReturn endPos
      Else
        ProcedureReturn -1
      EndIf
    CompilerCase #PB_OS_Windows
      Define startpos, endpos
       SendMessage_(GadgetID(editorNum), #EM_GETSEL, @startpos, @endpos)
      ProcedureReturn endpos
    CompilerCase #PB_OS_MacOS
      Debug "EditorGadgetGetSelectionEnd() not implemented on Mac OS"
      ProcedureReturn -1
  CompilerEndSelect
EndProcedure

Procedure.i EditorGadgetGetSelectionLength(editornum)
  ProcedureReturn EditorGadgetGetSelectionEnd(editornum)-EditorGadgetGetSelectionStart(editorNum)
EndProcedure

Procedure EditorGadgetSetSelectionRange(editornum, rstart, rend)
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      Define *buffer.GtkTextBuffer, startIter.GtkTextIter, endIter.GtkTextIter
      *buffer = gtk_text_view_get_buffer_(GadgetID(editorNum))
      gtk_text_buffer_get_selection_bounds_(*buffer, @startIter, @endIter)
      gtk_text_iter_set_offset_(@startIter, rstart)
      gtk_text_iter_set_offset_(@endIter, rend)
      gtk_text_buffer_select_range_(*buffer, @startIter, @endIter)
    CompilerCase #PB_OS_Windows
      SendMessage_(GadgetID(editorNum),#EM_SETSEL,rstart,rend)
      SendMessage_(GadgetID(editorNum), #EM_SCROLLCARET, 0, 0)
    CompilerCase #PB_OS_MacOS
      Debug "EditorGadgetSetSelectionRange() not implemented on Mac OS"
      ProcedureReturn -1
  CompilerEndSelect
EndProcedure

Procedure.s EditorGadgetGetSelection(editorNum)
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      Define *buffer.GtkTextBuffer, startIter.GtkTextIter, endIter.GtkTextIter, *slice, result$
      *buffer = gtk_text_view_get_buffer_(GadgetID(editorNum))
      If gtk_text_buffer_get_selection_bounds_(*buffer, @startIter, @endIter)
        *slice = gtk_text_buffer_get_slice_(*buffer, @startIter, @endIter, 1)
        result$ = PeekS(*slice, -1, #PB_UTF8)
        g_free_(*slice)
        ProcedureReturn result$
      Else
        ProcedureReturn ""
      EndIf
    CompilerCase #PB_OS_Windows
      Define text$
      text$=Space(EditorGadgetGetSelectionLength(editorNum)+8)
      SendMessage_(GadgetID(editorNum),#EM_GETSELTEXT,0,@text$)
    ProcedureReturn text$
    CompilerCase #PB_OS_MacOS
      Debug "EditorGadgetGetSelection() not implemented on Mac OS"
      ProcedureReturn ""
  CompilerEndSelect
EndProcedure


Procedure.i EditorGadgetGetCursorAt(editorNum)
  ProcedureReturn EditorGadgetGetSelectionEnd(editorNum)
EndProcedure

Procedure EditorGadgetSetCursorAt(editorNum, pos)
  EditorGadgetSetSelectionRange(editorNum, pos, pos)
EndProcedure

Procedure EditorGadgetInsert(editorNum, text$, autoselect=#False)
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      Define *buffer.GtkTextBuffer, *buffer2, len, selStart
      selStart=EditorGadgetGetSelectionStart(editorNum)
      *buffer = gtk_text_view_get_buffer_(GadgetID(editorNum))
      gtk_text_buffer_delete_selection_ (*buffer, 0, 1)
      len=StringByteLength(text$, #PB_UTF8)
      *buffer2=AllocateMemory(len+2)
      PokeS(*buffer2, text$, len, #PB_UTF8)
      gtk_text_buffer_insert_at_cursor_(*buffer, *buffer2, len)
      If autoselect
        EditorGadgetSetSelectionRange(editorNum, selStart, selStart+len)
      Else
        SetActiveGadget(editorNum)
      EndIf
      ProcedureReturn #True
    CompilerCase #PB_OS_MacOS
      Debug "EditorGadgetInsert() not implemented on Mac OS"
    CompilerCase #PB_OS_Windows
      Define success, selstart
      If autoselect
        selStart=EditorGadgetGetSelectionStart(GadgetID(editorNum))
      EndIf
      success=SendMessage_(GadgetID(editorNum),#EM_REPLACESEL,0,@text$)
      If autoselect
        EditorGadgetSetSelectionRange(editorNum, selstart, Len(text$))
      EndIf
       ProcedureReturn success
  CompilerEndSelect
EndProcedure

Procedure EditorGadgetSelectAll(editorNum)
  EditorGadgetSetSelectionRange(editorNum, 0, Len(GetGadgetText(editorNum)))
EndProcedure

Procedure EditorGadgetDeselectAll(editorNum)
  EditorGadgetSetSelectionRange(editorNum, EditorGadgetGetSelectionEnd(editorNum), EditorGadgetGetSelectionEnd(editorNum))
EndProcedure

; Procedure EditorGadgetCopy(editorNum)
;   Define selection$
;   selection$=EditorGadgetGetSelection(editorNum)
;   If selection$<>""
;     SetClipboardText(selection$)
;   EndIf
; EndProcedure

; Procedure EditorGadgetPaste(editorNum)
;   Define selection$
;   selection$=GetClipboardText()
;   If selection$<>""
;     EditorGadgetInsert(editorNum, selection$)
;   EndIf
; EndProcedure

; Procedure EditorGadgetCut(editorNum)
;   EditorGadgetCopy(editorNum)
;   EditorGadgetInsert(editorNum, "")
; EndProcedure

Procedure EditorGadgetSetFont(editorNum, font_id)
  Define buff$
  If IsGadget(editorNum)
    buff$=GetGadgetText(editorNum)
    SetGadgetFont(editorNum, font_id)
    SetGadgetText(editornum, buff$)
  EndIf
EndProcedure

#PB_EventType_EditorChange=1024

Procedure EditorGadgetScrollToEnd(editorNum)
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_MacOS
      Protected lines, lastline$
      lines=CountGadgetItems(editorNum)
      lastline$=GetGadgetItemText(editorNum, lines)
      SetGadgetItemText(editorNum, lines, lastline$)
    CompilerCase #PB_OS_Linux
      Protected end_mark,*buffer, end_iter.GtkTextIter
      *buffer=gtk_text_view_get_buffer_(GadgetID(editorNum))
      gtk_text_buffer_get_end_iter_(*buffer,@end_iter)
      end_mark=gtk_text_buffer_create_mark_(*buffer,"end_mark",@end_iter,#False)
      gtk_text_view_scroll_mark_onscreen_(GadgetID(editorNum),end_mark)
    CompilerCase #PB_OS_Windows
      Protected range.CHARRANGE         ;instantiates 'range' as CHARRANGE structure
      range\cpMin=-1                    ;range element cpMin
      range\cpMax=-1                    ;range element cpMax
      SendMessage_(GadgetID(editorNum),#EM_EXSETSEL,0,@range)
  CompilerEndSelect
EndProcedure

Procedure EditorGadgetRegisterChangeCallback(editorNum, *callback)
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_MacOS
      Debug "EditorGadgetRegisterChangeCallback() not yet implemented!"
    CompilerCase #PB_OS_Windows
      Debug "EditorGadgetRegisterChangeCallback() not yet implemented!"
    CompilerCase #PB_OS_Linux
      Define *buff,*buff2,len
      Define *buffer.GtkTextBuffer
      *buffer = gtk_text_view_get_buffer_(GadgetID(editorNum))
      len=StringByteLength("insert-text", #PB_Ascii)
      *buff=AllocateMemory(len+2)
      PokeS(*buff, "insert-text", len, #PB_Ascii)
      g_signal_connect_(*buffer, *buff, *callback, #Null)
      len=StringByteLength("delete-range", #PB_Ascii)
      *buff2=AllocateMemory(len+2)
      PokeS(*buff, "delete-range", len, #PB_Ascii)
      g_signal_connect_(*buffer, *buff, *callback, #Null)  
      FreeMemory(*buff)
      FreeMemory(*buff2)
  CompilerEndSelect
EndProcedure

Procedure EditorGadgetUndo( editorNum )
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_MacOS
      Debug "EditorGadgetUndo() not yet implemented!"
    CompilerCase #PB_OS_Windows
      ProcedureReturn SendMessage_(GadgetID(editorNum), #EM_UNDO, 0, 0)
    CompilerCase #PB_OS_Linux
      Debug "EditorGadgetUndo() not implemented!"
  CompilerEndSelect
EndProcedure

Procedure EditorGadgetCanUndo( editorNum )
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_MacOS
      Debug "EditorGadgetUndo() not yet implemented!"
    CompilerCase #PB_OS_Windows
      ProcedureReturn SendMessage_(GadgetID(editorNum), #EM_CANUNDO, 0, 0)
    CompilerCase #PB_OS_Linux
      Debug "EditorGadgetUndo() not implemented!"
  CompilerEndSelect
EndProcedure

Procedure EditorGadgetRedo( editorNum )
   CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_MacOS
      Debug "EditorGadgetUndo() not yet implemented!"
    CompilerCase #PB_OS_Windows
      ProcedureReturn SendMessage_(GadgetID(editorNum), #EM_REDO, 0, 0)
    CompilerCase #PB_OS_Linux
      Debug "EditorGadgetUndo() not implemented!"
  CompilerEndSelect
EndProcedure

Procedure EditorGadgetCanRedo( editorNum )
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_MacOS
      Debug "EditorGadgetUndo() not yet implemented!"
    CompilerCase #PB_OS_Windows
      ProcedureReturn SendMessage_(GadgetID(editorNum), #EM_CANREDO, 0, 0)
    CompilerCase #PB_OS_Linux
      Debug "EditorGadgetUndo() not implemented!"
  CompilerEndSelect
EndProcedure
"I have never let my schooling interfere with my education." - Mark Twain
User avatar
Erich
User
User
Posts: 49
Joined: Thu Sep 30, 2010 9:21 pm

Re: Cross-platform EditorGadget missing procedures

Post by Erich »

Problem is, as a lazy programmer I was hoping someone could complete some of the missing stuff for me. :wink:

I guess I'll have to learn some Cocoa once I find the time for it...
"I have never let my schooling interfere with my education." - Mark Twain
Post Reply