NO

Author Topic: Best GUI lib to use with Pelles?  (Read 21845 times)

Cnoob

  • Guest
Best GUI lib to use with Pelles?
« on: April 28, 2015, 12:05:58 PM »
Hi

Can anyone advise as to the easiest (for a noob) GUI lib to use with Pelles?

T.I.A.


Offline jj2007

  • Member
  • *
  • Posts: 536
Re: Best GUI lib to use with Pelles?
« Reply #1 on: April 28, 2015, 02:47:45 PM »
Plain WinAPI?

Code: [Select]
#include <windows.h>
#include <stdio.h> // needed for fopen
#pragma comment(linker, "comdlg32.lib") // needed for GetOpenFileName
#pragma comment(linker, "gdi32.lib") // needed for GetStockObject
#pragma warn(disable:2216)    // retval never used
#pragma warn(disable:2118)    // parameter .. is not referenced

#define winX 200 // main window x, y, width and height
#define winY 200
#define winW 700
#define winH 700

HWND hEdit; // global handle to the edit control
HINSTANCE hInst; // global instance handle

void ReadFile2EditControl(char* filename) { // does what its name says
  FILE *ofp = fopen(filename, "rb");
  if (ofp) {
fseek(ofp, 0, SEEK_END); // GetFileSize() won't work, so let's do it the archaic C way
int fLen=ftell(ofp); // position at end equals length of file
fseek(ofp, 0, SEEK_SET); // go back to start
PSTR pstrBuffer=malloc(fLen+1); // buffer includes a zero delimiter
if (pstrBuffer) {
   fread (pstrBuffer, 1, fLen, ofp); // read content into buffer
   pstrBuffer[fLen] = '\0'; // set the delimiter explicitly
   SetWindowText (hEdit, pstrBuffer) ; // put text into the edit control
   free (pstrBuffer) ;
}
  fclose (ofp) ;
  }
}

LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  switch(msg) {
case WM_CREATE:
{
int htMenu=0;
#define mOpen 101
#define mSave 102
#define hasmenu 1 // 0 = no menu
#if hasmenu
HMENU hMenu, hSubMenu;
hMenu=CreateMenu(); // create the main menu
hSubMenu=CreateMenu(); // create a sub-menu
AppendMenu(hMenu, MF_POPUP, (int)hSubMenu, "&File"); // add it to the main menu
AppendMenu(hSubMenu, MF_STRING, mOpen, "&Open"); // and add
AppendMenu(hSubMenu, MF_STRING, mSave, "&Save"); // two items
SetMenu(hwnd, hMenu); // attach menu to main window
htMenu=GetSystemMetrics(SM_CYMENU);
#endif
hEdit=CreateWindowEx(WS_EX_CLIENTEDGE, "edit", NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | ES_MULTILINE,
2, 1, winW-12, winH-htMenu-33, hwnd, (HMENU)103, hInst, NULL); // we have added an edit control
SendMessage(hEdit, EM_LIMITTEXT, 0, 0); // no limit
SetFocus(hEdit); // make sure you can start typing right away
SendMessage(hEdit, WM_SETFONT, (int)GetStockObject(ANSI_FIXED_FONT), 0); // SYSTEM_FIXED_FONT is bold and larger
}
break;

case WM_COMMAND:
if ((short)wParam==mOpen)
{
char fileName[MAX_PATH]="\x0000";

OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter ="Sources and Headers\0*.c;*.h;*.as?\0\0";
ofn.lpstrFile = fileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;

if (GetOpenFileName(&ofn)) ReadFile2EditControl(fileName);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
  return DefWindowProc(hwnd, msg, wParam, lParam);
}

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
  MSG msg;
  WNDCLASS wc;
  HWND hwnd;
  wc.style = CS_HREDRAW | CS_VREDRAW;
  wc.cbClsExtra = 0;
  wc.cbWndExtra = 0;
  wc.lpszClassName = "MyClass";
  wc.hInstance = hInstance;
  wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE+1);
  wc.lpszMenuName = NULL;
  wc.lpfnWndProc = WndProc;
  wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  RegisterClass(&wc);
  hInst = hInstance;
  hwnd = CreateWindow(wc.lpszClassName, "Hello World",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
winX, winY, winW, winH,
NULL, NULL, hInstance, NULL);
  while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
  return (int) msg.wParam;
}

Cnoob

  • Guest
Re: Best GUI lib to use with Pelles?
« Reply #2 on: April 28, 2015, 04:08:58 PM »
Hi jj2007

You must be joking, although I really appreciate you taking the time to reply, look at that code from a noob's perspective.
There is just too much when compared to something like GTK+

Code: [Select]
#include <gtk/gtk.h>

int main( int argc, char *argv[])
{
  GtkWidget *window;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_widget_show(window);

  gtk_main();

  return 0;
}

Which creates:



I understand that the example in GTK+ is much simpler than the one you provided but it is much easier than the standard API, surely you agree?
Since posting my original question, continued to look at all the GUI libraries available and GTK+ looks about the simplest, just not sure how well it's supported with Pelles.

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: Best GUI lib to use with Pelles?
« Reply #3 on: April 28, 2015, 10:01:22 PM »
I understand that the example in GTK+ is much simpler than the one you provided

Indeed. Add an edit control, a menu that works, a file open dialog and the ability to read the content of the chosen file into the edit control.
Then compare the two codes, and the sizes of the executables.

Cnoob

  • Guest
Re: Best GUI lib to use with Pelles?
« Reply #4 on: April 29, 2015, 12:44:52 AM »
I have no doubt that the resulting file size will be greater but it's a trade-off between simplicity and file size, within reason of course.
Have a look at this example: ( from here http://zetcode.com/tutorials/gtktutorial/gtkwidgetsII/ )

Code: [Select]
#include <gtk/gtk.h>

int main( int argc, char *argv[])
{

  GtkWidget *window;
  GtkWidget *label1;
  GtkWidget *label2;
  GtkWidget *hseparator;
  GtkWidget *vbox;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_title(GTK_WINDOW(window), "GtkHSeparator");
  gtk_window_set_resizable(GTK_WINDOW(window), FALSE);

  gtk_container_set_border_width(GTK_CONTAINER(window), 20);

  label1 = gtk_label_new("Zinc is a moderately reactive, blue gray metal \
that tarnishes in moist air and burns in air with a bright bluish-green flame,\
giving off fumes of zinc oxide. It reacts with acids, alkalis and other non-metals.\
If not completely pure, zinc reacts with dilute acids to release hydrogen.");

  gtk_label_set_line_wrap(GTK_LABEL(label1), TRUE);

  label2 = gtk_label_new("Copper is an essential trace nutrient to all high \
plants and animals. In animals, including humans, it is found primarily in \
the bloodstream, as a co-factor in various enzymes, and in copper-based pigments. \
However, in sufficient amounts, copper can be poisonous and even fatal to organisms.");

  gtk_label_set_line_wrap(GTK_LABEL(label2), TRUE);

  vbox = gtk_vbox_new(FALSE, 10);
  gtk_container_add(GTK_CONTAINER(window), vbox);

  hseparator = gtk_hseparator_new();

  gtk_box_pack_start(GTK_BOX(vbox), label1, FALSE, TRUE, 0);
  gtk_box_pack_start(GTK_BOX(vbox), hseparator, FALSE, TRUE, 10);
  gtk_box_pack_start(GTK_BOX(vbox), label2, FALSE, TRUE, 0);


  g_signal_connect_swapped(G_OBJECT(window), "destroy",
        G_CALLBACK(gtk_main_quit), G_OBJECT(window));

  gtk_widget_show_all(window);

  gtk_main();

  return 0;
}



How does the straight WinAPI method compare?

I don't mean it in a GTK is better way, I'm just looking for the simplest way.
Since both the GTK and API methods will have plenty of a certain amount of code as standard (per widget/dialog) maybe putting some/most of it
in a macro or an include file is one way to go?

For example, take the line:

Code: [Select]
gtk_container_set_border_width(GTK_CONTAINER(window), 20);

Could become:

Code: [Select]
#define SetBorderWidth(w)  gtk_container_set_border_width(GTK_CONTAINER(window), (w))

So we only need:

Code: [Select]
SetBorderWidth(20);

Could do this with lots more and save the macros in a separate include file.
« Last Edit: April 29, 2015, 12:48:52 AM by Cnoob »

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: Best GUI lib to use with Pelles?
« Reply #5 on: April 29, 2015, 11:32:08 AM »
I think that the GUI programming as the language choice is a personal preference, you can discuss for months, but there will always be good points for one and the other side  ;D
Don't forget that the main reason for GTK, and similar, anyway is the necessity to have a GUI system that can be used on different platforms (the same GUI desing will work on Linux or Windows or else).  :)
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

Cnoob

  • Guest
Re: Best GUI lib to use with Pelles?
« Reply #6 on: April 29, 2015, 12:26:54 PM »
Hi frankie

You are right in what you say and people will have their own choices about what they use.
As for me, I don't think that GTK is fantastic, it just looks the easiest to start with especially when combined with simplifying macros.
As JJ2007 wrote, there will be an overhead in file size and possibly even speed but that is the price to pay for something simpler.
By simpler I mean easier to start with, certainly not simpler as far as all the extra dependencies go.

Cnoob

  • Guest
Re: Best GUI lib to use with Pelles?
« Reply #7 on: April 29, 2015, 09:33:25 PM »
UPDATE:

Compiled the code in reply #4 and it came to 19KB, very acceptable.

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: Best GUI lib to use with Pelles?
« Reply #8 on: April 30, 2015, 03:34:47 AM »
Compiled the code in reply #4 and it came to 19KB, very acceptable.

That is indeed not much. Rumours say a "small" QT executable is about 10MB, so 19k is nothing compared to QT.

As Frankie wrote, the main reason for such packages is cross-platform compatibility. If being able to run it on Linux (1.5% of the market) and MacOS (8%) is an argument, ok, consider GTK.

If you are targeting Windows anyway, consider that the pure WinAPI example I posted does quite a bit of user interaction, with the menu and the file dialog. The gtk example just paints a static text. Here is one that does roughly the same:
Code: [Select]
GuiParas equ "Hello jj2007", x200, y200, w360, h80
include \masm32\MasmBasic\Res\MbGui.asm
Event Paint
  GuiText 3, 3, "Zinc is a moderately reactive, blue gray metal"
GuiEnd
A full-fledged Windows GUI application in five lines that displays a text but does otherwise nothing (it's not even C, it's assembler).

Complexity comes in when you start interacting with the user. How do you handle a mouse click, or keyboard input? How do you handle menus? What happens if you have a button in the upper right corner, and the user decides to resize the window?

One point to take into account is documentation. The Windows message loop may look a bit complicated at first sight, but when you google e.g. for WM_LBUTTONDOWN, you'll get 150,000+ hits, most of them code examples. Try to find the equivalent for gtk... ok, here is the official gtk tutorial for event handling 8)
« Last Edit: April 30, 2015, 03:38:59 AM by jj2007 »

Cnoob

  • Guest
Re: Best GUI lib to use with Pelles?
« Reply #9 on: April 30, 2015, 09:38:43 AM »
Hi jj2007

Without a doubt that there will be much more information available on the WinAPI then that for GTK.
The problem with the API method, is not that each line is complicated, it's the sheer number that is required.
From a noob's perspective, it's complicated enough trying to learn C without the extra need to remember
the whole sequence of commands and functions required to draw a simple text screen in a GUI.

This is where (IMO) GTK comes in, it simplifies things and one can even take it one step further and use macros.
However, it must be said that it's important not to over use macros and make it decidedly not very C like.
After all the whole idea is to learn C, so the GUI should be a customizable and easy layer on top of that whilst maintaining the C syntax as much as possible.

As for QT, uuurggh, I'm sure it has it's benefits but I see it as a clumsy brontosaurus trying to ride a mono-cycle
Have also heard that Nokia contributed code to it, well if that code is anything like they used in their phones (prior to the microsoft take-over), that alone
should be reason enough to stay well clear of QT at all costs.

I have pretty much decided that despite it's disadvantages, GTK is the way to go, or at least in the beginning as when used with macros, the whole GUI layer thing
becomes much more palatable.
This route has two other advantages, firstly as I create the macros, I'm learning C (and the joys of the pre-processesor  ;D ) and if ever need to do GUIs in Linux,
GTK will already be familiar territory so to speak.
« Last Edit: April 30, 2015, 09:43:37 AM by Cnoob »

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: Best GUI lib to use with Pelles?
« Reply #10 on: April 30, 2015, 02:43:48 PM »
As a side consideration only.
If you want learn C just produce console programs.
If you want learn Win programming use WinAPI, masking the more or less complex mechanisms that are behind using a surrogate is not effective: What you need to learn is how things works, how messages rotates, how they are handled, etc.  ;)
« Last Edit: May 05, 2015, 08:51:24 AM by frankie »
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

dizergin

  • Guest
Re: Best GUI lib to use with Pelles?
« Reply #11 on: May 04, 2015, 10:56:15 PM »
 :D or just to  make up complex  gui apps with minimum efforts.... if so - you probably should change the langauge...C is not proper choice for

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Best GUI lib to use with Pelles?
« Reply #12 on: May 05, 2015, 05:29:27 AM »
From a noob's perspective, it's complicated enough trying to learn C without the extra need to remember
the whole sequence of commands and functions required to draw a simple text screen in a GUI.
That's the core of the problem right here. Don't take two steps at once, do one at a time. First learn to properly code in C, understanding how pointers work, etc...
And only once you got the gist on that one, then start programming for a GUI. That's a complete different issue all together, regardless of the OS. And regardless of what kind of library/framework you are using...

Ralf

dizergin

  • Guest
Re: Best GUI lib to use with Pelles?
« Reply #13 on: May 05, 2015, 09:19:03 AM »

And only once you got the gist on that one, then start programming for a GUI. That's a complete different issue all together, regardless of the OS. And regardless of what kind of library/framework you are using...

Ralf
Even so - C usage for GUI programming-POOR choice regardless any framework (exept some simple cases or very special requirements upon).

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: Best GUI lib to use with Pelles?
« Reply #14 on: May 05, 2015, 02:14:05 PM »
Even so - C usage for GUI programming-POOR choice regardless any framework (exept some simple cases or very special requirements upon).

Nonsense. The whole Windows API is accessible to C, GUI-wise there is absolutely nothing you couldn't do in C. My personal editor's source is 16k lines, if that is possible in assembler, it certainly works also in C. Give me one example of a GUI feature that can't be implemented in C...