Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added A sub-menu to change the types of Font and Font-Styles #25

Merged
merged 16 commits into from
Mar 15, 2023
161 changes: 148 additions & 13 deletions DeskNoteView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
*/


#include "DeskNoteView.h"

#include <AboutWindow.h>
#include <Catalog.h>
#include <Invoker.h>
Expand All @@ -21,9 +19,13 @@
#include <SupportKit.h>


#include "DeskNoteView.h"


humdingerb marked this conversation as resolved.
Show resolved Hide resolved
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "View"


const float kWidgetSize = 7;


Expand Down Expand Up @@ -131,15 +133,17 @@ DeskNoteView::Draw(BRect rct)
void
DeskNoteView::MessageReceived(BMessage* msg)
{
BAlert* alert;
BAlert *alert;
BRect windSize, ourSize;
BMessenger* messenger;
BFont currentFont;
font_family currentFamily;
font_style currentStyle;
BMessenger *messenger;
BMessage *message;
BMenuItem *item;
BMenu *menu;
uint16 currentFace;
const rgb_color* bckgrnd;
const rgb_color *bckgrnd;
ssize_t size;
const char *fontFamily, *fontStyle;
void *ptr;

switch (msg->what) {
case B_ABOUT_REQUESTED:
Expand Down Expand Up @@ -186,6 +190,39 @@ DeskNoteView::MessageReceived(BMessage* msg)
}
break;
}
// Font type
case FONT_FAMILY:
{
fontFamily = NULL;
fontStyle = NULL;

// Setting the font family
msg->FindPointer("source", &ptr);
fCurrentFont = static_cast <BMenuItem*>(ptr);
fontFamily = fCurrentFont->Label();
SetFontStyle(fontFamily, fontStyle);
}
break;
// Font style
case FONT_STYLE:
{
fontFamily = NULL;
fontStyle = NULL;

// Setting the font style
msg->FindPointer("source", &ptr);
item = static_cast <BMenuItem*>(ptr);
fontStyle = item->Label();
menu = item->Menu();

if (menu != NULL) {
fCurrentFont = menu->Superitem();
if (fCurrentFont != NULL)
fontFamily = fCurrentFont->Label();
}
SetFontStyle(fontFamily, fontStyle);
}
break;
default:
BView::MessageReceived(msg);
}
Expand Down Expand Up @@ -227,11 +264,65 @@ DeskNoteView::MouseDown(BPoint point)
if (resizeThread > 0)
resume_thread(resizeThread);

} else if (mouseButtons == B_SECONDARY_MOUSE_BUTTON)
}
else if (mouseButtons == B_SECONDARY_MOUSE_BUTTON)
_ShowContextMenu(mousePoint);
}


void
DeskNoteView::_BuildStyleMenu(BMenu* menu)
{
//variables
font_family plainFamily, family;
font_style plainStyle, style;
BMenuItem* menuItem = NULL;
int32 numFamilies, numStyles;
BMenu* fontMenu;
uint32 flags;

if (menu == NULL)
return;

//Font Menu
fCurrentFont = 0;

be_plain_font->GetFamilyAndStyle(&plainFamily,&plainStyle);

// Taking the number of font families
numFamilies = count_font_families();
for (int32 i = 0; i < numFamilies; i++) {
// Getting the font families
if (get_font_family(i, &family) == B_OK) {
fontMenu = new BMenu(family);
fontMenu->SetRadioMode(true); // I can set only one item as "in use"
menuItem = new BMenuItem(fontMenu, new BMessage(FONT_FAMILY));
menuItem->SetTarget(this);
menu->AddItem(menuItem);

if (!strcmp(plainFamily,family)) {
//menuItem->SetMarked (true);
fCurrentFont = menuItem;
}
//Number of styles of that font family
numStyles = count_font_styles(family);

for (int32 j = 0; j < numStyles; j++) {
if (get_font_style(family,j,&style,&flags)==B_OK) {
menuItem = new BMenuItem(style, new BMessage(FONT_STYLE));
menuItem->SetTarget(this);
fontMenu->AddItem(menuItem);

//if (!strcmp (plainStyle, style))
//menuItem->SetMarked(true);

}
}
}
}
}


void
DeskNoteView::_BuildColorMenu(BMenu* menu)
{
Expand Down Expand Up @@ -287,7 +378,8 @@ DeskNoteView::_SetColors()
foreground.green = 11;
foreground.blue = 11;
widgetcolour = tint_color(background, B_DARKEN_1_TINT);
} else {
}
else {
foreground.red = 244;
foreground.green = 244;
foreground.blue = 244;
Expand Down Expand Up @@ -320,11 +412,15 @@ DeskNoteView::_ShowContextMenu(BPoint where)
bool canEdit = textView->IsEditable();
int32 length = textView->TextLength();

BPopUpMenu* menu = new BPopUpMenu(B_EMPTY_STRING, false, false);
BPopUpMenu* menu = new BPopUpMenu(B_EMPTY_STRING, false, false,B_ITEMS_IN_COLUMN);

colorMenu = new BMenu(B_TRANSLATE("Color"), 0, 0);
_BuildColorMenu(colorMenu);

styleMenu = new BMenu(B_TRANSLATE("Font"));
styleMenu->SetRadioMode(true);
_BuildStyleMenu(styleMenu);

BLayoutBuilder::Menu<>(menu)
.AddItem(B_TRANSLATE("Undo"), B_UNDO)
.SetEnabled(canEdit && isUndo)
Expand All @@ -340,9 +436,12 @@ DeskNoteView::_ShowContextMenu(BPoint where)
.AddSeparator()
.AddItem(B_TRANSLATE("Select all"), B_SELECT_ALL)
.SetEnabled(!(start == 0 && finish == length))
// custom menu
.AddSeparator()
// custom menu
.AddItem(colorMenu)
.AddItem(styleMenu)
.AddSeparator()
// About section
.AddItem(B_TRANSLATE("About DeskNotes" B_UTF8_ELLIPSIS),
new BMessage(B_ABOUT_REQUESTED));

Expand Down Expand Up @@ -371,6 +470,41 @@ DeskNoteView::SaveNote(BMessage* msg)
}


// Function for the changes in the "type of font"
void DeskNoteView::SetFontStyle(const char* fontFamily, const char* fontStyle)
{
// Variables
BMenuItem *superItem;
BMenuItem *menuItem;
BFont font;
font_family oldFamily;
font_style oldStyle;
uint32 sameProperties;
rgb_color sameColor;
BMenuItem *oldItem;

textView->GetFontAndColor(&font, &sameProperties, &sameColor);
// Copying the current font family and font style
font.GetFamilyAndStyle(&oldFamily, &oldStyle);

if (strcmp(oldFamily, fontFamily)) {
oldItem = styleMenu->FindItem(oldFamily);

if (oldItem != NULL)
// Removing the check
oldItem->SetMarked(false);
}

font.SetFamilyAndStyle(fontFamily, fontStyle);
textView->SetFontAndColor(&font);

superItem = styleMenu->FindItem(fontFamily);

if (superItem != NULL)
superItem->SetMarked(true); // Check the one that was selected
}


void
DeskNoteView::RestoreNote(BMessage* msg)
{
Expand Down Expand Up @@ -426,7 +560,8 @@ DeskNoteView::ResizeViewMethod(void* data)
theView->Window()->ResizeTo(x, y);
theView->Window()->Unlock();
snooze(20 * 1000);
} while (buttons);
}
while (buttons);

return 0;
}
14 changes: 14 additions & 0 deletions DeskNoteView.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
#include "DeskNoteTextView.h"



#define DN_LAUNCH 'dnLN'
#define DN_COLOR 'dnCL'
#define FONT_FAMILY 'fntf'
#define FONT_STYLE 'ftst'

const rgb_color palette[] = {
{ 255, 221, 191, 255 }, // lightest red
Expand Down Expand Up @@ -73,21 +76,32 @@ class DeskNoteView : public BView {

static const char defaultText[];
static const char aboutText[];

void SetFontStyle(const char* fontFamily,
const char *fontStyle);




private:
void _BuildColorMenu(BMenu* menu);
void _BuildStyleMenu(BMenu* menu);
void _SetColors();
void _ShowContextMenu(BPoint where);
bool WeAreAReplicant;
DeskNoteTextView* textView;
BRect ourSize;
BMenu* colorMenu;
BMenu* styleMenu;
BMenu* fFontMenu;
BMenuItem* fCurrentFont;
BMessage* orginalSettings;
BPopUpMenu* popupMenu;
rgb_color background;
rgb_color foreground;
rgb_color widgetcolour;
BDragger* dragger;

};

#endif // _DESKNOTEVIEW_H