Making a MaxGUI Application: StripAnimMakerLite Part 11 - PopUp Menu, Cut, Paste and Clear
(c) Assari Feb 05 2006

Once we can select a frame, which we have just done in the previous tutorial, we can now start to add functionalities such as Cut, Paste and Clear

But a big pre-requiste to that is adding a pop up menu to execute these functions. So by the end of this part, our program will be able to

New Additions to Part 1 - Variable Definitions

Most menu items do not need global variables, but for the PasteMenuBefore and PasteMenuAfter menu items we are creating globals so that we can enable and disable them where appropriate. We also need a clipboard variable to store our cut&paste item
    Global ToolMenu:TGadget '----Menu Item
Global PasteMenuBefore:TGadget '----Menu Item
Global PasteMenuAfter:TGadget '----Menu Item
Global AnimClipboard:TList=Createlist()


New Additions to Part 2 - Gadget Creation Block

We do not need any new gadget but we do have to modify our CreateMenu function to add in these new menu items

New Additions to Part 3 - MAIN LOOP

We do not need anything new here

New Additions to Part 4 - Function Definitions

In this block we need to do the following
Function CreateMenus()

Note that we have disabled the PasteMenuAfter and PasteMenuBefore menus as pasting is only allowed when there is something in the Animclipboard (from a cut operation).
    Function CreateMenus:Int()

  Local FileMenu:TGadget=CreateMenu("File",200,MyWindow)
  CreateMenu("Exit",210,FileMenu,KEY_F4,MODIFIER_OPTION)
  CreateMenu("Open",220,FileMenu)

  Local ToolMenu:TGadget=CreateMenu("AnimTool",300,MyWindow)
  CreateMenu("Toggle Animation",310,ToolMenu)
  CreateMenu("Clear Animation",320,ToolMenu)
  CreateMenu("Cut",330,ToolMenu)
  PasteMenuAfter:TGadget=CreateMenu("Paste After",340,ToolMenu)
  PasteMenuBefore:TGadget=CreateMenu("Before",350,ToolMenu)

  DisableMenu(PasteMenuAfter)
  DisableMenu(PasteMenuBefore)
 
  UpdateWindowMenu MyWindow

End Function


Function Process_MenuActionEvent

We now have more cases to handle in this function.
    Function Process_MenuActionEvent:Int()

 Select EventData()

     Case 210 'Exit or ALT-F4
       End

     Case 220 'File Open
       LoadCanvasImage("")

    Case 310    'Toggle Animation
        AnimFlag= True ~ AnimFlag
       
    Case 320 ;AnimClearAll
    Case 330 ;AnimListPtr=AnimCut()
    Case 340 ;AnimPaste(0)  'Paste After
    Case 350 ;AnimPaste(1)  'Paste Before

  End Select

End Function



Function Process_MouseDownEvent()

When we receive a mouse click from Canvas3 (Animation Strip), check that if it was a right click event then popup our menu

            Case Canvas3 'From Animstrip
            Select EventData()
                Case MOUSE_LEFT
                       SelectFrame()
                Case MOUSE_RIGHT
                    PopupWindowMenu MyWindow, ToolMenu

               End Select
            RedrawGadget(Canvas3)

Function AnimClearAll

Clear everything on the Animation Strip after confirmation. Notice that since we are using a TList object, we simply clear the list to clear our strip
    Function AnimClearAll:Int()

  Local YES:Int=Confirm("Delete All?")
  If YES Then
     ClearList(AnimList)
     AnimListPtr=Null
  EndIf

End Function

Function AnimCut

This function returns a TLink object back to AnimListPtr as after we have cut an image the pointer linking to this image would have changed as well. The cut operation is simply done via a RemoveLink function call.

The cut image is transferred to a clipboard which is also implemented as a list. I could have used just a TPixmap variable but a list also works.

Notice how we have enabled the PasteMenu items and then issue an UpdateWindowMenu function to refresh our menu status.
    Function AnimCut:TLink()

  If CountList(AnimList)=0 Return Null

  AnimFlag=False
  SelectFrame()
  ClearList(AnimClipBoard)
  Local Tmp:TPixmap=CopyPixmap(TPixmap(AnimListPtr.value()))
  ListAddLast AnimClipBoard,Tmp
  Local TmpLink:TLink=AnimListPtr.NextLink()
 
  If TmpLink=Null Then TmpLink=AnimListPtr.PrevLink() 'Must be last item in list
  RemoveLink(AnimListPtr)

  EnableMenu PasteMenuBefore
  EnableMenu PasteMenuAfter
  UpdateWindowMenu MyWindow 
 
  Return TmpLink

End Function


Function AnimPaste

This is two function in one, for the before and after pasting which differs only on whether we use the InsertAfterLink or InsertBeforeLink methods. Once the pasting is done we clear our clipboard and disables the Paste Menu items.
    Function AnimPaste:Int(Flag:Int=0)

    If CountList(AnimClipboard)=0 Return -1

    Local Tmp:TPixmap=Tpixmap(AnimClipboard.First())
    SelectFrame()
    If AnimList.Count()=0
       ListAddLast AnimList, Tmp
       AnimListPtr=AnimList.FirstLink()
    Else
        If Flag=0 Then
              AnimListPtr:TLink= ..
            AnimList.InsertAfterLink(Tmp:TPixmap, AnimListPtr:TLink)
        Else
            AnimListPtr:TLink= ..
            AnimList.InsertBeforeLink(Tmp:TPixmap, AnimListPtr:TLink)
        EndIf
    EndIf

  ClearList(AnimClipBoard)

  DisableMenu PasteMenuBefore
  DisableMenu PasteMenuAfter
  UpdateWindowMenu MyWindow 

End Function


Download the source here and after running it you should be able to right click on the Animation Strip and perform the menu operations.

   


    
That's all for now. Return to Main Index.