[erlang-questions] wxErlang question 6 - Image display

Joe Armstrong erlang@REDACTED
Tue Jul 11 10:28:40 CEST 2017


To answer my own question - this works but the code has a nasty smell
Is this really how do do things? must I setup a paint handler and
do it this way? Is there a better way??

-module(image_png).

-export([start/0]).

-include_lib("wx/include/wx.hrl").

start() ->
    W = wx:new(),
    Frame = wxFrame:new(W, -1, "PNG image"),
    Panel = wxPanel:new(Frame, [{size,{400,600}}]),
    Vbox = wxBoxSizer:new(?wxVERTICAL),
    wxSizer:add(Vbox, Panel, [{proportion, 1}, {flag, ?wxEXPAND}]),
    Image = wxBitmap:new("frame1.png", [{type, ?wxBITMAP_TYPE_PNG}]),
    F = fun(I, _) -> redraw(Image,I) end,
    wxPanel:connect(Panel, paint, [{callback,F}]),
    wxFrame:show(Frame).

redraw(Image, #wx{obj=Panel}) ->
    DC = wxPaintDC:new(Panel),
    wxDC:drawBitmap(DC,Image,{0,0}).

/Joe

On Sat, Jul 8, 2017 at 3:14 PM, Joe Armstrong <erlang@REDACTED> wrote:
> Images
>
> wxWidgets is breaking the principle of least astonishment
>
> I've been making a load of widgets and adding them to a Vbox
> which arranges them in a vertical stack.
>
> To make a button
>
>     Button = wxButton:new(Panel, Id, [{label,"My New Button"}]),
>     wxSizer:add(Vbox, Button, ...)
>
> To make a text editor
>
>     Button = wxTextCtrl:new(Panel, Id, ...),
>     wxSizer:add(Vbox, Editor, ...)
>
> To make a combo box
>
>      Combo = wxComboBox:new(Panel, Id, ..),
>      wxSizer:add(Vbox, Combo, ...)
>
> and so on.
>
> At this point I began thinking "I'm beginng to understand this stuff" but ...
>
> I thought I'd add an image, so I tried this:
>
>     Image = wxImage:new("image.jpg", []),
>     wxSizer:add(Vbox, Image, ...)
>
> But oh dear - this doesn't work.
>
> I Googled a bit - and it appears I have to mess with paint events and
> graphics contexts and so on.
>
> But why? - why break the principle of least atonishment.
>
> Is it possible to dispay an Image in a Panel *without* handling graphics context
> and paint events?



More information about the erlang-questions mailing list