archive-com.com » COM » B » BENRAMSEY.COM

Total: 425

Choose link from "Titles, links and description words view":

Or switch to "Titles and links view".
  • Making a GUI Mess of PHP, by Ben Ramsey
    connect method and you re well on your way to taming the wild beast of events Watch the Fireworks Events Are Firing All Around As I mentioned earlier almost every action within the window of a PHP GTK application has an event or signal associated with it This simple text editor application only deals with five such signals now We could extend the application to include many more features but for brevity s sake and for peace of mind my mind and yours we ll only take a look at these five signals destroy delete event activate clicked and changed Let s first take a look at the delete event signal This signal is fired primarily when the user clicks the X close button of the window Most of the time this terminates the program and it s terminated because the programmer chose to have it terminate using the delete event signal By default delete event returns false This forces delete event to call its default event handler the destroy method which in turn emits the destroy signal By default the destroy signal calls Gtk main quit to safely close the application Thus the example in Listing 1 is a bit redundant I don t need to explicitly tell delete event to return false nor do I need to tell the destroy signal to call Gtk main quit The program would work exactly the same if I left out these two lines However these illustrate an important concept and that is the default behavior can be overridden and the programmer can choose to modify how the program terminates by displaying shutdown messages or other interesting functionality such as returning true on delete event and forever trapping a user in the program no matter how often they click that X they can never close it that way Another signal activate fires when a user chooses a menu item from the drop down menus We ll explore the menus later on but for now take a look at Listing 2 Almost every menu item connects activate to some sort of action These actions all have equivalent functions found in Listing 4 A similar signal is the clicked signal which fires when a user clicks a button Finally the last signal that we ll wrangle is the changed signal This signal fires when well something changes Specifically it fires when text is changed in an editable region see Listing 3 This will come into play later when the pieces of the text editor start coming together And Tonight s Featured Menu Item Is We ve explored the window We ve examined the concepts of signals or events and connecting actions to them Now let s start putting these concepts into play as we move on to take a look at what s on the menu While Listing 2 seems much longer than Listing 1 it s not any more difficult to understand In fact most of the code is replicated several times over throughout the listing so there are only a few lines we need to examine Listing 2 The menu source pedit menu php php Application menu bar menu new GtkMenuBar box pack start menu false false File menu file new GtkMenuItem File menu append file file menu new GtkMenu new new GtkMenuItem New new connect activate new file file menu append new open new GtkMenuItem Open open connect activate file open dialog file menu append open separator new GtkMenuItem separator set sensitive false file menu append separator save new GtkMenuItem Save save connect activate save file file menu append save separator new GtkMenuItem separator set sensitive false file menu append separator close new GtkMenuItem Close Quit close connect object activate array Gtk main quit file menu append close file set submenu file menu Edit menu edit new GtkMenuItem Edit menu append edit edit menu new GtkMenu cut new GtkMenuItem Cut cut connect activate cut text edit menu append cut copy new GtkMenuItem Copy copy connect activate copy text edit menu append copy paste new GtkMenuItem Paste paste connect activate paste text edit menu append paste separator new GtkMenuItem separator set sensitive false edit menu append separator select all new GtkMenuItem Select All select all connect activate select all text edit menu append select all edit set submenu edit menu The first line of the listing creates a reference to a menu bar object easily just like it was to create the window earlier Storing that reference to the menu variable allows us to take on as many menu items as needed with ease The first few lines under the File menu section illustrate this file new GtkMenuItem File menu append file And there it is I ve created a menu item named File and stored to file To add this item to the menu bar just use the append method Menu items will appear on the menu in the order in which you append them Menus are simple to grasp They have three main components GtkMenuBar GtkMenuItem and GtkMenu The menu bar is the top level component To that we add menu items such as the File item later on in the code you ll notice that we append an Edit menu as well For each of these menu items we also create a GtkMenu object for example the one referenced by file menu and append still more items to it The GtkMenu object acts as a container for more items it forms the basis of what will become our drop down menu The following code illustrates the creation of our drop down menu container object and the first menu item in the object Notice again the use of append to attach individual items to the menu file menu new GtkMenu new new GtkMenuItem New new connect activate new file file menu append new So now we have a drop down menu named File and its first item is New The New menu choice has an activate signal that is fired when a user chooses or clicks on this menu item Note what happens when activate fires it calls the new file function which is a user defined function in Listing 4 The activate signal is not the only one allowed to menu items but it is the only one we will deal with for now Once all the items have been created and appended to the menu object file menu all that s left is to set the menu as a submenu of file This is done with the set submenu method as shown here file set submenu file menu In the end you ll have two drop down menus that appear similar to Figures 2 and 3 Fig 2 The file menu Fig 3 The edit menu Notice how I ve added several separators to break up the menu items These separators aren t created by any special method Create them with a GtkMenuItem that has no label and set their sensitivity to false with set sensitive This ensures that the user cannot interact with the item it s for decoration only Serving Up Some Delectable Text By now our text editor application is shaping up We have a window and we have our menus but we re still missing one of the most important features and that is the editable text field Figure 1 shows what our text area should look like Listing 3 shows how to make it work Listing 3 The text area source pedit textpad php php text area new GtkHBox box pack start text area default font Gdk font load courier medium r normal 140 m iso8859 1 textpad style new GtkStyle textpad style font default font Editable text field textpad new GtkText textpad set editable true textpad set style textpad style textpad set line wrap false textpad set word wrap false textpad connect changed text changed text area pack start textpad Vertical scrollbar textpad vadj textpad vadj textpad vscrollbar new GtkVScrollbar textpad vadj text area pack end textpad vscrollbar false Again as with the earlier parts of our application the window and the menus there is nothing entirely complex about the text area We ll start by creating a reference to a GtkText object textpad new GtkText Next we ll play around with a few settings Of course we want to set it editable It would defeat the purpose of a text editor if the text weren t editable textpad set editable true Another setting I use is set style This is an aesthetic setting that can be discarded without hurting the application However I have included it in order to set the font to a mono spaced typeface for easier reading To do this pass a GtkStyle object to set style However before passing a GtkStyle object we must first create one default font Gdk font load courier medium r normal 140 m iso8859 1 textpad style new GtkStyle textpad style font default font This block of code is a bit out of scope for our discussion but in short Gdk font load loads a font which must be provided in X Logical Font Description XLFD format The code provided should work on both Windows and Unix type systems Save the loaded font to a GtkStyle object and then pass it to the editor window with set style The two other settings set line wrap and set word wrap have been explicitly turned off by setting them to false Native support for text wrapping is awkward and it is suggested that PHP s wordwrap be used to handle wrapping though I do not use it in this example Perhaps future versions of PHP GTK will overcome the awkwardness and provide better support for wrapping Finally notice that like many of the other elements of this application I use connect on the textpad to connect the changed signal fired when the user changes text to the text changed function in Listing 4 The text changed function basically sets a text changed variable to true for testing purposes during other actions such as when the user decides to create a new file or open an existing file before saving the working document If text changed is set to true then we know to display an appropriate message so the user doesn t lose his changes Futhermore text changed provides another aesthetic feature by modifying the window title with set title to reflect changes made to the text I want to take this time to look at a few of the functions in Listing 4 that deal specifically with the GtkText object In particular these functions are new file and open file as well as cut text copy text paste text and select all text There is no ambiguity in these names they do what they say and references to them may be found in Listing 2 where they are connected to specific menu items When loading text into a GtkText object it s always a good idea to call the freeze method first as is illustrated in new file and open file This avoids any unsightly text flashes that the editor field may have while the text is updating and you don t want your editor to go around flashing everyone Another method set point places the cursor at a specific location in the editor field In this case we place it at the beginning so we can use forward delete to clear the field Finally in the case of open file we use the insert method which inserts text into the field and then we call the thaw method to update the display GtkText also has some clipboard features as shown in the cut copy and paste text functions These can be used in conjunction with the select all text function which uses the select region method to select a specific area of text Here we tell it to select everything Listing 4 The functions that make it work pedit functions php php function new file global text changed loaded file textpad w if text changed true save close dialog true else loaded file null textpad freeze textpad set point 0 textpad forward delete textpad get length textpad thaw w set title Untitled function file open dialog global text changed loaded file if text changed true save close dialog else fs new GtkFileSelection Select a File if is null loaded file directory dirname loaded file We must provide a trailing slash so provide the appropriate one for the current system directory strtoupper substr PHP OS 0 3 WIN fs set filename directory ok fs ok button ok connect clicked open file fs ok connect object clicked array fs destroy cancel fs cancel button cancel connect object clicked array fs destroy fs show function save file global text changed loaded file textpad w if is null loaded file bytes file put contents loaded file textpad get chars 0 1 if bytes false save error dialog else text changed false w set title loaded file else file save dialog return true function cut text global textpad textpad cut clipboard function copy text global textpad textpad copy clipboard function paste text global textpad textpad paste clipboard function select all text global textpad textpad select region 0 1 function open file button fs global textpad w loaded file textpad freeze textpad set point 0 textpad forward delete textpad get length textpad insert null null null file get contents fs get filename textpad thaw w set title fs get filename loaded file fs get filename return true function file save dialog fs new GtkFileSelection Save File ok fs ok button ok connect clicked file save from dialog fs ok connect object clicked array fs destroy cancel fs cancel button cancel connect object clicked array fs destroy fs show function file save from dialog button fs global w loaded file w set title fs get filename loaded file fs get filename save file return true function text changed global w text changed loaded file text changed true if is null loaded file w set title loaded file else w set title Untitled function save close dialog new file false dialog new GtkDialog dialog set title Save before closing dialog set usize 300 125 dialog set position GTK WIN POS CENTER dialog connect delete event create function return false dialog vbox dialog vbox dialog action area dialog action area label new GtkLabel Would you like to save the current file first dialog vbox pack start label label show ok new GtkButton Ok if new file ok connect clicked create function save file new file else ok connect clicked create function save file file open dialog ok connect object clicked array dialog destroy dialog action area pack start ok ok show no new GtkButton No if new file no connect clicked create function global text changed text changed false new file else no connect clicked create function global text changed text changed false file open dialog no connect object clicked array dialog destroy dialog action area pack start no no show cancel new GtkButton Cancel cancel connect object clicked array dialog destroy dialog action area pack start cancel cancel show dialog show function save error dialog dialog new GtkDialog dialog set title Error saving dialog set usize 300 125 dialog connect delete event create function return false dialog vbox dialog vbox dialog action area dialog action area label new GtkLabel Unable to open file for saving dialog vbox pack start label label show ok new GtkButton Ok ok connect object clicked array dialog destroy dialog action area pack start ok ok show dialog show This function exists in PHP 5 if function exists file put contents function file put contents filename data if h fopen filename w false return false if bytes fwrite h data false return false fclose h return bytes Presentation Is Everything Now that the application has been laid bare and exposed we need to take a look at a few methods I have skipped over that are important in pasting these individuals parts the window menu and text area together First let s journey back to Listing 1 and look at the GtkVBox object box new GtkVBox This creates a box in which the elements are arranged vertically That is each time an item is added to box it is stacked below the previously added item In our application we want the menu bar to appear at the top with the editor field below it thus we use GtkVBox as a container for these elements To add an item to a GtkVBox object use pack start or pack end box pack start menu false false This line from Listing 2 illustrates the pack start method pack start adds items from the top and left of the container It s name suggests that it packs them so one would assume that each item gets placed on top of the other like packing a box However the opposite is true Think of it like packing a box upside down in a weightless environment Each item packed in goes below the previously added item On the other hand pack end works like packing a box down here on Earth The first item in goes on the bottom or at the end Each subsequent item is placed on top When using pack start or pack end the first parameter is the object you wish to pack In the previous example we added the menu object to the box In the next example from Listing 3 a new box container is needed to hold both the editor field and a vertical scrollbar Since the scrollbar needs to be horizontally positioned to the right of the field we ll use GtkHBox for a horizontal box container text area new GtkHBox box pack start text area This creates our new horizonal box text area and adds it to the main box below the menu bar Now we can safely add textpad and textpad vscrollbar

    Original URL path: https://benramsey.com/articles/making-a-gui-mess-of-php/ (2016-04-26)
    Open archived version from archive


  • HTTP Is Dead. Long Live HTTP/2!, by Ben Ramsey
    Joind in Feedback Request for Comments RFC 2616 reigned supreme as the specification for the hypertext transfer protocol HTTP for fifteen years Now it s been obsoleted by a handful of new RFCs and HTTP 2 is a reality In this talk we ll take a look at the new RFCs discuss the differences and clarifications they make and take a look at what s new in HTTP 2 and

    Original URL path: https://benramsey.com/talks/2016/04/lonestarphp-http2/ (2016-04-26)
    Open archived version from archive

  • PHP Foundations, by Ben Ramsey
    newer or a Linux variant we will assume Ubuntu Desktop for the purposes of this course The ability to navigate the filesystem including creating and deleting files and folders The ability to download and install software on your computer Basic knowledge of writing html and css The syllabus is divided into 4 sections Part 1 Programming Logic We ll cover the very basics of programming variables logic conditionals and looping functions We ll then move into data types and math and type juggling we ll end with more complex data structures common to PHP objects and arrays Part 2 The Internet We ll cover what HTTP is and some basic theory how requests and responses work an overview of HTML CSS and a very small section on Javascript and the concepts behind server vs client side programming Then we ll move into basic http verbs get and post and work with forms Part 3 SQL and Crud using sqlite we ll go over teh basics of sql and how it works and learn about insert update delete and select Then we ll tie together our new internet forms knowledge with our new sql knowledge Part 4 Code Design We ll

    Original URL path: https://benramsey.com/talks/2016/04/lonestarphp-foundations/ (2016-04-26)
    Open archived version from archive

  • HTTP Is Dead. Long Live HTTP/2!, by Ben Ramsey
    2 é uma realidade Nesta palestra vamos dar uma olhada nas novas RFCs discutir as diferenças e esclarecimentos que eles fazem e dar uma olhada no que há de novo na HTTP 2 eo que isso significa para você English Request for Comments RFC 2616 reigned supreme as the specification for the hypertext transfer protocol HTTP for fifteen years Now it s been obsoleted by a handful of new RFCs

    Original URL path: https://benramsey.com/talks/2016/03/phpexperience-http2/ (2016-04-26)
    Open archived version from archive

  • Mocking With Mockery, by Ben Ramsey
    be used with any unit testing framework It is a flexible and human readable domain specific language DSL for mocking objects in unit tests With it we can create some pretty powerful tests and even test legacy code containing hard dependencies In this talk I ll show how to get started with Mockery I ll start with some basic mocking techniques and then move on to cover more advanced topics

    Original URL path: https://benramsey.com/talks/2016/03/midwestphp-mockery/ (2016-04-26)
    Open archived version from archive

  • Building Great APIs, by Ben Ramsey
    do we put these concepts into practice and build great APIs How RESTful do we need to be and where do we draw the line with a pragmatic approach to ship code and make our users happy In this talk I ll show how to build APIs that put into practice the concepts of REST while showing that it s okay to bend or break the rules Along the way

    Original URL path: https://benramsey.com/talks/2016/03/midwestphp-great-apis/ (2016-04-26)
    Open archived version from archive

  • Mocking With Mockery, by Ben Ramsey
    framework that may be used with any unit testing framework It is a flexible and human readable domain specific language DSL for mocking objects in unit tests With it we can create some pretty powerful tests and even test legacy code containing hard dependencies In this talk I ll show how to get started with Mockery I ll start with some basic mocking techniques and then move on to cover

    Original URL path: https://benramsey.com/talks/2016/01/skiphp-mockery/ (2016-04-26)
    Open archived version from archive

  • HTTP is Dead. Long Live HTTP/2!, by Ben Ramsey
    Watch Presentation Joind in Feedback Request for Comments RFC 2616 reigned supreme as the specification for the hypertext transfer protocol HTTP for fifteen years Now it s been obsoleted by a handful of new RFCs and HTTP 2 is a reality In this talk we ll take a look at the new RFCs discuss the differences and clarifications they make and take a look at what s new in HTTP

    Original URL path: https://benramsey.com/talks/2016/01/skiphp-http2/ (2016-04-26)
    Open archived version from archive



  •